Coverage Report

Created: 2026-07-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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 <openssl/err.h>
15
#include <openssl/proverr.h>
16
#include "internal/common.h"
17
#include "internal/constant_time.h"
18
#include "ml_dsa_local.h"
19
#include "ml_dsa_key.h"
20
#include "ml_dsa_matrix.h"
21
#include "ml_dsa_sign.h"
22
#include "ml_dsa_hash.h"
23
24
#define ML_DSA_MAX_LAMBDA 256 /* bit strength for ML-DSA-87 */
25
26
/*
27
 * @brief Initialize a Signature object by pointing all of its objects to
28
 * preallocated blocks. The values passed for hint, z and
29
 * c_tilde values are not owned/freed by the |sig| object.
30
 *
31
 * @param sig The ML_DSA_SIG to initialize.
32
 * @param hint A preallocated array of |k| polynomial blocks
33
 * @param k The number of |hint| polynomials
34
 * @param z A preallocated array of |l| polynomial blocks
35
 * @param l The number of |z| polynomials
36
 * @param c_tilde A preallocated buffer
37
 * @param c_tilde_len The size of |c_tilde|
38
 */
39
static void signature_init(ML_DSA_SIG *sig,
40
    POLY *hint, uint32_t k, POLY *z, uint32_t l,
41
    uint8_t *c_tilde, size_t c_tilde_len)
42
0
{
43
0
    vector_init(&sig->z, z, l);
44
0
    vector_init(&sig->hint, hint, k);
45
0
    sig->c_tilde = c_tilde;
46
0
    sig->c_tilde_len = c_tilde_len;
47
0
}
48
49
/*
50
 * @brief: Auxiliary functions to compute ML-DSA's MU.
51
 * This combines the steps of creating M' and concatenating it
52
 * to the Public Key Hash to obtain MU.
53
 * See FIPS 204 Algorithm 2 Step 10 (and algorithm 3 Step 5) as
54
 * well as Algorithm 7 Step 6 (and algorithm 8 Step 7)
55
 *
56
 * ML_DSA pure signatures are encoded as M' = 00 || ctx_len || ctx || msg
57
 * Where ctx is the empty string by default and ctx_len <= 255.
58
 * The message is appended to the encoded context.
59
 * Finally a public key hash is prepended, and the whole is hashed
60
 * to derive the mu value.
61
 *
62
 * @param key: A public or private ML-DSA key;
63
 * @param encode: if not set, assumes that M' is provided raw and the
64
 * following parameters are ignored.
65
 * @param ctx An optional context to add to the message encoding.
66
 * @param ctx_len The size of |ctx|. It must be in the range 0..255
67
 * @returns an EVP_MD_CTX if the operation is successful, NULL otherwise.
68
 */
69
EVP_MD_CTX *ossl_ml_dsa_mu_init_int(EVP_MD *shake256_md,
70
    const uint8_t *tr, size_t tr_len, int encode, int prehash,
71
    const uint8_t *ctx, size_t ctx_len)
72
0
{
73
0
    EVP_MD_CTX *md_ctx;
74
0
    uint8_t itb[2];
75
76
0
    md_ctx = EVP_MD_CTX_new();
77
0
    if (md_ctx == NULL)
78
0
        return NULL;
79
80
    /* H(.. */
81
0
    if (!EVP_DigestInit_ex2(md_ctx, shake256_md, NULL))
82
0
        goto err;
83
    /* ..pk (= key->tr) */
84
0
    if (!EVP_DigestUpdate(md_ctx, tr, tr_len))
85
0
        goto err;
86
    /* M' = .. */
87
0
    if (encode) {
88
0
        if (ctx_len > ML_DSA_MAX_CONTEXT_STRING_LEN)
89
0
            goto err;
90
        /* IntegerToBytes(0, 1) .. */
91
0
        itb[0] = prehash ? 1 : 0;
92
        /* || IntegerToBytes(|ctx|, 1) || .. */
93
0
        itb[1] = (uint8_t)ctx_len;
94
0
        if (!EVP_DigestUpdate(md_ctx, itb, 2))
95
0
            goto err;
96
        /* ctx || .. */
97
0
        if (!EVP_DigestUpdate(md_ctx, ctx, ctx_len))
98
0
            goto err;
99
        /* .. msg) will follow in update and final functions */
100
0
    }
101
102
0
    return md_ctx;
103
104
0
err:
105
0
    EVP_MD_CTX_free(md_ctx);
106
0
    return NULL;
107
0
}
108
109
EVP_MD_CTX *ossl_ml_dsa_mu_init(const ML_DSA_KEY *key, int encode,
110
    const uint8_t *ctx, size_t ctx_len)
