Reference implementations of PQC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

664 lines
17 KiB

  1. /*
  2. * AES implementation based on code from BearSSL (https://bearssl.org/)
  3. * by Thomas Pornin.
  4. *
  5. *
  6. * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  23. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  24. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  25. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. */
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include "aes.h"
  31. static inline uint32_t br_dec32le(const unsigned char *src) {
  32. return (uint32_t)src[0]
  33. | ((uint32_t)src[1] << 8)
  34. | ((uint32_t)src[2] << 16)
  35. | ((uint32_t)src[3] << 24);
  36. }
  37. static void br_range_dec32le(uint32_t *v, size_t num, const unsigned char *src) {
  38. while (num-- > 0) {
  39. *v ++ = br_dec32le(src);
  40. src += 4;
  41. }
  42. }
  43. static inline uint32_t br_swap32(uint32_t x) {
  44. x = ((x & (uint32_t)0x00FF00FF) << 8)
  45. | ((x >> 8) & (uint32_t)0x00FF00FF);
  46. return (x << 16) | (x >> 16);
  47. }
  48. static inline void br_enc32le(unsigned char *dst, uint32_t x) {
  49. dst[0] = (unsigned char)x;
  50. dst[1] = (unsigned char)(x >> 8);
  51. dst[2] = (unsigned char)(x >> 16);
  52. dst[3] = (unsigned char)(x >> 24);
  53. }
  54. static void br_range_enc32le(unsigned char *dst, const uint32_t *v, size_t num) {
  55. while (num-- > 0) {
  56. br_enc32le(dst, *v ++);
  57. dst += 4;
  58. }
  59. }
  60. static void br_aes_ct64_bitslice_Sbox(uint64_t *q) {
  61. /*
  62. * This S-box implementation is a straightforward translation of
  63. * the circuit described by Boyar and Peralta in "A new
  64. * combinational logic minimization technique with applications
  65. * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
  66. *
  67. * Note that variables x* (input) and s* (output) are numbered
  68. * in "reverse" order (x0 is the high bit, x7 is the low bit).
  69. */
  70. uint64_t x0, x1, x2, x3, x4, x5, x6, x7;
  71. uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
  72. uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
  73. uint64_t y20, y21;
  74. uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
  75. uint64_t z10, z11, z12, z13, z14, z15, z16, z17;
  76. uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
  77. uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
  78. uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
  79. uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
  80. uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
  81. uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
  82. uint64_t t60, t61, t62, t63, t64, t65, t66, t67;
  83. uint64_t s0, s1, s2, s3, s4, s5, s6, s7;
  84. x0 = q[7];
  85. x1 = q[6];
  86. x2 = q[5];
  87. x3 = q[4];
  88. x4 = q[3];
  89. x5 = q[2];
  90. x6 = q[1];
  91. x7 = q[0];
  92. /*
  93. * Top linear transformation.
  94. */
  95. y14 = x3 ^ x5;
  96. y13 = x0 ^ x6;
  97. y9 = x0 ^ x3;
  98. y8 = x0 ^ x5;
  99. t0 = x1 ^ x2;
  100. y1 = t0 ^ x7;
  101. y4 = y1 ^ x3;
  102. y12 = y13 ^ y14;
  103. y2 = y1 ^ x0;
  104. y5 = y1 ^ x6;
  105. y3 = y5 ^ y8;
  106. t1 = x4 ^ y12;
  107. y15 = t1 ^ x5;
  108. y20 = t1 ^ x1;
  109. y6 = y15 ^ x7;
  110. y10 = y15 ^ t0;
  111. y11 = y20 ^ y9;
  112. y7 = x7 ^ y11;
  113. y17 = y10 ^ y11;
  114. y19 = y10 ^ y8;
  115. y16 = t0 ^ y11;
  116. y21 = y13 ^ y16;
  117. y18 = x0 ^ y16;
  118. /*
  119. * Non-linear section.
  120. */
  121. t2 = y12 & y15;
  122. t3 = y3 & y6;
  123. t4 = t3 ^ t2;
  124. t5 = y4 & x7;
  125. t6 = t5 ^ t2;
  126. t7 = y13 & y16;
  127. t8 = y5 & y1;
  128. t9 = t8 ^ t7;
  129. t10 = y2 & y7;
  130. t11 = t10 ^ t7;
  131. t12 = y9 & y11;
  132. t13 = y14 & y17;
  133. t14 = t13 ^ t12;
  134. t15 = y8 & y10;
  135. t16 = t15 ^ t12;
  136. t17 = t4 ^ t14;
  137. t18 = t6 ^ t16;
  138. t19 = t9 ^ t14;
  139. t20 = t11 ^ t16;
  140. t21 = t17 ^ y20;
  141. t22 = t18 ^ y19;
  142. t23 = t19 ^ y21;
  143. t24 = t20 ^ y18;
  144. t25 = t21 ^ t22;
  145. t26 = t21 & t23;
  146. t27 = t24 ^ t26;
  147. t28 = t25 & t27;
  148. t29 = t28 ^ t22;
  149. t30 = t23 ^ t24;
  150. t31 = t22 ^ t26;
  151. t32 = t31 & t30;
  152. t33 = t32 ^ t24;
  153. t34 = t23 ^ t33;
  154. t35 = t27 ^ t33;
  155. t36 = t24 & t35;
  156. t37 = t36 ^ t34;
  157. t38 = t27 ^ t36;
  158. t39 = t29 & t38;
  159. t40 = t25 ^ t39;
  160. t41 = t40 ^ t37;
  161. t42 = t29 ^ t33;
  162. t43 = t29 ^ t40;
  163. t44 = t33 ^ t37;
  164. t45 = t42 ^ t41;
  165. z0 = t44 & y15;
  166. z1 = t37 & y6;
  167. z2 = t33 & x7;
  168. z3 = t43 & y16;
  169. z4 = t40 & y1;
  170. z5 = t29 & y7;
  171. z6 = t42 & y11;
  172. z7 = t45 & y17;
  173. z8 = t41 & y10;
  174. z9 = t44 & y12;
  175. z10 = t37 & y3;
  176. z11 = t33 & y4;
  177. z12 = t43 & y13;
  178. z13 = t40 & y5;
  179. z14 = t29 & y2;
  180. z15 = t42 & y9;
  181. z16 = t45 & y14;
  182. z17 = t41 & y8;
  183. /*
  184. * Bottom linear transformation.
  185. */
  186. t46 = z15 ^ z16;
  187. t47 = z10 ^ z11;
  188. t48 = z5 ^ z13;
  189. t49 = z9 ^ z10;
  190. t50 = z2 ^ z12;
  191. t51 = z2 ^ z5;
  192. t52 = z7 ^ z8;
  193. t53 = z0 ^ z3;
  194. t54 = z6 ^ z7;
  195. t55 = z16 ^ z17;
  196. t56 = z12 ^ t48;
  197. t57 = t50 ^ t53;
  198. t58 = z4 ^ t46;
  199. t59 = z3 ^ t54;
  200. t60 = t46 ^ t57;
  201. t61 = z14 ^ t57;
  202. t62 = t52 ^ t58;
  203. t63 = t49 ^ t58;
  204. t64 = z4 ^ t59;
  205. t65 = t61 ^ t62;
  206. t66 = z1 ^ t63;
  207. s0 = t59 ^ t63;
  208. s6 = t56 ^ ~t62;
  209. s7 = t48 ^ ~t60;
  210. t67 = t64 ^ t65;
  211. s3 = t53 ^ t66;
  212. s4 = t51 ^ t66;
  213. s5 = t47 ^ t65;
  214. s1 = t64 ^ ~s3;
  215. s2 = t55 ^ ~t67;
  216. q[7] = s0;
  217. q[6] = s1;
  218. q[5] = s2;
  219. q[4] = s3;
  220. q[3] = s4;
  221. q[2] = s5;
  222. q[1] = s6;
  223. q[0] = s7;
  224. }
  225. static void br_aes_ct64_ortho(uint64_t *q) {
  226. #define SWAPN(cl, ch, s, x, y) do { \
  227. uint64_t a, b; \
  228. a = (x); \
  229. b = (y); \
  230. (x) = (a & (uint64_t)(cl)) | ((b & (uint64_t)(cl)) << (s)); \
  231. (y) = ((a & (uint64_t)(ch)) >> (s)) | (b & (uint64_t)(ch)); \
  232. } while (0)
  233. #define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y)
  234. #define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y)
  235. #define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 4, x, y)
  236. SWAP2(q[0], q[1]);
  237. SWAP2(q[2], q[3]);
  238. SWAP2(q[4], q[5]);
  239. SWAP2(q[6], q[7]);
  240. SWAP4(q[0], q[2]);
  241. SWAP4(q[1], q[3]);
  242. SWAP4(q[4], q[6]);
  243. SWAP4(q[5], q[7]);
  244. SWAP8(q[0], q[4]);
  245. SWAP8(q[1], q[5]);
  246. SWAP8(q[2], q[6]);
  247. SWAP8(q[3], q[7]);
  248. }
  249. static void br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w) {
  250. uint64_t x0, x1, x2, x3;
  251. x0 = w[0];
  252. x1 = w[1];
  253. x2 = w[2];
  254. x3 = w[3];
  255. x0 |= (x0 << 16);
  256. x1 |= (x1 << 16);
  257. x2 |= (x2 << 16);
  258. x3 |= (x3 << 16);
  259. x0 &= (uint64_t)0x0000FFFF0000FFFF;
  260. x1 &= (uint64_t)0x0000FFFF0000FFFF;
  261. x2 &= (uint64_t)0x0000FFFF0000FFFF;
  262. x3 &= (uint64_t)0x0000FFFF0000FFFF;
  263. x0 |= (x0 << 8);
  264. x1 |= (x1 << 8);
  265. x2 |= (x2 << 8);
  266. x3 |= (x3 << 8);
  267. x0 &= (uint64_t)0x00FF00FF00FF00FF;
  268. x1 &= (uint64_t)0x00FF00FF00FF00FF;
  269. x2 &= (uint64_t)0x00FF00FF00FF00FF;
  270. x3 &= (uint64_t)0x00FF00FF00FF00FF;
  271. *q0 = x0 | (x2 << 8);
  272. *q1 = x1 | (x3 << 8);
  273. }
  274. static void br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1) {
  275. uint64_t x0, x1, x2, x3;
  276. x0 = q0 & (uint64_t)0x00FF00FF00FF00FF;
  277. x1 = q1 & (uint64_t)0x00FF00FF00FF00FF;
  278. x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
  279. x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
  280. x0 |= (x0 >> 8);
  281. x1 |= (x1 >> 8);
  282. x2 |= (x2 >> 8);
  283. x3 |= (x3 >> 8);
  284. x0 &= (uint64_t)0x0000FFFF0000FFFF;
  285. x1 &= (uint64_t)0x0000FFFF0000FFFF;
  286. x2 &= (uint64_t)0x0000FFFF0000FFFF;
  287. x3 &= (uint64_t)0x0000FFFF0000FFFF;
  288. w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16);
  289. w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16);
  290. w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16);
  291. w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16);
  292. }
  293. static const unsigned char Rcon[] = {
  294. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36
  295. };
  296. static uint32_t sub_word(uint32_t x) {
  297. uint64_t q[8];
  298. memset(q, 0, sizeof q);
  299. q[0] = x;
  300. br_aes_ct64_ortho(q);
  301. br_aes_ct64_bitslice_Sbox(q);
  302. br_aes_ct64_ortho(q);
  303. return (uint32_t)q[0];
  304. }
  305. static void br_aes_ct64_keysched(uint64_t *comp_skey, const unsigned char *key, unsigned int key_len) {
  306. unsigned int i, j, k, nk, nkf;
  307. uint32_t tmp;
  308. uint32_t skey[60];
  309. unsigned nrounds = 10 + ((key_len - 16) >> 2);
  310. nk = (key_len >> 2);
  311. nkf = ((nrounds + 1) << 2);
  312. br_range_dec32le(skey, (key_len >> 2), key);
  313. tmp = skey[(key_len >> 2) - 1];
  314. for (i = nk, j = 0, k = 0; i < nkf; i ++) {
  315. if (j == 0) {
  316. tmp = (tmp << 24) | (tmp >> 8);
  317. tmp = sub_word(tmp) ^ Rcon[k];
  318. } else if (nk > 6 && j == 4) {
  319. tmp = sub_word(tmp);
  320. }
  321. tmp ^= skey[i - nk];
  322. skey[i] = tmp;
  323. if (++ j == nk) {
  324. j = 0;
  325. k ++;
  326. }
  327. }
  328. for (i = 0, j = 0; i < nkf; i += 4, j += 2) {
  329. uint64_t q[8];
  330. br_aes_ct64_interleave_in(&q[0], &q[4], skey + i);
  331. q[1] = q[0];
  332. q[2] = q[0];
  333. q[3] = q[0];
  334. q[5] = q[4];
  335. q[6] = q[4];
  336. q[7] = q[4];
  337. br_aes_ct64_ortho(q);
  338. comp_skey[j + 0] =
  339. (q[0] & (uint64_t)0x1111111111111111)
  340. | (q[1] & (uint64_t)0x2222222222222222)
  341. | (q[2] & (uint64_t)0x4444444444444444)
  342. | (q[3] & (uint64_t)0x8888888888888888);
  343. comp_skey[j + 1] =
  344. (q[4] & (uint64_t)0x1111111111111111)
  345. | (q[5] & (uint64_t)0x2222222222222222)
  346. | (q[6] & (uint64_t)0x4444444444444444)
  347. | (q[7] & (uint64_t)0x8888888888888888);
  348. }
  349. }
  350. static void br_aes_ct64_skey_expand(uint64_t *skey, const uint64_t *comp_skey, unsigned int nrounds) {
  351. unsigned u, v, n;
  352. n = (nrounds + 1) << 1;
  353. for (u = 0, v = 0; u < n; u ++, v += 4) {
  354. uint64_t x0, x1, x2, x3;
  355. x0 = x1 = x2 = x3 = comp_skey[u];
  356. x0 &= (uint64_t)0x1111111111111111;
  357. x1 &= (uint64_t)0x2222222222222222;
  358. x2 &= (uint64_t)0x4444444444444444;
  359. x3 &= (uint64_t)0x8888888888888888;
  360. x1 >>= 1;
  361. x2 >>= 2;
  362. x3 >>= 3;
  363. skey[v + 0] = (x0 << 4) - x0;
  364. skey[v + 1] = (x1 << 4) - x1;
  365. skey[v + 2] = (x2 << 4) - x2;
  366. skey[v + 3] = (x3 << 4) - x3;
  367. }
  368. }
  369. static inline void add_round_key(uint64_t *q, const uint64_t *sk) {
  370. q[0] ^= sk[0];
  371. q[1] ^= sk[1];
  372. q[2] ^= sk[2];
  373. q[3] ^= sk[3];
  374. q[4] ^= sk[4];
  375. q[5] ^= sk[5];
  376. q[6] ^= sk[6];
  377. q[7] ^= sk[7];
  378. }
  379. static inline void shift_rows(uint64_t *q) {
  380. int i;
  381. for (i = 0; i < 8; i ++) {
  382. uint64_t x;
  383. x = q[i];
  384. q[i] = (x & (uint64_t)0x000000000000FFFF)
  385. | ((x & (uint64_t)0x00000000FFF00000) >> 4)
  386. | ((x & (uint64_t)0x00000000000F0000) << 12)
  387. | ((x & (uint64_t)0x0000FF0000000000) >> 8)
  388. | ((x & (uint64_t)0x000000FF00000000) << 8)
  389. | ((x & (uint64_t)0xF000000000000000) >> 12)
  390. | ((x & (uint64_t)0x0FFF000000000000) << 4);
  391. }
  392. }
  393. static inline uint64_t rotr32(uint64_t x) {
  394. return (x << 32) | (x >> 32);
  395. }
  396. static inline void mix_columns(uint64_t *q) {
  397. uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
  398. uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
  399. q0 = q[0];
  400. q1 = q[1];
  401. q2 = q[2];
  402. q3 = q[3];
  403. q4 = q[4];
  404. q5 = q[5];
  405. q6 = q[6];
  406. q7 = q[7];
  407. r0 = (q0 >> 16) | (q0 << 48);
  408. r1 = (q1 >> 16) | (q1 << 48);
  409. r2 = (q2 >> 16) | (q2 << 48);
  410. r3 = (q3 >> 16) | (q3 << 48);
  411. r4 = (q4 >> 16) | (q4 << 48);
  412. r5 = (q5 >> 16) | (q5 << 48);
  413. r6 = (q6 >> 16) | (q6 << 48);
  414. r7 = (q7 >> 16) | (q7 << 48);
  415. q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0);
  416. q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1);
  417. q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2);
  418. q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3);
  419. q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4);
  420. q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5);
  421. q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6);
  422. q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
  423. }
  424. static void inc4_be(uint32_t *x) {
  425. uint32_t t = br_swap32(*x) + 4;
  426. *x = br_swap32(t);
  427. }
  428. static void aes_ecb4x(unsigned char out[64], const uint32_t ivw[16], const uint64_t *sk_exp, unsigned int nrounds) {
  429. uint32_t w[16];
  430. uint64_t q[8];
  431. unsigned int i;
  432. memcpy(w, ivw, sizeof(w));
  433. for (i = 0; i < 4; i++) {
  434. br_aes_ct64_interleave_in(&q[i], &q[i + 4], w + (i << 2));
  435. }
  436. br_aes_ct64_ortho(q);
  437. add_round_key(q, sk_exp);
  438. for (i = 1; i < nrounds; i++) {
  439. br_aes_ct64_bitslice_Sbox(q);
  440. shift_rows(q);
  441. mix_columns(q);
  442. add_round_key(q, sk_exp + (i << 3));
  443. }
  444. br_aes_ct64_bitslice_Sbox(q);
  445. shift_rows(q);
  446. add_round_key(q, sk_exp + 8 * nrounds);
  447. br_aes_ct64_ortho(q);
  448. for (i = 0; i < 4; i ++) {
  449. br_aes_ct64_interleave_out(w + (i << 2), q[i], q[i + 4]);
  450. }
  451. br_range_enc32le(out, w, 16);
  452. }
  453. static void aes_ctr4x(unsigned char out[64], uint32_t ivw[16], const uint64_t *sk_exp, unsigned int nrounds) {
  454. aes_ecb4x(out, ivw, sk_exp, nrounds);
  455. /* Increase counter for next 4 blocks */
  456. inc4_be(ivw + 3);
  457. inc4_be(ivw + 7);
  458. inc4_be(ivw + 11);
  459. inc4_be(ivw + 15);
  460. }
  461. static void aes_ecb(unsigned char *out, const unsigned char *in, size_t nblocks, const uint64_t *rkeys, unsigned int nrounds) {
  462. uint32_t blocks[16];
  463. unsigned char t[64];
  464. while (nblocks >= 4) {
  465. br_range_dec32le(blocks, 16, in);
  466. aes_ecb4x(out, blocks, rkeys, nrounds);
  467. nblocks -= 4;
  468. in += 64;
  469. out += 64;
  470. }
  471. if (nblocks) {
  472. br_range_dec32le(blocks, nblocks * 4, in);
  473. aes_ecb4x(t, blocks, rkeys, nrounds);
  474. memcpy(out, t, nblocks * 16);
  475. }
  476. }
  477. static void aes_ctr(unsigned char *out, size_t outlen, const unsigned char *iv, const uint64_t *rkeys, unsigned int nrounds) {
  478. uint32_t ivw[16];
  479. size_t i;
  480. uint32_t cc = 0;
  481. br_range_dec32le(ivw, 3, iv);
  482. memcpy(ivw + 4, ivw, 3 * sizeof(uint32_t));
  483. memcpy(ivw + 8, ivw, 3 * sizeof(uint32_t));
  484. memcpy(ivw + 12, ivw, 3 * sizeof(uint32_t));
  485. ivw[ 3] = br_swap32(cc);
  486. ivw[ 7] = br_swap32(cc + 1);
  487. ivw[11] = br_swap32(cc + 2);
  488. ivw[15] = br_swap32(cc + 3);
  489. while (outlen > 64) {
  490. aes_ctr4x(out, ivw, rkeys, nrounds);
  491. out += 64;
  492. outlen -= 64;
  493. }
  494. if (outlen > 0) {
  495. unsigned char tmp[64];
  496. aes_ctr4x(tmp, ivw, rkeys, nrounds);
  497. for (i = 0; i < outlen; i++) {
  498. out[i] = tmp[i];
  499. }
  500. }
  501. }
  502. void aes128_ecb_keyexp(aes128ctx *r, const unsigned char *key) {
  503. uint64_t skey[22];
  504. r->sk_exp = malloc(sizeof(uint64_t) * PQC_AES128_STATESIZE);
  505. if (r->sk_exp == NULL) {
  506. exit(111);
  507. }
  508. br_aes_ct64_keysched(skey, key, 16);
  509. br_aes_ct64_skey_expand(r->sk_exp, skey, 10);
  510. }
  511. void aes128_ctr_keyexp(aes128ctx *r, const unsigned char *key) {
  512. aes128_ecb_keyexp(r, key);
  513. }
  514. void aes192_ecb_keyexp(aes192ctx *r, const unsigned char *key) {
  515. uint64_t skey[26];
  516. r->sk_exp = malloc(sizeof(uint64_t) * PQC_AES192_STATESIZE);
  517. if (r->sk_exp == NULL) {
  518. exit(111);
  519. }
  520. br_aes_ct64_keysched(skey, key, 24);
  521. br_aes_ct64_skey_expand(r->sk_exp, skey, 12);
  522. }
  523. void aes192_ctr_keyexp(aes192ctx *r, const unsigned char *key) {
  524. aes192_ecb_keyexp(r, key);
  525. }
  526. void aes256_ecb_keyexp(aes256ctx *r, const unsigned char *key) {
  527. uint64_t skey[30];
  528. r->sk_exp = malloc(sizeof(uint64_t) * PQC_AES256_STATESIZE);
  529. if (r->sk_exp == NULL) {
  530. exit(111);
  531. }
  532. br_aes_ct64_keysched(skey, key, 32);
  533. br_aes_ct64_skey_expand(r->sk_exp, skey, 14);
  534. }
  535. void aes256_ctr_keyexp(aes256ctx *r, const unsigned char *key) {
  536. aes256_ecb_keyexp(r, key);
  537. }
  538. void aes128_ecb(unsigned char *out, const unsigned char *in, size_t nblocks, const aes128ctx *ctx) {
  539. aes_ecb(out, in, nblocks, ctx->sk_exp, 10);
  540. }
  541. void aes128_ctr(unsigned char *out, size_t outlen, const unsigned char *iv, const aes128ctx *ctx) {
  542. aes_ctr(out, outlen, iv, ctx->sk_exp, 10);
  543. }
  544. void aes192_ecb(unsigned char *out, const unsigned char *in, size_t nblocks, const aes192ctx *ctx) {
  545. aes_ecb(out, in, nblocks, ctx->sk_exp, 12);
  546. }
  547. void aes192_ctr(unsigned char *out, size_t outlen, const unsigned char *iv, const aes192ctx *ctx) {
  548. aes_ctr(out, outlen, iv, ctx->sk_exp, 12);
  549. }
  550. void aes256_ecb(unsigned char *out, const unsigned char *in, size_t nblocks, const aes256ctx *ctx) {
  551. aes_ecb(out, in, nblocks, ctx->sk_exp, 14);
  552. }
  553. void aes256_ctr(unsigned char *out, size_t outlen, const unsigned char *iv, const aes256ctx *ctx) {
  554. aes_ctr(out, outlen, iv, ctx->sk_exp, 14);
  555. }
  556. void aes128_ctx_release(aes128ctx *r) {
  557. free(r->sk_exp);
  558. }
  559. void aes192_ctx_release(aes192ctx *r) {
  560. free(r->sk_exp);
  561. }
  562. void aes256_ctx_release(aes256ctx *r) {
  563. free(r->sk_exp);
  564. }