/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 | | /* |
223 | | * The seeds derived below and the sampling buffers in rej_ntt_poly() are |
224 | | * not cleansed: per FIPS 204 section 3.6.3 the matrix A is easily |
225 | | * computed from the public key and does not require any special |
226 | | * protections. |
227 | | */ |
228 | | |
229 | | /* The seed used for each matrix element is rho + column_index + row_index */ |
230 | 0 | memcpy(derived_seed, rho, ML_DSA_RHO_BYTES); |
231 | 0 | for (i = 0; i < out->k; i++) { |
232 | 0 | for (j = 0; j < out->l; j++) { |
233 | 0 | derived_seed[ML_DSA_RHO_BYTES + 1] = (uint8_t)i; |
234 | 0 | derived_seed[ML_DSA_RHO_BYTES] = (uint8_t)j; |
235 | | /* Generate the polynomial for each matrix element using a unique seed */ |
236 | 0 | if (!rej_ntt_poly(g_ctx, md, derived_seed, sizeof(derived_seed), poly++)) |
237 | 0 | goto err; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | ret = 1; |
241 | 0 | err: |
242 | 0 | return ret; |
243 | 0 | } |
244 | | |
245 | | /** |
246 | | * @brief Generates 2 vectors using rejection sampling whose polynomial |
247 | | * coefficients are in the interval [q-eta..0..eta] |
248 | | * |
249 | | * See FIPS 204, Algorithm 33, ExpandS(). |
250 | | * Note that in FIPS 204 the range -eta..eta is used. |
251 | | * |
252 | | * @param h_ctx A EVP_MD_CTX context to use to sample the seed. |
253 | | * @param md A pre-fetched SHAKE256 object. |
254 | | * @param eta Is either 2 or 4, and determines the range of the coefficients for |
255 | | * s1 and s2. |
256 | | * @param seed A 64 byte seed to use for sampling. |
257 | | * @param s1 A 1 * l column vector containing polynomials with coefficients in |
258 | | * the range (q-eta)..0..eta |
259 | | * @param s2 A 1 * k column vector containing polynomials with coefficients in |
260 | | * the range (q-eta)..0..eta |
261 | | * @returns 1 if s1 and s2 were successfully generated, or 0 otherwise. |
262 | | */ |
263 | | static int vector_expand_S_scalar(EVP_MD_CTX *h_ctx, const EVP_MD *md, int eta, |
264 | | const uint8_t *seed, VECTOR *s1, VECTOR *s2) |
265 | 0 | { |
266 | 0 | int ret = 0; |
267 | 0 | size_t i; |
268 | 0 | size_t l = s1->num_poly; |
269 | 0 | size_t k = s2->num_poly; |
270 | 0 | uint8_t derived_seed[ML_DSA_PRIV_SEED_BYTES + 2]; |
271 | 0 | COEFF_FROM_NIBBLE_FUNC *coef_from_nibble_fn; |
272 | |
|
273 | 0 | coef_from_nibble_fn = (eta == ML_DSA_ETA_4) ? coeff_from_nibble_4 : coeff_from_nibble_2; |
274 | | |
275 | | /* |
276 | | * Each polynomial generated uses a unique seed that consists of |
277 | | * seed + counter (where the counter is 2 bytes starting at 0) |
278 | | */ |
279 | 0 | memcpy(derived_seed, seed, ML_DSA_PRIV_SEED_BYTES); |
280 | 0 | derived_seed[ML_DSA_PRIV_SEED_BYTES] = 0; |
281 | 0 | derived_seed[ML_DSA_PRIV_SEED_BYTES + 1] = 0; |
282 | |
|
283 | 0 | for (i = 0; i < l; i++) { |
284 | 0 | if (!rej_bounded_poly(h_ctx, md, coef_from_nibble_fn, |
285 | 0 | derived_seed, sizeof(derived_seed), &s1->poly[i])) |
286 | 0 | goto err; |
287 | 0 | ++derived_seed[ML_DSA_PRIV_SEED_BYTES]; |
288 | 0 | } |
289 | 0 | for (i = 0; i < k; i++) { |
290 | 0 | if (!rej_bounded_poly(h_ctx, md, coef_from_nibble_fn, |
291 | 0 | derived_seed, sizeof(derived_seed), &s2->poly[i])) |
292 | 0 | goto err; |
293 | 0 | ++derived_seed[ML_DSA_PRIV_SEED_BYTES]; |
294 | 0 | } |
295 | 0 | ret = 1; |
296 | 0 | err: |
297 | 0 | OPENSSL_cleanse(derived_seed, sizeof(derived_seed)); |
298 | 0 | return ret; |
299 | 0 | } |
300 | | |
301 | | /* See FIPS 204, Algorithm 34, ExpandMask(), Step 4 & 5 */ |
302 | | int ossl_ml_dsa_poly_expand_mask(POLY *out, const uint8_t *seed, size_t seed_len, |
303 | | uint32_t gamma1, |
304 | | EVP_MD_CTX *h_ctx, const EVP_MD *md) |
305 | 0 | { |
306 | 0 | uint8_t buf[32 * 20]; |
307 | 0 | size_t buf_len = 32 * (gamma1 == ML_DSA_GAMMA1_TWO_POWER_19 ? 20 : 18); |
308 | 0 | int ret = shake_xof(h_ctx, md, seed, seed_len, buf, buf_len) |
309 | 0 | && ossl_ml_dsa_poly_decode_expand_mask(out, buf, buf_len, gamma1); |
310 | |
|
311 | 0 | OPENSSL_cleanse(buf, sizeof(buf)); |
312 | 0 | return ret; |
313 | 0 | } |
314 | | |
315 | | /* |
316 | | * @brief Sample a polynomial with coefficients in the range {-1..1}. |
317 | | * The number of non zero values (hamming weight) is given by tau |
318 | | * |
319 | | * See FIPS 204, Algorithm 29, SampleInBall() |
320 | | * This function is assumed to not be constant time. |
321 | | * The algorithm is based on Durstenfeld's version of the Fisher-Yates shuffle. |
322 | | * |
323 | | * Note that the coefficients returned by this implementation are positive |
324 | | * i.e one of q-1, 0, or 1. |
325 | | * |
326 | | * @param tau is the number of +1 or -1's in the polynomial 'out_c' (39, 49 or 60) |
327 | | * that is less than or equal to 64 |
328 | | */ |
329 | | int ossl_ml_dsa_poly_sample_in_ball(POLY *out_c, const uint8_t *seed, int seed_len, |
330 | | EVP_MD_CTX *h_ctx, const EVP_MD *md, |
331 | | uint32_t tau) |
332 | 0 | { |
333 | 0 | uint8_t block[SHAKE256_BLOCKSIZE]; |
334 | 0 | uint64_t signs; |
335 | 0 | int offset = 8; |
336 | 0 | size_t end; |
337 | 0 | int ret = 0; |
338 | | |
339 | | /* |
340 | | * Rather than squeeze 8 bytes followed by lots of 1 byte squeezes |
341 | | * the SHAKE blocksize is squeezed each time and buffered into 'block'. |
342 | | */ |
343 | 0 | if (!shake_xof(h_ctx, md, seed, seed_len, block, sizeof(block))) |
344 | 0 | goto err; |
345 | | |
346 | | /* |
347 | | * grab the first 64 bits - since tau < 64 |
348 | | * Each bit gives a +1 or -1 value. |
349 | | */ |
350 | 0 | OPENSSL_load_u64_le(&signs, block); |
351 | | |
352 | | /* |
353 | | * SampleInBall implements a Fisher-Yates shuffle whose rejection-sampling |
354 | | * inner loop and data-dependent array index unavoidably leak the structure |
355 | | * of the challenge polynomial via memory-access pattern and branch timing. |
356 | | * This is safe: c_tilde = H(mu ‖ w1) is the Fiat-Shamir commitment and is |
357 | | * published in the accepted signature, so the SHAKE bytes that build c are |
358 | | * effectively public. See the BoringSSL design discussion at |
359 | | * https://boringssl-review.googlesource.com/c/boringssl/+/67747/comment/8d8f01ac_70af3f21/ |
360 | | * |
361 | | * The first 8 bytes (the sign bits loaded into |signs| above) are left |
362 | | * tainted: they determine only the ±1 values written into c, which flow |
363 | | * into the CT arithmetic of cs1/cs2/ct0 alongside the already-tainted |
364 | | * secret polynomials and cause no spurious violations there. |
365 | | * Only the rejection-sampling bytes need to be declassified. |
366 | | */ |
367 | 0 | CONSTTIME_DECLASSIFY(block + offset, sizeof(block) - offset); |
368 | |
|
369 | 0 | poly_zero(out_c); |
370 | | |
371 | | /* Loop tau times */ |
372 | 0 | for (end = 256 - tau; end < 256; end++) { |
373 | 0 | size_t index; /* index is a random offset to write +1 or -1 */ |
374 | | |
375 | | /* rejection sample in {0..end} to choose an index to place -1 or 1 into */ |
376 | 0 | for (;;) { |
377 | 0 | if (offset == sizeof(block)) { |
378 | | /* squeeze another block if the bytes from block have been used */ |
379 | 0 | if (!EVP_DigestSqueeze(h_ctx, block, sizeof(block))) |
380 | 0 | goto err; |
381 | | /* See comment above for why the block is declassified. */ |
382 | 0 | CONSTTIME_DECLASSIFY(block, sizeof(block)); |
383 | 0 | offset = 0; |
384 | 0 | } |
385 | | |
386 | 0 | index = block[offset++]; |
387 | 0 | if (index <= end) |
388 | 0 | break; |
389 | 0 | } |
390 | | |
391 | | /* |
392 | | * In-place swap the coefficient we are about to replace to the end so |
393 | | * we don't lose any values that have been already written. |
394 | | */ |
395 | 0 | out_c->coeff[end] = out_c->coeff[index]; |
396 | | /* set the random coefficient value to either 1 or q-1 */ |
397 | 0 | out_c->coeff[index] = mod_sub(1, 2 * (signs & 1)); |
398 | 0 | signs >>= 1; /* grab the next random bit */ |
399 | 0 | } |
400 | 0 | ret = 1; |
401 | 0 | err: |
402 | 0 | OPENSSL_cleanse(block, sizeof(block)); |
403 | 0 | return ret; |
404 | 0 | } |
405 | | |
406 | | static void vector_expand_mask_scalar(VECTOR *out, |
407 | | const uint8_t rho_prime[ML_DSA_RHO_PRIME_BYTES], uint32_t kappa, uint32_t gamma1, |
408 | | EVP_MD_CTX *h_ctx, const EVP_MD *md) |
409 | 0 | { |
410 | 0 | size_t i; |
411 | 0 | uint8_t derived_seed[ML_DSA_RHO_PRIME_BYTES + 2]; |
412 | |
|
413 | 0 | memcpy(derived_seed, rho_prime, ML_DSA_RHO_PRIME_BYTES); |
414 | |
|
415 | 0 | for (i = 0; i < out->num_poly; i++) { |
416 | 0 | size_t index = kappa + i; |
417 | |
|
418 | 0 | derived_seed[ML_DSA_RHO_PRIME_BYTES] = index & 0xFF; |
419 | 0 | derived_seed[ML_DSA_RHO_PRIME_BYTES + 1] = (index >> 8) & 0xFF; |
420 | 0 | poly_expand_mask(out->poly + i, derived_seed, sizeof(derived_seed), |
421 | 0 | gamma1, h_ctx, md); |
422 | 0 | } |
423 | 0 | OPENSSL_cleanse(derived_seed, sizeof(derived_seed)); |
424 | 0 | } |
425 | | |
426 | | static const OSSL_ML_DSA_SAMPLE_OPS ml_dsa_sample_generic_meth = { |
427 | | matrix_expand_A_scalar, |
428 | | vector_expand_S_scalar, |
429 | | vector_expand_mask_scalar |
430 | | }; |
431 | | |
432 | | #if defined(KECCAK1600_ASM) \ |
433 | | && (defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)) \ |
434 | | && !defined(OPENSSL_NO_ASM) |
435 | | #include "ml_dsa_sample_hw_x86_64.inc" |
436 | | const OSSL_ML_DSA_SAMPLE_OPS *ossl_ml_dsa_sample_ops(void) |
437 | | { |
438 | | if (SHA3_avx512vl_capable()) |
439 | | return &ml_dsa_sample_x86_64; |
440 | | return &ml_dsa_sample_generic_meth; |
441 | | } |
442 | | #else |
443 | | const OSSL_ML_DSA_SAMPLE_OPS *ossl_ml_dsa_sample_ops(void) |
444 | 0 | { |
445 | 0 | return &ml_dsa_sample_generic_meth; |
446 | 0 | } |
447 | | #endif |