111
0
{
112
0
    if (key == NULL)
113
0
        return NULL;
114
0
    return ossl_ml_dsa_mu_init_int(key->shake256_md, key->tr, sizeof(key->tr),
115
0
        encode, 0, ctx, ctx_len);
116
0
}
117
118
/*
119
 * @brief: updates the internal ML-DSA hash with an additional message chunk.
120
 *
121
 * @param md_ctx: The hashing context
122
 * @param msg: The next message chunk
123
 * @param msg_len: The length of the msg buffer to process
124
 * @returns 1 on success, 0 on error
125
 */
126
int ossl_ml_dsa_mu_update(EVP_MD_CTX *md_ctx, const uint8_t *msg, size_t msg_len)
127
0
{
128
0
    return EVP_DigestUpdate(md_ctx, msg, msg_len);
129
0
}
130
131
/*
132
 * @brief: finalizes the internal ML-DSA hash
133
 *
134
 * @param md_ctx: The hashing context
135
 * @param mu: The output buffer for Mu
136
 * @param mu_len: The size of the output buffer
137
 * @returns 1 on success, 0 on error
138
 */
139
int ossl_ml_dsa_mu_finalize(EVP_MD_CTX *md_ctx, uint8_t *mu, size_t mu_len)
140
0
{
141
0
    if (!ossl_assert(mu_len == ML_DSA_MU_BYTES)) {
142
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
143
0
        return 0;
144
0
    }
145
0
    return EVP_DigestSqueeze(md_ctx, mu, mu_len);
146
0
}
147
148
/*
149
 * @brief FIPS 204, Algorithm 7, ML-DSA.Sign_internal()
150
 *
151
 * This algorithm is decomposed in 2 steps, a set of functions to compute mu
152
 * and then the actual signing function.
153
 *
154
 * @param priv: The private ML-DSA key
155
 * @param mu: The pre-computed mu hash
156
 * @param mu_len: The length of the mu buffer
157
 * @param rnd: The random buffer
158
 * @param rnd_len: The length of the random buffer
159
 * @param out_sig: The output signature buffer
160
 * @returns 1 on success, 0 on error
161
 */
162
static int ml_dsa_sign_internal(const ML_DSA_KEY *priv,
163
    const uint8_t *mu, size_t mu_len, const uint8_t *rnd, size_t rnd_len,
164
    uint8_t *out_sig)
