//############################################################################################################################################################################################################# #include "AES.h" //============================================================================================================================================================================================================= const uint8_t AES_INV_SBOX[256] PROGMEM= { 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB , 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB , 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E , 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25 , 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92 , 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84 , 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06 , 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B , 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73 , 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E , 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B , 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4 , 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F , 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF , 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61 , 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D }; //============================================================================================================================================================================================================= //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //============================================================================================================================================================================================================= //Static functions inline uint8_t AES_RotWord(uint8_t *Word); uint8_t AES_SubWord (uint8_t Word); inline void AES_KeyExpansion(uint8_t *AES_KEY, uint8_t *w); //uint8_t AES_xTime(uint8_t Value); void AES_AddRoundKey (uint8_t *State, uint8_t *Key); void AES_InvSubBytes(uint8_t *state); void AES_InvShiftRows(uint8_t *state); inline void AES_InvMixColumns(uint8_t *state); //void AES_ApplyIV(uint8_t *state, uint8_t *IV); //============================================================================================================================================================================================================= //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //============================================================================================================================================================================================================= inline uint8_t AES_RotWord(uint8_t *Word){ Word[0+4] = Word[1]; Word[1+4] = Word[2]; Word[2+4] = Word[3]; Word[3+4] = Word[0]; return Word[1]; } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uint8_t AES_SubWord (uint8_t Word) { //for (uint8_t i = 0; i < 256; i++){ //FIND BETTER SOLUTION (with out using precomputed values) // if (flash_read_byte((AES_INV_SBOX),i) == Word) // return i; //} //return 0; uint8_t j,i = 0; do { if (flash_read_byte((AES_INV_SBOX),i) == Word) j = i; i++; } while (i != 0); return j; } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- const uint8_t Rcon[7] PROGMEM= { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40};//, 0x80};//, 0x1B, 0x36 }; inline void AES_KeyExpansion(uint8_t *AES_KEY, uint8_t *w) { uint8_t KeyTmp; uint8_t I; //for (I = 0; I < AES_NK_256BIT*4; I++) //{ // w[I] = pgm_read_byte(&(AES_KEY[I])); //} _memcopy(AES_KEY,w,AES_NK_256BIT*4); for (I = AES_NK_256BIT*4; I < (uint8_t)(AES_NB*(uint8_t)(AES_NR_256BIT+1U)*4); I++) { KeyTmp = w[I-4]; if ((I & 0b11100) == 0 ) { if ((I & 0b11) == 0 ) { KeyTmp = AES_RotWord(&w[I-4]); KeyTmp = AES_SubWord(KeyTmp); //KeyTmp = KeyTmp ^ (1<<(I>>5)); KeyTmp = KeyTmp ^ flash_read_byte((Rcon),(I>>5)-1); } else KeyTmp = AES_SubWord(w[I]); } else if ( (I & 0b11100) == 16 ) KeyTmp = AES_SubWord(w[I-4]); w[I] = w[I-(AES_NK_256BIT*4)] ^ KeyTmp; } } //============================================================================================================================================================================================================= //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //============================================================================================================================================================================================================= //uint8_t AES_xTime(uint8_t Value) { return ( (Value & 0x80) ? ((uint8_t)(Value<<1)^0x1B) : (uint8_t)(Value<<1) ); } // //#define AES_MULTIPLY_02(X) AES_xTime((X)) //#define AES_MULTIPLY_03(X) ( (X) ^ AES_xTime((X)) ) //#define AES_MULTIPLY_09(X) ( (X) ^ AES_xTime(AES_xTime(AES_xTime((X)))) ) //#define AES_MULTIPLY_0B(X) ( (X) ^ AES_xTime((X)) ^ AES_xTime(AES_xTime(AES_xTime((X)))) ) //#define AES_MULTIPLY_0E(X) ( AES_xTime((X)) ^ AES_xTime(AES_xTime((X))) ^ AES_xTime(AES_xTime(AES_xTime((X)))) ) //#define AES_MULTIPLY_0D(X) ( (X) ^ AES_xTime(AES_xTime((X))) ^ AES_xTime(AES_xTime(AES_xTime((X)))) ) //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //static uint8_t rstate[4*AES_NB]; #define GF28_MUL2(a) { asm ("lsl %0 \n brcc .+2 \n eor %0, %1 \n" : "=r" (a) : "r" (c0x1b), "0" (a)); } //#define GF28_MUL2(a) { if (a&0x80) { a = (a<<1) ^ 0x1b; } else { a <<= 1; } } //#define GF28_MUL2(a) {a=AES_xTime(a);} inline void AES_InvMixColumns(uint8_t *state) { //for (uint8_t i = 0; i<16; i++) // rstate[i]=state[i]; //_memcopy(state,rstate,16); const uint8_t c0x1b = 0x1b; uint8_t i, a0, a1, a2, a3, sum, b, c, d, e; uint8_t* ptr = state; for(i = 0; i < 16; i+=4) //https://github.com/kildom/AVR-AES/blob/master/aes.c I don't fully understand this, but it works really fast { a0 = ptr[0]; a1 = ptr[1]; a2 = ptr[2]; a3 = ptr[3]; sum = a0 ^ a1 ^ a2 ^ a3; c = sum; GF28_MUL2(c); d = c ^ a0 ^ a2; GF28_MUL2(d); GF28_MUL2(d); d ^= sum; e = c ^ a1 ^ a3; GF28_MUL2(e); GF28_MUL2(e); e ^= sum; b = a0^a1; GF28_MUL2(b); *ptr++ = d ^ a0 ^ b; b = a1^a2; GF28_MUL2(b); *ptr++ = e ^ a1 ^ b; b = a2^a3; GF28_MUL2(b); *ptr++ = d ^ a2 ^ b; b = a3^a0; GF28_MUL2(b); *ptr++ = e ^ a3 ^ b; } //for (uint8_t i = 0; i < 16; i++){ // state[i] = AES_MULTIPLY_0E(rstate[(i&0xFC)+((i+0)&0b11)]) // ^ AES_MULTIPLY_0B(rstate[(i&0xFC)+((i+1)&0b11)]) // ^ AES_MULTIPLY_0D(rstate[(i&0xFC)+((i+2)&0b11)]) // ^ AES_MULTIPLY_09(rstate[(i&0xFC)+((i+3)&0b11)]); //} } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void AES_AddRoundKey (uint8_t *State, uint8_t *_key) { for (uint8_t i = 0; i < 16; i++) State[i] ^= (_key[i]); } //============================================================================================================================================================================================================= //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //============================================================================================================================================================================================================= void AES_InvShiftRows(uint8_t *state) { uint8_t ByteTmp1; uint8_t ByteTmp2; //Do not loop!!! Otherwise bad performance ByteTmp1 = state[13]; state[13] = state[9]; state[9] = state[5]; state[5] = state[1]; state[1] = ByteTmp1; ByteTmp1 = state[14]; ByteTmp2 = state[10]; state[14] = state[6]; state[10] = state[2]; state[6] = ByteTmp1; state[2] = ByteTmp2; ByteTmp1 = state[3]; state[3] = state[7]; state[7] = state[11]; state[11] = state[15]; state[15] = ByteTmp1; } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void AES_InvSubBytes(uint8_t *state) { for (uint8_t i = 0; i < 16; i++) state[i] = flash_read_byte((AES_INV_SBOX),state[i]); } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- static uint8_t NextIV[16]; void AES_InvCipher(uint8_t *IN, uint8_t *w, uint8_t *IV) { uint8_t *state = IN; _memcopy(IN,NextIV,16); AES_AddRoundKey(state, &w[AES_NR_256BIT*AES_NB*4]); for (uint8_t I = (uint8_t)(AES_NR_256BIT-1); I > 0; I--) { AES_InvShiftRows(state); AES_InvSubBytes(state); AES_AddRoundKey(state, &w[I<<4]); AES_InvMixColumns(state); } AES_InvShiftRows(state); AES_InvSubBytes(state); AES_AddRoundKey(state, &w[0]); AES_AddRoundKey(state, IV); //XOR output with IV _memcopy(NextIV,IV,16); } //============================================================================================================================================================================================================= //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //============================================================================================================================================================================================================= uint8_t w[(uint8_t)(AES_NB * (uint8_t)(AES_NR_256BIT+1U))*4]; uint8_t IVstored[16]; void AES_Decrypt(uint8_t *InpData, uint16_t InpDataLength) { //if (KeyLength != AES_256) return; //Only AES CBC 256 is supported if ( (InpDataLength & 0b1111) != 0 ) { return; } uint16_t DataPos = 0; while ( InpDataLength > 0 ) { AES_InvCipher(&InpData[DataPos], w, IVstored); DataPos = (uint16_t)(DataPos + AES_BLOCK_SIZE); InpDataLength = (uint16_t)(InpDataLength - AES_BLOCK_SIZE); } } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void AES_PrepareKeys(uint8_t *Key, uint8_t *IV){ AES_KeyExpansion(Key, w); //AES_Copy(IV,IVstored); //for (uint8_t i = 0; i < 16; i++){ // IVstored[i] = pgm_read_byte(&(IV[i])); //} _memcopy(IV,IVstored,16); } //------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //#############################################################################################################################################################################################################