kpqc 0.1.0

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 (191) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +21 -0
  4. data/README.md +174 -0
  5. data/Rakefile +57 -0
  6. data/THIRD_PARTY_NOTICES.md +53 -0
  7. data/ext/kpqc/aimer128f/extconf.rb +4 -0
  8. data/ext/kpqc/aimer128s/extconf.rb +4 -0
  9. data/ext/kpqc/aimer192f/extconf.rb +4 -0
  10. data/ext/kpqc/aimer192s/extconf.rb +4 -0
  11. data/ext/kpqc/aimer256f/extconf.rb +4 -0
  12. data/ext/kpqc/aimer256s/extconf.rb +4 -0
  13. data/ext/kpqc/extconf_helper.rb +93 -0
  14. data/ext/kpqc/haetae2/extconf.rb +4 -0
  15. data/ext/kpqc/haetae3/extconf.rb +4 -0
  16. data/ext/kpqc/haetae5/extconf.rb +4 -0
  17. data/ext/kpqc/module.c +195 -0
  18. data/ext/kpqc/ntruplus1152/extconf.rb +4 -0
  19. data/ext/kpqc/ntruplus768/extconf.rb +4 -0
  20. data/ext/kpqc/ntruplus864/extconf.rb +4 -0
  21. data/ext/kpqc/randombytes.c +107 -0
  22. data/ext/kpqc/smaugt128/extconf.rb +4 -0
  23. data/ext/kpqc/smaugt192/extconf.rb +4 -0
  24. data/ext/kpqc/smaugt256/extconf.rb +4 -0
  25. data/ext/kpqc/timer/extconf.rb +4 -0
  26. data/kpqc.gemspec +41 -0
  27. data/lib/kpqc/aimer.rb +38 -0
  28. data/lib/kpqc/api.rb +129 -0
  29. data/lib/kpqc/haetae.rb +25 -0
  30. data/lib/kpqc/ntruplus.rb +25 -0
  31. data/lib/kpqc/smaugt.rb +29 -0
  32. data/lib/kpqc/types.rb +103 -0
  33. data/lib/kpqc/version.rb +6 -0
  34. data/lib/kpqc.rb +33 -0
  35. data/test/algorithm_test.rb +111 -0
  36. data/test/api_test.rb +52 -0
  37. data/test/interop.mjs +34 -0
  38. data/test/interop_test.rb +82 -0
  39. data/test/kat/aimer128f.rsp +10 -0
  40. data/test/kat/aimer128s.rsp +10 -0
  41. data/test/kat/aimer192f.rsp +10 -0
  42. data/test/kat/aimer192s.rsp +10 -0
  43. data/test/kat/aimer256f.rsp +10 -0
  44. data/test/kat/aimer256s.rsp +10 -0
  45. data/test/kat/haetae2.rsp +10 -0
  46. data/test/kat/haetae3.rsp +10 -0
  47. data/test/kat/haetae5.rsp +10 -0
  48. data/test/kat/ntruplus1152.rsp +8 -0
  49. data/test/kat/ntruplus768.rsp +8 -0
  50. data/test/kat/ntruplus864.rsp +8 -0
  51. data/test/kat/smaugt128.rsp +8 -0
  52. data/test/kat/smaugt192.rsp +8 -0
  53. data/test/kat/smaugt256.rsp +8 -0
  54. data/test/kat/timer.rsp +8 -0
  55. data/test/kat_drbg.rb +155 -0
  56. data/test/kat_test.rb +99 -0
  57. data/test/test_helper.rb +21 -0
  58. data/vendor/AIMer/LICENSE +21 -0
  59. data/vendor/AIMer/aim2.c +163 -0
  60. data/vendor/AIMer/aim2.h +36 -0
  61. data/vendor/AIMer/aim2_constant.h +74 -0
  62. data/vendor/AIMer/api.h +42 -0
  63. data/vendor/AIMer/common/crypto_declassify.h +7 -0
  64. data/vendor/AIMer/common/fips202.c +878 -0
  65. data/vendor/AIMer/common/fips202.h +166 -0
  66. data/vendor/AIMer/common/rng.h +56 -0
  67. data/vendor/AIMer/field.h +43 -0
  68. data/vendor/AIMer/field128.c +454 -0
  69. data/vendor/AIMer/field192.c +485 -0
  70. data/vendor/AIMer/field256.c +564 -0
  71. data/vendor/AIMer/hash.c +50 -0
  72. data/vendor/AIMer/hash.h +37 -0
  73. data/vendor/AIMer/params/params-aimer-128f.h +34 -0
  74. data/vendor/AIMer/params/params-aimer-128s.h +34 -0
  75. data/vendor/AIMer/params/params-aimer-192f.h +34 -0
  76. data/vendor/AIMer/params/params-aimer-192s.h +34 -0
  77. data/vendor/AIMer/params/params-aimer-256f.h +34 -0
  78. data/vendor/AIMer/params/params-aimer-256s.h +34 -0
  79. data/vendor/AIMer/params.h +5 -0
  80. data/vendor/AIMer/sign.c +631 -0
  81. data/vendor/AIMer/sign.h +40 -0
  82. data/vendor/AIMer/tree.c +75 -0
  83. data/vendor/AIMer/tree.h +26 -0
  84. data/vendor/HAETAE/LICENSE +22 -0
  85. data/vendor/HAETAE/include/api.h +54 -0
  86. data/vendor/HAETAE/include/common.h +12 -0
  87. data/vendor/HAETAE/include/config.h +42 -0
  88. data/vendor/HAETAE/include/decompose.h +19 -0
  89. data/vendor/HAETAE/include/encoding.h +20 -0
  90. data/vendor/HAETAE/include/fft.h +26 -0
  91. data/vendor/HAETAE/include/fips202.h +40 -0
  92. data/vendor/HAETAE/include/fixpoint.h +125 -0
  93. data/vendor/HAETAE/include/ntt.h +16 -0
  94. data/vendor/HAETAE/include/packing.h +58 -0
  95. data/vendor/HAETAE/include/params.h +117 -0
  96. data/vendor/HAETAE/include/poly.h +65 -0
  97. data/vendor/HAETAE/include/polyfix.h +65 -0
  98. data/vendor/HAETAE/include/polymat.h +32 -0
  99. data/vendor/HAETAE/include/polyvec.h +131 -0
  100. data/vendor/HAETAE/include/randombytes.h +39 -0
  101. data/vendor/HAETAE/include/rans_byte.h +335 -0
  102. data/vendor/HAETAE/include/reduce.h +31 -0
  103. data/vendor/HAETAE/include/sampler.h +23 -0
  104. data/vendor/HAETAE/include/symmetric.h +52 -0
  105. data/vendor/HAETAE/src/decompose.c +73 -0
  106. data/vendor/HAETAE/src/encoding.c +700 -0
  107. data/vendor/HAETAE/src/fft.c +235 -0
  108. data/vendor/HAETAE/src/fips202.c +745 -0
  109. data/vendor/HAETAE/src/fixpoint.c +143 -0
  110. data/vendor/HAETAE/src/ntt.c +98 -0
  111. data/vendor/HAETAE/src/packing.c +595 -0
  112. data/vendor/HAETAE/src/poly.c +371 -0
  113. data/vendor/HAETAE/src/polyfix.c +298 -0
  114. data/vendor/HAETAE/src/polymat.c +82 -0
  115. data/vendor/HAETAE/src/polyvec.c +534 -0
  116. data/vendor/HAETAE/src/reduce.c +96 -0
  117. data/vendor/HAETAE/src/sampler.c +310 -0
  118. data/vendor/HAETAE/src/sign.c +558 -0
  119. data/vendor/HAETAE/src/symmetric-shake.c +50 -0
  120. data/vendor/NTRUplus/LICENSE +21 -0
  121. data/vendor/NTRUplus/NTRU+1152/api.h +24 -0
  122. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.c +878 -0
  123. data/vendor/NTRUplus/NTRU+1152/fips202/fips202.h +166 -0
  124. data/vendor/NTRUplus/NTRU+1152/kem.c +286 -0
  125. data/vendor/NTRUplus/NTRU+1152/ntt.c +394 -0
  126. data/vendor/NTRUplus/NTRU+1152/ntt.h +16 -0
  127. data/vendor/NTRUplus/NTRU+1152/params.h +18 -0
  128. data/vendor/NTRUplus/NTRU+1152/poly.c +318 -0
  129. data/vendor/NTRUplus/NTRU+1152/poly.h +31 -0
  130. data/vendor/NTRUplus/NTRU+1152/randombytes.h +9 -0
  131. data/vendor/NTRUplus/NTRU+1152/symmetric.c +39 -0
  132. data/vendor/NTRUplus/NTRU+1152/symmetric.h +12 -0
  133. data/vendor/NTRUplus/NTRU+768/api.h +24 -0
  134. data/vendor/NTRUplus/NTRU+768/fips202/fips202.c +878 -0
  135. data/vendor/NTRUplus/NTRU+768/fips202/fips202.h +166 -0
  136. data/vendor/NTRUplus/NTRU+768/kem.c +286 -0
  137. data/vendor/NTRUplus/NTRU+768/ntt.c +379 -0
  138. data/vendor/NTRUplus/NTRU+768/ntt.h +16 -0
  139. data/vendor/NTRUplus/NTRU+768/params.h +18 -0
  140. data/vendor/NTRUplus/NTRU+768/poly.c +320 -0
  141. data/vendor/NTRUplus/NTRU+768/poly.h +31 -0
  142. data/vendor/NTRUplus/NTRU+768/randombytes.h +9 -0
  143. data/vendor/NTRUplus/NTRU+768/symmetric.c +39 -0
  144. data/vendor/NTRUplus/NTRU+768/symmetric.h +12 -0
  145. data/vendor/NTRUplus/NTRU+864/api.h +24 -0
  146. data/vendor/NTRUplus/NTRU+864/fips202/fips202.c +878 -0
  147. data/vendor/NTRUplus/NTRU+864/fips202/fips202.h +166 -0
  148. data/vendor/NTRUplus/NTRU+864/kem.c +286 -0
  149. data/vendor/NTRUplus/NTRU+864/ntt.c +383 -0
  150. data/vendor/NTRUplus/NTRU+864/ntt.h +16 -0
  151. data/vendor/NTRUplus/NTRU+864/params.h +18 -0
  152. data/vendor/NTRUplus/NTRU+864/poly.c +314 -0
  153. data/vendor/NTRUplus/NTRU+864/poly.h +31 -0
  154. data/vendor/NTRUplus/NTRU+864/randombytes.h +9 -0
  155. data/vendor/NTRUplus/NTRU+864/symmetric.c +39 -0
  156. data/vendor/NTRUplus/NTRU+864/symmetric.h +12 -0
  157. data/vendor/SMAUG-T/LICENSE +22 -0
  158. data/vendor/SMAUG-T/include/api.h +37 -0
  159. data/vendor/SMAUG-T/include/cbd.h +15 -0
  160. data/vendor/SMAUG-T/include/ciphertext.h +45 -0
  161. data/vendor/SMAUG-T/include/common.h +12 -0
  162. data/vendor/SMAUG-T/include/config.h +48 -0
  163. data/vendor/SMAUG-T/include/dg.h +28 -0
  164. data/vendor/SMAUG-T/include/fips202.h +41 -0
  165. data/vendor/SMAUG-T/include/hash.h +21 -0
  166. data/vendor/SMAUG-T/include/hwt.h +28 -0
  167. data/vendor/SMAUG-T/include/indcpa.h +33 -0
  168. data/vendor/SMAUG-T/include/key.h +34 -0
  169. data/vendor/SMAUG-T/include/pack.h +78 -0
  170. data/vendor/SMAUG-T/include/packring.h +50 -0
  171. data/vendor/SMAUG-T/include/params.h +84 -0
  172. data/vendor/SMAUG-T/include/poly.h +41 -0
  173. data/vendor/SMAUG-T/include/randombytes.h +11 -0
  174. data/vendor/SMAUG-T/include/toomcook.h +14 -0
  175. data/vendor/SMAUG-T/include/verify.h +16 -0
  176. data/vendor/SMAUG-T/src/cbd.c +159 -0
  177. data/vendor/SMAUG-T/src/ciphertext.c +95 -0
  178. data/vendor/SMAUG-T/src/dg.c +112 -0
  179. data/vendor/SMAUG-T/src/fips202.c +745 -0
  180. data/vendor/SMAUG-T/src/hash.c +15 -0
  181. data/vendor/SMAUG-T/src/hwt.c +121 -0
  182. data/vendor/SMAUG-T/src/indcpa.c +187 -0
  183. data/vendor/SMAUG-T/src/kem.c +194 -0
  184. data/vendor/SMAUG-T/src/key.c +102 -0
  185. data/vendor/SMAUG-T/src/pack.c +349 -0
  186. data/vendor/SMAUG-T/src/packring.c +503 -0
  187. data/vendor/SMAUG-T/src/poly.c +200 -0
  188. data/vendor/SMAUG-T/src/toomcook.c +256 -0
  189. data/vendor/SMAUG-T/src/verify.c +48 -0
  190. data/vendor/SOURCES.json +130 -0
  191. metadata +280 -0
