Coverage Report

Created: 2026-09-12 06:55

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