~zooko/cryptopp/trunk

1 by weidai
Initial revision
1
// lubyrack.h - written and placed in the public domain by Wei Dai
2
3
#ifndef CRYPTOPP_LUBYRACK_H
4
#define CRYPTOPP_LUBYRACK_H
5
6
/** \file */
7
8
#include "simple.h"
9
#include "secblock.h"
10
11
NAMESPACE_BEGIN(CryptoPP)
12
244 by weidai
port to Borland C++Builder 2006
13
template <class T> struct DigestSizeDoubleWorkaround 	// VC60 workaround
14
{
15
	CRYPTOPP_CONSTANT(RESULT = 2*T::DIGESTSIZE)
16
};
1 by weidai
Initial revision
17
173 by weidai
fix documentation, fix PanamaMAC, fix algorithm names
18
//! algorithm info
1 by weidai
Initial revision
19
template <class T>
244 by weidai
port to Borland C++Builder 2006
20
struct LR_Info : public VariableKeyLength<16, 0, 2*(INT_MAX/2), 2>, public FixedBlockSize<DigestSizeDoubleWorkaround<T>::RESULT>
1 by weidai
Initial revision
21
{
22
	static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
23
};
24
25
//! Luby-Rackoff
26
template <class T>
27
class LR : public LR_Info<T>, public BlockCipherDocumentation
28
{
75 by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes
29
	class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<LR_Info<T> >
1 by weidai
Initial revision
30
	{
31
	public:
32
		// VC60 workaround: have to define these functions within class definition
232 by weidai
port to GCC 4, reorganize implementations of SetKey
33
		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
1 by weidai
Initial revision
34
		{
156 by weidai
port to GCC 3.4
35
			this->AssertValidKeyLength(length);
1 by weidai
Initial revision
36
37
			L = length/2;
38
			buffer.New(2*S);
39
			digest.New(S);
40
			key.Assign(userKey, 2*L);
41
		}
42
43
	protected:
244 by weidai
port to Borland C++Builder 2006
44
		CRYPTOPP_CONSTANT(S=T::DIGESTSIZE)
1 by weidai
Initial revision
45
		unsigned int L;	// key length / 2
46
		SecByteBlock key;
47
48
		mutable T hm;
49
		mutable SecByteBlock buffer, digest;
50
	};
51
57 by weidai
add CRYPTOPP_NO_VTABLE
52
	class CRYPTOPP_NO_VTABLE Enc : public Base
1 by weidai
Initial revision
53
	{
54
	public:
55
156 by weidai
port to GCC 3.4
56
#define KL this->key
57
#define KR this->key+this->L
58
#define BL this->buffer
59
#define BR this->buffer+this->S
1 by weidai
Initial revision
60
#define IL inBlock
156 by weidai
port to GCC 3.4
61
#define IR inBlock+this->S
1 by weidai
Initial revision
62
#define OL outBlock
156 by weidai
port to GCC 3.4
63
#define OR outBlock+this->S
1 by weidai
Initial revision
64
65
		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
66
		{
156 by weidai
port to GCC 3.4
67
			this->hm.Update(KL, this->L);
68
			this->hm.Update(IL, this->S);
69
			this->hm.Final(BR);
70
			xorbuf(BR, IR, this->S);
71
72
			this->hm.Update(KR, this->L);
73
			this->hm.Update(BR, this->S);
74
			this->hm.Final(BL);
75
			xorbuf(BL, IL, this->S);
76
77
			this->hm.Update(KL, this->L);
78
			this->hm.Update(BL, this->S);
79
			this->hm.Final(this->digest);
80
			xorbuf(BR, this->digest, this->S);
81
82
			this->hm.Update(KR, this->L);
83
			this->hm.Update(OR, this->S);
84
			this->hm.Final(this->digest);
85
			xorbuf(BL, this->digest, this->S);
1 by weidai
Initial revision
86
87
			if (xorBlock)
156 by weidai
port to GCC 3.4
88
				xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
1 by weidai
Initial revision
89
			else
202 by weidai
fix MSVC 2005 warnings
90
				memcpy_s(outBlock, 2*this->S, this->buffer, 2*this->S);
1 by weidai
Initial revision
91
		}
92
	};
93
57 by weidai
add CRYPTOPP_NO_VTABLE
94
	class CRYPTOPP_NO_VTABLE Dec : public Base
1 by weidai
Initial revision
95
	{
96
	public:
97
		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
98
		{
156 by weidai
port to GCC 3.4
99
			this->hm.Update(KR, this->L);
100
			this->hm.Update(IR, this->S);
101
			this->hm.Final(BL);
102
			xorbuf(BL, IL, this->S);
103
104
			this->hm.Update(KL, this->L);
105
			this->hm.Update(BL, this->S);
106
			this->hm.Final(BR);
107
			xorbuf(BR, IR, this->S);
108
109
			this->hm.Update(KR, this->L);
110
			this->hm.Update(BR, this->S);
111
			this->hm.Final(this->digest);
112
			xorbuf(BL, this->digest, this->S);
113
114
			this->hm.Update(KL, this->L);
115
			this->hm.Update(OL, this->S);
116
			this->hm.Final(this->digest);
117
			xorbuf(BR, this->digest, this->S);
1 by weidai
Initial revision
118
119
			if (xorBlock)
156 by weidai
port to GCC 3.4
120
				xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
1 by weidai
Initial revision
121
			else
156 by weidai
port to GCC 3.4
122
				memcpy(outBlock, this->buffer, 2*this->S);
1 by weidai
Initial revision
123
		}
124
#undef KL
125
#undef KR
126
#undef BL
127
#undef BR
128
#undef IL
129
#undef IR
130
#undef OL
131
#undef OR
132
	};
133
134
public:
75 by weidai
create DLL version, fix GetNextIV() bug in CTR and OFB modes
135
	typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
136
	typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
1 by weidai
Initial revision
137
};
138
139
NAMESPACE_END
140
141
#endif