Manually reformat aes_core.c
Add aes_core.c to the list of files not processed by openssl-format-source Conflicts: crypto/aes/aes_core.c Reviewed-by: Tim Hudson <tjh@openssl.org>
This commit is contained in:
parent
064198a817
commit
2cef4e6c29
2 changed files with 405 additions and 400 deletions
|
@ -617,161 +617,163 @@ static const u8 Td4[256] = {
|
||||||
0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU,
|
0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU,
|
||||||
};
|
};
|
||||||
static const u32 rcon[] = {
|
static const u32 rcon[] = {
|
||||||
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
||||||
0x10000000, 0x20000000, 0x40000000, 0x80000000,
|
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 */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand the cipher key into the encryption key schedule.
|
* Expand the cipher key into the encryption key schedule.
|
||||||
*/
|
*/
|
||||||
int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
||||||
AES_KEY *key) {
|
AES_KEY *key)
|
||||||
|
{
|
||||||
|
|
||||||
u32 *rk;
|
u32 *rk;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
u32 temp;
|
u32 temp;
|
||||||
|
|
||||||
if (!userKey || !key)
|
if (!userKey || !key)
|
||||||
return -1;
|
return -1;
|
||||||
if (bits != 128 && bits != 192 && bits != 256)
|
if (bits != 128 && bits != 192 && bits != 256)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
rk = key->rd_key;
|
rk = key->rd_key;
|
||||||
|
|
||||||
if (bits==128)
|
if (bits==128)
|
||||||
key->rounds = 10;
|
key->rounds = 10;
|
||||||
else if (bits==192)
|
else if (bits==192)
|
||||||
key->rounds = 12;
|
key->rounds = 12;
|
||||||
else
|
else
|
||||||
key->rounds = 14;
|
key->rounds = 14;
|
||||||
|
|
||||||
rk[0] = GETU32(userKey );
|
rk[0] = GETU32(userKey );
|
||||||
rk[1] = GETU32(userKey + 4);
|
rk[1] = GETU32(userKey + 4);
|
||||||
rk[2] = GETU32(userKey + 8);
|
rk[2] = GETU32(userKey + 8);
|
||||||
rk[3] = GETU32(userKey + 12);
|
rk[3] = GETU32(userKey + 12);
|
||||||
if (bits == 128) {
|
if (bits == 128) {
|
||||||
while (1) {
|
while (1) {
|
||||||
temp = rk[3];
|
temp = rk[3];
|
||||||
rk[4] = rk[0] ^
|
rk[4] = rk[0] ^
|
||||||
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||||
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
||||||
rcon[i];
|
rcon[i];
|
||||||
rk[5] = rk[1] ^ rk[4];
|
rk[5] = rk[1] ^ rk[4];
|
||||||
rk[6] = rk[2] ^ rk[5];
|
rk[6] = rk[2] ^ rk[5];
|
||||||
rk[7] = rk[3] ^ rk[6];
|
rk[7] = rk[3] ^ rk[6];
|
||||||
if (++i == 10) {
|
if (++i == 10) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
rk += 4;
|
rk += 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rk[4] = GETU32(userKey + 16);
|
rk[4] = GETU32(userKey + 16);
|
||||||
rk[5] = GETU32(userKey + 20);
|
rk[5] = GETU32(userKey + 20);
|
||||||
if (bits == 192) {
|
if (bits == 192) {
|
||||||
while (1) {
|
while (1) {
|
||||||
temp = rk[ 5];
|
temp = rk[ 5];
|
||||||
rk[ 6] = rk[ 0] ^
|
rk[ 6] = rk[ 0] ^
|
||||||
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||||
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
||||||
rcon[i];
|
rcon[i];
|
||||||
rk[ 7] = rk[ 1] ^ rk[ 6];
|
rk[ 7] = rk[ 1] ^ rk[ 6];
|
||||||
rk[ 8] = rk[ 2] ^ rk[ 7];
|
rk[ 8] = rk[ 2] ^ rk[ 7];
|
||||||
rk[ 9] = rk[ 3] ^ rk[ 8];
|
rk[ 9] = rk[ 3] ^ rk[ 8];
|
||||||
if (++i == 8) {
|
if (++i == 8) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
rk[10] = rk[ 4] ^ rk[ 9];
|
rk[10] = rk[ 4] ^ rk[ 9];
|
||||||
rk[11] = rk[ 5] ^ rk[10];
|
rk[11] = rk[ 5] ^ rk[10];
|
||||||
rk += 6;
|
rk += 6;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rk[6] = GETU32(userKey + 24);
|
rk[6] = GETU32(userKey + 24);
|
||||||
rk[7] = GETU32(userKey + 28);
|
rk[7] = GETU32(userKey + 28);
|
||||||
if (bits == 256) {
|
if (bits == 256) {
|
||||||
while (1) {
|
while (1) {
|
||||||
temp = rk[ 7];
|
temp = rk[ 7];
|
||||||
rk[ 8] = rk[ 0] ^
|
rk[ 8] = rk[ 0] ^
|
||||||
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
(Te2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||||
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
(Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
(Te0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
(Te1[(temp >> 24) ] & 0x000000ff) ^
|
||||||
rcon[i];
|
rcon[i];
|
||||||
rk[ 9] = rk[ 1] ^ rk[ 8];
|
rk[ 9] = rk[ 1] ^ rk[ 8];
|
||||||
rk[10] = rk[ 2] ^ rk[ 9];
|
rk[10] = rk[ 2] ^ rk[ 9];
|
||||||
rk[11] = rk[ 3] ^ rk[10];
|
rk[11] = rk[ 3] ^ rk[10];
|
||||||
if (++i == 7) {
|
if (++i == 7) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
temp = rk[11];
|
temp = rk[11];
|
||||||
rk[12] = rk[ 4] ^
|
rk[12] = rk[ 4] ^
|
||||||
(Te2[(temp >> 24) ] & 0xff000000) ^
|
(Te2[(temp >> 24) ] & 0xff000000) ^
|
||||||
(Te3[(temp >> 16) & 0xff] & 0x00ff0000) ^
|
(Te3[(temp >> 16) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(temp >> 8) & 0xff] & 0x0000ff00) ^
|
(Te0[(temp >> 8) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(temp ) & 0xff] & 0x000000ff);
|
(Te1[(temp ) & 0xff] & 0x000000ff);
|
||||||
rk[13] = rk[ 5] ^ rk[12];
|
rk[13] = rk[ 5] ^ rk[12];
|
||||||
rk[14] = rk[ 6] ^ rk[13];
|
rk[14] = rk[ 6] ^ rk[13];
|
||||||
rk[15] = rk[ 7] ^ rk[14];
|
rk[15] = rk[ 7] ^ rk[14];
|
||||||
|
|
||||||
rk += 8;
|
rk += 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand the cipher key into the decryption key schedule.
|
* Expand the cipher key into the decryption key schedule.
|
||||||
*/
|
*/
|
||||||
int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
||||||
AES_KEY *key) {
|
AES_KEY *key)
|
||||||
|
{
|
||||||
|
|
||||||
u32 *rk;
|
u32 *rk;
|
||||||
int i, j, status;
|
int i, j, status;
|
||||||
u32 temp;
|
u32 temp;
|
||||||
|
|
||||||
/* first, start with an encryption schedule */
|
/* first, start with an encryption schedule */
|
||||||
status = private_AES_set_encrypt_key(userKey, bits, key);
|
status = private_AES_set_encrypt_key(userKey, bits, key);
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
rk = key->rd_key;
|
rk = key->rd_key;
|
||||||
|
|
||||||
/* invert the order of the round keys: */
|
/* invert the order of the round keys: */
|
||||||
for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) {
|
for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) {
|
||||||
temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
|
temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
|
||||||
temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
|
temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
|
||||||
temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
|
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;
|
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 < (key->rounds); i++) {
|
for (i = 1; i < (key->rounds); i++) {
|
||||||
rk += 4;
|
rk += 4;
|
||||||
rk[0] =
|
rk[0] =
|
||||||
Td0[Te1[(rk[0] >> 24) ] & 0xff] ^
|
Td0[Te1[(rk[0] >> 24) ] & 0xff] ^
|
||||||
Td1[Te1[(rk[0] >> 16) & 0xff] & 0xff] ^
|
Td1[Te1[(rk[0] >> 16) & 0xff] & 0xff] ^
|
||||||
Td2[Te1[(rk[0] >> 8) & 0xff] & 0xff] ^
|
Td2[Te1[(rk[0] >> 8) & 0xff] & 0xff] ^
|
||||||
Td3[Te1[(rk[0] ) & 0xff] & 0xff];
|
Td3[Te1[(rk[0] ) & 0xff] & 0xff];
|
||||||
rk[1] =
|
rk[1] =
|
||||||
Td0[Te1[(rk[1] >> 24) ] & 0xff] ^
|
Td0[Te1[(rk[1] >> 24) ] & 0xff] ^
|
||||||
Td1[Te1[(rk[1] >> 16) & 0xff] & 0xff] ^
|
Td1[Te1[(rk[1] >> 16) & 0xff] & 0xff] ^
|
||||||
Td2[Te1[(rk[1] >> 8) & 0xff] & 0xff] ^
|
Td2[Te1[(rk[1] >> 8) & 0xff] & 0xff] ^
|
||||||
Td3[Te1[(rk[1] ) & 0xff] & 0xff];
|
Td3[Te1[(rk[1] ) & 0xff] & 0xff];
|
||||||
rk[2] =
|
rk[2] =
|
||||||
Td0[Te1[(rk[2] >> 24) ] & 0xff] ^
|
Td0[Te1[(rk[2] >> 24) ] & 0xff] ^
|
||||||
Td1[Te1[(rk[2] >> 16) & 0xff] & 0xff] ^
|
Td1[Te1[(rk[2] >> 16) & 0xff] & 0xff] ^
|
||||||
Td2[Te1[(rk[2] >> 8) & 0xff] & 0xff] ^
|
Td2[Te1[(rk[2] >> 8) & 0xff] & 0xff] ^
|
||||||
Td3[Te1[(rk[2] ) & 0xff] & 0xff];
|
Td3[Te1[(rk[2] ) & 0xff] & 0xff];
|
||||||
rk[3] =
|
rk[3] =
|
||||||
Td0[Te1[(rk[3] >> 24) ] & 0xff] ^
|
Td0[Te1[(rk[3] >> 24) ] & 0xff] ^
|
||||||
Td1[Te1[(rk[3] >> 16) & 0xff] & 0xff] ^
|
Td1[Te1[(rk[3] >> 16) & 0xff] & 0xff] ^
|
||||||
Td2[Te1[(rk[3] >> 8) & 0xff] & 0xff] ^
|
Td2[Te1[(rk[3] >> 8) & 0xff] & 0xff] ^
|
||||||
Td3[Te1[(rk[3] ) & 0xff] & 0xff];
|
Td3[Te1[(rk[3] ) & 0xff] & 0xff];
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -779,71 +781,71 @@ int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
||||||
* in and out can overlap
|
* in and out can overlap
|
||||||
*/
|
*/
|
||||||
void AES_encrypt(const unsigned char *in, unsigned char *out,
|
void AES_encrypt(const unsigned char *in, unsigned char *out,
|
||||||
const AES_KEY *key) {
|
const AES_KEY *key) {
|
||||||
|
|
||||||
const u32 *rk;
|
const u32 *rk;
|
||||||
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
||||||
#ifndef FULL_UNROLL
|
#ifndef FULL_UNROLL
|
||||||
int r;
|
int r;
|
||||||
#endif /* ?FULL_UNROLL */
|
#endif /* ?FULL_UNROLL */
|
||||||
|
|
||||||
assert(in && out && key);
|
assert(in && out && key);
|
||||||
rk = key->rd_key;
|
rk = key->rd_key;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* map byte array block to cipher state
|
* map byte array block to cipher state
|
||||||
* and add initial round key:
|
* and add initial round key:
|
||||||
*/
|
*/
|
||||||
s0 = GETU32(in ) ^ rk[0];
|
s0 = GETU32(in ) ^ rk[0];
|
||||||
s1 = GETU32(in + 4) ^ rk[1];
|
s1 = GETU32(in + 4) ^ rk[1];
|
||||||
s2 = GETU32(in + 8) ^ rk[2];
|
s2 = GETU32(in + 8) ^ rk[2];
|
||||||
s3 = GETU32(in + 12) ^ rk[3];
|
s3 = GETU32(in + 12) ^ rk[3];
|
||||||
#ifdef FULL_UNROLL
|
#ifdef FULL_UNROLL
|
||||||
/* round 1: */
|
/* round 1: */
|
||||||
t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4];
|
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];
|
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];
|
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];
|
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[ 7];
|
||||||
/* round 2: */
|
/* round 2: */
|
||||||
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8];
|
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8];
|
||||||
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9];
|
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];
|
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];
|
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];
|
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];
|
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];
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14];
|
||||||
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15];
|
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15];
|
||||||
/* round 4: */
|
/* round 4: */
|
||||||
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16];
|
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16];
|
||||||
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17];
|
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];
|
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];
|
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];
|
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];
|
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];
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[22];
|
||||||
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[23];
|
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[23];
|
||||||
/* round 6: */
|
/* round 6: */
|
||||||
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[24];
|
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[24];
|
||||||
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[25];
|
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];
|
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];
|
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];
|
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];
|
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];
|
t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[30];
|
||||||
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[31];
|
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[31];
|
||||||
/* round 8: */
|
/* round 8: */
|
||||||
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[32];
|
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[32];
|
||||||
s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[33];
|
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];
|
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];
|
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];
|
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];
|
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];
|
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];
|
t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[39];
|
||||||
if (key->rounds > 10) {
|
if (key->rounds > 10) {
|
||||||
/* round 10: */
|
/* round 10: */
|
||||||
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40];
|
s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40];
|
||||||
|
@ -932,37 +934,37 @@ void AES_encrypt(const unsigned char *in, unsigned char *out,
|
||||||
}
|
}
|
||||||
#endif /* ?FULL_UNROLL */
|
#endif /* ?FULL_UNROLL */
|
||||||
/*
|
/*
|
||||||
* apply last round and
|
* apply last round and
|
||||||
* map cipher state to byte array block:
|
* map cipher state to byte array block:
|
||||||
*/
|
*/
|
||||||
s0 =
|
s0 =
|
||||||
(Te2[(t0 >> 24) ] & 0xff000000) ^
|
(Te2[(t0 >> 24) ] & 0xff000000) ^
|
||||||
(Te3[(t1 >> 16) & 0xff] & 0x00ff0000) ^
|
(Te3[(t1 >> 16) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(t2 >> 8) & 0xff] & 0x0000ff00) ^
|
(Te0[(t2 >> 8) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(t3 ) & 0xff] & 0x000000ff) ^
|
(Te1[(t3 ) & 0xff] & 0x000000ff) ^
|
||||||
rk[0];
|
rk[0];
|
||||||
PUTU32(out , s0);
|
PUTU32(out , s0);
|
||||||
s1 =
|
s1 =
|
||||||
(Te2[(t1 >> 24) ] & 0xff000000) ^
|
(Te2[(t1 >> 24) ] & 0xff000000) ^
|
||||||
(Te3[(t2 >> 16) & 0xff] & 0x00ff0000) ^
|
(Te3[(t2 >> 16) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(t3 >> 8) & 0xff] & 0x0000ff00) ^
|
(Te0[(t3 >> 8) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(t0 ) & 0xff] & 0x000000ff) ^
|
(Te1[(t0 ) & 0xff] & 0x000000ff) ^
|
||||||
rk[1];
|
rk[1];
|
||||||
PUTU32(out + 4, s1);
|
PUTU32(out + 4, s1);
|
||||||
s2 =
|
s2 =
|
||||||
(Te2[(t2 >> 24) ] & 0xff000000) ^
|
(Te2[(t2 >> 24) ] & 0xff000000) ^
|
||||||
(Te3[(t3 >> 16) & 0xff] & 0x00ff0000) ^
|
(Te3[(t3 >> 16) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(t0 >> 8) & 0xff] & 0x0000ff00) ^
|
(Te0[(t0 >> 8) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(t1 ) & 0xff] & 0x000000ff) ^
|
(Te1[(t1 ) & 0xff] & 0x000000ff) ^
|
||||||
rk[2];
|
rk[2];
|
||||||
PUTU32(out + 8, s2);
|
PUTU32(out + 8, s2);
|
||||||
s3 =
|
s3 =
|
||||||
(Te2[(t3 >> 24) ] & 0xff000000) ^
|
(Te2[(t3 >> 24) ] & 0xff000000) ^
|
||||||
(Te3[(t0 >> 16) & 0xff] & 0x00ff0000) ^
|
(Te3[(t0 >> 16) & 0xff] & 0x00ff0000) ^
|
||||||
(Te0[(t1 >> 8) & 0xff] & 0x0000ff00) ^
|
(Te0[(t1 >> 8) & 0xff] & 0x0000ff00) ^
|
||||||
(Te1[(t2 ) & 0xff] & 0x000000ff) ^
|
(Te1[(t2 ) & 0xff] & 0x000000ff) ^
|
||||||
rk[3];
|
rk[3];
|
||||||
PUTU32(out + 12, s3);
|
PUTU32(out + 12, s3);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -970,21 +972,22 @@ void AES_encrypt(const unsigned char *in, unsigned char *out,
|
||||||
* in and out can overlap
|
* in and out can overlap
|
||||||
*/
|
*/
|
||||||
void AES_decrypt(const unsigned char *in, unsigned char *out,
|
void AES_decrypt(const unsigned char *in, unsigned char *out,
|
||||||
const AES_KEY *key) {
|
const AES_KEY *key)
|
||||||
|
{
|
||||||
|
|
||||||
const u32 *rk;
|
const u32 *rk;
|
||||||
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
||||||
#ifndef FULL_UNROLL
|
#ifndef FULL_UNROLL
|
||||||
int r;
|
int r;
|
||||||
#endif /* ?FULL_UNROLL */
|
#endif /* ?FULL_UNROLL */
|
||||||
|
|
||||||
assert(in && out && key);
|
assert(in && out && key);
|
||||||
rk = key->rd_key;
|
rk = key->rd_key;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* map byte array block to cipher state
|
* map byte array block to cipher state
|
||||||
* and add initial round key:
|
* and add initial round key:
|
||||||
*/
|
*/
|
||||||
s0 = GETU32(in ) ^ rk[0];
|
s0 = GETU32(in ) ^ rk[0];
|
||||||
s1 = GETU32(in + 4) ^ rk[1];
|
s1 = GETU32(in + 4) ^ rk[1];
|
||||||
s2 = GETU32(in + 8) ^ rk[2];
|
s2 = GETU32(in + 8) ^ rk[2];
|
||||||
|
@ -1059,7 +1062,7 @@ void AES_decrypt(const unsigned char *in, unsigned char *out,
|
||||||
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55];
|
t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rk += key->rounds << 2;
|
rk += key->rounds << 2;
|
||||||
#else /* !FULL_UNROLL */
|
#else /* !FULL_UNROLL */
|
||||||
/*
|
/*
|
||||||
* Nr - 1 full rounds:
|
* Nr - 1 full rounds:
|
||||||
|
@ -1123,37 +1126,37 @@ void AES_decrypt(const unsigned char *in, unsigned char *out,
|
||||||
}
|
}
|
||||||
#endif /* ?FULL_UNROLL */
|
#endif /* ?FULL_UNROLL */
|
||||||
/*
|
/*
|
||||||
* apply last round and
|
* apply last round and
|
||||||
* map cipher state to byte array block:
|
* map cipher state to byte array block:
|
||||||
*/
|
*/
|
||||||
s0 =
|
s0 =
|
||||||
(Td4[(t0 >> 24) ] << 24) ^
|
(Td4[(t0 >> 24) ] << 24) ^
|
||||||
(Td4[(t3 >> 16) & 0xff] << 16) ^
|
(Td4[(t3 >> 16) & 0xff] << 16) ^
|
||||||
(Td4[(t2 >> 8) & 0xff] << 8) ^
|
(Td4[(t2 >> 8) & 0xff] << 8) ^
|
||||||
(Td4[(t1 ) & 0xff]) ^
|
(Td4[(t1 ) & 0xff]) ^
|
||||||
rk[0];
|
rk[0];
|
||||||
PUTU32(out , s0);
|
PUTU32(out , s0);
|
||||||
s1 =
|
s1 =
|
||||||
(Td4[(t1 >> 24) ] << 24) ^
|
(Td4[(t1 >> 24) ] << 24) ^
|
||||||
(Td4[(t0 >> 16) & 0xff] << 16) ^
|
(Td4[(t0 >> 16) & 0xff] << 16) ^
|
||||||
(Td4[(t3 >> 8) & 0xff] << 8) ^
|
(Td4[(t3 >> 8) & 0xff] << 8) ^
|
||||||
(Td4[(t2 ) & 0xff]) ^
|
(Td4[(t2 ) & 0xff]) ^
|
||||||
rk[1];
|
rk[1];
|
||||||
PUTU32(out + 4, s1);
|
PUTU32(out + 4, s1);
|
||||||
s2 =
|
s2 =
|
||||||
(Td4[(t2 >> 24) ] << 24) ^
|
(Td4[(t2 >> 24) ] << 24) ^
|
||||||
(Td4[(t1 >> 16) & 0xff] << 16) ^
|
(Td4[(t1 >> 16) & 0xff] << 16) ^
|
||||||
(Td4[(t0 >> 8) & 0xff] << 8) ^
|
(Td4[(t0 >> 8) & 0xff] << 8) ^
|
||||||
(Td4[(t3 ) & 0xff]) ^
|
(Td4[(t3 ) & 0xff]) ^
|
||||||
rk[2];
|
rk[2];
|
||||||
PUTU32(out + 8, s2);
|
PUTU32(out + 8, s2);
|
||||||
s3 =
|
s3 =
|
||||||
(Td4[(t3 >> 24) ] << 24) ^
|
(Td4[(t3 >> 24) ] << 24) ^
|
||||||
(Td4[(t2 >> 16) & 0xff] << 16) ^
|
(Td4[(t2 >> 16) & 0xff] << 16) ^
|
||||||
(Td4[(t1 >> 8) & 0xff] << 8) ^
|
(Td4[(t1 >> 8) & 0xff] << 8) ^
|
||||||
(Td4[(t0 ) & 0xff]) ^
|
(Td4[(t0 ) & 0xff]) ^
|
||||||
rk[3];
|
rk[3];
|
||||||
PUTU32(out + 12, s3);
|
PUTU32(out + 12, s3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /* AES_ASM */
|
#else /* AES_ASM */
|
||||||
|
@ -1193,166 +1196,168 @@ static const u8 Te4[256] = {
|
||||||
0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U
|
0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U
|
||||||
};
|
};
|
||||||
static const u32 rcon[] = {
|
static const u32 rcon[] = {
|
||||||
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
0x01000000, 0x02000000, 0x04000000, 0x08000000,
|
||||||
0x10000000, 0x20000000, 0x40000000, 0x80000000,
|
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 */
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand the cipher key into the encryption key schedule.
|
* Expand the cipher key into the encryption key schedule.
|
||||||
*/
|
*/
|
||||||
int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
int private_AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
||||||
AES_KEY *key) {
|
AES_KEY *key)
|
||||||
u32 *rk;
|
{
|
||||||
|
u32 *rk;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
u32 temp;
|
u32 temp;
|
||||||
|
|
||||||
if (!userKey || !key)
|
if (!userKey || !key)
|
||||||
return -1;
|
return -1;
|
||||||
if (bits != 128 && bits != 192 && bits != 256)
|
if (bits != 128 && bits != 192 && bits != 256)
|
||||||
return -2;
|
return -2;
|
||||||
|
|
||||||
rk = key->rd_key;
|
rk = key->rd_key;
|
||||||
|
|
||||||
if (bits==128)
|
if (bits==128)
|
||||||
key->rounds = 10;
|
key->rounds = 10;
|
||||||
else if (bits==192)
|
else if (bits==192)
|
||||||
key->rounds = 12;
|
key->rounds = 12;
|
||||||
else
|
else
|
||||||
key->rounds = 14;
|
key->rounds = 14;
|
||||||
|
|
||||||
rk[0] = GETU32(userKey );
|
rk[0] = GETU32(userKey );
|
||||||
rk[1] = GETU32(userKey + 4);
|
rk[1] = GETU32(userKey + 4);
|
||||||
rk[2] = GETU32(userKey + 8);
|
rk[2] = GETU32(userKey + 8);
|
||||||
rk[3] = GETU32(userKey + 12);
|
rk[3] = GETU32(userKey + 12);
|
||||||
if (bits == 128) {
|
if (bits == 128) {
|
||||||
while (1) {
|
while (1) {
|
||||||
temp = rk[3];
|
temp = rk[3];
|
||||||
rk[4] = rk[0] ^
|
rk[4] = rk[0] ^
|
||||||
(Te4[(temp >> 16) & 0xff] << 24) ^
|
(Te4[(temp >> 16) & 0xff] << 24) ^
|
||||||
(Te4[(temp >> 8) & 0xff] << 16) ^
|
(Te4[(temp >> 8) & 0xff] << 16) ^
|
||||||
(Te4[(temp ) & 0xff] << 8) ^
|
(Te4[(temp ) & 0xff] << 8) ^
|
||||||
(Te4[(temp >> 24) ]) ^
|
(Te4[(temp >> 24) ]) ^
|
||||||
rcon[i];
|
rcon[i];
|
||||||
rk[5] = rk[1] ^ rk[4];
|
rk[5] = rk[1] ^ rk[4];
|
||||||
rk[6] = rk[2] ^ rk[5];
|
rk[6] = rk[2] ^ rk[5];
|
||||||
rk[7] = rk[3] ^ rk[6];
|
rk[7] = rk[3] ^ rk[6];
|
||||||
if (++i == 10) {
|
if (++i == 10) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
rk += 4;
|
rk += 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rk[4] = GETU32(userKey + 16);
|
rk[4] = GETU32(userKey + 16);
|
||||||
rk[5] = GETU32(userKey + 20);
|
rk[5] = GETU32(userKey + 20);
|
||||||
if (bits == 192) {
|
if (bits == 192) {
|
||||||
while (1) {
|
while (1) {
|
||||||
temp = rk[ 5];
|
temp = rk[ 5];
|
||||||
rk[ 6] = rk[ 0] ^
|
rk[ 6] = rk[ 0] ^
|
||||||
(Te4[(temp >> 16) & 0xff] << 24) ^
|
(Te4[(temp >> 16) & 0xff] << 24) ^
|
||||||
(Te4[(temp >> 8) & 0xff] << 16) ^
|
(Te4[(temp >> 8) & 0xff] << 16) ^
|
||||||
(Te4[(temp ) & 0xff] << 8) ^
|
(Te4[(temp ) & 0xff] << 8) ^
|
||||||
(Te4[(temp >> 24) ]) ^
|
(Te4[(temp >> 24) ]) ^
|
||||||
rcon[i];
|
rcon[i];
|
||||||
rk[ 7] = rk[ 1] ^ rk[ 6];
|
rk[ 7] = rk[ 1] ^ rk[ 6];
|
||||||
rk[ 8] = rk[ 2] ^ rk[ 7];
|
rk[ 8] = rk[ 2] ^ rk[ 7];
|
||||||
rk[ 9] = rk[ 3] ^ rk[ 8];
|
rk[ 9] = rk[ 3] ^ rk[ 8];
|
||||||
if (++i == 8) {
|
if (++i == 8) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
rk[10] = rk[ 4] ^ rk[ 9];
|
rk[10] = rk[ 4] ^ rk[ 9];
|
||||||
rk[11] = rk[ 5] ^ rk[10];
|
rk[11] = rk[ 5] ^ rk[10];
|
||||||
rk += 6;
|
rk += 6;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rk[6] = GETU32(userKey + 24);
|
rk[6] = GETU32(userKey + 24);
|
||||||
rk[7] = GETU32(userKey + 28);
|
rk[7] = GETU32(userKey + 28);
|
||||||
if (bits == 256) {
|
if (bits == 256) {
|
||||||
while (1) {
|
while (1) {
|
||||||
temp = rk[ 7];
|
temp = rk[ 7];
|
||||||
rk[ 8] = rk[ 0] ^
|
rk[ 8] = rk[ 0] ^
|
||||||
(Te4[(temp >> 16) & 0xff] << 24) ^
|
(Te4[(temp >> 16) & 0xff] << 24) ^
|
||||||
(Te4[(temp >> 8) & 0xff] << 16) ^
|
(Te4[(temp >> 8) & 0xff] << 16) ^
|
||||||
(Te4[(temp ) & 0xff] << 8) ^
|
(Te4[(temp ) & 0xff] << 8) ^
|
||||||
(Te4[(temp >> 24) ]) ^
|
(Te4[(temp >> 24) ]) ^
|
||||||
rcon[i];
|
rcon[i];
|
||||||
rk[ 9] = rk[ 1] ^ rk[ 8];
|
rk[ 9] = rk[ 1] ^ rk[ 8];
|
||||||
rk[10] = rk[ 2] ^ rk[ 9];
|
rk[10] = rk[ 2] ^ rk[ 9];
|
||||||
rk[11] = rk[ 3] ^ rk[10];
|
rk[11] = rk[ 3] ^ rk[10];
|
||||||
if (++i == 7) {
|
if (++i == 7) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
temp = rk[11];
|
temp = rk[11];
|
||||||
rk[12] = rk[ 4] ^
|
rk[12] = rk[ 4] ^
|
||||||
(Te4[(temp >> 24) ] << 24) ^
|
(Te4[(temp >> 24) ] << 24) ^
|
||||||
(Te4[(temp >> 16) & 0xff] << 16) ^
|
(Te4[(temp >> 16) & 0xff] << 16) ^
|
||||||
(Te4[(temp >> 8) & 0xff] << 8) ^
|
(Te4[(temp >> 8) & 0xff] << 8) ^
|
||||||
(Te4[(temp ) & 0xff]);
|
(Te4[(temp ) & 0xff]);
|
||||||
rk[13] = rk[ 5] ^ rk[12];
|
rk[13] = rk[ 5] ^ rk[12];
|
||||||
rk[14] = rk[ 6] ^ rk[13];
|
rk[14] = rk[ 6] ^ rk[13];
|
||||||
rk[15] = rk[ 7] ^ rk[14];
|
rk[15] = rk[ 7] ^ rk[14];
|
||||||
|
|
||||||
rk += 8;
|
rk += 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand the cipher key into the decryption key schedule.
|
* Expand the cipher key into the decryption key schedule.
|
||||||
*/
|
*/
|
||||||
int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
int private_AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
||||||
AES_KEY *key) {
|
AES_KEY *key)
|
||||||
|
{
|
||||||
|
|
||||||
u32 *rk;
|
u32 *rk;
|
||||||
int i, j, status;
|
int i, j, status;
|
||||||
u32 temp;
|
u32 temp;
|
||||||
|
|
||||||
/* first, start with an encryption schedule */
|
/* first, start with an encryption schedule */
|
||||||
status = private_AES_set_encrypt_key(userKey, bits, key);
|
status = private_AES_set_encrypt_key(userKey, bits, key);
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
rk = key->rd_key;
|
rk = key->rd_key;
|
||||||
|
|
||||||
/* invert the order of the round keys: */
|
/* invert the order of the round keys: */
|
||||||
for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) {
|
for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) {
|
||||||
temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
|
temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
|
||||||
temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
|
temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
|
||||||
temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
|
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;
|
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 < (key->rounds); i++) {
|
for (i = 1; i < (key->rounds); i++) {
|
||||||
rk += 4;
|
rk += 4;
|
||||||
for (j = 0; j < 4; j++) {
|
for (j = 0; j < 4; j++) {
|
||||||
u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m;
|
u32 tp1, tp2, tp4, tp8, tp9, tpb, tpd, tpe, m;
|
||||||
|
|
||||||
tp1 = rk[j];
|
tp1 = rk[j];
|
||||||
m = tp1 & 0x80808080;
|
m = tp1 & 0x80808080;
|
||||||
tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^
|
tp2 = ((tp1 & 0x7f7f7f7f) << 1) ^
|
||||||
((m - (m >> 7)) & 0x1b1b1b1b);
|
((m - (m >> 7)) & 0x1b1b1b1b);
|
||||||
m = tp2 & 0x80808080;
|
m = tp2 & 0x80808080;
|
||||||
tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^
|
tp4 = ((tp2 & 0x7f7f7f7f) << 1) ^
|
||||||
((m - (m >> 7)) & 0x1b1b1b1b);
|
((m - (m >> 7)) & 0x1b1b1b1b);
|
||||||
m = tp4 & 0x80808080;
|
m = tp4 & 0x80808080;
|
||||||
tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^
|
tp8 = ((tp4 & 0x7f7f7f7f) << 1) ^
|
||||||
((m - (m >> 7)) & 0x1b1b1b1b);
|
((m - (m >> 7)) & 0x1b1b1b1b);
|
||||||
tp9 = tp8 ^ tp1;
|
tp9 = tp8 ^ tp1;
|
||||||
tpb = tp9 ^ tp2;
|
tpb = tp9 ^ tp2;
|
||||||
tpd = tp9 ^ tp4;
|
tpd = tp9 ^ tp4;
|
||||||
tpe = tp8 ^ tp4 ^ tp2;
|
tpe = tp8 ^ tp4 ^ tp2;
|
||||||
#if defined(ROTATE)
|
#if defined(ROTATE)
|
||||||
rk[j] = tpe ^ ROTATE(tpd,16) ^
|
rk[j] = tpe ^ ROTATE(tpd,16) ^
|
||||||
ROTATE(tp9,24) ^ ROTATE(tpb,8);
|
ROTATE(tp9,24) ^ ROTATE(tpb,8);
|
||||||
#else
|
#else
|
||||||
rk[j] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^
|
rk[j] = tpe ^ (tpd >> 16) ^ (tpd << 16) ^
|
||||||
(tp9 >> 8) ^ (tp9 << 24) ^
|
(tp9 >> 8) ^ (tp9 << 24) ^
|
||||||
(tpb >> 24) ^ (tpb << 8);
|
(tpb >> 24) ^ (tpb << 8);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* AES_ASM */
|
#endif /* AES_ASM */
|
||||||
|
|
|
@ -88,7 +88,7 @@ do
|
||||||
case `basename $j` in
|
case `basename $j` in
|
||||||
# the list of files that indent is unable to handle correctly
|
# the list of files that indent is unable to handle correctly
|
||||||
# that we simply leave alone for manual formatting now
|
# that we simply leave alone for manual formatting now
|
||||||
obj_dat.h)
|
obj_dat.h|aes_core.c)
|
||||||
echo "skipping $j"
|
echo "skipping $j"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
|
|
Loading…
Reference in a new issue