/************************************************************************************ * AES Encryption Library * * Author(s): JRJEN1, BPPED1, B06623 * * (c) Copyright 2005, Freescale, Inc. All rights reserved. * * Freescale Confidential Proprietary * Digianswer Confidential * * No part of this document must be reproduced in any form - including copied, * transcribed, printed or by any electronic means - without specific written * permission from Freescale. * * Last Inspected: 1/14/2008 * Last Tested: 1/14/2008 ************************************************************************************/ #include "Aes.h" /****************************************************************************** * LOCAL DATA * ******************************************************************************/ /* RAM DATA */ UINT8 gsTmp,gsTmp2, gsRound, gsTmp3; UINT8 *gReturnData; #if (MEMORY_IN_STACK == 1) UINT8 *gsState; UINT8 *gsKey; #else UINT8 gsState[16]; UINT8 gsKey[16]; #endif /****************************************************************************** * PRIVATE FUNCTIONS * ******************************************************************************/ /*****************************************************************************/ #define s_Box(x) gSBox_c[x] const unsigned char gSBox_c[256] = { 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22 }; unsigned static char xtime(unsigned char x) { if(x & 0x80){ return ((x << 1) ^ 0x1B); } else { return (x << 1); } } void static AES_AddKeyByteSubShiftKeyExpand(void) { /* Add round key */ /* S-Box lookup */ /* Transformation */ /* Expand key */ gsState[0] = s_Box(gsState[0]^gsKey[0]); gsState[4] = s_Box(gsState[4]^gsKey[4]); gsState[8] = s_Box(gsState[8]^gsKey[8]); gsState[12] = s_Box(gsState[12]^gsKey[12]); gsTmp = gsState[1]^gsKey[1]; gsState[1] = s_Box(gsState[5]^gsKey[5]); gsState[5] = s_Box(gsState[9]^gsKey[9]); gsState[9] = s_Box(gsState[13]^gsKey[13]); gsState[13] = s_Box(gsTmp); gsTmp = gsState[2]^gsKey[2]; gsState[2] = s_Box(gsState[10]^gsKey[10]); gsState[10] = s_Box(gsTmp); gsTmp = gsState[6]^gsKey[6]; gsState[6] = s_Box(gsState[14]^gsKey[14]); gsState[14] = s_Box(gsTmp); gsTmp = gsState[15]^gsKey[15]; gsState[15] = s_Box(gsState[11]^gsKey[11]); gsState[11] = s_Box(gsState[7]^gsKey[7]); gsState[7] = s_Box(gsState[3]^gsKey[3]); gsState[3] = s_Box(gsTmp); /* Expand key */ gsKey[0] ^= s_Box(gsKey[13]) ^ gsRound; gsKey[4] ^= gsKey[0]; gsKey[8] ^= gsKey[4]; gsKey[3] ^= s_Box(gsKey[12]); gsKey[12] ^= gsKey[8]; gsKey[1] ^= s_Box(gsKey[14]); gsKey[5] ^= gsKey[1]; gsKey[9] ^= gsKey[5]; gsKey[13] ^= gsKey[9]; gsKey[2] ^= s_Box(gsKey[15]); gsKey[6] ^= gsKey[2]; gsKey[10] ^= gsKey[6]; gsKey[14] ^= gsKey[10]; gsKey[7] ^= gsKey[3]; gsKey[11] ^= gsKey[7]; gsKey[15] ^= gsKey[11]; } /*****************************************************************************/ void static AES_MixColumn(void) { for(gsTmp3=0;gsTmp3<16;gsTmp3 = gsTmp3 + 4){ /* Mix */ gsTmp = gsState[0+gsTmp3]; gsTmp2=gsState[0+gsTmp3] ^ gsState[1+gsTmp3] ^ gsState[2+gsTmp3] ^ gsState[3+gsTmp3]; gsState[0+gsTmp3] ^= xtime(gsState[0+gsTmp3] ^ gsState[1+gsTmp3]) ^ gsTmp2; gsState[1+gsTmp3] ^= xtime(gsState[1+gsTmp3] ^ gsState[2+gsTmp3]) ^ gsTmp2; gsState[2+gsTmp3] ^= xtime(gsState[2+gsTmp3] ^ gsState[3+gsTmp3]) ^ gsTmp2; gsState[3+gsTmp3] ^= xtime(gsState[3+gsTmp3] ^ gsTmp) ^ gsTmp2; } } /****************************************************************************** * PUBLIC FUNCTIONS * ******************************************************************************/ /*************************************** AES ************************************** * AES encryption engine * * void SecLib_Aes(const unsigned char *pData, const unsigned char *pKey, unsigned char *pReturnData) * * Input: 128bit key, and 128bit data, d0,d1,d2,...,d13,d14,d15 * Output 128bit encrypted data cipher **********************************************************************************/ void Aes_Encrypt(UINT8 *Data, UINT8 *Key, UINT8 *ReturnData) { #if (MEMORY_IN_STACK == 1) UINT8 buff1[16]; UINT8 buff2[16]; gsState = buff1; gsKey = buff2; #endif gReturnData = ReturnData; for(gsTmp=0;gsTmp<16;gsTmp++){ gsKey[gsTmp] = Key[gsTmp]; gsState[gsTmp] = Data[gsTmp]; } gsRound = 1; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 2; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 4; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 8; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 16; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 32; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 64; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 128; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 27; AES_AddKeyByteSubShiftKeyExpand(); AES_MixColumn(); gsRound = 54; AES_AddKeyByteSubShiftKeyExpand(); for(gsTmp=0;gsTmp<16;gsTmp++){ gReturnData[gsTmp] = gsState[gsTmp] ^ gsKey[gsTmp]; } }