BeRTOS
|
00001 00041 #include "aes.h" 00042 #include <cfg/compiler.h> 00043 #include <cpu/byteorder.h> 00044 00045 #include <string.h> 00046 00047 // AES only supports Nb=4 00048 #define Nb 4 // number of columns in the state & expanded key 00049 00050 typedef struct 00051 { 00052 BlockCipher c; 00053 uint8_t num_rounds; 00054 int8_t key_status; 00055 uint8_t _dummy1; 00056 uint8_t _dummy2; 00057 uint8_t expkey[0]; 00058 } AES_Context; 00059 00060 00061 #if CPU_REG_BITS == 32 00062 00063 // 32-bit optimized implementation 00064 #include "aes_f32.h" 00065 00066 #else 00067 00068 // Full 8-bit implementation 00069 #include "aes_f8.h" 00070 00071 #endif 00072 00073 00074 /******************************************************************************/ 00075 00076 void AES128_init(AES128_Context *aes_) 00077 { 00078 AES_Context *aes = (AES_Context *)aes_; 00079 aes->c.set_key = AES_expandKey; 00080 aes->c.enc_block = AES_encrypt; 00081 aes->c.dec_block = AES_decrypt; 00082 aes->c.block_len = Nb*4; 00083 aes->c.key_len = 16; 00084 aes->num_rounds = 10; 00085 } 00086 00087 void AES192_init(AES192_Context *aes_) 00088 { 00089 AES_Context *aes = (AES_Context *)aes_; 00090 aes->c.set_key = AES_expandKey; 00091 aes->c.enc_block = AES_encrypt; 00092 aes->c.dec_block = AES_decrypt; 00093 aes->c.block_len = Nb*4; 00094 aes->c.key_len = 24; 00095 aes->num_rounds = 12; 00096 } 00097 00098 void AES256_init(AES256_Context *aes_) 00099 { 00100 AES_Context *aes = (AES_Context *)aes_; 00101 aes->c.set_key = AES_expandKey; 00102 aes->c.enc_block = AES_encrypt; 00103 aes->c.dec_block = AES_decrypt; 00104 aes->c.block_len = Nb*4; 00105 aes->c.key_len = 32; 00106 aes->num_rounds = 14; 00107 }