Coverage Report

Created: 2025-06-13 06:58

/src/openssl/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
42
{
23
42
    return key->params;
24
42
}
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
16
{
29
16
    return key->seed;
30
16
}
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
474
{
41
474
    int ret = 0;
42
43
474
    if (key == NULL
44
474
        || key->pub_encoding != NULL
45
474
        || key->priv_encoding != NULL
46
474
        || (sk != NULL && sk_len != key->params->sk_len)
47
474
        || (seed != NULL && seed_len != ML_DSA_SEED_BYTES)
48
474
        || key->seed != NULL)
49
0
        return 0;
50
51
474
    if (sk != NULL
52
474
        && (key->priv_encoding = OPENSSL_memdup(sk, sk_len)) == NULL)
53
0
        goto end;
54
474
    if (seed != NULL
55
474
        && (key->seed = OPENSSL_memdup(seed, seed_len)) == NULL)
56
0
        goto end;
57
474
    key->prov_flags |= flags_set;
58
474
    key->prov_flags &= ~flags_clr;
59
474
    ret = 1;
60
61
474
 end:
62
474
    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
474
    return ret;
68
474
}
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
483
{
81
483
    ML_DSA_KEY *ret;
82
483
    const ML_DSA_PARAMS *params = ossl_ml_dsa_params_get(evp_type);
83
84
483
    if (params == NULL)
85
0
        return NULL;
86
87
483
    ret = OPENSSL_zalloc(sizeof(*ret));
88
483
    if (ret != NULL) {
89
483
        ret->libctx = libctx;
90
483
        ret->params = params;
91
483
        ret->prov_flags = ML_DSA_KEY_PROV_FLAGS_DEFAULT;
92
483
        ret->shake128_md = EVP_MD_fetch(libctx, "SHAKE-128", propq);
93
483
        ret->shake256_md = EVP_MD_fetch(libctx, "SHAKE-256", propq);
94
483
        if (ret->shake128_md == NULL || ret->shake256_md == NULL)
95
0
            goto err;
96
483
    }
97
483
    return ret;
98
0
err:
99
0
    ossl_ml_dsa_key_free(ret);
100
0
    return NULL;
101
483
}
102
103
int ossl_ml_dsa_key_pub_alloc(ML_DSA_KEY *key)
104
461
{
105
461
    if (key->t1.poly != NULL)
106
0
        return 0;
107
461
    return vector_alloc(&key->t1, key->params->k);
108
461
}
109
110
int ossl_ml_dsa_key_priv_alloc(ML_DSA_KEY *key)
111
456
{
112
456
    size_t k = key->params->k, l = key->params->l;
113
456
    POLY *poly;
114
115
456
    if (key->s1.poly != NULL)
116
0
        return 0;
117
456
    if (!vector_alloc(&key->s1, l + 2 * k))
118
0
        return 0;
119
120
456
    poly = key->s1.poly;
121
456
    key->s1.num_poly = l;
122
456
    vector_init(&key->s2, poly + l, k);
123
456
    vector_init(&key->t0, poly + l + k, k);
124
456
    return 1;
125
456
}
126
127
/**
128
 * @brief Destroy an ML_DSA_KEY object
129
 */
130
void ossl_ml_dsa_key_free(ML_DSA_KEY *key)
131
76.8k
{
132
76.8k
    if (key == NULL)
133
76.3k
        return;
134
135
486
    EVP_MD_free(key->shake128_md);
136
486
    EVP_MD_free(key->shake256_md);
137
486
    ossl_ml_dsa_key_reset(key);
138
486
    OPENSSL_free(key);
139
486
}
140
141
/**
142
 * @brief Factory reset an ML_DSA_KEY object
143
 */
