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 // Returns log2 of aNum where aNum is a power
74 inline TUint8 CryptoLog2(TUint8 aNum)
100 inline TUint BitsToBytes(TUint bitCount)
102 return ((bitCount+7)/(BYTE_BITS));
105 inline TUint BytesToWords(TUint byteCount)
107 return ((byteCount+WORD_SIZE-1)/WORD_SIZE);
110 inline TUint BitsToWords(TUint bitCount)
112 return ((bitCount+WORD_BITS-1)/(WORD_BITS));
115 inline TUint WordsToBits(TUint wordCount)
117 return wordCount * WORD_BITS;
120 inline TUint BytesToBits(TUint byteCount)
122 return byteCount * BYTE_BITS;
125 inline TUint WordsToBytes(TUint wordCount)
127 return wordCount * WORD_BYTES;
130 inline void XorWords(TUint32* r, const TUint32* a, TUint n)
132 assert(((TUint32)r & 3) == 0); // Catch alignment problems
134 for (TUint i=0; i<n; i++)
138 inline void XorBuf(TUint8* buf, const TUint8* mask, TUint count)
140 if (((TUint)buf | (TUint)mask | count) % WORD_SIZE == 0)
142 XorWords((TUint32*)buf, (const TUint32*)mask, count/WORD_SIZE);
146 for (TUint i=0; i<count; i++)
151 // ************** rotate functions ***************
152 template <class T> inline T rotlFixed(T x, TUint y)
154 assert(y < sizeof(T)*8);
155 return ( (T)((x<<y) | (x>>(sizeof(T)*8-y))) );
158 template <class T> inline T rotrFixed(T x, TUint y)
160 assert(y < sizeof(T)*8);
161 return ((T)((x>>y) | (x<<(sizeof(T)*8-y))));
164 inline TUint32 byteReverse(TUint32 value)
166 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
167 return rotlFixed(value, 16U);
171 void byteReverse(T* out, const T* in, TUint32 byteCount)
173 TUint count = (byteCount+sizeof(T)-1)/sizeof(T);
174 for (TUint i=0; i<count; i++)
175 out[i] = byteReverse(in[i]);
179 inline void GetUserKeyLittleEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
181 const TUint U = sizeof(T);
182 assert(inlen <= outlen*U);
183 Mem::Copy(out, in, inlen);
184 Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
188 inline void GetUserKeyBigEndian(T *out, TUint32 outlen, const TUint8* in, TUint32 inlen)
190 const TUint U = sizeof(T);
191 assert(inlen <= outlen*U);
192 Mem::Copy(out, in, inlen);
193 Mem::FillZ((TUint8*)out+inlen, outlen*U-inlen);
194 byteReverse(out, out, inlen);
197 // The following methods have be changed to use byte rather than word accesses,
198 // as if the input pointer is not be word aligned a fault occurs on arm
199 // hardware. This isn't optimal from a performance point of view, but it is
200 // neccessary because the crypto interfaces (CSymmetricCipher,
201 // CBlockTransformation) allow clients to pass non-aligned data.
203 // Fetch 4 words from user's buffer into "a", "b", "c", "d" in LITTLE-endian order
204 inline void GetBlockLittleEndian(const TUint8* block, TUint16 &a, TUint16 &b, TUint16 &c, TUint16 &d)
206 a = (TUint16)(block[0] | block[1] << 8);
207 b = (TUint16)(block[2] | block[3] << 8);
208 c = (TUint16)(block[4] | block[5] << 8);
209 d = (TUint16)(block[6] | block[7] << 8);
212 // Put 4 words back into user's buffer in LITTLE-endian order
213 inline void PutBlockLittleEndian(TUint8* block, TUint16 a, TUint16 b, TUint16 c, TUint16 d)
215 block[0] = (TUint8)(a & 0xff);
216 block[1] = (TUint8)(a >> 8);
217 block[2] = (TUint8)(b & 0xff);
218 block[3] = (TUint8)(b >> 8);
219 block[4] = (TUint8)(c & 0xff);
220 block[5] = (TUint8)(c >> 8);
221 block[6] = (TUint8)(d & 0xff);
222 block[7] = (TUint8)(d >> 8);
225 // Fetch 1 word from user's buffer in BIG-endian order
226 inline void GetWordBigEndian(const TUint8* block, TUint32 &a)
228 a = block[0] << 24 | block[1] << 16 | block[2] << 8 | block[3];
231 // Put 1 word back into user's buffer in BIG-endian order
232 inline void PutWordBigEndian(TUint8* block, TUint32 a)
234 block[0] = (TUint8)(a >> 24);
235 block[1] = (TUint8)((a >> 16) & 0xff);
236 block[2] = (TUint8)((a >> 8) & 0xff);
237 block[3] = (TUint8)(a & 0xff);
240 // Fetch 2 words from user's buffer into "a", "b" in BIG-endian order
241 inline void GetBlockBigEndian(const TUint8* block, TUint32 &a, TUint32& b)
243 GetWordBigEndian(block, a);
244 GetWordBigEndian(block + 4, b);
247 // Put 2 words back into user's buffer in BIG-endian order
248 inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b)
250 PutWordBigEndian(block, a);
251 PutWordBigEndian(block + 4, b);
254 // Fetch 4 words from user's buffer into "a", "b", "c", "d" in BIG-endian order
255 inline void GetBlockBigEndian(const TUint8* block, TUint32& a, TUint32& b, TUint32& c, TUint32& d)
257 GetWordBigEndian(block, a);
258 GetWordBigEndian(block + 4, b);
259 GetWordBigEndian(block + 8, c);
260 GetWordBigEndian(block + 12, d);
263 // Put 4 words back into user's buffer in BIG-endian order
264 inline void PutBlockBigEndian(TUint8* block, TUint32 a, TUint32 b, TUint32 c, TUint32 d)
266 PutWordBigEndian(block, a);
267 PutWordBigEndian(block + 4, b);
268 PutWordBigEndian(block + 8, c);
269 PutWordBigEndian(block + 12, d);
272 #endif // __INLINES_H__