Coverage Report

Created: 2025-08-28 07:07

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