pq_crypto 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 (93) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +37 -0
  3. data/CHANGELOG.md +29 -0
  4. data/GET_STARTED.md +65 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +135 -0
  7. data/SECURITY.md +57 -0
  8. data/ext/pqcrypto/extconf.rb +157 -0
  9. data/ext/pqcrypto/mldsa_api.h +51 -0
  10. data/ext/pqcrypto/mlkem_api.h +21 -0
  11. data/ext/pqcrypto/pqcrypto_ruby_secure.c +889 -0
  12. data/ext/pqcrypto/pqcrypto_secure.c +1178 -0
  13. data/ext/pqcrypto/pqcrypto_secure.h +135 -0
  14. data/ext/pqcrypto/vendor/.vendored +5 -0
  15. data/ext/pqcrypto/vendor/pqclean/common/aes.c +639 -0
  16. data/ext/pqcrypto/vendor/pqclean/common/aes.h +64 -0
  17. data/ext/pqcrypto/vendor/pqclean/common/compat.h +73 -0
  18. data/ext/pqcrypto/vendor/pqclean/common/crypto_declassify.h +7 -0
  19. data/ext/pqcrypto/vendor/pqclean/common/fips202.c +928 -0
  20. data/ext/pqcrypto/vendor/pqclean/common/fips202.h +166 -0
  21. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/feat.S +168 -0
  22. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/fips202x2.c +684 -0
  23. data/ext/pqcrypto/vendor/pqclean/common/keccak2x/fips202x2.h +60 -0
  24. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-times4-SIMD256.c +1028 -0
  25. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-times4-SnP.h +50 -0
  26. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/KeccakP-1600-unrolling.macros +198 -0
  27. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/Makefile +8 -0
  28. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/Makefile.Microsoft_nmake +8 -0
  29. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/SIMD256-config.h +3 -0
  30. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/align.h +34 -0
  31. data/ext/pqcrypto/vendor/pqclean/common/keccak4x/brg_endian.h +142 -0
  32. data/ext/pqcrypto/vendor/pqclean/common/nistseedexpander.c +101 -0
  33. data/ext/pqcrypto/vendor/pqclean/common/nistseedexpander.h +39 -0
  34. data/ext/pqcrypto/vendor/pqclean/common/randombytes.c +355 -0
  35. data/ext/pqcrypto/vendor/pqclean/common/randombytes.h +27 -0
  36. data/ext/pqcrypto/vendor/pqclean/common/sha2.c +769 -0
  37. data/ext/pqcrypto/vendor/pqclean/common/sha2.h +173 -0
  38. data/ext/pqcrypto/vendor/pqclean/common/sp800-185.c +156 -0
  39. data/ext/pqcrypto/vendor/pqclean/common/sp800-185.h +27 -0
  40. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/LICENSE +5 -0
  41. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/Makefile +19 -0
  42. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/Makefile.Microsoft_nmake +23 -0
  43. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/api.h +18 -0
  44. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/cbd.c +83 -0
  45. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/cbd.h +11 -0
  46. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/indcpa.c +327 -0
  47. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/indcpa.h +22 -0
  48. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/kem.c +164 -0
  49. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/kem.h +23 -0
  50. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/ntt.c +146 -0
  51. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/ntt.h +14 -0
  52. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/params.h +36 -0
  53. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/poly.c +299 -0
  54. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/poly.h +37 -0
  55. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/polyvec.c +188 -0
  56. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/polyvec.h +26 -0
  57. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/reduce.c +41 -0
  58. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/reduce.h +13 -0
  59. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/symmetric-shake.c +71 -0
  60. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/symmetric.h +30 -0
  61. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/verify.c +67 -0
  62. data/ext/pqcrypto/vendor/pqclean/crypto_kem/ml-kem-768/clean/verify.h +13 -0
  63. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/LICENSE +5 -0
  64. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/Makefile +19 -0
  65. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/Makefile.Microsoft_nmake +23 -0
  66. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/api.h +50 -0
  67. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/ntt.c +98 -0
  68. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/ntt.h +10 -0
  69. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/packing.c +261 -0
  70. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/packing.h +31 -0
  71. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/params.h +44 -0
  72. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/poly.c +799 -0
  73. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/poly.h +52 -0
  74. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/polyvec.c +415 -0
  75. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/polyvec.h +65 -0
  76. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/reduce.c +69 -0
  77. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/reduce.h +17 -0
  78. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/rounding.c +92 -0
  79. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/rounding.h +14 -0
  80. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/sign.c +407 -0
  81. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/sign.h +47 -0
  82. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/symmetric-shake.c +26 -0
  83. data/ext/pqcrypto/vendor/pqclean/crypto_sign/ml-dsa-65/clean/symmetric.h +34 -0
  84. data/lib/pq_crypto/errors.rb +10 -0
  85. data/lib/pq_crypto/hybrid_kem.rb +106 -0
  86. data/lib/pq_crypto/kem.rb +199 -0
  87. data/lib/pq_crypto/serialization.rb +102 -0
  88. data/lib/pq_crypto/signature.rb +198 -0
  89. data/lib/pq_crypto/version.rb +5 -0
  90. data/lib/pq_crypto.rb +177 -0
  91. data/lib/pqcrypto.rb +3 -0
  92. data/script/vendor_libs.rb +199 -0
  93. metadata +195 -0