144
void ossl_ml_dsa_key_reset(ML_DSA_KEY *key)
145
486
{
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
486
    if (key->s1.poly != NULL) {
151
456
        vector_zero(&key->s1);
152
456
        vector_zero(&key->s2);
153
456
        vector_zero(&key->t0);
154
456
        vector_free(&key->s1);
155
456
        key->s2.poly = NULL;
156
456
        key->t0.poly = NULL;
157
456
    }
158
    /* The |t1| vector is public and allocated separately */
159
486
    vector_free(&key->t1);
160
486
    OPENSSL_cleanse(key->K, sizeof(key->K));
161
486
    OPENSSL_free(key->pub_encoding);
162
486
    key->pub_encoding = NULL;
163
486
    if (key->priv_encoding != NULL)
164
444
        OPENSSL_clear_free(key->priv_encoding, key->params->sk_len);
165
486
    key->priv_encoding = NULL;
166
486
    if (key->seed != NULL)
167
444
        OPENSSL_clear_free(key->seed, ML_DSA_SEED_BYTES);
168
486
    key->seed = NULL;
169
486
}
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
23
{
257
23
    int key_checked = 0;
258
259
23
    if (key1->params != key2->params)
260
0
        return 0;
261
262
23
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
263
23
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
264
23
            if (key1->pub_encoding != NULL && key2->pub_encoding != NULL) {
265
23
                if (memcmp(key1->pub_encoding, key2->pub_encoding,
266
23
                                  key1->params->pk_len) != 0)
267
20
                    return 0;
268
3
                key_checked = 1;
269
3
            }
270
23
        }
271
3
        if (!key_checked
272
3
                && (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
3
        return key_checked;
281
3
    }
282
0
    return 1;
283
23
}
284
285
int ossl_ml_dsa_key_has(const ML_DSA_KEY *key, int selection)
286
53
{
287
53
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
288
        /* Note that the public key always exists if there is a private key */
289
53
        if (ossl_ml_dsa_key_get_pub(key) == NULL)
290
0
            return 0; /* No public key */
291
53
        if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
292
53
                && ossl_ml_dsa_key_get_priv(key) == NULL)
293
0
            return 0; /* No private key */
294
53
        return 1;
295
53
    }
296
0
    return 0;