165
0
{
166
0
    int ret = 0;
167
0
    const OSSL_ML_DSA_SAMPLE_OPS *sample_ops = ossl_ml_dsa_sample_ops();
168
0
    const ML_DSA_PARAMS *params = priv->params;
169
0
    EVP_MD_CTX *md_ctx = NULL;
170
0
    uint32_t k = (uint32_t)params->k, l = (uint32_t)params->l;
171
0
    uint32_t gamma1 = params->gamma1, gamma2 = params->gamma2;
172
0
    uint8_t *alloc = NULL, *w1_encoded = NULL;
173
0
    void *alloc_freeptr = NULL;
174
0
    size_t alloc_len, w1_encoded_len;
175
0
    size_t num_polys_sig_k = 2 * k;
176
0
    size_t num_polys_k = 5 * k;
177
0
    size_t num_polys_l = 3 * l;
178
0
    size_t num_polys_k_by_l = k * l;
179
0
    size_t poly_count;
180
0
    POLY *p, *c_ntt;
181
0
    VECTOR s1_ntt, s2_ntt, t0_ntt, w, w1, cs1, cs2, y;
182
0
    MATRIX a_ntt;
183
0
    ML_DSA_SIG sig;
184
0
    uint8_t rho_prime[ML_DSA_RHO_PRIME_BYTES];
185
0
    uint8_t c_tilde[ML_DSA_MAX_LAMBDA / 4];
186
0
    size_t c_tilde_len = params->bit_strength >> 2;
187
0
    size_t kappa;
188
189
0
    if (mu_len != ML_DSA_MU_BYTES) {
190
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
191
0
        return 0;
192
0
    }
193
194
    /* Allocate w1_encoded buffer */
195
0
    w1_encoded_len = k * (gamma2 == ML_DSA_GAMMA2_Q_MINUS1_DIV88 ? 192 : 128);
196
0
    w1_encoded = OPENSSL_malloc(w1_encoded_len);
197
0
    if (w1_encoded == NULL)
198
0
        return 0;
199
200
    /* Allocate aligned POLY array */
201
0
    poly_count = 1 + num_polys_k + num_polys_l + num_polys_k_by_l + num_polys_sig_k;
202
0
    alloc_len = sizeof(*p) * poly_count;
203
0
    alloc = OPENSSL_aligned_alloc(alloc_len, 16, &alloc_freeptr);
204
0
    if (alloc == NULL)
205
0
        goto err;
206
207
0
    md_ctx = EVP_MD_CTX_new();
208
0
    if (md_ctx == NULL)
209
0
        goto err;
210
211
    /* Init the temp vectors to point to the aligned polys blob */
212
0
    p = (POLY *)alloc;
213
0
    c_ntt = p++;
214
0
    matrix_init(&a_ntt, p, k, l);
215
0
    p += num_polys_k_by_l;
216
0
    vector_init(&s2_ntt, p, k);
217
0
    vector_init(&t0_ntt, s2_ntt.poly + k, k);
218
0
    vector_init(&w, t0_ntt.poly + k, k);
219
0
    vector_init(&w1, w.poly + k, k);
220
0
    vector_init(&cs2, w1.poly + k, k);
221
0
    p += num_polys_k;
222
0
    vector_init(&s1_ntt, p, l);
223
0
    vector_init(&y, p + l, l);
224
0
    vector_init(&cs1, p + 2 * l, l);
225
0
    p += num_polys_l;
226
0
    signature_init(&sig, p, k, p + k, l, c_tilde, c_tilde_len);
227
    /* End of the allocated blob setup */
228
229
    /*
230
     * Mark the private key material as secret before we start computing with
231
     * it.  Any control-flow branch or memory-index that transitively depends
232
     * on these bytes will be flagged by Valgrind when the library is built
233
     * with enable-ct-validation.
234
     */
235
0
    CONSTTIME_SECRET(priv->K, sizeof(priv->K));
236
0
    CONSTTIME_SECRET_VECTOR(priv->s1);
237
0
    CONSTTIME_SECRET_VECTOR(priv->s2);
238
0
    CONSTTIME_SECRET_VECTOR(priv->t0);
239
240
0
    if (!sample_ops->matrix_expand_A(md_ctx, priv->shake128_md, priv->rho, &a_ntt))
241
0
        goto err;
242
243
    /*
244
     * rho_prime is derived from the secret K and must remain tainted
245
     * throughout the rejection loop: knowing it would let an attacker
246
     * reconstruct every mask y and recover c*s1 = z - y from the final
247
     * signature.  Do NOT declassify it.
248
     */
249
0
    if (!shake_xof_3(md_ctx, priv->shake256_md, priv->K, sizeof(priv->K),
250
0
            rnd, rnd_len, mu, mu_len,
251
0
            rho_prime, sizeof(rho_prime)))
252
0
        goto err;
253
254
0
    vector_copy(&s1_ntt, &priv->s1);
255
0
    vector_ntt(&s1_ntt);
256
0
    vector_copy(&s2_ntt, &priv->s2);
257
0
    vector_ntt(&s2_ntt);
258
0
    vector_copy(&t0_ntt, &priv->t0);
259
0
    vector_ntt(&t0_ntt);
260
261
    /*
262
     * kappa must not exceed 2^16. But the probability of it
263
     * exceeding even 1000 iterations is vanishingly small.
264
     */
265
0
    for (kappa = 0;; kappa += l) {
266
0
        VECTOR *y_ntt = &cs1;
267
0
        VECTOR *r0 = &w1;
268
0
        VECTOR *ct0 = &w1;
269
0
        uint32_t z_max, r0_max, ct0_max, h_ones;
270
271
0
        sample_ops->vector_expand_mask(&y, rho_prime,
272
0
            (uint32_t)kappa, gamma1, md_ctx, priv->shake256_md);
273
0
        vector_copy(y_ntt, &y);
274
0
        vector_ntt(y_ntt);
275
276
0
        matrix_mult_vector(&a_ntt, y_ntt, &w);
277
0
        vector_ntt_inverse(&w);
278
279
0
        vector_high_bits(&w, gamma2, &w1);
280
0
        ossl_ml_dsa_w1_encode(&w1, gamma2, w1_encoded, w1_encoded_len);
281
282
0
        if (!shake_xof_2(md_ctx, priv->shake256_md, mu, mu_len,
283
0
                w1_encoded, w1_encoded_len, c_tilde, c_tilde_len))
284
0
            break;
285
286
0
        if (!poly_sample_in_ball_ntt(c_ntt, c_tilde, (int)c_tilde_len,
287
0
                md_ctx, priv->shake256_md, params->tau))
288
0
            break;
289
290
0
        vector_mult_scalar(&s1_ntt, c_ntt, &cs1);
291
0
        vector_ntt_inverse(&cs1);
292
0
        vector_mult_scalar(&s2_ntt, c_ntt, &cs2);
293
0
        vector_ntt_inverse(&cs2);
294
295
0
        vector_add(&y, &cs1, &sig.z);
296
297
        /* r0 = lowbits(w - cs2) */
298
0
        vector_sub(&w, &cs2, r0);
299
0
        vector_low_bits(r0, gamma2, r0);
300
301
        /*
302
         * Leaking that the signature is rejected is fine: the next attempt
303
         * is (indistinguishable from) independent of this one, so an
304
         * observer learns nothing about the secret key beyond the number of
305
         * iterations, which is itself safe to reveal.
306
         * Declassify the bound-check output so that Valgrind does not flag
307
         * these intentional leaks.
308
         */
309
0
        z_max = vector_max(&sig.z);
310
0
        r0_max = vector_max_signed(r0);
311
0
        if (constant_time_declassify_u32(
312
0
                constant_time_ge(z_max, gamma1 - params->beta)
313
0
                | constant_time_ge(r0_max, gamma2 - params->beta)))
314
0
            continue;
315
316
0
        vector_mult_scalar(&t0_ntt, c_ntt, ct0);
317
0
        vector_ntt_inverse(ct0);
318
0
        vector_make_hint(ct0, &cs2, &w, gamma2, &sig.hint);
319
320
0
        ct0_max = vector_max(ct0);
321
0
        h_ones = (uint32_t)vector_count_ones(&sig.hint);
322
        /* Same reasoning applies to the leak as above */
323
0
        if (constant_time_declassify_u32(
324
0
                constant_time_ge(ct0_max, gamma2)
325
0
                | constant_time_lt(params->omega, h_ones)))
326
0
            continue;
327
328
        /*
329
         * The iteration has passed both rejection tests: the signature is
330
         * accepted.  Declassify all three public outputs before encoding.
331
         *
332
         * sig.z and sig.hint were computed from secret key material (s1,
333
         * s2, t0) and carry taint, but the rejection checks above have
334
         * verified they lie within the ranges required by the security
335
         * proof, so they reveal nothing about the key.
336
         *
337
         * c_tilde = H(mu || w1) carries taint that propagated from the
338
         * secret rho_prime through y → w → w1.  It is the Fiat-Shamir
339
         * challenge commitment and is published as part of the signature.
340
         * We defer its declassification to here (rather than immediately
341
         * after the SHAKE call) so that Valgrind can check that
342
         * poly_sample_in_ball_ntt and the NTT challenge arithmetic are
343
         * data-oblivious with respect to their tainted inputs.
344
         */
345
0
        CONSTTIME_DECLASSIFY(c_tilde, c_tilde_len);
346
0
        CONSTTIME_DECLASSIFY_VECTOR(sig.z);
347
0
        CONSTTIME_DECLASSIFY_VECTOR(sig.hint);
348
349
0
        ret = ossl_ml_dsa_sig_encode(&sig, params, out_sig);
350
0
        break;
351
0
    }
352
0
err:
353
0
    EVP_MD_CTX_free(md_ctx);
354
0
    if (alloc_freeptr != NULL) {
355
        /* Clear the actual sensitive buffer */
356
0
        if (alloc != NULL)
357
0
            OPENSSL_cleanse(alloc, alloc_len);
358
0
        OPENSSL_free(alloc_freeptr);
359
0
    }
360
0
    if (w1_encoded != NULL)
361
0
        OPENSSL_clear_free(w1_encoded, w1_encoded_len);
362
0
    OPENSSL_cleanse(rho_prime, sizeof(rho_prime));
363
    /*
364
     * Declassify the private key material before returning.  The key struct
365
     * is not owned here, so we do not free it, but we must remove the
366
     * "secret" taint so that the caller does not inherit spurious Valgrind
367
     * "uninitialised" state.  The polynomial data in |alloc| was already
368
     * zeroed and freed above; rho_prime is stack-allocated and cleansed.
369
     */
370
0
    CONSTTIME_DECLASSIFY(priv->K, sizeof(priv->K));
371
0
    CONSTTIME_DECLASSIFY_VECTOR(priv->s1);
372
0
    CONSTTIME_DECLASSIFY_VECTOR(priv->s2);
373
0
    CONSTTIME_DECLASSIFY_VECTOR(priv->t0);
374
0
    return ret;
375
0
}
376
377
/*
378
 * @brief FIPS 204, Algorithm 8, ML-DSA.Verify_internal().
379
 *
380
 * This algorithm is decomposed in 2 steps, a set of functions to compute mu
381
 * and then the actual verification function.
382
 *
383
 * @param pub: The public ML-DSA key
384
 * @param mu: The pre-computed mu hash
385
 * @param mu_len: The length of the mu buffer
386
 * @param sig_enc: The encoded signature to be verified
387
 * @param sig_enc_len: the encoded csignature length
388
 * @returns 1 on success, 0 on error
389
 */
