Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/ml_dsa/ml_dsa_key.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/err.h>
13
#include <openssl/params.h>
14
#include <openssl/proverr.h>
15
#include <openssl/rand.h>
16
#include "ml_dsa_key.h"
17
#include "ml_dsa_matrix.h"
18
#include "ml_dsa_hash.h"
19
#include "internal/encoder.h"
20
21
const ML_DSA_PARAMS *ossl_ml_dsa_key_params(const ML_DSA_KEY *key)
22
677
{
23
677
    return key->params;
24
677
}
25
26
/* Returns the seed data or NULL if there is no seed */
27
const uint8_t *ossl_ml_dsa_key_get_seed(const ML_DSA_KEY *key)
28
1.56k
{
29
1.56k
    return key->seed;
30
1.56k
}
31
32
int ossl_ml_dsa_key_get_prov_flags(const ML_DSA_KEY *key)
33
47
{
34
47
    return key->prov_flags;
35
47
}
36
37
int ossl_ml_dsa_set_prekey(ML_DSA_KEY *key, int flags_set, int flags_clr,
38
    const uint8_t *seed, size_t seed_len,
39
    const uint8_t *sk, size_t sk_len)
40
1.80k
{
41
1.80k
    int ret = 0;
42
43
1.80k
    if (key == NULL
44
1.80k
        || key->pub_encoding != NULL
45
1.80k
        || key->priv_encoding != NULL
46
1.80k
        || (sk != NULL && sk_len != key->params->sk_len)
47
1.80k
        || (seed != NULL && seed_len != ML_DSA_SEED_BYTES)
48
1.80k
        || key->seed != NULL)
49
0
        return 0;
50
51
1.80k
    if (sk != NULL) {
52
0
        key->priv_encoding = OPENSSL_secure_malloc(sk_len);
53
0
        if (key->priv_encoding == NULL)
54
0
            goto end;
55
0
        memcpy(key->priv_encoding, sk, sk_len);
56
0
    }
57
58
1.80k
    if (seed != NULL) {
59
96
        if ((key->seed = OPENSSL_secure_malloc(seed_len)) == NULL)
60
0
            goto end;
61
96
        memcpy(key->seed, seed, seed_len);
62
96
    }
63
1.80k
    key->prov_flags |= flags_set;
64
1.80k
    key->prov_flags &= ~flags_clr;
65
1.80k
    ret = 1;
66
67
1.80k
end:
68
1.80k
    if (!ret) {
69
0
        OPENSSL_secure_clear_free(key->priv_encoding, sk_len);
70
0
        OPENSSL_secure_clear_free(key->seed, seed_len);
71
0
        key->priv_encoding = key->seed = NULL;
72
0
    }
73
1.80k
    return ret;
74
1.80k
}
75
76
/*
77
 * @brief Fetch digest algorithms based on a propq.
78
 * For the import case ossl_ml_dsa_key_new() gets passed a NULL propq,
79
 * so the propq is optionally deferred to the import using OSSL_PARAM.
80
 */
81
int ossl_ml_dsa_key_fetch_digests(ML_DSA_KEY *key, const char *propq)
82
715
{
83
715
    EVP_MD_free(key->shake128_md);
84
715
    EVP_MD_free(key->shake256_md);
85
715
    key->shake128_md = EVP_MD_fetch(key->libctx, "SHAKE-128", propq);
86
715
    key->shake256_md = EVP_MD_fetch(key->libctx, "SHAKE-256", propq);
87
715
    return (key->shake128_md != NULL && key->shake256_md != NULL);
88
715
}
89
90
/**
91
 * @brief Create a new ML_DSA_KEY object
92
 *
93
 * @param libctx A OSSL_LIB_CTX object used for fetching algorithms.
94
 * @param propq The property query used for fetching algorithms
95
 * @param alg The algorithm name associated with the key type
96
 * @returns The new ML_DSA_KEY object on success, or NULL on malloc failure
97
 */
