libosmogsm  0.9.3
Osmocom GSM library
src/gsm/milenage/aes_i.h
00001 /*
00002  * AES (Rijndael) cipher
00003  * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #pragma once
00016 
00017 #include "aes.h"
00018 
00019 /* #define FULL_UNROLL */
00020 #define AES_SMALL_TABLES
00021 
00022 extern const u32 Te0[256];
00023 extern const u32 Te1[256];
00024 extern const u32 Te2[256];
00025 extern const u32 Te3[256];
00026 extern const u32 Te4[256];
00027 extern const u32 Td0[256];
00028 extern const u32 Td1[256];
00029 extern const u32 Td2[256];
00030 extern const u32 Td3[256];
00031 extern const u32 Td4[256];
00032 extern const u32 rcon[10];
00033 extern const u8 Td4s[256];
00034 extern const u8 rcons[10];
00035 
00036 #ifndef AES_SMALL_TABLES
00037 
00038 #define RCON(i) rcon[(i)]
00039 
00040 #define TE0(i) Te0[((i) >> 24) & 0xff]
00041 #define TE1(i) Te1[((i) >> 16) & 0xff]
00042 #define TE2(i) Te2[((i) >> 8) & 0xff]
00043 #define TE3(i) Te3[(i) & 0xff]
00044 #define TE41(i) (Te4[((i) >> 24) & 0xff] & 0xff000000)
00045 #define TE42(i) (Te4[((i) >> 16) & 0xff] & 0x00ff0000)
00046 #define TE43(i) (Te4[((i) >> 8) & 0xff] & 0x0000ff00)
00047 #define TE44(i) (Te4[(i) & 0xff] & 0x000000ff)
00048 #define TE421(i) (Te4[((i) >> 16) & 0xff] & 0xff000000)
00049 #define TE432(i) (Te4[((i) >> 8) & 0xff] & 0x00ff0000)
00050 #define TE443(i) (Te4[(i) & 0xff] & 0x0000ff00)
00051 #define TE414(i) (Te4[((i) >> 24) & 0xff] & 0x000000ff)
00052 #define TE4(i) (Te4[(i)] & 0x000000ff)
00053 
00054 #define TD0(i) Td0[((i) >> 24) & 0xff]
00055 #define TD1(i) Td1[((i) >> 16) & 0xff]
00056 #define TD2(i) Td2[((i) >> 8) & 0xff]
00057 #define TD3(i) Td3[(i) & 0xff]
00058 #define TD41(i) (Td4[((i) >> 24) & 0xff] & 0xff000000)
00059 #define TD42(i) (Td4[((i) >> 16) & 0xff] & 0x00ff0000)
00060 #define TD43(i) (Td4[((i) >> 8) & 0xff] & 0x0000ff00)
00061 #define TD44(i) (Td4[(i) & 0xff] & 0x000000ff)
00062 #define TD0_(i) Td0[(i) & 0xff]
00063 #define TD1_(i) Td1[(i) & 0xff]
00064 #define TD2_(i) Td2[(i) & 0xff]
00065 #define TD3_(i) Td3[(i) & 0xff]
00066 
00067 #else /* AES_SMALL_TABLES */
00068 
00069 #define RCON(i) (rcons[(i)] << 24)
00070 
00071 static inline u32 rotr(u32 val, int bits)
00072 {
00073         return (val >> bits) | (val << (32 - bits));
00074 }
00075 
00076 #define TE0(i) Te0[((i) >> 24) & 0xff]
00077 #define TE1(i) rotr(Te0[((i) >> 16) & 0xff], 8)
00078 #define TE2(i) rotr(Te0[((i) >> 8) & 0xff], 16)
00079 #define TE3(i) rotr(Te0[(i) & 0xff], 24)
00080 #define TE41(i) ((Te0[((i) >> 24) & 0xff] << 8) & 0xff000000)
00081 #define TE42(i) (Te0[((i) >> 16) & 0xff] & 0x00ff0000)
00082 #define TE43(i) (Te0[((i) >> 8) & 0xff] & 0x0000ff00)
00083 #define TE44(i) ((Te0[(i) & 0xff] >> 8) & 0x000000ff)
00084 #define TE421(i) ((Te0[((i) >> 16) & 0xff] << 8) & 0xff000000)
00085 #define TE432(i) (Te0[((i) >> 8) & 0xff] & 0x00ff0000)
00086 #define TE443(i) (Te0[(i) & 0xff] & 0x0000ff00)
00087 #define TE414(i) ((Te0[((i) >> 24) & 0xff] >> 8) & 0x000000ff)
00088 #define TE4(i) ((Te0[(i)] >> 8) & 0x000000ff)
00089 
00090 #define TD0(i) Td0[((i) >> 24) & 0xff]
00091 #define TD1(i) rotr(Td0[((i) >> 16) & 0xff], 8)
00092 #define TD2(i) rotr(Td0[((i) >> 8) & 0xff], 16)
00093 #define TD3(i) rotr(Td0[(i) & 0xff], 24)
00094 #define TD41(i) (Td4s[((i) >> 24) & 0xff] << 24)
00095 #define TD42(i) (Td4s[((i) >> 16) & 0xff] << 16)
00096 #define TD43(i) (Td4s[((i) >> 8) & 0xff] << 8)
00097 #define TD44(i) (Td4s[(i) & 0xff])
00098 #define TD0_(i) Td0[(i) & 0xff]
00099 #define TD1_(i) rotr(Td0[(i) & 0xff], 8)
00100 #define TD2_(i) rotr(Td0[(i) & 0xff], 16)
00101 #define TD3_(i) rotr(Td0[(i) & 0xff], 24)
00102 
00103 #endif /* AES_SMALL_TABLES */
00104 
00105 #ifdef _MSC_VER
00106 #define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
00107 #define GETU32(p) SWAP(*((u32 *)(p)))
00108 #define PUTU32(ct, st) { *((u32 *)(ct)) = SWAP((st)); }
00109 #else
00110 #define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ \
00111 ((u32)(pt)[2] <<  8) ^ ((u32)(pt)[3]))
00112 #define PUTU32(ct, st) { \
00113 (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
00114 (ct)[2] = (u8)((st) >>  8); (ct)[3] = (u8)(st); }
00115 #endif
00116 
00117 #define AES_PRIV_SIZE (4 * 44)
00118 
00119 void rijndaelKeySetupEnc(u32 rk[/*44*/], const u8 cipherKey[]);