@@ -0,0 +1,173 @@
1
+ #ifndef SHA2_H
2
+ #define SHA2_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ /* The incremental API allows hashing of individual input blocks; these blocks
8
+ must be exactly 64 bytes each.
9
+ Use the 'finalize' functions for any remaining bytes (possibly over 64). */
10
+
11
+ #define PQC_SHA256CTX_BYTES 40
12
+ /* Structure for the incremental API */
13
+ typedef struct {
14
+ uint8_t *ctx;
15
+ } sha224ctx;
16
+
17
+ /* Structure for the incremental API */
18
+ typedef struct {
19
+ uint8_t *ctx;
20
+ } sha256ctx;
21
+
22
+ #define PQC_SHA512CTX_BYTES 72
23
+ /* Structure for the incremental API */
24
+ typedef struct {
25
+ uint8_t *ctx;
26
+ } sha384ctx;
27
+
28
+ /* Structure for the incremental API */
29
+ typedef struct {
30
+ uint8_t *ctx;
31
+ } sha512ctx;
32
+
33
+ /* ====== SHA224 API ==== */
34
+
35
+ /**
36
+ * Initialize the incremental hashing API.
37
+ *
38
+ * Can't be called multiple times.
39
+ */
40
+ void sha224_inc_init(sha224ctx *state);
41
+
42
+ /**
43
+ * Copy the hashing state
44
+ */
45
+ void sha224_inc_ctx_clone(sha224ctx *stateout, const sha224ctx *statein);
46
+
47
+ /**
48
+ * Absorb blocks
49
+ */
50
+ void sha224_inc_blocks(sha224ctx *state, const uint8_t *in, size_t inblocks);
51
+
52
+ /**
53
+ * Finalize and obtain the digest
54
+ *
55
+ * If applicable, this function will free the memory associated with the sha224ctx.
56
+ *
57
+ * If not calling this function, call `sha224_inc_ctx_release`
58
+ */
59
+ void sha224_inc_finalize(uint8_t *out, sha224ctx *state, const uint8_t *in, size_t inlen);
60
+
61
+ /**
62
+ * Destroy the state. Make sure to use this, as this API may not always be stack-based.
63
+ */
64
+ void sha224_inc_ctx_release(sha224ctx *state);
65
+
66
+ /**
67
+ * All-in-one sha224 function
68
+ */
69
+ void sha224(uint8_t *out, const uint8_t *in, size_t inlen);
70
+
71
+ /* ====== SHA256 API ==== */
72
+
73
+ /**
74
+ * Initialize the incremental hashing API
75
+ */
76
+ void sha256_inc_init(sha256ctx *state);
77
+
78
+ /**
79
+ * Copy the hashing state
80
+ */
81
+ void sha256_inc_ctx_clone(sha256ctx *stateout, const sha256ctx *statein);
82
+
83
+ /**
84
+ * Absorb blocks
85
+ */
86
+ void sha256_inc_blocks(sha256ctx *state, const uint8_t *in, size_t inblocks);
87
+
88
+ /**
89
+ * Finalize and obtain the digest
90
+ *
91
+ * If applicable, this function will free the memory associated with the sha256ctx.
92
+ */
93
+ void sha256_inc_finalize(uint8_t *out, sha256ctx *state, const uint8_t *in, size_t inlen);
94
+
95
+ /**
96
+ * Destroy the state. Make sure to use this, as this API may not always be stack-based.
97
+ */
98
+ void sha256_inc_ctx_release(sha256ctx *state);
99
+
100
+ /**
101
+ * All-in-one sha256 function
102
+ */
103
+ void sha256(uint8_t *out, const uint8_t *in, size_t inlen);
104
+
105
+ /* ====== SHA384 API ==== */
106
+
107
+ /**
108
+ * Initialize the incremental hashing API
109
+ */
110
+ void sha384_inc_init(sha384ctx *state);
111
+
112
+ /**
113
+ * Copy the hashing state
114
+ */
115
+ void sha384_inc_ctx_clone(sha384ctx *stateout, const sha384ctx *statein);
116
+
117
+ /**
118
+ * Absorb blocks
119
+ */
120
+ void sha384_inc_blocks(sha384ctx *state, const uint8_t *in, size_t inblocks);
121
+
122
+ /**
123
+ * Finalize and obtain the digest.
124
+ *
125
+ * If applicable, this function will free the memory associated with the sha384ctx.
126
+ */
127
+ void sha384_inc_finalize(uint8_t *out, sha384ctx *state, const uint8_t *in, size_t inlen);
128
+
129
+ /**
130
+ * Destroy the state. Make sure to use this if not calling finalize, as this API may not always be stack-based.
131
+ */
132
+ void sha384_inc_ctx_release(sha384ctx *state);
133
+
134
+ /**
135
+ * All-in-one sha384 function
136
+ */
137
+ void sha384(uint8_t *out, const uint8_t *in, size_t inlen);
138
+
139
+ /* ====== SHA512 API ==== */
140
+
141
+ /**
142
+ * Initialize the incremental hashing API
143
+ */
144
+ void sha512_inc_init(sha512ctx *state);
145
+
146
+ /**
147
+ * Copy the hashing state
148
+ */
149
+ void sha512_inc_ctx_clone(sha512ctx *stateout, const sha512ctx *statein);
150
+
151
+ /**
152
+ * Absorb blocks
153
+ */
154
+ void sha512_inc_blocks(sha512ctx *state, const uint8_t *in, size_t inblocks);
155
+
156
+ /**
157
+ * Finalize and obtain the digest
158
+ *
159
+ * If applicable, this function will free the memory associated with the sha512ctx.
160
+ */
161
+ void sha512_inc_finalize(uint8_t *out, sha512ctx *state, const uint8_t *in, size_t inlen);
162
+
163
+ /**
164
+ * Destroy the state. Make sure to use this if not calling finalize, as this API may not always be stack-based.
165
+ */
166
+ void sha512_inc_ctx_release(sha512ctx *state);
167
+
168
+ /**
169
+ * All-in-one sha512 function
170
+ */
171
+ void sha512(uint8_t *out, const uint8_t *in, size_t inlen);
172
+
173
+ #endif
@@ -0,0 +1,156 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+
4
+ #include "sp800-185.h"
5
+
6
+ static size_t left_encode(uint8_t *encbuf, size_t value) {
7
+ size_t n, i, v;
8
+
9
+ for (v = value, n = 0; v && (n < sizeof(size_t)); n++, v >>= 8) {
10
+ ; /* empty */
11
+ }
12
+ if (n == 0) {
13
+ n = 1;
14
+ }
15
+ for (i = 1; i <= n; i++) {
16
+ encbuf[i] = (uint8_t)(value >> (8 * (n - i)));
17
+ }
18
+ encbuf[0] = (uint8_t)n;
19
+ return n + 1;
20
+ }
21
+
22
+ void cshake128_inc_init(shake128incctx *state, const uint8_t *name, size_t namelen, const uint8_t *cstm, size_t cstmlen) {
23
+ uint8_t encbuf[sizeof(size_t) +1];
24
+
25
+ shake128_inc_init(state);
26
+
27
+ shake128_inc_absorb(state, encbuf, left_encode(encbuf, SHAKE128_RATE));
28
+
29
+ shake128_inc_absorb(state, encbuf, left_encode(encbuf, namelen * 8));
30
+ shake128_inc_absorb(state, name, namelen);
31
+
32
+ shake128_inc_absorb(state, encbuf, left_encode(encbuf, cstmlen * 8));
33
+ shake128_inc_absorb(state, cstm, cstmlen);
34
+
35
+ if (state->ctx[25] != 0) {
36
+ state->ctx[25] = SHAKE128_RATE - 1;
37
+ encbuf[0] = 0;
38
+ shake128_inc_absorb(state, encbuf, 1);
39
+ }
40
+ }
41
+
42
+ void cshake128_inc_absorb(shake128incctx *state, const uint8_t *input, size_t inlen) {
43
+ shake128_inc_absorb(state, input, inlen);
44
+ }
45
+
46
+ void cshake128_inc_finalize(shake128incctx *state) {
47
+ state->ctx[state->ctx[25] >> 3] ^= (uint64_t)0x04 << (8 * (state->ctx[25] & 0x07));
48
+ state->ctx[(SHAKE128_RATE - 1) >> 3] ^= (uint64_t)128 << (8 * ((SHAKE128_RATE - 1) & 0x07));
49
+ state->ctx[25] = 0;
50
+ }
51
+
52
+ void cshake128_inc_squeeze(uint8_t *output, size_t outlen, shake128incctx *state) {
53
+ shake128_inc_squeeze(output, outlen, state);
54
+ }
55
+
56
+ void cshake128_inc_ctx_release(shake128incctx *state) {
57
+ shake128_inc_ctx_release(state);
58
+ }
59
+
60
+ void cshake128_inc_ctx_clone(shake128incctx *dest, const shake128incctx *src) {
61
+ shake128_inc_ctx_clone(dest, src);
62
+ }
63
+
64
+ void cshake256_inc_init(shake256incctx *state, const uint8_t *name, size_t namelen, const uint8_t *cstm, size_t cstmlen) {
65
+ uint8_t encbuf[sizeof(size_t) +1];
66
+
67
+ shake256_inc_init(state);
68
+
69
+ shake256_inc_absorb(state, encbuf, left_encode(encbuf, SHAKE256_RATE));
70
+
71
+ shake256_inc_absorb(state, encbuf, left_encode(encbuf, namelen * 8));
72
+ shake256_inc_absorb(state, name, namelen);
73
+
74
+ shake256_inc_absorb(state, encbuf, left_encode(encbuf, cstmlen * 8));
75
+ shake256_inc_absorb(state, cstm, cstmlen);
76
+
77
+ if (state->ctx[25] != 0) {
78
+ state->ctx[25] = SHAKE256_RATE - 1;
79
+ encbuf[0] = 0;
80
+ shake256_inc_absorb(state, encbuf, 1);
81
+ }
82
+ }
83
+
84
+ void cshake256_inc_absorb(shake256incctx *state, const uint8_t *input, size_t inlen) {
85
+ shake256_inc_absorb(state, input, inlen);
86
+ }
87
+
88
+ void cshake256_inc_finalize(shake256incctx *state) {
89
+ state->ctx[state->ctx[25] >> 3] ^= (uint64_t)0x04 << (8 * (state->ctx[25] & 0x07));
90
+ state->ctx[(SHAKE256_RATE - 1) >> 3] ^= (uint64_t)128 << (8 * ((SHAKE256_RATE - 1) & 0x07));
91
+ state->ctx[25] = 0;
92
+ }
93
+
94
+ void cshake256_inc_squeeze(uint8_t *output, size_t outlen, shake256incctx *state) {
95
+ shake256_inc_squeeze(output, outlen, state);
96
+ }
97
+
98
+ void cshake256_inc_ctx_release(shake256incctx *state) {
99
+ shake256_inc_ctx_release(state);
100
+ }
101
+
102
+ void cshake256_inc_ctx_clone(shake256incctx *dest, const shake256incctx *src) {
103
+ shake256_inc_ctx_clone(dest, src);
104
+ }
105
+
106
+ /*************************************************
107
+ * Name: cshake128
108
+ *
109
+ * Description: cSHAKE128 XOF with non-incremental API
110
+ *
111
+ * Arguments: - uint8_t *output: pointer to output
112
+ * - size_t outlen: requested output length in bytes
113
+ * - const uint8_t *name: pointer to function-name string
114
+ * - size_t namelen: length of function-name string in bytes
115
+ * - const uint8_t *cstm: pointer to non-empty customization string
116
+ * - size_t cstmlen: length of customization string in bytes
117
+ * - const uint8_t *input: pointer to input
118
+ * - size_t inlen: length of input in bytes
119
+ **************************************************/
120
+ void cshake128(uint8_t *output, size_t outlen,
121
+ const uint8_t *name, size_t namelen,
122
+ const uint8_t *cstm, size_t cstmlen,
123
+ const uint8_t *input, size_t inlen) {
124
+ shake128incctx state;
125
+ cshake128_inc_init(&state, name, namelen, cstm, cstmlen);
126
+ cshake128_inc_absorb(&state, input, inlen);
127
+ cshake128_inc_finalize(&state);
128
+ cshake128_inc_squeeze(output, outlen, &state);
129
+ cshake128_inc_ctx_release(&state);
130
+ }
131
+
132
+ /*************************************************
133
+ * Name: cshake256
134
+ *
135
+ * Description: cSHAKE256 XOF with non-incremental API
136
+ *
137
+ * Arguments: - uint8_t *output: pointer to output
138
+ * - size_t outlen: requested output length in bytes
139
+ * - const uint8_t *name: pointer to function-name string
140
+ * - size_t namelen: length of function-name string in bytes
141
+ * - const uint8_t *cstm: pointer to non-empty customization string
142
+ * - size_t cstmlen: length of customization string in bytes
143
+ * - const uint8_t *input: pointer to input
144
+ * - size_t inlen: length of input in bytes
145
+ **************************************************/
146
+ void cshake256(uint8_t *output, size_t outlen,
147
+ const uint8_t *name, size_t namelen,
148
+ const uint8_t *cstm, size_t cstmlen,
149
+ const uint8_t *input, size_t inlen) {
150
+ shake256incctx state;
151
+ cshake256_inc_init(&state, name, namelen, cstm, cstmlen);
152
+ cshake256_inc_absorb(&state, input, inlen);
153
+ cshake256_inc_finalize(&state);
154
+ cshake256_inc_squeeze(output, outlen, &state);
155
+ cshake256_inc_ctx_release(&state);
156
+ }
@@ -0,0 +1,27 @@
1
+ #ifndef SP800_185_H
2
+ #define SP800_185_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #include "fips202.h"
8
+
9
+ void cshake128_inc_init(shake128incctx *state, const uint8_t *name, size_t namelen, const uint8_t *cstm, size_t cstmlen);
10
+ void cshake128_inc_absorb(shake128incctx *state, const uint8_t *input, size_t inlen);
11
+ void cshake128_inc_finalize(shake128incctx *state);
12
+ void cshake128_inc_squeeze(uint8_t *output, size_t outlen, shake128incctx *state);
13
+ void cshake128_inc_ctx_release(shake128incctx *state);
14
+ void cshake128_inc_ctx_clone(shake128incctx *dest, const shake128incctx *src);
15
+
16
+ void cshake128(uint8_t *output, size_t outlen, const uint8_t *name, size_t namelen, const uint8_t *cstm, size_t cstmlen, const uint8_t *input, size_t inlen);
17
+
18
+ void cshake256_inc_init(shake256incctx *state, const uint8_t *name, size_t namelen, const uint8_t *cstm, size_t cstmlen);
19
+ void cshake256_inc_absorb(shake256incctx *state, const uint8_t *input, size_t inlen);
20
+ void cshake256_inc_finalize(shake256incctx *state);
21
+ void cshake256_inc_squeeze(uint8_t *output, size_t outlen, shake256incctx *state);
22
+ void cshake256_inc_ctx_release(shake256incctx *state);
23
+ void cshake256_inc_ctx_clone(shake256incctx *dest, const shake256incctx *src);
24
+
25
+ void cshake256(uint8_t *output, size_t outlen, const uint8_t *name, size_t namelen, const uint8_t *cstm, size_t cstmlen, const uint8_t *input, size_t inlen);
26
+
27
+ #endif
@@ -0,0 +1,5 @@
1
+ Public Domain (https://creativecommons.org/share-your-work/public-domain/cc0/)
2
+
3
+ For Keccak and AES we are using public-domain
4
+ code from sources and by authors listed in
5
+ comments on top of the respective files.
@@ -0,0 +1,19 @@
1
+ # This Makefile can be used with GNU Make or BSD Make
2
+
3
+ LIB=libml-kem-768_clean.a
4
+ HEADERS=api.h cbd.h indcpa.h kem.h ntt.h params.h poly.h polyvec.h reduce.h symmetric.h verify.h
5
+ OBJECTS=cbd.o indcpa.o kem.o ntt.o poly.o polyvec.o reduce.o symmetric-shake.o verify.o
6
+
7
+ CFLAGS=-O3 -Wall -Wextra -Wpedantic -Werror -Wmissing-prototypes -Wredundant-decls -std=c99 -I../../../common $(EXTRAFLAGS)
8
+
9
+ all: $(LIB)
10
+
11
+ %.o: %.c $(HEADERS)
12
+ $(CC) $(CFLAGS) -c -o $@ $<
13
+
14
+ $(LIB): $(OBJECTS)
15
+ $(AR) -r $@ $(OBJECTS)
16
+
17
+ clean:
18
+ $(RM) $(OBJECTS)
19
+ $(RM) $(LIB)
@@ -0,0 +1,23 @@
1
+ # This Makefile can be used with Microsoft Visual Studio's nmake using the command:
2
+ # nmake /f Makefile.Microsoft_nmake
3
+
4
+ LIBRARY=libml-kem-768_clean.lib
5
+ OBJECTS=cbd.obj indcpa.obj kem.obj ntt.obj poly.obj polyvec.obj reduce.obj symmetric-shake.obj verify.obj
6
+
7
+ # Warning C4146 is raised when a unary minus operator is applied to an
8
+ # unsigned type; this has nonetheless been standard and portable for as
9
+ # long as there has been a C standard, and we need it for constant-time
10
+ # computations. Thus, we disable that spurious warning.
11
+ CFLAGS=/nologo /O2 /I ..\..\..\common /W4 /WX /wd4146
12
+
13
+ all: $(LIBRARY)
14
+
15
+ # Make sure objects are recompiled if headers change.
16
+ $(OBJECTS): *.h
17
+
18
+ $(LIBRARY): $(OBJECTS)
19
+ LIB.EXE /NOLOGO /WX /OUT:$@ $**
20
+
21
+ clean:
22
+ -DEL $(OBJECTS)
23
+ -DEL $(LIBRARY)
@@ -0,0 +1,18 @@
1
+ #ifndef PQCLEAN_MLKEM768_CLEAN_API_H
2
+ #define PQCLEAN_MLKEM768_CLEAN_API_H
3
+
4
+ #include <stdint.h>
5
+
6
+ #define PQCLEAN_MLKEM768_CLEAN_CRYPTO_SECRETKEYBYTES 2400
7
+ #define PQCLEAN_MLKEM768_CLEAN_CRYPTO_PUBLICKEYBYTES 1184
8
+ #define PQCLEAN_MLKEM768_CLEAN_CRYPTO_CIPHERTEXTBYTES 1088
9
+ #define PQCLEAN_MLKEM768_CLEAN_CRYPTO_BYTES 32
10
+ #define PQCLEAN_MLKEM768_CLEAN_CRYPTO_ALGNAME "ML-KEM-768"
11
+
12
+ int PQCLEAN_MLKEM768_CLEAN_crypto_kem_keypair(uint8_t *pk, uint8_t *sk);
13
+
14
+ int PQCLEAN_MLKEM768_CLEAN_crypto_kem_enc(uint8_t *ct, uint8_t *ss, const uint8_t *pk);
15
+
16
+ int PQCLEAN_MLKEM768_CLEAN_crypto_kem_dec(uint8_t *ss, const uint8_t *ct, const uint8_t *sk);
17
+
18
+ #endif
@@ -0,0 +1,83 @@
1
+ #include "cbd.h"
2
+ #include "params.h"
3
+ #include <stdint.h>
4
+
5
+ /*************************************************
6
+ * Name: load32_littleendian
7
+ *
8
+ * Description: load 4 bytes into a 32-bit integer
9
+ * in little-endian order
10
+ *
11
+ * Arguments: - const uint8_t *x: pointer to input byte array
12
+ *
13
+ * Returns 32-bit unsigned integer loaded from x
14
+ **************************************************/
15
+ static uint32_t load32_littleendian(const uint8_t x[4]) {
16
+ uint32_t r;
17
+ r = (uint32_t)x[0];
18
+ r |= (uint32_t)x[1] << 8;
19
+ r |= (uint32_t)x[2] << 16;
20
+ r |= (uint32_t)x[3] << 24;
21
+ return r;
22
+ }
23
+
24
+ /*************************************************
25
+ * Name: load24_littleendian
26
+ *
27
+ * Description: load 3 bytes into a 32-bit integer
28
+ * in little-endian order.
29
+ * This function is only needed for Kyber-512
30
+ *
31
+ * Arguments: - const uint8_t *x: pointer to input byte array
32
+ *
33
+ * Returns 32-bit unsigned integer loaded from x (most significant byte is zero)
34
+ **************************************************/
35
+
36
+
37
+ /*************************************************
38
+ * Name: cbd2
39
+ *
40
+ * Description: Given an array of uniformly random bytes, compute
41
+ * polynomial with coefficients distributed according to
42
+ * a centered binomial distribution with parameter eta=2
43
+ *
44
+ * Arguments: - poly *r: pointer to output polynomial
45
+ * - const uint8_t *buf: pointer to input byte array
46
+ **************************************************/
47
+ static void cbd2(poly *r, const uint8_t buf[2 * KYBER_N / 4]) {
48
+ unsigned int i, j;
49
+ uint32_t t, d;
50
+ int16_t a, b;
51
+
52
+ for (i = 0; i < KYBER_N / 8; i++) {
53
+ t = load32_littleendian(buf + 4 * i);
54
+ d = t & 0x55555555;
55
+ d += (t >> 1) & 0x55555555;
56
+
57
+ for (j = 0; j < 8; j++) {
58
+ a = (d >> (4 * j + 0)) & 0x3;
59
+ b = (d >> (4 * j + 2)) & 0x3;
60
+ r->coeffs[8 * i + j] = a - b;
61
+ }
62
+ }
63
+ }
64
+
65
+ /*************************************************
66
+ * Name: cbd3
67
+ *
68
+ * Description: Given an array of uniformly random bytes, compute
69
+ * polynomial with coefficients distributed according to
70
+ * a centered binomial distribution with parameter eta=3.
71
+ * This function is only needed for Kyber-512
72
+ *
73
+ * Arguments: - poly *r: pointer to output polynomial
74
+ * - const uint8_t *buf: pointer to input byte array
75
+ **************************************************/
76
+
77
+ void PQCLEAN_MLKEM768_CLEAN_poly_cbd_eta1(poly *r, const uint8_t buf[KYBER_ETA1 * KYBER_N / 4]) {
78
+ cbd2(r, buf);
79
+ }
80
+
81
+ void PQCLEAN_MLKEM768_CLEAN_poly_cbd_eta2(poly *r, const uint8_t buf[KYBER_ETA2 * KYBER_N / 4]) {
82
+ cbd2(r, buf);
83
+ }
@@ -0,0 +1,11 @@
1
+ #ifndef PQCLEAN_MLKEM768_CLEAN_CBD_H
2
+ #define PQCLEAN_MLKEM768_CLEAN_CBD_H
3
+ #include "params.h"
4
+ #include "poly.h"
5
+ #include <stdint.h>
6
+
7
+ void PQCLEAN_MLKEM768_CLEAN_poly_cbd_eta1(poly *r, const uint8_t buf[KYBER_ETA1 * KYBER_N / 4]);
8
+
9
+ void PQCLEAN_MLKEM768_CLEAN_poly_cbd_eta2(poly *r, const uint8_t buf[KYBER_ETA2 * KYBER_N / 4]);
10
+
11
+ #endif