/src/openssl35/crypto/ml_dsa/ml_dsa_sign.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/core_dispatch.h> |
11 | | #include <openssl/core_names.h> |
12 | | #include <openssl/params.h> |
13 | | #include <openssl/rand.h> |
14 | | #include "ml_dsa_local.h" |
15 | | #include "ml_dsa_key.h" |
16 | | #include "ml_dsa_matrix.h" |
17 | | #include "ml_dsa_sign.h" |
18 | | #include "ml_dsa_hash.h" |
19 | | |
20 | | #define ML_DSA_MAX_LAMBDA 256 /* bit strength for ML-DSA-87 */ |
21 | | |
22 | | /* |
23 | | * @brief Initialize a Signature object by pointing all of its objects to |
24 | | * preallocated blocks. The values passed for hint, z and |
25 | | * c_tilde values are not owned/freed by the |sig| object. |
26 | | * |
27 | | * @param sig The ML_DSA_SIG to initialize. |
28 | | * @param hint A preallocated array of |k| polynomial blocks |
29 | | * @param k The number of |hint| polynomials |
30 | | * @param z A preallocated array of |l| polynomial blocks |
31 | | * @param l The number of |z| polynomials |
32 | | * @param c_tilde A preallocated buffer |
33 | | * @param c_tilde_len The size of |c_tilde| |
34 | | */ |
35 | | static void signature_init(ML_DSA_SIG *sig, |
36 | | POLY *hint, uint32_t k, POLY *z, uint32_t l, |
37 | | uint8_t *c_tilde, size_t c_tilde_len) |
38 | 1.19k | { |
39 | 1.19k | vector_init(&sig->z, z, l); |
40 | 1.19k | vector_init(&sig->hint, hint, k); |
41 | 1.19k | sig->c_tilde = c_tilde; |
42 | 1.19k | sig->c_tilde_len = c_tilde_len; |
43 | 1.19k | } |
44 | | |
45 | | /* |
46 | | * FIPS 204, Algorithm 7, ML-DSA.Sign_internal() |
47 | | * @returns 1 on success and 0 on failure. |
48 | | */ |
49 | | static int ml_dsa_sign_internal(const ML_DSA_KEY *priv, int msg_is_mu, |
50 | | const uint8_t *encoded_msg, |
51 | | size_t encoded_msg_len, |
52 | | const uint8_t *rnd, size_t rnd_len, |
53 | | uint8_t *out_sig) |
54 | 173 | { |
55 | 173 | int ret = 0; |
56 | 173 | const ML_DSA_PARAMS *params = priv->params; |
57 | 173 | EVP_MD_CTX *md_ctx = NULL; |
58 | 173 | uint32_t k = params->k, l = params->l; |
59 | 173 | uint32_t gamma1 = params->gamma1, gamma2 = params->gamma2; |
60 | 173 | uint8_t *alloc = NULL, *w1_encoded; |
61 | 173 | size_t alloc_len, w1_encoded_len; |
62 | 173 | size_t num_polys_sig_k = 2 * k; |
63 | 173 | size_t num_polys_k = 5 * k; |
64 | 173 | size_t num_polys_l = 3 * l; |
65 | 173 | size_t num_polys_k_by_l = k * l; |
66 | 173 | POLY *polys = NULL, *p, *c_ntt; |
67 | 173 | VECTOR s1_ntt, s2_ntt, t0_ntt, w, w1, cs1, cs2, y; |
68 | 173 | MATRIX a_ntt; |
69 | 173 | ML_DSA_SIG sig; |
70 | 173 | uint8_t mu[ML_DSA_MU_BYTES], *mu_ptr = mu; |
71 | 173 | const size_t mu_len = sizeof(mu); |
72 | 173 | uint8_t rho_prime[ML_DSA_RHO_PRIME_BYTES]; |
73 | 173 | uint8_t c_tilde[ML_DSA_MAX_LAMBDA / 4]; |
74 | 173 | size_t c_tilde_len = params->bit_strength >> 2; |
75 | 173 | size_t kappa; |
76 | | |
77 | | /* |
78 | | * Allocate a single blob for most of the variable size temporary variables. |
79 | | * Mostly used for VECTOR POLYNOMIALS (every POLY is 1K). |
80 | | */ |
81 | 173 | w1_encoded_len = k * (gamma2 == ML_DSA_GAMMA2_Q_MINUS1_DIV88 ? 192 : 128); |
82 | 173 | alloc_len = w1_encoded_len |
83 | 173 | + sizeof(*polys) * (1 + num_polys_k + num_polys_l + num_polys_k_by_l + num_polys_sig_k); |
84 | 173 | alloc = OPENSSL_malloc(alloc_len); |
85 | 173 | if (alloc == NULL) |
86 | 0 | return 0; |
87 | 173 | md_ctx = EVP_MD_CTX_new(); |
88 | 173 | if (md_ctx == NULL) |
89 | 0 | goto err; |
90 | | |
91 | 173 | w1_encoded = alloc; |
92 | | /* Init the temp vectors to point to the allocated polys blob */ |
93 | 173 | p = (POLY *)(w1_encoded + w1_encoded_len); |
94 | 173 | c_ntt = p++; |
95 | 173 | matrix_init(&a_ntt, p, k, l); |
96 | 173 | p += num_polys_k_by_l; |
97 | 173 | vector_init(&s2_ntt, p, k); |
98 | 173 | vector_init(&t0_ntt, s2_ntt.poly + k, k); |
99 | 173 | vector_init(&w, t0_ntt.poly + k, k); |
100 | 173 | vector_init(&w1, w.poly + k, k); |
101 | 173 | vector_init(&cs2, w1.poly + k, k); |
102 | 173 | p += num_polys_k; |
103 | 173 | vector_init(&s1_ntt, p, l); |
104 | 173 | vector_init(&y, p + l, l); |
105 | 173 | vector_init(&cs1, p + 2 * l, l); |
106 | 173 | p += num_polys_l; |
107 | 173 | signature_init(&sig, p, k, p + k, l, c_tilde, c_tilde_len); |
108 | | /* End of the allocated blob setup */ |
109 | | |
110 | 173 | if (!matrix_expand_A(md_ctx, priv->shake128_md, priv->rho, &a_ntt)) |
111 | 0 | goto err; |
112 | 173 | if (msg_is_mu) { |
113 | 0 | if (encoded_msg_len != mu_len) |
114 | 0 | goto err; |
115 | 0 | mu_ptr = (uint8_t *)encoded_msg; |
116 | 173 | } else { |
117 | 173 | if (!shake_xof_2(md_ctx, priv->shake256_md, priv->tr, sizeof(priv->tr), |
118 | 173 | encoded_msg, encoded_msg_len, mu_ptr, mu_len)) |
119 | 0 | goto err; |
120 | 173 | } |
121 | 173 | if (!shake_xof_3(md_ctx, priv->shake256_md, priv->K, sizeof(priv->K), |
122 | 173 | rnd, rnd_len, mu_ptr, mu_len, |
123 | 173 | rho_prime, sizeof(rho_prime))) |
124 | 0 | goto err; |
125 | | |
126 | 173 | vector_copy(&s1_ntt, &priv->s1); |
127 | 173 | vector_ntt(&s1_ntt); |
128 | 173 | vector_copy(&s2_ntt, &priv->s2); |
129 | 173 | vector_ntt(&s2_ntt); |
130 | 173 | vector_copy(&t0_ntt, &priv->t0); |
131 | 173 | vector_ntt(&t0_ntt); |
132 | | |
133 | | /* |
134 | | * kappa must not exceed 2^16. But the probability of it |
135 | | * exceeding even 1000 iterations is vanishingly small. |
136 | | */ |
137 | 816 | for (kappa = 0;; kappa += l) { |
138 | 816 | VECTOR *y_ntt = &cs1; |
139 | 816 | VECTOR *r0 = &w1; |
140 | 816 | VECTOR *ct0 = &w1; |
141 | 816 | uint32_t z_max, r0_max, ct0_max, h_ones; |
142 | | |
143 | 816 | vector_expand_mask(&y, rho_prime, sizeof(rho_prime), kappa, |
144 | 816 | gamma1, md_ctx, priv->shake256_md); |
145 | 816 | vector_copy(y_ntt, &y); |
146 | 816 | vector_ntt(y_ntt); |
147 | | |
148 | 816 | matrix_mult_vector(&a_ntt, y_ntt, &w); |
149 | 816 | vector_ntt_inverse(&w); |
150 | | |
151 | 816 | vector_high_bits(&w, gamma2, &w1); |
152 | 816 | ossl_ml_dsa_w1_encode(&w1, gamma2, w1_encoded, w1_encoded_len); |
153 | | |
154 | 816 | if (!shake_xof_2(md_ctx, priv->shake256_md, mu_ptr, mu_len, |
155 | 816 | w1_encoded, w1_encoded_len, c_tilde, c_tilde_len)) |
156 | 0 | break; |
157 | | |
158 | 816 | if (!poly_sample_in_ball_ntt(c_ntt, c_tilde, c_tilde_len, |
159 | 816 | md_ctx, priv->shake256_md, params->tau)) |
160 | 0 | break; |
161 | | |
162 | 816 | vector_mult_scalar(&s1_ntt, c_ntt, &cs1); |
163 | 816 | vector_ntt_inverse(&cs1); |
164 | 816 | vector_mult_scalar(&s2_ntt, c_ntt, &cs2); |
165 | 816 | vector_ntt_inverse(&cs2); |
166 | | |
167 | 816 | vector_add(&y, &cs1, &sig.z); |
168 | | |
169 | | /* r0 = lowbits(w - cs2) */ |
170 | 816 | vector_sub(&w, &cs2, r0); |
171 | 816 | vector_low_bits(r0, gamma2, r0); |
172 | | |
173 | | /* |
174 | | * Leaking that the signature is rejected is fine as the next attempt at a |
175 | | * signature will be (indistinguishable from) independent of this one. |
176 | | */ |
177 | 816 | z_max = vector_max(&sig.z); |
178 | 816 | r0_max = vector_max_signed(r0); |
179 | 816 | if (value_barrier_32(constant_time_ge(z_max, gamma1 - params->beta) |
180 | 816 | | constant_time_ge(r0_max, gamma2 - params->beta))) |
181 | 640 | continue; |
182 | | |
183 | 176 | vector_mult_scalar(&t0_ntt, c_ntt, ct0); |
184 | 176 | vector_ntt_inverse(ct0); |
185 | 176 | vector_make_hint(ct0, &cs2, &w, gamma2, &sig.hint); |
186 | | |
187 | 176 | ct0_max = vector_max(ct0); |
188 | 176 | h_ones = vector_count_ones(&sig.hint); |
189 | | /* Same reasoning applies to the leak as above */ |
190 | 176 | if (value_barrier_32(constant_time_ge(ct0_max, gamma2) |
191 | 176 | | constant_time_lt(params->omega, h_ones))) |
192 | 3 | continue; |
193 | 173 | ret = ossl_ml_dsa_sig_encode(&sig, params, out_sig); |
194 | 173 | break; |
195 | 176 | } |
196 | 173 | err: |
197 | 173 | EVP_MD_CTX_free(md_ctx); |
198 | 173 | OPENSSL_clear_free(alloc, alloc_len); |
199 | 173 | OPENSSL_cleanse(rho_prime, sizeof(rho_prime)); |
200 | 173 | return ret; |
201 | 173 | } |
202 | | |
203 | | /* |
204 | | * See FIPS 204, Algorithm 8, ML-DSA.Verify_internal(). |
205 | | */ |
206 | | static int ml_dsa_verify_internal(const ML_DSA_KEY *pub, int msg_is_mu, |
207 | | const uint8_t *msg_enc, size_t msg_enc_len, |
208 | | const uint8_t *sig_enc, size_t sig_enc_len) |
209 | 173 | { |
210 | 173 | int ret = 0; |
211 | 173 | uint8_t *alloc = NULL, *w1_encoded; |
212 | 173 | POLY *polys = NULL, *p, *c_ntt; |
213 | 173 | MATRIX a_ntt; |
214 | 173 | VECTOR az_ntt, ct1_ntt, *z_ntt, *w1, *w_approx; |
215 | 173 | ML_DSA_SIG sig; |
216 | 173 | const ML_DSA_PARAMS *params = pub->params; |
217 | 173 | uint32_t k = pub->params->k; |
218 | 173 | uint32_t l = pub->params->l; |
219 | 173 | uint32_t gamma2 = params->gamma2; |
220 | 173 | size_t w1_encoded_len; |
221 | 173 | size_t num_polys_sig = k + l; |
222 | 173 | size_t num_polys_k = 2 * k; |
223 | 173 | size_t num_polys_l = 1 * l; |
224 | 173 | size_t num_polys_k_by_l = k * l; |
225 | 173 | uint8_t mu[ML_DSA_MU_BYTES], *mu_ptr = mu; |
226 | 173 | const size_t mu_len = sizeof(mu); |
227 | 173 | uint8_t c_tilde[ML_DSA_MAX_LAMBDA / 4]; |
228 | 173 | uint8_t c_tilde_sig[ML_DSA_MAX_LAMBDA / 4]; |
229 | 173 | EVP_MD_CTX *md_ctx = NULL; |
230 | 173 | size_t c_tilde_len = params->bit_strength >> 2; |
231 | 173 | uint32_t z_max; |
232 | | |
233 | | /* Allocate space for all the POLYNOMIALS used by temporary VECTORS */ |
234 | 173 | w1_encoded_len = k * (gamma2 == ML_DSA_GAMMA2_Q_MINUS1_DIV88 ? 192 : 128); |
235 | 173 | alloc = OPENSSL_malloc(w1_encoded_len |
236 | 173 | + sizeof(*polys) * (1 + num_polys_k + num_polys_l + num_polys_k_by_l + num_polys_sig)); |
237 | 173 | if (alloc == NULL) |
238 | 0 | return 0; |
239 | 173 | md_ctx = EVP_MD_CTX_new(); |
240 | 173 | if (md_ctx == NULL) |
241 | 0 | goto err; |
242 | | |
243 | 173 | w1_encoded = alloc; |
244 | | /* Init the temp vectors to point to the allocated polys blob */ |
245 | 173 | p = (POLY *)(w1_encoded + w1_encoded_len); |
246 | 173 | c_ntt = p++; |
247 | 173 | matrix_init(&a_ntt, p, k, l); |
248 | 173 | p += num_polys_k_by_l; |
249 | 173 | signature_init(&sig, p, k, p + k, l, c_tilde_sig, c_tilde_len); |
250 | 173 | p += num_polys_sig; |
251 | 173 | vector_init(&az_ntt, p, k); |
252 | 173 | vector_init(&ct1_ntt, p + k, k); |
253 | | |
254 | 173 | if (!ossl_ml_dsa_sig_decode(&sig, sig_enc, sig_enc_len, pub->params) |
255 | 173 | || !matrix_expand_A(md_ctx, pub->shake128_md, pub->rho, &a_ntt)) |
256 | 0 | goto err; |
257 | 173 | if (msg_is_mu) { |
258 | 0 | if (msg_enc_len != mu_len) |
259 | 0 | goto err; |
260 | 0 | mu_ptr = (uint8_t *)msg_enc; |
261 | 173 | } else { |
262 | 173 | if (!shake_xof_2(md_ctx, pub->shake256_md, pub->tr, sizeof(pub->tr), |
263 | 173 | msg_enc, msg_enc_len, mu_ptr, mu_len)) |
264 | 0 | goto err; |
265 | 173 | } |
266 | | /* Compute verifiers challenge c_ntt = NTT(SampleInBall(c_tilde) */ |
267 | 173 | if (!poly_sample_in_ball_ntt(c_ntt, c_tilde_sig, c_tilde_len, |
268 | 173 | md_ctx, pub->shake256_md, params->tau)) |
269 | 0 | goto err; |
270 | | |
271 | | /* ct1_ntt = NTT(c) * NTT(t1 * 2^d) */ |
272 | 173 | vector_scale_power2_round_ntt(&pub->t1, &ct1_ntt); |
273 | 173 | vector_mult_scalar(&ct1_ntt, c_ntt, &ct1_ntt); |
274 | | |
275 | | /* compute z_max early in order to reuse sig.z */ |
276 | 173 | z_max = vector_max(&sig.z); |
277 | | |
278 | | /* w_approx = NTT_inverse(A * NTT(z) - ct1_ntt) */ |
279 | 173 | z_ntt = &sig.z; |
280 | 173 | vector_ntt(z_ntt); |
281 | 173 | matrix_mult_vector(&a_ntt, z_ntt, &az_ntt); |
282 | 173 | w_approx = &az_ntt; |
283 | 173 | vector_sub(&az_ntt, &ct1_ntt, w_approx); |
284 | 173 | vector_ntt_inverse(w_approx); |
285 | | |
286 | | /* compute w1_encoded */ |
287 | 173 | w1 = w_approx; |
288 | 173 | vector_use_hint(&sig.hint, w_approx, gamma2, w1); |
289 | 173 | ossl_ml_dsa_w1_encode(w1, gamma2, w1_encoded, w1_encoded_len); |
290 | | |
291 | 173 | if (!shake_xof_3(md_ctx, pub->shake256_md, mu_ptr, mu_len, |
292 | 173 | w1_encoded, w1_encoded_len, NULL, 0, c_tilde, c_tilde_len)) |
293 | 0 | goto err; |
294 | | |
295 | 173 | ret = (z_max < (uint32_t)(params->gamma1 - params->beta)) |
296 | 173 | && memcmp(c_tilde, sig.c_tilde, c_tilde_len) == 0; |
297 | 173 | err: |
298 | 173 | OPENSSL_free(alloc); |
299 | 173 | EVP_MD_CTX_free(md_ctx); |
300 | 173 | return ret; |
301 | 173 | } |
302 | | |
303 | | /** |
304 | | * @brief Encode a message |
305 | | * See FIPS 204 Algorithm 2 Step 10 (and algorithm 3 Step 5). |
306 | | * |
307 | | * ML_DSA pure signatures are encoded as M' = 00 || ctx_len || ctx || msg |
308 | | * Where ctx is the empty string by default and ctx_len <= 255. |
309 | | * |
310 | | * Note this code could be shared with SLH_DSA |
311 | | * |
312 | | * @param msg A message to encode |
313 | | * @param msg_len The size of |msg| |
314 | | * @param ctx An optional context to add to the message encoding. |
315 | | * @param ctx_len The size of |ctx|. It must be in the range 0..255 |
316 | | * @param encode Use the Pure signature encoding if this is 1, and dont encode |
317 | | * if this value is 0. |
318 | | * @param tmp A small buffer that may be used if the message is small. |
319 | | * @param tmp_len The size of |tmp| |
320 | | * @param out_len The size of the returned encoded buffer. |
321 | | * @returns A buffer containing the encoded message. If the passed in |
322 | | * |tmp| buffer is big enough to hold the encoded message then it returns |tmp| |
323 | | * otherwise it allocates memory which must be freed by the caller. If |encode| |
324 | | * is 0 then it returns |msg|. NULL is returned if there is a failure. |
325 | | */ |
326 | | static uint8_t *msg_encode(const uint8_t *msg, size_t msg_len, |
327 | | const uint8_t *ctx, size_t ctx_len, int encode, |
328 | | uint8_t *tmp, size_t tmp_len, size_t *out_len) |
329 | 346 | { |
330 | 346 | uint8_t *encoded = NULL; |
331 | 346 | size_t encoded_len; |
332 | | |
333 | 346 | if (encode == 0) { |
334 | | /* Raw message */ |
335 | 0 | *out_len = msg_len; |
336 | 0 | return (uint8_t *)msg; |
337 | 0 | } |
338 | 346 | if (ctx_len > ML_DSA_MAX_CONTEXT_STRING_LEN) |
339 | 0 | return NULL; |
340 | | |
341 | | /* Pure encoding */ |
342 | 346 | encoded_len = 1 + 1 + ctx_len + msg_len; |
343 | 346 | *out_len = encoded_len; |
344 | 346 | if (encoded_len <= tmp_len) { |
345 | 346 | encoded = tmp; |
346 | 346 | } else { |
347 | 0 | encoded = OPENSSL_malloc(encoded_len); |
348 | 0 | if (encoded == NULL) |
349 | 0 | return NULL; |
350 | 0 | } |
351 | 346 | encoded[0] = 0; |
352 | 346 | encoded[1] = (uint8_t)ctx_len; |
353 | 346 | memcpy(&encoded[2], ctx, ctx_len); |
354 | 346 | memcpy(&encoded[2 + ctx_len], msg, msg_len); |
355 | 346 | return encoded; |
356 | 346 | } |
357 | | |
358 | | /** |
359 | | * See FIPS 204 Section 5.2 Algorithm 2 ML-DSA.Sign() |
360 | | * |
361 | | * @returns 1 on success, or 0 on error. |
362 | | */ |
363 | | int ossl_ml_dsa_sign(const ML_DSA_KEY *priv, int msg_is_mu, |
364 | | const uint8_t *msg, size_t msg_len, |
365 | | const uint8_t *context, size_t context_len, |
366 | | const uint8_t *rand, size_t rand_len, int encode, |
367 | | unsigned char *sig, size_t *sig_len, size_t sig_size) |
368 | 346 | { |
369 | 346 | int ret = 1; |
370 | 346 | uint8_t m_tmp[1024], *m = m_tmp, *alloced_m = NULL; |
371 | 346 | size_t m_len = 0; |
372 | | |
373 | 346 | if (ossl_ml_dsa_key_get_priv(priv) == NULL) |
374 | 0 | return 0; |
375 | 346 | if (sig != NULL) { |
376 | 173 | if (sig_size < priv->params->sig_len) |
377 | 0 | return 0; |
378 | 173 | if (msg_is_mu) { |
379 | 0 | m = (uint8_t *)msg; |
380 | 0 | m_len = msg_len; |
381 | 173 | } else { |
382 | 173 | m = msg_encode(msg, msg_len, context, context_len, encode, |
383 | 173 | m_tmp, sizeof(m_tmp), &m_len); |
384 | 173 | if (m == NULL) |
385 | 0 | return 0; |
386 | 173 | if (m != msg && m != m_tmp) |
387 | 0 | alloced_m = m; |
388 | 173 | } |
389 | 173 | ret = ml_dsa_sign_internal(priv, msg_is_mu, m, m_len, rand, rand_len, sig); |
390 | 173 | OPENSSL_free(alloced_m); |
391 | 173 | } |
392 | 346 | if (sig_len != NULL) |
393 | 346 | *sig_len = priv->params->sig_len; |
394 | 346 | return ret; |
395 | 346 | } |
396 | | |
397 | | /** |
398 | | * See FIPS 203 Section 5.3 Algorithm 3 ML-DSA.Verify() |
399 | | * @returns 1 on success, or 0 on error. |
400 | | */ |
401 | | int ossl_ml_dsa_verify(const ML_DSA_KEY *pub, int msg_is_mu, |
402 | | const uint8_t *msg, size_t msg_len, |
403 | | const uint8_t *context, size_t context_len, int encode, |
404 | | const uint8_t *sig, size_t sig_len) |
405 | 173 | { |
406 | 173 | uint8_t *m, *alloced_m = NULL; |
407 | 173 | size_t m_len; |
408 | 173 | uint8_t m_tmp[1024]; |
409 | 173 | int ret = 0; |
410 | | |
411 | 173 | if (ossl_ml_dsa_key_get_pub(pub) == NULL) |
412 | 0 | return 0; |
413 | | |
414 | 173 | if (msg_is_mu) { |
415 | 0 | m = (uint8_t *)msg; |
416 | 0 | m_len = msg_len; |
417 | 173 | } else { |
418 | 173 | m = msg_encode(msg, msg_len, context, context_len, encode, |
419 | 173 | m_tmp, sizeof(m_tmp), &m_len); |
420 | 173 | if (m == NULL) |
421 | 0 | return 0; |
422 | 173 | if (m != msg && m != m_tmp) |
423 | 0 | alloced_m = m; |
424 | 173 | } |
425 | | |
426 | 173 | ret = ml_dsa_verify_internal(pub, msg_is_mu, m, m_len, sig, sig_len); |
427 | 173 | OPENSSL_free(alloced_m); |
428 | 173 | return ret; |
429 | 173 | } |