98
ML_DSA_KEY *ossl_ml_dsa_key_new(OSSL_LIB_CTX *libctx, const char *propq,
99
    int evp_type)
100
715
{
101
715
    ML_DSA_KEY *ret;
102
715
    const ML_DSA_PARAMS *params = ossl_ml_dsa_params_get(evp_type);
103
104
715
    if (params == NULL)
105
0
        return NULL;
106
107
715
    ret = OPENSSL_zalloc(sizeof(*ret));
108
715
    if (ret != NULL) {
109
715
        ret->libctx = libctx;
110
715
        ret->params = params;
111
715
        ret->prov_flags = ML_DSA_KEY_PROV_FLAGS_DEFAULT;
112
715
        if (!ossl_ml_dsa_key_fetch_digests(ret, propq))
113
0
            goto err;
114
715
    }
115
715
    return ret;
116
0
err:
117
0
    ossl_ml_dsa_key_free(ret);
118
0
    return NULL;
119
715
}
120
121
int ossl_ml_dsa_key_pub_alloc(ML_DSA_KEY *key)
122
2.34k
{
123
2.34k
    if (key->t1.poly != NULL)
124
0
        return 0;
125
2.34k
    return vector_alloc(&key->t1, key->params->k);
126
2.34k
}
127
128
int ossl_ml_dsa_key_priv_alloc(ML_DSA_KEY *key)
129
2.34k
{
130
2.34k
    size_t k = key->params->k, l = key->params->l;
131
2.34k
    POLY *poly;
132
133
2.34k
    if (key->s1.poly != NULL)
134
0
        return 0;
135
2.34k
    if (!vector_secure_alloc(&key->s1, l + 2 * k))
136
0
        return 0;
137
138
2.34k
    poly = key->s1.poly;
139
2.34k
    key->s1.num_poly = l;
140
2.34k
    vector_init(&key->s2, poly + l, k);
141
2.34k
    vector_init(&key->t0, poly + l + k, k);
142
2.34k
    return 1;
143
2.34k
}
144
145
/**
146
 * @brief Destroy an ML_DSA_KEY object
147
 */
148
void ossl_ml_dsa_key_free(ML_DSA_KEY *key)
149
210k
{
150
210k
    if (key == NULL)
151
207k
        return;
152
153
2.48k
    EVP_MD_free(key->shake128_md);
154
2.48k
    EVP_MD_free(key->shake256_md);
155
2.48k
    ossl_ml_dsa_key_reset(key);
156
2.48k
    OPENSSL_free(key);
157
2.48k
}
158
159
/**
160
 * @brief Factory reset an ML_DSA_KEY object
161
 */
162
void ossl_ml_dsa_key_reset(ML_DSA_KEY *key)
163
2.48k
{
164
    /*
165
     * The allocation for |s1.poly| subsumes those for |s2| and |t0|, which we
166
     * must not access after |s1|'s poly is freed.
167
     */
168
2.48k
    if (key->s1.poly != NULL) {
169
2.34k
        const ML_DSA_PARAMS *params = key->params;
170
2.34k
        size_t k = params->k, l = params->l;
171
172
2.34k
        vector_secure_free(&key->s1, l + 2 * k);
173
2.34k
        vector_init(&key->s2, NULL, 0);
174
2.34k
        vector_init(&key->t0, NULL, 0);
175
2.34k
    }
176
    /* The |t1| vector is public and allocated separately */
177
2.48k
    vector_free(&key->t1);
178
2.48k
    OPENSSL_cleanse(key->K, sizeof(key->K));
179
2.48k
    OPENSSL_free(key->pub_encoding);
180
2.48k
    key->pub_encoding = NULL;
181
2.48k
    if (key->priv_encoding != NULL)
182
2.29k
        OPENSSL_secure_clear_free(key->priv_encoding, key->params->sk_len);
183
2.48k
    key->priv_encoding = NULL;
184
2.48k
    if (key->seed != NULL)
185
2.26k
        OPENSSL_secure_clear_free(key->seed, ML_DSA_SEED_BYTES);
186
2.48k
    key->seed = NULL;
187
2.48k
}
188
189
/**
190
 * @brief Duplicate a key
191
 *
192
 * @param src A ML_DSA_KEY object to copy
193
 * @param selection to select public and/or private components. Selecting the
194
 *                  private key will also select the public key
195
 * @returns The duplicated key, or NULL on failure.
196
 */