@@ -0,0 +1,166 @@
1
+ #ifndef FIPS202_H
2
+ #define FIPS202_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #define SHAKE128_RATE 168
8
+ #define SHAKE256_RATE 136
9
+ #define SHA3_256_RATE 136
10
+ #define SHA3_384_RATE 104
11
+ #define SHA3_512_RATE 72
12
+
13
+ #define PQC_SHAKEINCCTX_BYTES (sizeof(uint64_t)*26)
14
+ #define PQC_SHAKECTX_BYTES (sizeof(uint64_t)*25)
15
+
16
+ // Context for incremental API
17
+ typedef struct {
18
+ uint64_t ctx[26];
19
+ } shake128incctx;
20
+
21
+ // Context for non-incremental API
22
+ typedef struct {
23
+ uint64_t ctx[25];
24
+ } shake128ctx;
25
+
26
+ // Context for incremental API
27
+ typedef struct {
28
+ uint64_t ctx[26];
29
+ } shake256incctx;
30
+
31
+ // Context for non-incremental API
32
+ typedef struct {
33
+ uint64_t ctx[25];
34
+ } shake256ctx;
35
+
36
+ // Context for incremental API
37
+ typedef struct {
38
+ uint64_t ctx[26];
39
+ } sha3_256incctx;
40
+
41
+ // Context for incremental API
42
+ typedef struct {
43
+ uint64_t ctx[26];
44
+ } sha3_384incctx;
45
+
46
+ // Context for incremental API
47
+ typedef struct {
48
+ uint64_t ctx[26];
49
+ } sha3_512incctx;
50
+
51
+ /* Initialize the state and absorb the provided input.
52
+ *
53
+ * This function does not support being called multiple times
54
+ * with the same state.
55
+ */
56
+ void shake128_absorb(shake128ctx *state, const uint8_t *input, size_t inlen);
57
+ /* Squeeze output out of the sponge.
58
+ *
59
+ * Supports being called multiple times
60
+ */
61
+ void shake128_squeezeblocks(uint8_t *output, size_t nblocks, shake128ctx *state);
62
+ /* Free the state */
63
+ void shake128_ctx_release(shake128ctx *state);
64
+ /* Copy the state. */
65
+ void shake128_ctx_clone(shake128ctx *dest, const shake128ctx *src);
66
+
67
+ /* Initialize incremental hashing API */
68
+ void shake128_inc_init(shake128incctx *state);
69
+ /* Absorb more information into the XOF.
70
+ *
71
+ * Can be called multiple times.
72
+ */
73
+ void shake128_inc_absorb(shake128incctx *state, const uint8_t *input, size_t inlen);
74
+ /* Finalize the XOF for squeezing */
75
+ void shake128_inc_finalize(shake128incctx *state);
76
+ /* Squeeze output out of the sponge.
77
+ *
78
+ * Supports being called multiple times
79
+ */
80
+ void shake128_inc_squeeze(uint8_t *output, size_t outlen, shake128incctx *state);
81
+ /* Copy the context of the SHAKE128 XOF */
82
+ void shake128_inc_ctx_clone(shake128incctx *dest, const shake128incctx *src);
83
+ /* Free the context of the SHAKE128 XOF */
84
+ void shake128_inc_ctx_release(shake128incctx *state);
85
+
86
+ /* Initialize the state and absorb the provided input.
87
+ *
88
+ * This function does not support being called multiple times
89
+ * with the same state.
90
+ */
91
+ void shake256_absorb(shake256ctx *state, const uint8_t *input, size_t inlen);
92
+ /* Squeeze output out of the sponge.
93
+ *
94
+ * Supports being called multiple times
95
+ */
96
+ void shake256_squeezeblocks(uint8_t *output, size_t nblocks, shake256ctx *state);
97
+ /* Free the context held by this XOF */
98
+ void shake256_ctx_release(shake256ctx *state);
99
+ /* Copy the context held by this XOF */
100
+ void shake256_ctx_clone(shake256ctx *dest, const shake256ctx *src);
101
+
102
+ /* Initialize incremental hashing API */
103
+ void shake256_inc_init(shake256incctx *state);
104
+ void shake256_inc_absorb(shake256incctx *state, const uint8_t *input, size_t inlen);
105
+ /* Prepares for squeeze phase */
106
+ void shake256_inc_finalize(shake256incctx *state);
107
+ /* Squeeze output out of the sponge.
108
+ *
109
+ * Supports being called multiple times
110
+ */
111
+ void shake256_inc_squeeze(uint8_t *output, size_t outlen, shake256incctx *state);
112
+ /* Copy the state */
113
+ void shake256_inc_ctx_clone(shake256incctx *dest, const shake256incctx *src);
114
+ /* Free the state */
115
+ void shake256_inc_ctx_release(shake256incctx *state);
116
+
117
+ /* One-stop SHAKE128 call */
118
+ void shake128(uint8_t *output, size_t outlen,
119
+ const uint8_t *input, size_t inlen);
120
+
121
+ /* One-stop SHAKE256 call */
122
+ void shake256(uint8_t *output, size_t outlen,
123
+ const uint8_t *input, size_t inlen);
124
+
125
+ /* Initialize the incremental hashing state */
126
+ void sha3_256_inc_init(sha3_256incctx *state);
127
+ /* Absorb blocks into SHA3 */
128
+ void sha3_256_inc_absorb(sha3_256incctx *state, const uint8_t *input, size_t inlen);
129
+ /* Obtain the output of the function and free `state` */
130
+ void sha3_256_inc_finalize(uint8_t *output, sha3_256incctx *state);
131
+ /* Copy the context */
132
+ void sha3_256_inc_ctx_clone(sha3_256incctx *dest, const sha3_256incctx *src);
133
+ /* Release the state, don't use if `_finalize` has been used */
134
+ void sha3_256_inc_ctx_release(sha3_256incctx *state);
135
+
136
+ void sha3_256(uint8_t *output, const uint8_t *input, size_t inlen);
137
+
138
+ /* Initialize the incremental hashing state */
139
+ void sha3_384_inc_init(sha3_384incctx *state);
140
+ /* Absorb blocks into SHA3 */
141
+ void sha3_384_inc_absorb(sha3_384incctx *state, const uint8_t *input, size_t inlen);
142
+ /* Obtain the output of the function and free `state` */
143
+ void sha3_384_inc_finalize(uint8_t *output, sha3_384incctx *state);
144
+ /* Copy the context */
145
+ void sha3_384_inc_ctx_clone(sha3_384incctx *dest, const sha3_384incctx *src);
146
+ /* Release the state, don't use if `_finalize` has been used */
147
+ void sha3_384_inc_ctx_release(sha3_384incctx *state);
148
+
149
+ /* One-stop SHA3-384 shop */
150
+ void sha3_384(uint8_t *output, const uint8_t *input, size_t inlen);
151
+
152
+ /* Initialize the incremental hashing state */
153
+ void sha3_512_inc_init(sha3_512incctx *state);
154
+ /* Absorb blocks into SHA3 */
155
+ void sha3_512_inc_absorb(sha3_512incctx *state, const uint8_t *input, size_t inlen);
156
+ /* Obtain the output of the function and free `state` */
157
+ void sha3_512_inc_finalize(uint8_t *output, sha3_512incctx *state);
158
+ /* Copy the context */
159
+ void sha3_512_inc_ctx_clone(sha3_512incctx *dest, const sha3_512incctx *src);
160
+ /* Release the state, don't use if `_finalize` has been used */
161
+ void sha3_512_inc_ctx_release(sha3_512incctx *state);
162
+
163
+ /* One-stop SHA3-512 shop */
164
+ void sha3_512(uint8_t *output, const uint8_t *input, size_t inlen);
165
+
166
+ #endif
@@ -0,0 +1,286 @@
1
+ /* kpqc-py: propagate OS entropy failures (2026-09-06). */
2
+ #include <stddef.h>
3
+ #include <stdint.h>
4
+ #include "api.h"
5
+ #include "params.h"
6
+ #include "symmetric.h"
7
+ #include "poly.h"
8
+ #include "fips202/fips202.h"
9
+ #include "randombytes.h"
10
+
11
+ /*************************************************
12
+ * Name: verify
13
+ *
14
+ * Description: Compares two byte arrays for equality in constant time.
15
+ *
16
+ * Arguments: - const uint8_t *a: first byte array
17
+ * - const uint8_t *b: second byte array
18
+ * - size_t len: length of the arrays
19
+ *
20
+ * Returns 0 if the arrays are equal, 1 otherwise.
21
+ **************************************************/
22
+ static inline int verify(const uint8_t *a, const uint8_t *b, size_t len)
23
+ {
24
+ uint8_t acc = 0;
25
+
26
+ for (size_t i = 0; i < len; i++)
27
+ acc |= (uint8_t)(a[i] ^ b[i]);
28
+
29
+ return (-(uint64_t)acc) >> 63;
30
+ }
31
+
32
+ /*************************************************
33
+ * Name: genf_derand
34
+ *
35
+ * Description: Deterministically generates a secret polynomial f and its
36
+ * multiplicative inverse finv in the NTT domain.
37
+ *
38
+ * Arguments: - poly *f: output polynomial f (NTT domain)
39
+ * - poly *finv: output multiplicative inverse of f
40
+ * in the NTT domain
41
+ * - const uint8_t *coins: 32-byte deterministic seed
42
+ *
43
+ * Returns 0 on success; non-zero if f is not invertible in the NTT domain.
44
+ **************************************************/
45
+ static inline int genf_derand(poly *f, poly *finv, const uint8_t *coins)
46
+ {
47
+ uint8_t buf[NTRUPLUS_N / 4];
48
+
49
+ shake256(buf, sizeof buf, coins, 32);
50
+
51
+ poly_cbd1(f, buf);
52
+ poly_triple(f, f);
53
+ f->coeffs[0] += 1;
54
+
55
+ poly_ntt(f);
56
+
57
+ return poly_baseinv(finv, f);
58
+ }
59
+
60
+ /*************************************************
61
+ * Name: geng_derand
62
+ *
63
+ * Description: Deterministically generates a secret polynomial g and its
64
+ * multiplicative inverse ginv in the NTT domain.
65
+ *
66
+ * Arguments: - poly *g: output polynomial g (NTT domain)
67
+ * - poly *ginv: output multiplicative inverse of g
68
+ * in the NTT domain
69
+ * - const uint8_t *coins: 32-byte deterministic seed
70
+ *
71
+ * Returns 0 on success; non-zero if g is not invertible in the NTT domain.
72
+ **************************************************/
73
+ static inline int geng_derand(poly *g, poly *ginv, const uint8_t *coins)
74
+ {
75
+ uint8_t buf[NTRUPLUS_N / 4];
76
+
77
+ shake256(buf, sizeof buf, coins, 32);
78
+
79
+ poly_cbd1(g, buf);
80
+ poly_triple(g, g);
81
+
82
+ poly_ntt(g);
83
+
84
+ return poly_baseinv(ginv, g);
85
+ }
86
+
87
+ /*************************************************
88
+ * Name: crypto_kem_keypair_derand
89
+ *
90
+ * Description: Computes the deterministic public and secret key pair from
91
+ * the secret polynomials f and g and their multiplicative
92
+ * inverses finv and ginv in the NTT domain.
93
+ *
94
+ * Arguments: - uint8_t *pk: output public key
95
+ * - uint8_t *sk: output secret key
96
+ * - const poly *f: secret polynomial f (NTT domain)
97
+ * - const poly *finv: multiplicative inverse of f (NTT domain)
98
+ * - const poly *g: secret polynomial g (NTT domain)
99
+ * - const poly *ginv: multiplicative inverse of g (NTT domain)
100
+ *
101
+ * Returns: void
102
+ **************************************************/
103
+ static inline void crypto_kem_keypair_derand(uint8_t *pk, uint8_t *sk,
104
+ const poly *f, const poly *finv,
105
+ const poly *g, const poly *ginv)
106
+ {
107
+ poly h, hinv;
108
+
109
+ poly_basemul(&h, g, finv);
110
+ poly_basemul(&hinv, f, ginv);
111
+
112
+ poly_tobytes(pk, &h);
113
+ poly_tobytes(sk, f);
114
+ poly_tobytes(sk + NTRUPLUS_POLYBYTES, &hinv);
115
+ hash_f(sk + 2 * NTRUPLUS_POLYBYTES, pk);
116
+ }
117
+
118
+ /*************************************************
119
+ * Name: crypto_kem_keypair
120
+ *
121
+ * Description: Generates an NTRU+ public/secret key pair for the
122
+ * CCA-secure key encapsulation mechanism. Secret
123
+ * polynomials f and g and their NTT-domain inverses
124
+ * are sampled internally using fresh randomness.
125
+ *
126
+ * Arguments: - uint8_t *pk: output public key
127
+ * (array of CRYPTO_PUBLICKEYBYTES bytes)
128
+ * - uint8_t *sk: output secret key
129
+ * (array of CRYPTO_SECRETKEYBYTES bytes)
130
+ *
131
+ * Returns 0 on success.
132
+ **************************************************/
133
+ int crypto_kem_keypair(uint8_t *pk, uint8_t *sk)
134
+ {
135
+ uint8_t coins[NTRUPLUS_SYMBYTES];
136
+
137
+ poly f, finv;
138
+ poly g, ginv;
139
+
140
+ do {
141
+ if (randombytes(coins, sizeof coins) != 0) return -1;
142
+ } while (genf_derand(&f, &finv, coins));
143
+
144
+ do {
145
+ if (randombytes(coins, sizeof coins) != 0) return -1;
146
+ } while (geng_derand(&g, &ginv, coins));
147
+
148
+ crypto_kem_keypair_derand(pk, sk, &f, &finv, &g, &ginv);
149
+ return 0;
150
+ }
151
+
152
+ /*************************************************
153
+ * Name: crypto_kem_enc_derand
154
+ *
155
+ * Description: Deterministically performs NTRU+ KEM encapsulation using
156
+ * the public key pk and the supplied randomness coins,
157
+ * producing a ciphertext ct and shared secret ss.
158
+ *
159
+ * Arguments: - uint8_t *ct: output ciphertext
160
+ * - uint8_t *ss: output shared secret
161
+ * - const uint8_t *pk: input public key
162
+ * - const uint8_t *coins: input randomness for
163
+ * deterministic encapsulation
164
+ *
165
+ * Returns 0 on success.
166
+ **************************************************/
167
+ static inline int crypto_kem_enc_derand(uint8_t *ct, uint8_t *ss,
168
+ const uint8_t *pk,
169
+ const uint8_t *coins)
170
+ {
171
+ uint8_t msg[NTRUPLUS_N / 8 + NTRUPLUS_SYMBYTES];
172
+ uint8_t buf1[NTRUPLUS_SYMBYTES + NTRUPLUS_N / 4];
173
+ uint8_t buf2[NTRUPLUS_POLYBYTES];
174
+
175
+ poly c, h, r, m;
176
+
177
+ for (size_t i = 0; i < NTRUPLUS_N / 8; i++)
178
+ msg[i] = coins[i];
179
+
180
+ hash_f(msg + NTRUPLUS_N / 8, pk);
181
+ hash_h(buf1, msg);
182
+
183
+ poly_cbd1(&r, buf1 + NTRUPLUS_SYMBYTES);
184
+ poly_ntt(&r);
185
+
186
+ poly_tobytes(buf2, &r);
187
+ hash_g(buf2, buf2);
188
+ poly_sotp_encode(&m, msg, buf2);
189
+ poly_ntt(&m);
190
+
191
+ poly_frombytes(&h, pk);
192
+ poly_basemul_add(&c, &h, &r, &m);
193
+ poly_tobytes(ct, &c);
194
+
195
+ for (size_t i = 0; i < NTRUPLUS_SSBYTES; i++)
196
+ ss[i] = buf1[i];
197
+
198
+ return 0;
199
+ }
200
+
201
+ /*************************************************
202
+ * Name: crypto_kem_enc
203
+ *
204
+ * Description: Generates an NTRU+ KEM ciphertext ct and shared secret ss
205
+ * using the public key pk. Fresh randomness is internally
206
+ * sampled and used for encapsulation.
207
+ *
208
+ * Arguments: - uint8_t *ct: output ciphertext
209
+ * (array of CRYPTO_CIPHERTEXTBYTES bytes)
210
+ * - uint8_t *ss: output shared secret
211
+ * (array of CRYPTO_BYTES bytes)
212
+ * - const uint8_t *pk: input public key
213
+ * (array of CRYPTO_PUBLICKEYBYTES bytes)
214
+ *
215
+ * Returns 0 on success.
216
+ **************************************************/
217
+ int crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk)
218
+ {
219
+ uint8_t coins[NTRUPLUS_N / 8];
220
+
221
+ if (randombytes(coins, sizeof coins) != 0) return -1;
222
+ return crypto_kem_enc_derand(ct, ss, pk, coins);
223
+ }
224
+
225
+ /*************************************************
226
+ * Name: crypto_kem_dec
227
+ *
228
+ * Description: Performs NTRU+ KEM decapsulation. Given a ciphertext ct
229
+ * and a secret key sk, computes the shared secret ss. If
230
+ * decapsulation fails, ss is set to all zero bytes.
231
+ *
232
+ * Arguments: - uint8_t *ss: output shared secret
233
+ * (array of CRYPTO_BYTES bytes)
234
+ * - const uint8_t *ct: input ciphertext
235
+ * (array of CRYPTO_CIPHERTEXTBYTES bytes)
236
+ * - const uint8_t *sk: input secret key
237
+ * (array of CRYPTO_SECRETKEYBYTES bytes)
238
+ *
239
+ * Returns 0 on success, 1 on failure.
240
+ **************************************************/
241
+ int crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk)
242
+ {
243
+ uint8_t msg[NTRUPLUS_N / 8 + NTRUPLUS_SYMBYTES];
244
+ uint8_t buf1[NTRUPLUS_POLYBYTES];
245
+ uint8_t buf2[NTRUPLUS_POLYBYTES];
246
+ uint8_t buf3[NTRUPLUS_POLYBYTES + NTRUPLUS_SYMBYTES];
247
+
248
+ int8_t fail;
249
+
250
+ poly c, f, hinv;
251
+ poly r1, r2;
252
+ poly m1, m2;
253
+
254
+ poly_frombytes(&c, ct);
255
+ poly_frombytes(&f, sk);
256
+ poly_frombytes(&hinv, sk + NTRUPLUS_POLYBYTES);
257
+
258
+ poly_basemul(&m1, &c, &f);
259
+ poly_invntt(&m1);
260
+ poly_crepmod3(&m1, &m1);
261
+
262
+ m2 = m1;
263
+ poly_ntt(&m2);
264
+ poly_sub(&c, &c, &m2);
265
+ poly_basemul(&r2, &c, &hinv);
266
+
267
+ poly_tobytes(buf1, &r2);
268
+ hash_g(buf2, buf1);
269
+ fail = poly_sotp_decode(msg, &m1, buf2);
270
+
271
+ for (size_t i = 0; i < NTRUPLUS_SYMBYTES; i++)
272
+ msg[i + NTRUPLUS_N / 8] = sk[i + 2 * NTRUPLUS_POLYBYTES];
273
+
274
+ hash_h(buf3, msg);
275
+
276
+ poly_cbd1(&r1, buf3 + NTRUPLUS_SSBYTES);
277
+ poly_ntt(&r1);
278
+ poly_tobytes(buf2, &r1);
279
+
280
+ fail |= verify(buf1, buf2, NTRUPLUS_POLYBYTES);
281
+
282
+ for (size_t i = 0; i < NTRUPLUS_SSBYTES; i++)
283
+ ss[i] = buf3[i] & ~(-fail);
284
+
285
+ return fail;
286
+ }