The code from https://github.com/vpcola/Nucleo

Committer:
sinrab
Date:
Wed Oct 08 11:00:24 2014 +0000
Revision:
0:5464d5e415e5
The code from https://github.com/vpcola/Nucleo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sinrab 0:5464d5e415e5 1 /*****************************************************************************
sinrab 0:5464d5e415e5 2 *
sinrab 0:5464d5e415e5 3 * C++ interface/implementation created by Martin Kojtal (0xc0170). Thanks to
sinrab 0:5464d5e415e5 4 * Jim Carver and Frank Vannieuwkerke for their inital cc3000 mbed port and
sinrab 0:5464d5e415e5 5 * provided help.
sinrab 0:5464d5e415e5 6 *
sinrab 0:5464d5e415e5 7 * This version of "host driver" uses CC3000 Host Driver Implementation. Thus
sinrab 0:5464d5e415e5 8 * read the following copyright:
sinrab 0:5464d5e415e5 9 *
sinrab 0:5464d5e415e5 10 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
sinrab 0:5464d5e415e5 11 *
sinrab 0:5464d5e415e5 12 * Redistribution and use in source and binary forms, with or without
sinrab 0:5464d5e415e5 13 * modification, are permitted provided that the following conditions
sinrab 0:5464d5e415e5 14 * are met:
sinrab 0:5464d5e415e5 15 *
sinrab 0:5464d5e415e5 16 * Redistributions of source code must retain the above copyright
sinrab 0:5464d5e415e5 17 * notice, this list of conditions and the following disclaimer.
sinrab 0:5464d5e415e5 18 *
sinrab 0:5464d5e415e5 19 * Redistributions in binary form must reproduce the above copyright
sinrab 0:5464d5e415e5 20 * notice, this list of conditions and the following disclaimer in the
sinrab 0:5464d5e415e5 21 * documentation and/or other materials provided with the
sinrab 0:5464d5e415e5 22 * distribution.
sinrab 0:5464d5e415e5 23 *
sinrab 0:5464d5e415e5 24 * Neither the name of Texas Instruments Incorporated nor the names of
sinrab 0:5464d5e415e5 25 * its contributors may be used to endorse or promote products derived
sinrab 0:5464d5e415e5 26 * from this software without specific prior written permission.
sinrab 0:5464d5e415e5 27 *
sinrab 0:5464d5e415e5 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
sinrab 0:5464d5e415e5 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
sinrab 0:5464d5e415e5 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
sinrab 0:5464d5e415e5 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
sinrab 0:5464d5e415e5 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sinrab 0:5464d5e415e5 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
sinrab 0:5464d5e415e5 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
sinrab 0:5464d5e415e5 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
sinrab 0:5464d5e415e5 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
sinrab 0:5464d5e415e5 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
sinrab 0:5464d5e415e5 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
sinrab 0:5464d5e415e5 39 *
sinrab 0:5464d5e415e5 40 *****************************************************************************/
sinrab 0:5464d5e415e5 41 #include "cc3000.h"
sinrab 0:5464d5e415e5 42
sinrab 0:5464d5e415e5 43 namespace mbed_cc3000 {
sinrab 0:5464d5e415e5 44
sinrab 0:5464d5e415e5 45 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
sinrab 0:5464d5e415e5 46
sinrab 0:5464d5e415e5 47 // forward sbox
sinrab 0:5464d5e415e5 48 static const uint8_t sbox[256] = {
sinrab 0:5464d5e415e5 49 //0 1 2 3 4 5 6 7 8 9 A B C D E F
sinrab 0:5464d5e415e5 50 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0
sinrab 0:5464d5e415e5 51 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1
sinrab 0:5464d5e415e5 52 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2
sinrab 0:5464d5e415e5 53 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3
sinrab 0:5464d5e415e5 54 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4
sinrab 0:5464d5e415e5 55 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5
sinrab 0:5464d5e415e5 56 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6
sinrab 0:5464d5e415e5 57 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7
sinrab 0:5464d5e415e5 58 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8
sinrab 0:5464d5e415e5 59 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9
sinrab 0:5464d5e415e5 60 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A
sinrab 0:5464d5e415e5 61 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B
sinrab 0:5464d5e415e5 62 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C
sinrab 0:5464d5e415e5 63 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D
sinrab 0:5464d5e415e5 64 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E
sinrab 0:5464d5e415e5 65 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F
sinrab 0:5464d5e415e5 66 // inverse sbox
sinrab 0:5464d5e415e5 67 static const uint8_t rsbox[256] =
sinrab 0:5464d5e415e5 68 { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb
sinrab 0:5464d5e415e5 69 , 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb
sinrab 0:5464d5e415e5 70 , 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e
sinrab 0:5464d5e415e5 71 , 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25
sinrab 0:5464d5e415e5 72 , 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92
sinrab 0:5464d5e415e5 73 , 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84
sinrab 0:5464d5e415e5 74 , 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06
sinrab 0:5464d5e415e5 75 , 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b
sinrab 0:5464d5e415e5 76 , 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73
sinrab 0:5464d5e415e5 77 , 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e
sinrab 0:5464d5e415e5 78 , 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b
sinrab 0:5464d5e415e5 79 , 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4
sinrab 0:5464d5e415e5 80 , 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f
sinrab 0:5464d5e415e5 81 , 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef
sinrab 0:5464d5e415e5 82 , 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61
sinrab 0:5464d5e415e5 83 , 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
sinrab 0:5464d5e415e5 84 // round constant
sinrab 0:5464d5e415e5 85 static const uint8_t Rcon[11] = {0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
sinrab 0:5464d5e415e5 86
sinrab 0:5464d5e415e5 87 void cc3000_security::expandKey(uint8_t *expanded_key, uint8_t *key) {
sinrab 0:5464d5e415e5 88 uint16_t ii, buf1;
sinrab 0:5464d5e415e5 89 for (ii=0;ii<16;ii++)
sinrab 0:5464d5e415e5 90 expanded_key[ii] = key[ii];
sinrab 0:5464d5e415e5 91 for (ii=1;ii<11;ii++)
sinrab 0:5464d5e415e5 92 {
sinrab 0:5464d5e415e5 93 buf1 = expanded_key[ii*16 - 4];
sinrab 0:5464d5e415e5 94 expanded_key[ii*16 + 0] = sbox[expanded_key[ii*16 - 3]]^expanded_key[(ii-1)*16 + 0]^Rcon[ii];
sinrab 0:5464d5e415e5 95 expanded_key[ii*16 + 1] = sbox[expanded_key[ii*16 - 2]]^expanded_key[(ii-1)*16 + 1];
sinrab 0:5464d5e415e5 96 expanded_key[ii*16 + 2] = sbox[expanded_key[ii*16 - 1]]^expanded_key[(ii-1)*16 + 2];
sinrab 0:5464d5e415e5 97 expanded_key[ii*16 + 3] = sbox[buf1 ]^expanded_key[(ii-1)*16 + 3];
sinrab 0:5464d5e415e5 98 expanded_key[ii*16 + 4] = expanded_key[(ii-1)*16 + 4]^expanded_key[ii*16 + 0];
sinrab 0:5464d5e415e5 99 expanded_key[ii*16 + 5] = expanded_key[(ii-1)*16 + 5]^expanded_key[ii*16 + 1];
sinrab 0:5464d5e415e5 100 expanded_key[ii*16 + 6] = expanded_key[(ii-1)*16 + 6]^expanded_key[ii*16 + 2];
sinrab 0:5464d5e415e5 101 expanded_key[ii*16 + 7] = expanded_key[(ii-1)*16 + 7]^expanded_key[ii*16 + 3];
sinrab 0:5464d5e415e5 102 expanded_key[ii*16 + 8] = expanded_key[(ii-1)*16 + 8]^expanded_key[ii*16 + 4];
sinrab 0:5464d5e415e5 103 expanded_key[ii*16 + 9] = expanded_key[(ii-1)*16 + 9]^expanded_key[ii*16 + 5];
sinrab 0:5464d5e415e5 104 expanded_key[ii*16 +10] = expanded_key[(ii-1)*16 +10]^expanded_key[ii*16 + 6];
sinrab 0:5464d5e415e5 105 expanded_key[ii*16 +11] = expanded_key[(ii-1)*16 +11]^expanded_key[ii*16 + 7];
sinrab 0:5464d5e415e5 106 expanded_key[ii*16 +12] = expanded_key[(ii-1)*16 +12]^expanded_key[ii*16 + 8];
sinrab 0:5464d5e415e5 107 expanded_key[ii*16 +13] = expanded_key[(ii-1)*16 +13]^expanded_key[ii*16 + 9];
sinrab 0:5464d5e415e5 108 expanded_key[ii*16 +14] = expanded_key[(ii-1)*16 +14]^expanded_key[ii*16 +10];
sinrab 0:5464d5e415e5 109 expanded_key[ii*16 +15] = expanded_key[(ii-1)*16 +15]^expanded_key[ii*16 +11];
sinrab 0:5464d5e415e5 110 }
sinrab 0:5464d5e415e5 111 }
sinrab 0:5464d5e415e5 112
sinrab 0:5464d5e415e5 113 uint8_t cc3000_security::galois_mul2(uint8_t value) {
sinrab 0:5464d5e415e5 114 if (value >> 7) {
sinrab 0:5464d5e415e5 115 value = value << 1;
sinrab 0:5464d5e415e5 116 return (value ^ 0x1b);
sinrab 0:5464d5e415e5 117 } else {
sinrab 0:5464d5e415e5 118 return (value << 1);
sinrab 0:5464d5e415e5 119 }
sinrab 0:5464d5e415e5 120 }
sinrab 0:5464d5e415e5 121
sinrab 0:5464d5e415e5 122 void cc3000_security::aes_encr(uint8_t *state, uint8_t *expanded_key) {
sinrab 0:5464d5e415e5 123 uint8_t buf1, buf2, buf3, round;
sinrab 0:5464d5e415e5 124
sinrab 0:5464d5e415e5 125 for (round = 0; round < 9; round ++)
sinrab 0:5464d5e415e5 126 {
sinrab 0:5464d5e415e5 127 // addroundkey, sbox and shiftrows
sinrab 0:5464d5e415e5 128 // row 0
sinrab 0:5464d5e415e5 129 state[ 0] = sbox[(state[ 0] ^ expanded_key[(round*16) ])];
sinrab 0:5464d5e415e5 130 state[ 4] = sbox[(state[ 4] ^ expanded_key[(round*16) + 4])];
sinrab 0:5464d5e415e5 131 state[ 8] = sbox[(state[ 8] ^ expanded_key[(round*16) + 8])];
sinrab 0:5464d5e415e5 132 state[12] = sbox[(state[12] ^ expanded_key[(round*16) + 12])];
sinrab 0:5464d5e415e5 133 // row 1
sinrab 0:5464d5e415e5 134 buf1 = state[1] ^ expanded_key[(round*16) + 1];
sinrab 0:5464d5e415e5 135 state[ 1] = sbox[(state[ 5] ^ expanded_key[(round*16) + 5])];
sinrab 0:5464d5e415e5 136 state[ 5] = sbox[(state[ 9] ^ expanded_key[(round*16) + 9])];
sinrab 0:5464d5e415e5 137 state[ 9] = sbox[(state[13] ^ expanded_key[(round*16) + 13])];
sinrab 0:5464d5e415e5 138 state[13] = sbox[buf1];
sinrab 0:5464d5e415e5 139 // row 2
sinrab 0:5464d5e415e5 140 buf1 = state[2] ^ expanded_key[(round*16) + 2];
sinrab 0:5464d5e415e5 141 buf2 = state[6] ^ expanded_key[(round*16) + 6];
sinrab 0:5464d5e415e5 142 state[ 2] = sbox[(state[10] ^ expanded_key[(round*16) + 10])];
sinrab 0:5464d5e415e5 143 state[ 6] = sbox[(state[14] ^ expanded_key[(round*16) + 14])];
sinrab 0:5464d5e415e5 144 state[10] = sbox[buf1];
sinrab 0:5464d5e415e5 145 state[14] = sbox[buf2];
sinrab 0:5464d5e415e5 146 // row 3
sinrab 0:5464d5e415e5 147 buf1 = state[15] ^ expanded_key[(round*16) + 15];
sinrab 0:5464d5e415e5 148 state[15] = sbox[(state[11] ^ expanded_key[(round*16) + 11])];
sinrab 0:5464d5e415e5 149 state[11] = sbox[(state[ 7] ^ expanded_key[(round*16) + 7])];
sinrab 0:5464d5e415e5 150 state[ 7] = sbox[(state[ 3] ^ expanded_key[(round*16) + 3])];
sinrab 0:5464d5e415e5 151 state[ 3] = sbox[buf1];
sinrab 0:5464d5e415e5 152
sinrab 0:5464d5e415e5 153 // mixcolums //////////
sinrab 0:5464d5e415e5 154 // col1
sinrab 0:5464d5e415e5 155 buf1 = state[0] ^ state[1] ^ state[2] ^ state[3];
sinrab 0:5464d5e415e5 156 buf2 = state[0];
sinrab 0:5464d5e415e5 157 buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 158 buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 159 buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 160 buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 161 // col2
sinrab 0:5464d5e415e5 162 buf1 = state[4] ^ state[5] ^ state[6] ^ state[7];
sinrab 0:5464d5e415e5 163 buf2 = state[4];
sinrab 0:5464d5e415e5 164 buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 165 buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 166 buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 167 buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 168 // col3
sinrab 0:5464d5e415e5 169 buf1 = state[8] ^ state[9] ^ state[10] ^ state[11];
sinrab 0:5464d5e415e5 170 buf2 = state[8];
sinrab 0:5464d5e415e5 171 buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 172 buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 173 buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 174 buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 175 // col4
sinrab 0:5464d5e415e5 176 buf1 = state[12] ^ state[13] ^ state[14] ^ state[15];
sinrab 0:5464d5e415e5 177 buf2 = state[12];
sinrab 0:5464d5e415e5 178 buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 179 buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 180 buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 181 buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 182
sinrab 0:5464d5e415e5 183 }
sinrab 0:5464d5e415e5 184 // 10th round without mixcols
sinrab 0:5464d5e415e5 185 state[ 0] = sbox[(state[ 0] ^ expanded_key[(round*16) ])];
sinrab 0:5464d5e415e5 186 state[ 4] = sbox[(state[ 4] ^ expanded_key[(round*16) + 4])];
sinrab 0:5464d5e415e5 187 state[ 8] = sbox[(state[ 8] ^ expanded_key[(round*16) + 8])];
sinrab 0:5464d5e415e5 188 state[12] = sbox[(state[12] ^ expanded_key[(round*16) + 12])];
sinrab 0:5464d5e415e5 189 // row 1
sinrab 0:5464d5e415e5 190 buf1 = state[1] ^ expanded_key[(round*16) + 1];
sinrab 0:5464d5e415e5 191 state[ 1] = sbox[(state[ 5] ^ expanded_key[(round*16) + 5])];
sinrab 0:5464d5e415e5 192 state[ 5] = sbox[(state[ 9] ^ expanded_key[(round*16) + 9])];
sinrab 0:5464d5e415e5 193 state[ 9] = sbox[(state[13] ^ expanded_key[(round*16) + 13])];
sinrab 0:5464d5e415e5 194 state[13] = sbox[buf1];
sinrab 0:5464d5e415e5 195 // row 2
sinrab 0:5464d5e415e5 196 buf1 = state[2] ^ expanded_key[(round*16) + 2];
sinrab 0:5464d5e415e5 197 buf2 = state[6] ^ expanded_key[(round*16) + 6];
sinrab 0:5464d5e415e5 198 state[ 2] = sbox[(state[10] ^ expanded_key[(round*16) + 10])];
sinrab 0:5464d5e415e5 199 state[ 6] = sbox[(state[14] ^ expanded_key[(round*16) + 14])];
sinrab 0:5464d5e415e5 200 state[10] = sbox[buf1];
sinrab 0:5464d5e415e5 201 state[14] = sbox[buf2];
sinrab 0:5464d5e415e5 202 // row 3
sinrab 0:5464d5e415e5 203 buf1 = state[15] ^ expanded_key[(round*16) + 15];
sinrab 0:5464d5e415e5 204 state[15] = sbox[(state[11] ^ expanded_key[(round*16) + 11])];
sinrab 0:5464d5e415e5 205 state[11] = sbox[(state[ 7] ^ expanded_key[(round*16) + 7])];
sinrab 0:5464d5e415e5 206 state[ 7] = sbox[(state[ 3] ^ expanded_key[(round*16) + 3])];
sinrab 0:5464d5e415e5 207 state[ 3] = sbox[buf1];
sinrab 0:5464d5e415e5 208 // last addroundkey
sinrab 0:5464d5e415e5 209 state[ 0]^=expanded_key[160];
sinrab 0:5464d5e415e5 210 state[ 1]^=expanded_key[161];
sinrab 0:5464d5e415e5 211 state[ 2]^=expanded_key[162];
sinrab 0:5464d5e415e5 212 state[ 3]^=expanded_key[163];
sinrab 0:5464d5e415e5 213 state[ 4]^=expanded_key[164];
sinrab 0:5464d5e415e5 214 state[ 5]^=expanded_key[165];
sinrab 0:5464d5e415e5 215 state[ 6]^=expanded_key[166];
sinrab 0:5464d5e415e5 216 state[ 7]^=expanded_key[167];
sinrab 0:5464d5e415e5 217 state[ 8]^=expanded_key[168];
sinrab 0:5464d5e415e5 218 state[ 9]^=expanded_key[169];
sinrab 0:5464d5e415e5 219 state[10]^=expanded_key[170];
sinrab 0:5464d5e415e5 220 state[11]^=expanded_key[171];
sinrab 0:5464d5e415e5 221 state[12]^=expanded_key[172];
sinrab 0:5464d5e415e5 222 state[13]^=expanded_key[173];
sinrab 0:5464d5e415e5 223 state[14]^=expanded_key[174];
sinrab 0:5464d5e415e5 224 state[15]^=expanded_key[175];
sinrab 0:5464d5e415e5 225 }
sinrab 0:5464d5e415e5 226
sinrab 0:5464d5e415e5 227 void cc3000_security::aes_decr(uint8_t *state, uint8_t *expanded_key) {
sinrab 0:5464d5e415e5 228 uint8_t buf1, buf2, buf3;
sinrab 0:5464d5e415e5 229 int8_t round;
sinrab 0:5464d5e415e5 230 round = 9;
sinrab 0:5464d5e415e5 231
sinrab 0:5464d5e415e5 232 // initial addroundkey
sinrab 0:5464d5e415e5 233 state[ 0]^=expanded_key[160];
sinrab 0:5464d5e415e5 234 state[ 1]^=expanded_key[161];
sinrab 0:5464d5e415e5 235 state[ 2]^=expanded_key[162];
sinrab 0:5464d5e415e5 236 state[ 3]^=expanded_key[163];
sinrab 0:5464d5e415e5 237 state[ 4]^=expanded_key[164];
sinrab 0:5464d5e415e5 238 state[ 5]^=expanded_key[165];
sinrab 0:5464d5e415e5 239 state[ 6]^=expanded_key[166];
sinrab 0:5464d5e415e5 240 state[ 7]^=expanded_key[167];
sinrab 0:5464d5e415e5 241 state[ 8]^=expanded_key[168];
sinrab 0:5464d5e415e5 242 state[ 9]^=expanded_key[169];
sinrab 0:5464d5e415e5 243 state[10]^=expanded_key[170];
sinrab 0:5464d5e415e5 244 state[11]^=expanded_key[171];
sinrab 0:5464d5e415e5 245 state[12]^=expanded_key[172];
sinrab 0:5464d5e415e5 246 state[13]^=expanded_key[173];
sinrab 0:5464d5e415e5 247 state[14]^=expanded_key[174];
sinrab 0:5464d5e415e5 248 state[15]^=expanded_key[175];
sinrab 0:5464d5e415e5 249
sinrab 0:5464d5e415e5 250 // 10th round without mixcols
sinrab 0:5464d5e415e5 251 state[ 0] = rsbox[state[ 0]] ^ expanded_key[(round*16) ];
sinrab 0:5464d5e415e5 252 state[ 4] = rsbox[state[ 4]] ^ expanded_key[(round*16) + 4];
sinrab 0:5464d5e415e5 253 state[ 8] = rsbox[state[ 8]] ^ expanded_key[(round*16) + 8];
sinrab 0:5464d5e415e5 254 state[12] = rsbox[state[12]] ^ expanded_key[(round*16) + 12];
sinrab 0:5464d5e415e5 255 // row 1
sinrab 0:5464d5e415e5 256 buf1 = rsbox[state[13]] ^ expanded_key[(round*16) + 1];
sinrab 0:5464d5e415e5 257 state[13] = rsbox[state[ 9]] ^ expanded_key[(round*16) + 13];
sinrab 0:5464d5e415e5 258 state[ 9] = rsbox[state[ 5]] ^ expanded_key[(round*16) + 9];
sinrab 0:5464d5e415e5 259 state[ 5] = rsbox[state[ 1]] ^ expanded_key[(round*16) + 5];
sinrab 0:5464d5e415e5 260 state[ 1] = buf1;
sinrab 0:5464d5e415e5 261 // row 2
sinrab 0:5464d5e415e5 262 buf1 = rsbox[state[ 2]] ^ expanded_key[(round*16) + 10];
sinrab 0:5464d5e415e5 263 buf2 = rsbox[state[ 6]] ^ expanded_key[(round*16) + 14];
sinrab 0:5464d5e415e5 264 state[ 2] = rsbox[state[10]] ^ expanded_key[(round*16) + 2];
sinrab 0:5464d5e415e5 265 state[ 6] = rsbox[state[14]] ^ expanded_key[(round*16) + 6];
sinrab 0:5464d5e415e5 266 state[10] = buf1;
sinrab 0:5464d5e415e5 267 state[14] = buf2;
sinrab 0:5464d5e415e5 268 // row 3
sinrab 0:5464d5e415e5 269 buf1 = rsbox[state[ 3]] ^ expanded_key[(round*16) + 15];
sinrab 0:5464d5e415e5 270 state[ 3] = rsbox[state[ 7]] ^ expanded_key[(round*16) + 3];
sinrab 0:5464d5e415e5 271 state[ 7] = rsbox[state[11]] ^ expanded_key[(round*16) + 7];
sinrab 0:5464d5e415e5 272 state[11] = rsbox[state[15]] ^ expanded_key[(round*16) + 11];
sinrab 0:5464d5e415e5 273 state[15] = buf1;
sinrab 0:5464d5e415e5 274
sinrab 0:5464d5e415e5 275 for (round = 8; round >= 0; round--)
sinrab 0:5464d5e415e5 276 {
sinrab 0:5464d5e415e5 277 // barreto
sinrab 0:5464d5e415e5 278 //col1
sinrab 0:5464d5e415e5 279 buf1 = galois_mul2(galois_mul2(state[0]^state[2]));
sinrab 0:5464d5e415e5 280 buf2 = galois_mul2(galois_mul2(state[1]^state[3]));
sinrab 0:5464d5e415e5 281 state[0] ^= buf1; state[1] ^= buf2; state[2] ^= buf1; state[3] ^= buf2;
sinrab 0:5464d5e415e5 282 //col2
sinrab 0:5464d5e415e5 283 buf1 = galois_mul2(galois_mul2(state[4]^state[6]));
sinrab 0:5464d5e415e5 284 buf2 = galois_mul2(galois_mul2(state[5]^state[7]));
sinrab 0:5464d5e415e5 285 state[4] ^= buf1; state[5] ^= buf2; state[6] ^= buf1; state[7] ^= buf2;
sinrab 0:5464d5e415e5 286 //col3
sinrab 0:5464d5e415e5 287 buf1 = galois_mul2(galois_mul2(state[8]^state[10]));
sinrab 0:5464d5e415e5 288 buf2 = galois_mul2(galois_mul2(state[9]^state[11]));
sinrab 0:5464d5e415e5 289 state[8] ^= buf1; state[9] ^= buf2; state[10] ^= buf1; state[11] ^= buf2;
sinrab 0:5464d5e415e5 290 //col4
sinrab 0:5464d5e415e5 291 buf1 = galois_mul2(galois_mul2(state[12]^state[14]));
sinrab 0:5464d5e415e5 292 buf2 = galois_mul2(galois_mul2(state[13]^state[15]));
sinrab 0:5464d5e415e5 293 state[12] ^= buf1; state[13] ^= buf2; state[14] ^= buf1; state[15] ^= buf2;
sinrab 0:5464d5e415e5 294 // mixcolums //////////
sinrab 0:5464d5e415e5 295 // col1
sinrab 0:5464d5e415e5 296 buf1 = state[0] ^ state[1] ^ state[2] ^ state[3];
sinrab 0:5464d5e415e5 297 buf2 = state[0];
sinrab 0:5464d5e415e5 298 buf3 = state[0]^state[1]; buf3=galois_mul2(buf3); state[0] = state[0] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 299 buf3 = state[1]^state[2]; buf3=galois_mul2(buf3); state[1] = state[1] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 300 buf3 = state[2]^state[3]; buf3=galois_mul2(buf3); state[2] = state[2] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 301 buf3 = state[3]^buf2; buf3=galois_mul2(buf3); state[3] = state[3] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 302 // col2
sinrab 0:5464d5e415e5 303 buf1 = state[4] ^ state[5] ^ state[6] ^ state[7];
sinrab 0:5464d5e415e5 304 buf2 = state[4];
sinrab 0:5464d5e415e5 305 buf3 = state[4]^state[5]; buf3=galois_mul2(buf3); state[4] = state[4] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 306 buf3 = state[5]^state[6]; buf3=galois_mul2(buf3); state[5] = state[5] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 307 buf3 = state[6]^state[7]; buf3=galois_mul2(buf3); state[6] = state[6] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 308 buf3 = state[7]^buf2; buf3=galois_mul2(buf3); state[7] = state[7] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 309 // col3
sinrab 0:5464d5e415e5 310 buf1 = state[8] ^ state[9] ^ state[10] ^ state[11];
sinrab 0:5464d5e415e5 311 buf2 = state[8];
sinrab 0:5464d5e415e5 312 buf3 = state[8]^state[9]; buf3=galois_mul2(buf3); state[8] = state[8] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 313 buf3 = state[9]^state[10]; buf3=galois_mul2(buf3); state[9] = state[9] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 314 buf3 = state[10]^state[11]; buf3=galois_mul2(buf3); state[10] = state[10] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 315 buf3 = state[11]^buf2; buf3=galois_mul2(buf3); state[11] = state[11] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 316 // col4
sinrab 0:5464d5e415e5 317 buf1 = state[12] ^ state[13] ^ state[14] ^ state[15];
sinrab 0:5464d5e415e5 318 buf2 = state[12];
sinrab 0:5464d5e415e5 319 buf3 = state[12]^state[13]; buf3=galois_mul2(buf3); state[12] = state[12] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 320 buf3 = state[13]^state[14]; buf3=galois_mul2(buf3); state[13] = state[13] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 321 buf3 = state[14]^state[15]; buf3=galois_mul2(buf3); state[14] = state[14] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 322 buf3 = state[15]^buf2; buf3=galois_mul2(buf3); state[15] = state[15] ^ buf3 ^ buf1;
sinrab 0:5464d5e415e5 323
sinrab 0:5464d5e415e5 324 // addroundkey, rsbox and shiftrows
sinrab 0:5464d5e415e5 325 // row 0
sinrab 0:5464d5e415e5 326 state[ 0] = rsbox[state[ 0]] ^ expanded_key[(round*16) ];
sinrab 0:5464d5e415e5 327 state[ 4] = rsbox[state[ 4]] ^ expanded_key[(round*16) + 4];
sinrab 0:5464d5e415e5 328 state[ 8] = rsbox[state[ 8]] ^ expanded_key[(round*16) + 8];
sinrab 0:5464d5e415e5 329 state[12] = rsbox[state[12]] ^ expanded_key[(round*16) + 12];
sinrab 0:5464d5e415e5 330 // row 1
sinrab 0:5464d5e415e5 331 buf1 = rsbox[state[13]] ^ expanded_key[(round*16) + 1];
sinrab 0:5464d5e415e5 332 state[13] = rsbox[state[ 9]] ^ expanded_key[(round*16) + 13];
sinrab 0:5464d5e415e5 333 state[ 9] = rsbox[state[ 5]] ^ expanded_key[(round*16) + 9];
sinrab 0:5464d5e415e5 334 state[ 5] = rsbox[state[ 1]] ^ expanded_key[(round*16) + 5];
sinrab 0:5464d5e415e5 335 state[ 1] = buf1;
sinrab 0:5464d5e415e5 336 // row 2
sinrab 0:5464d5e415e5 337 buf1 = rsbox[state[ 2]] ^ expanded_key[(round*16) + 10];
sinrab 0:5464d5e415e5 338 buf2 = rsbox[state[ 6]] ^ expanded_key[(round*16) + 14];
sinrab 0:5464d5e415e5 339 state[ 2] = rsbox[state[10]] ^ expanded_key[(round*16) + 2];
sinrab 0:5464d5e415e5 340 state[ 6] = rsbox[state[14]] ^ expanded_key[(round*16) + 6];
sinrab 0:5464d5e415e5 341 state[10] = buf1;
sinrab 0:5464d5e415e5 342 state[14] = buf2;
sinrab 0:5464d5e415e5 343 // row 3
sinrab 0:5464d5e415e5 344 buf1 = rsbox[state[ 3]] ^ expanded_key[(round*16) + 15];
sinrab 0:5464d5e415e5 345 state[ 3] = rsbox[state[ 7]] ^ expanded_key[(round*16) + 3];
sinrab 0:5464d5e415e5 346 state[ 7] = rsbox[state[11]] ^ expanded_key[(round*16) + 7];
sinrab 0:5464d5e415e5 347 state[11] = rsbox[state[15]] ^ expanded_key[(round*16) + 11];
sinrab 0:5464d5e415e5 348 state[15] = buf1;
sinrab 0:5464d5e415e5 349 }
sinrab 0:5464d5e415e5 350 }
sinrab 0:5464d5e415e5 351
sinrab 0:5464d5e415e5 352 void cc3000_security::aes_encrypt(uint8_t *state, uint8_t *key) {
sinrab 0:5464d5e415e5 353 // expand the key into 176 bytes
sinrab 0:5464d5e415e5 354 expandKey(_expanded_key, key);
sinrab 0:5464d5e415e5 355 aes_encr(state, _expanded_key);
sinrab 0:5464d5e415e5 356 }
sinrab 0:5464d5e415e5 357
sinrab 0:5464d5e415e5 358 void cc3000_security::aes_decrypt(uint8_t *state, uint8_t *key) {
sinrab 0:5464d5e415e5 359 expandKey(_expanded_key, key); // expand the key into 176 bytes
sinrab 0:5464d5e415e5 360 aes_decr(state, _expanded_key);
sinrab 0:5464d5e415e5 361 }
sinrab 0:5464d5e415e5 362
sinrab 0:5464d5e415e5 363 int32_t cc3000_security::aes_read_key(uint8_t *key) {
sinrab 0:5464d5e415e5 364 int32_t returnValue;
sinrab 0:5464d5e415e5 365
sinrab 0:5464d5e415e5 366 returnValue = nvmem_read(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key);
sinrab 0:5464d5e415e5 367
sinrab 0:5464d5e415e5 368 return returnValue;
sinrab 0:5464d5e415e5 369 }
sinrab 0:5464d5e415e5 370
sinrab 0:5464d5e415e5 371 int32_t cc3000_security::aes_write_key(uint8_t *key) {
sinrab 0:5464d5e415e5 372 int32_t returnValue;
sinrab 0:5464d5e415e5 373
sinrab 0:5464d5e415e5 374 returnValue = nvmem_write(NVMEM_AES128_KEY_FILEID, AES128_KEY_SIZE, 0, key);
sinrab 0:5464d5e415e5 375
sinrab 0:5464d5e415e5 376 return returnValue;
sinrab 0:5464d5e415e5 377 }
sinrab 0:5464d5e415e5 378 #endif
sinrab 0:5464d5e415e5 379
sinrab 0:5464d5e415e5 380 } // mbed_cc3000 namespace
sinrab 0:5464d5e415e5 381
sinrab 0:5464d5e415e5 382