197
ML_DSA_KEY *ossl_ml_dsa_key_dup(const ML_DSA_KEY *src, int selection)
198
55
{
199
55
    ML_DSA_KEY *ret = NULL;
200
201
55
    if (src == NULL)
202
0
        return NULL;
203
204
    /* Prekeys with just a seed or private key are not dupable */
205
55
    if (src->pub_encoding == NULL
206
0
        && (src->priv_encoding != NULL || src->seed != NULL))
207
0
        return NULL;
208
209
55
    ret = OPENSSL_zalloc(sizeof(*ret));
210
55
    if (ret != NULL) {
211
55
        ret->libctx = src->libctx;
212
55
        ret->params = src->params;
213
55
        ret->prov_flags = src->prov_flags;
214
55
        if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
215
55
            if (src->pub_encoding != NULL) {
216
                /* The public components are present if the private key is present */
217
55
                memcpy(ret->rho, src->rho, sizeof(src->rho));
218
55
                memcpy(ret->tr, src->tr, sizeof(src->tr));
219
55
                if (src->t1.poly != NULL) {
220
55
                    if (!ossl_ml_dsa_key_pub_alloc(ret))
221
0
                        goto err;
222
55
                    vector_copy(&ret->t1, &src->t1);
223
55
                }
224
55
                if ((ret->pub_encoding = OPENSSL_memdup(src->pub_encoding,
225
55
                         src->params->pk_len))
226
55
                    == NULL)
227
0
                    goto err;
228
55
            }
229
55
            if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
230
55
                if (src->priv_encoding != NULL) {
231
49
                    memcpy(ret->K, src->K, sizeof(src->K));
232
49
                    if (src->s1.poly != NULL) {
233
49
                        if (!ossl_ml_dsa_key_priv_alloc(ret))
234
0
                            goto err;
235
49
                        vector_copy(&ret->s1, &src->s1);
236
49
                        vector_copy(&ret->s2, &src->s2);
237
49
                        vector_copy(&ret->t0, &src->t0);
238
49
                    }
239
49
                    ret->priv_encoding = OPENSSL_secure_malloc(src->params->sk_len);
240
49
                    if (ret->priv_encoding == NULL)
241
0
                        goto err;
242
49
                    memcpy(ret->priv_encoding, src->priv_encoding, src->params->sk_len);
243
49
                }
244
55
                if (src->seed != NULL) {
245
49
                    ret->seed = OPENSSL_secure_malloc(ML_DSA_SEED_BYTES);
246
49
                    if (ret->seed == NULL)
247
0
                        goto err;
248
49
                    memcpy(ret->seed, src->seed, ML_DSA_SEED_BYTES);
249
49
                }
250
55
            }
251
55
        }
252
55
        EVP_MD_up_ref(src->shake128_md);
253
55
        EVP_MD_up_ref(src->shake256_md);
254
55
        ret->shake128_md = src->shake128_md;
255
55
        ret->shake256_md = src->shake256_md;
256
55
    }
257
55
    return ret;
258
0
err:
259
0
    ossl_ml_dsa_key_free(ret);
260
0
    return NULL;
