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.

964 lines
27 KiB

  1. /*
  2. * Constant time implementation of the Haraka hash function.
  3. *
  4. * The bit-sliced implementation of the AES round functions are
  5. * based on the AES implementation in BearSSL written
  6. * by Thomas Pornin <pornin@bolet.org>
  7. */
  8. #include <stddef.h>
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "haraka.h"
  13. #define HARAKAS_RATE 32
  14. static const uint64_t haraka512_rc64[10][8] = {
  15. {0x24cf0ab9086f628b, 0xbdd6eeecc83b8382, 0xd96fb0306cdad0a7, 0xaace082ac8f95f89, 0x449d8e8870d7041f, 0x49bb2f80b2b3e2f8, 0x0569ae98d93bb258, 0x23dc9691e7d6a4b1},
  16. {0xd8ba10ede0fe5b6e, 0x7ecf7dbe424c7b8e, 0x6ea9949c6df62a31, 0xbf3f3c97ec9c313e, 0x241d03a196a1861e, 0xead3a51116e5a2ea, 0x77d479fcad9574e3, 0x18657a1af894b7a0},
  17. {0x10671e1a7f595522, 0xd9a00ff675d28c7b, 0x2f1edf0d2b9ba661, 0xb8ff58b8e3de45f9, 0xee29261da9865c02, 0xd1532aa4b50bdf43, 0x8bf858159b231bb1, 0xdf17439d22d4f599},
  18. {0xdd4b2f0870b918c0, 0x757a81f3b39b1bb6, 0x7a5c556898952e3f, 0x7dd70a16d915d87a, 0x3ae61971982b8301, 0xc3ab319e030412be, 0x17c0033ac094a8cb, 0x5a0630fc1a8dc4ef},
  19. {0x17708988c1632f73, 0xf92ddae090b44f4f, 0x11ac0285c43aa314, 0x509059941936b8ba, 0xd03e152fa2ce9b69, 0x3fbcbcb63a32998b, 0x6204696d692254f7, 0x915542ed93ec59b4},
  20. {0xf4ed94aa8879236e, 0xff6cb41cd38e03c0, 0x069b38602368aeab, 0x669495b820f0ddba, 0xf42013b1b8bf9e3d, 0xcf935efe6439734d, 0xbc1dcf42ca29e3f8, 0x7e6d3ed29f78ad67},
  21. {0xf3b0f6837ffcddaa, 0x3a76faef934ddf41, 0xcec7ae583a9c8e35, 0xe4dd18c68f0260af, 0x2c0e5df1ad398eaa, 0x478df5236ae22e8c, 0xfb944c46fe865f39, 0xaa48f82f028132ba},
  22. {0x231b9ae2b76aca77, 0x292a76a712db0b40, 0x5850625dc8134491, 0x73137dd469810fb5, 0x8a12a6a202a474fd, 0xd36fd9daa78bdb80, 0xb34c5e733505706f, 0xbaf1cdca818d9d96},
  23. {0x2e99781335e8c641, 0xbddfe5cce47d560e, 0xf74e9bf32e5e040c, 0x1d7a709d65996be9, 0x670df36a9cf66cdd, 0xd05ef84a176a2875, 0x0f888e828cb1c44e, 0x1a79e9c9727b052c},
  24. {0x83497348628d84de, 0x2e9387d51f22a754, 0xb000068da2f852d6, 0x378c9e1190fd6fe5, 0x870027c316de7293, 0xe51a9d4462e047bb, 0x90ecf7f8c6251195, 0x655953bfbed90a9c},
  25. };
  26. static inline uint32_t br_dec32le(const unsigned char *src) {
  27. return (uint32_t)src[0]
  28. | ((uint32_t)src[1] << 8)
  29. | ((uint32_t)src[2] << 16)
  30. | ((uint32_t)src[3] << 24);
  31. }
  32. static void br_range_dec32le(uint32_t *v, size_t num, const unsigned char *src) {
  33. while (num-- > 0) {
  34. *v ++ = br_dec32le(src);
  35. src += 4;
  36. }
  37. }
  38. static inline void br_enc32le(unsigned char *dst, uint32_t x) {
  39. dst[0] = (unsigned char)x;
  40. dst[1] = (unsigned char)(x >> 8);
  41. dst[2] = (unsigned char)(x >> 16);
  42. dst[3] = (unsigned char)(x >> 24);
  43. }
  44. static void br_range_enc32le(unsigned char *dst, const uint32_t *v, size_t num) {
  45. while (num-- > 0) {
  46. br_enc32le(dst, *v ++);
  47. dst += 4;
  48. }
  49. }
  50. static void br_aes_ct64_bitslice_Sbox(uint64_t *q) {
  51. /*
  52. * This S-box implementation is a straightforward translation of
  53. * the circuit described by Boyar and Peralta in "A new
  54. * combinational logic minimization technique with applications
  55. * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
  56. *
  57. * Note that variables x* (input) and s* (output) are numbered
  58. * in "reverse" order (x0 is the high bit, x7 is the low bit).
  59. */
  60. uint64_t x0, x1, x2, x3, x4, x5, x6, x7;
  61. uint64_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
  62. uint64_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
  63. uint64_t y20, y21;
  64. uint64_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
  65. uint64_t z10, z11, z12, z13, z14, z15, z16, z17;
  66. uint64_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
  67. uint64_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
  68. uint64_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
  69. uint64_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
  70. uint64_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
  71. uint64_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
  72. uint64_t t60, t61, t62, t63, t64, t65, t66, t67;
  73. uint64_t s0, s1, s2, s3, s4, s5, s6, s7;
  74. x0 = q[7];
  75. x1 = q[6];
  76. x2 = q[5];
  77. x3 = q[4];
  78. x4 = q[3];
  79. x5 = q[2];
  80. x6 = q[1];
  81. x7 = q[0];
  82. /*
  83. * Top linear transformation.
  84. */
  85. y14 = x3 ^ x5;
  86. y13 = x0 ^ x6;
  87. y9 = x0 ^ x3;
  88. y8 = x0 ^ x5;
  89. t0 = x1 ^ x2;
  90. y1 = t0 ^ x7;
  91. y4 = y1 ^ x3;
  92. y12 = y13 ^ y14;
  93. y2 = y1 ^ x0;
  94. y5 = y1 ^ x6;
  95. y3 = y5 ^ y8;
  96. t1 = x4 ^ y12;
  97. y15 = t1 ^ x5;
  98. y20 = t1 ^ x1;
  99. y6 = y15 ^ x7;
  100. y10 = y15 ^ t0;
  101. y11 = y20 ^ y9;
  102. y7 = x7 ^ y11;
  103. y17 = y10 ^ y11;
  104. y19 = y10 ^ y8;
  105. y16 = t0 ^ y11;
  106. y21 = y13 ^ y16;
  107. y18 = x0 ^ y16;
  108. /*
  109. * Non-linear section.
  110. */
  111. t2 = y12 & y15;
  112. t3 = y3 & y6;
  113. t4 = t3 ^ t2;
  114. t5 = y4 & x7;
  115. t6 = t5 ^ t2;
  116. t7 = y13 & y16;
  117. t8 = y5 & y1;
  118. t9 = t8 ^ t7;
  119. t10 = y2 & y7;
  120. t11 = t10 ^ t7;
  121. t12 = y9 & y11;
  122. t13 = y14 & y17;
  123. t14 = t13 ^ t12;
  124. t15 = y8 & y10;
  125. t16 = t15 ^ t12;
  126. t17 = t4 ^ t14;
  127. t18 = t6 ^ t16;
  128. t19 = t9 ^ t14;
  129. t20 = t11 ^ t16;
  130. t21 = t17 ^ y20;
  131. t22 = t18 ^ y19;
  132. t23 = t19 ^ y21;
  133. t24 = t20 ^ y18;
  134. t25 = t21 ^ t22;
  135. t26 = t21 & t23;
  136. t27 = t24 ^ t26;
  137. t28 = t25 & t27;
  138. t29 = t28 ^ t22;
  139. t30 = t23 ^ t24;
  140. t31 = t22 ^ t26;
  141. t32 = t31 & t30;
  142. t33 = t32 ^ t24;
  143. t34 = t23 ^ t33;
  144. t35 = t27 ^ t33;
  145. t36 = t24 & t35;
  146. t37 = t36 ^ t34;
  147. t38 = t27 ^ t36;
  148. t39 = t29 & t38;
  149. t40 = t25 ^ t39;
  150. t41 = t40 ^ t37;
  151. t42 = t29 ^ t33;
  152. t43 = t29 ^ t40;
  153. t44 = t33 ^ t37;
  154. t45 = t42 ^ t41;
  155. z0 = t44 & y15;
  156. z1 = t37 & y6;
  157. z2 = t33 & x7;
  158. z3 = t43 & y16;
  159. z4 = t40 & y1;
  160. z5 = t29 & y7;
  161. z6 = t42 & y11;
  162. z7 = t45 & y17;
  163. z8 = t41 & y10;
  164. z9 = t44 & y12;
  165. z10 = t37 & y3;
  166. z11 = t33 & y4;
  167. z12 = t43 & y13;
  168. z13 = t40 & y5;
  169. z14 = t29 & y2;
  170. z15 = t42 & y9;
  171. z16 = t45 & y14;
  172. z17 = t41 & y8;
  173. /*
  174. * Bottom linear transformation.
  175. */
  176. t46 = z15 ^ z16;
  177. t47 = z10 ^ z11;
  178. t48 = z5 ^ z13;
  179. t49 = z9 ^ z10;
  180. t50 = z2 ^ z12;
  181. t51 = z2 ^ z5;
  182. t52 = z7 ^ z8;
  183. t53 = z0 ^ z3;
  184. t54 = z6 ^ z7;
  185. t55 = z16 ^ z17;
  186. t56 = z12 ^ t48;
  187. t57 = t50 ^ t53;
  188. t58 = z4 ^ t46;
  189. t59 = z3 ^ t54;
  190. t60 = t46 ^ t57;
  191. t61 = z14 ^ t57;
  192. t62 = t52 ^ t58;
  193. t63 = t49 ^ t58;
  194. t64 = z4 ^ t59;
  195. t65 = t61 ^ t62;
  196. t66 = z1 ^ t63;
  197. s0 = t59 ^ t63;
  198. s6 = t56 ^ ~t62;
  199. s7 = t48 ^ ~t60;
  200. t67 = t64 ^ t65;
  201. s3 = t53 ^ t66;
  202. s4 = t51 ^ t66;
  203. s5 = t47 ^ t65;
  204. s1 = t64 ^ ~s3;
  205. s2 = t55 ^ ~t67;
  206. q[7] = s0;
  207. q[6] = s1;
  208. q[5] = s2;
  209. q[4] = s3;
  210. q[3] = s4;
  211. q[2] = s5;
  212. q[1] = s6;
  213. q[0] = s7;
  214. }
  215. static void br_aes_ct_bitslice_Sbox(uint32_t *q) {
  216. /*
  217. * This S-box implementation is a straightforward translation of
  218. * the circuit described by Boyar and Peralta in "A new
  219. * combinational logic minimization technique with applications
  220. * to cryptology" (https://eprint.iacr.org/2009/191.pdf).
  221. *
  222. * Note that variables x* (input) and s* (output) are numbered
  223. * in "reverse" order (x0 is the high bit, x7 is the low bit).
  224. */
  225. uint32_t x0, x1, x2, x3, x4, x5, x6, x7;
  226. uint32_t y1, y2, y3, y4, y5, y6, y7, y8, y9;
  227. uint32_t y10, y11, y12, y13, y14, y15, y16, y17, y18, y19;
  228. uint32_t y20, y21;
  229. uint32_t z0, z1, z2, z3, z4, z5, z6, z7, z8, z9;
  230. uint32_t z10, z11, z12, z13, z14, z15, z16, z17;
  231. uint32_t t0, t1, t2, t3, t4, t5, t6, t7, t8, t9;
  232. uint32_t t10, t11, t12, t13, t14, t15, t16, t17, t18, t19;
  233. uint32_t t20, t21, t22, t23, t24, t25, t26, t27, t28, t29;
  234. uint32_t t30, t31, t32, t33, t34, t35, t36, t37, t38, t39;
  235. uint32_t t40, t41, t42, t43, t44, t45, t46, t47, t48, t49;
  236. uint32_t t50, t51, t52, t53, t54, t55, t56, t57, t58, t59;
  237. uint32_t t60, t61, t62, t63, t64, t65, t66, t67;
  238. uint32_t s0, s1, s2, s3, s4, s5, s6, s7;
  239. x0 = q[7];
  240. x1 = q[6];
  241. x2 = q[5];
  242. x3 = q[4];
  243. x4 = q[3];
  244. x5 = q[2];
  245. x6 = q[1];
  246. x7 = q[0];
  247. /*
  248. * Top linear transformation.
  249. */
  250. y14 = x3 ^ x5;
  251. y13 = x0 ^ x6;
  252. y9 = x0 ^ x3;
  253. y8 = x0 ^ x5;
  254. t0 = x1 ^ x2;
  255. y1 = t0 ^ x7;
  256. y4 = y1 ^ x3;
  257. y12 = y13 ^ y14;
  258. y2 = y1 ^ x0;
  259. y5 = y1 ^ x6;
  260. y3 = y5 ^ y8;
  261. t1 = x4 ^ y12;
  262. y15 = t1 ^ x5;
  263. y20 = t1 ^ x1;
  264. y6 = y15 ^ x7;
  265. y10 = y15 ^ t0;
  266. y11 = y20 ^ y9;
  267. y7 = x7 ^ y11;
  268. y17 = y10 ^ y11;
  269. y19 = y10 ^ y8;
  270. y16 = t0 ^ y11;
  271. y21 = y13 ^ y16;
  272. y18 = x0 ^ y16;
  273. /*
  274. * Non-linear section.
  275. */
  276. t2 = y12 & y15;
  277. t3 = y3 & y6;
  278. t4 = t3 ^ t2;
  279. t5 = y4 & x7;
  280. t6 = t5 ^ t2;
  281. t7 = y13 & y16;
  282. t8 = y5 & y1;
  283. t9 = t8 ^ t7;
  284. t10 = y2 & y7;
  285. t11 = t10 ^ t7;
  286. t12 = y9 & y11;
  287. t13 = y14 & y17;
  288. t14 = t13 ^ t12;
  289. t15 = y8 & y10;
  290. t16 = t15 ^ t12;
  291. t17 = t4 ^ t14;
  292. t18 = t6 ^ t16;
  293. t19 = t9 ^ t14;
  294. t20 = t11 ^ t16;
  295. t21 = t17 ^ y20;
  296. t22 = t18 ^ y19;
  297. t23 = t19 ^ y21;
  298. t24 = t20 ^ y18;
  299. t25 = t21 ^ t22;
  300. t26 = t21 & t23;
  301. t27 = t24 ^ t26;
  302. t28 = t25 & t27;
  303. t29 = t28 ^ t22;
  304. t30 = t23 ^ t24;
  305. t31 = t22 ^ t26;
  306. t32 = t31 & t30;
  307. t33 = t32 ^ t24;
  308. t34 = t23 ^ t33;
  309. t35 = t27 ^ t33;
  310. t36 = t24 & t35;
  311. t37 = t36 ^ t34;
  312. t38 = t27 ^ t36;
  313. t39 = t29 & t38;
  314. t40 = t25 ^ t39;
  315. t41 = t40 ^ t37;
  316. t42 = t29 ^ t33;
  317. t43 = t29 ^ t40;
  318. t44 = t33 ^ t37;
  319. t45 = t42 ^ t41;
  320. z0 = t44 & y15;
  321. z1 = t37 & y6;
  322. z2 = t33 & x7;
  323. z3 = t43 & y16;
  324. z4 = t40 & y1;
  325. z5 = t29 & y7;
  326. z6 = t42 & y11;
  327. z7 = t45 & y17;
  328. z8 = t41 & y10;
  329. z9 = t44 & y12;
  330. z10 = t37 & y3;
  331. z11 = t33 & y4;
  332. z12 = t43 & y13;
  333. z13 = t40 & y5;
  334. z14 = t29 & y2;
  335. z15 = t42 & y9;
  336. z16 = t45 & y14;
  337. z17 = t41 & y8;
  338. /*
  339. * Bottom linear transformation.
  340. */
  341. t46 = z15 ^ z16;
  342. t47 = z10 ^ z11;
  343. t48 = z5 ^ z13;
  344. t49 = z9 ^ z10;
  345. t50 = z2 ^ z12;
  346. t51 = z2 ^ z5;
  347. t52 = z7 ^ z8;
  348. t53 = z0 ^ z3;
  349. t54 = z6 ^ z7;
  350. t55 = z16 ^ z17;
  351. t56 = z12 ^ t48;
  352. t57 = t50 ^ t53;
  353. t58 = z4 ^ t46;
  354. t59 = z3 ^ t54;
  355. t60 = t46 ^ t57;
  356. t61 = z14 ^ t57;
  357. t62 = t52 ^ t58;
  358. t63 = t49 ^ t58;
  359. t64 = z4 ^ t59;
  360. t65 = t61 ^ t62;
  361. t66 = z1 ^ t63;
  362. s0 = t59 ^ t63;
  363. s6 = t56 ^ ~t62;
  364. s7 = t48 ^ ~t60;
  365. t67 = t64 ^ t65;
  366. s3 = t53 ^ t66;
  367. s4 = t51 ^ t66;
  368. s5 = t47 ^ t65;
  369. s1 = t64 ^ ~s3;
  370. s2 = t55 ^ ~t67;
  371. q[7] = s0;
  372. q[6] = s1;
  373. q[5] = s2;
  374. q[4] = s3;
  375. q[3] = s4;
  376. q[2] = s5;
  377. q[1] = s6;
  378. q[0] = s7;
  379. }
  380. static void br_aes_ct_ortho(uint32_t *q) {
  381. #define SWAPN_32(cl, ch, s, x, y) do { \
  382. uint32_t a, b; \
  383. a = (x); \
  384. b = (y); \
  385. (x) = (a & (uint32_t)(cl)) | ((b & (uint32_t)(cl)) << (s)); \
  386. (y) = ((a & (uint32_t)(ch)) >> (s)) | (b & (uint32_t)(ch)); \
  387. } while (0)
  388. #define SWAP2_32(x, y) SWAPN_32(0x55555555, 0xAAAAAAAA, 1, x, y)
  389. #define SWAP4_32(x, y) SWAPN_32(0x33333333, 0xCCCCCCCC, 2, x, y)
  390. #define SWAP8_32(x, y) SWAPN_32(0x0F0F0F0F, 0xF0F0F0F0, 4, x, y)
  391. SWAP2_32(q[0], q[1]);
  392. SWAP2_32(q[2], q[3]);
  393. SWAP2_32(q[4], q[5]);
  394. SWAP2_32(q[6], q[7]);
  395. SWAP4_32(q[0], q[2]);
  396. SWAP4_32(q[1], q[3]);
  397. SWAP4_32(q[4], q[6]);
  398. SWAP4_32(q[5], q[7]);
  399. SWAP8_32(q[0], q[4]);
  400. SWAP8_32(q[1], q[5]);
  401. SWAP8_32(q[2], q[6]);
  402. SWAP8_32(q[3], q[7]);
  403. }
  404. static inline void add_round_key32(uint32_t *q, const uint32_t *sk) {
  405. q[0] ^= sk[0];
  406. q[1] ^= sk[1];
  407. q[2] ^= sk[2];
  408. q[3] ^= sk[3];
  409. q[4] ^= sk[4];
  410. q[5] ^= sk[5];
  411. q[6] ^= sk[6];
  412. q[7] ^= sk[7];
  413. }
  414. static inline void shift_rows32(uint32_t *q) {
  415. int i;
  416. for (i = 0; i < 8; i++) {
  417. uint32_t x;
  418. x = q[i];
  419. q[i] = (x & 0x000000FF)
  420. | ((x & 0x0000FC00) >> 2) | ((x & 0x00000300) << 6)
  421. | ((x & 0x00F00000) >> 4) | ((x & 0x000F0000) << 4)
  422. | ((x & 0xC0000000) >> 6) | ((x & 0x3F000000) << 2);
  423. }
  424. }
  425. static inline uint32_t rotr16(uint32_t x) {
  426. return (x << 16) | (x >> 16);
  427. }
  428. static inline void mix_columns32(uint32_t *q) {
  429. uint32_t q0, q1, q2, q3, q4, q5, q6, q7;
  430. uint32_t r0, r1, r2, r3, r4, r5, r6, r7;
  431. q0 = q[0];
  432. q1 = q[1];
  433. q2 = q[2];
  434. q3 = q[3];
  435. q4 = q[4];
  436. q5 = q[5];
  437. q6 = q[6];
  438. q7 = q[7];
  439. r0 = (q0 >> 8) | (q0 << 24);
  440. r1 = (q1 >> 8) | (q1 << 24);
  441. r2 = (q2 >> 8) | (q2 << 24);
  442. r3 = (q3 >> 8) | (q3 << 24);
  443. r4 = (q4 >> 8) | (q4 << 24);
  444. r5 = (q5 >> 8) | (q5 << 24);
  445. r6 = (q6 >> 8) | (q6 << 24);
  446. r7 = (q7 >> 8) | (q7 << 24);
  447. q[0] = q7 ^ r7 ^ r0 ^ rotr16(q0 ^ r0);
  448. q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr16(q1 ^ r1);
  449. q[2] = q1 ^ r1 ^ r2 ^ rotr16(q2 ^ r2);
  450. q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr16(q3 ^ r3);
  451. q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr16(q4 ^ r4);
  452. q[5] = q4 ^ r4 ^ r5 ^ rotr16(q5 ^ r5);
  453. q[6] = q5 ^ r5 ^ r6 ^ rotr16(q6 ^ r6);
  454. q[7] = q6 ^ r6 ^ r7 ^ rotr16(q7 ^ r7);
  455. }
  456. static void br_aes_ct64_ortho(uint64_t *q) {
  457. #define SWAPN(cl, ch, s, x, y) do { \
  458. uint64_t a, b; \
  459. a = (x); \
  460. b = (y); \
  461. (x) = (a & (uint64_t)(cl)) | ((b & (uint64_t)(cl)) << (s)); \
  462. (y) = ((a & (uint64_t)(ch)) >> (s)) | (b & (uint64_t)(ch)); \
  463. } while (0)
  464. #define SWAP2(x, y) SWAPN(0x5555555555555555, 0xAAAAAAAAAAAAAAAA, 1, x, y)
  465. #define SWAP4(x, y) SWAPN(0x3333333333333333, 0xCCCCCCCCCCCCCCCC, 2, x, y)
  466. #define SWAP8(x, y) SWAPN(0x0F0F0F0F0F0F0F0F, 0xF0F0F0F0F0F0F0F0, 4, x, y)
  467. SWAP2(q[0], q[1]);
  468. SWAP2(q[2], q[3]);
  469. SWAP2(q[4], q[5]);
  470. SWAP2(q[6], q[7]);
  471. SWAP4(q[0], q[2]);
  472. SWAP4(q[1], q[3]);
  473. SWAP4(q[4], q[6]);
  474. SWAP4(q[5], q[7]);
  475. SWAP8(q[0], q[4]);
  476. SWAP8(q[1], q[5]);
  477. SWAP8(q[2], q[6]);
  478. SWAP8(q[3], q[7]);
  479. }
  480. static void br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w) {
  481. uint64_t x0, x1, x2, x3;
  482. x0 = w[0];
  483. x1 = w[1];
  484. x2 = w[2];
  485. x3 = w[3];
  486. x0 |= (x0 << 16);
  487. x1 |= (x1 << 16);
  488. x2 |= (x2 << 16);
  489. x3 |= (x3 << 16);
  490. x0 &= (uint64_t)0x0000FFFF0000FFFF;
  491. x1 &= (uint64_t)0x0000FFFF0000FFFF;
  492. x2 &= (uint64_t)0x0000FFFF0000FFFF;
  493. x3 &= (uint64_t)0x0000FFFF0000FFFF;
  494. x0 |= (x0 << 8);
  495. x1 |= (x1 << 8);
  496. x2 |= (x2 << 8);
  497. x3 |= (x3 << 8);
  498. x0 &= (uint64_t)0x00FF00FF00FF00FF;
  499. x1 &= (uint64_t)0x00FF00FF00FF00FF;
  500. x2 &= (uint64_t)0x00FF00FF00FF00FF;
  501. x3 &= (uint64_t)0x00FF00FF00FF00FF;
  502. *q0 = x0 | (x2 << 8);
  503. *q1 = x1 | (x3 << 8);
  504. }
  505. static void br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1) {
  506. uint64_t x0, x1, x2, x3;
  507. x0 = q0 & (uint64_t)0x00FF00FF00FF00FF;
  508. x1 = q1 & (uint64_t)0x00FF00FF00FF00FF;
  509. x2 = (q0 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
  510. x3 = (q1 >> 8) & (uint64_t)0x00FF00FF00FF00FF;
  511. x0 |= (x0 >> 8);
  512. x1 |= (x1 >> 8);
  513. x2 |= (x2 >> 8);
  514. x3 |= (x3 >> 8);
  515. x0 &= (uint64_t)0x0000FFFF0000FFFF;
  516. x1 &= (uint64_t)0x0000FFFF0000FFFF;
  517. x2 &= (uint64_t)0x0000FFFF0000FFFF;
  518. x3 &= (uint64_t)0x0000FFFF0000FFFF;
  519. w[0] = (uint32_t)x0 | (uint32_t)(x0 >> 16);
  520. w[1] = (uint32_t)x1 | (uint32_t)(x1 >> 16);
  521. w[2] = (uint32_t)x2 | (uint32_t)(x2 >> 16);
  522. w[3] = (uint32_t)x3 | (uint32_t)(x3 >> 16);
  523. }
  524. static inline void add_round_key(uint64_t *q, const uint64_t *sk) {
  525. q[0] ^= sk[0];
  526. q[1] ^= sk[1];
  527. q[2] ^= sk[2];
  528. q[3] ^= sk[3];
  529. q[4] ^= sk[4];
  530. q[5] ^= sk[5];
  531. q[6] ^= sk[6];
  532. q[7] ^= sk[7];
  533. }
  534. static inline void shift_rows(uint64_t *q) {
  535. int i;
  536. for (i = 0; i < 8; i++) {
  537. uint64_t x;
  538. x = q[i];
  539. q[i] = (x & (uint64_t)0x000000000000FFFF)
  540. | ((x & (uint64_t)0x00000000FFF00000) >> 4)
  541. | ((x & (uint64_t)0x00000000000F0000) << 12)
  542. | ((x & (uint64_t)0x0000FF0000000000) >> 8)
  543. | ((x & (uint64_t)0x000000FF00000000) << 8)
  544. | ((x & (uint64_t)0xF000000000000000) >> 12)
  545. | ((x & (uint64_t)0x0FFF000000000000) << 4);
  546. }
  547. }
  548. static inline uint64_t rotr32(uint64_t x) {
  549. return (x << 32) | (x >> 32);
  550. }
  551. static inline void mix_columns(uint64_t *q) {
  552. uint64_t q0, q1, q2, q3, q4, q5, q6, q7;
  553. uint64_t r0, r1, r2, r3, r4, r5, r6, r7;
  554. q0 = q[0];
  555. q1 = q[1];
  556. q2 = q[2];
  557. q3 = q[3];
  558. q4 = q[4];
  559. q5 = q[5];
  560. q6 = q[6];
  561. q7 = q[7];
  562. r0 = (q0 >> 16) | (q0 << 48);
  563. r1 = (q1 >> 16) | (q1 << 48);
  564. r2 = (q2 >> 16) | (q2 << 48);
  565. r3 = (q3 >> 16) | (q3 << 48);
  566. r4 = (q4 >> 16) | (q4 << 48);
  567. r5 = (q5 >> 16) | (q5 << 48);
  568. r6 = (q6 >> 16) | (q6 << 48);
  569. r7 = (q7 >> 16) | (q7 << 48);
  570. q[0] = q7 ^ r7 ^ r0 ^ rotr32(q0 ^ r0);
  571. q[1] = q0 ^ r0 ^ q7 ^ r7 ^ r1 ^ rotr32(q1 ^ r1);
  572. q[2] = q1 ^ r1 ^ r2 ^ rotr32(q2 ^ r2);
  573. q[3] = q2 ^ r2 ^ q7 ^ r7 ^ r3 ^ rotr32(q3 ^ r3);
  574. q[4] = q3 ^ r3 ^ q7 ^ r7 ^ r4 ^ rotr32(q4 ^ r4);
  575. q[5] = q4 ^ r4 ^ r5 ^ rotr32(q5 ^ r5);
  576. q[6] = q5 ^ r5 ^ r6 ^ rotr32(q6 ^ r6);
  577. q[7] = q6 ^ r6 ^ r7 ^ rotr32(q7 ^ r7);
  578. }
  579. static void interleave_constant(uint64_t *out, const unsigned char *in) {
  580. uint32_t tmp_32_constant[16];
  581. int i;
  582. br_range_dec32le(tmp_32_constant, 16, in);
  583. for (i = 0; i < 4; i++) {
  584. br_aes_ct64_interleave_in(&out[i], &out[i + 4], tmp_32_constant + (i << 2));
  585. }
  586. br_aes_ct64_ortho(out);
  587. }
  588. static void interleave_constant32(uint32_t *out, const unsigned char *in) {
  589. int i;
  590. for (i = 0; i < 4; i++) {
  591. out[2 * i] = br_dec32le(in + 4 * i);
  592. out[2 * i + 1] = br_dec32le(in + 4 * i + 16);
  593. }
  594. br_aes_ct_ortho(out);
  595. }
  596. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_tweak_constants(
  597. harakactx *state,
  598. const unsigned char *pk_seed, const unsigned char *sk_seed,
  599. unsigned long long seed_length) {
  600. unsigned char buf[40 * 16];
  601. int i;
  602. /* Use the standard constants to generate tweaked ones. */
  603. memcpy((uint8_t *)state->tweaked512_rc64, (uint8_t *)haraka512_rc64, 40 * 16);
  604. /* Constants for sk.seed */
  605. if (sk_seed != NULL) {
  606. PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka_S(
  607. buf, 40 * 16, sk_seed, seed_length, state);
  608. /* Interleave constants */
  609. for (i = 0; i < 10; i++) {
  610. interleave_constant32(state->tweaked256_rc32_sseed[i], buf + 32 * i);
  611. }
  612. }
  613. /* Constants for pk.seed */
  614. PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka_S(
  615. buf, 40 * 16, pk_seed, seed_length, state);
  616. for (i = 0; i < 10; i++) {
  617. interleave_constant32(state->tweaked256_rc32[i], buf + 32 * i);
  618. interleave_constant(state->tweaked512_rc64[i], buf + 64 * i);
  619. }
  620. }
  621. static void haraka_S_absorb(unsigned char *s,
  622. const unsigned char *m, unsigned long long mlen,
  623. unsigned char p, const harakactx *state) {
  624. unsigned long long i;
  625. unsigned char t[HARAKAS_RATE];
  626. while (mlen >= HARAKAS_RATE) {
  627. /* XOR block to state */
  628. for (i = 0; i < HARAKAS_RATE; ++i) {
  629. s[i] ^= m[i];
  630. }
  631. PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka512_perm(s, s, state);
  632. mlen -= HARAKAS_RATE;
  633. m += HARAKAS_RATE;
  634. }
  635. for (i = 0; i < HARAKAS_RATE; ++i) {
  636. t[i] = 0;
  637. }
  638. for (i = 0; i < mlen; ++i) {
  639. t[i] = m[i];
  640. }
  641. t[i] = p;
  642. t[HARAKAS_RATE - 1] |= 128;
  643. for (i = 0; i < HARAKAS_RATE; ++i) {
  644. s[i] ^= t[i];
  645. }
  646. }
  647. static void haraka_S_squeezeblocks(unsigned char *h, unsigned long long nblocks,
  648. unsigned char *s, const harakactx *state) {
  649. while (nblocks > 0) {
  650. PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka512_perm(s, s, state);
  651. memcpy(h, s, HARAKAS_RATE);
  652. h += HARAKAS_RATE;
  653. nblocks--;
  654. }
  655. }
  656. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka_S_inc_init(uint8_t *s_inc) {
  657. size_t i;
  658. for (i = 0; i < 64; i++) {
  659. s_inc[i] = 0;
  660. }
  661. s_inc[64] = 0;
  662. }
  663. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka_S_inc_absorb(uint8_t *s_inc, const uint8_t *m, size_t mlen, const harakactx *state) {
  664. size_t i;
  665. /* Recall that s_inc[64] is the non-absorbed bytes xored into the state */
  666. while (mlen + s_inc[64] >= HARAKAS_RATE) {
  667. for (i = 0; i < (size_t)(HARAKAS_RATE - s_inc[64]); i++) {
  668. /* Take the i'th byte from message
  669. xor with the s_inc[64] + i'th byte of the state */
  670. s_inc[s_inc[64] + i] ^= m[i];
  671. }
  672. mlen -= (size_t)(HARAKAS_RATE - s_inc[64]);
  673. m += HARAKAS_RATE - s_inc[64];
  674. s_inc[64] = 0;
  675. PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka512_perm(s_inc, s_inc, state);
  676. }
  677. for (i = 0; i < mlen; i++) {
  678. s_inc[s_inc[64] + i] ^= m[i];
  679. }
  680. s_inc[64] = (uint8_t)(mlen + s_inc[64]);
  681. }
  682. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka_S_inc_finalize(uint8_t *s_inc) {
  683. /* After haraka_S_inc_absorb, we are guaranteed that s_inc[64] < HARAKAS_RATE,
  684. so we can always use one more byte for p in the current state. */
  685. s_inc[s_inc[64]] ^= 0x1F;
  686. s_inc[HARAKAS_RATE - 1] ^= 128;
  687. s_inc[64] = 0;
  688. }
  689. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka_S_inc_squeeze(uint8_t *out, size_t outlen, uint8_t *s_inc, const harakactx *state) {
  690. uint8_t i;
  691. /* First consume any bytes we still have sitting around */
  692. for (i = 0; i < outlen && i < s_inc[64]; i++) {
  693. /* There are s_inc[64] bytes left, so r - s_inc[64] is the first
  694. available byte. We consume from there, i.e., up to r. */
  695. out[i] = s_inc[(HARAKAS_RATE - s_inc[64] + i)];
  696. }
  697. out += i;
  698. outlen -= i;
  699. s_inc[64] = (uint8_t)(s_inc[64] - i);
  700. /* Then squeeze the remaining necessary blocks */
  701. while (outlen > 0) {
  702. PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka512_perm(s_inc, s_inc, state);
  703. for (i = 0; i < outlen && i < HARAKAS_RATE; i++) {
  704. out[i] = s_inc[i];
  705. }
  706. out += i;
  707. outlen -= i;
  708. s_inc[64] = (uint8_t)(HARAKAS_RATE - i);
  709. }
  710. }
  711. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka_S(unsigned char *out, unsigned long long outlen, const unsigned char *in, unsigned long long inlen, const harakactx *state) {
  712. unsigned long long i;
  713. unsigned char s[64];
  714. unsigned char d[32];
  715. for (i = 0; i < 64; i++) {
  716. s[i] = 0;
  717. }
  718. haraka_S_absorb(s, in, inlen, 0x1F, state);
  719. haraka_S_squeezeblocks(out, outlen / 32, s, state);
  720. out += (outlen / 32) * 32;
  721. if (outlen % 32) {
  722. haraka_S_squeezeblocks(d, 1, s, state);
  723. for (i = 0; i < outlen % 32; i++) {
  724. out[i] = d[i];
  725. }
  726. }
  727. }
  728. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka512_perm(unsigned char *out, const unsigned char *in, const harakactx *state) {
  729. uint32_t w[16];
  730. uint64_t q[8], tmp_q;
  731. unsigned int i, j;
  732. br_range_dec32le(w, 16, in);
  733. for (i = 0; i < 4; i++) {
  734. br_aes_ct64_interleave_in(&q[i], &q[i + 4], w + (i << 2));
  735. }
  736. br_aes_ct64_ortho(q);
  737. /* AES rounds */
  738. for (i = 0; i < 5; i++) {
  739. for (j = 0; j < 2; j++) {
  740. br_aes_ct64_bitslice_Sbox(q);
  741. shift_rows(q);
  742. mix_columns(q);
  743. add_round_key(q, state->tweaked512_rc64[2 * i + j]);
  744. }
  745. /* Mix states */
  746. for (j = 0; j < 8; j++) {
  747. tmp_q = q[j];
  748. q[j] = (tmp_q & 0x0001000100010001) << 5 |
  749. (tmp_q & 0x0002000200020002) << 12 |
  750. (tmp_q & 0x0004000400040004) >> 1 |
  751. (tmp_q & 0x0008000800080008) << 6 |
  752. (tmp_q & 0x0020002000200020) << 9 |
  753. (tmp_q & 0x0040004000400040) >> 4 |
  754. (tmp_q & 0x0080008000800080) << 3 |
  755. (tmp_q & 0x2100210021002100) >> 5 |
  756. (tmp_q & 0x0210021002100210) << 2 |
  757. (tmp_q & 0x0800080008000800) << 4 |
  758. (tmp_q & 0x1000100010001000) >> 12 |
  759. (tmp_q & 0x4000400040004000) >> 10 |
  760. (tmp_q & 0x8400840084008400) >> 3;
  761. }
  762. }
  763. br_aes_ct64_ortho(q);
  764. for (i = 0; i < 4; i ++) {
  765. br_aes_ct64_interleave_out(w + (i << 2), q[i], q[i + 4]);
  766. }
  767. br_range_enc32le(out, w, 16);
  768. }
  769. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka512(unsigned char *out, const unsigned char *in, const harakactx *state) {
  770. int i;
  771. unsigned char buf[64];
  772. PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka512_perm(buf, in, state);
  773. /* Feed-forward */
  774. for (i = 0; i < 64; i++) {
  775. buf[i] = buf[i] ^ in[i];
  776. }
  777. /* Truncated */
  778. memcpy(out, buf + 8, 8);
  779. memcpy(out + 8, buf + 24, 8);
  780. memcpy(out + 16, buf + 32, 8);
  781. memcpy(out + 24, buf + 48, 8);
  782. }
  783. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka256(unsigned char *out, const unsigned char *in, const harakactx *state) {
  784. uint32_t q[8], tmp_q;
  785. int i, j;
  786. for (i = 0; i < 4; i++) {
  787. q[2 * i] = br_dec32le(in + 4 * i);
  788. q[2 * i + 1] = br_dec32le(in + 4 * i + 16);
  789. }
  790. br_aes_ct_ortho(q);
  791. /* AES rounds */
  792. for (i = 0; i < 5; i++) {
  793. for (j = 0; j < 2; j++) {
  794. br_aes_ct_bitslice_Sbox(q);
  795. shift_rows32(q);
  796. mix_columns32(q);
  797. add_round_key32(q, state->tweaked256_rc32[2 * i + j]);
  798. }
  799. /* Mix states */
  800. for (j = 0; j < 8; j++) {
  801. tmp_q = q[j];
  802. q[j] = (tmp_q & 0x81818181) |
  803. (tmp_q & 0x02020202) << 1 |
  804. (tmp_q & 0x04040404) << 2 |
  805. (tmp_q & 0x08080808) << 3 |
  806. (tmp_q & 0x10101010) >> 3 |
  807. (tmp_q & 0x20202020) >> 2 |
  808. (tmp_q & 0x40404040) >> 1;
  809. }
  810. }
  811. br_aes_ct_ortho(q);
  812. for (i = 0; i < 4; i++) {
  813. br_enc32le(out + 4 * i, q[2 * i]);
  814. br_enc32le(out + 4 * i + 16, q[2 * i + 1]);
  815. }
  816. for (i = 0; i < 32; i++) {
  817. out[i] ^= in[i];
  818. }
  819. }
  820. void PQCLEAN_SPHINCSHARAKA128SROBUST_CLEAN_haraka256_sk(unsigned char *out, const unsigned char *in, const harakactx *state) {
  821. uint32_t q[8], tmp_q;
  822. int i, j;
  823. for (i = 0; i < 4; i++) {
  824. q[2 * i] = br_dec32le(in + 4 * i);
  825. q[2 * i + 1] = br_dec32le(in + 4 * i + 16);
  826. }
  827. br_aes_ct_ortho(q);
  828. /* AES rounds */
  829. for (i = 0; i < 5; i++) {
  830. for (j = 0; j < 2; j++) {
  831. br_aes_ct_bitslice_Sbox(q);
  832. shift_rows32(q);
  833. mix_columns32(q);
  834. add_round_key32(q, state->tweaked256_rc32_sseed[2 * i + j]);
  835. }
  836. /* Mix states */
  837. for (j = 0; j < 8; j++) {
  838. tmp_q = q[j];
  839. q[j] = (tmp_q & 0x81818181) |
  840. (tmp_q & 0x02020202) << 1 |
  841. (tmp_q & 0x04040404) << 2 |
  842. (tmp_q & 0x08080808) << 3 |
  843. (tmp_q & 0x10101010) >> 3 |
  844. (tmp_q & 0x20202020) >> 2 |
  845. (tmp_q & 0x40404040) >> 1;
  846. }
  847. }
  848. br_aes_ct_ortho(q);
  849. for (i = 0; i < 4; i++) {
  850. br_enc32le(out + 4 * i, q[2 * i]);
  851. br_enc32le(out + 4 * i + 16, q[2 * i + 1]);
  852. }
  853. for (i = 0; i < 32; i++) {
  854. out[i] ^= in[i];
  855. }
  856. }