/src/openssl40/crypto/ml_dsa/ml_dsa_sample.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <openssl/byteorder.h> |
11 | | #include <openssl/crypto.h> |
12 | | #include "ml_dsa_local.h" |
13 | | #include "ml_dsa_vector.h" |
14 | | #include "ml_dsa_matrix.h" |
15 | | #include "ml_dsa_hash.h" |
16 | | #include "internal/sha3.h" |
17 | | #include "internal/packet.h" |
18 | | |
19 | | #define SHAKE128_BLOCKSIZE SHA3_BLOCKSIZE(128) |
20 | | #define SHAKE256_BLOCKSIZE SHA3_BLOCKSIZE(256) |
21 | | |
22 | | /* |
23 | | * This is a constant time version of n % 5 |
24 | | * Note that 0xFFFF / 5 = 0x3333, 2 is added to make an over-estimate of 1/5 |
25 | | * and then we divide by (0xFFFF + 1) |
26 | | */ |
27 | 4.29M | #define MOD5(n) ((n) - 5 * (0x3335 * (n) >> 16)) |
28 | | |
29 | | #if SHAKE128_BLOCKSIZE % 3 != 0 |
30 | | #error "rej_ntt_poly() requires SHAKE128_BLOCKSIZE to be a multiple of 3" |
31 | | #endif |
32 | | |
33 | | typedef int(COEFF_FROM_NIBBLE_FUNC)(uint32_t nibble, uint32_t *out); |
34 | | |
35 | | static COEFF_FROM_NIBBLE_FUNC coeff_from_nibble_4; |
36 | | static COEFF_FROM_NIBBLE_FUNC coeff_from_nibble_2; |
37 | | |
38 | | /** |
39 | | * @brief Combine 3 bytes to form an coefficient. |
40 | | * See FIPS 204, Algorithm 14, CoeffFromThreeBytes() |
41 | | * |
42 | | * This is not constant time as it is used to generate the matrix A which is public. |
43 | | * |
44 | | * @param s A byte array of 3 uniformly distributed bytes. |
45 | | * @param out The returned coefficient in the range 0..q-1. |
46 | | * @returns 1 if the value is less than q or 0 otherwise. |
47 | | * This is used for rejection sampling. |
48 | | */ |
49 | | static ossl_inline int coeff_from_three_bytes(const uint8_t *s, uint32_t *out) |
50 | 32.9M | { |
51 | | /* Zero out the top bit of the 3rd byte to get a value in the range 0..2^23-1) */ |
52 | 32.9M | *out = (uint32_t)s[0] | ((uint32_t)s[1] << 8) | (((uint32_t)s[2] & 0x7f) << 16); |
53 | 32.9M | return *out < ML_DSA_Q; |
54 | 32.9M | } |
55 | | |
56 | | /** |
57 | | * @brief Generate a value in the range (q-4..0..4) |
58 | | * See FIPS 204, Algorithm 15, CoeffFromHalfByte() where eta = 4 |
59 | | * Note the FIPS 204 code uses the range -4..4 (whereas this code adds q to the |
60 | | * negative numbers). |
61 | | * |
62 | | * @param nibble A value in the range 0..15 |
63 | | * @param out The returned value if the range (q-4)..0..4 if nibble is < 9 |
64 | | * @returns 1 nibble was in range, or 0 if the nibble was rejected. |
65 | | */ |
66 | | static ossl_inline int coeff_from_nibble_4(uint32_t nibble, uint32_t *out) |
67 | 3.42M | { |
68 | | /* |
69 | | * This is not constant time but will not leak any important info since |
70 | | * the value is either chosen or thrown away. |
71 | | */ |
72 | 3.42M | if (value_barrier_32(nibble < 9)) { |
73 | 1.92M | *out = mod_sub(4, nibble); |
74 | 1.92M | return 1; |
75 | 1.92M | } |
76 | 1.49M | return 0; |
77 | 3.42M | } |
78 | | |
79 | | /** |
80 | | * @brief Generate a value in the range (q-2..0..2) |
81 | | * See FIPS 204, Algorithm 15, CoeffFromHalfByte() where eta = 2 |
82 | | * Note the FIPS 204 code uses the range -2..2 (whereas this code adds q to the |
83 | | * negative numbers). |
84 | | * |
85 | | * @param nibble A value in the range 0..15 |
86 | | * @param out The returned value if the range (q-2)..0..2 if nibble is < 15 |
87 | | * @returns 1 nibble was in range, or 0 if the nibble was rejected. |
88 | | */ |
89 | | static ossl_inline int coeff_from_nibble_2(uint32_t nibble, uint32_t *out) |
90 | 4.58M | { |
91 | 4.58M | if (value_barrier_32(nibble < 15)) { |
92 | 4.29M | *out = mod_sub(2, MOD5(nibble)); |
93 | 4.29M | return 1; |
94 | 4.29M | } |
95 | 287k | return 0; |
96 | 4.58M | } |
97 | | |
98 | | /** |
99 | | * @brief Use a seed value to generate a polynomial with coefficients in the |
100 | | * range of 0..q-1 using rejection sampling. |
101 | | * SHAKE128 is used to absorb the seed, and then sequences of 3 sample bytes are |
102 | | * squeezed to try to produce coefficients. |
103 | | * The SHAKE128 stream is used to get uniformly distributed elements. |
104 | | * This algorithm is used for matrix expansion and only operates on public inputs. |
105 | | * |
106 | | * See FIPS 204, Algorithm 30, RejNTTPoly() |
107 | | * |
108 | | * @param g_ctx A EVP_MD_CTX object used for sampling the seed. |
109 | | * @param md A pre-fetched SHAKE128 object. |
110 | | * @param seed The seed to use for sampling. |
111 | | * @param seed_len The size of |seed| |
112 | | * @param out The returned polynomial with coefficients in the range of |
113 | | * 0..q-1. This range is required for NTT. |
114 | | * @returns 1 if the polynomial was successfully generated, or 0 if any of the |
115 | | * digest operations failed. |
116 | | */ |
117 | | static int rej_ntt_poly(EVP_MD_CTX *g_ctx, const EVP_MD *md, |
118 | | const uint8_t *seed, size_t seed_len, POLY *out) |
119 | 128k | { |
120 | 128k | int j = 0; |
121 | 128k | uint8_t blocks[SHAKE128_BLOCKSIZE], *b, *end = blocks + sizeof(blocks); |
122 | | |
123 | | /* |
124 | | * Instead of just squeezing 3 bytes at a time, we grab a whole block |
125 | | * Note that the shake128 blocksize of 168 is divisible by 3. |
126 | | */ |
127 | 128k | if (!shake_xof(g_ctx, md, seed, seed_len, blocks, sizeof(blocks))) |
128 | 0 | return 0; |
129 | | |
130 | 643k | while (1) { |
131 | 33.5M | for (b = blocks; b < end; b += 3) { |
132 | 32.9M | if (coeff_from_three_bytes(b, &(out->coeff[j]))) { |
133 | 32.9M | if (++j >= ML_DSA_NUM_POLY_COEFFICIENTS) |
134 | 128k | return 1; /* finished */ |
135 | 32.9M | } |
136 | 32.9M | } |
137 | 514k | if (!EVP_DigestSqueeze(g_ctx, blocks, sizeof(blocks))) |
138 | 0 | return 0; |
139 | 514k | } |
140 | 128k | } |
141 | | |
142 | | /** |
143 | | * @brief Use a seed value to generate a polynomial with coefficients in the |
144 | | * range of ((q-eta)..0..eta) using rejection sampling. eta is either 2 or 4. |
145 | | * SHAKE256 is used to absorb the seed, and then samples are squeezed. |
146 | | * See FIPS 204, Algorithm 31, RejBoundedPoly() |
147 | | * |
148 | | * @param h_ctx A EVP_MD_CTX object context used to sample the seed. |
149 | | * @param md A pre-fetched SHAKE256 object. |
150 | | * @param coef_from_nibble A function that is dependent on eta, which takes a |
151 | | * nibble and tries to see if it is in the correct range. |
152 | | * @param seed The seed to use for sampling. |
153 | | * @param seed_len The size of |seed| |
154 | | * @param out The returned polynomial with coefficients in the range of |
155 | | * ((q-eta)..0..eta) |
156 | | * @returns 1 if the polynomial was successfully generated, or 0 if any of the |
157 | | * digest operations failed. |
158 | | */ |
159 | | static int rej_bounded_poly(EVP_MD_CTX *h_ctx, const EVP_MD *md, |
160 | | COEFF_FROM_NIBBLE_FUNC *coef_from_nibble, |
161 | | const uint8_t *seed, size_t seed_len, POLY *out) |
162 | 24.3k | { |
163 | 24.3k | int ret = 0; |
164 | 24.3k | int j = 0; |
165 | 24.3k | uint32_t z0, z1; |
166 | 24.3k | uint8_t blocks[SHAKE256_BLOCKSIZE], *b, *end = blocks + sizeof(blocks); |
167 | | |
168 | | /* Instead of just squeezing 1 byte at a time, we grab a whole block */ |
169 | 24.3k | if (!shake_xof(h_ctx, md, seed, seed_len, blocks, sizeof(blocks))) |
170 | 0 | goto err; |
171 | | |
172 | 40.9k | while (1) { |
173 | 4.02M | for (b = blocks; b < end; b++) { |
174 | 4.01M | z0 = *b & 0x0F; /* lower nibble of byte */ |
175 | 4.01M | z1 = *b >> 4; /* high nibble of byte */ |
176 | | |
177 | 4.01M | if (coef_from_nibble(z0, &out->coeff[j]) |
178 | 3.11M | && ++j >= ML_DSA_NUM_POLY_COEFFICIENTS) { |
179 | 12.2k | ret = 1; |
180 | 12.2k | goto err; |
181 | 12.2k | } |
182 | 3.99M | if (coef_from_nibble(z1, &out->coeff[j]) |
183 | 3.10M | && ++j >= ML_DSA_NUM_POLY_COEFFICIENTS) { |
184 | 12.0k | ret = 1; |
185 | 12.0k | goto err; |
186 | 12.0k | } |
187 | 3.99M | } |
188 | 16.6k | if (!EVP_DigestSqueeze(h_ctx, blocks, sizeof(blocks))) |
189 | 0 | goto err; |
190 | 16.6k | } |
191 | 24.3k | err: |
192 | 24.3k | OPENSSL_cleanse(blocks, sizeof(blocks)); |
193 | 24.3k | return ret; |
194 | 24.3k | } |
195 | | |
196 | | /** |
197 | | * @brief Generate a k * l matrix that has uniformly distributed polynomial |
198 | | * elements using rejection sampling. |
199 | | * See FIPS 204, Algorithm 32, ExpandA() |
200 | | * |
201 | | * @param g_ctx A EVP_MD_CTX context used for rejection sampling |
202 | | * seed values generated from the seed rho. |
203 | | * @param md A pre-fetched SHAKE128 object |
204 | | * @param rho A 32 byte seed to generated the matrix from. |
205 | | * @param out The generated k * l matrix of polynomials with coefficients |
206 | | * in the range of 0..q-1. |
207 | | * @returns 1 if the matrix was generated, or 0 on error. |
208 | | */ |
209 | | int ossl_ml_dsa_matrix_expand_A(EVP_MD_CTX *g_ctx, const EVP_MD *md, |
210 | | const uint8_t *rho, MATRIX *out) |
211 | 2.76k | { |
212 | 2.76k | int ret = 0; |
213 | 2.76k | size_t i, j; |
214 | 2.76k | uint8_t derived_seed[ML_DSA_RHO_BYTES + 2]; |
215 | 2.76k | POLY *poly = out->m_poly; |
216 | | |
217 | | /* |
218 | | * The seeds derived below and the sampling buffers in rej_ntt_poly() are |
219 | | * not cleansed: per FIPS 204 section 3.6.3 the matrix A is easily |
220 | | * computed from the public key and does not require any special |
221 | | * protections. |
222 | | */ |
223 | | |
224 | | /* The seed used for each matrix element is rho + column_index + row_index */ |
225 | 2.76k | memcpy(derived_seed, rho, ML_DSA_RHO_BYTES); |
226 | | |
227 | 18.8k | for (i = 0; i < out->k; i++) { |
228 | 104k | for (j = 0; j < out->l; j++) { |
229 | 88.8k | derived_seed[ML_DSA_RHO_BYTES + 1] = (uint8_t)i; |
230 | 88.8k | derived_seed[ML_DSA_RHO_BYTES] = (uint8_t)j; |
231 | | /* Generate the polynomial for each matrix element using a unique seed */ |
232 | 88.8k | if (!rej_ntt_poly(g_ctx, md, derived_seed, sizeof(derived_seed), poly++)) |
233 | 0 | goto err; |
234 | 88.8k | } |
235 | 16.0k | } |
236 | 2.76k | ret = 1; |
237 | 2.76k | err: |
238 | 2.76k | return ret; |
239 | 2.76k | } |
240 | | |
241 | | /** |
242 | | * @brief Generates 2 vectors using rejection sampling whose polynomial |
243 | | * coefficients are in the interval [q-eta..0..eta] |
244 | | * |
245 | | * See FIPS 204, Algorithm 33, ExpandS(). |
246 | | * Note that in FIPS 204 the range -eta..eta is used. |
247 | | * |
248 | | * @param h_ctx A EVP_MD_CTX context to use to sample the seed. |
249 | | * @param md A pre-fetched SHAKE256 object. |
250 | | * @param eta Is either 2 or 4, and determines the range of the coefficients for |
251 | | * s1 and s2. |
252 | | * @param seed A 64 byte seed to use for sampling. |
253 | | * @param s1 A 1 * l column vector containing polynomials with coefficients in |
254 | | * the range (q-eta)..0..eta |
255 | | * @param s2 A 1 * k column vector containing polynomials with coefficients in |
256 | | * the range (q-eta)..0..eta |
257 | | * @returns 1 if s1 and s2 were successfully generated, or 0 otherwise. |
258 | | */ |
259 | | int ossl_ml_dsa_vector_expand_S(EVP_MD_CTX *h_ctx, const EVP_MD *md, int eta, |
260 | | const uint8_t *seed, VECTOR *s1, VECTOR *s2) |
261 | 1.50k | { |
262 | 1.50k | int ret = 0; |
263 | 1.50k | size_t i; |
264 | 1.50k | size_t l = s1->num_poly; |
265 | 1.50k | size_t k = s2->num_poly; |
266 | 1.50k | uint8_t derived_seed[ML_DSA_PRIV_SEED_BYTES + 2]; |
267 | 1.50k | COEFF_FROM_NIBBLE_FUNC *coef_from_nibble_fn; |
268 | | |
269 | 1.50k | coef_from_nibble_fn = (eta == ML_DSA_ETA_4) ? coeff_from_nibble_4 : coeff_from_nibble_2; |
270 | | |
271 | | /* |
272 | | * Each polynomial generated uses a unique seed that consists of |
273 | | * seed + counter (where the counter is 2 bytes starting at 0) |
274 | | */ |
275 | 1.50k | memcpy(derived_seed, seed, ML_DSA_PRIV_SEED_BYTES); |
276 | 1.50k | derived_seed[ML_DSA_PRIV_SEED_BYTES] = 0; |
277 | 1.50k | derived_seed[ML_DSA_PRIV_SEED_BYTES + 1] = 0; |
278 | | |
279 | 9.40k | for (i = 0; i < l; i++) { |
280 | 7.89k | if (!rej_bounded_poly(h_ctx, md, coef_from_nibble_fn, |
281 | 7.89k | derived_seed, sizeof(derived_seed), &s1->poly[i])) |
282 | 0 | goto err; |
283 | 7.89k | ++derived_seed[ML_DSA_PRIV_SEED_BYTES]; |
284 | 7.89k | } |
285 | 10.3k | for (i = 0; i < k; i++) { |
286 | 8.83k | if (!rej_bounded_poly(h_ctx, md, coef_from_nibble_fn, |
287 | 8.83k | derived_seed, sizeof(derived_seed), &s2->poly[i])) |
288 | 0 | goto err; |
289 | 8.83k | ++derived_seed[ML_DSA_PRIV_SEED_BYTES]; |
290 | 8.83k | } |
291 | 1.50k | ret = 1; |
292 | 1.50k | err: |
293 | 1.50k | OPENSSL_cleanse(derived_seed, sizeof(derived_seed)); |
294 | 1.50k | return ret; |
295 | 1.50k | } |
296 | | |
297 | | /* See FIPS 204, Algorithm 34, ExpandMask(), Step 4 & 5 */ |
298 | | int ossl_ml_dsa_poly_expand_mask(POLY *out, const uint8_t *seed, size_t seed_len, |
299 | | uint32_t gamma1, |
300 | | EVP_MD_CTX *h_ctx, const EVP_MD *md) |
301 | 20.5k | { |
302 | 20.5k | uint8_t buf[32 * 20]; |
303 | 20.5k | size_t buf_len = 32 * (gamma1 == ML_DSA_GAMMA1_TWO_POWER_19 ? 20 : 18); |
304 | 20.5k | int ret = shake_xof(h_ctx, md, seed, seed_len, buf, buf_len) |
305 | 20.5k | && ossl_ml_dsa_poly_decode_expand_mask(out, buf, buf_len, gamma1); |
306 | | |
307 | 20.5k | OPENSSL_cleanse(buf, sizeof(buf)); |
308 | 20.5k | return ret; |
309 | 20.5k | } |
310 | | |
311 | | /* |
312 | | * @brief Sample a polynomial with coefficients in the range {-1..1}. |
313 | | * The number of non zero values (hamming weight) is given by tau |
314 | | * |
315 | | * See FIPS 204, Algorithm 29, SampleInBall() |
316 | | * This function is assumed to not be constant time. |
317 | | * The algorithm is based on Durstenfeld's version of the Fisher-Yates shuffle. |
318 | | * |
319 | | * Note that the coefficients returned by this implementation are positive |
320 | | * i.e one of q-1, 0, or 1. |
321 | | * |
322 | | * @param tau is the number of +1 or -1's in the polynomial 'out_c' (39, 49 or 60) |
323 | | * that is less than or equal to 64 |
324 | | */ |
325 | | int ossl_ml_dsa_poly_sample_in_ball(POLY *out_c, const uint8_t *seed, int seed_len, |
326 | | EVP_MD_CTX *h_ctx, const EVP_MD *md, |
327 | | uint32_t tau) |
328 | 4.92k | { |
329 | 4.92k | uint8_t block[SHAKE256_BLOCKSIZE]; |
330 | 4.92k | uint64_t signs; |
331 | 4.92k | int offset = 8; |
332 | 4.92k | size_t end; |
333 | 4.92k | int ret = 0; |
334 | | |
335 | | /* |
336 | | * Rather than squeeze 8 bytes followed by lots of 1 byte squeezes |
337 | | * the SHAKE blocksize is squeezed each time and buffered into 'block'. |
338 | | */ |
339 | 4.92k | if (!shake_xof(h_ctx, md, seed, seed_len, block, sizeof(block))) |
340 | 0 | goto err; |
341 | | |
342 | | /* |
343 | | * grab the first 64 bits - since tau < 64 |
344 | | * Each bit gives a +1 or -1 value. |
345 | | */ |
346 | 4.92k | OPENSSL_load_u64_le(&signs, block); |
347 | | |
348 | 4.92k | poly_zero(out_c); |
349 | | |
350 | | /* Loop tau times */ |
351 | 240k | for (end = 256 - tau; end < 256; end++) { |
352 | 235k | size_t index; /* index is a random offset to write +1 or -1 */ |
353 | | |
354 | | /* rejection sample in {0..end} to choose an index to place -1 or 1 into */ |
355 | 261k | for (;;) { |
356 | 261k | if (offset == sizeof(block)) { |
357 | | /* squeeze another block if the bytes from block have been used */ |
358 | 0 | if (!EVP_DigestSqueeze(h_ctx, block, sizeof(block))) |
359 | 0 | goto err; |
360 | 0 | offset = 0; |
361 | 0 | } |
362 | | |
363 | 261k | index = block[offset++]; |
364 | 261k | if (index <= end) |
365 | 235k | break; |
366 | 261k | } |
367 | | |
368 | | /* |
369 | | * In-place swap the coefficient we are about to replace to the end so |
370 | | * we don't lose any values that have been already written. |
371 | | */ |
372 | 235k | out_c->coeff[end] = out_c->coeff[index]; |
373 | | /* set the random coefficient value to either 1 or q-1 */ |
374 | 235k | out_c->coeff[index] = mod_sub(1, 2 * (signs & 1)); |
375 | 235k | signs >>= 1; /* grab the next random bit */ |
376 | 235k | } |
377 | 4.92k | ret = 1; |
378 | 4.92k | err: |
379 | 4.92k | OPENSSL_cleanse(block, sizeof(block)); |
380 | 4.92k | return ret; |
381 | 4.92k | } |