QT3 -> QT4

git-svn-id: https://svn.code.sf.net/p/keepassx/code/trunk@5 b624d157-de02-0410-bad0-e51aec6abb33
This commit is contained in:
tariq
2005-11-15 20:45:14 +00:00
parent 192dbba0e4
commit 28c71dfe1e
51 changed files with 977 additions and 937 deletions

View File

@@ -40,7 +40,7 @@
#include "rijndael.h"
static UINT8 S[256]=
static Q_UINT8 S[256]=
{
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118,
202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192,
@@ -61,7 +61,7 @@ static UINT8 S[256]=
};
static UINT8 T1[256][4]=
static Q_UINT8 T1[256][4]=
{
0xc6,0x63,0x63,0xa5, 0xf8,0x7c,0x7c,0x84, 0xee,0x77,0x77,0x99, 0xf6,0x7b,0x7b,0x8d,
0xff,0xf2,0xf2,0x0d, 0xd6,0x6b,0x6b,0xbd, 0xde,0x6f,0x6f,0xb1, 0x91,0xc5,0xc5,0x54,
@@ -129,7 +129,7 @@ static UINT8 T1[256][4]=
0x7b,0xb0,0xb0,0xcb, 0xa8,0x54,0x54,0xfc, 0x6d,0xbb,0xbb,0xd6, 0x2c,0x16,0x16,0x3a
};
static UINT8 T2[256][4]=
static Q_UINT8 T2[256][4]=
{
0xa5,0xc6,0x63,0x63, 0x84,0xf8,0x7c,0x7c, 0x99,0xee,0x77,0x77, 0x8d,0xf6,0x7b,0x7b,
0x0d,0xff,0xf2,0xf2, 0xbd,0xd6,0x6b,0x6b, 0xb1,0xde,0x6f,0x6f, 0x54,0x91,0xc5,0xc5,
@@ -197,7 +197,7 @@ static UINT8 T2[256][4]=
0xcb,0x7b,0xb0,0xb0, 0xfc,0xa8,0x54,0x54, 0xd6,0x6d,0xbb,0xbb, 0x3a,0x2c,0x16,0x16
};
static UINT8 T3[256][4]=
static Q_UINT8 T3[256][4]=
{
0x63,0xa5,0xc6,0x63, 0x7c,0x84,0xf8,0x7c, 0x77,0x99,0xee,0x77, 0x7b,0x8d,0xf6,0x7b,
0xf2,0x0d,0xff,0xf2, 0x6b,0xbd,0xd6,0x6b, 0x6f,0xb1,0xde,0x6f, 0xc5,0x54,0x91,0xc5,
@@ -265,7 +265,7 @@ static UINT8 T3[256][4]=
0xb0,0xcb,0x7b,0xb0, 0x54,0xfc,0xa8,0x54, 0xbb,0xd6,0x6d,0xbb, 0x16,0x3a,0x2c,0x16
};
static UINT8 T4[256][4]=
static Q_UINT8 T4[256][4]=
{
0x63,0x63,0xa5,0xc6, 0x7c,0x7c,0x84,0xf8, 0x77,0x77,0x99,0xee, 0x7b,0x7b,0x8d,0xf6,
0xf2,0xf2,0x0d,0xff, 0x6b,0x6b,0xbd,0xd6, 0x6f,0x6f,0xb1,0xde, 0xc5,0xc5,0x54,0x91,
@@ -333,7 +333,7 @@ static UINT8 T4[256][4]=
0xb0,0xb0,0xcb,0x7b, 0x54,0x54,0xfc,0xa8, 0xbb,0xbb,0xd6,0x6d, 0x16,0x16,0x3a,0x2c
};
static UINT8 T5[256][4]=
static Q_UINT8 T5[256][4]=
{
0x51,0xf4,0xa7,0x50, 0x7e,0x41,0x65,0x53, 0x1a,0x17,0xa4,0xc3, 0x3a,0x27,0x5e,0x96,
0x3b,0xab,0x6b,0xcb, 0x1f,0x9d,0x45,0xf1, 0xac,0xfa,0x58,0xab, 0x4b,0xe3,0x03,0x93,
@@ -401,7 +401,7 @@ static UINT8 T5[256][4]=
0x7b,0xcb,0x84,0x61, 0xd5,0x32,0xb6,0x70, 0x48,0x6c,0x5c,0x74, 0xd0,0xb8,0x57,0x42
};
static UINT8 T6[256][4]=
static Q_UINT8 T6[256][4]=
{
0x50,0x51,0xf4,0xa7, 0x53,0x7e,0x41,0x65, 0xc3,0x1a,0x17,0xa4, 0x96,0x3a,0x27,0x5e,
0xcb,0x3b,0xab,0x6b, 0xf1,0x1f,0x9d,0x45, 0xab,0xac,0xfa,0x58, 0x93,0x4b,0xe3,0x03,
@@ -469,7 +469,7 @@ static UINT8 T6[256][4]=
0x61,0x7b,0xcb,0x84, 0x70,0xd5,0x32,0xb6, 0x74,0x48,0x6c,0x5c, 0x42,0xd0,0xb8,0x57
};
static UINT8 T7[256][4]=
static Q_UINT8 T7[256][4]=
{
0xa7,0x50,0x51,0xf4, 0x65,0x53,0x7e,0x41, 0xa4,0xc3,0x1a,0x17, 0x5e,0x96,0x3a,0x27,
0x6b,0xcb,0x3b,0xab, 0x45,0xf1,0x1f,0x9d, 0x58,0xab,0xac,0xfa, 0x03,0x93,0x4b,0xe3,
@@ -537,7 +537,7 @@ static UINT8 T7[256][4]=
0x84,0x61,0x7b,0xcb, 0xb6,0x70,0xd5,0x32, 0x5c,0x74,0x48,0x6c, 0x57,0x42,0xd0,0xb8
};
static UINT8 T8[256][4]=
static Q_UINT8 T8[256][4]=
{
0xf4,0xa7,0x50,0x51, 0x41,0x65,0x53,0x7e, 0x17,0xa4,0xc3,0x1a, 0x27,0x5e,0x96,0x3a,
0xab,0x6b,0xcb,0x3b, 0x9d,0x45,0xf1,0x1f, 0xfa,0x58,0xab,0xac, 0xe3,0x03,0x93,0x4b,
@@ -605,7 +605,7 @@ static UINT8 T8[256][4]=
0xcb,0x84,0x61,0x7b, 0x32,0xb6,0x70,0xd5, 0x6c,0x5c,0x74,0x48, 0xb8,0x57,0x42,0xd0
};
static UINT8 S5[256]=
static Q_UINT8 S5[256]=
{
0x52,0x09,0x6a,0xd5,
0x30,0x36,0xa5,0x38,
@@ -673,7 +673,7 @@ static UINT8 S5[256]=
0x55,0x21,0x0c,0x7d
};
static UINT8 U1[256][4]=
static Q_UINT8 U1[256][4]=
{
0x00,0x00,0x00,0x00, 0x0e,0x09,0x0d,0x0b, 0x1c,0x12,0x1a,0x16, 0x12,0x1b,0x17,0x1d,
0x38,0x24,0x34,0x2c, 0x36,0x2d,0x39,0x27, 0x24,0x36,0x2e,0x3a, 0x2a,0x3f,0x23,0x31,
@@ -741,7 +741,7 @@ static UINT8 U1[256][4]=
0x9f,0x5d,0x80,0xbe, 0x91,0x54,0x8d,0xb5, 0x83,0x4f,0x9a,0xa8, 0x8d,0x46,0x97,0xa3
};
static UINT8 U2[256][4]=
static Q_UINT8 U2[256][4]=
{
0x00,0x00,0x00,0x00, 0x0b,0x0e,0x09,0x0d, 0x16,0x1c,0x12,0x1a, 0x1d,0x12,0x1b,0x17,
0x2c,0x38,0x24,0x34, 0x27,0x36,0x2d,0x39, 0x3a,0x24,0x36,0x2e, 0x31,0x2a,0x3f,0x23,
@@ -809,7 +809,7 @@ static UINT8 U2[256][4]=
0xbe,0x9f,0x5d,0x80, 0xb5,0x91,0x54,0x8d, 0xa8,0x83,0x4f,0x9a, 0xa3,0x8d,0x46,0x97
};
static UINT8 U3[256][4]=
static Q_UINT8 U3[256][4]=
{
0x00,0x00,0x00,0x00, 0x0d,0x0b,0x0e,0x09, 0x1a,0x16,0x1c,0x12, 0x17,0x1d,0x12,0x1b,
0x34,0x2c,0x38,0x24, 0x39,0x27,0x36,0x2d, 0x2e,0x3a,0x24,0x36, 0x23,0x31,0x2a,0x3f,
@@ -877,7 +877,7 @@ static UINT8 U3[256][4]=
0x80,0xbe,0x9f,0x5d, 0x8d,0xb5,0x91,0x54, 0x9a,0xa8,0x83,0x4f, 0x97,0xa3,0x8d,0x46
};
static UINT8 U4[256][4]=
static Q_UINT8 U4[256][4]=
{
0x00,0x00,0x00,0x00, 0x09,0x0d,0x0b,0x0e, 0x12,0x1a,0x16,0x1c, 0x1b,0x17,0x1d,0x12,
0x24,0x34,0x2c,0x38, 0x2d,0x39,0x27,0x36, 0x36,0x2e,0x3a,0x24, 0x3f,0x23,0x31,0x2a,
@@ -945,7 +945,7 @@ static UINT8 U4[256][4]=
0x5d,0x80,0xbe,0x9f, 0x54,0x8d,0xb5,0x91, 0x4f,0x9a,0xa8,0x83, 0x46,0x97,0xa3,0x8d
};
static UINT32 rcon[30]=
static Q_UINT32 rcon[30]=
{
0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
@@ -970,7 +970,7 @@ Rijndael::~Rijndael()
// nothing here
}
int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UINT8 * initVector)
int Rijndael::init(Mode mode,Direction dir,const Q_UINT8 * key,KeyLength keyLen,Q_UINT8 * initVector)
{
// Not initialized yet
m_state = Invalid;
@@ -999,7 +999,7 @@ int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UI
}
}
UINT32 uKeyLenInBytes;
Q_UINT32 uKeyLenInBytes;
// And check the key length
switch(keyLen)
@@ -1025,9 +1025,9 @@ int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UI
if(!key) return RIJNDAEL_BAD_KEY;
UINT8 keyMatrix[_MAX_KEY_COLUMNS][4];
Q_UINT8 keyMatrix[_MAX_KEY_COLUMNS][4];
for(UINT32 i = 0;i < uKeyLenInBytes;i++)keyMatrix[i >> 2][i & 3] = key[i];
for(Q_UINT32 i = 0;i < uKeyLenInBytes;i++)keyMatrix[i >> 2][i & 3] = key[i];
keySched(keyMatrix);
@@ -1038,10 +1038,10 @@ int Rijndael::init(Mode mode,Direction dir,const UINT8 * key,KeyLength keyLen,UI
return RIJNDAEL_SUCCESS;
}
int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer)
int Rijndael::blockEncrypt(const Q_UINT8 *input,int inputLen,Q_UINT8 *outBuffer)
{
int i, k, numBlocks;
UINT8 block[16], iv[4][4];
Q_UINT8 block[16], iv[4][4];
if(m_state != Valid) return RIJNDAEL_NOT_INITIALIZED;
if(m_direction != Encrypt) return RIJNDAEL_BAD_DIRECTION;
@@ -1060,18 +1060,18 @@ int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer)
}
break;
case CBC:
((UINT32*)block)[0] = ((UINT32*)m_initVector)[0] ^ ((UINT32*)input)[0];
((UINT32*)block)[1] = ((UINT32*)m_initVector)[1] ^ ((UINT32*)input)[1];
((UINT32*)block)[2] = ((UINT32*)m_initVector)[2] ^ ((UINT32*)input)[2];
((UINT32*)block)[3] = ((UINT32*)m_initVector)[3] ^ ((UINT32*)input)[3];
((Q_UINT32*)block)[0] = ((Q_UINT32*)m_initVector)[0] ^ ((Q_UINT32*)input)[0];
((Q_UINT32*)block)[1] = ((Q_UINT32*)m_initVector)[1] ^ ((Q_UINT32*)input)[1];
((Q_UINT32*)block)[2] = ((Q_UINT32*)m_initVector)[2] ^ ((Q_UINT32*)input)[2];
((Q_UINT32*)block)[3] = ((Q_UINT32*)m_initVector)[3] ^ ((Q_UINT32*)input)[3];
encrypt(block,outBuffer);
input += 16;
for(i = numBlocks - 1;i > 0;i--)
{
((UINT32*)block)[0] = ((UINT32*)outBuffer)[0] ^ ((UINT32*)input)[0];
((UINT32*)block)[1] = ((UINT32*)outBuffer)[1] ^ ((UINT32*)input)[1];
((UINT32*)block)[2] = ((UINT32*)outBuffer)[2] ^ ((UINT32*)input)[2];
((UINT32*)block)[3] = ((UINT32*)outBuffer)[3] ^ ((UINT32*)input)[3];
((Q_UINT32*)block)[0] = ((Q_UINT32*)outBuffer)[0] ^ ((Q_UINT32*)input)[0];
((Q_UINT32*)block)[1] = ((Q_UINT32*)outBuffer)[1] ^ ((Q_UINT32*)input)[1];
((Q_UINT32*)block)[2] = ((Q_UINT32*)outBuffer)[2] ^ ((Q_UINT32*)input)[2];
((Q_UINT32*)block)[3] = ((Q_UINT32*)outBuffer)[3] ^ ((Q_UINT32*)input)[3];
outBuffer += 16;
encrypt(block,outBuffer);
input += 16;
@@ -1081,19 +1081,19 @@ int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer)
#if STRICT_ALIGN
memcpy(iv,m_initVector,16);
#else /* !STRICT_ALIGN */
*((UINT32*)iv[0]) = *((UINT32*)(m_initVector ));
*((UINT32*)iv[1]) = *((UINT32*)(m_initVector + 4));
*((UINT32*)iv[2]) = *((UINT32*)(m_initVector + 8));
*((UINT32*)iv[3]) = *((UINT32*)(m_initVector +12));
*((Q_UINT32*)iv[0]) = *((Q_UINT32*)(m_initVector ));
*((Q_UINT32*)iv[1]) = *((Q_UINT32*)(m_initVector + 4));
*((Q_UINT32*)iv[2]) = *((Q_UINT32*)(m_initVector + 8));
*((Q_UINT32*)iv[3]) = *((Q_UINT32*)(m_initVector +12));
#endif /* ?STRICT_ALIGN */
for(i = numBlocks; i > 0; i--)
{
for(k = 0; k < 128; k++)
{
*((UINT32*) block ) = *((UINT32*)iv[0]);
*((UINT32*)(block+ 4)) = *((UINT32*)iv[1]);
*((UINT32*)(block+ 8)) = *((UINT32*)iv[2]);
*((UINT32*)(block+12)) = *((UINT32*)iv[3]);
*((Q_UINT32*) block ) = *((Q_UINT32*)iv[0]);
*((Q_UINT32*)(block+ 4)) = *((Q_UINT32*)iv[1]);
*((Q_UINT32*)(block+ 8)) = *((Q_UINT32*)iv[2]);
*((Q_UINT32*)(block+12)) = *((Q_UINT32*)iv[3]);
encrypt(block,block);
outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7);
iv[0][0] = (iv[0][0] << 1) | (iv[0][1] >> 7);
@@ -1123,10 +1123,10 @@ int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer)
return 128 * numBlocks;
}
int Rijndael::padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
int Rijndael::padEncrypt(const Q_UINT8 *input, int inputOctets, Q_UINT8 *outBuffer)
{
int i, numBlocks, padLen;
UINT8 block[16], *iv;
Q_UINT8 block[16], *iv;
if(m_state != Valid) return RIJNDAEL_NOT_INITIALIZED;
if(m_direction != Encrypt) return RIJNDAEL_NOT_INITIALIZED;
@@ -1154,10 +1154,10 @@ int Rijndael::padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
iv = m_initVector;
for(i = numBlocks; i > 0; i--)
{
((UINT32*)block)[0] = ((UINT32*)input)[0] ^ ((UINT32*)iv)[0];
((UINT32*)block)[1] = ((UINT32*)input)[1] ^ ((UINT32*)iv)[1];
((UINT32*)block)[2] = ((UINT32*)input)[2] ^ ((UINT32*)iv)[2];
((UINT32*)block)[3] = ((UINT32*)input)[3] ^ ((UINT32*)iv)[3];
((Q_UINT32*)block)[0] = ((Q_UINT32*)input)[0] ^ ((Q_UINT32*)iv)[0];
((Q_UINT32*)block)[1] = ((Q_UINT32*)input)[1] ^ ((Q_UINT32*)iv)[1];
((Q_UINT32*)block)[2] = ((Q_UINT32*)input)[2] ^ ((Q_UINT32*)iv)[2];
((Q_UINT32*)block)[3] = ((Q_UINT32*)input)[3] ^ ((Q_UINT32*)iv)[3];
encrypt(block, outBuffer);
iv = outBuffer;
input += 16;
@@ -1169,7 +1169,7 @@ int Rijndael::padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
block[i] = input[i] ^ iv[i];
}
for (i = 16 - padLen; i < 16; i++) {
block[i] = (UINT8)padLen ^ iv[i];
block[i] = (Q_UINT8)padLen ^ iv[i];
}
encrypt(block,outBuffer);
break;
@@ -1181,10 +1181,10 @@ int Rijndael::padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
return 16*(numBlocks + 1);
}
int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer)
int Rijndael::blockDecrypt(const Q_UINT8 *input, int inputLen, Q_UINT8 *outBuffer)
{
int i, k, numBlocks;
UINT8 block[16], iv[4][4];
Q_UINT8 block[16], iv[4][4];
if(m_state != Valid) return RIJNDAEL_NOT_INITIALIZED;
if((m_mode != CFB1) && (m_direction == Encrypt)) return RIJNDAEL_BAD_DIRECTION;
@@ -1207,26 +1207,26 @@ int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer)
#if STRICT_ALIGN
memcpy(iv,m_initVector,16);
#else
*((UINT32*)iv[0]) = *((UINT32*)(m_initVector ));
*((UINT32*)iv[1]) = *((UINT32*)(m_initVector+ 4));
*((UINT32*)iv[2]) = *((UINT32*)(m_initVector+ 8));
*((UINT32*)iv[3]) = *((UINT32*)(m_initVector+12));
*((Q_UINT32*)iv[0]) = *((Q_UINT32*)(m_initVector ));
*((Q_UINT32*)iv[1]) = *((Q_UINT32*)(m_initVector+ 4));
*((Q_UINT32*)iv[2]) = *((Q_UINT32*)(m_initVector+ 8));
*((Q_UINT32*)iv[3]) = *((Q_UINT32*)(m_initVector+12));
#endif
for (i = numBlocks; i > 0; i--)
{
decrypt(input, block);
((UINT32*)block)[0] ^= *((UINT32*)iv[0]);
((UINT32*)block)[1] ^= *((UINT32*)iv[1]);
((UINT32*)block)[2] ^= *((UINT32*)iv[2]);
((UINT32*)block)[3] ^= *((UINT32*)iv[3]);
((Q_UINT32*)block)[0] ^= *((Q_UINT32*)iv[0]);
((Q_UINT32*)block)[1] ^= *((Q_UINT32*)iv[1]);
((Q_UINT32*)block)[2] ^= *((Q_UINT32*)iv[2]);
((Q_UINT32*)block)[3] ^= *((Q_UINT32*)iv[3]);
#if STRICT_ALIGN
memcpy(iv, input, 16);
memcpy(outBuf, block, 16);
#else
*((UINT32*)iv[0]) = ((UINT32*)input)[0]; ((UINT32*)outBuffer)[0] = ((UINT32*)block)[0];
*((UINT32*)iv[1]) = ((UINT32*)input)[1]; ((UINT32*)outBuffer)[1] = ((UINT32*)block)[1];
*((UINT32*)iv[2]) = ((UINT32*)input)[2]; ((UINT32*)outBuffer)[2] = ((UINT32*)block)[2];
*((UINT32*)iv[3]) = ((UINT32*)input)[3]; ((UINT32*)outBuffer)[3] = ((UINT32*)block)[3];
*((Q_UINT32*)iv[0]) = ((Q_UINT32*)input)[0]; ((Q_UINT32*)outBuffer)[0] = ((Q_UINT32*)block)[0];
*((Q_UINT32*)iv[1]) = ((Q_UINT32*)input)[1]; ((Q_UINT32*)outBuffer)[1] = ((Q_UINT32*)block)[1];
*((Q_UINT32*)iv[2]) = ((Q_UINT32*)input)[2]; ((Q_UINT32*)outBuffer)[2] = ((Q_UINT32*)block)[2];
*((Q_UINT32*)iv[3]) = ((Q_UINT32*)input)[3]; ((Q_UINT32*)outBuffer)[3] = ((Q_UINT32*)block)[3];
#endif
input += 16;
outBuffer += 16;
@@ -1236,19 +1236,19 @@ int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer)
#if STRICT_ALIGN
memcpy(iv, m_initVector, 16);
#else
*((UINT32*)iv[0]) = *((UINT32*)(m_initVector));
*((UINT32*)iv[1]) = *((UINT32*)(m_initVector+ 4));
*((UINT32*)iv[2]) = *((UINT32*)(m_initVector+ 8));
*((UINT32*)iv[3]) = *((UINT32*)(m_initVector+12));
*((Q_UINT32*)iv[0]) = *((Q_UINT32*)(m_initVector));
*((Q_UINT32*)iv[1]) = *((Q_UINT32*)(m_initVector+ 4));
*((Q_UINT32*)iv[2]) = *((Q_UINT32*)(m_initVector+ 8));
*((Q_UINT32*)iv[3]) = *((Q_UINT32*)(m_initVector+12));
#endif
for(i = numBlocks; i > 0; i--)
{
for(k = 0; k < 128; k++)
{
*((UINT32*) block ) = *((UINT32*)iv[0]);
*((UINT32*)(block+ 4)) = *((UINT32*)iv[1]);
*((UINT32*)(block+ 8)) = *((UINT32*)iv[2]);
*((UINT32*)(block+12)) = *((UINT32*)iv[3]);
*((Q_UINT32*) block ) = *((Q_UINT32*)iv[0]);
*((Q_UINT32*)(block+ 4)) = *((Q_UINT32*)iv[1]);
*((Q_UINT32*)(block+ 8)) = *((Q_UINT32*)iv[2]);
*((Q_UINT32*)(block+12)) = *((Q_UINT32*)iv[3]);
encrypt(block, block);
iv[0][0] = (iv[0][0] << 1) | (iv[0][1] >> 7);
iv[0][1] = (iv[0][1] << 1) | (iv[0][2] >> 7);
@@ -1278,11 +1278,11 @@ int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer)
return 128*numBlocks;
}
int Rijndael::padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
int Rijndael::padDecrypt(const Q_UINT8 *input, int inputOctets, Q_UINT8 *outBuffer)
{
int i, numBlocks, padLen;
UINT8 block[16];
UINT32 iv[4];
Q_UINT8 block[16];
Q_UINT32 iv[4];
if(m_state != Valid) return RIJNDAEL_NOT_INITIALIZED;
if(m_direction != Decrypt) return RIJNDAEL_BAD_DIRECTION;
@@ -1317,10 +1317,10 @@ int Rijndael::padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
for (i = numBlocks - 1; i > 0; i--)
{
decrypt(input, block);
((UINT32*)block)[0] ^= iv[0];
((UINT32*)block)[1] ^= iv[1];
((UINT32*)block)[2] ^= iv[2];
((UINT32*)block)[3] ^= iv[3];
((Q_UINT32*)block)[0] ^= iv[0];
((Q_UINT32*)block)[1] ^= iv[1];
((Q_UINT32*)block)[2] ^= iv[2];
((Q_UINT32*)block)[3] ^= iv[3];
memcpy(iv, input, 16);
memcpy(outBuffer, block, 16);
input += 16;
@@ -1328,10 +1328,10 @@ int Rijndael::padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
}
/* last block */
decrypt(input, block);
((UINT32*)block)[0] ^= iv[0];
((UINT32*)block)[1] ^= iv[1];
((UINT32*)block)[2] ^= iv[2];
((UINT32*)block)[3] ^= iv[3];
((Q_UINT32*)block)[0] ^= iv[0];
((Q_UINT32*)block)[1] ^= iv[1];
((Q_UINT32*)block)[2] ^= iv[2];
((Q_UINT32*)block)[3] ^= iv[3];
padLen = block[15];
if((padLen <= 0) || (padLen > 16)) return RIJNDAEL_CORRUPTED_DATA;
for(i = 16 - padLen; i < 16; i++)
@@ -1354,7 +1354,7 @@ int Rijndael::padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Rijndael::keySched(UINT8 key[_MAX_KEY_COLUMNS][4])
void Rijndael::keySched(Q_UINT8 key[_MAX_KEY_COLUMNS][4])
{
int j,rconpointer = 0;
@@ -1362,13 +1362,13 @@ void Rijndael::keySched(UINT8 key[_MAX_KEY_COLUMNS][4])
// The number of calculations depends on keyBits and blockBits
int uKeyColumns = m_uRounds - 6;
UINT8 tempKey[_MAX_KEY_COLUMNS][4];
Q_UINT8 tempKey[_MAX_KEY_COLUMNS][4];
// Copy the input key to the temporary key matrix
for(j = 0;j < uKeyColumns;j++)
{
*((UINT32*)(tempKey[j])) = *((UINT32*)(key[j]));
*((Q_UINT32*)(tempKey[j])) = *((Q_UINT32*)(key[j]));
}
int r = 0;
@@ -1379,7 +1379,7 @@ void Rijndael::keySched(UINT8 key[_MAX_KEY_COLUMNS][4])
{
for(;(j < uKeyColumns) && (t < 4); j++, t++)
{
*((UINT32*)m_expandedKey[r][t]) = *((UINT32*)tempKey[j]);
*((Q_UINT32*)m_expandedKey[r][t]) = *((Q_UINT32*)tempKey[j]);
}
@@ -1402,12 +1402,12 @@ void Rijndael::keySched(UINT8 key[_MAX_KEY_COLUMNS][4])
{
for(j = 1; j < uKeyColumns; j++)
{
*((UINT32*)tempKey[j]) ^= *((UINT32*)tempKey[j-1]);
*((Q_UINT32*)tempKey[j]) ^= *((Q_UINT32*)tempKey[j-1]);
}
} else {
for(j = 1; j < uKeyColumns/2; j++)
{
*((UINT32*)tempKey[j]) ^= *((UINT32*)tempKey[j-1]);
*((Q_UINT32*)tempKey[j]) ^= *((Q_UINT32*)tempKey[j-1]);
}
tempKey[uKeyColumns/2][0] ^= S[tempKey[uKeyColumns/2 - 1][0]];
tempKey[uKeyColumns/2][1] ^= S[tempKey[uKeyColumns/2 - 1][1]];
@@ -1415,14 +1415,14 @@ void Rijndael::keySched(UINT8 key[_MAX_KEY_COLUMNS][4])
tempKey[uKeyColumns/2][3] ^= S[tempKey[uKeyColumns/2 - 1][3]];
for(j = uKeyColumns/2 + 1; j < uKeyColumns; j++)
{
*((UINT32*)tempKey[j]) ^= *((UINT32*)tempKey[j-1]);
*((Q_UINT32*)tempKey[j]) ^= *((Q_UINT32*)tempKey[j-1]);
}
}
for(j = 0; (j < uKeyColumns) && (r <= (int)m_uRounds); )
{
for(; (j < uKeyColumns) && (t < 4); j++, t++)
{
*((UINT32*)m_expandedKey[r][t]) = *((UINT32*)tempKey[j]);
*((Q_UINT32*)m_expandedKey[r][t]) = *((Q_UINT32*)tempKey[j]);
}
if(t == 4)
{
@@ -1436,74 +1436,74 @@ void Rijndael::keySched(UINT8 key[_MAX_KEY_COLUMNS][4])
void Rijndael::keyEncToDec()
{
int r;
UINT8 *w;
Q_UINT8 *w;
for(r = 1; r < (int)m_uRounds; r++)
{
w = m_expandedKey[r][0];
*((UINT32*)w) = *((UINT32*)U1[w[0]]) ^ *((UINT32*)U2[w[1]]) ^ *((UINT32*)U3[w[2]]) ^ *((UINT32*)U4[w[3]]);
*((Q_UINT32*)w) = *((Q_UINT32*)U1[w[0]]) ^ *((Q_UINT32*)U2[w[1]]) ^ *((Q_UINT32*)U3[w[2]]) ^ *((Q_UINT32*)U4[w[3]]);
w = m_expandedKey[r][1];
*((UINT32*)w) = *((UINT32*)U1[w[0]]) ^ *((UINT32*)U2[w[1]]) ^ *((UINT32*)U3[w[2]]) ^ *((UINT32*)U4[w[3]]);
*((Q_UINT32*)w) = *((Q_UINT32*)U1[w[0]]) ^ *((Q_UINT32*)U2[w[1]]) ^ *((Q_UINT32*)U3[w[2]]) ^ *((Q_UINT32*)U4[w[3]]);
w = m_expandedKey[r][2];
*((UINT32*)w) = *((UINT32*)U1[w[0]]) ^ *((UINT32*)U2[w[1]]) ^ *((UINT32*)U3[w[2]]) ^ *((UINT32*)U4[w[3]]);
*((Q_UINT32*)w) = *((Q_UINT32*)U1[w[0]]) ^ *((Q_UINT32*)U2[w[1]]) ^ *((Q_UINT32*)U3[w[2]]) ^ *((Q_UINT32*)U4[w[3]]);
w = m_expandedKey[r][3];
*((UINT32*)w) = *((UINT32*)U1[w[0]]) ^ *((UINT32*)U2[w[1]]) ^ *((UINT32*)U3[w[2]]) ^ *((UINT32*)U4[w[3]]);
*((Q_UINT32*)w) = *((Q_UINT32*)U1[w[0]]) ^ *((Q_UINT32*)U2[w[1]]) ^ *((Q_UINT32*)U3[w[2]]) ^ *((Q_UINT32*)U4[w[3]]);
}
}
void Rijndael::encrypt(const UINT8 a[16], UINT8 b[16])
void Rijndael::encrypt(const Q_UINT8 a[16], Q_UINT8 b[16])
{
int r;
UINT8 temp[4][4];
Q_UINT8 temp[4][4];
*((UINT32*)temp[0]) = *((UINT32*)(a )) ^ *((UINT32*)m_expandedKey[0][0]);
*((UINT32*)temp[1]) = *((UINT32*)(a+ 4)) ^ *((UINT32*)m_expandedKey[0][1]);
*((UINT32*)temp[2]) = *((UINT32*)(a+ 8)) ^ *((UINT32*)m_expandedKey[0][2]);
*((UINT32*)temp[3]) = *((UINT32*)(a+12)) ^ *((UINT32*)m_expandedKey[0][3]);
*((UINT32*)(b )) = *((UINT32*)T1[temp[0][0]])
^ *((UINT32*)T2[temp[1][1]])
^ *((UINT32*)T3[temp[2][2]])
^ *((UINT32*)T4[temp[3][3]]);
*((UINT32*)(b + 4)) = *((UINT32*)T1[temp[1][0]])
^ *((UINT32*)T2[temp[2][1]])
^ *((UINT32*)T3[temp[3][2]])
^ *((UINT32*)T4[temp[0][3]]);
*((UINT32*)(b + 8)) = *((UINT32*)T1[temp[2][0]])
^ *((UINT32*)T2[temp[3][1]])
^ *((UINT32*)T3[temp[0][2]])
^ *((UINT32*)T4[temp[1][3]]);
*((UINT32*)(b +12)) = *((UINT32*)T1[temp[3][0]])
^ *((UINT32*)T2[temp[0][1]])
^ *((UINT32*)T3[temp[1][2]])
^ *((UINT32*)T4[temp[2][3]]);
*((Q_UINT32*)temp[0]) = *((Q_UINT32*)(a )) ^ *((Q_UINT32*)m_expandedKey[0][0]);
*((Q_UINT32*)temp[1]) = *((Q_UINT32*)(a+ 4)) ^ *((Q_UINT32*)m_expandedKey[0][1]);
*((Q_UINT32*)temp[2]) = *((Q_UINT32*)(a+ 8)) ^ *((Q_UINT32*)m_expandedKey[0][2]);
*((Q_UINT32*)temp[3]) = *((Q_UINT32*)(a+12)) ^ *((Q_UINT32*)m_expandedKey[0][3]);
*((Q_UINT32*)(b )) = *((Q_UINT32*)T1[temp[0][0]])
^ *((Q_UINT32*)T2[temp[1][1]])
^ *((Q_UINT32*)T3[temp[2][2]])
^ *((Q_UINT32*)T4[temp[3][3]]);
*((Q_UINT32*)(b + 4)) = *((Q_UINT32*)T1[temp[1][0]])
^ *((Q_UINT32*)T2[temp[2][1]])
^ *((Q_UINT32*)T3[temp[3][2]])
^ *((Q_UINT32*)T4[temp[0][3]]);
*((Q_UINT32*)(b + 8)) = *((Q_UINT32*)T1[temp[2][0]])
^ *((Q_UINT32*)T2[temp[3][1]])
^ *((Q_UINT32*)T3[temp[0][2]])
^ *((Q_UINT32*)T4[temp[1][3]]);
*((Q_UINT32*)(b +12)) = *((Q_UINT32*)T1[temp[3][0]])
^ *((Q_UINT32*)T2[temp[0][1]])
^ *((Q_UINT32*)T3[temp[1][2]])
^ *((Q_UINT32*)T4[temp[2][3]]);
for(r = 1; r < (int)m_uRounds-1; r++)
{
*((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[r][0]);
*((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[r][1]);
*((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[r][2]);
*((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[r][3]);
*((Q_UINT32*)temp[0]) = *((Q_UINT32*)(b )) ^ *((Q_UINT32*)m_expandedKey[r][0]);
*((Q_UINT32*)temp[1]) = *((Q_UINT32*)(b+ 4)) ^ *((Q_UINT32*)m_expandedKey[r][1]);
*((Q_UINT32*)temp[2]) = *((Q_UINT32*)(b+ 8)) ^ *((Q_UINT32*)m_expandedKey[r][2]);
*((Q_UINT32*)temp[3]) = *((Q_UINT32*)(b+12)) ^ *((Q_UINT32*)m_expandedKey[r][3]);
*((UINT32*)(b )) = *((UINT32*)T1[temp[0][0]])
^ *((UINT32*)T2[temp[1][1]])
^ *((UINT32*)T3[temp[2][2]])
^ *((UINT32*)T4[temp[3][3]]);
*((UINT32*)(b + 4)) = *((UINT32*)T1[temp[1][0]])
^ *((UINT32*)T2[temp[2][1]])
^ *((UINT32*)T3[temp[3][2]])
^ *((UINT32*)T4[temp[0][3]]);
*((UINT32*)(b + 8)) = *((UINT32*)T1[temp[2][0]])
^ *((UINT32*)T2[temp[3][1]])
^ *((UINT32*)T3[temp[0][2]])
^ *((UINT32*)T4[temp[1][3]]);
*((UINT32*)(b +12)) = *((UINT32*)T1[temp[3][0]])
^ *((UINT32*)T2[temp[0][1]])
^ *((UINT32*)T3[temp[1][2]])
^ *((UINT32*)T4[temp[2][3]]);
*((Q_UINT32*)(b )) = *((Q_UINT32*)T1[temp[0][0]])
^ *((Q_UINT32*)T2[temp[1][1]])
^ *((Q_UINT32*)T3[temp[2][2]])
^ *((Q_UINT32*)T4[temp[3][3]]);
*((Q_UINT32*)(b + 4)) = *((Q_UINT32*)T1[temp[1][0]])
^ *((Q_UINT32*)T2[temp[2][1]])
^ *((Q_UINT32*)T3[temp[3][2]])
^ *((Q_UINT32*)T4[temp[0][3]]);
*((Q_UINT32*)(b + 8)) = *((Q_UINT32*)T1[temp[2][0]])
^ *((Q_UINT32*)T2[temp[3][1]])
^ *((Q_UINT32*)T3[temp[0][2]])
^ *((Q_UINT32*)T4[temp[1][3]]);
*((Q_UINT32*)(b +12)) = *((Q_UINT32*)T1[temp[3][0]])
^ *((Q_UINT32*)T2[temp[0][1]])
^ *((Q_UINT32*)T3[temp[1][2]])
^ *((Q_UINT32*)T4[temp[2][3]]);
}
*((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[m_uRounds-1][0]);
*((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[m_uRounds-1][1]);
*((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[m_uRounds-1][2]);
*((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[m_uRounds-1][3]);
*((Q_UINT32*)temp[0]) = *((Q_UINT32*)(b )) ^ *((Q_UINT32*)m_expandedKey[m_uRounds-1][0]);
*((Q_UINT32*)temp[1]) = *((Q_UINT32*)(b+ 4)) ^ *((Q_UINT32*)m_expandedKey[m_uRounds-1][1]);
*((Q_UINT32*)temp[2]) = *((Q_UINT32*)(b+ 8)) ^ *((Q_UINT32*)m_expandedKey[m_uRounds-1][2]);
*((Q_UINT32*)temp[3]) = *((Q_UINT32*)(b+12)) ^ *((Q_UINT32*)m_expandedKey[m_uRounds-1][3]);
b[ 0] = T1[temp[0][0]][1];
b[ 1] = T1[temp[1][1]][1];
b[ 2] = T1[temp[2][2]][1];
@@ -1520,66 +1520,66 @@ void Rijndael::encrypt(const UINT8 a[16], UINT8 b[16])
b[13] = T1[temp[0][1]][1];
b[14] = T1[temp[1][2]][1];
b[15] = T1[temp[2][3]][1];
*((UINT32*)(b )) ^= *((UINT32*)m_expandedKey[m_uRounds][0]);
*((UINT32*)(b+ 4)) ^= *((UINT32*)m_expandedKey[m_uRounds][1]);
*((UINT32*)(b+ 8)) ^= *((UINT32*)m_expandedKey[m_uRounds][2]);
*((UINT32*)(b+12)) ^= *((UINT32*)m_expandedKey[m_uRounds][3]);
*((Q_UINT32*)(b )) ^= *((Q_UINT32*)m_expandedKey[m_uRounds][0]);
*((Q_UINT32*)(b+ 4)) ^= *((Q_UINT32*)m_expandedKey[m_uRounds][1]);
*((Q_UINT32*)(b+ 8)) ^= *((Q_UINT32*)m_expandedKey[m_uRounds][2]);
*((Q_UINT32*)(b+12)) ^= *((Q_UINT32*)m_expandedKey[m_uRounds][3]);
}
void Rijndael::decrypt(const UINT8 a[16], UINT8 b[16])
void Rijndael::decrypt(const Q_UINT8 a[16], Q_UINT8 b[16])
{
int r;
UINT8 temp[4][4];
Q_UINT8 temp[4][4];
*((UINT32*)temp[0]) = *((UINT32*)(a )) ^ *((UINT32*)m_expandedKey[m_uRounds][0]);
*((UINT32*)temp[1]) = *((UINT32*)(a+ 4)) ^ *((UINT32*)m_expandedKey[m_uRounds][1]);
*((UINT32*)temp[2]) = *((UINT32*)(a+ 8)) ^ *((UINT32*)m_expandedKey[m_uRounds][2]);
*((UINT32*)temp[3]) = *((UINT32*)(a+12)) ^ *((UINT32*)m_expandedKey[m_uRounds][3]);
*((Q_UINT32*)temp[0]) = *((Q_UINT32*)(a )) ^ *((Q_UINT32*)m_expandedKey[m_uRounds][0]);
*((Q_UINT32*)temp[1]) = *((Q_UINT32*)(a+ 4)) ^ *((Q_UINT32*)m_expandedKey[m_uRounds][1]);
*((Q_UINT32*)temp[2]) = *((Q_UINT32*)(a+ 8)) ^ *((Q_UINT32*)m_expandedKey[m_uRounds][2]);
*((Q_UINT32*)temp[3]) = *((Q_UINT32*)(a+12)) ^ *((Q_UINT32*)m_expandedKey[m_uRounds][3]);
*((UINT32*)(b )) = *((UINT32*)T5[temp[0][0]])
^ *((UINT32*)T6[temp[3][1]])
^ *((UINT32*)T7[temp[2][2]])
^ *((UINT32*)T8[temp[1][3]]);
*((UINT32*)(b+ 4)) = *((UINT32*)T5[temp[1][0]])
^ *((UINT32*)T6[temp[0][1]])
^ *((UINT32*)T7[temp[3][2]])
^ *((UINT32*)T8[temp[2][3]]);
*((UINT32*)(b+ 8)) = *((UINT32*)T5[temp[2][0]])
^ *((UINT32*)T6[temp[1][1]])
^ *((UINT32*)T7[temp[0][2]])
^ *((UINT32*)T8[temp[3][3]]);
*((UINT32*)(b+12)) = *((UINT32*)T5[temp[3][0]])
^ *((UINT32*)T6[temp[2][1]])
^ *((UINT32*)T7[temp[1][2]])
^ *((UINT32*)T8[temp[0][3]]);
*((Q_UINT32*)(b )) = *((Q_UINT32*)T5[temp[0][0]])
^ *((Q_UINT32*)T6[temp[3][1]])
^ *((Q_UINT32*)T7[temp[2][2]])
^ *((Q_UINT32*)T8[temp[1][3]]);
*((Q_UINT32*)(b+ 4)) = *((Q_UINT32*)T5[temp[1][0]])
^ *((Q_UINT32*)T6[temp[0][1]])
^ *((Q_UINT32*)T7[temp[3][2]])
^ *((Q_UINT32*)T8[temp[2][3]]);
*((Q_UINT32*)(b+ 8)) = *((Q_UINT32*)T5[temp[2][0]])
^ *((Q_UINT32*)T6[temp[1][1]])
^ *((Q_UINT32*)T7[temp[0][2]])
^ *((Q_UINT32*)T8[temp[3][3]]);
*((Q_UINT32*)(b+12)) = *((Q_UINT32*)T5[temp[3][0]])
^ *((Q_UINT32*)T6[temp[2][1]])
^ *((Q_UINT32*)T7[temp[1][2]])
^ *((Q_UINT32*)T8[temp[0][3]]);
for(r = m_uRounds-1; r > 1; r--)
{
*((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[r][0]);
*((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[r][1]);
*((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[r][2]);
*((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[r][3]);
*((UINT32*)(b )) = *((UINT32*)T5[temp[0][0]])
^ *((UINT32*)T6[temp[3][1]])
^ *((UINT32*)T7[temp[2][2]])
^ *((UINT32*)T8[temp[1][3]]);
*((UINT32*)(b+ 4)) = *((UINT32*)T5[temp[1][0]])
^ *((UINT32*)T6[temp[0][1]])
^ *((UINT32*)T7[temp[3][2]])
^ *((UINT32*)T8[temp[2][3]]);
*((UINT32*)(b+ 8)) = *((UINT32*)T5[temp[2][0]])
^ *((UINT32*)T6[temp[1][1]])
^ *((UINT32*)T7[temp[0][2]])
^ *((UINT32*)T8[temp[3][3]]);
*((UINT32*)(b+12)) = *((UINT32*)T5[temp[3][0]])
^ *((UINT32*)T6[temp[2][1]])
^ *((UINT32*)T7[temp[1][2]])
^ *((UINT32*)T8[temp[0][3]]);
*((Q_UINT32*)temp[0]) = *((Q_UINT32*)(b )) ^ *((Q_UINT32*)m_expandedKey[r][0]);
*((Q_UINT32*)temp[1]) = *((Q_UINT32*)(b+ 4)) ^ *((Q_UINT32*)m_expandedKey[r][1]);
*((Q_UINT32*)temp[2]) = *((Q_UINT32*)(b+ 8)) ^ *((Q_UINT32*)m_expandedKey[r][2]);
*((Q_UINT32*)temp[3]) = *((Q_UINT32*)(b+12)) ^ *((Q_UINT32*)m_expandedKey[r][3]);
*((Q_UINT32*)(b )) = *((Q_UINT32*)T5[temp[0][0]])
^ *((Q_UINT32*)T6[temp[3][1]])
^ *((Q_UINT32*)T7[temp[2][2]])
^ *((Q_UINT32*)T8[temp[1][3]]);
*((Q_UINT32*)(b+ 4)) = *((Q_UINT32*)T5[temp[1][0]])
^ *((Q_UINT32*)T6[temp[0][1]])
^ *((Q_UINT32*)T7[temp[3][2]])
^ *((Q_UINT32*)T8[temp[2][3]]);
*((Q_UINT32*)(b+ 8)) = *((Q_UINT32*)T5[temp[2][0]])
^ *((Q_UINT32*)T6[temp[1][1]])
^ *((Q_UINT32*)T7[temp[0][2]])
^ *((Q_UINT32*)T8[temp[3][3]]);
*((Q_UINT32*)(b+12)) = *((Q_UINT32*)T5[temp[3][0]])
^ *((Q_UINT32*)T6[temp[2][1]])
^ *((Q_UINT32*)T7[temp[1][2]])
^ *((Q_UINT32*)T8[temp[0][3]]);
}
*((UINT32*)temp[0]) = *((UINT32*)(b )) ^ *((UINT32*)m_expandedKey[1][0]);
*((UINT32*)temp[1]) = *((UINT32*)(b+ 4)) ^ *((UINT32*)m_expandedKey[1][1]);
*((UINT32*)temp[2]) = *((UINT32*)(b+ 8)) ^ *((UINT32*)m_expandedKey[1][2]);
*((UINT32*)temp[3]) = *((UINT32*)(b+12)) ^ *((UINT32*)m_expandedKey[1][3]);
*((Q_UINT32*)temp[0]) = *((Q_UINT32*)(b )) ^ *((Q_UINT32*)m_expandedKey[1][0]);
*((Q_UINT32*)temp[1]) = *((Q_UINT32*)(b+ 4)) ^ *((Q_UINT32*)m_expandedKey[1][1]);
*((Q_UINT32*)temp[2]) = *((Q_UINT32*)(b+ 8)) ^ *((Q_UINT32*)m_expandedKey[1][2]);
*((Q_UINT32*)temp[3]) = *((Q_UINT32*)(b+12)) ^ *((Q_UINT32*)m_expandedKey[1][3]);
b[ 0] = S5[temp[0][0]];
b[ 1] = S5[temp[3][1]];
b[ 2] = S5[temp[2][2]];
@@ -1596,10 +1596,10 @@ void Rijndael::decrypt(const UINT8 a[16], UINT8 b[16])
b[13] = S5[temp[2][1]];
b[14] = S5[temp[1][2]];
b[15] = S5[temp[0][3]];
*((UINT32*)(b )) ^= *((UINT32*)m_expandedKey[0][0]);
*((UINT32*)(b+ 4)) ^= *((UINT32*)m_expandedKey[0][1]);
*((UINT32*)(b+ 8)) ^= *((UINT32*)m_expandedKey[0][2]);
*((UINT32*)(b+12)) ^= *((UINT32*)m_expandedKey[0][3]);
*((Q_UINT32*)(b )) ^= *((Q_UINT32*)m_expandedKey[0][0]);
*((Q_UINT32*)(b+ 4)) ^= *((Q_UINT32*)m_expandedKey[0][1]);
*((Q_UINT32*)(b+ 8)) ^= *((Q_UINT32*)m_expandedKey[0][2]);
*((Q_UINT32*)(b+12)) ^= *((Q_UINT32*)m_expandedKey[0][3]);
}

View File

@@ -101,9 +101,9 @@ protected:
State m_state;
Mode m_mode;
Direction m_direction;
UINT8 m_initVector[MAX_IV_SIZE];
UINT32 m_uRounds;
UINT8 m_expandedKey[_MAX_ROUNDS+1][4][4];
Q_UINT8 m_initVector[MAX_IV_SIZE];
Q_UINT32 m_uRounds;
Q_UINT8 m_expandedKey[_MAX_ROUNDS+1][4][4];
public:
//////////////////////////////////////////////////////////////////////////////////////////
// API
@@ -123,7 +123,7 @@ public:
// this CAN be binary data (it is not expected to be null terminated)
// keyLen : Rijndael::Key16Bytes , Rijndael::Key24Bytes or Rijndael::Key32Bytes
// initVector: initialization vector, you will usually use 0 here
int init(Mode mode,Direction dir,const UINT8 *key,KeyLength keyLen,UINT8 * initVector = 0);
int init(Mode mode,Direction dir,const Q_UINT8 *key,KeyLength keyLen,Q_UINT8 * initVector = 0);
// Encrypts the input array (can be binary data)
// The input array length must be a multiple of 16 bytes, the remaining part
// is DISCARDED.
@@ -131,28 +131,28 @@ public:
// Input len is in BITS!
// outBuffer must be at least inputLen / 8 bytes long.
// Returns the encrypted buffer length in BITS or an error code < 0 in case of error
int blockEncrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer);
int blockEncrypt(const Q_UINT8 *input, int inputLen, Q_UINT8 *outBuffer);
// Encrypts the input array (can be binary data)
// The input array can be any length , it is automatically padded on a 16 byte boundary.
// Input len is in BYTES!
// outBuffer must be at least (inputLen + 16) bytes long
// Returns the encrypted buffer length in BYTES or an error code < 0 in case of error
int padEncrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer);
int padEncrypt(const Q_UINT8 *input, int inputOctets, Q_UINT8 *outBuffer);
// Decrypts the input vector
// Input len is in BITS!
// outBuffer must be at least inputLen / 8 bytes long
// Returns the decrypted buffer length in BITS and an error code < 0 in case of error
int blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer);
int blockDecrypt(const Q_UINT8 *input, int inputLen, Q_UINT8 *outBuffer);
// Decrypts the input vector
// Input len is in BYTES!
// outBuffer must be at least inputLen bytes long
// Returns the decrypted buffer length in BYTES and an error code < 0 in case of error
int padDecrypt(const UINT8 *input, int inputOctets, UINT8 *outBuffer);
int padDecrypt(const Q_UINT8 *input, int inputOctets, Q_UINT8 *outBuffer);
protected:
void keySched(UINT8 key[_MAX_KEY_COLUMNS][4]);
void keySched(Q_UINT8 key[_MAX_KEY_COLUMNS][4]);
void keyEncToDec();
void encrypt(const UINT8 a[16], UINT8 b[16]);
void decrypt(const UINT8 a[16], UINT8 b[16]);
void encrypt(const Q_UINT8 a[16], Q_UINT8 b[16]);
void decrypt(const Q_UINT8 a[16], Q_UINT8 b[16]);
};
#endif // _RIJNDAEL_H_

View File

@@ -1,261 +1,261 @@
/*
100% free public domain implementation of the SHA-1 algorithm
by Dominik Reichl <dominik.reichl@t-online.de>
Version 1.5 - 2005-01-01
- 64-bit compiler compatibility added
- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
- Removed unnecessary variable initializations
- ROL32 improvement for the Microsoft compiler (using _rotl)
======== Test Vectors (from FIPS PUB 180-1) ========
SHA1("abc") =
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
SHA1(A million repetitions of "a") =
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#include "global.h"
#include "sha1.h"
#define SHA1_MAX_FILE_BUFFER 8000
// Rotate x bits to the left
#ifndef ROL32
#ifdef _MSC_VER
#define ROL32(_val32, _nBits) _rotl(_val32, _nBits)
#else
#define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
#endif
#endif
#ifdef KEEPASS_LITTLE_ENDIAN
#define SHABLK0(i) (m_block->l[i] = \
(ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
#else
#define SHABLK0(i) (m_block->l[i])
#endif
#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
// SHA-1 rounds
#define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
#define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
#define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
#define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
#define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }
CSHA1::CSHA1()
{
m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
Reset();
}
CSHA1::~CSHA1()
{
Reset();
}
void CSHA1::Reset()
{
// SHA1 initialization constants
m_state[0] = 0x67452301;
m_state[1] = 0xEFCDAB89;
m_state[2] = 0x98BADCFE;
m_state[3] = 0x10325476;
m_state[4] = 0xC3D2E1F0;
m_count[0] = 0;
m_count[1] = 0;
}
void CSHA1::Update(unsigned char* data, int len){
UINT_32 i, j;
j = (m_count[0] >> 3) & 63;
if((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
m_count[1] += (len >> 29);
if((j + len) > 63)
{
i = 64 - j;
memcpy(&m_buffer[j], data, i);
Transform(m_state, m_buffer);
for( ; i + 63 < len; i += 64) Transform(m_state, &data[i]);
j = 0;
}
else i = 0;
memcpy(&m_buffer[j], &data[i], len - i);
}
void CSHA1::Transform(UINT_32 *state, UINT_8 *buffer)
{
// Copy state[] to working vars
UINT_32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
memcpy(m_block, buffer, 64);
// 4 rounds of 20 operations each. Loop unrolled.
_R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
_R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
_R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
_R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
_R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
_R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
_R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
_R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
_R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
_R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
_R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
_R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
_R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
_R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
_R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
_R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
_R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
_R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
_R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
_R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
// Add the working vars back into state
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
// Wipe variables
#ifdef SHA1_WIPE_VARIABLES
a = b = c = d = e = 0;
#endif
}
// Hash in file contents
bool CSHA1::HashFile(char *szFileName)
{
unsigned long ulFileSize, ulRest, ulBlocks;
unsigned long i;
UINT_8 uData[SHA1_MAX_FILE_BUFFER];
FILE *fIn;
if(szFileName == NULL) return false;
fIn = fopen(szFileName, "rb");
if(fIn == NULL) return false;
fseek(fIn, 0, SEEK_END);
ulFileSize = (unsigned long)ftell(fIn);
fseek(fIn, 0, SEEK_SET);
if(ulFileSize != 0)
{
ulBlocks = ulFileSize / SHA1_MAX_FILE_BUFFER;
ulRest = ulFileSize % SHA1_MAX_FILE_BUFFER;
}
else
{
ulBlocks = 0;
ulRest = 0;
}
for(i = 0; i < ulBlocks; i++)
{
fread(uData, 1, SHA1_MAX_FILE_BUFFER, fIn);
Update((UINT_8 *)uData, SHA1_MAX_FILE_BUFFER);
}
if(ulRest != 0)
{
fread(uData, 1, ulRest, fIn);
Update((UINT_8 *)uData, ulRest);
}
fclose(fIn); fIn = NULL;
return true;
}
void CSHA1::Final()
{
UINT_32 i;
UINT_8 finalcount[8];
for(i = 0; i < 8; i++)
finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)]
>> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
Update((UINT_8 *)"\200", 1);
while ((m_count[0] & 504) != 448)
Update((UINT_8 *)"\0", 1);
Update(finalcount, 8); // Cause a SHA1Transform()
for(i = 0; i < 20; i++)
{
m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
}
// Wipe variables for security reasons
#ifdef SHA1_WIPE_VARIABLES
i = 0;
memset(m_buffer, 0, 64);
memset(m_state, 0, 20);
memset(m_count, 0, 8);
memset(finalcount, 0, 8);
Transform(m_state, m_buffer);
#endif
}
// Get the final hash as a pre-formatted string
void CSHA1::ReportHash(char *szReport, unsigned char uReportType)
{
unsigned char i;
char szTemp[16];
if(szReport == NULL) return;
if(uReportType == REPORT_HEX)
{
sprintf(szTemp, "%02X", m_digest[0]);
strcat(szReport, szTemp);
for(i = 1; i < 20; i++)
{
sprintf(szTemp, " %02X", m_digest[i]);
strcat(szReport, szTemp);
}
}
else if(uReportType == REPORT_DIGIT)
{
sprintf(szTemp, "%u", m_digest[0]);
strcat(szReport, szTemp);
for(i = 1; i < 20; i++)
{
sprintf(szTemp, " %u", m_digest[i]);
strcat(szReport, szTemp);
}
}
else strcpy(szReport, "Error: Unknown report type!");
}
// Get the raw message digest
void CSHA1::GetHash(UINT_8 *puDest)
{
memcpy(puDest, m_digest, 20);
}
/*
100% free public domain implementation of the SHA-1 algorithm
by Dominik Reichl <dominik.reichl@t-online.de>
Version 1.5 - 2005-01-01
- 64-bit compiler compatibility added
- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
- Removed unnecessary variable initializations
- ROL32 improvement for the Microsoft compiler (using _rotl)
======== Test Vectors (from FIPS PUB 180-1) ========
SHA1("abc") =
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
SHA1(A million repetitions of "a") =
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#include "global.h"
#include "sha1.h"
#define SHA1_MAX_FILE_BUFFER 8000
// Rotate x bits to the left
#ifndef ROL32
#ifdef _MSC_VER
#define ROL32(_val32, _nBits) _rotl(_val32, _nBits)
#else
#define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits))))
#endif
#endif
#ifdef KEEPASS_LITTLE_ENDIAN
#define SHABLK0(i) (m_block->l[i] = \
(ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF))
#else
#define SHABLK0(i) (m_block->l[i])
#endif
#define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \
^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1))
// SHA-1 rounds
#define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
#define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); }
#define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); }
#define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); }
#define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); }
CSHA1::CSHA1()
{
m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace;
Reset();
}
CSHA1::~CSHA1()
{
Reset();
}
void CSHA1::Reset()
{
// SHA1 initialization constants
m_state[0] = 0x67452301;
m_state[1] = 0xEFCDAB89;
m_state[2] = 0x98BADCFE;
m_state[3] = 0x10325476;
m_state[4] = 0xC3D2E1F0;
m_count[0] = 0;
m_count[1] = 0;
}
void CSHA1::Update(unsigned char* data, int len){
Q_UINT_32 i, j;
j = (m_count[0] >> 3) & 63;
if((m_count[0] += len << 3) < (len << 3)) m_count[1]++;
m_count[1] += (len >> 29);
if((j + len) > 63)
{
i = 64 - j;
memcpy(&m_buffer[j], data, i);
Transform(m_state, m_buffer);
for( ; i + 63 < len; i += 64) Transform(m_state, &data[i]);
j = 0;
}
else i = 0;
memcpy(&m_buffer[j], &data[i], len - i);
}
void CSHA1::Transform(Q_UINT_32 *state, Q_UINT_8 *buffer)
{
// Copy state[] to working vars
Q_UINT_32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
memcpy(m_block, buffer, 64);
// 4 rounds of 20 operations each. Loop unrolled.
_R0(a,b,c,d,e, 0); _R0(e,a,b,c,d, 1); _R0(d,e,a,b,c, 2); _R0(c,d,e,a,b, 3);
_R0(b,c,d,e,a, 4); _R0(a,b,c,d,e, 5); _R0(e,a,b,c,d, 6); _R0(d,e,a,b,c, 7);
_R0(c,d,e,a,b, 8); _R0(b,c,d,e,a, 9); _R0(a,b,c,d,e,10); _R0(e,a,b,c,d,11);
_R0(d,e,a,b,c,12); _R0(c,d,e,a,b,13); _R0(b,c,d,e,a,14); _R0(a,b,c,d,e,15);
_R1(e,a,b,c,d,16); _R1(d,e,a,b,c,17); _R1(c,d,e,a,b,18); _R1(b,c,d,e,a,19);
_R2(a,b,c,d,e,20); _R2(e,a,b,c,d,21); _R2(d,e,a,b,c,22); _R2(c,d,e,a,b,23);
_R2(b,c,d,e,a,24); _R2(a,b,c,d,e,25); _R2(e,a,b,c,d,26); _R2(d,e,a,b,c,27);
_R2(c,d,e,a,b,28); _R2(b,c,d,e,a,29); _R2(a,b,c,d,e,30); _R2(e,a,b,c,d,31);
_R2(d,e,a,b,c,32); _R2(c,d,e,a,b,33); _R2(b,c,d,e,a,34); _R2(a,b,c,d,e,35);
_R2(e,a,b,c,d,36); _R2(d,e,a,b,c,37); _R2(c,d,e,a,b,38); _R2(b,c,d,e,a,39);
_R3(a,b,c,d,e,40); _R3(e,a,b,c,d,41); _R3(d,e,a,b,c,42); _R3(c,d,e,a,b,43);
_R3(b,c,d,e,a,44); _R3(a,b,c,d,e,45); _R3(e,a,b,c,d,46); _R3(d,e,a,b,c,47);
_R3(c,d,e,a,b,48); _R3(b,c,d,e,a,49); _R3(a,b,c,d,e,50); _R3(e,a,b,c,d,51);
_R3(d,e,a,b,c,52); _R3(c,d,e,a,b,53); _R3(b,c,d,e,a,54); _R3(a,b,c,d,e,55);
_R3(e,a,b,c,d,56); _R3(d,e,a,b,c,57); _R3(c,d,e,a,b,58); _R3(b,c,d,e,a,59);
_R4(a,b,c,d,e,60); _R4(e,a,b,c,d,61); _R4(d,e,a,b,c,62); _R4(c,d,e,a,b,63);
_R4(b,c,d,e,a,64); _R4(a,b,c,d,e,65); _R4(e,a,b,c,d,66); _R4(d,e,a,b,c,67);
_R4(c,d,e,a,b,68); _R4(b,c,d,e,a,69); _R4(a,b,c,d,e,70); _R4(e,a,b,c,d,71);
_R4(d,e,a,b,c,72); _R4(c,d,e,a,b,73); _R4(b,c,d,e,a,74); _R4(a,b,c,d,e,75);
_R4(e,a,b,c,d,76); _R4(d,e,a,b,c,77); _R4(c,d,e,a,b,78); _R4(b,c,d,e,a,79);
// Add the working vars back into state
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
// Wipe variables
#ifdef SHA1_WIPE_VARIABLES
a = b = c = d = e = 0;
#endif
}
// Hash in file contents
bool CSHA1::HashFile(char *szFileName)
{
unsigned long ulFileSize, ulRest, ulBlocks;
unsigned long i;
Q_UINT_8 uData[SHA1_MAX_FILE_BUFFER];
FILE *fIn;
if(szFileName == NULL) return false;
fIn = fopen(szFileName, "rb");
if(fIn == NULL) return false;
fseek(fIn, 0, SEEK_END);
ulFileSize = (unsigned long)ftell(fIn);
fseek(fIn, 0, SEEK_SET);
if(ulFileSize != 0)
{
ulBlocks = ulFileSize / SHA1_MAX_FILE_BUFFER;
ulRest = ulFileSize % SHA1_MAX_FILE_BUFFER;
}
else
{
ulBlocks = 0;
ulRest = 0;
}
for(i = 0; i < ulBlocks; i++)
{
fread(uData, 1, SHA1_MAX_FILE_BUFFER, fIn);
Update((Q_UINT_8 *)uData, SHA1_MAX_FILE_BUFFER);
}
if(ulRest != 0)
{
fread(uData, 1, ulRest, fIn);
Update((Q_UINT_8 *)uData, ulRest);
}
fclose(fIn); fIn = NULL;
return true;
}
void CSHA1::Final()
{
Q_UINT_32 i;
Q_UINT_8 finalcount[8];
for(i = 0; i < 8; i++)
finalcount[i] = (Q_UINT_8)((m_count[((i >= 4) ? 0 : 1)]
>> ((3 - (i & 3)) * 8) ) & 255); // Endian independent
Update((Q_UINT_8 *)"\200", 1);
while ((m_count[0] & 504) != 448)
Update((Q_UINT_8 *)"\0", 1);
Update(finalcount, 8); // Cause a SHA1Transform()
for(i = 0; i < 20; i++)
{
m_digest[i] = (Q_UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8) ) & 255);
}
// Wipe variables for security reasons
#ifdef SHA1_WIPE_VARIABLES
i = 0;
memset(m_buffer, 0, 64);
memset(m_state, 0, 20);
memset(m_count, 0, 8);
memset(finalcount, 0, 8);
Transform(m_state, m_buffer);
#endif
}
// Get the final hash as a pre-formatted string
void CSHA1::ReportHash(char *szReport, unsigned char uReportType)
{
unsigned char i;
char szTemp[16];
if(szReport == NULL) return;
if(uReportType == REPORT_HEX)
{
sprintf(szTemp, "%02X", m_digest[0]);
strcat(szReport, szTemp);
for(i = 1; i < 20; i++)
{
sprintf(szTemp, " %02X", m_digest[i]);
strcat(szReport, szTemp);
}
}
else if(uReportType == REPORT_DIGIT)
{
sprintf(szTemp, "%u", m_digest[0]);
strcat(szReport, szTemp);
for(i = 1; i < 20; i++)
{
sprintf(szTemp, " %u", m_digest[i]);
strcat(szReport, szTemp);
}
}
else strcpy(szReport, "Error: Unknown report type!");
}
// Get the raw message digest
void CSHA1::GetHash(Q_UINT_8 *puDest)
{
memcpy(puDest, m_digest, 20);
}

View File

@@ -1,99 +1,99 @@
/*
100% free public domain implementation of the SHA-1 algorithm
by Dominik Reichl <dominik.reichl@t-online.de>
Version 1.5 - 2005-01-01
- 64-bit compiler compatibility added
- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
- Removed unnecessary variable initializations
- ROL32 improvement for the Microsoft compiler (using _rotl)
======== Test Vectors (from FIPS PUB 180-1) ========
SHA1("abc") =
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
SHA1(A million repetitions of "a") =
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#ifndef ___SHA1_HDR___
#define ___SHA1_HDR___
#include <stdio.h> // Needed for file access
#include <memory.h> // Needed for memset and memcpy
#include <string.h> // Needed for strcat and strcpy
// If you're compiling big endian, just comment out the following line
// #define or #undef this, if you want the CSHA1 class to wipe all
// temporary variables after processing
#define SHA1_WIPE_VARIABLES
/////////////////////////////////////////////////////////////////////////////
// Define 8- and 32-bit variables
#ifndef UINT_32
#define UINT_8 unsigned char
#if (ULONG_MAX == 0xFFFFFFFF)
#define UINT_32 unsigned long
#else
#define UINT_32 unsigned int
#endif
#endif
/////////////////////////////////////////////////////////////////////////////
// Declare SHA1 workspace
typedef union
{
UINT_8 c[64];
UINT_32 l[16];
} SHA1_WORKSPACE_BLOCK;
class CSHA1
{
public:
// Two different formats for ReportHash(...)
enum
{
REPORT_HEX = 0,
REPORT_DIGIT = 1
};
// Constructor and Destructor
CSHA1();
~CSHA1();
UINT_32 m_state[5];
UINT_32 m_count[2];
UINT_8 m_buffer[64];
UINT_8 m_digest[20];
void Reset();
// Update the hash value
void Update(unsigned char* data, int len);
bool HashFile(char *szFileName);
// Finalize hash and report
void Final();
void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
void GetHash(UINT_8 *puDest);
private:
// Private SHA-1 transformation
void Transform(UINT_32 *state, UINT_8 *buffer);
// Member variables
UINT_8 m_workspace[64];
SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
};
#endif
/*
100% free public domain implementation of the SHA-1 algorithm
by Dominik Reichl <dominik.reichl@t-online.de>
Version 1.5 - 2005-01-01
- 64-bit compiler compatibility added
- Made variable wiping optional (define SHA1_WIPE_VARIABLES)
- Removed unnecessary variable initializations
- ROL32 improvement for the Microsoft compiler (using _rotl)
======== Test Vectors (from FIPS PUB 180-1) ========
SHA1("abc") =
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") =
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
SHA1(A million repetitions of "a") =
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
#ifndef ___SHA1_HDR___
#define ___SHA1_HDR___
#include <stdio.h> // Needed for file access
#include <memory.h> // Needed for memset and memcpy
#include <string.h> // Needed for strcat and strcpy
// If you're compiling big endian, just comment out the following line
// #define or #undef this, if you want the CSHA1 class to wipe all
// temporary variables after processing
#define SHA1_WIPE_VARIABLES
/////////////////////////////////////////////////////////////////////////////
// Define 8- and 32-bit variables
#ifndef Q_UINT_32
#define Q_UINT_8 unsigned char
#if (ULONG_MAX == 0xFFFFFFFF)
#define Q_UINT_32 unsigned long
#else
#define Q_UINT_32 unsigned int
#endif
#endif
/////////////////////////////////////////////////////////////////////////////
// Declare SHA1 workspace
typedef union
{
Q_UINT_8 c[64];
Q_UINT_32 l[16];
} SHA1_WORKSPACE_BLOCK;
class CSHA1
{
public:
// Two different formats for ReportHash(...)
enum
{
REPORT_HEX = 0,
REPORT_DIGIT = 1
};
// Constructor and Destructor
CSHA1();
~CSHA1();
Q_UINT_32 m_state[5];
Q_UINT_32 m_count[2];
Q_UINT_8 m_buffer[64];
Q_UINT_8 m_digest[20];
void Reset();
// Update the hash value
void Update(unsigned char* data, int len);
bool HashFile(char *szFileName);
// Finalize hash and report
void Final();
void ReportHash(char *szReport, unsigned char uReportType = REPORT_HEX);
void GetHash(Q_UINT_8 *puDest);
private:
// Private SHA-1 transformation
void Transform(Q_UINT_32 *state, Q_UINT_8 *buffer);
// Member variables
Q_UINT_8 m_workspace[64];
SHA1_WORKSPACE_BLOCK *m_block; // SHA1 pointer to the byte array above
};
#endif

View File

@@ -22,7 +22,7 @@
#include "sha256.h"
#define GET_UINT32(n,b,i) \
#define GET_Q_UINT32(n,b,i) \
{ \
(n) = ( (uint32) (b)[(i) ] << 24 ) \
| ( (uint32) (b)[(i) + 1] << 16 ) \
@@ -30,7 +30,7 @@
| ( (uint32) (b)[(i) + 3] ); \
}
#define PUT_UINT32(n,b,i) \
#define PUT_Q_UINT32(n,b,i) \
{ \
(b)[(i) ] = (uint8) ( (n) >> 24 ); \
(b)[(i) + 1] = (uint8) ( (n) >> 16 ); \
@@ -58,22 +58,22 @@ void sha256_process( sha256_context *ctx, uint8 data[64] )
uint32 temp1, temp2, W[64];
uint32 A, B, C, D, E, F, G, H;
GET_UINT32( W[0], data, 0 );
GET_UINT32( W[1], data, 4 );
GET_UINT32( W[2], data, 8 );
GET_UINT32( W[3], data, 12 );
GET_UINT32( W[4], data, 16 );
GET_UINT32( W[5], data, 20 );
GET_UINT32( W[6], data, 24 );
GET_UINT32( W[7], data, 28 );
GET_UINT32( W[8], data, 32 );
GET_UINT32( W[9], data, 36 );
GET_UINT32( W[10], data, 40 );
GET_UINT32( W[11], data, 44 );
GET_UINT32( W[12], data, 48 );
GET_UINT32( W[13], data, 52 );
GET_UINT32( W[14], data, 56 );
GET_UINT32( W[15], data, 60 );
GET_Q_UINT32( W[0], data, 0 );
GET_Q_UINT32( W[1], data, 4 );
GET_Q_UINT32( W[2], data, 8 );
GET_Q_UINT32( W[3], data, 12 );
GET_Q_UINT32( W[4], data, 16 );
GET_Q_UINT32( W[5], data, 20 );
GET_Q_UINT32( W[6], data, 24 );
GET_Q_UINT32( W[7], data, 28 );
GET_Q_UINT32( W[8], data, 32 );
GET_Q_UINT32( W[9], data, 36 );
GET_Q_UINT32( W[10], data, 40 );
GET_Q_UINT32( W[11], data, 44 );
GET_Q_UINT32( W[12], data, 48 );
GET_Q_UINT32( W[13], data, 52 );
GET_Q_UINT32( W[14], data, 56 );
GET_Q_UINT32( W[15], data, 60 );
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
@@ -241,8 +241,8 @@ void sha256_finish( sha256_context *ctx, uint8 digest[32] )
| ( ctx->total[1] << 3 );
low = ( ctx->total[0] << 3 );
PUT_UINT32( high, msglen, 0 );
PUT_UINT32( low, msglen, 4 );
PUT_Q_UINT32( high, msglen, 0 );
PUT_Q_UINT32( low, msglen, 4 );
last = ctx->total[0] & 0x3F;
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
@@ -250,14 +250,14 @@ void sha256_finish( sha256_context *ctx, uint8 digest[32] )
sha256_update( ctx, sha256_padding, padn );
sha256_update( ctx, msglen, 8 );
PUT_UINT32( ctx->state[0], digest, 0 );
PUT_UINT32( ctx->state[1], digest, 4 );
PUT_UINT32( ctx->state[2], digest, 8 );
PUT_UINT32( ctx->state[3], digest, 12 );
PUT_UINT32( ctx->state[4], digest, 16 );
PUT_UINT32( ctx->state[5], digest, 20 );
PUT_UINT32( ctx->state[6], digest, 24 );
PUT_UINT32( ctx->state[7], digest, 28 );
PUT_Q_UINT32( ctx->state[0], digest, 0 );
PUT_Q_UINT32( ctx->state[1], digest, 4 );
PUT_Q_UINT32( ctx->state[2], digest, 8 );
PUT_Q_UINT32( ctx->state[3], digest, 12 );
PUT_Q_UINT32( ctx->state[4], digest, 16 );
PUT_Q_UINT32( ctx->state[5], digest, 20 );
PUT_Q_UINT32( ctx->state[6], digest, 24 );
PUT_Q_UINT32( ctx->state[7], digest, 28 );
}
#ifdef TEST

View File

@@ -40,7 +40,7 @@ CTwofish::~CTwofish()
{
}
bool CTwofish::init(UINT8 *pKey, unsigned long uKeyLen, UINT8 *initVector)
bool CTwofish::init(Q_UINT8 *pKey, unsigned long uKeyLen, Q_UINT8 *initVector)
{
//ASSERT(pKey != NULL);
if(pKey == NULL) return false;
@@ -61,10 +61,10 @@ bool CTwofish::init(UINT8 *pKey, unsigned long uKeyLen, UINT8 *initVector)
return true;
}
int CTwofish::padEncrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer)
int CTwofish::padEncrypt(Q_UINT8 *pInput, int nInputOctets, Q_UINT8 *pOutBuffer)
{
int i, numBlocks, padLen;
UINT8 block[16], *iv;
Q_UINT8 block[16], *iv;
//ASSERT((pInput != NULL) && (nInputOctets != NULL) && (pOutBuffer != NULL));
if((pInput == NULL) || (nInputOctets <= 0) || (pOutBuffer == NULL)) return 0;
@@ -74,10 +74,10 @@ int CTwofish::padEncrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer)
iv = m_pInitVector;
for(i = numBlocks; i > 0; i--)
{
((UINT32*)block)[0] = ((UINT32*)pInput)[0] ^ ((UINT32*)iv)[0];
((UINT32*)block)[1] = ((UINT32*)pInput)[1] ^ ((UINT32*)iv)[1];
((UINT32*)block)[2] = ((UINT32*)pInput)[2] ^ ((UINT32*)iv)[2];
((UINT32*)block)[3] = ((UINT32*)pInput)[3] ^ ((UINT32*)iv)[3];
((Q_UINT32*)block)[0] = ((Q_UINT32*)pInput)[0] ^ ((Q_UINT32*)iv)[0];
((Q_UINT32*)block)[1] = ((Q_UINT32*)pInput)[1] ^ ((Q_UINT32*)iv)[1];
((Q_UINT32*)block)[2] = ((Q_UINT32*)pInput)[2] ^ ((Q_UINT32*)iv)[2];
((Q_UINT32*)block)[3] = ((Q_UINT32*)pInput)[3] ^ ((Q_UINT32*)iv)[3];
Twofish_encrypt(&m_key, (Twofish_Byte *)block, (Twofish_Byte *)pOutBuffer);
@@ -90,12 +90,12 @@ int CTwofish::padEncrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer)
for (i = 0; i < 16 - padLen; i++)
{
block[i] = (UINT8)(pInput[i] ^ iv[i]);
block[i] = (Q_UINT8)(pInput[i] ^ iv[i]);
}
for (i = 16 - padLen; i < 16; i++)
{
block[i] = (UINT8)((UINT8)padLen ^ iv[i]);
block[i] = (Q_UINT8)((Q_UINT8)padLen ^ iv[i]);
}
Twofish_encrypt(&m_key, (Twofish_Byte *)block, (Twofish_Byte *)pOutBuffer);
@@ -103,11 +103,11 @@ int CTwofish::padEncrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer)
return 16 * (numBlocks + 1);
}
int CTwofish::padDecrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer)
int CTwofish::padDecrypt(Q_UINT8 *pInput, int nInputOctets, Q_UINT8 *pOutBuffer)
{
int i, numBlocks, padLen;
UINT8 block[16];
UINT32 iv[4];
Q_UINT8 block[16];
Q_UINT32 iv[4];
//ASSERT((pInput != NULL) && (nInputOctets != NULL) && (pOutBuffer != NULL));
if((pInput == NULL) || (nInputOctets <= 0) || (pOutBuffer == NULL)) return 0;
@@ -121,10 +121,10 @@ int CTwofish::padDecrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer)
for(i = numBlocks - 1; i > 0; i--)
{
Twofish_decrypt(&m_key, (Twofish_Byte *)pInput, (Twofish_Byte *)block);
((UINT32*)block)[0] ^= iv[0];
((UINT32*)block)[1] ^= iv[1];
((UINT32*)block)[2] ^= iv[2];
((UINT32*)block)[3] ^= iv[3];
((Q_UINT32*)block)[0] ^= iv[0];
((Q_UINT32*)block)[1] ^= iv[1];
((Q_UINT32*)block)[2] ^= iv[2];
((Q_UINT32*)block)[3] ^= iv[3];
memcpy(iv, pInput, 16);
memcpy(pOutBuffer, block, 16);
pInput += 16;
@@ -132,10 +132,10 @@ int CTwofish::padDecrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer)
}
Twofish_decrypt(&m_key, (Twofish_Byte *)pInput, (Twofish_Byte *)block);
((UINT32*)block)[0] ^= iv[0];
((UINT32*)block)[1] ^= iv[1];
((UINT32*)block)[2] ^= iv[2];
((UINT32*)block)[3] ^= iv[3];
((Q_UINT32*)block)[0] ^= iv[0];
((Q_UINT32*)block)[1] ^= iv[1];
((Q_UINT32*)block)[2] ^= iv[2];
((Q_UINT32*)block)[3] ^= iv[3];
padLen = block[15];
if(padLen <= 0 || padLen > 16) return -1;
for(i = 16 - padLen; i < 16; i++)

View File

@@ -39,14 +39,14 @@ public:
CTwofish();
virtual ~CTwofish();
bool init(UINT8 *pKey, unsigned long uKeyLen, UINT8 *initVector = NULL);
bool init(Q_UINT8 *pKey, unsigned long uKeyLen, Q_UINT8 *initVector = NULL);
int padEncrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer);
int padDecrypt(UINT8 *pInput, int nInputOctets, UINT8 *pOutBuffer);
int padEncrypt(Q_UINT8 *pInput, int nInputOctets, Q_UINT8 *pOutBuffer);
int padDecrypt(Q_UINT8 *pInput, int nInputOctets, Q_UINT8 *pOutBuffer);
private:
Twofish_key m_key;
UINT8 m_pInitVector[16];
Q_UINT8 m_pInitVector[16];
};
#endif

View File

@@ -245,7 +245,7 @@
*
* Unfortunately there is no portable way of writing the constant
* 0xffffffff. You don't know which suffix to use (U, or UL?)
* The UINT32_MASK definition uses a bit of trickery. Shift-left
* The Q_UINT32_MASK definition uses a bit of trickery. Shift-left
* is only defined if the shift amount is strictly less than the size
* of the UInt32, so we can't use (1<<32). The answer it to take the value
* 2, cast it to a UInt32, shift it left 31 positions, and subtract one.
@@ -263,11 +263,11 @@
* For example, MS compilers have the __rotl and __rotr functions
* that generate x86 rotation instructions.
*/
#define UINT32_MASK ( (((Twofish_UInt32)2)<<31) - 1 )
#define Q_UINT32_MASK ( (((Twofish_UInt32)2)<<31) - 1 )
#ifndef _MSC_VER
#define ROL32(x,n) ( (x)<<(n) | ((x) & UINT32_MASK) >> (32-(n)) )
#define ROR32(x,n) ( (x)>>(n) | ((x) & UINT32_MASK) << (32-(n)) )
#define ROL32(x,n) ( (x)<<(n) | ((x) & Q_UINT32_MASK) >> (32-(n)) )
#define ROR32(x,n) ( (x)>>(n) | ((x) & Q_UINT32_MASK) << (32-(n)) )
#else
#define ROL32(x,n) (_lrotl((x), (n)))
#define ROR32(x,n) (_lrotr((x), (n)))
@@ -306,7 +306,7 @@
* This macro does not affect the conversion of the inputs and outputs
* of the cipher. See the CONVERT_USING_CASTS macro for that.
*/
#define SELECT_BYTE_FROM_UINT32_IN_MEMORY 0 /* default = 0 */
#define SELECT_BYTE_FROM_Q_UINT32_IN_MEMORY 0 /* default = 0 */
/*
@@ -331,7 +331,7 @@
* This option does not work unless a UInt32 is exactly 32 bits.
*
* This macro only changes the reading/writing of the plaintext/ciphertext.
* See the SELECT_BYTE_FROM_UINT32_IN_MEMORY to affect the way in which
* See the SELECT_BYTE_FROM_Q_UINT32_IN_MEMORY to affect the way in which
* a UInt32 is split into 4 bytes for the S-box selection.
*/
#define CONVERT_USING_CASTS 0 /* default = 0 */
@@ -339,7 +339,7 @@
/*
* Endianness switch.
* Only relevant if SELECT_BYTE_FROM_UINT32_IN_MEMORY or
* Only relevant if SELECT_BYTE_FROM_Q_UINT32_IN_MEMORY or
* CONVERT_USING_CASTS is set.
*
* Set to 1 on a big-endian machine, and to 0 on a little-endian machine.
@@ -407,7 +407,7 @@
/*
* Compute byte offset within a UInt32 stored in memory.
*
* This is only used when SELECT_BYTE_FROM_UINT32_IN_MEMORY is set.
* This is only used when SELECT_BYTE_FROM_Q_UINT32_IN_MEMORY is set.
*
* The input is the byte number 0..3, 0 for least significant.
* Note the use of sizeof() to support UInt32 types that are larger
@@ -424,7 +424,7 @@
* Macro to get Byte no. b from UInt32 value X.
* We use two different definition, depending on the settings.
*/
#if SELECT_BYTE_FROM_UINT32_IN_MEMORY
#if SELECT_BYTE_FROM_Q_UINT32_IN_MEMORY
/* Pick the byte from the memory in which X is stored. */
#define SELECT_BYTE( X, b ) (((Twofish_Byte *)(&(X)))[BYTE_OFFSET(b)])
#else