Update contrib.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
29 #define assert(x) __ASSERT_DEBUG((x), User::Panic(_L("crypto.dll"), 1))
31 #if defined(__GCC32__)
32 typedef long long Int64;
33 typedef unsigned long long Uint64;
34 #elif defined(__VC32__)
35 typedef __int64 Int64;
36 typedef unsigned __int64 Uint64;
37 #elif defined(__CW32__)
39 typedef long long Int64;
40 typedef unsigned long long Uint64;
45 typedef TUint32 word32;
47 const TUint WORD_SIZE = sizeof(TUint);
48 const TUint WORD_BYTES = WORD_SIZE;
49 const TUint BYTE_BITS = 8;
50 const TUint WORD_BITS = WORD_SIZE*BYTE_BITS;
52 //These next two versions of GETBYTE compile to LDR's of words and then shifts
53 //and ands to get it down to a byte.
54 //#define GETBYTE(x, y) (TUint)(((x)>>(8*(y)))&255)
55 //#define GETBYTE(x, y) (TUint)TUint8((x)>>(8*(y)))
57 //This next version gets the best assembler on gcc and armv4 (it uses LDRB
58 //rather than shifts and ands
59 #define GETBYTE(x, y) (((TUint8 *)&(x))[y])
61 #define MAKE_DWORD(lowWord, highWord) ((dword(highWord)<<WORD_BITS) | (lowWord))
62 #define LOW_WORD(x) (TUint32)(x)
63 #define HIGH_WORD(x) (TUint32)((x)>>WORD_BITS)
65 template <class T> inline void TClassSwap(T& a, T& b)
72 inline TUint BitsToBytes(TUint bitCount)
74 return ((bitCount+7)/(BYTE_BITS));
77 inline TUint BytesToWords(TUint byteCount)
79 return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
82 inline TUint BitsToWords(TUint bitCount)
84 return ((bitCount+WORD_BITS-1)/(WORD_BITS));
87 inline TUint WordsToBits(TUint wordCount)
89 return wordCount * WORD_BITS;
92 inline TUint BytesToBits(TUint byteCount)
94 return byteCount * BYTE_BITS;
97 inline TUint WordsToBytes(TUint wordCount)
99 return wordCount * WORD_BYTES;
102 inline void XorWords(TUint* r, const TUint* a, TUint n)
104 assert(((TUint32)r & 3) == 0); // Catch alignment problems
106 for (TUint i=0; i<n; i++)
110 inline void XorBuf(TUint8* buf, const TUint8* mask, TUint count)
112 if (((TUint)buf | (TUint)mask | count) % WORD_SIZE == 0)
114 XorWords((TUint*)buf, (const TUint*)mask, count/WORD_SIZE);
118 for (TUint i=0; i<count; i++)
123 // ************** rotate functions ***************
124 template <class T> inline T rotlFixed(T x, TUint y)
126 assert(y < sizeof(T)*8);
127 return ( (T)((x<<y) | (x>>(sizeof(T)*8-y))) );
130 template <class T> inline T rotrFixed(T x, TUint y)
132 assert(y < sizeof(T)*8);
133 return ((T)((x>>y) | (x<<(sizeof(T)*8-y))));
136 inline TUint32 byteReverse(TUint32 value)
138 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
139 return rotlFixed(value, 16U);
143 void byteReverse(T* out, const T* in, TUint32 byteCount)
145 TUint count = (byteCount+sizeof(T)-1)/sizeof(T);
146 for (TUint i=0; i<count; i++)
147 out[i] = byteReverse(in[i]);
151 inline void GetUserKeyLittleEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
153 const TUint U = sizeof(T);
154 assert(inlen <= outlen*U);
155 Mem::Copy(out, in, inlen);
156 Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
160 inline void GetUserKeyBigEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
162 const TUint U = sizeof(T);
163 assert(inlen <= outlen*U);
164 Mem::Copy(out, in, inlen);
165 Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
166 byteReverse(out, out, inlen);
169 // The following methods have be changed to use byte rather than word accesses,
170 // as if the input pointer is not be word aligned a fault occurs on arm
171 // hardware. This isn't optimal from a performance point of view, but it is
172 // neccessary because the crypto interfaces (CSymmetricCipher,
173 // CBlockTransformation) allow clients to pass non-aligned data.
175 // Fetch 4 words from user's buffer into "a", "b", "c", "d" in LITTLE-endian order
176 inline void GetBlockLittleEndian(const TUint8* block, TUint16 &a, TUint16 &b, TUint16 &c, TUint16 &d)
178 a = (TUint16)(block[0] | block[1] << 8);
179 b = (TUint16)(block[2] | block[3] << 8);
180 c = (TUint16)(block[4] | block[5] << 8);
181 d = (TUint16)(block[6] | block[7] << 8);
184 // Put 4 words back into user's buffer in LITTLE-endian order
185 inline void PutBlockLittleEndian(TUint8* block, TUint16 a, TUint16 b, TUint16 c, TUint16 d)
187 block[0] = (TUint8)(a & 0xff);
188 block[1] = (TUint8)(a >> 8);
189 block[2] = (TUint8)(b & 0xff);
190 block[3] = (TUint8)(b >> 8);
191 block[4] = (TUint8)(c & 0xff);
192 block[5] = (TUint8)(c >> 8);
193 block[6] = (TUint8)(d & 0xff);
194 block[7] = (TUint8)(d >> 8);
197 // Fetch 1 word from user's buffer in BIG-endian order
198 inline void GetWordBigEndian(const TUint8* block, TUint32 &a)
200 a = block[0] << 24 | block[1] << 16 | block[2] << 8 | block[3];
203 // Put 1 word back into user's buffer in BIG-endian order
204 inline void PutWordBigEndian(TUint8* block, TUint32 a)
206 block[0] = (TUint8)(a >> 24);
207 block[1] = (TUint8)((a >> 16) & 0xff);
208 block[2] = (TUint8)((a >> 8) & 0xff);
209 block[3] = (TUint8)(a & 0xff);
212 // Fetch 2 words from user's buffer into "a", "b" in BIG-endian order
213 inline void GetBlockBigEndian(const TUint8* block, TUint32 &a, TUint32& b)
215 GetWordBigEndian(block, a);
216 GetWordBigEndian(block + 4, b);
219 // Put 2 words back into user's buffer in BIG-endian order
220 inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b)
222 PutWordBigEndian(block, a);
223 PutWordBigEndian(block + 4, b);
226 // Fetch 4 words from user's buffer into "a", "b", "c", "d" in BIG-endian order
227 inline void GetBlockBigEndian(const TUint8* block, TUint32& a, TUint32& b, TUint32& c, TUint32& d)
229 GetWordBigEndian(block, a);
230 GetWordBigEndian(block + 4, b);
231 GetWordBigEndian(block + 8, c);
232 GetWordBigEndian(block + 12, d);
235 // Put 4 words back into user's buffer in BIG-endian order
236 inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b, TUint32 c, TUint32 d)
238 PutWordBigEndian(block, a);
239 PutWordBigEndian(block + 4, b);
240 PutWordBigEndian(block + 8, c);
241 PutWordBigEndian(block + 12, d);
244 #endif // __INLINES_H__