390
static int ml_dsa_verify_internal(const ML_DSA_KEY *pub,
391
    const uint8_t *mu, size_t mu_len,
392
    const uint8_t *sig_enc, size_t sig_enc_len)
393
0
{
394
0
    int ret = 0;
395
0
    const OSSL_ML_DSA_SAMPLE_OPS *sample_ops = ossl_ml_dsa_sample_ops();
396
0
    uint8_t *alloc = NULL, *w1_encoded = NULL;
397
0
    void *alloc_freeptr = NULL;
398
0
    POLY *p, *c_ntt;
399
0
    MATRIX a_ntt;
400
0
    VECTOR az_ntt, ct1_ntt, *z_ntt, *w1, *w_approx;
401
0
    ML_DSA_SIG sig;
402
0
    const ML_DSA_PARAMS *params = pub->params;
403
0
    uint32_t k = (uint32_t)pub->params->k;
404
0
    uint32_t l = (uint32_t)pub->params->l;
405
0
    uint32_t gamma2 = params->gamma2;
406
0
    size_t w1_encoded_len;
407
0
    size_t num_polys_sig = k + l;
408
0
    size_t num_polys_k = 2 * k;
409
0
    size_t num_polys_l = 1 * l;
410
0
    size_t num_polys_k_by_l = k * l;
411
0
    size_t poly_count;
412
0
    size_t alloc_len;
413
0
    uint8_t c_tilde[ML_DSA_MAX_LAMBDA / 4];
414
0
    uint8_t c_tilde_sig[ML_DSA_MAX_LAMBDA / 4];
415
0
    EVP_MD_CTX *md_ctx = NULL;
416
0
    size_t c_tilde_len = params->bit_strength >> 2;
417
0
    uint32_t z_max;
418
419
    /* FIPS 204 compliance: Also validate signature length before decoding */
420
0
    if (mu_len != ML_DSA_MU_BYTES || sig_enc_len != params->sig_len) {
421
0
        ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
422
0
        return 0;
423
0
    }
424
425
    /* Allocate w1_encoded buffer */
426
0
    w1_encoded_len = k * (gamma2 == ML_DSA_GAMMA2_Q_MINUS1_DIV88 ? 192 : 128);
427
0
    w1_encoded = OPENSSL_malloc(w1_encoded_len);
428
0
    if (w1_encoded == NULL)
429
0
        return 0;
430
431
    /* Allocate aligned POLY array */
432
0
    poly_count = 1 + num_polys_k + num_polys_l + num_polys_k_by_l + num_polys_sig;
433
0
    alloc_len = sizeof(*p) * poly_count;
434
0
    alloc = OPENSSL_aligned_alloc(alloc_len, 16, &alloc_freeptr);
435
0
    if (alloc == NULL)
436
0
        goto err;
437
438
0
    md_ctx = EVP_MD_CTX_new();
439
0
    if (md_ctx == NULL)
440
0
        goto err;
441
442
    /* Init the temp vectors to point to the aligned polys blob */
443
0
    p = (POLY *)alloc;
444
0
    c_ntt = p++;
445
0
    matrix_init(&a_ntt, p, k, l);
446
0
    p += num_polys_k_by_l;
447
0
    signature_init(&sig, p, k, p + k, l, c_tilde_sig, c_tilde_len);
448
0
    p += num_polys_sig;
449
0
    vector_init(&az_ntt, p, k);
450
0
    vector_init(&ct1_ntt, p + k, k);
451
452
0
    if (!ossl_ml_dsa_sig_decode(&sig, sig_enc, sig_enc_len, pub->params)
453
0
        || !sample_ops->matrix_expand_A(md_ctx, pub->shake128_md, pub->rho, &a_ntt))
454
0
        goto err;
455
456
    /* Compute verifiers challenge c_ntt = NTT(SampleInBall(c_tilde)) */
457
0
    if (!poly_sample_in_ball_ntt(c_ntt, c_tilde_sig, (int)c_tilde_len,
458
0
            md_ctx, pub->shake256_md, params->tau))
459
0
        goto err;
460
461
    /* ct1_ntt = NTT(c) * NTT(t1 * 2^d) */
462
0
    vector_scale_power2_round_ntt(&pub->t1, &ct1_ntt);
463
0
    vector_mult_scalar(&ct1_ntt, c_ntt, &ct1_ntt);
464
465
    /* compute z_max early in order to reuse sig.z */
466
0
    z_max = vector_max(&sig.z);
467
468
    /* w_approx = NTT_inverse(A * NTT(z) - ct1_ntt) */
469
0
    z_ntt = &sig.z;
470
0
    vector_ntt(z_ntt);
471
0
    matrix_mult_vector(&a_ntt, z_ntt, &az_ntt);
472
0
    w_approx = &az_ntt;
473
0
    vector_sub(&az_ntt, &ct1_ntt, w_approx);
474
0
    vector_ntt_inverse(w_approx);
475
476
    /* compute w1_encoded */
477
0
    w1 = w_approx;
478
0
    vector_use_hint(&sig.hint, w_approx, gamma2, w1);
479
0
    ossl_ml_dsa_w1_encode(w1, gamma2, w1_encoded, w1_encoded_len);
480
481
0
    if (!shake_xof_3(md_ctx, pub->shake256_md, mu, mu_len,
482
0
            w1_encoded, w1_encoded_len, NULL, 0, c_tilde, c_tilde_len))
483
0
        goto err;
484
485
0
    ret = (z_max < (uint32_t)(params->gamma1 - params->beta))
486
0
        && memcmp(c_tilde, sig.c_tilde, c_tilde_len) == 0;
487
0
err:
488
0
    if (alloc_freeptr != NULL)
489
0
        OPENSSL_free(alloc_freeptr);
490
0
    OPENSSL_free(w1_encoded);
491
0
    EVP_MD_CTX_free(md_ctx);
492
0
    return ret;
493
0
}
494
495
/**
496
 * See FIPS 204 Section 5.2 Algorithm 2 ML-DSA.Sign()
497
 *
498
 * @returns 1 on success, or 0 on error.
499
 */
