/src/openssl/crypto/ml_dsa/ml_dsa_sample.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2024-2025 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/constant_time.h" |
17 | | #include "internal/sha3.h" |
18 | | #include "internal/packet.h" |
19 | | |
20 | | #define SHAKE128_BLOCKSIZE SHA3_BLOCKSIZE(128) |
21 | | #define SHAKE256_BLOCKSIZE SHA3_BLOCKSIZE(256) |
22 | | |
23 | | /* |
24 | | * This is a constant time version of n % 5 |
25 | | * Note that 0xFFFF / 5 = 0x3333, 2 is added to make an over-estimate of 1/5 |
26 | | * and then we divide by (0xFFFF + 1) |
27 | | */ |
28 | 0 | #define MOD5(n) ((n) - 5 * (0x3335 * (n) >> 16)) |
29 | | |
30 | | #if SHAKE128_BLOCKSIZE % 3 != 0 |
31 | | #error "rej_ntt_poly() requires SHAKE128_BLOCKSIZE to be a multiple of 3" |
32 | | #endif |
33 | | |
34 | | typedef int(COEFF_FROM_NIBBLE_FUNC)(uint32_t nibble, uint32_t *out); |
35 | | |
36 | | static COEFF_FROM_NIBBLE_FUNC coeff_from_nibble_4; |
37 | | static COEFF_FROM_NIBBLE_FUNC coeff_from_nibble_2; |
38 | | |
39 | | static ML_DSA_MATRIX_EXPAND_A_FN matrix_expand_A_scalar; |
40 | | static ML_DSA_VECTOR_EXPAND_S_FN vector_expand_S_scalar; |
41 | | static ML_DSA_VECTOR_EXPAND_MASK_FN vector_expand_mask_scalar; |
42 | | |
43 | | /** |
44 | | * @brief Combine 3 bytes to form an coefficient. |
45 | | * See FIPS 204, Algorithm 14, CoeffFromThreeBytes() |
46 | | * |
47 | | * This is not constant time as it is used to generate the matrix A which is public. |
48 | | * |
49 | | * @param s A byte array of 3 uniformly distributed bytes. |
50 | | * @param out The returned coefficient in the range 0..q-1. |
51 | | * @returns 1 if the value is less than q or 0 otherwise. |
52 | | * This is used for rejection sampling. |
53 | | */ |
54 | | static ossl_inline int coeff_from_three_bytes(const uint8_t *s, uint32_t *out) |
55 | 0 | { |
56 | | /* Zero out the top bit of the 3rd byte to get a value in the range 0..2^23-1) */ |
57 | 0 | *out = (uint32_t)s[0] | ((uint32_t)s[1] << 8) | (((uint32_t)s[2] & 0x7f) << 16); |
58 | 0 | return *out < ML_DSA_Q; |
59 | 0 | } |
60 | | |
61 | | /** |
62 | | * @brief Generate a value in the range (q-4..0..4) |
63 | | * See FIPS 204, Algorithm 15, CoeffFromHalfByte() where eta = 4 |
64 | | * Note the FIPS 204 code uses the range -4..4 (whereas this code adds q to the |
65 | | * negative numbers). |
66 | | * |
67 | | * @param nibble A value in the range 0..15 |
68 | | * @param out The returned value if the range (q-4)..0..4 if nibble is < 9 |
69 | | * @returns 1 nibble was in range, or 0 if the nibble was rejected. |
70 | | */ |
71 | | static ossl_inline int coeff_from_nibble_4(uint32_t nibble, uint32_t *out) |
72 | 0 | { |
73 | | /* |
74 | | * This is not constant time but will not leak any important info since |
75 | | * the value is either chosen or thrown away. |
76 | | */ |
77 | 0 | if (value_barrier_32(nibble < 9)) { |
78 | 0 | *out = mod_sub(4, nibble); |
79 | 0 | return 1; |
80 | 0 | } |
81 | 0 | return 0; |
82 | 0 | } |
83 | | |
84 | | /** |
85 | | * @brief Generate a value in the range (q-2..0..2) |
86 | | * See FIPS 204, Algorithm 15, CoeffFromHalfByte() where eta = 2 |
87 | | * Note the FIPS 204 code uses the range -2..2 (whereas this code adds q to the |
88 | | * negative numbers). |
89 | | * |
90 | | * @param nibble A value in the range 0..15 |
91 | | * @param out The returned value if the range (q-2)..0..2 if nibble is < 15 |
92 | | * @returns 1 nibble was in range, or 0 if the nibble was rejected. |
93 | | */ |
94 | | static ossl_inline int coeff_from_nibble_2(uint32_t nibble, uint32_t *out) |
95 | 0 | { |
96 | 0 | if (value_barrier_32(nibble < 15)) { |
97 | 0 | *out = mod_sub(2, MOD5(nibble)); |
98 | 0 | return 1; |
99 | 0 | } |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | | /** |
104 | | * @brief Use a seed value to generate a polynomial with coefficients in the |
105 | | * range of 0..q-1 using rejection sampling. |
106 | | * SHAKE128 is used to absorb the seed, and then sequences of 3 sample bytes are |
107 | | * squeezed to try to produce coefficients. |
108 | | * The SHAKE128 stream is used to get uniformly distributed elements. |
109 | | * This algorithm is used for matrix expansion and only operates on public inputs. |
110 | | * |
111 | | * See FIPS 204, Algorithm 30, RejNTTPoly() |
112 | | * |
113 | | * @param g_ctx A EVP_MD_CTX object used for sampling the seed. |
114 | | * @param md A pre-fetched SHAKE128 object. |
115 | | * @param seed The seed to use for sampling. |
116 | | * @param seed_len The size of |seed| |
117 | | * @param out The returned polynomial with coefficients in the range of |
118 | | * 0..q-1. This range is required for NTT. |
119 | | * @returns 1 if the polynomial was successfully generated, or 0 if any of the |
120 | | * digest operations failed. |
121 | | */ |
122 | | static int rej_ntt_poly(EVP_MD_CTX *g_ctx, const EVP_MD *md, |
123 | | const uint8_t *seed, size_t seed_len, POLY *out) |
124 | 0 | { |
125 | 0 | int j = 0; |
126 | 0 | uint8_t blocks[SHAKE128_BLOCKSIZE], *b, *end = blocks + sizeof(blocks); |
127 | | |
128 | | /* |
129 | | * Instead of just squeezing 3 bytes at a time, we grab a whole block |
130 | | * Note that the shake128 blocksize of 168 is divisible by 3. |
131 | | */ |
132 | 0 | if (!shake_xof(g_ctx, md, seed, seed_len, blocks, sizeof(blocks))) |
133 | 0 | return 0; |
134 | | |
135 | 0 | while (1) { |
136 | 0 | for (b = blocks; b < end; b += 3) { |
137 | 0 | if (coeff_from_three_bytes(b, &(out->coeff[j]))) { |
138 | 0 | if (++j >= ML_DSA_NUM_POLY_COEFFICIENTS) |
139 | 0 | return 1; /* finished */ |
140 | 0 | } |
141 | 0 | } |
142 | 0 | if (!EVP_DigestSqueeze(g_ctx, blocks, sizeof(blocks))) |
143 | 0 | return 0; |
144 | 0 | } |
145 | 0 | } |
146 | | |
147 | | /** |
148 | | * @brief Use a seed value to generate a polynomial with coefficients in the |
149 | | * range of ((q-eta)..0..eta) using rejection sampling. eta is either 2 or 4. |
150 | | * SHAKE256 is used to absorb the seed, and then samples are squeezed. |
151 | | * See FIPS 204, Algorithm 31, RejBoundedPoly() |
152 | | * |
153 | | * @param h_ctx A EVP_MD_CTX object context used to sample the seed. |
154 | | * @param md A pre-fetched SHAKE256 object. |
155 | | * @param coef_from_nibble A function that is dependent on eta, which takes a |
156 | | * nibble and tries to see if it is in the correct range. |
157 | | * @param seed The seed to use for sampling. |
158 | | * @param seed_len The size of |seed| |
159 | | * @param out The returned polynomial with coefficients in the range of |
160 | | * ((q-eta)..0..eta) |
161 | | * @returns 1 if the polynomial was successfully generated, or 0 if any of the |
162 | | * digest operations failed. |
163 | | */ |
164 | | static int rej_bounded_poly(EVP_MD_CTX *h_ctx, const EVP_MD *md, |
165 | | COEFF_FROM_NIBBLE_FUNC *coef_from_nibble, |
166 | | const uint8_t *seed, size_t seed_len, POLY *out) |
167 | 0 | { |
168 | 0 | int ret = 0; |
169 | 0 | int j = 0; |
170 | 0 | uint32_t z0, z1; |
171 | 0 | uint8_t blocks[SHAKE256_BLOCKSIZE], *b, *end = blocks + sizeof(blocks); |
172 | | |
173 | | /* Instead of just squeezing 1 byte at a time, we grab a whole block */ |
174 | 0 | if (!shake_xof(h_ctx, md, seed, seed_len, blocks, sizeof(blocks))) |
175 | 0 | goto err; |
176 | | |
177 | 0 | while (1) { |
178 | 0 | for (b = blocks; b < end; b++) { |
179 | 0 | z0 = *b & 0x0F; /* lower nibble of byte */ |
180 | 0 | z1 = *b >> 4; /* high nibble of byte */ |
181 | |
|
182 | 0 | if (coef_from_nibble(z0, &out->coeff[j]) |
183 | 0 | && ++j >= ML_DSA_NUM_POLY_COEFFICIENTS) { |
184 | 0 | ret = 1; |
185 | 0 | goto err; |
186 | 0 | } |
187 | 0 | if (coef_from_nibble(z1, &out->coeff[j]) |
188 | 0 | && ++j >= ML_DSA_NUM_POLY_COEFFICIENTS) { |
189 | 0 | ret = 1; |
190 | 0 | goto err; |
191 | 0 | } |
192 | 0 | } |
193 | 0 | if (!EVP_DigestSqueeze(h_ctx, blocks, sizeof(blocks))) |
194 | 0 | goto err; |
195 | 0 | } |
196 | 0 | err: |
197 | 0 | OPENSSL_cleanse(blocks, sizeof(blocks)); |
198 | 0 | return ret; |
199 | 0 | } |
200 | | |
201 | | /** |
202 | | * @brief Generate a k * l matrix that has uniformly distributed polynomial |
203 | | * elements using rejection sampling. |
204 | | * See FIPS 204, Algorithm 32, ExpandA() |
205 | | * |
206 | | * @param g_ctx A EVP_MD_CTX context used for rejection sampling |
207 | | * seed values generated from the seed rho. |
208 | | * @param md A pre-fetched SHAKE128 object |
209 | | * @param rho A 32 byte seed to generated the matrix from. |
210 | | * @param out The generated k * l matrix of polynomials with coefficients |
211 | | * in the range of 0..q-1. |
212 | | * @returns 1 if the matrix was generated, or 0 on error. |
213 | | */ |
214 | | static int matrix_expand_A_scalar(EVP_MD_CTX *g_ctx, const EVP_MD *md, |
215 | | const uint8_t *rho, MATRIX *out) |
216 | 0 | { |
217 | 0 | int ret = 0; |
218 | 0 | size_t i, j; |
219 | 0 | uint8_t derived_seed[ML_DSA_RHO_BYTES + 2]; |
220 | 0 | POLY *poly = out->m_poly; |
221 | | |
222 | | /* The seed used for each matrix element is rho + column_index + row_index */ |
223 | 0 | memcpy(derived_seed, rho, ML_DSA_RHO_BYTES); |
224 | 0 | for (i = 0; i < out->k; i++) { |
225 | 0 | for (j = 0; j < out->l; j++) { |
226 | 0 | derived_seed[ML_DSA_RHO_BYTES + 1] = (uint8_t)i; |
227 | 0 | derived_seed[ML_DSA_RHO_BYTES] = (uint8_t)j; |
228 | | /* Generate the polynomial for each matrix element using a unique seed */ |
229 | 0 | if (!rej_ntt_poly(g_ctx, md, derived_seed, sizeof(derived_seed), poly++)) |
230 | 0 | goto err; |
231 | 0 | } |
232 | 0 | } |
233 | 0 | ret = 1; |
234 | 0 | err: |
235 | 0 | return ret; |
236 | 0 | } |
237 | | |
238 | | /** |
239 | | * @brief Generates 2 vectors using rejection sampling whose polynomial |
240 | | * coefficients are in the interval [q-eta..0..eta] |
241 | | * |
242 | | * See FIPS 204, Algorithm 33, ExpandS(). |
243 | | * Note that in FIPS 204 the range -eta..eta is used. |
244 | | * |
245 | | * @param h_ctx A EVP_MD_CTX context to use to sample the seed. |
246 | | * @param md A pre-fetched SHAKE256 object. |
247 | | * @param eta Is either 2 or 4, and determines the range of the coefficients for |
248 | | * s1 and s2. |
249 | | * @param seed A 64 byte seed to use for sampling. |
250 | | * @param s1 A 1 * l column vector containing polynomials with coefficients in |
251 | | * the range (q-eta)..0..eta |
252 | | * @param s2 A 1 * k column vector containing polynomials with coefficients in |
253 | | * the range (q-eta)..0..eta |
254 | | * @returns 1 if s1 and s2 were successfully generated, or 0 otherwise. |
255 | | */ |
256 | | static int vector_expand_S_scalar(EVP_MD_CTX *h_ctx, const EVP_MD *md, int eta, |
257 | | const uint8_t *seed, VECTOR *s1, VECTOR *s2) |
258 | 0 | { |
259 | 0 | int ret = 0; |
260 | 0 | size_t i; |
261 | 0 | size_t l = s1->num_poly; |
262 | 0 | size_t k = s2->num_poly; |
263 | 0 | uint8_t derived_seed[ML_DSA_PRIV_SEED_BYTES + 2]; |
264 | 0 | COEFF_FROM_NIBBLE_FUNC *coef_from_nibble_fn; |
265 | |
|
266 | 0 | coef_from_nibble_fn = (eta == ML_DSA_ETA_4) ? coeff_from_nibble_4 : coeff_from_nibble_2; |
267 | | |
268 | | /* |
269 | | * Each polynomial generated uses a unique seed that consists of |
270 | | * seed + counter (where the counter is 2 bytes starting at 0) |
271 | | */ |
272 | 0 | memcpy(derived_seed, seed, ML_DSA_PRIV_SEED_BYTES); |
273 | 0 | derived_seed[ML_DSA_PRIV_SEED_BYTES] = 0; |
274 | 0 | derived_seed[ML_DSA_PRIV_SEED_BYTES + 1] = 0; |
275 | |
|
276 | 0 | for (i = 0; i < l; i++) { |
277 | 0 | if (!rej_bounded_poly(h_ctx, md, coef_from_nibble_fn, |
278 | 0 | derived_seed, sizeof(derived_seed), &s1->poly[i])) |
279 | 0 | goto err; |
280 | 0 | ++derived_seed[ML_DSA_PRIV_SEED_BYTES]; |
281 | 0 | } |
282 | 0 | for (i = 0; i < k; i++) { |
283 | 0 | if (!rej_bounded_poly(h_ctx, md, coef_from_nibble_fn, |
284 | 0 | derived_seed, sizeof(derived_seed), &s2->poly[i])) |
285 | 0 | goto err; |
286 | 0 | ++derived_seed[ML_DSA_PRIV_SEED_BYTES]; |
287 | 0 | } |
288 | 0 | ret = 1; |
289 | 0 | err: |
290 | 0 | OPENSSL_cleanse(derived_seed, sizeof(derived_seed)); |
291 | 0 | return ret; |
292 | 0 | } |
293 | | |
294 | | /* See FIPS 204, Algorithm 34, ExpandMask(), Step 4 & 5 */ |
295 | | int ossl_ml_dsa_poly_expand_mask(POLY *out, const uint8_t *seed, size_t seed_len, |
296 | | uint32_t gamma1, |
297 | | EVP_MD_CTX *h_ctx, const EVP_MD *md) |
298 | 0 | { |
299 | 0 | uint8_t buf[32 * 20]; |
300 | 0 | size_t buf_len = 32 * (gamma1 == ML_DSA_GAMMA1_TWO_POWER_19 ? 20 : 18); |
301 | 0 | int ret = shake_xof(h_ctx, md, seed, seed_len, buf, buf_len) |
302 | 0 | && ossl_ml_dsa_poly_decode_expand_mask(out, buf, buf_len, gamma1); |
303 | |
|
304 | 0 | OPENSSL_cleanse(buf, sizeof(buf)); |
305 | 0 | return ret; |
306 | 0 | } |
307 | | |
308 | | /* |
309 | | * @brief Sample a polynomial with coefficients in the range {-1..1}. |
310 | | * The number of non zero values (hamming weight) is given by tau |
311 | | * |
312 | | * See FIPS 204, Algorithm 29, SampleInBall() |
313 | | * This function is assumed to not be constant time. |
314 | | * The algorithm is based on Durstenfeld's version of the Fisher-Yates shuffle. |
315 | | * |
316 | | * Note that the coefficients returned by this implementation are positive |
317 | | * i.e one of q-1, 0, or 1. |
318 | | * |
319 | | * @param tau is the number of +1 or -1's in the polynomial 'out_c' (39, 49 or 60) |
320 | | * that is less than or equal to 64 |
321 | | */ |
322 | | int ossl_ml_dsa_poly_sample_in_ball(POLY *out_c, const uint8_t *seed, int seed_len, |
323 | | EVP_MD_CTX *h_ctx, const EVP_MD *md, |
324 | | uint32_t tau) |
325 | 0 | { |
326 | 0 | uint8_t block[SHAKE256_BLOCKSIZE]; |
327 | 0 | uint64_t signs; |
328 | 0 | int offset = 8; |
329 | 0 | size_t end; |
330 | | |
331 | | /* |
332 | | * Rather than squeeze 8 bytes followed by lots of 1 byte squeezes |
333 | | * the SHAKE blocksize is squeezed each time and buffered into 'block'. |
334 | | */ |
335 | 0 | if (!shake_xof(h_ctx, md, seed, seed_len, block, sizeof(block))) |
336 | 0 | return 0; |
337 | | |
338 | | /* |
339 | | * grab the first 64 bits - since tau < 64 |
340 | | * Each bit gives a +1 or -1 value. |
341 | | */ |
342 | 0 | OPENSSL_load_u64_le(&signs, block); |
343 | | |
344 | | /* |
345 | | * SampleInBall implements a Fisher-Yates shuffle whose rejection-sampling |
346 | | * inner loop and data-dependent array index unavoidably leak the structure |
347 | | * of the challenge polynomial via memory-access pattern and branch timing. |
348 | | * This is safe: c_tilde = H(mu ‖ w1) is the Fiat-Shamir commitment and is |
349 | | * published in the accepted signature, so the SHAKE bytes that build c are |
350 | | * effectively public. See the BoringSSL design discussion at |
351 | | * https://boringssl-review.googlesource.com/c/boringssl/+/67747/comment/8d8f01ac_70af3f21/ |
352 | | * |
353 | | * The first 8 bytes (the sign bits loaded into |signs| above) are left |
354 | | * tainted: they determine only the ±1 values written into c, which flow |
355 | | * into the CT arithmetic of cs1/cs2/ct0 alongside the already-tainted |
356 | | * secret polynomials and cause no spurious violations there. |
357 | | * Only the rejection-sampling bytes need to be declassified. |
358 | | */ |
359 | 0 | CONSTTIME_DECLASSIFY(block + offset, sizeof(block) - offset); |
360 | |
|
361 | 0 | poly_zero(out_c); |
362 | | |
363 | | /* Loop tau times */ |
364 | 0 | for (end = 256 - tau; end < 256; end++) { |
365 | 0 | size_t index; /* index is a random offset to write +1 or -1 */ |
366 | | |
367 | | /* rejection sample in {0..end} to choose an index to place -1 or 1 into */ |
368 | 0 | for (;;) { |
369 | 0 | if (offset == sizeof(block)) { |
370 | | /* squeeze another block if the bytes from block have been used */ |
371 | 0 | if (!EVP_DigestSqueeze(h_ctx, block, sizeof(block))) |
372 | 0 | return 0; |
373 | | /* See comment above for why the block is declassified. */ |
374 | 0 | CONSTTIME_DECLASSIFY(block, sizeof(block)); |
375 | 0 | offset = 0; |
376 | 0 | } |
377 | | |
378 | 0 | index = block[offset++]; |
379 | 0 | if (index <= end) |
380 | 0 | break; |
381 | 0 | } |
382 | | |
383 | | /* |
384 | | * In-place swap the coefficient we are about to replace to the end so |
385 | | * we don't lose any values that have been already written. |
386 | | */ |
387 | 0 | out_c->coeff[end] = out_c->coeff[index]; |
388 | | /* set the random coefficient value to either 1 or q-1 */ |
389 | 0 | out_c->coeff[index] = mod_sub(1, 2 * (signs & 1)); |
390 | 0 | signs >>= 1; /* grab the next random bit */ |
391 | 0 | } |
392 | 0 | return 1; |
393 | 0 | } |
394 | | |
395 | | static void vector_expand_mask_scalar(VECTOR *out, |
396 | | const uint8_t rho_prime[ML_DSA_RHO_PRIME_BYTES], uint32_t kappa, uint32_t gamma1, |
397 | | EVP_MD_CTX *h_ctx, const EVP_MD *md) |
398 | 0 | { |
399 | 0 | size_t i; |
400 | 0 | uint8_t derived_seed[ML_DSA_RHO_PRIME_BYTES + 2]; |
401 | |
|
402 | 0 | memcpy(derived_seed, rho_prime, ML_DSA_RHO_PRIME_BYTES); |
403 | |
|
404 | 0 | for (i = 0; i < out->num_poly; i++) { |
405 | 0 | size_t index = kappa + i; |
406 | |
|
407 | 0 | derived_seed[ML_DSA_RHO_PRIME_BYTES] = index & 0xFF; |
408 | 0 | derived_seed[ML_DSA_RHO_PRIME_BYTES + 1] = (index >> 8) & 0xFF; |
409 | 0 | poly_expand_mask(out->poly + i, derived_seed, sizeof(derived_seed), |
410 | 0 | gamma1, h_ctx, md); |
411 | 0 | } |
412 | 0 | OPENSSL_cleanse(derived_seed, sizeof(derived_seed)); |
413 | 0 | } |
414 | | |
415 | | static const OSSL_ML_DSA_SAMPLE_OPS ml_dsa_sample_generic_meth = { |
416 | | matrix_expand_A_scalar, |
417 | | vector_expand_S_scalar, |
418 | | vector_expand_mask_scalar |
419 | | }; |
420 | | |
421 | | #if defined(KECCAK1600_ASM) \ |
422 | | && (defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)) \ |
423 | | && !defined(OPENSSL_NO_ASM) |
424 | | #include "ml_dsa_sample_hw_x86_64.inc" |
425 | | const OSSL_ML_DSA_SAMPLE_OPS *ossl_ml_dsa_sample_ops(void) |
426 | | { |
427 | | if (SHA3_avx512vl_capable()) |
428 | | return &ml_dsa_sample_x86_64; |
429 | | return &ml_dsa_sample_generic_meth; |
430 | | } |
431 | | #else |
432 | | const OSSL_ML_DSA_SAMPLE_OPS *ossl_ml_dsa_sample_ops(void) |
433 | 0 | { |
434 | 0 | return &ml_dsa_sample_generic_meth; |
435 | 0 | } |
436 | | #endif |