CC3000HostDriver for device TI CC3000 some changes were made due to mbed compiler and the use of void*
Embed:
(wiki syntax)
Show/hide line numbers
security.cpp
00001 /***************************************************************************** 00002 * 00003 * security.c - CC3000 Host Driver Implementation. 00004 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * Neither the name of Texas Instruments Incorporated nor the names of 00019 * its contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00025 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00026 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 *****************************************************************************/ 00035 00036 //***************************************************************************** 00037 // 00038 //! \addtogroup security_api 00039 //! @{ 00040 // 00041 //***************************************************************************** 00042 00043 #include "security.h" 00044 00045 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG 00046 // foreward sbox 00047 const unsigned char sbox[256] = { 00048 //0 1 2 3 4 5 6 7 8 9 A B C D E F 00049 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0 00050 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1 00051 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2 00052 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3 00053 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4 00054 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5 00055 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6 00056 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7 00057 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8 00058 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9 00059 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A 00060 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B 00061 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C 00062 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D 00063 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E 00064 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F 00065 // inverse sbox 00066 const unsigned char rsbox[256] = 00067 { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb 00068 , 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb 00069 , 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e 00070 , 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25 00071 , 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92 00072 , 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84 00073 , 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06 00074 , 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b 00075 , 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73 00076 , 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e 00077 , 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b 00078 , 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4 00079 , 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f 00080 , 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef 00081 , 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61 00082 , 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; 00083 // round constant 00084 const unsigned char Rcon[11] = { 00085 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36}; 00086 00087 00088 unsigned char expandedKey[176]; 00089 00090 //***************************************************************************** 00091 // 00092 //! expandKey 00093 //! 00094 //! @param key AES128 key - 16 bytes 00095 //! @param expandedKey expanded AES128 key 00096 //! 00097 //! @return none 00098 //! 00099 //! @brief expend a 16 bytes key for AES128 implementation 00100 //! 00101 //***************************************************************************** 00102 00103 void expandKey(unsigned char *expandedKey, 00104 unsigned char *key) 00105 { 00106 unsigned short ii, buf1; 00107 for (ii=0;ii<16;ii++) 00108 expandedKey[ii] = key[ii]; 00109 for (ii=1;ii<11;ii++){ 00110 buf1 = expandedKey[ii*16 - 4]; 00111 expandedKey[ii*16 + 0] = sbox[expandedKey[ii*16 - 3]]^expandedKey[(ii-1)*16 + 0]^Rcon[ii]; 00112 expandedKey[ii*16 + 1] = sbox[expandedKey[ii*16 - 2]]^expandedKey[(ii-1)*16 + 1]; 00113 expandedKey[ii*16 + 2] = sbox[expandedKey[ii*16 - 1]]^expandedKey[(ii-1)*16 + 2]; 00114 expandedKey[ii*16 + 3] = sbox[buf1 ]^expandedKey[(ii-1)*16 + 3]; 00115 expandedKey[ii*16 + 4] = expandedKey[(ii-1)*16 + 4]^expandedKey[ii*16 + 0]; 00116 expandedKey[ii*16 + 5] = expandedKey[(ii-1)*16 + 5]^expandedKey[ii*16 + 1]; 00117 expandedKey[ii*16 + 6] = expandedKey[(ii-1)*16 + 6]^expandedKey[ii*16 + 2]; 00118 expandedKey[ii*16 + 7] = expandedKey[(ii-1)*16 + 7]^expandedKey[ii*16 + 3]; 00119 expandedKey[ii*16 + 8] = expandedKey[(ii-1)*16 + 8]^expandedKey[ii*16 + 4]; 00120 expandedKey[ii*16 + 9] = expandedKey[(ii-1)*16 + 9]^expandedKey[ii*16 + 5]; 00121 expandedKey[ii*16 +10] = expandedKey[(ii-1)*16 +10]^expandedKey[ii*16 + 6]; 00122 expandedKey[ii*16 +11] = expandedKey[(ii-1)*16 +11]^expandedKey[ii*16 + 7]; 00123 expandedKey[ii*16 +12] = expandedKey[(ii-1)*16 +12]^expandedKey[ii*16 + 8]; 00124 expandedKey[ii*16 +13] = expandedKey[(ii-1)*16 +13]^expandedKey[ii*16 + 9]; 00125 expandedKey[ii*16 +14] = expandedKey[(ii-1)*16 +14]^expandedKey[ii*16 +10]; 00126 expandedKey[ii*16 +15] = expandedKey[(ii-1)*16 +15]^expandedKey[ii*16 +11]; 00127 } 00128 00129 } 00130 00131 //***************************************************************************** 00132 // 00133 //! galois_mul2 00134 //! 00135 //! @param value argument to multiply 00136 //! 00137 //! @return multiplied argument 00138 //! 00139 //! @brief multiply by 2 in the galois field 00140 //! 00141 //***************************************************************************** 00142 00143 unsigned char galois_mul2(unsigned char value) 00144 { 00145 if (value>>7) 00146 { 00147 value = value << 1; 00148 return (value^0x1b); 00149 } else 00150 return value<<1; 00151 } 00152 00153 //***************************************************************************** 00154 // 00155 //! aes_encr 00156 //! 00157 //! @param[in] expandedKey expanded AES128 key 00158 //! @param[in/out] state 16 bytes of plain text and cipher text 00159 //! 00160 //! @return none 00161 //! 00162 //! @brief internal implementation of AES128 encryption. 00163 //! straight forward aes encryption implementation 00164 //! first the group of operations 00165 //! - addRoundKey 00166 //! - subbytes 00167 //! - shiftrows 00168 //! - mixcolums 00169 //! is executed 9 times, after this addroundkey to finish the 9th 00170 //! round, after that the 10th round without mixcolums 00171 //! no further subfunctions to save cycles for function calls 00172 //! no structuring with "for (....)" to save cycles. 00173 //! 00174 //! 00175 //***************************************************************************** 00176 00177 void aes_encr(unsigned char *state, unsigned char *expandedKey) 00178 { 00179 unsigned char buf1, buf2, buf3, round; 00180 00181 for (round = 0; round < 9; round ++){ 00182 // addroundkey, sbox and shiftrows 00183 // row 0 00184 state[ 0] = sbox[(state[ 0] ^ expandedKey[(round*16) ])]; 00185 state[ 4] = sbox[(state[ 4] ^ expandedKey[(round*16) + 4])]; 00186 state[ 8] = sbox[(state[ 8] ^ expandedKey[(round*16) + 8])]; 00187 state[12] = sbox[(state[12] ^ expandedKey[(round*16) + 12])]; 00188 // row 1 00189 buf1 = state[1] ^ expandedKey[(round*16) + 1]; 00190 state[ 1] = sbox[(state[ 5] ^ expandedKey[(round*16) + 5])]; 00191 state[ 5] = sbox[(state[ 9] ^ expandedKey[(round*16) + 9])]; 00192 state[ 9] = sbox[(state[13] ^ expandedKey[(round*16) + 13])]; 00193 state[13] = sbox[buf1]; 00194 // row 2 00195 buf1 = state[2] ^ expandedKey[(round*16) + 2]; 00196 buf2 = state[6] ^ expandedKey[(round*16) + 6]; 00197 state[ 2] = sbox[(state[10] ^ expandedKey[(round*16) + 10])]; 00198 state[ 6] = sbox[(state[14] ^ expandedKey[(round*16) + 14])]; 00199 state[10] = sbox[buf1]; 00200 state[14] = sbox[buf2]; 00201 // row 3 00202 buf1 = state[15] ^ expandedKey[(round*16) + 15]; 00203 state[15] = sbox[(state[11] ^ expandedKey[(round*16) + 11])]; 00204 state[11] = sbox[(state[ 7] ^ expandedKey[(round*16) + 7])]; 00205 state[ 7] = sbox[(state[ 3] ^ expandedKey[(round*16) + 3])]; 00206 state[ 3] = sbox[buf1]; 00207 00208 // mixcolums ////////// 00209 // col1 00210 buf1 = state[0] ^ state[1] ^ state[2] ^ state[3]; 00211 buf2 = state[0]; 00212 buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1; 00213 buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1; 00214 buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1; 00215 buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1; 00216 // col2 00217 buf1 = state[4] ^ state[5] ^ state[6] ^ state[7]; 00218 buf2 = state[4]; 00219 buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1; 00220 buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1; 00221 buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1; 00222 buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1; 00223 // col3 00224 buf1 = state[8] ^ state[9] ^ state[10] ^ state[11]; 00225 buf2 = state[8]; 00226 buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1; 00227 buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1; 00228 buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1; 00229 buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1; 00230 // col4 00231 buf1 = state[12] ^ state[13] ^ state[14] ^ state[15]; 00232 buf2 = state[12]; 00233 buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1; 00234 buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1; 00235 buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1; 00236 buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1; 00237 00238 } 00239 // 10th round without mixcols 00240 state[ 0] = sbox[(state[ 0] ^ expandedKey[(round*16) ])]; 00241 state[ 4] = sbox[(state[ 4] ^ expandedKey[(round*16) + 4])]; 00242 state[ 8] = sbox[(state[ 8] ^ expandedKey[(round*16) + 8])]; 00243 state[12] = sbox[(state[12] ^ expandedKey[(round*16) + 12])]; 00244 // row 1 00245 buf1 = state[1] ^ expandedKey[(round*16) + 1]; 00246 state[ 1] = sbox[(state[ 5] ^ expandedKey[(round*16) + 5])]; 00247 state[ 5] = sbox[(state[ 9] ^ expandedKey[(round*16) + 9])]; 00248 state[ 9] = sbox[(state[13] ^ expandedKey[(round*16) + 13])]; 00249 state[13] = sbox[buf1]; 00250 // row 2 00251 buf1 = state[2] ^ expandedKey[(round*16) + 2]; 00252 buf2 = state[6] ^ expandedKey[(round*16) + 6]; 00253 state[ 2] = sbox[(state[10] ^ expandedKey[(round*16) + 10])]; 00254 state[ 6] = sbox[(state[14] ^ expandedKey[(round*16) + 14])]; 00255 state[10] = sbox[buf1]; 00256 state[14] = sbox[buf2]; 00257 // row 3 00258 buf1 = state[15] ^ expandedKey[(round*16) + 15]; 00259 state[15] = sbox[(state[11] ^ expandedKey[(round*16) + 11])]; 00260 state[11] = sbox[(state[ 7] ^ expandedKey[(round*16) + 7])]; 00261 state[ 7] = sbox[(state[ 3] ^ expandedKey[(round*16) + 3])]; 00262 state[ 3] = sbox[buf1]; 00263 // last addroundkey 00264 state[ 0]^=expandedKey[160]; 00265 state[ 1]^=expandedKey[161]; 00266 state[ 2]^=expandedKey[162]; 00267 state[ 3]^=expandedKey[163]; 00268 state[ 4]^=expandedKey[164]; 00269 state[ 5]^=expandedKey[165]; 00270 state[ 6]^=expandedKey[166]; 00271 state[ 7]^=expandedKey[167]; 00272 state[ 8]^=expandedKey[168]; 00273 state[ 9]^=expandedKey[169]; 00274 state[10]^=expandedKey[170]; 00275 state[11]^=expandedKey[171]; 00276 state[12]^=expandedKey[172]; 00277 state[13]^=expandedKey[173]; 00278 state[14]^=expandedKey[174]; 00279 state[15]^=expandedKey[175]; 00280 } 00281 00282 //***************************************************************************** 00283 // 00284 //! aes_decr 00285 //! 00286 //! @param[in] expandedKey expanded AES128 key 00287 //! @param[in\out] state 16 bytes of cipher text and plain text 00288 //! 00289 //! @return none 00290 //! 00291 //! @brief internal implementation of AES128 decryption. 00292 //! straight forward aes decryption implementation 00293 //! the order of substeps is the exact reverse of decryption 00294 //! inverse functions: 00295 //! - addRoundKey is its own inverse 00296 //! - rsbox is inverse of sbox 00297 //! - rightshift instead of leftshift 00298 //! - invMixColumns = barreto + mixColumns 00299 //! no further subfunctions to save cycles for function calls 00300 //! no structuring with "for (....)" to save cycles 00301 //! 00302 //***************************************************************************** 00303 00304 void aes_decr(unsigned char *state, unsigned char *expandedKey) 00305 { 00306 unsigned char buf1, buf2, buf3; 00307 signed char round; 00308 round = 9; 00309 00310 // initial addroundkey 00311 state[ 0]^=expandedKey[160]; 00312 state[ 1]^=expandedKey[161]; 00313 state[ 2]^=expandedKey[162]; 00314 state[ 3]^=expandedKey[163]; 00315 state[ 4]^=expandedKey[164]; 00316 state[ 5]^=expandedKey[165]; 00317 state[ 6]^=expandedKey[166]; 00318 state[ 7]^=expandedKey[167]; 00319 state[ 8]^=expandedKey[168]; 00320 state[ 9]^=expandedKey[169]; 00321 state[10]^=expandedKey[170]; 00322 state[11]^=expandedKey[171]; 00323 state[12]^=expandedKey[172]; 00324 state[13]^=expandedKey[173]; 00325 state[14]^=expandedKey[174]; 00326 state[15]^=expandedKey[175]; 00327 00328 // 10th round without mixcols 00329 state[ 0] = rsbox[state[ 0]] ^ expandedKey[(round*16) ]; 00330 state[ 4] = rsbox[state[ 4]] ^ expandedKey[(round*16) + 4]; 00331 state[ 8] = rsbox[state[ 8]] ^ expandedKey[(round*16) + 8]; 00332 state[12] = rsbox[state[12]] ^ expandedKey[(round*16) + 12]; 00333 // row 1 00334 buf1 = rsbox[state[13]] ^ expandedKey[(round*16) + 1]; 00335 state[13] = rsbox[state[ 9]] ^ expandedKey[(round*16) + 13]; 00336 state[ 9] = rsbox[state[ 5]] ^ expandedKey[(round*16) + 9]; 00337 state[ 5] = rsbox[state[ 1]] ^ expandedKey[(round*16) + 5]; 00338 state[ 1] = buf1; 00339 // row 2 00340 buf1 = rsbox[state[ 2]] ^ expandedKey[(round*16) + 10]; 00341 buf2 = rsbox[state[ 6]] ^ expandedKey[(round*16) + 14]; 00342 state[ 2] = rsbox[state[10]] ^ expandedKey[(round*16) + 2]; 00343 state[ 6] = rsbox[state[14]] ^ expandedKey[(round*16) + 6]; 00344 state[10] = buf1; 00345 state[14] = buf2; 00346 // row 3 00347 buf1 = rsbox[state[ 3]] ^ expandedKey[(round*16) + 15]; 00348 state[ 3] = rsbox[state[ 7]] ^ expandedKey[(round*16) + 3]; 00349 state[ 7] = rsbox[state[11]] ^ expandedKey[(round*16) + 7]; 00350 state[11] = rsbox[state[15]] ^ expandedKey[(round*16) + 11]; 00351 state[15] = buf1; 00352 00353 for (round = 8; round >= 0; round--){ 00354 // barreto 00355 //col1 00356 buf1 = galois_mul2(galois_mul2(state[0]^state[2])); 00357 buf2 = galois_mul2(galois_mul2(state[1]^state[3])); 00358 state[0] ^= buf1; state[1] ^= buf2; state[2] ^= buf1; state[3] ^= buf2; 00359 //col2 00360 buf1 = galois_mul2(galois_mul2(state[4]^state[6])); 00361 buf2 = galois_mul2(galois_mul2(state[5]^state[7])); 00362 state[4] ^= buf1; state[5] ^= buf2; state[6] ^= buf1; state[7] ^= buf2; 00363 //col3 00364 buf1 = galois_mul2(galois_mul2(state[8]^state[10])); 00365 buf2 = galois_mul2(galois_mul2(state[9]^state[11])); 00366 state[8] ^= buf1; state[9] ^= buf2; state[10] ^= buf1; state[11] ^= buf2; 00367 //col4 00368 buf1 = galois_mul2(galois_mul2(state[12]^state[14])); 00369 buf2 = galois_mul2(galois_mul2(state[13]^state[15])); 00370 state[12] ^= buf1; state[13] ^= buf2; state[14] ^= buf1; state[15] ^= buf2; 00371 // mixcolums ////////// 00372 // col1 00373 buf1 = state[0] ^ state[1] ^ state[2] ^ state[3]; 00374 buf2 = state[0]; 00375 buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1; 00376 buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1; 00377 buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1; 00378 buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1; 00379 // col2 00380 buf1 = state[4] ^ state[5] ^ state[6] ^ state[7]; 00381 buf2 = state[4]; 00382 buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1; 00383 buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1; 00384 buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1; 00385 buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1; 00386 // col3 00387 buf1 = state[8] ^ state[9] ^ state[10] ^ state[11]; 00388 buf2 = state[8]; 00389 buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1; 00390 buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1; 00391 buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1; 00392 buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1; 00393 // col4 00394 buf1 = state[12] ^ state[13] ^ state[14] ^ state[15]; 00395 buf2 = state[12]; 00396 buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1; 00397 buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1; 00398 buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1; 00399 buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1; 00400 00401 // addroundkey, rsbox and shiftrows 00402 // row 0 00403 state[ 0] = rsbox[state[ 0]] ^ expandedKey[(round*16) ]; 00404 state[ 4] = rsbox[state[ 4]] ^ expandedKey[(round*16) + 4]; 00405 state[ 8] = rsbox[state[ 8]] ^ expandedKey[(round*16) + 8]; 00406 state[12] = rsbox[state[12]] ^ expandedKey[(round*16) + 12]; 00407 // row 1 00408 buf1 = rsbox[state[13]] ^ expandedKey[(round*16) + 1]; 00409 state[13] = rsbox[state[ 9]] ^ expandedKey[(round*16) + 13]; 00410 state[ 9] = rsbox[state[ 5]] ^ expandedKey[(round*16) + 9]; 00411 state[ 5] = rsbox[state[ 1]] ^ expandedKey[(round*16) + 5]; 00412 state[ 1] = buf1; 00413 // row 2 00414 buf1 = rsbox[state[ 2]] ^ expandedKey[(round*16) + 10]; 00415 buf2 = rsbox[state[ 6]] ^ expandedKey[(round*16) + 14]; 00416 state[ 2] = rsbox[state[10]] ^ expandedKey[(round*16) + 2]; 00417 state[ 6] = rsbox[state[14]] ^ expandedKey[(round*16) + 6]; 00418 state[10] = buf1; 00419 state[14] = buf2; 00420 // row 3 00421 buf1 = rsbox[state[ 3]] ^ expandedKey[(round*16) + 15]; 00422 state[ 3] = rsbox[state[ 7]] ^ expandedKey[(round*16) + 3]; 00423 state[ 7] = rsbox[state[11]] ^ expandedKey[(round*16) + 7]; 00424 state[11] = rsbox[state[15]] ^ expandedKey[(round*16) + 11]; 00425 state[15] = buf1; 00426 } 00427 00428 } 00429 00430 //***************************************************************************** 00431 // 00432 //! aes_encrypt 00433 //! 00434 //! @param[in] key AES128 key of size 16 bytes 00435 //! @param[in\out] state 16 bytes of plain text and cipher text 00436 //! 00437 //! @return none 00438 //! 00439 //! @brief AES128 encryption: 00440 //! Given AES128 key and 16 bytes plain text, cipher text of 16 bytes 00441 //! is computed. The AES implementation is in mode ECB (Electronic 00442 //! Code Book). 00443 //! 00444 //! 00445 //***************************************************************************** 00446 00447 void aes_encrypt(unsigned char *state, 00448 unsigned char *key) 00449 { 00450 // expand the key into 176 bytes 00451 expandKey(expandedKey, key); 00452 aes_encr(state, expandedKey); 00453 } 00454 00455 //***************************************************************************** 00456 // 00457 //! aes_decrypt 00458 //! 00459 //! @param[in] key AES128 key of size 16 bytes 00460 //! @param[in\out] state 16 bytes of cipher text and plain text 00461 //! 00462 //! @return none 00463 //! 00464 //! @brief AES128 decryption: 00465 //! Given AES128 key and 16 bytes cipher text, plain text of 16 bytes 00466 //! is computed The AES implementation is in mode ECB 00467 //! (Electronic Code Book). 00468 //! 00469 //! 00470 //***************************************************************************** 00471 00472 void aes_decrypt(unsigned char *state, 00473 unsigned char *key) 00474 { 00475 expandKey(expandedKey, key); // expand the key into 176 bytes 00476 aes_decr(state, expandedKey); 00477 } 00478 00479 //***************************************************************************** 00480 // 00481 //! aes_read_key 00482 //! 00483 //! @param[out] key AES128 key of size 16 bytes 00484 //! 00485 //! @return on success 0, error otherwise. 00486 //! 00487 //! @brief Reads AES128 key from EEPROM 00488 //! Reads the AES128 key from fileID #12 in EEPROM 00489 //! returns an error if the key does not exist. 00490 //! 00491 //! 00492 //***************************************************************************** 00493 00494 signed long aes_read_key(unsigned char *key) 00495 { 00496 signed long returnValue; 00497 00498 returnValue = nvmem_read(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); 00499 00500 return returnValue; 00501 } 00502 00503 //***************************************************************************** 00504 // 00505 //! aes_write_key 00506 //! 00507 //! @param[out] key AES128 key of size 16 bytes 00508 //! 00509 //! @return on success 0, error otherwise. 00510 //! 00511 //! @brief writes AES128 key from EEPROM 00512 //! Writes the AES128 key to fileID #12 in EEPROM 00513 //! 00514 //! 00515 //***************************************************************************** 00516 00517 signed long aes_write_key(unsigned char *key) 00518 { 00519 signed long returnValue; 00520 00521 returnValue = nvmem_write(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key); 00522 00523 return returnValue; 00524 } 00525 00526 #endif //CC3000_UNENCRYPTED_SMART_CONFIG 00527 00528 //***************************************************************************** 00529 // 00530 // Close the Doxygen group. 00531 //! @} 00532 // 00533 //***************************************************************************** 00534
Generated on Tue Jul 12 2022 19:26:44 by
1.7.2