diff options
Diffstat (limited to 'mbglib/common/aes128.c')
-rw-r--r-- | mbglib/common/aes128.c | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/mbglib/common/aes128.c b/mbglib/common/aes128.c index 542467b..fb6a71e 100644 --- a/mbglib/common/aes128.c +++ b/mbglib/common/aes128.c @@ -1,7 +1,7 @@ /************************************************************************** * - * $Id: aes128.c 1.2 2009/10/01 14:03:09 martin REL_M $ + * $Id: aes128.c 1.2.1.1 2009/12/22 12:23:00 martin TRASH $ * * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany * @@ -10,6 +10,8 @@ * * ----------------------------------------------------------------------- * $Log: aes128.c $ + * Revision 1.2.1.1 2009/12/22 12:23:00 martin + * Started to fix possible 32/64 bit issues. * Revision 1.2 2009/10/01 14:03:09 martin * Added standard file header. * Fixed compiler warnings. @@ -36,26 +38,28 @@ #define FTABLE_ENTRIES 256 -static ulong FSb[FTABLE_ENTRIES]; -static ulong FT0[FTABLE_ENTRIES]; -static ulong FT1[FTABLE_ENTRIES]; -static ulong FT2[FTABLE_ENTRIES]; -static ulong FT3[FTABLE_ENTRIES]; +static uint32_t FSb[FTABLE_ENTRIES]; +static uint32_t FT0[FTABLE_ENTRIES]; +static uint32_t FT1[FTABLE_ENTRIES]; +static uint32_t FT2[FTABLE_ENTRIES]; +static uint32_t FT3[FTABLE_ENTRIES]; /* rounding constants */ #define RCON_TABLE_ENTRIES 10 -ulong RCON[RCON_TABLE_ENTRIES]; +uint32_t RCON[RCON_TABLE_ENTRIES]; /* tables generation flag */ static int initialized; -#define ROTR8(x) ( ( ( x << 24 ) & 0xFFFFFFFFUL ) | \ - ( ( x & 0xFFFFFFFFUL ) >> 8 ) ) -#define XTIME(x) ( ( x << 1 ) ^ ( ( x & 0x80 ) ? 0x1B : 0x00 ) ) -#define MUL(x,y) ( ( x && y ) ? pow[(log[x] + log[y]) % 255] : 0 ) +#define ROTR8( _x ) ( ( ( (_x) << 24 ) & 0xFFFFFFFFUL ) | \ + ( ( (_x) & 0xFFFFFFFFUL ) >> 8 ) ) + +#define XTIME( _x ) ( ( (_x) << 1 ) ^ ( ( (_x) & 0x80 ) ? 0x1B : 0x00 ) ) + +#define MUL( _x, _y ) ( ( (_x) && (_y) ) ? pow[(log[_x] + log[_y]) % 255] : 0 ) @@ -63,7 +67,8 @@ static int initialized; void aes_gen_tables( void ) { int i; - uint8_t x, y; + uint8_t x; + uint8_t y; uint8_t pow[FTABLE_ENTRIES]; uint8_t log[FTABLE_ENTRIES]; @@ -78,7 +83,7 @@ void aes_gen_tables( void ) /* calculate the round constants */ for ( i = 0, x = 1; i < RCON_TABLE_ENTRIES; i++, x = XTIME( x ) ) - RCON[i] = (ulong) x << 24; + RCON[i] = (uint32_t) x << 24; /* generate the forward and reverse S-boxes */ @@ -104,10 +109,10 @@ void aes_gen_tables( void ) x = (uint8_t) FSb[i]; y = XTIME( x ); - FT0[i] = (ulong) ( x ^ y ) ^ - ( (ulong) x << 8 ) ^ - ( (ulong) x << 16 ) ^ - ( (ulong) y << 24 ); + FT0[i] = (uint32_t) ( x ^ y ) ^ + ( (uint32_t) x << 8 ) ^ + ( (uint32_t) x << 16 ) ^ + ( (uint32_t) y << 24 ); FT0[i] &= 0xFFFFFFFFUL; @@ -122,20 +127,20 @@ void aes_gen_tables( void ) /* platform-independant 32-bit integer manipulation macros */ -#define GET_ulong(n,b,i) \ -{ \ - (n) = ( (ulong) (b)[(i) ] << 24 ) \ - | ( (ulong) (b)[(i) + 1] << 16 ) \ - | ( (ulong) (b)[(i) + 2] << 8 ) \ - | ( (ulong) (b)[(i) + 3] ); \ +#define GET_ulong( _n, _b, _i ) \ +{ \ + (_n) = ( (uint32_t) (_b)[(_i) ] << 24 ) \ + | ( (uint32_t) (_b)[(_i) + 1] << 16 ) \ + | ( (uint32_t) (_b)[(_i) + 2] << 8 ) \ + | ( (uint32_t) (_b)[(_i) + 3] ); \ } -#define PUT_ulong(n,b,i) \ -{ \ - (b)[(i) ] = (uint8_t) ( (n) >> 24 ); \ - (b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \ - (b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \ - (b)[(i) + 3] = (uint8_t) ( (n) ); \ +#define PUT_ulong( _n, _b, _i ) \ +{ \ + (_b)[(_i) ] = (uint8_t) ( (_n) >> 24 ); \ + (_b)[(_i) + 1] = (uint8_t) ( (_n) >> 16 ); \ + (_b)[(_i) + 2] = (uint8_t) ( (_n) >> 8 ); \ + (_b)[(_i) + 3] = (uint8_t) ( (_n) ); \ } @@ -150,7 +155,7 @@ void aes_gen_tables( void ) int aes128_set_key( aes128_context *ctx, uint8_t *key ) { int i; - ulong *RK; + uint32_t *RK; if ( !initialized ) { @@ -188,7 +193,7 @@ int aes128_set_key( aes128_context *ctx, uint8_t *key ) static /*HDR*/ void aes128_encrypt( aes128_context *ctx, uint8_t input[16], uint8_t output[16] ) { - ulong *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; + uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; RK = ctx->erk; @@ -199,23 +204,23 @@ void aes128_encrypt( aes128_context *ctx, uint8_t input[16], uint8_t output[16] #define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ { \ - RK += 4; \ - \ + RK += 4; \ + \ X0 = RK[0] ^ FT0[ (uint8_t) ( Y0 >> 24 ) ] ^ \ FT1[ (uint8_t) ( Y1 >> 16 ) ] ^ \ FT2[ (uint8_t) ( Y2 >> 8 ) ] ^ \ FT3[ (uint8_t) ( Y3 ) ]; \ - \ + \ X1 = RK[1] ^ FT0[ (uint8_t) ( Y1 >> 24 ) ] ^ \ FT1[ (uint8_t) ( Y2 >> 16 ) ] ^ \ FT2[ (uint8_t) ( Y3 >> 8 ) ] ^ \ FT3[ (uint8_t) ( Y0 ) ]; \ - \ + \ X2 = RK[2] ^ FT0[ (uint8_t) ( Y2 >> 24 ) ] ^ \ FT1[ (uint8_t) ( Y3 >> 16 ) ] ^ \ FT2[ (uint8_t) ( Y0 >> 8 ) ] ^ \ FT3[ (uint8_t) ( Y1 ) ]; \ - \ + \ X3 = RK[3] ^ FT0[ (uint8_t) ( Y3 >> 24 ) ] ^ \ FT1[ (uint8_t) ( Y0 >> 16 ) ] ^ \ FT2[ (uint8_t) ( Y1 >> 8 ) ] ^ \ |