261
55
}
262
263
/**
264
 * @brief Are 2 keys equal?
265
 *
266
 * To be equal the keys must have matching public or private key data and
267
 * contain the same parameters.
268
 * (Note that in OpenSSL that the private key always has a public key component).
269
 *
270
 * @param key1 A ML_DSA_KEY object
271
 * @param key2 A ML_DSA_KEY object
272
 * @param selection to select public and/or private component comparison.
273
 * @returns 1 if the keys are equal otherwise it returns 0.
274
 */
275
int ossl_ml_dsa_key_equal(const ML_DSA_KEY *key1, const ML_DSA_KEY *key2,
276
    int selection)
277
165
{
278
165
    int key_checked = 0;
279
280
165
    if (key1->params != key2->params)
281
0
        return 0;
282
283
165
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
284
165
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
285
165
            if (key1->pub_encoding != NULL && key2->pub_encoding != NULL) {
286
165
                if (memcmp(key1->pub_encoding, key2->pub_encoding,
287
165
                        key1->params->pk_len)
288
165
                    != 0)
289
62
                    return 0;
290
103
                key_checked = 1;
291
103
            }
292
165
        }
293
103
        if (!key_checked
294
0
            && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
295
0
            if (key1->priv_encoding != NULL && key2->priv_encoding != NULL) {
296
0
                if (CRYPTO_memcmp(key1->priv_encoding, key2->priv_encoding,
297
0
                        key1->params->sk_len)
298
0
                    != 0)
299
0
                    return 0;
300
0
                key_checked = 1;
301
0
            }
302
0
        }
303
103
        return key_checked;
304
103
    }
305
0
    return 1;
306
165
}
307
308
int ossl_ml_dsa_key_has(const ML_DSA_KEY *key, int selection)
309
575
{
310
575
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
311
        /* Note that the public key always exists if there is a private key */
312
575
        if (ossl_ml_dsa_key_get_pub(key) == NULL)
313
26
            return 0; /* No public key */
314
549
        if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
315
94
            && ossl_ml_dsa_key_get_priv(key) == NULL)
316
0
            return 0; /* No private key */
317
549
        return 1;
318
549
    }
319
0
    return 0;
320
575
}
321
322
/*
323
 * @brief Given a key containing private key values for rho, s1 & s2
324
 * generate the public value t and return the compressed values t1, t0.
325
 *
326
 * @param key A private key containing params, rh0, s1 & s2.
327
 * @param md_ctx A EVP_MD_CTX used for sampling.
328
 * @param t1 The returned polynomial encoding of the 10 MSB of each coefficient
329
 *        of the uncompressed public key polynomial t.
330
 * @param t0 The returned polynomial encoding of the 13 LSB of each coefficient
331
 *        of the uncompressed public key polynomial t.
332
 * @returns 1 on success, or 0 on failure.
333
 */
334
static int public_from_private(const ML_DSA_KEY *key, EVP_MD_CTX *md_ctx,
335
    const OSSL_ML_DSA_SAMPLE_OPS *sample_ops, VECTOR *t1, VECTOR *t0)
