@pinkparrot/qsafe-mayo-wasm 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/.gitmodules +3 -0
  2. package/.vscode/launch.json +12 -0
  3. package/LICENSE +201 -0
  4. package/bridge/mayo1_bridge.c +26 -0
  5. package/bridge/mayo2_bridge.c +26 -0
  6. package/bridge/randombytes_inject.c +44 -0
  7. package/build_mayo1.ps1 +36 -0
  8. package/build_mayo2.ps1 +36 -0
  9. package/dist/mayo.browser.min.js +216 -0
  10. package/dist/mayo1.js +0 -0
  11. package/dist/mayo2.js +0 -0
  12. package/dist/mayo_api.js +139 -0
  13. package/dist/package.json +1 -0
  14. package/gitignore +2 -0
  15. package/index.mjs +1 -0
  16. package/mayo-c/.astylerc +16 -0
  17. package/mayo-c/.cmake/flags.cmake +45 -0
  18. package/mayo-c/.cmake/sanitizers.cmake +81 -0
  19. package/mayo-c/.cmake/target.cmake +71 -0
  20. package/mayo-c/.github/workflows/ci_clang.yml +61 -0
  21. package/mayo-c/.github/workflows/ci_gcc.yml +60 -0
  22. package/mayo-c/.github/workflows/cmake.yml +160 -0
  23. package/mayo-c/.github/workflows/macos_m1.yml +68 -0
  24. package/mayo-c/CMakeLists.txt +35 -0
  25. package/mayo-c/KAT/PQCsignKAT_24_MAYO_1.req +900 -0
  26. package/mayo-c/KAT/PQCsignKAT_24_MAYO_1.rsp +902 -0
  27. package/mayo-c/KAT/PQCsignKAT_24_MAYO_2.req +900 -0
  28. package/mayo-c/KAT/PQCsignKAT_24_MAYO_2.rsp +902 -0
  29. package/mayo-c/KAT/PQCsignKAT_32_MAYO_3.req +900 -0
  30. package/mayo-c/KAT/PQCsignKAT_32_MAYO_3.rsp +902 -0
  31. package/mayo-c/KAT/PQCsignKAT_40_MAYO_5.req +900 -0
  32. package/mayo-c/KAT/PQCsignKAT_40_MAYO_5.rsp +902 -0
  33. package/mayo-c/LICENSE +202 -0
  34. package/mayo-c/META/MAYO-1_META.yml +52 -0
  35. package/mayo-c/META/MAYO-2_META.yml +52 -0
  36. package/mayo-c/META/MAYO-3_META.yml +52 -0
  37. package/mayo-c/META/MAYO-5_META.yml +52 -0
  38. package/mayo-c/NOTICE +13 -0
  39. package/mayo-c/README.md +183 -0
  40. package/mayo-c/apps/CMakeLists.txt +31 -0
  41. package/mayo-c/apps/PQCgenKAT_sign.c +281 -0
  42. package/mayo-c/apps/example.c +151 -0
  43. package/mayo-c/apps/example_nistapi.c +124 -0
  44. package/mayo-c/include/mayo.h +442 -0
  45. package/mayo-c/include/mem.h +25 -0
  46. package/mayo-c/include/randombytes.h +31 -0
  47. package/mayo-c/scripts/contstants.py +141 -0
  48. package/mayo-c/scripts/find_irred_poly.sage +39 -0
  49. package/mayo-c/src/AVX2/arithmetic_common.h +159 -0
  50. package/mayo-c/src/AVX2/echelon_form.h +91 -0
  51. package/mayo-c/src/AVX2/echelon_form_loop.h +58 -0
  52. package/mayo-c/src/AVX2/shuffle_arithmetic.h +442 -0
  53. package/mayo-c/src/CMakeLists.txt +98 -0
  54. package/mayo-c/src/arithmetic.c +128 -0
  55. package/mayo-c/src/arithmetic.h +124 -0
  56. package/mayo-c/src/common/aes128ctr.c +293 -0
  57. package/mayo-c/src/common/aes_c.c +741 -0
  58. package/mayo-c/src/common/aes_ctr.h +32 -0
  59. package/mayo-c/src/common/aes_neon.c +201 -0
  60. package/mayo-c/src/common/debug_bench_tools.h +69 -0
  61. package/mayo-c/src/common/fips202.c +1093 -0
  62. package/mayo-c/src/common/fips202.h +12 -0
  63. package/mayo-c/src/common/mem.c +19 -0
  64. package/mayo-c/src/common/randombytes_ctrdrbg.c +141 -0
  65. package/mayo-c/src/common/randombytes_system.c +399 -0
  66. package/mayo-c/src/generic/arithmetic_dynamic.h +68 -0
  67. package/mayo-c/src/generic/arithmetic_fixed.h +84 -0
  68. package/mayo-c/src/generic/echelon_form.h +152 -0
  69. package/mayo-c/src/generic/ef_inner_loop.h +56 -0
  70. package/mayo-c/src/generic/generic_arithmetic.h +294 -0
  71. package/mayo-c/src/mayo.c +675 -0
  72. package/mayo-c/src/mayo_1/api.c +46 -0
  73. package/mayo-c/src/mayo_1/api.h +43 -0
  74. package/mayo-c/src/mayo_2/api.c +46 -0
  75. package/mayo-c/src/mayo_2/api.h +43 -0
  76. package/mayo-c/src/mayo_3/api.c +46 -0
  77. package/mayo-c/src/mayo_3/api.h +43 -0
  78. package/mayo-c/src/mayo_5/api.c +46 -0
  79. package/mayo-c/src/mayo_5/api.h +43 -0
  80. package/mayo-c/src/neon/arithmetic_common.h +132 -0
  81. package/mayo-c/src/neon/echelon_form.h +55 -0
  82. package/mayo-c/src/neon/echelon_form_loop.h +58 -0
  83. package/mayo-c/src/neon/shuffle_arithmetic.h +462 -0
  84. package/mayo-c/src/params.c +42 -0
  85. package/mayo-c/src/simple_arithmetic.h +138 -0
  86. package/mayo-c/test/CMakeLists.txt +51 -0
  87. package/mayo-c/test/bench.c +166 -0
  88. package/mayo-c/test/m1cycles.c +155 -0
  89. package/mayo-c/test/m1cycles.h +13 -0
  90. package/mayo-c/test/test_kat.c +271 -0
  91. package/mayo-c/test/test_mayo.c +139 -0
  92. package/mayo-c/test/test_sample_solution.c +75 -0
  93. package/mayo-c/test/test_various.c +680 -0
  94. package/package.json +39 -0
  95. package/publish.bat +22 -0
  96. package/readme.md +80 -0
  97. package/test/test.mjs +42 -0
