aes.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /****************************************************************************
  2. * crypto/aes.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  5. * Extracted from the CC3000 Host Driver Implementation.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * Neither the name of Texas Instruments Incorporated nor the names of
  20. * its contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdint.h>
  41. #include <errno.h>
  42. #include <nuttx/crypto/aes.h>
  43. /****************************************************************************
  44. * Private Data
  45. ****************************************************************************/
  46. /* Forward sbox */
  47. static const uint8_t g_sbox[256] =
  48. {
  49. /* 0 1 2 3 4 5 6 7 8 9
  50. * A B C D E F
  51. */
  52. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01,
  53. 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, /* 0 */
  54. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4,
  55. 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, /* 1 */
  56. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5,
  57. 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, /* 2 */
  58. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12,
  59. 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, /* 3 */
  60. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b,
  61. 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, /* 4 */
  62. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb,
  63. 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, /* 5 */
  64. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9,
  65. 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, /* 6 */
  66. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6,
  67. 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, /* 7 */
  68. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7,
  69. 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, /* 8 */
  70. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee,
  71. 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, /* 9 */
  72. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3,
  73. 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, /* A */
  74. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56,
  75. 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, /* B */
  76. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd,
  77. 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, /* C */
  78. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35,
  79. 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, /* D */
  80. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e,
  81. 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, /* E */
  82. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99,
  83. 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 /* F */
  84. };
  85. /* Inverse sbox */
  86. static const uint8_t g_rsbox[256] =
  87. {
  88. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40,
  89. 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  90. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e,
  91. 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  92. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c,
  93. 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  94. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b,
  95. 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  96. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4,
  97. 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  98. 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15,
  99. 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  100. 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4,
  101. 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  102. 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf,
  103. 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  104. 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2,
  105. 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  106. 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9,
  107. 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  108. 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7,
  109. 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  110. 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb,
  111. 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  112. 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12,
  113. 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  114. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5,
  115. 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  116. 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb,
  117. 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  118. 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69,
  119. 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  120. };
  121. /* Round constant */
  122. static const uint8_t g_rcon[11] =
  123. {
  124. 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
  125. };
  126. static struct aes_state_s g_aes_state;
  127. /****************************************************************************
  128. * Private Functions
  129. ****************************************************************************/
  130. /****************************************************************************
  131. * Name: expand_key
  132. *
  133. * Description:
  134. * Expend a 16 bytes key for AES128 implementation
  135. *
  136. * Input Parameters:
  137. * key AES128 key - 16 bytes
  138. * expanded_key expanded AES128 key
  139. *
  140. * Returned Value:
  141. * None
  142. *
  143. ****************************************************************************/
  144. static void expand_key(FAR uint8_t *expanded_key, FAR const uint8_t *key)
  145. {
  146. uint16_t buf1;
  147. uint16_t ii;
  148. for (ii = 0; ii < 16; ii++)
  149. {
  150. expanded_key[ii] = key[ii];
  151. }
  152. for (ii = 1; ii < 11; ii++)
  153. {
  154. buf1 = expanded_key[ii * 16 - 4];
  155. expanded_key[ii * 16 + 0] = g_sbox[expanded_key[ii *16 - 3]] ^
  156. expanded_key[(ii - 1) * 16 + 0] ^ g_rcon[ii];
  157. expanded_key[ii * 16 + 1] = g_sbox[expanded_key[ii *16 - 2]] ^
  158. expanded_key[(ii - 1) * 16 + 1];
  159. expanded_key[ii * 16 + 2] = g_sbox[expanded_key[ii *16 - 1]] ^
  160. expanded_key[(ii - 1) * 16 + 2];
  161. expanded_key[ii * 16 + 3] = g_sbox[buf1] ^
  162. expanded_key[(ii - 1) * 16 + 3];
  163. expanded_key[ii * 16 + 4] = expanded_key[(ii - 1) * 16 + 4] ^
  164. expanded_key[ii * 16 + 0];
  165. expanded_key[ii * 16 + 5] = expanded_key[(ii - 1) * 16 + 5] ^
  166. expanded_key[ii * 16 + 1];
  167. expanded_key[ii * 16 + 6] = expanded_key[(ii - 1) * 16 + 6] ^
  168. expanded_key[ii * 16 + 2];
  169. expanded_key[ii * 16 + 7] = expanded_key[(ii - 1) * 16 + 7] ^
  170. expanded_key[ii * 16 + 3];
  171. expanded_key[ii * 16 + 8] = expanded_key[(ii - 1) * 16 + 8] ^
  172. expanded_key[ii * 16 + 4];
  173. expanded_key[ii * 16 + 9] = expanded_key[(ii - 1) * 16 + 9] ^
  174. expanded_key[ii * 16 + 5];
  175. expanded_key[ii * 16 +10] = expanded_key[(ii - 1) * 16 +10] ^
  176. expanded_key[ii * 16 + 6];
  177. expanded_key[ii * 16 +11] = expanded_key[(ii - 1) * 16 +11] ^
  178. expanded_key[ii * 16 + 7];
  179. expanded_key[ii * 16 +12] = expanded_key[(ii - 1) * 16 +12] ^
  180. expanded_key[ii * 16 + 8];
  181. expanded_key[ii * 16 +13] = expanded_key[(ii - 1) * 16 +13] ^
  182. expanded_key[ii * 16 + 9];
  183. expanded_key[ii * 16 +14] = expanded_key[(ii - 1) * 16 +14] ^
  184. expanded_key[ii * 16 +10];
  185. expanded_key[ii * 16 +15] = expanded_key[(ii - 1) * 16 +15] ^
  186. expanded_key[ii * 16 +11];
  187. }
  188. }
  189. /****************************************************************************
  190. * Name: galois_mul2
  191. *
  192. * Description:
  193. * Multiply by 2 in the galois field
  194. *
  195. * Input Parameters:
  196. * value argument to multiply
  197. *
  198. * Returned Value:
  199. * Multiplied argument
  200. *
  201. ****************************************************************************/
  202. static uint8_t galois_mul2(uint8_t value)
  203. {
  204. if (value >> 7)
  205. {
  206. value = value << 1;
  207. return (value ^ 0x1b);
  208. }
  209. else
  210. {
  211. return value << 1;
  212. }
  213. }
  214. /****************************************************************************
  215. * Name: aes_encr
  216. *
  217. * Description:
  218. * Internal implementation of AES128 encryption.
  219. * Straight forward aes encryption implementation. First the group of
  220. * operations:
  221. *
  222. * - addRoundKey
  223. * - subbytes
  224. * - shiftrows
  225. * - mixcolums
  226. *
  227. * is executed 9 times, after this addroundkey to finish the 9th round,
  228. * after that the 10th round without mixcolums no further subfunctions
  229. * to save cycles for function calls no structuring with "for (....)"
  230. * to save cycles.
  231. *
  232. * Input Parameters:
  233. * expanded_key expanded AES128 key
  234. * state 16 bytes of plain text and cipher text
  235. *
  236. * Returned Value:
  237. * None
  238. *
  239. ****************************************************************************/
  240. static void aes_encr(FAR uint8_t *state, FAR const uint8_t *expanded_key)
  241. {
  242. uint8_t buf1;
  243. uint8_t buf2;
  244. uint8_t buf3;
  245. uint8_t round;
  246. for (round = 0; round < 9; round ++)
  247. {
  248. /* addroundkey, sbox and shiftrows */
  249. /* Row 0 */
  250. state[0] = g_sbox[(state[0] ^ expanded_key[(round * 16)])];
  251. state[4] = g_sbox[(state[4] ^ expanded_key[(round * 16) + 4])];
  252. state[8] = g_sbox[(state[8] ^ expanded_key[(round * 16) + 8])];
  253. state[12] = g_sbox[(state[12] ^ expanded_key[(round * 16) + 12])];
  254. /* Row 1 */
  255. buf1 = state[1] ^ expanded_key[(round * 16) + 1];
  256. state[1] = g_sbox[(state[5] ^ expanded_key[(round * 16) + 5])];
  257. state[5] = g_sbox[(state[9] ^ expanded_key[(round * 16) + 9])];
  258. state[9] = g_sbox[(state[13] ^ expanded_key[(round * 16) + 13])];
  259. state[13] = g_sbox[buf1];
  260. /* Row 2 */
  261. buf1 = state[2] ^ expanded_key[(round * 16) + 2];
  262. buf2 = state[6] ^ expanded_key[(round * 16) + 6];
  263. state[2] = g_sbox[(state[10] ^ expanded_key[(round * 16) + 10])];
  264. state[6] = g_sbox[(state[14] ^ expanded_key[(round * 16) + 14])];
  265. state[10] = g_sbox[buf1];
  266. state[14] = g_sbox[buf2];
  267. /* Row 3 */
  268. buf1 = state[15] ^ expanded_key[(round * 16) + 15];
  269. state[15] = g_sbox[(state[11] ^ expanded_key[(round * 16) + 11])];
  270. state[11] = g_sbox[(state[7] ^ expanded_key[(round * 16) + 7])];
  271. state[7] = g_sbox[(state[3] ^ expanded_key[(round * 16) + 3])];
  272. state[3] = g_sbox[buf1];
  273. /* mixcolums */
  274. /* Col1 */
  275. buf1 = state[0] ^ state[1] ^ state[2] ^ state[3];
  276. buf2 = state[0];
  277. buf3 = state[0] ^ state[1];
  278. buf3 = galois_mul2(buf3);
  279. state[0] = state[0] ^ buf3 ^ buf1;
  280. buf3 = state[1] ^ state[2];
  281. buf3 = galois_mul2(buf3);
  282. state[1] = state[1] ^ buf3 ^ buf1;
  283. buf3 = state[2] ^ state[3];
  284. buf3 = galois_mul2(buf3);
  285. state[2] = state[2] ^ buf3 ^ buf1;
  286. buf3 = state[3] ^ buf2;
  287. buf3 = galois_mul2(buf3);
  288. state[3] = state[3] ^ buf3 ^ buf1;
  289. /* Col2 */
  290. buf1 = state[4] ^ state[5] ^ state[6] ^ state[7];
  291. buf2 = state[4];
  292. buf3 = state[4] ^ state[5];
  293. buf3 = galois_mul2(buf3);
  294. state[4] = state[4] ^ buf3 ^ buf1;
  295. buf3 = state[5] ^ state[6];
  296. buf3 = galois_mul2(buf3);
  297. state[5] = state[5] ^ buf3 ^ buf1;
  298. buf3 = state[6] ^ state[7];
  299. buf3 = galois_mul2(buf3);
  300. state[6] = state[6] ^ buf3 ^ buf1;
  301. buf3 = state[7] ^ buf2;
  302. buf3 = galois_mul2(buf3);
  303. state[7] = state[7] ^ buf3 ^ buf1;
  304. /* Col3 */
  305. buf1 = state[8] ^ state[9] ^ state[10] ^ state[11];
  306. buf2 = state[8];
  307. buf3 = state[8] ^ state[9];
  308. buf3 = galois_mul2(buf3);
  309. state[8] = state[8] ^ buf3 ^ buf1;
  310. buf3 = state[9] ^ state[10];
  311. buf3 = galois_mul2(buf3);
  312. state[9] = state[9] ^ buf3 ^ buf1;
  313. buf3 = state[10] ^ state[11];
  314. buf3 = galois_mul2(buf3);
  315. state[10] = state[10] ^ buf3 ^ buf1;
  316. buf3 = state[11] ^ buf2;
  317. buf3 = galois_mul2(buf3);
  318. state[11] = state[11] ^ buf3 ^ buf1;
  319. /* Col4 */
  320. buf1 = state[12] ^ state[13] ^ state[14] ^ state[15];
  321. buf2 = state[12];
  322. buf3 = state[12] ^ state[13];
  323. buf3 = galois_mul2(buf3);
  324. state[12] = state[12] ^ buf3 ^ buf1;
  325. buf3 = state[13] ^ state[14];
  326. buf3 = galois_mul2(buf3);
  327. state[13] = state[13] ^ buf3 ^ buf1;
  328. buf3 = state[14] ^ state[15];
  329. buf3 = galois_mul2(buf3);
  330. state[14] = state[14] ^ buf3 ^ buf1;
  331. buf3 = state[15] ^ buf2;
  332. buf3 = galois_mul2(buf3);
  333. state[15] = state[15] ^ buf3 ^ buf1;
  334. }
  335. /* 10th round without mixcols */
  336. state[0] = g_sbox[(state[0] ^ expanded_key[(round * 16)])];
  337. state[4] = g_sbox[(state[4] ^ expanded_key[(round * 16) + 4])];
  338. state[8] = g_sbox[(state[8] ^ expanded_key[(round * 16) + 8])];
  339. state[12] = g_sbox[(state[12] ^ expanded_key[(round * 16) + 12])];
  340. /* Row 1 */
  341. buf1 = state[1] ^ expanded_key[(round * 16) + 1];
  342. state[1] = g_sbox[(state[5] ^ expanded_key[(round * 16) + 5])];
  343. state[5] = g_sbox[(state[9] ^ expanded_key[(round * 16) + 9])];
  344. state[9] = g_sbox[(state[13] ^ expanded_key[(round * 16) + 13])];
  345. state[13] = g_sbox[buf1];
  346. /* Row 2 */
  347. buf1 = state[2] ^ expanded_key[(round * 16) + 2];
  348. buf2 = state[6] ^ expanded_key[(round * 16) + 6];
  349. state[2] = g_sbox[(state[10] ^ expanded_key[(round * 16) + 10])];
  350. state[6] = g_sbox[(state[14] ^ expanded_key[(round * 16) + 14])];
  351. state[10] = g_sbox[buf1];
  352. state[14] = g_sbox[buf2];
  353. /* Row 3 */
  354. buf1 = state[15] ^ expanded_key[(round * 16) + 15];
  355. state[15] = g_sbox[(state[11] ^ expanded_key[(round * 16) + 11])];
  356. state[11] = g_sbox[(state[7] ^ expanded_key[(round * 16) + 7])];
  357. state[7] = g_sbox[(state[3] ^ expanded_key[(round * 16) + 3])];
  358. state[3] = g_sbox[buf1];
  359. /* Last addroundkey */
  360. state[0] ^= expanded_key[160];
  361. state[1] ^= expanded_key[161];
  362. state[2] ^= expanded_key[162];
  363. state[3] ^= expanded_key[163];
  364. state[4] ^= expanded_key[164];
  365. state[5] ^= expanded_key[165];
  366. state[6] ^= expanded_key[166];
  367. state[7] ^= expanded_key[167];
  368. state[8] ^= expanded_key[168];
  369. state[9] ^= expanded_key[169];
  370. state[10] ^= expanded_key[170];
  371. state[11] ^= expanded_key[171];
  372. state[12] ^= expanded_key[172];
  373. state[13] ^= expanded_key[173];
  374. state[14] ^= expanded_key[174];
  375. state[15] ^= expanded_key[175];
  376. }
  377. /****************************************************************************
  378. * Name: aes_decr
  379. *
  380. * Description:
  381. * Internal implementation of AES128 decryption.
  382. * Straight forward aes decryption implementation. The order of substeps is
  383. * the exact reverse of decryption inverse functions:
  384. *
  385. * - addRoundKey is its own inverse
  386. * - rsbox is inverse of sbox
  387. * - rightshift instead of leftshift
  388. * - invMixColumns = barreto + mixColumns
  389. *
  390. * No further subfunctions to save cycles for function calls no structuring
  391. * with "for (....)" to save cycles
  392. *
  393. * Input Parameters:
  394. * expanded_key expanded AES128 key
  395. * state 16 bytes of cipher text and plain text
  396. *
  397. * Returned Value:
  398. * None
  399. *
  400. ****************************************************************************/
  401. static void aes_decr(FAR uint8_t *state, FAR const uint8_t *expanded_key)
  402. {
  403. uint8_t buf1;
  404. uint8_t buf2;
  405. uint8_t buf3;
  406. int8_t round;
  407. round = 9;
  408. /* Initial addroundkey */
  409. state[0] ^= expanded_key[160];
  410. state[1] ^= expanded_key[161];
  411. state[2] ^= expanded_key[162];
  412. state[3] ^= expanded_key[163];
  413. state[4] ^= expanded_key[164];
  414. state[5] ^= expanded_key[165];
  415. state[6] ^= expanded_key[166];
  416. state[7] ^= expanded_key[167];
  417. state[8] ^= expanded_key[168];
  418. state[9] ^= expanded_key[169];
  419. state[10] ^= expanded_key[170];
  420. state[11] ^= expanded_key[171];
  421. state[12] ^= expanded_key[172];
  422. state[13] ^= expanded_key[173];
  423. state[14] ^= expanded_key[174];
  424. state[15] ^= expanded_key[175];
  425. /* 10th round without mixcols */
  426. state[0] = g_rsbox[state[0]] ^ expanded_key[(round * 16)];
  427. state[4] = g_rsbox[state[4]] ^ expanded_key[(round * 16) + 4];
  428. state[8] = g_rsbox[state[8]] ^ expanded_key[(round * 16) + 8];
  429. state[12] = g_rsbox[state[12]] ^ expanded_key[(round * 16) + 12];
  430. /* Row 1 */
  431. buf1 = g_rsbox[state[13]] ^ expanded_key[(round * 16) + 1];
  432. state[13] = g_rsbox[state[9]] ^ expanded_key[(round * 16) + 13];
  433. state[9] = g_rsbox[state[5]] ^ expanded_key[(round * 16) + 9];
  434. state[5] = g_rsbox[state[1]] ^ expanded_key[(round * 16) + 5];
  435. state[1] = buf1;
  436. /* Row 2 */
  437. buf1 = g_rsbox[state[2]] ^ expanded_key[(round * 16) + 10];
  438. buf2 = g_rsbox[state[6]] ^ expanded_key[(round * 16) + 14];
  439. state[2] = g_rsbox[state[10]] ^ expanded_key[(round * 16) + 2];
  440. state[6] = g_rsbox[state[14]] ^ expanded_key[(round * 16) + 6];
  441. state[10] = buf1;
  442. state[14] = buf2;
  443. /* Row 3 */
  444. buf1 = g_rsbox[state[3]] ^ expanded_key[(round * 16) + 15];
  445. state[3] = g_rsbox[state[7]] ^ expanded_key[(round * 16) + 3];
  446. state[7] = g_rsbox[state[11]] ^ expanded_key[(round * 16) + 7];
  447. state[11] = g_rsbox[state[15]] ^ expanded_key[(round * 16) + 11];
  448. state[15] = buf1;
  449. for (round = 8; round >= 0; round--)
  450. {
  451. /* barreto */
  452. /* Col1 */
  453. buf1 = galois_mul2(galois_mul2(state[0] ^ state[2]));
  454. buf2 = galois_mul2(galois_mul2(state[1] ^ state[3]));
  455. state[0] ^= buf1;
  456. state[1] ^= buf2;
  457. state[2] ^= buf1;
  458. state[3] ^= buf2;
  459. /* Col2 */
  460. buf1 = galois_mul2(galois_mul2(state[4] ^ state[6]));
  461. buf2 = galois_mul2(galois_mul2(state[5] ^ state[7]));
  462. state[4] ^= buf1;
  463. state[5] ^= buf2;
  464. state[6] ^= buf1;
  465. state[7] ^= buf2;
  466. /* Col3 */
  467. buf1 = galois_mul2(galois_mul2(state[8] ^ state[10]));
  468. buf2 = galois_mul2(galois_mul2(state[9] ^ state[11]));
  469. state[8] ^= buf1;
  470. state[9] ^= buf2;
  471. state[10] ^= buf1;
  472. state[11] ^= buf2;
  473. /* Col4 */
  474. buf1 = galois_mul2(galois_mul2(state[12] ^ state[14]));
  475. buf2 = galois_mul2(galois_mul2(state[13] ^ state[15]));
  476. state[12] ^= buf1;
  477. state[13] ^= buf2;
  478. state[14] ^= buf1;
  479. state[15] ^= buf2;
  480. /* mixcolums */
  481. /* Col1 */
  482. buf1 = state[0] ^ state[1] ^ state[2] ^ state[3];
  483. buf2 = state[0];
  484. buf3 = state[0] ^ state[1];
  485. buf3 = galois_mul2(buf3);
  486. state[0] = state[0] ^ buf3 ^ buf1;
  487. buf3 = state[1] ^ state[2];
  488. buf3 = galois_mul2(buf3);
  489. state[1] = state[1] ^ buf3 ^ buf1;
  490. buf3 = state[2] ^ state[3];
  491. buf3 = galois_mul2(buf3);
  492. state[2] = state[2] ^ buf3 ^ buf1;
  493. buf3 = state[3] ^ buf2;
  494. buf3 = galois_mul2(buf3);
  495. state[3] = state[3] ^ buf3 ^ buf1;
  496. /* Col2 */
  497. buf1 = state[4] ^ state[5] ^ state[6] ^ state[7];
  498. buf2 = state[4];
  499. buf3 = state[4] ^ state[5];
  500. buf3 = galois_mul2(buf3);
  501. state[4] = state[4] ^ buf3 ^ buf1;
  502. buf3 = state[5] ^ state[6];
  503. buf3 = galois_mul2(buf3);
  504. state[5] = state[5] ^ buf3 ^ buf1;
  505. buf3 = state[6] ^ state[7];
  506. buf3 = galois_mul2(buf3);
  507. state[6] = state[6] ^ buf3 ^ buf1;
  508. buf3 = state[7] ^ buf2;
  509. buf3 = galois_mul2(buf3);
  510. state[7] = state[7] ^ buf3 ^ buf1;
  511. /* Col3 */
  512. buf1 = state[8] ^ state[9] ^ state[10] ^ state[11];
  513. buf2 = state[8];
  514. buf3 = state[8] ^ state[9];
  515. buf3 = galois_mul2(buf3);
  516. state[8] = state[8] ^ buf3 ^ buf1;
  517. buf3 = state[9] ^ state[10];
  518. buf3 = galois_mul2(buf3);
  519. state[9] = state[9] ^ buf3 ^ buf1;
  520. buf3 = state[10] ^ state[11];
  521. buf3 = galois_mul2(buf3);
  522. state[10] = state[10] ^ buf3 ^ buf1;
  523. buf3 = state[11] ^ buf2;
  524. buf3 = galois_mul2(buf3);
  525. state[11] = state[11] ^ buf3 ^ buf1;
  526. /* Col4 */
  527. buf1 = state[12] ^ state[13] ^ state[14] ^ state[15];
  528. buf2 = state[12];
  529. buf3 = state[12] ^ state[13];
  530. buf3 = galois_mul2(buf3);
  531. state[12] = state[12] ^ buf3 ^ buf1;
  532. buf3 = state[13] ^ state[14];
  533. buf3 = galois_mul2(buf3);
  534. state[13] = state[13] ^ buf3 ^ buf1;
  535. buf3 = state[14] ^ state[15];
  536. buf3 = galois_mul2(buf3);
  537. state[14] = state[14] ^ buf3 ^ buf1;
  538. buf3 = state[15] ^ buf2;
  539. buf3 = galois_mul2(buf3);
  540. state[15] = state[15] ^ buf3 ^ buf1;
  541. /* addroundkey, rsbox and shiftrows */
  542. /* Row 0 */
  543. state[0] = g_rsbox[state[0]] ^ expanded_key[(round * 16)];
  544. state[4] = g_rsbox[state[4]] ^ expanded_key[(round * 16) + 4];
  545. state[8] = g_rsbox[state[8]] ^ expanded_key[(round * 16) + 8];
  546. state[12] = g_rsbox[state[12]] ^ expanded_key[(round * 16) + 12];
  547. /* Row 1 */
  548. buf1 = g_rsbox[state[13]] ^ expanded_key[(round * 16) + 1];
  549. state[13] = g_rsbox[state[9]] ^ expanded_key[(round * 16) + 13];
  550. state[9] = g_rsbox[state[5]] ^ expanded_key[(round * 16) + 9];
  551. state[5] = g_rsbox[state[1]] ^ expanded_key[(round * 16) + 5];
  552. state[1] = buf1;
  553. /* Row 2 */
  554. buf1 = g_rsbox[state[2]] ^ expanded_key[(round * 16) + 10];
  555. buf2 = g_rsbox[state[6]] ^ expanded_key[(round * 16) + 14];
  556. state[2] = g_rsbox[state[10]] ^ expanded_key[(round * 16) + 2];
  557. state[6] = g_rsbox[state[14]] ^ expanded_key[(round * 16) + 6];
  558. state[10] = buf1;
  559. state[14] = buf2;
  560. /* Row 3 */
  561. buf1 = g_rsbox[state[3]] ^ expanded_key[(round * 16) + 15];
  562. state[3] = g_rsbox[state[7]] ^ expanded_key[(round * 16) + 3];
  563. state[7] = g_rsbox[state[11]] ^ expanded_key[(round * 16) + 7];
  564. state[11] = g_rsbox[state[15]] ^ expanded_key[(round * 16) + 11];
  565. state[15] = buf1;
  566. }
  567. }
  568. /****************************************************************************
  569. * Public Functions
  570. ****************************************************************************/
  571. /****************************************************************************
  572. * Name: aes_setupkey
  573. *
  574. * Description:
  575. * Configure the given AES context for operation with the selected key.
  576. *
  577. * Input Parameters:
  578. * state an AES context that can be used for AES operations
  579. * key a pointer to a 16-byte buffer holding the AES-128 key
  580. * len length of the key, must be 16
  581. *
  582. * TODO: Support other key lengths of 24 (AES-192) and 32 (AES-256)
  583. *
  584. * Returned Value:
  585. * 0 if OK
  586. * -EINVAL if len is not 16
  587. *
  588. ****************************************************************************/
  589. int aes_setupkey(FAR struct aes_state_s *state,
  590. FAR const uint8_t *key,
  591. int len)
  592. {
  593. if (len != 16)
  594. {
  595. return -EINVAL;
  596. }
  597. expand_key(state->expanded_key, key);
  598. return 0;
  599. }
  600. /****************************************************************************
  601. * Name: aes_encipher
  602. *
  603. * Description:
  604. * Encipher some 16-byte blocks (without any operation mode) using the
  605. * previously defined key. The function can be called multiple times with
  606. * the same state parameter.
  607. *
  608. * Returned Value:
  609. * None
  610. *
  611. ****************************************************************************/
  612. void aes_encipher(FAR struct aes_state_s *state, FAR uint8_t *blocks,
  613. int nblk)
  614. {
  615. int i;
  616. uint32_t off = 0;
  617. for (i = 0; i < nblk; i++)
  618. {
  619. aes_encr(blocks + off, state->expanded_key);
  620. off += 16;
  621. }
  622. }
  623. /****************************************************************************
  624. * Name: aes_decipher
  625. *
  626. * Description:
  627. * Decipher some 16-byte blocks (without any operation mode) using the
  628. * previously defined key. The function can be called multiple times with
  629. * the same state parameter.
  630. *
  631. * Returned Value:
  632. * None
  633. *
  634. ****************************************************************************/
  635. void aes_decipher(FAR struct aes_state_s *state, FAR uint8_t *blocks,
  636. int nblk)
  637. {
  638. int i;
  639. uint32_t off = 0;
  640. for (i = 0; i < nblk; i++)
  641. {
  642. aes_decr(blocks + off, state->expanded_key);
  643. off += 16;
  644. }
  645. }
  646. /****************************************************************************
  647. * Name: aes_encrypt
  648. *
  649. * Description:
  650. * AES128 encryption: Given AES128 key and 16 bytes plain text, cipher
  651. * text of 16 bytes is computed. The AES implementation is in mode ECB
  652. * (Electronic Code Book).
  653. *
  654. * Input Parameters:
  655. * key AES128 key of size 16 bytes
  656. * state 16 bytes of plain text and cipher text
  657. *
  658. * Returned Value:
  659. * None
  660. *
  661. ****************************************************************************/
  662. void aes_encrypt(FAR uint8_t *state, FAR const uint8_t *key)
  663. {
  664. /* Expand the key into 176 bytes */
  665. aes_setupkey(&g_aes_state, key, 16);
  666. aes_encr(state, g_aes_state.expanded_key);
  667. }
  668. /****************************************************************************
  669. * Name: aes_decrypt
  670. *
  671. * Description:
  672. * AES128 decryption: Given AES128 key and 16 bytes cipher text, plain
  673. * text of 16 bytes is computed The AES implementation is in mode ECB
  674. * (Electronic Code Book).
  675. *
  676. * Input Parameters:
  677. * key AES128 key of size 16 bytes
  678. * state 16 bytes of plain text and cipher text
  679. *
  680. * Returned Value:
  681. * None
  682. *
  683. ****************************************************************************/
  684. void aes_decrypt(FAR uint8_t *state, FAR const uint8_t *key)
  685. {
  686. /* Expand the key into 176 bytes */
  687. aes_setupkey(&g_aes_state, key, 16);
  688. aes_decr(state, g_aes_state.expanded_key);
  689. }