Code cleanup

This commit is contained in:
Dag-Erling Smørgrav 2015-10-03 15:33:47 +00:00 committed by des
parent ed9403627f
commit 03bd9ea902

View file

@ -43,29 +43,28 @@
*/
#ifndef GET_ULONG_LE
#define GET_ULONG_LE(n,b,i) \
{ \
do { \
(n) = ((uint32_t)(b)[(i) ] ) \
| ((uint32_t)(b)[(i) + 1] << 8) \
| ((uint32_t)(b)[(i) + 2] << 16) \
| ((uint32_t)(b)[(i) + 3] << 24); \
}
} while (0)
#endif
#ifndef PUT_ULONG_LE
#define PUT_ULONG_LE(n,b,i) \
{ \
do { \
(b)[(i) ] = (uint8_t)((n) ); \
(b)[(i) + 1] = (uint8_t)((n) >> 8); \
(b)[(i) + 2] = (uint8_t)((n) >> 16); \
(b)[(i) + 3] = (uint8_t)((n) >> 24); \
}
} while (0)
#endif
/*
* Forward S-box
*/
static const uint8_t FSb[256] =
{
static const uint8_t FSb[256] = {
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5,
0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0,
@ -104,7 +103,6 @@ static const uint8_t FSb[256] =
* Forward tables
*/
#define FT \
\
V(A5,63,63,C6), V(84,7C,7C,F8), V(99,77,77,EE), V(8D,7B,7B,F6), \
V(0D,F2,F2,FF), V(BD,6B,6B,D6), V(B1,6F,6F,DE), V(54,C5,C5,91), \
V(50,30,30,60), V(03,01,01,02), V(A9,67,67,CE), V(7D,2B,2B,56), \
@ -191,8 +189,7 @@ static const uint32_t FT3[256] = { FT };
/*
* Reverse S-box
*/
static const uint8_t RSb[256] =
{
static const uint8_t RSb[256] = {
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38,
0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87,
@ -231,7 +228,6 @@ static const uint8_t RSb[256] =
* Reverse tables
*/
#define RT \
\
V(50,A7,F4,51), V(53,65,41,7E), V(C3,A4,17,1A), V(96,5E,27,3A), \
V(CB,6B,AB,3B), V(F1,45,9D,1F), V(AB,58,FA,AC), V(93,03,E3,4B), \
V(55,FA,30,20), V(F6,6D,76,AD), V(91,76,CC,88), V(25,4C,02,F5), \
@ -318,8 +314,7 @@ static const uint32_t RT3[256] = { RT };
/*
* Round constants
*/
static const uint32_t RCON[10] =
{
static const uint32_t RCON[10] = {
0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x0000001B, 0x00000036
@ -334,27 +329,25 @@ aes_setkey_enc(aes_ctx *ctx, const uint8_t *key, int keysize)
int i;
uint32_t *RK;
switch( keysize )
{
case 128: ctx->nr = 10; break;
case 192: ctx->nr = 12; break;
case 256: ctx->nr = 14; break;
default : return;
switch (keysize) {
case 16:
ctx->nr = 10;
break;
case 24:
ctx->nr = 12;
break;
case 32:
ctx->nr = 14;
break;
default:
return;
}
ctx->rk = RK = ctx->buf;
for( i = 0; i < (keysize >> 5); i++ )
{
for (i = 0; i < (keysize >> 2); i++)
GET_ULONG_LE(RK[i], key, i << 2);
}
switch( ctx->nr )
{
switch (ctx->nr) {
case 10:
for( i = 0; i < 10; i++, RK += 4 )
{
for (i = 0; i < 10; i++, RK += 4) {
RK[4] = RK[0] ^ RCON[i] ^
(FSb[(RK[3] >> 8) & 0xFF] ) ^
(FSb[(RK[3] >> 16) & 0xFF] << 8) ^
@ -366,11 +359,8 @@ aes_setkey_enc(aes_ctx *ctx, const uint8_t *key, int keysize)
RK[7] = RK[3] ^ RK[6];
}
break;
case 12:
for( i = 0; i < 8; i++, RK += 6 )
{
for (i = 0; i < 8; i++, RK += 6) {
RK[6] = RK[0] ^ RCON[i] ^
(FSb[(RK[5] >> 8) & 0xFF] ) ^
(FSb[(RK[5] >> 16) & 0xFF] << 8) ^
@ -384,35 +374,27 @@ aes_setkey_enc(aes_ctx *ctx, const uint8_t *key, int keysize)
RK[11] = RK[5] ^ RK[10];
}
break;
case 14:
for( i = 0; i < 7; i++, RK += 8 )
{
for (i = 0; i < 7; i++, RK += 8) {
RK[8] = RK[0] ^ RCON[i] ^
(FSb[(RK[7] >> 8) & 0xFF] ) ^
(FSb[(RK[7] >> 16) & 0xFF] << 8) ^
(FSb[(RK[7] >> 24) & 0xFF] << 16) ^
(FSb[(RK[7] ) & 0xFF] << 24);
RK[9] = RK[1] ^ RK[8];
RK[10] = RK[2] ^ RK[9];
RK[11] = RK[3] ^ RK[10];
RK[12] = RK[4] ^
(FSb[(RK[11] ) & 0xFF] ) ^
(FSb[(RK[11] >> 8) & 0xFF] << 8) ^
(FSb[(RK[11] >> 16) & 0xFF] << 16) ^
(FSb[(RK[11] >> 24) & 0xFF] << 24);
RK[13] = RK[5] ^ RK[12];
RK[14] = RK[6] ^ RK[13];
RK[15] = RK[7] ^ RK[14];
}
break;
default:
break;
}
}
@ -428,84 +410,83 @@ aes_setkey_dec(aes_ctx *ctx, const uint8_t *key, int keysize)
uint32_t *RK;
uint32_t *SK;
switch( keysize )
{
case 128: ctx->nr = 10; break;
case 192: ctx->nr = 12; break;
case 256: ctx->nr = 14; break;
default : return;
switch (keysize) {
case 16:
ctx->nr = 10;
break;
case 24:
ctx->nr = 12;
break;
case 32:
ctx->nr = 14;
break;
default:
return;
}
ctx->rk = RK = ctx->buf;
aes_setkey_enc(&cty, key, keysize);
SK = cty.rk + cty.nr * 4;
*RK++ = *SK++;
*RK++ = *SK++;
*RK++ = *SK++;
*RK++ = *SK++;
for( i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8 )
{
for( j = 0; j < 4; j++, SK++ )
{
*RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^
for (i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8) {
for (j = 0; j < 4; j++, SK++) {
*RK++ =
RT0[FSb[(*SK ) & 0xFF]]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Y3 >> 8) & 0xFF] ^ \
RT2[(Y2 >> 16) & 0xFF] ^ \
RT3[(Y1 >> 24) & 0xFF]; \
\
X1 = *RK++ ^ RT0[ ( Y1 ) & 0xFF ] ^ \
X1 = *RK++ ^ \
RT0[(Y1 ) & 0xFF] ^ \
RT1[(Y0 >> 8) & 0xFF] ^ \
RT2[(Y3 >> 16) & 0xFF] ^ \
RT3[(Y2 >> 24) & 0xFF]; \
\
X2 = *RK++ ^ RT0[ ( Y2 ) & 0xFF ] ^ \
X2 = *RK++ ^ \
RT0[(Y2 ) & 0xFF] ^ \
RT1[(Y1 >> 8) & 0xFF] ^ \
RT2[(Y0 >> 16) & 0xFF] ^ \
RT3[(Y3 >> 24) & 0xFF]; \
\
X3 = *RK++ ^ RT0[ ( Y3 ) & 0xFF ] ^ \
X3 = *RK++ ^ \
RT0[(Y3 ) & 0xFF] ^ \
RT1[(Y2 >> 8) & 0xFF] ^ \
RT2[(Y1 >> 16) & 0xFF] ^ \
RT3[(Y0 >> 24) & 0xFF]; \
@ -521,42 +502,39 @@ aes_enc(aes_ctx *ctx, const uint8_t *input, uint8_t *output)
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
RK = ctx->rk;
GET_ULONG_LE( X0, input, 0 ); X0 ^= *RK++;
GET_ULONG_LE( X1, input, 4 ); X1 ^= *RK++;
GET_ULONG_LE( X2, input, 8 ); X2 ^= *RK++;
GET_ULONG_LE( X3, input, 12 ); X3 ^= *RK++;
{
for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
{
GET_ULONG_LE(X0, input, 0);
X0 ^= *RK++;
GET_ULONG_LE(X1, input, 4);
X1 ^= *RK++;
GET_ULONG_LE(X2, input, 8);
X2 ^= *RK++;
GET_ULONG_LE(X3, input, 12);
X3 ^= *RK++;
for (i = (ctx->nr >> 1) - 1; i > 0; i--) {
AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
AES_FROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3);
}
AES_FROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
X0 = *RK++ ^ ( FSb[ ( Y0 ) & 0xFF ] ) ^
X0 = *RK++ ^
(FSb[(Y0 ) & 0xFF] ) ^
(FSb[(Y1 >> 8) & 0xFF] << 8) ^
(FSb[(Y2 >> 16) & 0xFF] << 16) ^
(FSb[(Y3 >> 24) & 0xFF] << 24);
X1 = *RK++ ^ ( FSb[ ( Y1 ) & 0xFF ] ) ^
X1 = *RK++ ^
(FSb[(Y1 ) & 0xFF] ) ^
(FSb[(Y2 >> 8) & 0xFF] << 8) ^
(FSb[(Y3 >> 16) & 0xFF] << 16) ^
(FSb[(Y0 >> 24) & 0xFF] << 24);
X2 = *RK++ ^ ( FSb[ ( Y2 ) & 0xFF ] ) ^
X2 = *RK++ ^
(FSb[(Y2 ) & 0xFF] ) ^
(FSb[(Y3 >> 8) & 0xFF] << 8) ^
(FSb[(Y0 >> 16) & 0xFF] << 16) ^
(FSb[(Y1 >> 24) & 0xFF] << 24);
X3 = *RK++ ^ ( FSb[ ( Y3 ) & 0xFF ] ) ^
X3 = *RK++ ^
(FSb[(Y3 ) & 0xFF] ) ^
(FSb[(Y0 >> 8) & 0xFF] << 8) ^
(FSb[(Y1 >> 16) & 0xFF] << 16) ^
(FSb[(Y2 >> 24) & 0xFF] << 24);
}
PUT_ULONG_LE(X0, output, 0);
PUT_ULONG_LE(X1, output, 4);
PUT_ULONG_LE(X2, output, 8);
@ -573,42 +551,39 @@ aes_dec(aes_ctx *ctx, const uint8_t *input, uint8_t *output)
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
RK = ctx->rk;
GET_ULONG_LE( X0, input, 0 ); X0 ^= *RK++;
GET_ULONG_LE( X1, input, 4 ); X1 ^= *RK++;
GET_ULONG_LE( X2, input, 8 ); X2 ^= *RK++;
GET_ULONG_LE( X3, input, 12 ); X3 ^= *RK++;
{
for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
{
GET_ULONG_LE(X0, input, 0);
X0 ^= *RK++;
GET_ULONG_LE(X1, input, 4);
X1 ^= *RK++;
GET_ULONG_LE(X2, input, 8);
X2 ^= *RK++;
GET_ULONG_LE(X3, input, 12);
X3 ^= *RK++;
for (i = (ctx->nr >> 1) - 1; i > 0; i--) {
AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
AES_RROUND(X0, X1, X2, X3, Y0, Y1, Y2, Y3);
}
AES_RROUND(Y0, Y1, Y2, Y3, X0, X1, X2, X3);
X0 = *RK++ ^ ( RSb[ ( Y0 ) & 0xFF ] ) ^
X0 = *RK++ ^
(RSb[(Y0 ) & 0xFF] ) ^
(RSb[(Y3 >> 8) & 0xFF] << 8) ^
(RSb[(Y2 >> 16) & 0xFF] << 16) ^
(RSb[(Y1 >> 24) & 0xFF] << 24);
X1 = *RK++ ^ ( RSb[ ( Y1 ) & 0xFF ] ) ^
X1 = *RK++ ^
(RSb[(Y1 ) & 0xFF] ) ^
(RSb[(Y0 >> 8) & 0xFF] << 8) ^
(RSb[(Y3 >> 16) & 0xFF] << 16) ^
(RSb[(Y2 >> 24) & 0xFF] << 24);
X2 = *RK++ ^ ( RSb[ ( Y2 ) & 0xFF ] ) ^
X2 = *RK++ ^
(RSb[(Y2 ) & 0xFF] ) ^
(RSb[(Y1 >> 8) & 0xFF] << 8) ^
(RSb[(Y0 >> 16) & 0xFF] << 16) ^
(RSb[(Y3 >> 24) & 0xFF] << 24);
X3 = *RK++ ^ ( RSb[ ( Y3 ) & 0xFF ] ) ^
X3 = *RK++ ^
(RSb[(Y3 ) & 0xFF] ) ^
(RSb[(Y2 >> 8) & 0xFF] << 8) ^
(RSb[(Y1 >> 16) & 0xFF] << 16) ^
(RSb[(Y0 >> 24) & 0xFF] << 24);
}
PUT_ULONG_LE(X0, output, 0);
PUT_ULONG_LE(X1, output, 4);
PUT_ULONG_LE(X2, output, 8);
@ -622,9 +597,9 @@ aes_init(aes_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen)
memset(ctx, 0, sizeof *ctx);
ctx->mode = mode;
if (ctx->mode == CIPHER_MODE_DECRYPT)
aes_setkey_dec(ctx, key, keylen * 8);
aes_setkey_dec(ctx, key, keylen);
else
aes_setkey_enc(ctx, key, keylen * 8);
aes_setkey_enc(ctx, key, keylen);
}