@@ -0,0 +1,1093 @@
1
+ // SPDX-License-Identifier: Public Domain and Apache-2.0
2
+
3
+ /* AES implementation based on code from PQClean,
4
+ * which is in turn based based on the public domain implementation in
5
+ * crypto_hash/keccakc512/simple/ from http://bench.cr.yp.to/supercop.html
6
+ * by Ronny Van Keer
7
+ * and the public domain "TweetFips202" implementation
8
+ * from https://twitter.com/tweetfips202
9
+ * by Gilles Van Assche, Daniel J. Bernstein, and Peter Schwabe */
10
+
11
+ #include <stddef.h>
12
+ #include <stdint.h>
13
+ #include <stdlib.h>
14
+ #include <string.h>
15
+
16
+ #include <stddef.h>
17
+ #include <stdint.h>
18
+
19
+ #define SHAKE128_RATE 168
20
+ #define SHAKE256_RATE 136
21
+ #define SHA3_256_RATE 136
22
+ #define SHA3_384_RATE 104
23
+ #define SHA3_512_RATE 72
24
+
25
+ #define PQC_SHAKEINCCTX_BYTES (sizeof(uint64_t)*26)
26
+ #define PQC_SHAKECTX_BYTES (sizeof(uint64_t)*25)
27
+
28
+ // Context for incremental API
29
+ typedef struct {
30
+ uint64_t *ctx;
31
+ } shake128incctx;
32
+
33
+ // Context for non-incremental API
34
+ typedef struct {
35
+ uint64_t *ctx;
36
+ } shake128ctx;
37
+
38
+ // Context for incremental API
39
+ typedef struct {
40
+ uint64_t *ctx;
41
+ } shake256incctx;
42
+
43
+ // Context for non-incremental API
44
+ typedef struct {
45
+ uint64_t *ctx;
46
+ } shake256ctx;
47
+
48
+ // Context for incremental API
49
+ typedef struct {
50
+ uint64_t *ctx;
51
+ } sha3_256incctx;
52
+
53
+ // Context for incremental API
54
+ typedef struct {
55
+ uint64_t *ctx;
56
+ } sha3_384incctx;
57
+
58
+ // Context for incremental API
59
+ typedef struct {
60
+ uint64_t *ctx;
61
+ } sha3_512incctx;
62
+
63
+ /* Initialize the state and absorb the provided input.
64
+ *
65
+ * This function does not support being called multiple times
66
+ * with the same state.
67
+ */
68
+ void shake128_absorb(shake128ctx *state, const uint8_t *input, size_t inlen);
69
+ /* Squeeze output out of the sponge.
70
+ *
71
+ * Supports being called multiple times
72
+ */
73
+ void shake128_squeezeblocks(uint8_t *output, size_t nblocks, shake128ctx *state);
74
+ /* Free the state */
75
+ void shake128_ctx_release(shake128ctx *state);
76
+ /* Copy the state. */
77
+ void shake128_ctx_clone(shake128ctx *dest, const shake128ctx *src);
78
+
79
+ /* Initialize incremental hashing API */
80
+ void shake128_inc_init(shake128incctx *state);
81
+ /* Absorb more information into the XOF.
82
+ *
83
+ * Can be called multiple times.
84
+ */
85
+ void shake128_inc_absorb(shake128incctx *state, const uint8_t *input, size_t inlen);
86
+ /* Finalize the XOF for squeezing */
87
+ void shake128_inc_finalize(shake128incctx *state);
88
+ /* Squeeze output out of the sponge.
89
+ *
90
+ * Supports being called multiple times
91
+ */
92
+ void shake128_inc_squeeze(uint8_t *output, size_t outlen, shake128incctx *state);
93
+ /* Copy the context of the SHAKE128 XOF */
94
+ void shake128_inc_ctx_clone(shake128incctx *dest, const shake128incctx *src);
95
+ /* Free the context of the SHAKE128 XOF */
96
+ void shake128_inc_ctx_release(shake128incctx *state);
97
+
98
+ /* Initialize the state and absorb the provided input.
99
+ *
100
+ * This function does not support being called multiple times
101
+ * with the same state.
102
+ */
103
+ void shake256_absorb(shake256ctx *state, const uint8_t *input, size_t inlen);
104
+ /* Squeeze output out of the sponge.
105
+ *
106
+ * Supports being called multiple times
107
+ */
108
+ void shake256_squeezeblocks(uint8_t *output, size_t nblocks, shake256ctx *state);
109
+ /* Free the context held by this XOF */
110
+ void shake256_ctx_release(shake256ctx *state);
111
+ /* Copy the context held by this XOF */
112
+ void shake256_ctx_clone(shake256ctx *dest, const shake256ctx *src);
113
+
114
+ /* Initialize incremental hashing API */
115
+ void shake256_inc_init(shake256incctx *state);
116
+ void shake256_inc_absorb(shake256incctx *state, const uint8_t *input, size_t inlen);
117
+ /* Prepares for squeeze phase */
118
+ void shake256_inc_finalize(shake256incctx *state);
119
+ /* Squeeze output out of the sponge.
120
+ *
121
+ * Supports being called multiple times
122
+ */
123
+ void shake256_inc_squeeze(uint8_t *output, size_t outlen, shake256incctx *state);
124
+ /* Copy the state */
125
+ void shake256_inc_ctx_clone(shake256incctx *dest, const shake256incctx *src);
126
+ /* Free the state */
127
+ void shake256_inc_ctx_release(shake256incctx *state);
128
+
129
+ /* One-stop SHAKE128 call */
130
+ void shake128(uint8_t *output, size_t outlen,
131
+ const uint8_t *input, size_t inlen);
132
+
133
+ /* One-stop SHAKE256 call */
134
+ void shake256(uint8_t *output, size_t outlen,
135
+ const uint8_t *input, size_t inlen);
136
+
137
+ /* Initialize the incremental hashing state */
138
+ void sha3_256_inc_init(sha3_256incctx *state);
139
+ /* Absorb blocks into SHA3 */
140
+ void sha3_256_inc_absorb(sha3_256incctx *state, const uint8_t *input, size_t inlen);
141
+ /* Obtain the output of the function and free `state` */
142
+ void sha3_256_inc_finalize(uint8_t *output, sha3_256incctx *state);
143
+ /* Copy the context */
144
+ void sha3_256_inc_ctx_clone(sha3_256incctx *dest, const sha3_256incctx *src);
145
+ /* Release the state, don't use if `_finalize` has been used */
146
+ void sha3_256_inc_ctx_release(sha3_256incctx *state);
147
+
148
+ void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen);
149
+
150
+ /* Initialize the incremental hashing state */
151
+ void sha3_384_inc_init(sha3_384incctx *state);
152
+ /* Absorb blocks into SHA3 */
153
+ void sha3_384_inc_absorb(sha3_384incctx *state, const uint8_t *input, size_t inlen);
154
+ /* Obtain the output of the function and free `state` */
155
+ void sha3_384_inc_finalize(uint8_t *output, sha3_384incctx *state);
156
+ /* Copy the context */
157
+ void sha3_384_inc_ctx_clone(sha3_384incctx *dest, const sha3_384incctx *src);
158
+ /* Release the state, don't use if `_finalize` has been used */
159
+ void sha3_384_inc_ctx_release(sha3_384incctx *state);
160
+
161
+ /* One-stop SHA3-384 shop */
162
+ void sha3_384(uint8_t *output, const uint8_t *input, size_t inlen);
163
+
164
+ /* Initialize the incremental hashing state */
165
+ void sha3_512_inc_init(sha3_512incctx *state);
166
+ /* Absorb blocks into SHA3 */
167
+ void sha3_512_inc_absorb(sha3_512incctx *state, const uint8_t *input, size_t inlen);
168
+ /* Obtain the output of the function and free `state` */
169
+ void sha3_512_inc_finalize(uint8_t *output, sha3_512incctx *state);
170
+ /* Copy the context */
171
+ void sha3_512_inc_ctx_clone(sha3_512incctx *dest, const sha3_512incctx *src);
172
+ /* Release the state, don't use if `_finalize` has been used */
173
+ void sha3_512_inc_ctx_release(sha3_512incctx *state);
174
+
175
+ /* One-stop SHA3-512 shop */
176
+ void sha3_512(uint8_t *output, const uint8_t *input, size_t inlen);
177
+
178
+ #define NROUNDS 24
179
+ #define ROL(a, offset) (((a) << (offset)) ^ ((a) >> (64 - (offset))))
180
+
181
+ /*************************************************
182
+ * Name: load64
183
+ *
184
+ * Description: Load 8 bytes into uint64_t in little-endian order
185
+ *
186
+ * Arguments: - const uint8_t *x: pointer to input byte array
187
+ *
188
+ * Returns the loaded 64-bit unsigned integer
189
+ **************************************************/
190
+ static uint64_t load64(const uint8_t *x) {
191
+ uint64_t r = 0;
192
+ for (size_t i = 0; i < 8; ++i) {
193
+ r |= (uint64_t)x[i] << 8 * i;
194
+ }
195
+
196
+ return r;
197
+ }
198
+
199
+ /*************************************************
200
+ * Name: store64
201
+ *
202
+ * Description: Store a 64-bit integer to a byte array in little-endian order
203
+ *
204
+ * Arguments: - uint8_t *x: pointer to the output byte array
205
+ * - uint64_t u: input 64-bit unsigned integer
206
+ **************************************************/
207
+ static void store64(uint8_t *x, uint64_t u) {
208
+ for (size_t i = 0; i < 8; ++i) {
209
+ x[i] = (uint8_t) (u >> 8 * i);
210
+ }
211
+ }
212
+
213
+ /* Keccak round constants */
214
+ static const uint64_t KeccakF_RoundConstants[NROUNDS] = {
215
+ 0x0000000000000001ULL, 0x0000000000008082ULL,
216
+ 0x800000000000808aULL, 0x8000000080008000ULL,
217
+ 0x000000000000808bULL, 0x0000000080000001ULL,
218
+ 0x8000000080008081ULL, 0x8000000000008009ULL,
219
+ 0x000000000000008aULL, 0x0000000000000088ULL,
220
+ 0x0000000080008009ULL, 0x000000008000000aULL,
221
+ 0x000000008000808bULL, 0x800000000000008bULL,
222
+ 0x8000000000008089ULL, 0x8000000000008003ULL,
223
+ 0x8000000000008002ULL, 0x8000000000000080ULL,
224
+ 0x000000000000800aULL, 0x800000008000000aULL,
225
+ 0x8000000080008081ULL, 0x8000000000008080ULL,
226
+ 0x0000000080000001ULL, 0x8000000080008008ULL
227
+ };
228
+
229
+ /*************************************************
230
+ * Name: KeccakF1600_StatePermute
231
+ *
232
+ * Description: The Keccak F1600 Permutation
233
+ *
234
+ * Arguments: - uint64_t *state: pointer to input/output Keccak state
235
+ **************************************************/
236
+ static void KeccakF1600_StatePermute(uint64_t *state) {
237
+ int round;
238
+
239
+ uint64_t Aba, Abe, Abi, Abo, Abu;
240
+ uint64_t Aga, Age, Agi, Ago, Agu;
241
+ uint64_t Aka, Ake, Aki, Ako, Aku;
242
+ uint64_t Ama, Ame, Ami, Amo, Amu;
243
+ uint64_t Asa, Ase, Asi, Aso, Asu;
244
+ uint64_t BCa, BCe, BCi, BCo, BCu;
245
+ uint64_t Da, De, Di, Do, Du;
246
+ uint64_t Eba, Ebe, Ebi, Ebo, Ebu;
247
+ uint64_t Ega, Ege, Egi, Ego, Egu;
248
+ uint64_t Eka, Eke, Eki, Eko, Eku;
249
+ uint64_t Ema, Eme, Emi, Emo, Emu;
250
+ uint64_t Esa, Ese, Esi, Eso, Esu;
251
+
252
+ // copyFromState(A, state)
253
+ Aba = state[0];
254
+ Abe = state[1];
255
+ Abi = state[2];
256
+ Abo = state[3];
257
+ Abu = state[4];
258
+ Aga = state[5];
259
+ Age = state[6];
260
+ Agi = state[7];
261
+ Ago = state[8];
262
+ Agu = state[9];
263
+ Aka = state[10];
264
+ Ake = state[11];
265
+ Aki = state[12];
266
+ Ako = state[13];
267
+ Aku = state[14];
268
+ Ama = state[15];
269
+ Ame = state[16];
270
+ Ami = state[17];
271
+ Amo = state[18];
272
+ Amu = state[19];
273
+ Asa = state[20];
274
+ Ase = state[21];
275
+ Asi = state[22];
276
+ Aso = state[23];
277
+ Asu = state[24];
278
+
279
+ for (round = 0; round < NROUNDS; round += 2) {
280
+ // prepareTheta
281
+ BCa = Aba ^ Aga ^ Aka ^ Ama ^ Asa;
282
+ BCe = Abe ^ Age ^ Ake ^ Ame ^ Ase;
283
+ BCi = Abi ^ Agi ^ Aki ^ Ami ^ Asi;
284
+ BCo = Abo ^ Ago ^ Ako ^ Amo ^ Aso;
285
+ BCu = Abu ^ Agu ^ Aku ^ Amu ^ Asu;
286
+
287
+ // thetaRhoPiChiIotaPrepareTheta(round , A, E)
288
+ Da = BCu ^ ROL(BCe, 1);
289
+ De = BCa ^ ROL(BCi, 1);
290
+ Di = BCe ^ ROL(BCo, 1);
291
+ Do = BCi ^ ROL(BCu, 1);
292
+ Du = BCo ^ ROL(BCa, 1);
293
+
294
+ Aba ^= Da;
295
+ BCa = Aba;
296
+ Age ^= De;
297
+ BCe = ROL(Age, 44);
298
+ Aki ^= Di;
299
+ BCi = ROL(Aki, 43);
300
+ Amo ^= Do;
301
+ BCo = ROL(Amo, 21);
302
+ Asu ^= Du;
303
+ BCu = ROL(Asu, 14);
304
+ Eba = BCa ^ ((~BCe) & BCi);
305
+ Eba ^= KeccakF_RoundConstants[round];
306
+ Ebe = BCe ^ ((~BCi) & BCo);
307
+ Ebi = BCi ^ ((~BCo) & BCu);
308
+ Ebo = BCo ^ ((~BCu) & BCa);
309
+ Ebu = BCu ^ ((~BCa) & BCe);
310
+
311
+ Abo ^= Do;
312
+ BCa = ROL(Abo, 28);
313
+ Agu ^= Du;
314
+ BCe = ROL(Agu, 20);
315
+ Aka ^= Da;
316
+ BCi = ROL(Aka, 3);
317
+ Ame ^= De;
318
+ BCo = ROL(Ame, 45);
319
+ Asi ^= Di;
320
+ BCu = ROL(Asi, 61);
321
+ Ega = BCa ^ ((~BCe) & BCi);
322
+ Ege = BCe ^ ((~BCi) & BCo);
323
+ Egi = BCi ^ ((~BCo) & BCu);
324
+ Ego = BCo ^ ((~BCu) & BCa);
325
+ Egu = BCu ^ ((~BCa) & BCe);
326
+
327
+ Abe ^= De;
328
+ BCa = ROL(Abe, 1);
329
+ Agi ^= Di;
330
+ BCe = ROL(Agi, 6);
331
+ Ako ^= Do;
332
+ BCi = ROL(Ako, 25);
333
+ Amu ^= Du;
334
+ BCo = ROL(Amu, 8);
335
+ Asa ^= Da;
336
+ BCu = ROL(Asa, 18);
337
+ Eka = BCa ^ ((~BCe) & BCi);
338
+ Eke = BCe ^ ((~BCi) & BCo);
339
+ Eki = BCi ^ ((~BCo) & BCu);
340
+ Eko = BCo ^ ((~BCu) & BCa);
341
+ Eku = BCu ^ ((~BCa) & BCe);
342
+
343
+ Abu ^= Du;
344
+ BCa = ROL(Abu, 27);
345
+ Aga ^= Da;
346
+ BCe = ROL(Aga, 36);
347
+ Ake ^= De;
348
+ BCi = ROL(Ake, 10);
349
+ Ami ^= Di;
350
+ BCo = ROL(Ami, 15);
351
+ Aso ^= Do;
352
+ BCu = ROL(Aso, 56);
353
+ Ema = BCa ^ ((~BCe) & BCi);
354
+ Eme = BCe ^ ((~BCi) & BCo);
355
+ Emi = BCi ^ ((~BCo) & BCu);
356
+ Emo = BCo ^ ((~BCu) & BCa);
357
+ Emu = BCu ^ ((~BCa) & BCe);
358
+
359
+ Abi ^= Di;
360
+ BCa = ROL(Abi, 62);
361
+ Ago ^= Do;
362
+ BCe = ROL(Ago, 55);
363
+ Aku ^= Du;
364
+ BCi = ROL(Aku, 39);
365
+ Ama ^= Da;
366
+ BCo = ROL(Ama, 41);
367
+ Ase ^= De;
368
+ BCu = ROL(Ase, 2);
369
+ Esa = BCa ^ ((~BCe) & BCi);
370
+ Ese = BCe ^ ((~BCi) & BCo);
371
+ Esi = BCi ^ ((~BCo) & BCu);
372
+ Eso = BCo ^ ((~BCu) & BCa);
373
+ Esu = BCu ^ ((~BCa) & BCe);
374
+
375
+ // prepareTheta
376
+ BCa = Eba ^ Ega ^ Eka ^ Ema ^ Esa;
377
+ BCe = Ebe ^ Ege ^ Eke ^ Eme ^ Ese;
378
+ BCi = Ebi ^ Egi ^ Eki ^ Emi ^ Esi;
379
+ BCo = Ebo ^ Ego ^ Eko ^ Emo ^ Eso;
380
+ BCu = Ebu ^ Egu ^ Eku ^ Emu ^ Esu;
381
+
382
+ // thetaRhoPiChiIotaPrepareTheta(round+1, E, A)
383
+ Da = BCu ^ ROL(BCe, 1);
384
+ De = BCa ^ ROL(BCi, 1);
385
+ Di = BCe ^ ROL(BCo, 1);
386
+ Do = BCi ^ ROL(BCu, 1);
387
+ Du = BCo ^ ROL(BCa, 1);
388
+
389
+ Eba ^= Da;
390
+ BCa = Eba;
391
+ Ege ^= De;
392
+ BCe = ROL(Ege, 44);
393
+ Eki ^= Di;
394
+ BCi = ROL(Eki, 43);
395
+ Emo ^= Do;
396
+ BCo = ROL(Emo, 21);
397
+ Esu ^= Du;
398
+ BCu = ROL(Esu, 14);
399
+ Aba = BCa ^ ((~BCe) & BCi);
400
+ Aba ^= KeccakF_RoundConstants[round + 1];
401
+ Abe = BCe ^ ((~BCi) & BCo);
402
+ Abi = BCi ^ ((~BCo) & BCu);
403
+ Abo = BCo ^ ((~BCu) & BCa);
404
+ Abu = BCu ^ ((~BCa) & BCe);
405
+
406
+ Ebo ^= Do;
407
+ BCa = ROL(Ebo, 28);
408
+ Egu ^= Du;
409
+ BCe = ROL(Egu, 20);
410
+ Eka ^= Da;
411
+ BCi = ROL(Eka, 3);
412
+ Eme ^= De;
413
+ BCo = ROL(Eme, 45);
414
+ Esi ^= Di;
415
+ BCu = ROL(Esi, 61);
416
+ Aga = BCa ^ ((~BCe) & BCi);
417
+ Age = BCe ^ ((~BCi) & BCo);
418
+ Agi = BCi ^ ((~BCo) & BCu);
419
+ Ago = BCo ^ ((~BCu) & BCa);
420
+ Agu = BCu ^ ((~BCa) & BCe);
421
+
422
+ Ebe ^= De;
423
+ BCa = ROL(Ebe, 1);
424
+ Egi ^= Di;
425
+ BCe = ROL(Egi, 6);
426
+ Eko ^= Do;
427
+ BCi = ROL(Eko, 25);
428
+ Emu ^= Du;
429
+ BCo = ROL(Emu, 8);
430
+ Esa ^= Da;
431
+ BCu = ROL(Esa, 18);
432
+ Aka = BCa ^ ((~BCe) & BCi);
433
+ Ake = BCe ^ ((~BCi) & BCo);
434
+ Aki = BCi ^ ((~BCo) & BCu);
435
+ Ako = BCo ^ ((~BCu) & BCa);
436
+ Aku = BCu ^ ((~BCa) & BCe);
437
+
438
+ Ebu ^= Du;
439
+ BCa = ROL(Ebu, 27);
440
+ Ega ^= Da;
441
+ BCe = ROL(Ega, 36);
442
+ Eke ^= De;
443
+ BCi = ROL(Eke, 10);
444
+ Emi ^= Di;
445
+ BCo = ROL(Emi, 15);
446
+ Eso ^= Do;
447
+ BCu = ROL(Eso, 56);
448
+ Ama = BCa ^ ((~BCe) & BCi);
449
+ Ame = BCe ^ ((~BCi) & BCo);
450
+ Ami = BCi ^ ((~BCo) & BCu);
451
+ Amo = BCo ^ ((~BCu) & BCa);
452
+ Amu = BCu ^ ((~BCa) & BCe);
453
+
454
+ Ebi ^= Di;
455
+ BCa = ROL(Ebi, 62);
456
+ Ego ^= Do;
457
+ BCe = ROL(Ego, 55);
458
+ Eku ^= Du;
459
+ BCi = ROL(Eku, 39);
460
+ Ema ^= Da;
461
+ BCo = ROL(Ema, 41);
462
+ Ese ^= De;
463
+ BCu = ROL(Ese, 2);
464
+ Asa = BCa ^ ((~BCe) & BCi);
465
+ Ase = BCe ^ ((~BCi) & BCo);
466
+ Asi = BCi ^ ((~BCo) & BCu);
467
+ Aso = BCo ^ ((~BCu) & BCa);
468
+ Asu = BCu ^ ((~BCa) & BCe);
469
+ }
470
+
471
+ // copyToState(state, A)
472
+ state[0] = Aba;
473
+ state[1] = Abe;
474
+ state[2] = Abi;
475
+ state[3] = Abo;
476
+ state[4] = Abu;
477
+ state[5] = Aga;
478
+ state[6] = Age;
479
+ state[7] = Agi;
480
+ state[8] = Ago;
481
+ state[9] = Agu;
482
+ state[10] = Aka;
483
+ state[11] = Ake;
484
+ state[12] = Aki;
485
+ state[13] = Ako;
486
+ state[14] = Aku;
487
+ state[15] = Ama;
488
+ state[16] = Ame;
489
+ state[17] = Ami;
490
+ state[18] = Amo;
491
+ state[19] = Amu;
492
+ state[20] = Asa;
493
+ state[21] = Ase;
494
+ state[22] = Asi;
495
+ state[23] = Aso;
496
+ state[24] = Asu;
497
+ }
498
+
499
+ /*************************************************
500
+ * Name: keccak_absorb
501
+ *
502
+ * Description: Absorb step of Keccak;
503
+ * non-incremental, starts by zeroeing the state.
504
+ *
505
+ * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state
506
+ * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
507
+ * - const uint8_t *m: pointer to input to be absorbed into s
508
+ * - size_t mlen: length of input in bytes
509
+ * - uint8_t p: domain-separation byte for different
510
+ * Keccak-derived functions
511
+ **************************************************/
512
+ static void keccak_absorb(uint64_t *s, uint32_t r, const uint8_t *m,
513
+ size_t mlen, uint8_t p) {
514
+ size_t i;
515
+ uint8_t t[200];
516
+
517
+ /* Zero state */
518
+ for (i = 0; i < 25; ++i) {
519
+ s[i] = 0;
520
+ }
521
+
522
+ while (mlen >= r) {
523
+ for (i = 0; i < r / 8; ++i) {
524
+ s[i] ^= load64(m + 8 * i);
525
+ }
526
+
527
+ KeccakF1600_StatePermute(s);
528
+ mlen -= r;
529
+ m += r;
530
+ }
531
+
532
+ for (i = 0; i < r; ++i) {
533
+ t[i] = 0;
534
+ }
535
+ for (i = 0; i < mlen; ++i) {
536
+ t[i] = m[i];
537
+ }
538
+ t[i] = p;
539
+ t[r - 1] |= 128;
540
+ for (i = 0; i < r / 8; ++i) {
541
+ s[i] ^= load64(t + 8 * i);
542
+ }
543
+ }
544
+
545
+ /*************************************************
546
+ * Name: keccak_squeezeblocks
547
+ *
548
+ * Description: Squeeze step of Keccak. Squeezes full blocks of r bytes each.
549
+ * Modifies the state. Can be called multiple times to keep
550
+ * squeezing, i.e., is incremental.
551
+ *
552
+ * Arguments: - uint8_t *h: pointer to output blocks
553
+ * - size_t nblocks: number of blocks to be
554
+ * squeezed (written to h)
555
+ * - uint64_t *s: pointer to input/output Keccak state
556
+ * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
557
+ **************************************************/
558
+ static void keccak_squeezeblocks(uint8_t *h, size_t nblocks,
559
+ uint64_t *s, uint32_t r) {
560
+ while (nblocks > 0) {
561
+ KeccakF1600_StatePermute(s);
562
+ for (size_t i = 0; i < (r >> 3); i++) {
563
+ store64(h + 8 * i, s[i]);
564
+ }
565
+ h += r;
566
+ nblocks--;
567
+ }
568
+ }
569
+
570
+ /*************************************************
571
+ * Name: keccak_inc_init
572
+ *
573
+ * Description: Initializes the incremental Keccak state to zero.
574
+ *
575
+ * Arguments: - uint64_t *s_inc: pointer to input/output incremental state
576
+ * First 25 values represent Keccak state.
577
+ * 26th value represents either the number of absorbed bytes
578
+ * that have not been permuted, or not-yet-squeezed bytes.
579
+ **************************************************/
580
+ static void keccak_inc_init(uint64_t *s_inc) {
581
+ size_t i;
582
+
583
+ for (i = 0; i < 25; ++i) {
584
+ s_inc[i] = 0;
585
+ }
586
+ s_inc[25] = 0;
587
+ }
588
+
589
+ /*************************************************
590
+ * Name: keccak_inc_absorb
591
+ *
592
+ * Description: Incremental keccak absorb
593
+ * Preceded by keccak_inc_init, succeeded by keccak_inc_finalize
594
+ *
595
+ * Arguments: - uint64_t *s_inc: pointer to input/output incremental state
596
+ * First 25 values represent Keccak state.
597
+ * 26th value represents either the number of absorbed bytes
598
+ * that have not been permuted, or not-yet-squeezed bytes.
599
+ * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
600
+ * - const uint8_t *m: pointer to input to be absorbed into s
601
+ * - size_t mlen: length of input in bytes
602
+ **************************************************/
603
+ static void keccak_inc_absorb(uint64_t *s_inc, uint32_t r, const uint8_t *m,
604
+ size_t mlen) {
605
+ size_t i;
606
+
607
+ /* Recall that s_inc[25] is the non-absorbed bytes xored into the state */
608
+ while (mlen + s_inc[25] >= r) {
609
+ for (i = 0; i < r - (uint32_t)s_inc[25]; i++) {
610
+ /* Take the i'th byte from message
611
+ xor with the s_inc[25] + i'th byte of the state; little-endian */
612
+ s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07));
613
+ }
614
+ mlen -= (size_t)(r - s_inc[25]);
615
+ m += r - s_inc[25];
616
+ s_inc[25] = 0;
617
+
618
+ KeccakF1600_StatePermute(s_inc);
619
+ }
620
+
621
+ for (i = 0; i < mlen; i++) {
622
+ s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07));
623
+ }
624
+ s_inc[25] += mlen;
625
+ }
626
+
627
+ /*************************************************
628
+ * Name: keccak_inc_finalize
629
+ *
630
+ * Description: Finalizes Keccak absorb phase, prepares for squeezing
631
+ *
632
+ * Arguments: - uint64_t *s_inc: pointer to input/output incremental state
633
+ * First 25 values represent Keccak state.
634
+ * 26th value represents either the number of absorbed bytes
635
+ * that have not been permuted, or not-yet-squeezed bytes.
636
+ * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
637
+ * - uint8_t p: domain-separation byte for different
638
+ * Keccak-derived functions
639
+ **************************************************/
640
+ static void keccak_inc_finalize(uint64_t *s_inc, uint32_t r, uint8_t p) {
641
+ /* After keccak_inc_absorb, we are guaranteed that s_inc[25] < r,
642
+ so we can always use one more byte for p in the current state. */
643
+ s_inc[s_inc[25] >> 3] ^= (uint64_t)p << (8 * (s_inc[25] & 0x07));
644
+ s_inc[(r - 1) >> 3] ^= (uint64_t)128 << (8 * ((r - 1) & 0x07));
645
+ s_inc[25] = 0;
646
+ }
647
+
648
+ /*************************************************
649
+ * Name: keccak_inc_squeeze
650
+ *
651
+ * Description: Incremental Keccak squeeze; can be called on byte-level
652
+ *
653
+ * Arguments: - uint8_t *h: pointer to output bytes
654
+ * - size_t outlen: number of bytes to be squeezed
655
+ * - uint64_t *s_inc: pointer to input/output incremental state
656
+ * First 25 values represent Keccak state.
657
+ * 26th value represents either the number of absorbed bytes
658
+ * that have not been permuted, or not-yet-squeezed bytes.
659
+ * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128)
660
+ **************************************************/
661
+ static void keccak_inc_squeeze(uint8_t *h, size_t outlen,
662
+ uint64_t *s_inc, uint32_t r) {
663
+ size_t i;
664
+
665
+ /* First consume any bytes we still have sitting around */
666
+ for (i = 0; i < outlen && i < s_inc[25]; i++) {
667
+ /* There are s_inc[25] bytes left, so r - s_inc[25] is the first
668
+ available byte. We consume from there, i.e., up to r. */
669
+ h[i] = (uint8_t)(s_inc[(r - s_inc[25] + i) >> 3] >> (8 * ((r - s_inc[25] + i) & 0x07)));
670
+ }
671
+ h += i;
672
+ outlen -= i;
673
+ s_inc[25] -= i;
674
+
675
+ /* Then squeeze the remaining necessary blocks */
676
+ while (outlen > 0) {
677
+ KeccakF1600_StatePermute(s_inc);
678
+
679
+ for (i = 0; i < outlen && i < r; i++) {
680
+ h[i] = (uint8_t)(s_inc[i >> 3] >> (8 * (i & 0x07)));
681
+ }
682
+ h += i;
683
+ outlen -= i;
684
+ s_inc[25] = r - i;
685
+ }
686
+ }
687
+
688
+ void shake128_inc_init(shake128incctx *state) {
689
+ state->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
690
+ if (state->ctx == NULL) {
691
+ exit(111);
692
+ }
693
+ keccak_inc_init(state->ctx);
694
+ }
695
+
696
+ void shake128_inc_absorb(shake128incctx *state, const uint8_t *input, size_t inlen) {
697
+ keccak_inc_absorb(state->ctx, SHAKE128_RATE, input, inlen);
698
+ }
699
+
700
+ void shake128_inc_finalize(shake128incctx *state) {
701
+ keccak_inc_finalize(state->ctx, SHAKE128_RATE, 0x1F);
702
+ }
703
+
704
+ void shake128_inc_squeeze(uint8_t *output, size_t outlen, shake128incctx *state) {
705
+ keccak_inc_squeeze(output, outlen, state->ctx, SHAKE128_RATE);
706
+ }
707
+
708
+ void shake128_inc_ctx_clone(shake128incctx *dest, const shake128incctx *src) {
709
+ dest->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
710
+ if (dest->ctx == NULL) {
711
+ exit(111);
712
+ }
713
+ memcpy(dest->ctx, src->ctx, PQC_SHAKEINCCTX_BYTES);
714
+ }
715
+
716
+ void shake128_inc_ctx_release(shake128incctx *state) {
717
+ free(state->ctx);
718
+ }
719
+
720
+ void shake256_inc_init(shake256incctx *state) {
721
+ state->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
722
+ if (state->ctx == NULL) {
723
+ exit(111);
724
+ }
725
+ keccak_inc_init(state->ctx);
726
+ }
727
+
728
+ void shake256_inc_absorb(shake256incctx *state, const uint8_t *input, size_t inlen) {
729
+ keccak_inc_absorb(state->ctx, SHAKE256_RATE, input, inlen);
730
+ }
731
+
732
+ void shake256_inc_finalize(shake256incctx *state) {
733
+ keccak_inc_finalize(state->ctx, SHAKE256_RATE, 0x1F);
734
+ }
735
+
736
+ void shake256_inc_squeeze(uint8_t *output, size_t outlen, shake256incctx *state) {
737
+ keccak_inc_squeeze(output, outlen, state->ctx, SHAKE256_RATE);
738
+ }
739
+
740
+ void shake256_inc_ctx_clone(shake256incctx *dest, const shake256incctx *src) {
741
+ dest->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
742
+ if (dest->ctx == NULL) {
743
+ exit(111);
744
+ }
745
+ memcpy(dest->ctx, src->ctx, PQC_SHAKEINCCTX_BYTES);
746
+ }
747
+
748
+ void shake256_inc_ctx_release(shake256incctx *state) {
749
+ free(state->ctx);
750
+ }
751
+
752
+
753
+ /*************************************************
754
+ * Name: shake128_absorb
755
+ *
756
+ * Description: Absorb step of the SHAKE128 XOF.
757
+ * non-incremental, starts by zeroeing the state.
758
+ *
759
+ * Arguments: - uint64_t *s: pointer to (uninitialized) output Keccak state
760
+ * - const uint8_t *input: pointer to input to be absorbed
761
+ * into s
762
+ * - size_t inlen: length of input in bytes
763
+ **************************************************/
764
+ void shake128_absorb(shake128ctx *state, const uint8_t *input, size_t inlen) {
765
+ state->ctx = malloc(PQC_SHAKECTX_BYTES);
766
+ if (state->ctx == NULL) {
767
+ exit(111);
768
+ }
769
+ keccak_absorb(state->ctx, SHAKE128_RATE, input, inlen, 0x1F);
770
+ }
771
+
772
+ /*************************************************
773
+ * Name: shake128_squeezeblocks
774
+ *
775
+ * Description: Squeeze step of SHAKE128 XOF. Squeezes full blocks of
776
+ * SHAKE128_RATE bytes each. Modifies the state. Can be called
777
+ * multiple times to keep squeezing, i.e., is incremental.
778
+ *
779
+ * Arguments: - uint8_t *output: pointer to output blocks
780
+ * - size_t nblocks: number of blocks to be squeezed
781
+ * (written to output)
782
+ * - shake128ctx *state: pointer to input/output Keccak state
783
+ **************************************************/
784
+ void shake128_squeezeblocks(uint8_t *output, size_t nblocks, shake128ctx *state) {
785
+ keccak_squeezeblocks(output, nblocks, state->ctx, SHAKE128_RATE);
786
+ }
787
+
788
+ void shake128_ctx_clone(shake128ctx *dest, const shake128ctx *src) {
789
+ dest->ctx = malloc(PQC_SHAKECTX_BYTES);
790
+ if (dest->ctx == NULL) {
791
+ exit(111);
792
+ }
793
+ memcpy(dest->ctx, src->ctx, PQC_SHAKECTX_BYTES);
794
+ }
795
+
796
+ /** Release the allocated state. Call only once. */
797
+ void shake128_ctx_release(shake128ctx *state) {
798
+ free(state->ctx);
799
+ }
800
+
801
+ /*************************************************
802
+ * Name: shake256_absorb
803
+ *
804
+ * Description: Absorb step of the SHAKE256 XOF.
805
+ * non-incremental, starts by zeroeing the state.
806
+ *
807
+ * Arguments: - shake256ctx *state: pointer to (uninitialized) output Keccak state
808
+ * - const uint8_t *input: pointer to input to be absorbed
809
+ * into s
810
+ * - size_t inlen: length of input in bytes
811
+ **************************************************/
812
+ void shake256_absorb(shake256ctx *state, const uint8_t *input, size_t inlen) {
813
+ state->ctx = malloc(PQC_SHAKECTX_BYTES);
814
+ if (state->ctx == NULL) {
815
+ exit(111);
816
+ }
817
+ keccak_absorb(state->ctx, SHAKE256_RATE, input, inlen, 0x1F);
818
+ }
819
+
820
+ /*************************************************
821
+ * Name: shake256_squeezeblocks
822
+ *
823
+ * Description: Squeeze step of SHAKE256 XOF. Squeezes full blocks of
824
+ * SHAKE256_RATE bytes each. Modifies the state. Can be called
825
+ * multiple times to keep squeezing, i.e., is incremental.
826
+ *
827
+ * Arguments: - uint8_t *output: pointer to output blocks
828
+ * - size_t nblocks: number of blocks to be squeezed
829
+ * (written to output)
830
+ * - shake256ctx *state: pointer to input/output Keccak state
831
+ **************************************************/
832
+ void shake256_squeezeblocks(uint8_t *output, size_t nblocks, shake256ctx *state) {
833
+ keccak_squeezeblocks(output, nblocks, state->ctx, SHAKE256_RATE);
834
+ }
835
+
836
+ void shake256_ctx_clone(shake256ctx *dest, const shake256ctx *src) {
837
+ dest->ctx = malloc(PQC_SHAKECTX_BYTES);
838
+ if (dest->ctx == NULL) {
839
+ exit(111);
840
+ }
841
+ memcpy(dest->ctx, src->ctx, PQC_SHAKECTX_BYTES);
842
+ }
843
+
844
+ /** Release the allocated state. Call only once. */
845
+ void shake256_ctx_release(shake256ctx *state) {
846
+ free(state->ctx);
847
+ }
848
+
849
+ /*************************************************
850
+ * Name: shake128
851
+ *
852
+ * Description: SHAKE128 XOF with non-incremental API
853
+ *
854
+ * Arguments: - uint8_t *output: pointer to output
855
+ * - size_t outlen: requested output length in bytes
856
+ * - const uint8_t *input: pointer to input
857
+ * - size_t inlen: length of input in bytes
858
+ **************************************************/
859
+ void shake128(uint8_t *output, size_t outlen,
860
+ const uint8_t *input, size_t inlen) {
861
+ size_t nblocks = outlen / SHAKE128_RATE;
862
+ uint8_t t[SHAKE128_RATE];
863
+ shake128ctx s;
864
+
865
+ shake128_absorb(&s, input, inlen);
866
+ shake128_squeezeblocks(output, nblocks, &s);
867
+
868
+ output += nblocks * SHAKE128_RATE;
869
+ outlen -= nblocks * SHAKE128_RATE;
870
+
871
+ if (outlen) {
872
+ shake128_squeezeblocks(t, 1, &s);
873
+ for (size_t i = 0; i < outlen; ++i) {
874
+ output[i] = t[i];
875
+ }
876
+ }
877
+ shake128_ctx_release(&s);
878
+ }
879
+
880
+ /*************************************************
881
+ * Name: shake256
882
+ *
883
+ * Description: SHAKE256 XOF with non-incremental API
884
+ *
885
+ * Arguments: - uint8_t *output: pointer to output
886
+ * - size_t outlen: requested output length in bytes
887
+ * - const uint8_t *input: pointer to input
888
+ * - size_t inlen: length of input in bytes
889
+ **************************************************/
890
+ void shake256(uint8_t *output, size_t outlen,
891
+ const uint8_t *input, size_t inlen) {
892
+ size_t nblocks = outlen / SHAKE256_RATE;
893
+ uint8_t t[SHAKE256_RATE];
894
+ shake256ctx s;
895
+
896
+ shake256_absorb(&s, input, inlen);
897
+ shake256_squeezeblocks(output, nblocks, &s);
898
+
899
+ output += nblocks * SHAKE256_RATE;
900
+ outlen -= nblocks * SHAKE256_RATE;
901
+
902
+ if (outlen) {
903
+ shake256_squeezeblocks(t, 1, &s);
904
+ for (size_t i = 0; i < outlen; ++i) {
905
+ output[i] = t[i];
906
+ }
907
+ }
908
+ shake256_ctx_release(&s);
909
+ }
910
+
911
+ void sha3_256_inc_init(sha3_256incctx *state) {
912
+ state->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
913
+ if (state->ctx == NULL) {
914
+ exit(111);
915
+ }
916
+ keccak_inc_init(state->ctx);
917
+ }
918
+
919
+ void sha3_256_inc_ctx_clone(sha3_256incctx *dest, const sha3_256incctx *src) {
920
+ dest->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
921
+ if (dest->ctx == NULL) {
922
+ exit(111);
923
+ }
924
+ memcpy(dest->ctx, src->ctx, PQC_SHAKEINCCTX_BYTES);
925
+ }
926
+
927
+ void sha3_256_inc_ctx_release(sha3_256incctx *state) {
928
+ free(state->ctx);
929
+ }
930
+
931
+ void sha3_256_inc_absorb(sha3_256incctx *state, const uint8_t *input, size_t inlen) {
932
+ keccak_inc_absorb(state->ctx, SHA3_256_RATE, input, inlen);
933
+ }
934
+
935
+ void sha3_256_inc_finalize(uint8_t *output, sha3_256incctx *state) {
936
+ uint8_t t[SHA3_256_RATE];
937
+ keccak_inc_finalize(state->ctx, SHA3_256_RATE, 0x06);
938
+
939
+ keccak_squeezeblocks(t, 1, state->ctx, SHA3_256_RATE);
940
+
941
+ sha3_256_inc_ctx_release(state);
942
+
943
+ for (size_t i = 0; i < 32; i++) {
944
+ output[i] = t[i];
945
+ }
946
+ }
947
+
948
+ /*************************************************
949
+ * Name: sha3_256
950
+ *
951
+ * Description: SHA3-256 with non-incremental API
952
+ *
953
+ * Arguments: - uint8_t *output: pointer to output
954
+ * - const uint8_t *input: pointer to input
955
+ * - size_t inlen: length of input in bytes
956
+ **************************************************/
957
+ void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen) {
958
+ uint64_t s[25];
959
+ uint8_t t[SHA3_256_RATE];
960
+
961
+ /* Absorb input */
962
+ keccak_absorb(s, SHA3_256_RATE, input, inlen, 0x06);
963
+
964
+ /* Squeeze output */
965
+ keccak_squeezeblocks(t, 1, s, SHA3_256_RATE);
966
+
967
+ for (size_t i = 0; i < 32; i++) {
968
+ output[i] = t[i];
969
+ }
970
+ }
971
+
972
+ void sha3_384_inc_init(sha3_384incctx *state) {
973
+ state->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
974
+ if (state->ctx == NULL) {
975
+ exit(111);
976
+ }
977
+ keccak_inc_init(state->ctx);
978
+ }
979
+
980
+ void sha3_384_inc_ctx_clone(sha3_384incctx *dest, const sha3_384incctx *src) {
981
+ dest->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
982
+ if (dest->ctx == NULL) {
983
+ exit(111);
984
+ }
985
+ memcpy(dest->ctx, src->ctx, PQC_SHAKEINCCTX_BYTES);
986
+ }
987
+
988
+ void sha3_384_inc_absorb(sha3_384incctx *state, const uint8_t *input, size_t inlen) {
989
+ keccak_inc_absorb(state->ctx, SHA3_384_RATE, input, inlen);
990
+ }
991
+
992
+ void sha3_384_inc_ctx_release(sha3_384incctx *state) {
993
+ free(state->ctx);
994
+ }
995
+
996
+ void sha3_384_inc_finalize(uint8_t *output, sha3_384incctx *state) {
997
+ uint8_t t[SHA3_384_RATE];
998
+ keccak_inc_finalize(state->ctx, SHA3_384_RATE, 0x06);
999
+
1000
+ keccak_squeezeblocks(t, 1, state->ctx, SHA3_384_RATE);
1001
+
1002
+ sha3_384_inc_ctx_release(state);
1003
+
1004
+ for (size_t i = 0; i < 48; i++) {
1005
+ output[i] = t[i];
1006
+ }
1007
+ }
1008
+
1009
+ /*************************************************
1010
+ * Name: sha3_384
1011
+ *
1012
+ * Description: SHA3-256 with non-incremental API
1013
+ *
1014
+ * Arguments: - uint8_t *output: pointer to output
1015
+ * - const uint8_t *input: pointer to input
1016
+ * - size_t inlen: length of input in bytes
1017
+ **************************************************/
1018
+ void sha3_384(uint8_t *output, const uint8_t *input, size_t inlen) {
1019
+ uint64_t s[25];
1020
+ uint8_t t[SHA3_384_RATE];
1021
+
1022
+ /* Absorb input */
1023
+ keccak_absorb(s, SHA3_384_RATE, input, inlen, 0x06);
1024
+
1025
+ /* Squeeze output */
1026
+ keccak_squeezeblocks(t, 1, s, SHA3_384_RATE);
1027
+
1028
+ for (size_t i = 0; i < 48; i++) {
1029
+ output[i] = t[i];
1030
+ }
1031
+ }
1032
+
1033
+ void sha3_512_inc_init(sha3_512incctx *state) {
1034
+ state->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
1035
+ if (state->ctx == NULL) {
1036
+ exit(111);
1037
+ }
1038
+ keccak_inc_init(state->ctx);
1039
+ }
1040
+
1041
+ void sha3_512_inc_ctx_clone(sha3_512incctx *dest, const sha3_512incctx *src) {
1042
+ dest->ctx = malloc(PQC_SHAKEINCCTX_BYTES);
1043
+ if (dest->ctx == NULL) {
1044
+ exit(111);
1045
+ }
1046
+ memcpy(dest->ctx, src->ctx, PQC_SHAKEINCCTX_BYTES);
1047
+ }
1048
+
1049
+ void sha3_512_inc_absorb(sha3_512incctx *state, const uint8_t *input, size_t inlen) {
1050
+ keccak_inc_absorb(state->ctx, SHA3_512_RATE, input, inlen);
1051
+ }
1052
+
1053
+ void sha3_512_inc_ctx_release(sha3_512incctx *state) {
1054
+ free(state->ctx);
1055
+ }
1056
+
1057
+ void sha3_512_inc_finalize(uint8_t *output, sha3_512incctx *state) {
1058
+ uint8_t t[SHA3_512_RATE];
1059
+ keccak_inc_finalize(state->ctx, SHA3_512_RATE, 0x06);
1060
+
1061
+ keccak_squeezeblocks(t, 1, state->ctx, SHA3_512_RATE);
1062
+
1063
+ sha3_512_inc_ctx_release(state);
1064
+
1065
+ for (size_t i = 0; i < 64; i++) {
1066
+ output[i] = t[i];
1067
+ }
1068
+ }
1069
+
1070
+ /*************************************************
1071
+ * Name: sha3_512
1072
+ *
1073
+ * Description: SHA3-512 with non-incremental API
1074
+ *
1075
+ * Arguments: - uint8_t *output: pointer to output
1076
+ * - const uint8_t *input: pointer to input
1077
+ * - size_t inlen: length of input in bytes
1078
+ **************************************************/
1079
+ void sha3_512(uint8_t *output, const uint8_t *input, size_t inlen) {
1080
+ uint64_t s[25];
1081
+ uint8_t t[SHA3_512_RATE];
1082
+
1083
+ /* Absorb input */
1084
+ keccak_absorb(s, SHA3_512_RATE, input, inlen, 0x06);
1085
+
1086
+ /* Squeeze output */
1087
+ keccak_squeezeblocks(t, 1, s, SHA3_512_RATE);
1088
+
1089
+ for (size_t i = 0; i < 64; i++) {
1090
+ output[i] = t[i];
1091
+ }
1092
+ }
1093
+