500
int ossl_ml_dsa_sign(const ML_DSA_KEY *priv,
501
    int msg_is_mu, const uint8_t *msg, size_t msg_len,
502
    const uint8_t *context, size_t context_len,
503
    const uint8_t *rand, size_t rand_len, int encode,
504
    unsigned char *sig, size_t *sig_len, size_t sig_size)
505
0
{
506
0
    EVP_MD_CTX *md_ctx = NULL;
507
0
    uint8_t mu[ML_DSA_MU_BYTES];
508
0
    const uint8_t *mu_ptr = mu;
509
0
    size_t mu_len = sizeof(mu);
510
0
    int ret = 0;
511
512
0
    if (ossl_ml_dsa_key_get_priv(priv) == NULL)
513
0
        return 0;
514
515
0
    if (sig_len != NULL)
516
0
        *sig_len = priv->params->sig_len;
517
518
0
    if (sig == NULL)
519
0
        return (sig_len != NULL) ? 1 : 0;
520
521
0
    if (sig_size < priv->params->sig_len)
522
0
        return 0;
523
524
0
    if (msg_is_mu) {
525
0
        mu_ptr = msg;
526
0
        mu_len = msg_len;
527
0
    } else {
528
0
        md_ctx = ossl_ml_dsa_mu_init(priv, encode, context, context_len);
529
0
        if (md_ctx == NULL)
530
0
            return 0;
531
532
0
        if (!ossl_ml_dsa_mu_update(md_ctx, msg, msg_len))
533
0
            goto err;
534
535
0
        if (!ossl_ml_dsa_mu_finalize(md_ctx, mu, mu_len))
536
0
            goto err;
537
0
    }
538
539
0
    ret = ml_dsa_sign_internal(priv, mu_ptr, mu_len, rand, rand_len, sig);
540
541
0
err:
542
0
    EVP_MD_CTX_free(md_ctx);
543
0
    return ret;
544
0
}
545
546
/**
547
 * See FIPS 203 Section 5.3 Algorithm 3 ML-DSA.Verify()
548
 * @returns 1 on success, or 0 on error.
549
 */