336
2.17k
{
337
2.17k
    int ret = 0;
338
2.17k
    const ML_DSA_PARAMS *params = key->params;
339
2.17k
    uint32_t k = (uint32_t)params->k, l = (uint32_t)params->l;
340
2.17k
    POLY *polys;
341
2.17k
    MATRIX a_ntt;
342
2.17k
    VECTOR s1_ntt;
343
2.17k
    VECTOR t;
344
345
2.17k
    polys = OPENSSL_malloc_array(k + l + k * l, sizeof(*polys));
346
2.17k
    if (polys == NULL)
347
0
        return 0;
348
349
2.17k
    vector_init(&t, polys, k);
350
2.17k
    vector_init(&s1_ntt, t.poly + k, l);
351
2.17k
    matrix_init(&a_ntt, s1_ntt.poly + l, k, l);
352
353
    /* Using rho generate A' = A in NTT form */
354
2.17k
    if (!sample_ops->matrix_expand_A(md_ctx, key->shake128_md, key->rho, &a_ntt))
355
0
        goto err;
356
357
    /* t = NTT_inv(A' * NTT(s1)) + s2 */
358
2.17k
    vector_copy(&s1_ntt, &key->s1);
359
2.17k
    vector_ntt(&s1_ntt);
360
361
2.17k
    matrix_mult_vector(&a_ntt, &s1_ntt, &t);
362
2.17k
    vector_ntt_inverse(&t);
363
2.17k
    vector_add(&t, &key->s2, &t);
364
365
    /* Compress t */
366
2.17k
    vector_power2_round(&t, t1, t0);
367
368
2.17k
    ret = 1;
369
2.17k
err:
370
    /*
371
     * The low bits of |t| are private and |s1_ntt| is secret, wipe both.
372
     * The trailing |a_ntt| matrix is not wiped: per FIPS 204 section 3.6.3
373
     * the matrix A is easily computed from the public key and does not
374
     * require any special protections.
375
     */
376
2.17k
    OPENSSL_cleanse(polys, (k + l) * sizeof(*polys));
377
2.17k
    OPENSSL_free(polys);
378
2.17k
    return ret;
379
2.17k
}
380
381
int ossl_ml_dsa_key_public_from_private(ML_DSA_KEY *key)
382
2
{
383
2
    int ret = 0;
384
2
    const OSSL_ML_DSA_SAMPLE_OPS *sample_ops = ossl_ml_dsa_sample_ops();
385
2
    VECTOR t0;
386
2
    EVP_MD_CTX *md_ctx = NULL;
387
388
2
    if (!vector_alloc(&t0, key->params->k)) /* t0 is already in the private key */
389
0
        return 0;
390
2
    ret = ((md_ctx = EVP_MD_CTX_new()) != NULL)
391
2
        && ossl_ml_dsa_key_pub_alloc(key) /* allocate space for t1 */
392
2
        && public_from_private(key, md_ctx, sample_ops, &key->t1, &t0)
393
2
        && vector_equal(&t0, &key->t0) /* compare the generated t0 to the expected */
394
0
        && ossl_ml_dsa_pk_encode(key)
395
0
        && shake_xof(md_ctx, key->shake256_md,
396
0
            key->pub_encoding, key->params->pk_len,
397
0
            key->tr, sizeof(key->tr));
398
2
    vector_zero(&t0);
399
2
    vector_free(&t0);
400
2
    EVP_MD_CTX_free(md_ctx);
401
2
    return ret;
402
2
}
403
404
int ossl_ml_dsa_key_pairwise_check(const ML_DSA_KEY *key)
405
0
{
406
0
    int ret = 0;
407
0
    const OSSL_ML_DSA_SAMPLE_OPS *sample_ops = ossl_ml_dsa_sample_ops();
408
0
    VECTOR t1, t0;
409
0
    POLY *polys = NULL;
410
0
    uint32_t k = (uint32_t)key->params->k;
411
0
    EVP_MD_CTX *md_ctx = NULL;
412
413
0
    if (key->pub_encoding == NULL || key->priv_encoding == 0)
414
0
        return 0;
415
416
0
    polys = OPENSSL_malloc_array(2 * k, sizeof(*polys));
417
0
    if (polys == NULL)
418
0
        return 0;
419
0
    md_ctx = EVP_MD_CTX_new();
420
0
    if (md_ctx == NULL)
421
0
        goto err;
422
423
0
    vector_init(&t1, polys, k);
424
0
    vector_init(&t0, polys + k, k);
425
0
    if (!public_from_private(key, md_ctx, sample_ops, &t1, &t0))
426
0
        goto err;
427
428
0
    ret = vector_equal(&t1, &key->t1) && vector_equal(&t0, &key->t0);
429
0
err:
430
0
    EVP_MD_CTX_free(md_ctx);
431
0
    OPENSSL_clear_free(polys, 2 * k * sizeof(*polys));
432
0
    return ret;
433
0
}
434
435
/*
436
 * @brief Generate a public-private key pair from a seed.
437
 * See FIPS 204, Algorithm 6 ML-DSA.KeyGen_internal().
438
 *
439
 * @param out The generated key (which contains params on input)
440
 *
441
 * @returns 1 on success or 0 on failure.
442
 */
