cifra_eax_aes.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include "handy.h"
  17. #include "cf_config.h"
  18. #include "cifra_eax_aes.h"
  19. #include "bitops.h"
  20. #include "tassert.h"
  21. static const uint8_t S[256] =
  22. {
  23. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe,
  24. 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4,
  25. 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7,
  26. 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3,
  27. 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09,
  28. 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3,
  29. 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe,
  30. 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  31. 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92,
  32. 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c,
  33. 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19,
  34. 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
  35. 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2,
  36. 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5,
  37. 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25,
  38. 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  39. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86,
  40. 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e,
  41. 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42,
  42. 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  43. };
  44. static const uint8_t Rcon[11] =
  45. {
  46. 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
  47. };
  48. #ifdef INLINE_FUNCS
  49. static inline uint32_t word4(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3)
  50. {
  51. return b0 << 24 | b1 << 16 | b2 << 8 | b3;
  52. }
  53. static inline uint8_t byte(uint32_t w, unsigned x)
  54. {
  55. /* nb. bytes are numbered 0 (leftmost, top)
  56. * to 3 (rightmost). */
  57. x = 3 - x;
  58. return (w >> (x * 8)) & 0xff;
  59. }
  60. static uint32_t round_constant(uint32_t i)
  61. {
  62. return Rcon[i] << 24;
  63. }
  64. static uint32_t rot_word(uint32_t w)
  65. {
  66. /* Takes
  67. * word [a0,a1,a2,a3]
  68. * returns
  69. * word [a1,a2,a3,a0]
  70. *
  71. */
  72. return rotl32(w, 8);
  73. }
  74. #endif
  75. #define word4(a, b, c, d) (((uint32_t)(a) << 24) | ((uint32_t)(b) << 16) | ((uint32_t)(c) << 8) | (d))
  76. #define byte(w, x) ((w >> ((3 - (x)) << 3)) & 0xff)
  77. #define round_constant(i) ((uint32_t)(Rcon[i]) << 24)
  78. #define rot_word(w) rotl32((w), 8)
  79. static uint32_t sub_word(uint32_t w, const uint8_t *sbox)
  80. {
  81. uint8_t a = byte(w, 0),
  82. b = byte(w, 1),
  83. c = byte(w, 2),
  84. d = byte(w, 3);
  85. #if CF_CACHE_SIDE_CHANNEL_PROTECTION
  86. select_u8x4(&a, &b, &c, &d, sbox, 256);
  87. #else
  88. a = sbox[a];
  89. b = sbox[b];
  90. c = sbox[c];
  91. d = sbox[d];
  92. #endif
  93. return word4(a, b, c, d);
  94. }
  95. static void aes_schedule(cf_aes_context *ctx, const uint8_t *key, size_t nkey)
  96. {
  97. size_t i,
  98. nb = AES_BLOCKSZ / 4,
  99. nk = nkey / 4,
  100. n = nb * (ctx->rounds + 1);
  101. uint32_t *w = ctx->ks;
  102. /* First words are just the key. */
  103. for (i = 0; i < nk; i++)
  104. {
  105. w[i] = read32_be(key + i * 4);
  106. }
  107. uint32_t i_div_nk = 1;
  108. uint32_t i_mod_nk = 0;
  109. for (; i < n; i++, i_mod_nk++)
  110. {
  111. uint32_t temp = w[i - 1];
  112. if (i_mod_nk == nk)
  113. {
  114. i_div_nk++;
  115. i_mod_nk = 0;
  116. }
  117. if (i_mod_nk == 0)
  118. temp = sub_word(rot_word(temp), S) ^ round_constant(i_div_nk);
  119. else if (nk > 6 && i_mod_nk == 4)
  120. temp = sub_word(temp, S);
  121. w[i] = w[i - nk] ^ temp;
  122. }
  123. }
  124. void cf_aes_init(cf_aes_context *ctx, const uint8_t *key, size_t nkey)
  125. {
  126. memset(ctx, 0, sizeof *ctx);
  127. switch (nkey)
  128. {
  129. #if CF_AES_MAXROUNDS >= AES128_ROUNDS
  130. case 16:
  131. ctx->rounds = AES128_ROUNDS;
  132. aes_schedule(ctx, key, nkey);
  133. break;
  134. #endif
  135. #if CF_AES_MAXROUNDS >= AES192_ROUNDS
  136. case 24:
  137. ctx->rounds = AES192_ROUNDS;
  138. aes_schedule(ctx, key, nkey);
  139. break;
  140. #endif
  141. #if CF_AES_MAXROUNDS >= AES256_ROUNDS
  142. case 32:
  143. ctx->rounds = AES256_ROUNDS;
  144. aes_schedule(ctx, key, nkey);
  145. break;
  146. #endif
  147. default:
  148. abort();
  149. }
  150. }
  151. static void add_round_key(uint32_t state[4], const uint32_t rk[4])
  152. {
  153. state[0] ^= rk[0];
  154. state[1] ^= rk[1];
  155. state[2] ^= rk[2];
  156. state[3] ^= rk[3];
  157. }
  158. static void sub_block(uint32_t state[4])
  159. {
  160. state[0] = sub_word(state[0], S);
  161. state[1] = sub_word(state[1], S);
  162. state[2] = sub_word(state[2], S);
  163. state[3] = sub_word(state[3], S);
  164. }
  165. static void shift_rows(uint32_t state[4])
  166. {
  167. uint32_t u, v, x, y;
  168. u = word4(byte(state[0], 0),
  169. byte(state[1], 1),
  170. byte(state[2], 2),
  171. byte(state[3], 3));
  172. v = word4(byte(state[1], 0),
  173. byte(state[2], 1),
  174. byte(state[3], 2),
  175. byte(state[0], 3));
  176. x = word4(byte(state[2], 0),
  177. byte(state[3], 1),
  178. byte(state[0], 2),
  179. byte(state[1], 3));
  180. y = word4(byte(state[3], 0),
  181. byte(state[0], 1),
  182. byte(state[1], 2),
  183. byte(state[2], 3));
  184. state[0] = u;
  185. state[1] = v;
  186. state[2] = x;
  187. state[3] = y;
  188. }
  189. static uint32_t gf_poly_mul2(uint32_t x)
  190. {
  191. return
  192. ((x & 0x7f7f7f7f) << 1) ^
  193. (((x & 0x80808080) >> 7) * 0x1b);
  194. }
  195. static uint32_t mix_column(uint32_t x)
  196. {
  197. uint32_t x2 = gf_poly_mul2(x);
  198. return x2 ^ rotr32(x ^ x2, 24) ^ rotr32(x, 16) ^ rotr32(x, 8);
  199. }
  200. static void mix_columns(uint32_t state[4])
  201. {
  202. state[0] = mix_column(state[0]);
  203. state[1] = mix_column(state[1]);
  204. state[2] = mix_column(state[2]);
  205. state[3] = mix_column(state[3]);
  206. }
  207. void cf_aes_encrypt(const cf_aes_context *ctx,
  208. const uint8_t in[AES_BLOCKSZ],
  209. uint8_t out[AES_BLOCKSZ])
  210. {
  211. assert(ctx->rounds == AES128_ROUNDS ||
  212. ctx->rounds == AES192_ROUNDS ||
  213. ctx->rounds == AES256_ROUNDS);
  214. uint32_t state[4] = {
  215. read32_be(in + 0),
  216. read32_be(in + 4),
  217. read32_be(in + 8),
  218. read32_be(in + 12)
  219. };
  220. const uint32_t *round_keys = ctx->ks;
  221. add_round_key(state, round_keys);
  222. round_keys += 4;
  223. for (uint32_t round = 1; round < ctx->rounds; round++)
  224. {
  225. sub_block(state);
  226. shift_rows(state);
  227. mix_columns(state);
  228. add_round_key(state, round_keys);
  229. round_keys += 4;
  230. }
  231. sub_block(state);
  232. shift_rows(state);
  233. add_round_key(state, round_keys);
  234. write32_be(state[0], out + 0);
  235. write32_be(state[1], out + 4);
  236. write32_be(state[2], out + 8);
  237. write32_be(state[3], out + 12);
  238. }
  239. #if CF_AES_ENCRYPT_ONLY == 0
  240. static const uint8_t S_inv[256] =
  241. {
  242. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81,
  243. 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e,
  244. 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23,
  245. 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 0x08, 0x2e, 0xa1, 0x66,
  246. 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 0x72,
  247. 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65,
  248. 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46,
  249. 0x57, 0xa7, 0x8d, 0x9d, 0x84, 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a,
  250. 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca,
  251. 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91,
  252. 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6,
  253. 0x73, 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
  254. 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f,
  255. 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2,
  256. 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 0x1f, 0xdd, 0xa8,
  257. 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  258. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93,
  259. 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb,
  260. 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6,
  261. 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  262. };
  263. static void inv_sub_block(uint32_t state[4])
  264. {
  265. state[0] = sub_word(state[0], S_inv);
  266. state[1] = sub_word(state[1], S_inv);
  267. state[2] = sub_word(state[2], S_inv);
  268. state[3] = sub_word(state[3], S_inv);
  269. }
  270. static void inv_shift_rows(uint32_t state[4])
  271. {
  272. uint32_t u, v, x, y;
  273. u = word4(byte(state[0], 0),
  274. byte(state[3], 1),
  275. byte(state[2], 2),
  276. byte(state[1], 3));
  277. v = word4(byte(state[1], 0),
  278. byte(state[0], 1),
  279. byte(state[3], 2),
  280. byte(state[2], 3));
  281. x = word4(byte(state[2], 0),
  282. byte(state[1], 1),
  283. byte(state[0], 2),
  284. byte(state[3], 3));
  285. y = word4(byte(state[3], 0),
  286. byte(state[2], 1),
  287. byte(state[1], 2),
  288. byte(state[0], 3));
  289. state[0] = u;
  290. state[1] = v;
  291. state[2] = x;
  292. state[3] = y;
  293. }
  294. static uint32_t inv_mix_column(uint32_t x)
  295. {
  296. uint32_t x2 = gf_poly_mul2(x),
  297. x4 = gf_poly_mul2(x2),
  298. x9 = x ^ gf_poly_mul2(x4),
  299. x11 = x2 ^ x9,
  300. x13 = x4 ^ x9;
  301. return x ^ x2 ^ x13 ^ rotr32(x11, 24) ^ rotr32(x13, 16) ^ rotr32(x9, 8);
  302. }
  303. static void inv_mix_columns(uint32_t state[4])
  304. {
  305. state[0] = inv_mix_column(state[0]);
  306. state[1] = inv_mix_column(state[1]);
  307. state[2] = inv_mix_column(state[2]);
  308. state[3] = inv_mix_column(state[3]);
  309. }
  310. void cf_aes_decrypt(const cf_aes_context *ctx,
  311. const uint8_t in[AES_BLOCKSZ],
  312. uint8_t out[AES_BLOCKSZ])
  313. {
  314. assert(ctx->rounds == AES128_ROUNDS ||
  315. ctx->rounds == AES192_ROUNDS ||
  316. ctx->rounds == AES256_ROUNDS);
  317. uint32_t state[4] = {
  318. read32_be(in + 0),
  319. read32_be(in + 4),
  320. read32_be(in + 8),
  321. read32_be(in + 12)
  322. };
  323. const uint32_t *round_keys = &ctx->ks[ctx->rounds << 2];
  324. add_round_key(state, round_keys);
  325. round_keys -= 4;
  326. for (uint32_t round = ctx->rounds - 1; round != 0; round--)
  327. {
  328. inv_shift_rows(state);
  329. inv_sub_block(state);
  330. add_round_key(state, round_keys);
  331. inv_mix_columns(state);
  332. round_keys -= 4;
  333. }
  334. inv_shift_rows(state);
  335. inv_sub_block(state);
  336. add_round_key(state, round_keys);
  337. write32_be(state[0], out + 0);
  338. write32_be(state[1], out + 4);
  339. write32_be(state[2], out + 8);
  340. write32_be(state[3], out + 12);
  341. }
  342. #else
  343. void cf_aes_decrypt(const cf_aes_context *ctx,
  344. const uint8_t in[AES_BLOCKSZ],
  345. uint8_t out[AES_BLOCKSZ])
  346. {
  347. abort();
  348. }
  349. #endif
  350. void cf_aes_finish(cf_aes_context *ctx)
  351. {
  352. mem_clean(ctx, sizeof *ctx);
  353. }
  354. const cf_prp cf_aes = {
  355. .blocksz = AES_BLOCKSZ,
  356. .encrypt = (cf_prp_block) cf_aes_encrypt,
  357. .decrypt = (cf_prp_block) cf_aes_decrypt
  358. };