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.
 
 
 
 

574 lines
17 KiB

  1. /* Based on the public domain implementation in
  2. * crypto_hash/keccakc512/simple/ from http://bench.cr.yp.to/supercop.html
  3. * by Ronny Van Keer
  4. * and the public domain "TweetFips202" implementation
  5. * from https://twitter.com/tweetfips202
  6. * by Gilles Van Assche, Daniel J. Bernstein, and Peter Schwabe */
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include "fips202.h"
  10. #define NROUNDS 24
  11. #define ROL(a, offset) (((a) << (offset)) ^ ((a) >> (64 - (offset))))
  12. /*************************************************
  13. * Name: load64
  14. *
  15. * Description: Load 8 bytes into uint64_t in little-endian order
  16. *
  17. * Arguments: - const uint8_t *x: pointer to input byte array
  18. *
  19. * Returns the loaded 64-bit unsigned integer
  20. **************************************************/
  21. static uint64_t load64(const uint8_t *x) {
  22. uint64_t r = 0;
  23. for (size_t i = 0; i < 8; ++i) {
  24. r |= (uint64_t)x[i] << 8 * i;
  25. }
  26. return r;
  27. }
  28. /*************************************************
  29. * Name: store64
  30. *
  31. * Description: Store a 64-bit integer to a byte array in little-endian order
  32. *
  33. * Arguments: - uint8_t *x: pointer to the output byte array
  34. * - uint64_t u: input 64-bit unsigned integer
  35. **************************************************/
  36. static void store64(uint8_t *x, uint64_t u) {
  37. for (size_t i = 0; i < 8; ++i) {
  38. x[i] = (uint8_t) (u >> 8 * i);
  39. }
  40. }
  41. /* Keccak round constants */
  42. static const uint64_t KeccakF_RoundConstants[NROUNDS] = {
  43. 0x0000000000000001ULL, 0x0000000000008082ULL,
  44. 0x800000000000808aULL, 0x8000000080008000ULL,
  45. 0x000000000000808bULL, 0x0000000080000001ULL,
  46. 0x8000000080008081ULL, 0x8000000000008009ULL,
  47. 0x000000000000008aULL, 0x0000000000000088ULL,
  48. 0x0000000080008009ULL, 0x000000008000000aULL,
  49. 0x000000008000808bULL, 0x800000000000008bULL,
  50. 0x8000000000008089ULL, 0x8000000000008003ULL,
  51. 0x8000000000008002ULL, 0x8000000000000080ULL,
  52. 0x000000000000800aULL, 0x800000008000000aULL,
  53. 0x8000000080008081ULL, 0x8000000000008080ULL,
  54. 0x0000000080000001ULL, 0x8000000080008008ULL
  55. };
  56. /*************************************************
  57. * Name: KeccakF1600_StatePermute
  58. *
  59. * Description: The Keccak F1600 Permutation
  60. *
  61. * Arguments: - uint64_t *state: pointer to input/output Keccak state
  62. **************************************************/
  63. static void KeccakF1600_StatePermute(uint64_t *state) {
  64. int round;
  65. uint64_t Aba, Abe, Abi, Abo, Abu;
  66. uint64_t Aga, Age, Agi, Ago, Agu;
  67. uint64_t Aka, Ake, Aki, Ako, Aku;
  68. uint64_t Ama, Ame, Ami, Amo, Amu;
  69. uint64_t Asa, Ase, Asi, Aso, Asu;
  70. uint64_t BCa, BCe, BCi, BCo, BCu;
  71. uint64_t Da, De, Di, Do, Du;
  72. uint64_t Eba, Ebe, Ebi, Ebo, Ebu;
  73. uint64_t Ega, Ege, Egi, Ego, Egu;
  74. uint64_t Eka, Eke, Eki, Eko, Eku;
  75. uint64_t Ema, Eme, Emi, Emo, Emu;
  76. uint64_t Esa, Ese, Esi, Eso, Esu;
  77. // copyFromState(A, state)
  78. Aba = state[0];
  79. Abe = state[1];
  80. Abi = state[2];
  81. Abo = state[3];
  82. Abu = state[4];
  83. Aga = state[5];
  84. Age = state[6];
  85. Agi = state[7];
  86. Ago = state[8];
  87. Agu = state[9];
  88. Aka = state[10];
  89. Ake = state[11];
  90. Aki = state[12];
  91. Ako = state[13];
  92. Aku = state[14];
  93. Ama = state[15];
  94. Ame = state[16];
  95. Ami = state[17];
  96. Amo = state[18];
  97. Amu = state[19];
  98. Asa = state[20];
  99. Ase = state[21];
  100. Asi = state[22];
  101. Aso = state[23];
  102. Asu = state[24];
  103. for (round = 0; round < NROUNDS; round += 2) {
  104. // prepareTheta
  105. BCa = Aba ^ Aga ^ Aka ^ Ama ^ Asa;
  106. BCe = Abe ^ Age ^ Ake ^ Ame ^ Ase;
  107. BCi = Abi ^ Agi ^ Aki ^ Ami ^ Asi;
  108. BCo = Abo ^ Ago ^ Ako ^ Amo ^ Aso;
  109. BCu = Abu ^ Agu ^ Aku ^ Amu ^ Asu;
  110. // thetaRhoPiChiIotaPrepareTheta(round , A, E)
  111. Da = BCu ^ ROL(BCe, 1);
  112. De = BCa ^ ROL(BCi, 1);
  113. Di = BCe ^ ROL(BCo, 1);
  114. Do = BCi ^ ROL(BCu, 1);
  115. Du = BCo ^ ROL(BCa, 1);
  116. Aba ^= Da;
  117. BCa = Aba;
  118. Age ^= De;
  119. BCe = ROL(Age, 44);
  120. Aki ^= Di;
  121. BCi = ROL(Aki, 43);
  122. Amo ^= Do;
  123. BCo = ROL(Amo, 21);
  124. Asu ^= Du;
  125. BCu = ROL(Asu, 14);
  126. Eba = BCa ^ ((~BCe) & BCi);
  127. Eba ^= KeccakF_RoundConstants[round];
  128. Ebe = BCe ^ ((~BCi) & BCo);
  129. Ebi = BCi ^ ((~BCo) & BCu);
  130. Ebo = BCo ^ ((~BCu) & BCa);
  131. Ebu = BCu ^ ((~BCa) & BCe);
  132. Abo ^= Do;
  133. BCa = ROL(Abo, 28);
  134. Agu ^= Du;
  135. BCe = ROL(Agu, 20);
  136. Aka ^= Da;
  137. BCi = ROL(Aka, 3);
  138. Ame ^= De;
  139. BCo = ROL(Ame, 45);
  140. Asi ^= Di;
  141. BCu = ROL(Asi, 61);
  142. Ega = BCa ^ ((~BCe) & BCi);
  143. Ege = BCe ^ ((~BCi) & BCo);
  144. Egi = BCi ^ ((~BCo) & BCu);
  145. Ego = BCo ^ ((~BCu) & BCa);
  146. Egu = BCu ^ ((~BCa) & BCe);
  147. Abe ^= De;
  148. BCa = ROL(Abe, 1);
  149. Agi ^= Di;
  150. BCe = ROL(Agi, 6);
  151. Ako ^= Do;
  152. BCi = ROL(Ako, 25);
  153. Amu ^= Du;
  154. BCo = ROL(Amu, 8);
  155. Asa ^= Da;
  156. BCu = ROL(Asa, 18);
  157. Eka = BCa ^ ((~BCe) & BCi);
  158. Eke = BCe ^ ((~BCi) & BCo);
  159. Eki = BCi ^ ((~BCo) & BCu);
  160. Eko = BCo ^ ((~BCu) & BCa);
  161. Eku = BCu ^ ((~BCa) & BCe);
  162. Abu ^= Du;
  163. BCa = ROL(Abu, 27);
  164. Aga ^= Da;
  165. BCe = ROL(Aga, 36);
  166. Ake ^= De;
  167. BCi = ROL(Ake, 10);
  168. Ami ^= Di;
  169. BCo = ROL(Ami, 15);
  170. Aso ^= Do;
  171. BCu = ROL(Aso, 56);
  172. Ema = BCa ^ ((~BCe) & BCi);
  173. Eme = BCe ^ ((~BCi) & BCo);
  174. Emi = BCi ^ ((~BCo) & BCu);
  175. Emo = BCo ^ ((~BCu) & BCa);
  176. Emu = BCu ^ ((~BCa) & BCe);
  177. Abi ^= Di;
  178. BCa = ROL(Abi, 62);
  179. Ago ^= Do;
  180. BCe = ROL(Ago, 55);
  181. Aku ^= Du;
  182. BCi = ROL(Aku, 39);
  183. Ama ^= Da;
  184. BCo = ROL(Ama, 41);
  185. Ase ^= De;
  186. BCu = ROL(Ase, 2);
  187. Esa = BCa ^ ((~BCe) & BCi);
  188. Ese = BCe ^ ((~BCi) & BCo);
  189. Esi = BCi ^ ((~BCo) & BCu);
  190. Eso = BCo ^ ((~BCu) & BCa);
  191. Esu = BCu ^ ((~BCa) & BCe);
  192. // prepareTheta
  193. BCa = Eba ^ Ega ^ Eka ^ Ema ^ Esa;
  194. BCe = Ebe ^ Ege ^ Eke ^ Eme ^ Ese;
  195. BCi = Ebi ^ Egi ^ Eki ^ Emi ^ Esi;
  196. BCo = Ebo ^ Ego ^ Eko ^ Emo ^ Eso;
  197. BCu = Ebu ^ Egu ^ Eku ^ Emu ^ Esu;
  198. // thetaRhoPiChiIotaPrepareTheta(round+1, E, A)
  199. Da = BCu ^ ROL(BCe, 1);
  200. De = BCa ^ ROL(BCi, 1);
  201. Di = BCe ^ ROL(BCo, 1);
  202. Do = BCi ^ ROL(BCu, 1);
  203. Du = BCo ^ ROL(BCa, 1);
  204. Eba ^= Da;
  205. BCa = Eba;
  206. Ege ^= De;
  207. BCe = ROL(Ege, 44);
  208. Eki ^= Di;
  209. BCi = ROL(Eki, 43);
  210. Emo ^= Do;
  211. BCo = ROL(Emo, 21);
  212. Esu ^= Du;
  213. BCu = ROL(Esu, 14);
  214. Aba = BCa ^ ((~BCe) & BCi);
  215. Aba ^= KeccakF_RoundConstants[round + 1];
  216. Abe = BCe ^ ((~BCi) & BCo);
  217. Abi = BCi ^ ((~BCo) & BCu);
  218. Abo = BCo ^ ((~BCu) & BCa);
  219. Abu = BCu ^ ((~BCa) & BCe);
  220. Ebo ^= Do;
  221. BCa = ROL(Ebo, 28);
  222. Egu ^= Du;
  223. BCe = ROL(Egu, 20);
  224. Eka ^= Da;
  225. BCi = ROL(Eka, 3);
  226. Eme ^= De;
  227. BCo = ROL(Eme, 45);
  228. Esi ^= Di;
  229. BCu = ROL(Esi, 61);
  230. Aga = BCa ^ ((~BCe) & BCi);
  231. Age = BCe ^ ((~BCi) & BCo);
  232. Agi = BCi ^ ((~BCo) & BCu);
  233. Ago = BCo ^ ((~BCu) & BCa);
  234. Agu = BCu ^ ((~BCa) & BCe);
  235. Ebe ^= De;
  236. BCa = ROL(Ebe, 1);
  237. Egi ^= Di;
  238. BCe = ROL(Egi, 6);
  239. Eko ^= Do;
  240. BCi = ROL(Eko, 25);
  241. Emu ^= Du;
  242. BCo = ROL(Emu, 8);
  243. Esa ^= Da;
  244. BCu = ROL(Esa, 18);
  245. Aka = BCa ^ ((~BCe) & BCi);
  246. Ake = BCe ^ ((~BCi) & BCo);
  247. Aki = BCi ^ ((~BCo) & BCu);
  248. Ako = BCo ^ ((~BCu) & BCa);
  249. Aku = BCu ^ ((~BCa) & BCe);
  250. Ebu ^= Du;
  251. BCa = ROL(Ebu, 27);
  252. Ega ^= Da;
  253. BCe = ROL(Ega, 36);
  254. Eke ^= De;
  255. BCi = ROL(Eke, 10);
  256. Emi ^= Di;
  257. BCo = ROL(Emi, 15);
  258. Eso ^= Do;
  259. BCu = ROL(Eso, 56);
  260. Ama = BCa ^ ((~BCe) & BCi);
  261. Ame = BCe ^ ((~BCi) & BCo);
  262. Ami = BCi ^ ((~BCo) & BCu);
  263. Amo = BCo ^ ((~BCu) & BCa);
  264. Amu = BCu ^ ((~BCa) & BCe);
  265. Ebi ^= Di;
  266. BCa = ROL(Ebi, 62);
  267. Ego ^= Do;
  268. BCe = ROL(Ego, 55);
  269. Eku ^= Du;
  270. BCi = ROL(Eku, 39);
  271. Ema ^= Da;
  272. BCo = ROL(Ema, 41);
  273. Ese ^= De;
  274. BCu = ROL(Ese, 2);
  275. Asa = BCa ^ ((~BCe) & BCi);
  276. Ase = BCe ^ ((~BCi) & BCo);
  277. Asi = BCi ^ ((~BCo) & BCu);
  278. Aso = BCo ^ ((~BCu) & BCa);
  279. Asu = BCu ^ ((~BCa) & BCe);
  280. }
  281. // copyToState(state, A)
  282. state[0] = Aba;
  283. state[1] = Abe;
  284. state[2] = Abi;
  285. state[3] = Abo;
  286. state[4] = Abu;
  287. state[5] = Aga;
  288. state[6] = Age;
  289. state[7] = Agi;
  290. state[8] = Ago;
  291. state[9] = Agu;
  292. state[10] = Aka;
  293. state[11] = Ake;
  294. state[12] = Aki;
  295. state[13] = Ako;
  296. state[14] = Aku;
  297. state[15] = Ama;
  298. state[16] = Ame;
  299. state[17] = Ami;
  300. state[18] = Amo;
  301. state[19] = Amu;
  302. state[20] = Asa;
  303. state[21] = Ase;
  304. state[22] = Asi;
  305. state[23] = Aso;
  306. state[24] = Asu;
  307. }
  308. /*************************************************
  309. * Name: keccak_absorb
  310. *
  311. * Description: Absorb step of Keccak;
  312. * non-incremental, starts by zeroeing the state.
  313. *
  314. * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state
  315. * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
  316. * - const uint8_t *m: pointer to input to be absorbed into s
  317. * - size_t mlen: length of input in bytes
  318. * - uint8_t p: domain-separation byte for different
  319. * Keccak-derived functions
  320. **************************************************/
  321. static void keccak_absorb(uint64_t *s, uint32_t r, const uint8_t *m,
  322. size_t mlen, uint8_t p) {
  323. size_t i;
  324. uint8_t t[200];
  325. /* Zero state */
  326. for (i = 0; i < 25; ++i) {
  327. s[i] = 0;
  328. }
  329. while (mlen >= r) {
  330. for (i = 0; i < r / 8; ++i) {
  331. s[i] ^= load64(m + 8 * i);
  332. }
  333. KeccakF1600_StatePermute(s);
  334. mlen -= r;
  335. m += r;
  336. }
  337. for (i = 0; i < r; ++i) {
  338. t[i] = 0;
  339. }
  340. for (i = 0; i < mlen; ++i) {
  341. t[i] = m[i];
  342. }
  343. t[i] = p;
  344. t[r - 1] |= 128;
  345. for (i = 0; i < r / 8; ++i) {
  346. s[i] ^= load64(t + 8 * i);
  347. }
  348. }
  349. /*************************************************
  350. * Name: keccak_squeezeblocks
  351. *
  352. * Description: Squeeze step of Keccak. Squeezes full blocks of r bytes each.
  353. * Modifies the state. Can be called multiple times to keep
  354. * squeezing, i.e., is incremental.
  355. *
  356. * Arguments: - uint8_t *h: pointer to output blocks
  357. * - size_t nblocks: number of blocks to be
  358. * squeezed (written to h)
  359. * - uint64_t *s: pointer to input/output Keccak state
  360. * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
  361. **************************************************/
  362. static void keccak_squeezeblocks(uint8_t *h, size_t nblocks,
  363. uint64_t *s, uint32_t r) {
  364. while (nblocks > 0) {
  365. KeccakF1600_StatePermute(s);
  366. for (size_t i = 0; i < (r >> 3); i++) {
  367. store64(h + 8 * i, s[i]);
  368. }
  369. h += r;
  370. nblocks--;
  371. }
  372. }
  373. /*************************************************
  374. * Name: shake128_absorb
  375. *
  376. * Description: Absorb step of the SHAKE128 XOF.
  377. * non-incremental, starts by zeroeing the state.
  378. *
  379. * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state
  380. * - const uint8_t *input: pointer to input to be absorbed
  381. * into s
  382. * - size_t inlen: length of input in bytes
  383. **************************************************/
  384. void shake128_absorb(uint64_t *s, const uint8_t *input, size_t inlen) {
  385. keccak_absorb(s, SHAKE128_RATE, input, inlen, 0x1F);
  386. }
  387. /*************************************************
  388. * Name: shake128_squeezeblocks
  389. *
  390. * Description: Squeeze step of SHAKE128 XOF. Squeezes full blocks of
  391. * SHAKE128_RATE bytes each. Modifies the state. Can be called
  392. * multiple times to keep squeezing, i.e., is incremental.
  393. *
  394. * Arguments: - uint8_t *output: pointer to output blocks
  395. * - size_t nblocks: number of blocks to be squeezed
  396. * (written to output)
  397. * - uint64_t *s: pointer to input/output Keccak state
  398. **************************************************/
  399. void shake128_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s) {
  400. keccak_squeezeblocks(output, nblocks, s, SHAKE128_RATE);
  401. }
  402. /*************************************************
  403. * Name: shake256_absorb
  404. *
  405. * Description: Absorb step of the SHAKE256 XOF.
  406. * non-incremental, starts by zeroeing the state.
  407. *
  408. * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state
  409. * - const uint8_t *input: pointer to input to be absorbed
  410. * into s
  411. * - size_t inlen: length of input in bytes
  412. **************************************************/
  413. void shake256_absorb(uint64_t *s, const uint8_t *input, size_t inlen) {
  414. keccak_absorb(s, SHAKE256_RATE, input, inlen, 0x1F);
  415. }
  416. /*************************************************
  417. * Name: shake256_squeezeblocks
  418. *
  419. * Description: Squeeze step of SHAKE256 XOF. Squeezes full blocks of
  420. * SHAKE256_RATE bytes each. Modifies the state. Can be called
  421. * multiple times to keep squeezing, i.e., is incremental.
  422. *
  423. * Arguments: - uint8_t *output: pointer to output blocks
  424. * - size_t nblocks: number of blocks to be squeezed
  425. * (written to output)
  426. * - uint64_t *s: pointer to input/output Keccak state
  427. **************************************************/
  428. void shake256_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s) {
  429. keccak_squeezeblocks(output, nblocks, s, SHAKE256_RATE);
  430. }
  431. /*************************************************
  432. * Name: shake128
  433. *
  434. * Description: SHAKE128 XOF with non-incremental API
  435. *
  436. * Arguments: - uint8_t *output: pointer to output
  437. * - size_t outlen: requested output length in bytes
  438. * - const uint8_t *input: pointer to input
  439. * - size_t inlen: length of input in bytes
  440. **************************************************/
  441. void shake128(uint8_t *output, size_t outlen,
  442. const uint8_t *input, size_t inlen) {
  443. size_t nblocks = outlen / SHAKE128_RATE;
  444. uint8_t t[SHAKE128_RATE];
  445. uint64_t s[25];
  446. shake128_absorb(s, input, inlen);
  447. shake128_squeezeblocks(output, nblocks, s);
  448. output += nblocks * SHAKE128_RATE;
  449. outlen -= nblocks * SHAKE128_RATE;
  450. if (outlen) {
  451. shake128_squeezeblocks(t, 1, s);
  452. for (size_t i = 0; i < outlen; ++i) {
  453. output[i] = t[i];
  454. }
  455. }
  456. }
  457. /*************************************************
  458. * Name: shake256
  459. *
  460. * Description: SHAKE256 XOF with non-incremental API
  461. *
  462. * Arguments: - uint8_t *output: pointer to output
  463. * - size_t outlen: requested output length in bytes
  464. * - const uint8_t *input: pointer to input
  465. * - size_t inlen: length of input in bytes
  466. **************************************************/
  467. void shake256(uint8_t *output, size_t outlen,
  468. const uint8_t *input, size_t inlen) {
  469. size_t nblocks = outlen / SHAKE256_RATE;
  470. uint8_t t[SHAKE256_RATE];
  471. uint64_t s[25];
  472. shake256_absorb(s, input, inlen);
  473. shake256_squeezeblocks(output, nblocks, s);
  474. output += nblocks * SHAKE256_RATE;
  475. outlen -= nblocks * SHAKE256_RATE;
  476. if (outlen) {
  477. shake256_squeezeblocks(t, 1, s);
  478. for (size_t i = 0; i < outlen; ++i) {
  479. output[i] = t[i];
  480. }
  481. }
  482. }
  483. /*************************************************
  484. * Name: sha3_256
  485. *
  486. * Description: SHA3-256 with non-incremental API
  487. *
  488. * Arguments: - uint8_t *output: pointer to output
  489. * - const uint8_t *input: pointer to input
  490. * - size_t inlen: length of input in bytes
  491. **************************************************/
  492. void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen) {
  493. uint64_t s[25];
  494. uint8_t t[SHA3_256_RATE];
  495. /* Absorb input */
  496. keccak_absorb(s, SHA3_256_RATE, input, inlen, 0x06);
  497. /* Squeeze output */
  498. keccak_squeezeblocks(t, 1, s, SHA3_256_RATE);
  499. for (size_t i = 0; i < 32; i++) {
  500. output[i] = t[i];
  501. }
  502. }
  503. /*************************************************
  504. * Name: sha3_512
  505. *
  506. * Description: SHA3-512 with non-incremental API
  507. *
  508. * Arguments: - uint8_t *output: pointer to output
  509. * - const uint8_t *input: pointer to input
  510. * - size_t inlen: length of input in bytes
  511. **************************************************/
  512. void sha3_512(uint8_t *output, const uint8_t *input, size_t inlen) {
  513. uint64_t s[25];
  514. uint8_t t[SHA3_512_RATE];
  515. /* Absorb input */
  516. keccak_absorb(s, SHA3_512_RATE, input, inlen, 0x06);
  517. /* Squeeze output */
  518. keccak_squeezeblocks(t, 1, s, SHA3_512_RATE);
  519. for (size_t i = 0; i < 64; i++) {
  520. output[i] = t[i];
  521. }
  522. }