443
static int keygen_internal(ML_DSA_KEY *out)
444
1.17k
{
445
1.17k
    int ret = 0;
446
1.17k
    const OSSL_ML_DSA_SAMPLE_OPS *sample_ops = ossl_ml_dsa_sample_ops();
447
1.17k
    uint8_t augmented_seed[ML_DSA_SEED_BYTES + 2];
448
1.17k
    uint8_t expanded_seed[ML_DSA_RHO_BYTES + ML_DSA_PRIV_SEED_BYTES + ML_DSA_K_BYTES];
449
1.17k
    const uint8_t *const rho = expanded_seed; /* p = Public Random Seed */
450
1.17k
    const uint8_t *const priv_seed = expanded_seed + ML_DSA_RHO_BYTES;
451
1.17k
    const uint8_t *const K = priv_seed + ML_DSA_PRIV_SEED_BYTES;
452
1.17k
    const ML_DSA_PARAMS *params = out->params;
453
1.17k
    EVP_MD_CTX *md_ctx = NULL;
454
455
1.17k
    if (out->seed == NULL
456
1.17k
        || (md_ctx = EVP_MD_CTX_new()) == NULL
457
1.17k
        || !ossl_ml_dsa_key_pub_alloc(out)
458
1.17k
        || !ossl_ml_dsa_key_priv_alloc(out))
459
0
        goto err;
460
461
    /* augmented_seed = seed || k || l */
462
1.17k
    memcpy(augmented_seed, out->seed, ML_DSA_SEED_BYTES);
463
1.17k
    augmented_seed[ML_DSA_SEED_BYTES] = (uint8_t)params->k;
464
1.17k
    augmented_seed[ML_DSA_SEED_BYTES + 1] = (uint8_t)params->l;
465
    /* Expand the seed into p[32], p'[64], K[32] */
466
1.17k
    if (!shake_xof(md_ctx, out->shake256_md, augmented_seed, sizeof(augmented_seed),
467
1.17k
            expanded_seed, sizeof(expanded_seed)))
468
0
        goto err;
469
470
1.17k
    memcpy(out->rho, rho, sizeof(out->rho));
471
1.17k
    memcpy(out->K, K, sizeof(out->K));
472
473
1.17k
    ret = sample_ops->vector_expand_S(md_ctx, out->shake256_md, params->eta,
474
1.17k
              priv_seed, &out->s1, &out->s2)
475
1.17k
        && public_from_private(out, md_ctx, sample_ops, &out->t1, &out->t0)
476
1.17k
        && ossl_ml_dsa_pk_encode(out)
477
1.17k
        && shake_xof(md_ctx, out->shake256_md, out->pub_encoding, out->params->pk_len,
478
1.17k
            out->tr, sizeof(out->tr))
479
1.17k
        && ossl_ml_dsa_sk_encode(out);
480
481
1.17k
err:
482
1.17k
    EVP_MD_CTX_free(md_ctx);
483
1.17k
    OPENSSL_cleanse(augmented_seed, sizeof(augmented_seed));
484
1.17k
    OPENSSL_cleanse(expanded_seed, sizeof(expanded_seed));
485
1.17k
    return ret;
486
1.17k
}
487
488
int ossl_ml_dsa_generate_key(ML_DSA_KEY *out)
489
2.17k
{
490
2.17k
    size_t seed_len = ML_DSA_SEED_BYTES;
491
2.17k
    uint8_t *sk;
492
2.17k
    int ret;
493
494
2.17k
    if (out->seed == NULL) {
495
1.98k
        if ((out->seed = OPENSSL_secure_malloc(seed_len)) == NULL)
496
0
            return 0;
497
1.98k
        if (RAND_priv_bytes_ex(out->libctx, out->seed, seed_len, 0) <= 0) {
498
0
            OPENSSL_secure_free(out->seed);
499
0
            out->seed = NULL;
500
0
            return 0;
501
0
        }
502
1.98k
    }
503
    /* We're generating from a seed, drop private prekey encoding */
504
2.17k
    sk = out->priv_encoding;
505
2.17k
    out->priv_encoding = NULL;
506
2.17k
    if (sk == NULL) {
507
2.17k
        ret = keygen_internal(out);
508
2.17k
    } else {
509
        /*
510
         * A constant-time comparison is unnecessary here since this check
511
         * is only performed during key generation and is not exposed to
512
         * timing attacks.
513
         */
514
0
        if ((ret = keygen_internal(out)) != 0
515
0
            && memcmp(out->priv_encoding, sk, out->params->sk_len) != 0) {
516
0
            ret = 0;
517
0
            ossl_ml_dsa_key_reset(out);
518
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
519
0
                "explicit %s private key does not match seed",
520
0
                out->params->alg);
521
0
        }
522
0
        OPENSSL_secure_clear_free(sk, out->params->sk_len);
523
0
    }
