|
@@ -34,6 +34,7 @@
|
|
|
|
|
|
typedef uchar u8;
|
|
|
typedef u32int u32;
|
|
|
+
|
|
|
#define FULL_UNROLL
|
|
|
|
|
|
static const u32 Td0[256];
|
|
@@ -61,12 +62,14 @@ setupAESstate(AESstate *s, uchar key[], int keybytes, uchar *ivec)
|
|
|
memmove(s->ivec, ivec, AESbsize);
|
|
|
if(keybytes==16 || keybytes==24 || keybytes==32)
|
|
|
s->setup = 0xcafebabe;
|
|
|
- // else rijndaelKeySetup was invalid
|
|
|
+ /* else rijndaelKeySetup was invalid */
|
|
|
}
|
|
|
|
|
|
-// Define by analogy with desCBCencrypt; AES modes are not standardized yet.
|
|
|
-// Because of the way that non-multiple-of-16 buffers are handled,
|
|
|
-// the decryptor must be fed buffers of the same size as the encryptor.
|
|
|
+/*
|
|
|
+ * Define by analogy with desCBCencrypt; AES modes are not standardized yet.
|
|
|
+ * Because of the way that non-multiple-of-16 buffers are handled,
|
|
|
+ * the decryptor must be fed buffers of the same size as the encryptor.
|
|
|
+ */
|
|
|
void
|
|
|
aesCBCencrypt(uchar *p, int len, AESstate *s)
|
|
|
{
|
|
@@ -126,23 +129,26 @@ aesCBCdecrypt(uchar *p, int len, AESstate *s)
|
|
|
*
|
|
|
* @return the number of rounds for the given cipher key size.
|
|
|
*/
|
|
|
-static int rijndaelKeySetup(u32 erk[/*4*(Nr + 1)*/], u32 drk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits) {
|
|
|
+static int
|
|
|
+rijndaelKeySetup(u32 erk[/* 4*(Nr + 1) */], u32 drk[/* 4*(Nr + 1) */],
|
|
|
+ const u8 cipherKey[], int keyBits)
|
|
|
+{
|
|
|
int Nr, i;
|
|
|
|
|
|
/* expand the cipher key: */
|
|
|
Nr = rijndaelKeySetupEnc(erk, cipherKey, keyBits);
|
|
|
|
|
|
/*
|
|
|
- * invert the order of the round keys and
|
|
|
- * apply the inverse MixColumn transform to all round keys but the first and the last
|
|
|
+ * invert the order of the round keys and apply the inverse MixColumn
|
|
|
+ * transform to all round keys but the first and the last
|
|
|
*/
|
|
|
- drk[0 ] = erk[4*Nr ];
|
|
|
+ drk[0 ] = erk[4*Nr ];
|
|
|
drk[1 ] = erk[4*Nr + 1];
|
|
|
- drk[2 ] = erk[4*Nr + 2];
|
|
|
+ drk[2 ] = erk[4*Nr + 2];
|
|
|
drk[3 ] = erk[4*Nr + 3];
|
|
|
- drk[4*Nr ] = erk[0 ];
|
|
|
+ drk[4*Nr ] = erk[0 ];
|
|
|
drk[4*Nr + 1] = erk[1 ];
|
|
|
- drk[4*Nr + 2] = erk[2 ];
|
|
|
+ drk[4*Nr + 2] = erk[2 ];
|
|
|
drk[4*Nr + 3] = erk[3 ];
|
|
|
erk += 4 * Nr;
|
|
|
for (i = 1; i < Nr; i++) {
|
|
@@ -851,7 +857,8 @@ static const u8 Td4[256] = {
|
|
|
static const u32 rcon[] = {
|
|
|
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
|
|
0x10000000, 0x20000000, 0x40000000, 0x80000000,
|
|
|
- 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
|
|
|
+ 0x1B000000, 0x36000000,
|
|
|
+ /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
|
|
|
};
|
|
|
|
|
|
#define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
|
|
@@ -860,17 +867,21 @@ static const u32 rcon[] = {
|
|
|
#define GETU32(p) SWAP(*((u32 *)(p)))
|
|
|
#define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
|
|
|
#else
|
|
|
-#define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]))
|
|
|
-#define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); }
|
|
|
+#define GETU32(pt) (((u32)(pt)[0]<<24) ^ ((u32)(pt)[1]<<16) ^ \
|
|
|
+ ((u32)(pt)[2]<<8) ^ ((u32)(pt)[3]))
|
|
|
+#define PUTU32(ct, st) { (ct)[0] = (u8)((st)>>24); (ct)[1] = (u8)((st)>>16); \
|
|
|
+ (ct)[2] = (u8)((st)>> 8); (ct)[3] = (u8)(st); }
|
|
|
#endif
|
|
|
|
|
|
-/**
|
|
|
+/*
|
|
|
* Expand the cipher key into the encryption key schedule.
|
|
|
*
|
|
|
* @return the number of rounds for the given cipher key size.
|
|
|
*/
|
|
|
-static int rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits) {
|
|
|
- int i = 0;
|
|
|
+static int
|
|
|
+rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits)
|
|
|
+{
|
|
|
+ int i = 0;
|
|
|
u32 temp;
|
|
|
|
|
|
rk[0] = GETU32(cipherKey );
|
|
@@ -920,32 +931,31 @@ static int rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int
|
|
|
rk[6] = GETU32(cipherKey + 24);
|
|
|
rk[7] = GETU32(cipherKey + 28);
|
|
|
if (keyBits == 256) {
|
|
|
- for (;;) {
|
|
|
- temp = rk[ 7];
|
|
|
- rk[ 8] = rk[ 0] ^
|
|
|
- (Te4[(temp >> 16) & 0xff] << 24) ^
|
|
|
- (Te4[(temp >> 8) & 0xff] << 16) ^
|
|
|
- (Te4[(temp ) & 0xff] << 8) ^
|
|
|
- (Te4[(temp >> 24) ] ) ^
|
|
|
- rcon[i];
|
|
|
- rk[ 9] = rk[ 1] ^ rk[ 8];
|
|
|
- rk[10] = rk[ 2] ^ rk[ 9];
|
|
|
- rk[11] = rk[ 3] ^ rk[10];
|
|
|
+ for (;;) {
|
|
|
+ temp = rk[ 7];
|
|
|
+ rk[ 8] = rk[ 0] ^
|
|
|
+ (Te4[(temp >> 16) & 0xff] << 24) ^
|
|
|
+ (Te4[(temp >> 8) & 0xff] << 16) ^
|
|
|
+ (Te4[(temp ) & 0xff] << 8) ^
|
|
|
+ (Te4[(temp >> 24) ] ) ^
|
|
|
+ rcon[i];
|
|
|
+ rk[ 9] = rk[ 1] ^ rk[ 8];
|
|
|
+ rk[10] = rk[ 2] ^ rk[ 9];
|
|
|
+ rk[11] = rk[ 3] ^ rk[10];
|
|
|
if (++i == 7) {
|
|
|
return 14;
|
|
|
}
|
|
|
- temp = rk[11];
|
|
|
- rk[12] = rk[ 4] ^
|
|
|
- (Te4[(temp >> 24) ] << 24) ^
|
|
|
- (Te4[(temp >> 16) & 0xff] << 16) ^
|
|
|
- (Te4[(temp >> 8) & 0xff] << 8) ^
|
|
|
- (Te4[(temp ) & 0xff] );
|
|
|
- rk[13] = rk[ 5] ^ rk[12];
|
|
|
- rk[14] = rk[ 6] ^ rk[13];
|
|
|
- rk[15] = rk[ 7] ^ rk[14];
|
|
|
-
|
|
|
+ temp = rk[11];
|
|
|
+ rk[12] = rk[ 4] ^
|
|
|
+ (Te4[(temp >> 24) ] << 24) ^
|
|
|
+ (Te4[(temp >> 16) & 0xff] << 16) ^
|
|
|
+ (Te4[(temp >> 8) & 0xff] << 8) ^
|
|
|
+ (Te4[(temp ) & 0xff] );
|
|
|
+ rk[13] = rk[ 5] ^ rk[12];
|
|
|
+ rk[14] = rk[ 6] ^ rk[13];
|
|
|
+ rk[15] = rk[ 7] ^ rk[14];
|
|
|
rk += 8;
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
@@ -955,7 +965,9 @@ static int rijndaelKeySetupEnc(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int
|
|
|
*
|
|
|
* @return the number of rounds for the given cipher key size.
|
|
|
*/
|
|
|
-static int rijndaelKeySetupDec(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int keyBits) {
|
|
|
+static int
|
|
|
+rijndaelKeySetupDec(u32 rk[/* 4*(Nr + 1) */], const u8 cipherKey[], int keyBits)
|
|
|
+{
|
|
|
int Nr, i, j;
|
|
|
u32 temp;
|
|
|
|
|
@@ -968,7 +980,10 @@ static int rijndaelKeySetupDec(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int
|
|
|
temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
|
|
|
temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
|
|
|
}
|
|
|
- /* apply the inverse MixColumn transform to all round keys but the first and the last: */
|
|
|
+ /*
|
|
|
+ * apply the inverse MixColumn transform to all round keys
|
|
|
+ * but the first and the last:
|
|
|
+ */
|
|
|
for (i = 1; i < Nr; i++) {
|
|
|
rk += 4;
|
|
|
rk[0] =
|
|
@@ -995,13 +1010,16 @@ static int rijndaelKeySetupDec(u32 rk[/*4*(Nr + 1)*/], const u8 cipherKey[], int
|
|
|
return Nr;
|
|
|
}
|
|
|
|
|
|
-static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16], u8 ct[16]) {
|
|
|
+static void
|
|
|
+rijndaelEncrypt(const u32 rk[/* 4*(Nr + 1) */], int Nr, const u8 pt[16],
|
|
|
+ u8 ct[16])
|
|
|
+{
|
|
|
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
|
|
#ifndef FULL_UNROLL
|
|
|
- int r;
|
|
|
+ int r;
|
|
|
#endif /* ?FULL_UNROLL */
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
* map byte array block to cipher state
|
|
|
* and add initial round key:
|
|
|
*/
|
|
@@ -1010,7 +1028,7 @@ static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16
|
|
|
s2 = GETU32(pt + 8) ^ rk[2];
|
|
|
s3 = GETU32(pt + 12) ^ rk[3];
|
|
|
#ifdef FULL_UNROLL
|
|
|
- /* round 1: */
|
|
|
+ /* round 1: */
|
|
|
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4];
|
|
|
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5];
|
|
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[ 6];
|
|
@@ -1020,7 +1038,7 @@ static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16
|
|
|
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9];
|
|
|
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[10];
|
|
|
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[11];
|
|
|
- /* round 3: */
|
|
|
+ /* round 3: */
|
|
|
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[12];
|
|
|
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[13];
|
|
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14];
|
|
@@ -1030,7 +1048,7 @@ static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16
|
|
|
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17];
|
|
|
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[18];
|
|
|
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[19];
|
|
|
- /* round 5: */
|
|
|
+ /* round 5: */
|
|
|
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[20];
|
|
|
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[21];
|
|
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[22];
|
|
@@ -1040,7 +1058,7 @@ static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16
|
|
|
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[25];
|
|
|
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[26];
|
|
|
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[27];
|
|
|
- /* round 7: */
|
|
|
+ /* round 7: */
|
|
|
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[28];
|
|
|
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[29];
|
|
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[30];
|
|
@@ -1050,99 +1068,98 @@ static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16
|
|
|
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[33];
|
|
|
s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[34];
|
|
|
s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[35];
|
|
|
- /* round 9: */
|
|
|
+ /* round 9: */
|
|
|
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[36];
|
|
|
t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[37];
|
|
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[38];
|
|
|
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[39];
|
|
|
- if (Nr > 10) {
|
|
|
- /* round 10: */
|
|
|
- s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40];
|
|
|
- s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[41];
|
|
|
- s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[42];
|
|
|
- s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[43];
|
|
|
- /* round 11: */
|
|
|
- t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[44];
|
|
|
- t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[45];
|
|
|
- t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[46];
|
|
|
- t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[47];
|
|
|
- if (Nr > 12) {
|
|
|
- /* round 12: */
|
|
|
- s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[48];
|
|
|
- s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[49];
|
|
|
- s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[50];
|
|
|
- s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[51];
|
|
|
- /* round 13: */
|
|
|
- t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[52];
|
|
|
- t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[53];
|
|
|
- t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[54];
|
|
|
- t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[55];
|
|
|
- }
|
|
|
- }
|
|
|
- rk += Nr << 2;
|
|
|
-#else /* !FULL_UNROLL */
|
|
|
- /*
|
|
|
+ if (Nr > 10) {
|
|
|
+ /* round 10: */
|
|
|
+ s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40];
|
|
|
+ s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[41];
|
|
|
+ s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[42];
|
|
|
+ s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[43];
|
|
|
+ /* round 11: */
|
|
|
+ t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[44];
|
|
|
+ t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[45];
|
|
|
+ t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[46];
|
|
|
+ t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[47];
|
|
|
+ if (Nr > 12) {
|
|
|
+ /* round 12: */
|
|
|
+ s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[48];
|
|
|
+ s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[49];
|
|
|
+ s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[50];
|
|
|
+ s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[51];
|
|
|
+ /* round 13: */
|
|
|
+ t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[52];
|
|
|
+ t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[53];
|
|
|
+ t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[54];
|
|
|
+ t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[55];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rk += Nr << 2;
|
|
|
+#else /* !FULL_UNROLL */
|
|
|
+ /*
|
|
|
* Nr - 1 full rounds:
|
|
|
*/
|
|
|
- r = Nr >> 1;
|
|
|
- for (;;) {
|
|
|
- t0 =
|
|
|
- Te0[(s0 >> 24) ] ^
|
|
|
- Te1[(s1 >> 16) & 0xff] ^
|
|
|
- Te2[(s2 >> 8) & 0xff] ^
|
|
|
- Te3[(s3 ) & 0xff] ^
|
|
|
- rk[4];
|
|
|
- t1 =
|
|
|
- Te0[(s1 >> 24) ] ^
|
|
|
- Te1[(s2 >> 16) & 0xff] ^
|
|
|
- Te2[(s3 >> 8) & 0xff] ^
|
|
|
- Te3[(s0 ) & 0xff] ^
|
|
|
- rk[5];
|
|
|
- t2 =
|
|
|
- Te0[(s2 >> 24) ] ^
|
|
|
- Te1[(s3 >> 16) & 0xff] ^
|
|
|
- Te2[(s0 >> 8) & 0xff] ^
|
|
|
- Te3[(s1 ) & 0xff] ^
|
|
|
- rk[6];
|
|
|
- t3 =
|
|
|
- Te0[(s3 >> 24) ] ^
|
|
|
- Te1[(s0 >> 16) & 0xff] ^
|
|
|
- Te2[(s1 >> 8) & 0xff] ^
|
|
|
- Te3[(s2 ) & 0xff] ^
|
|
|
- rk[7];
|
|
|
+ r = Nr >> 1;
|
|
|
+ for (;;) {
|
|
|
+ t0 =
|
|
|
+ Te0[(s0 >> 24) ] ^
|
|
|
+ Te1[(s1 >> 16) & 0xff] ^
|
|
|
+ Te2[(s2 >> 8) & 0xff] ^
|
|
|
+ Te3[(s3 ) & 0xff] ^
|
|
|
+ rk[4];
|
|
|
+ t1 =
|
|
|
+ Te0[(s1 >> 24) ] ^
|
|
|
+ Te1[(s2 >> 16) & 0xff] ^
|
|
|
+ Te2[(s3 >> 8) & 0xff] ^
|
|
|
+ Te3[(s0 ) & 0xff] ^
|
|
|
+ rk[5];
|
|
|
+ t2 =
|
|
|
+ Te0[(s2 >> 24) ] ^
|
|
|
+ Te1[(s3 >> 16) & 0xff] ^
|
|
|
+ Te2[(s0 >> 8) & 0xff] ^
|
|
|
+ Te3[(s1 ) & 0xff] ^
|
|
|
+ rk[6];
|
|
|
+ t3 =
|
|
|
+ Te0[(s3 >> 24) ] ^
|
|
|
+ Te1[(s0 >> 16) & 0xff] ^
|
|
|
+ Te2[(s1 >> 8) & 0xff] ^
|
|
|
+ Te3[(s2 ) & 0xff] ^
|
|
|
+ rk[7];
|
|
|
|
|
|
- rk += 8;
|
|
|
- if (--r == 0) {
|
|
|
- break;
|
|
|
- }
|
|
|
+ rk += 8;
|
|
|
+ if (--r == 0)
|
|
|
+ break;
|
|
|
|
|
|
- s0 =
|
|
|
- Te0[(t0 >> 24) ] ^
|
|
|
- Te1[(t1 >> 16) & 0xff] ^
|
|
|
- Te2[(t2 >> 8) & 0xff] ^
|
|
|
- Te3[(t3 ) & 0xff] ^
|
|
|
- rk[0];
|
|
|
- s1 =
|
|
|
- Te0[(t1 >> 24) ] ^
|
|
|
- Te1[(t2 >> 16) & 0xff] ^
|
|
|
- Te2[(t3 >> 8) & 0xff] ^
|
|
|
- Te3[(t0 ) & 0xff] ^
|
|
|
- rk[1];
|
|
|
- s2 =
|
|
|
- Te0[(t2 >> 24) ] ^
|
|
|
- Te1[(t3 >> 16) & 0xff] ^
|
|
|
- Te2[(t0 >> 8) & 0xff] ^
|
|
|
- Te3[(t1 ) & 0xff] ^
|
|
|
- rk[2];
|
|
|
- s3 =
|
|
|
- Te0[(t3 >> 24) ] ^
|
|
|
- Te1[(t0 >> 16) & 0xff] ^
|
|
|
- Te2[(t1 >> 8) & 0xff] ^
|
|
|
- Te3[(t2 ) & 0xff] ^
|
|
|
- rk[3];
|
|
|
- }
|
|
|
-#endif /* ?FULL_UNROLL */
|
|
|
- /*
|
|
|
+ s0 =
|
|
|
+ Te0[(t0 >> 24) ] ^
|
|
|
+ Te1[(t1 >> 16) & 0xff] ^
|
|
|
+ Te2[(t2 >> 8) & 0xff] ^
|
|
|
+ Te3[(t3 ) & 0xff] ^
|
|
|
+ rk[0];
|
|
|
+ s1 =
|
|
|
+ Te0[(t1 >> 24) ] ^
|
|
|
+ Te1[(t2 >> 16) & 0xff] ^
|
|
|
+ Te2[(t3 >> 8) & 0xff] ^
|
|
|
+ Te3[(t0 ) & 0xff] ^
|
|
|
+ rk[1];
|
|
|
+ s2 =
|
|
|
+ Te0[(t2 >> 24) ] ^
|
|
|
+ Te1[(t3 >> 16) & 0xff] ^
|
|
|
+ Te2[(t0 >> 8) & 0xff] ^
|
|
|
+ Te3[(t1 ) & 0xff] ^
|
|
|
+ rk[2];
|
|
|
+ s3 =
|
|
|
+ Te0[(t3 >> 24) ] ^
|
|
|
+ Te1[(t0 >> 16) & 0xff] ^
|
|
|
+ Te2[(t1 >> 8) & 0xff] ^
|
|
|
+ Te3[(t2 ) & 0xff] ^
|
|
|
+ rk[3];
|
|
|
+ }
|
|
|
+#endif /* ?FULL_UNROLL */
|
|
|
+ /*
|
|
|
* apply last round and
|
|
|
* map cipher state to byte array block:
|
|
|
*/
|
|
@@ -1176,13 +1193,16 @@ static void rijndaelEncrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 pt[16
|
|
|
PUTU32(ct + 12, s3);
|
|
|
}
|
|
|
|
|
|
-static void rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16], u8 pt[16]) {
|
|
|
+static void
|
|
|
+rijndaelDecrypt(const u32 rk[/* 4*(Nr + 1) */], int Nr, const u8 ct[16],
|
|
|
+ u8 pt[16])
|
|
|
+{
|
|
|
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
|
|
#ifndef FULL_UNROLL
|
|
|
- int r;
|
|
|
-#endif /* ?FULL_UNROLL */
|
|
|
+ int r;
|
|
|
+#endif /* ?FULL_UNROLL */
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
* map byte array block to cipher state
|
|
|
* and add initial round key:
|
|
|
*/
|
|
@@ -1260,8 +1280,8 @@ static void rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16
|
|
|
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55];
|
|
|
}
|
|
|
}
|
|
|
- rk += Nr << 2;
|
|
|
-#else /* !FULL_UNROLL */
|
|
|
+ rk += Nr << 2;
|
|
|
+#else /* !FULL_UNROLL */
|
|
|
/*
|
|
|
* Nr - 1 full rounds:
|
|
|
*/
|
|
@@ -1293,9 +1313,8 @@ static void rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16
|
|
|
rk[7];
|
|
|
|
|
|
rk += 8;
|
|
|
- if (--r == 0) {
|
|
|
+ if (--r == 0)
|
|
|
break;
|
|
|
- }
|
|
|
|
|
|
s0 =
|
|
|
Td0[(t0 >> 24) ] ^
|
|
@@ -1322,8 +1341,8 @@ static void rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16
|
|
|
Td3[(t0 ) & 0xff] ^
|
|
|
rk[3];
|
|
|
}
|
|
|
-#endif /* ?FULL_UNROLL */
|
|
|
- /*
|
|
|
+#endif /* ?FULL_UNROLL */
|
|
|
+ /*
|
|
|
* apply last round and
|
|
|
* map cipher state to byte array block:
|
|
|
*/
|
|
@@ -1359,11 +1378,14 @@ static void rijndaelDecrypt(const u32 rk[/*4*(Nr + 1)*/], int Nr, const u8 ct[16
|
|
|
|
|
|
#ifdef INTERMEDIATE_VALUE_KAT
|
|
|
|
|
|
-static void rijndaelEncryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[16], int rounds) {
|
|
|
+static void
|
|
|
+rijndaelEncryptRound(const u32 rk[/* 4*(Nr + 1) */], int Nr, u8 block[16],
|
|
|
+ int rounds)
|
|
|
+{
|
|
|
int r;
|
|
|
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
* map byte array block to cipher state
|
|
|
* and add initial round key:
|
|
|
*/
|
|
@@ -1371,9 +1393,9 @@ static void rijndaelEncryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[
|
|
|
s1 = GETU32(block + 4) ^ rk[1];
|
|
|
s2 = GETU32(block + 8) ^ rk[2];
|
|
|
s3 = GETU32(block + 12) ^ rk[3];
|
|
|
- rk += 4;
|
|
|
+ rk += 4;
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
* Nr - 1 full rounds:
|
|
|
*/
|
|
|
for (r = (rounds < Nr ? rounds : Nr - 1); r > 0; r--) {
|
|
@@ -1401,45 +1423,42 @@ static void rijndaelEncryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[
|
|
|
Te2[(s1 >> 8) & 0xff] ^
|
|
|
Te3[(s2 ) & 0xff] ^
|
|
|
rk[3];
|
|
|
-
|
|
|
s0 = t0;
|
|
|
s1 = t1;
|
|
|
s2 = t2;
|
|
|
s3 = t3;
|
|
|
rk += 4;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
+ /*
|
|
|
* apply last round and
|
|
|
* map cipher state to byte array block:
|
|
|
*/
|
|
|
if (rounds == Nr) {
|
|
|
- t0 =
|
|
|
- (Te4[(s0 >> 24) ] << 24) ^
|
|
|
- (Te4[(s1 >> 16) & 0xff] << 16) ^
|
|
|
- (Te4[(s2 >> 8) & 0xff] << 8) ^
|
|
|
- (Te4[(s3 ) & 0xff] ) ^
|
|
|
- rk[0];
|
|
|
- t1 =
|
|
|
- (Te4[(s1 >> 24) ] << 24) ^
|
|
|
- (Te4[(s2 >> 16) & 0xff] << 16) ^
|
|
|
- (Te4[(s3 >> 8) & 0xff] << 8) ^
|
|
|
- (Te4[(s0 ) & 0xff] ) ^
|
|
|
- rk[1];
|
|
|
- t2 =
|
|
|
- (Te4[(s2 >> 24) ] << 24) ^
|
|
|
- (Te4[(s3 >> 16) & 0xff] << 16) ^
|
|
|
- (Te4[(s0 >> 8) & 0xff] << 8) ^
|
|
|
- (Te4[(s1 ) & 0xff] ) ^
|
|
|
- rk[2];
|
|
|
- t3 =
|
|
|
- (Te4[(s3 >> 24) ] << 24) ^
|
|
|
- (Te4[(s0 >> 16) & 0xff] << 16) ^
|
|
|
- (Te4[(s1 >> 8) & 0xff] << 8) ^
|
|
|
- (Te4[(s2 ) & 0xff] ) ^
|
|
|
- rk[3];
|
|
|
-
|
|
|
+ t0 =
|
|
|
+ (Te4[(s0 >> 24) ] << 24) ^
|
|
|
+ (Te4[(s1 >> 16) & 0xff] << 16) ^
|
|
|
+ (Te4[(s2 >> 8) & 0xff] << 8) ^
|
|
|
+ (Te4[(s3 ) & 0xff] ) ^
|
|
|
+ rk[0];
|
|
|
+ t1 =
|
|
|
+ (Te4[(s1 >> 24) ] << 24) ^
|
|
|
+ (Te4[(s2 >> 16) & 0xff] << 16) ^
|
|
|
+ (Te4[(s3 >> 8) & 0xff] << 8) ^
|
|
|
+ (Te4[(s0 ) & 0xff] ) ^
|
|
|
+ rk[1];
|
|
|
+ t2 =
|
|
|
+ (Te4[(s2 >> 24) ] << 24) ^
|
|
|
+ (Te4[(s3 >> 16) & 0xff] << 16) ^
|
|
|
+ (Te4[(s0 >> 8) & 0xff] << 8) ^
|
|
|
+ (Te4[(s1 ) & 0xff] ) ^
|
|
|
+ rk[2];
|
|
|
+ t3 =
|
|
|
+ (Te4[(s3 >> 24) ] << 24) ^
|
|
|
+ (Te4[(s0 >> 16) & 0xff] << 16) ^
|
|
|
+ (Te4[(s1 >> 8) & 0xff] << 8) ^
|
|
|
+ (Te4[(s2 ) & 0xff] ) ^
|
|
|
+ rk[3];
|
|
|
s0 = t0;
|
|
|
s1 = t1;
|
|
|
s2 = t2;
|
|
@@ -1452,11 +1471,14 @@ static void rijndaelEncryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[
|
|
|
PUTU32(block + 12, s3);
|
|
|
}
|
|
|
|
|
|
-static void rijndaelDecryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[16], int rounds) {
|
|
|
+static void
|
|
|
+rijndaelDecryptRound(const u32 rk[/* 4*(Nr + 1) */], int Nr, u8 block[16],
|
|
|
+ int rounds)
|
|
|
+{
|
|
|
int r;
|
|
|
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
* map byte array block to cipher state
|
|
|
* and add initial round key:
|
|
|
*/
|
|
@@ -1464,9 +1486,9 @@ static void rijndaelDecryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[
|
|
|
s1 = GETU32(block + 4) ^ rk[1];
|
|
|
s2 = GETU32(block + 8) ^ rk[2];
|
|
|
s3 = GETU32(block + 12) ^ rk[3];
|
|
|
- rk += 4;
|
|
|
+ rk += 4;
|
|
|
|
|
|
- /*
|
|
|
+ /*
|
|
|
* Nr - 1 full rounds:
|
|
|
*/
|
|
|
for (r = (rounds < Nr ? rounds : Nr) - 1; r > 0; r--) {
|
|
@@ -1500,10 +1522,9 @@ static void rijndaelDecryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[
|
|
|
s2 = t2;
|
|
|
s3 = t3;
|
|
|
rk += 4;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
+ /*
|
|
|
* complete the last round and
|
|
|
* map cipher state to byte array block:
|
|
|
*/
|
|
@@ -1529,10 +1550,10 @@ static void rijndaelDecryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[
|
|
|
(Td4[(s0 ) & 0xff] );
|
|
|
|
|
|
if (rounds == Nr) {
|
|
|
- t0 ^= rk[0];
|
|
|
- t1 ^= rk[1];
|
|
|
- t2 ^= rk[2];
|
|
|
- t3 ^= rk[3];
|
|
|
+ t0 ^= rk[0];
|
|
|
+ t1 ^= rk[1];
|
|
|
+ t2 ^= rk[2];
|
|
|
+ t3 ^= rk[3];
|
|
|
}
|
|
|
|
|
|
PUTU32(block , t0);
|
|
@@ -1541,4 +1562,4 @@ static void rijndaelDecryptRound(const u32 rk[/*4*(Nr + 1)*/], int Nr, u8 block[
|
|
|
PUTU32(block + 12, t3);
|
|
|
}
|
|
|
|
|
|
-#endif /* INTERMEDIATE_VALUE_KAT */
|
|
|
+#endif /* INTERMEDIATE_VALUE_KAT */
|