297
53
}
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
444
{
314
444
    const ML_DSA_PARAMS *params = key->params;
315
444
    uint32_t k = params->k, l = params->l;
316
444
    POLY *polys;
317
444
    MATRIX a_ntt;
318
444
    VECTOR s1_ntt;
319
444
    VECTOR t;
320
321
444
    polys = OPENSSL_malloc(sizeof(*polys) * (k + l + k * l));
322
444
    if (polys == NULL)
323
0
        return 0;
324
325
444
    vector_init(&t, polys, k);
326
444
    vector_init(&s1_ntt, t.poly + k, l);
327
444
    matrix_init(&a_ntt, s1_ntt.poly + l, k, l);
328
329
    /* Using rho generate A' = A in NTT form */
330
444
    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
444
    vector_copy(&s1_ntt, &key->s1);
335
444
    vector_ntt(&s1_ntt);
336
337
444
    matrix_mult_vector(&a_ntt, &s1_ntt, &t);
338
444
    vector_ntt_inverse(&t);
339
444
    vector_add(&t, &key->s2, &t);
340
341
    /* Compress t */
342
444
    vector_power2_round(&t, t1, t0);
343
344
    /* Zeroize secret */
345
444
    vector_zero(&s1_ntt);
346
444
err:
347
444
    OPENSSL_free(polys);
348
444
    return 1;
349
444
}
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
444
{
412
444
    int ret = 0;
413
444
    uint8_t augmented_seed[ML_DSA_SEED_BYTES + 2];
414
444
    uint8_t expanded_seed[ML_DSA_RHO_BYTES + ML_DSA_PRIV_SEED_BYTES + ML_DSA_K_BYTES];
415
444
    const uint8_t *const rho = expanded_seed; /* p = Public Random Seed */
416
444
    const uint8_t *const priv_seed = expanded_seed + ML_DSA_RHO_BYTES;
417
444
    const uint8_t *const K = priv_seed + ML_DSA_PRIV_SEED_BYTES;
418
444
    const ML_DSA_PARAMS *params = out->params;
419
444
    EVP_MD_CTX *md_ctx = NULL;
420
421
444
    if (out->seed == NULL
422
444
        || (md_ctx = EVP_MD_CTX_new()) == NULL
423
444
        || !ossl_ml_dsa_key_pub_alloc(out)
424
444
        || !ossl_ml_dsa_key_priv_alloc(out))
425
0
        goto err;
426
427
    /* augmented_seed = seed || k || l */
428
444
    memcpy(augmented_seed, out->seed, ML_DSA_SEED_BYTES);
429
444
    augmented_seed[ML_DSA_SEED_BYTES] = (uint8_t)params->k;
430
444
    augmented_seed[ML_DSA_SEED_BYTES + 1] = (uint8_t)params->l;
431
    /* Expand the seed into p[32], p'[64], K[32] */
432
444
    if (!shake_xof(md_ctx, out->shake256_md, augmented_seed, sizeof(augmented_seed),
433
444
                   expanded_seed, sizeof(expanded_seed)))
434
0
        goto err;
435
436
444
    memcpy(out->rho, rho, sizeof(out->rho));
437
444
    memcpy(out->K, K, sizeof(out->K));
438
439
444
    ret = vector_expand_S(md_ctx, out->shake256_md, params->eta, priv_seed, &out->s1, &out->s2)
440
444
        && public_from_private(out, md_ctx, &out->t1, &out->t0)
441
444
        && ossl_ml_dsa_pk_encode(out)
442
444
        && shake_xof(md_ctx, out->shake256_md, out->pub_encoding, out->params->pk_len,
443
444
                     out->tr, sizeof(out->tr))
444
444
        && ossl_ml_dsa_sk_encode(out);
445
446
444
err:
447
444
    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
444
    EVP_MD_CTX_free(md_ctx);
452
444
    OPENSSL_cleanse(augmented_seed, sizeof(augmented_seed));
453
444
    OPENSSL_cleanse(expanded_seed, sizeof(expanded_seed));
454
444
    return ret;
455
444
}
456
457
int ossl_ml_dsa_generate_key(ML_DSA_KEY *out)
458
444
{
459
444
    size_t seed_len = ML_DSA_SEED_BYTES;
460
444
    uint8_t *sk;
461
444
    int ret;
462
463
444
    if (out->seed == NULL) {
464
444
        if ((out->seed = OPENSSL_malloc(seed_len)) == NULL)
465
0
            return 0;
466
444
        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
444
    }
472
    /* We're generating from a seed, drop private prekey encoding */
473
444
    sk = out->priv_encoding;
474
444
    out->priv_encoding = NULL;
475
444
    if (sk == NULL) {
476
444
        ret = keygen_internal(out);
477
444
    } 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
444
    return ret;
489
444
}
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
380
{
503
380
    return (key->params->evp_type == evp_type);
504
380
}
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
268
{
509
268
    return key->pub_encoding;
510
268
}
511
512
/* Returns the encoded public key size */
513
size_t ossl_ml_dsa_key_get_pub_len(const ML_DSA_KEY *key)
514
465
{
515
465
    return key->params->pk_len;
516
465
}
517
518
size_t ossl_ml_dsa_key_get_collision_strength_bits(const ML_DSA_KEY *key)
519
461
{
520
461
    return key->params->bit_strength;
521
461
}
522
523
int ossl_ml_dsa_key_get_security_category(const ML_DSA_KEY *key)
524
461
{
525
461
    return key->params->security_category;
526
461
}
527
528
/* Returns the private key data or NULL if there is no private key */
529
const uint8_t *ossl_ml_dsa_key_get_priv(const ML_DSA_KEY *key)
530
396
{
531
396
    return key->priv_encoding;
532
396
}
533
534
size_t ossl_ml_dsa_key_get_priv_len(const ML_DSA_KEY *key)
535
4
{
536
4
    return key->params->sk_len;
537
4
}
538
539
size_t ossl_ml_dsa_key_get_sig_len(const ML_DSA_KEY *key)
540
461
{
541
461
    return key->params->sig_len;
542
461
}
543
544
OSSL_LIB_CTX *ossl_ml_dsa_key_get0_libctx(const ML_DSA_KEY *key)
545
0
{
546
0
    return key != NULL ? key->libctx : NULL;
547
0
}
548
549
const char *ossl_ml_dsa_key_get_name(const ML_DSA_KEY *key)
550
380
{
551
380
    return key->params->alg;
552
380
}