524
2.17k
    return ret;
525
2.17k
}
526
527
/**
528
 * @brief This is used when a ML DSA key is used for an operation.
529
 * This checks that the algorithm is the same (i.e. uses the same parameters)
530
 *
531
 * @param key A ML_DSA key to use for an operation.
532
 * @param evp_type The algorithm nid associated with an operation
533
 *
534
 * @returns 1 if the algorithm matches, or 0 otherwise.
535
 */
536
537
int ossl_ml_dsa_key_matches(const ML_DSA_KEY *key, int evp_type)
538
1.78k
{
539
1.78k
    return (key->params->evp_type == evp_type);
540
1.78k
}
541
542
/* Returns the public key data or NULL if there is no public key */
543
const uint8_t *ossl_ml_dsa_key_get_pub(const ML_DSA_KEY *key)
544
2.98k
{
545
2.98k
    return key->pub_encoding;
546
2.98k
}
547
548
/* Returns the encoded public key size */
549
size_t ossl_ml_dsa_key_get_pub_len(const ML_DSA_KEY *key)
550
2.35k
{
551
2.35k
    return key->params->pk_len;
552
2.35k
}
553
554
size_t ossl_ml_dsa_key_get_collision_strength_bits(const ML_DSA_KEY *key)
555
2.33k
{
556
2.33k
    return key->params->bit_strength;
557
2.33k
}
558
559
int ossl_ml_dsa_key_get_security_category(const ML_DSA_KEY *key)
560
1.71k
{
561
1.71k
    return key->params->security_category;
562
1.71k
}
563
564
/* Returns the private key data or NULL if there is no private key */
565
const uint8_t *ossl_ml_dsa_key_get_priv(const ML_DSA_KEY *key)
566
3.44k
{
567
3.44k
    return key->priv_encoding;
568
3.44k
}
569
570
size_t ossl_ml_dsa_key_get_priv_len(const ML_DSA_KEY *key)
571
49
{
572
49
    return key->params->sk_len;
573
49
}
574
575
size_t ossl_ml_dsa_key_get_sig_len(const ML_DSA_KEY *key)
576
2.33k
{
577
2.33k
    return key->params->sig_len;
578
2.33k
}
579
580
OSSL_LIB_CTX *ossl_ml_dsa_key_get0_libctx(const ML_DSA_KEY *key)
581
0
{
582
0
    return key != NULL ? key->libctx : NULL;
583
0
}
584
585
const char *ossl_ml_dsa_key_get_name(const ML_DSA_KEY *key)
586
1.78k
{
587
1.78k
    return key->params->alg;
588
1.78k
}