550
int ossl_ml_dsa_verify(const ML_DSA_KEY *pub,
551
    int msg_is_mu, const uint8_t *msg, size_t msg_len,
552
    const uint8_t *context, size_t context_len, int encode,
553
    const uint8_t *sig, size_t sig_len)
554
0
{
555
0
    EVP_MD_CTX *md_ctx = NULL;
556
0
    uint8_t mu[ML_DSA_MU_BYTES];
557
0
    const uint8_t *mu_ptr = mu;
558
0
    size_t mu_len = sizeof(mu);
559
0
    int ret = 0;
560
561
0
    if (ossl_ml_dsa_key_get_pub(pub) == NULL)
562
0
        return 0;
563
564
0
    if (msg_is_mu) {
565
0
        mu_ptr = msg;
566
0
        mu_len = msg_len;
567
0
    } else {
568
0
        md_ctx = ossl_ml_dsa_mu_init(pub, encode, context, context_len);
569
0
        if (md_ctx == NULL)
570
0
            return 0;
571
572
0
        if (!ossl_ml_dsa_mu_update(md_ctx, msg, msg_len))
573
0
            goto err;
574
575
0
        if (!ossl_ml_dsa_mu_finalize(md_ctx, mu, mu_len))
576
0
            goto err;
577
0
    }
578
579
0
    ret = ml_dsa_verify_internal(pub, mu_ptr, mu_len, sig, sig_len);
580
0
err:
581
0
    EVP_MD_CTX_free(md_ctx);
582
0
    return ret;
583
0
}