Coverage Report

Created: 2025-12-10 06:24

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