Coverage Report

Created: 2025-11-16 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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
370
{
23
370
    return key->params;
24
370
}
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.06k
{
29
1.06k
    return key->seed;
30
1.06k
}
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
618
{
41
618
    int ret = 0;
42
43
618
    if (key == NULL
44
618
        || key->pub_encoding != NULL
45
618
        || key->priv_encoding != NULL
46
618
        || (sk != NULL && sk_len != key->params->sk_len)
47
618
        || (seed != NULL && seed_len != ML_DSA_SEED_BYTES)
48
618
        || key->seed != NULL)
49
0
        return 0;
50
51
618
    if (sk != NULL
52
0
        && (key->priv_encoding = OPENSSL_memdup(sk, sk_len)) == NULL)
53
0
        goto end;
54
618
    if (seed != NULL
55
52
        && (key->seed = OPENSSL_memdup(seed, seed_len)) == NULL)
56
0
        goto end;
57
618
    key->prov_flags |= flags_set;
58
618
    key->prov_flags &= ~flags_clr;
59
618
    ret = 1;
60
61
618
 end:
62
618
    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
618
    return ret;
68
618
}
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
1.05k
{
81
1.05k
    ML_DSA_KEY *ret;
82
1.05k
    const ML_DSA_PARAMS *params = ossl_ml_dsa_params_get(evp_type);
83
84
1.05k
    if (params == NULL)
85
0
        return NULL;
86
87
1.05k
    ret = OPENSSL_zalloc(sizeof(*ret));
88
1.05k
    if (ret != NULL) {
89
1.05k
        ret->libctx = libctx;
90
1.05k
        ret->params = params;
91
1.05k
        ret->prov_flags = ML_DSA_KEY_PROV_FLAGS_DEFAULT;
92
1.05k
        ret->shake128_md = EVP_MD_fetch(libctx, "SHAKE-128", propq);
93
1.05k
        ret->shake256_md = EVP_MD_fetch(libctx, "SHAKE-256", propq);
94
1.05k
        if (ret->shake128_md == NULL || ret->shake256_md == NULL)
95
0
            goto err;
96
1.05k
    }
97
1.05k
    return ret;
98
0
err:
99
0
    ossl_ml_dsa_key_free(ret);
100
0
    return NULL;
101
1.05k
}
102
103
int ossl_ml_dsa_key_pub_alloc(ML_DSA_KEY *key)
104
1.06k
{
105
1.06k
    if (key->t1.poly != NULL)
106
0
        return 0;
107
1.06k
    return vector_alloc(&key->t1, key->params->k);
108
1.06k
}
109
110
int ossl_ml_dsa_key_priv_alloc(ML_DSA_KEY *key)
111
1.05k
{
112
1.05k
    size_t k = key->params->k, l = key->params->l;
113
1.05k
    POLY *poly;
114
115
1.05k
    if (key->s1.poly != NULL)
116
0
        return 0;
117
1.05k
    if (!vector_alloc(&key->s1, l + 2 * k))
118
0
        return 0;
119
120
1.05k
    poly = key->s1.poly;
121
1.05k
    key->s1.num_poly = l;
122
1.05k
    vector_init(&key->s2, poly + l, k);
123
1.05k
    vector_init(&key->t0, poly + l + k, k);
124
1.05k
    return 1;
125
1.05k
}
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
1.12k
    EVP_MD_free(key->shake128_md);
136
1.12k
    EVP_MD_free(key->shake256_md);
137
1.12k
    ossl_ml_dsa_key_reset(key);
138
1.12k
    OPENSSL_free(key);
139
1.12k
}
140
141
/**
142
 * @brief Factory reset an ML_DSA_KEY object
143
 */
144
void ossl_ml_dsa_key_reset(ML_DSA_KEY *key)
145
1.12k
{
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
1.12k
    if (key->s1.poly != NULL) {
151
1.05k
        vector_zero(&key->s1);
152
1.05k
        vector_zero(&key->s2);
153
1.05k
        vector_zero(&key->t0);
154
1.05k
        vector_free(&key->s1);
155
1.05k
        key->s2.poly = NULL;
156
1.05k
        key->t0.poly = NULL;
157
1.05k
    }
158
    /* The |t1| vector is public and allocated separately */
159
1.12k
    vector_free(&key->t1);
160
1.12k
    OPENSSL_cleanse(key->K, sizeof(key->K));
161
1.12k
    OPENSSL_free(key->pub_encoding);
162
1.12k
    key->pub_encoding = NULL;
163
1.12k
    if (key->priv_encoding != NULL)
164
1.02k
        OPENSSL_clear_free(key->priv_encoding, key->params->sk_len);
165
1.12k
    key->priv_encoding = NULL;
166
1.12k
    if (key->seed != NULL)
167
1.02k
        OPENSSL_clear_free(key->seed, ML_DSA_SEED_BYTES);
168
1.12k
    key->seed = NULL;
169
1.12k
}
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
4
{
181
4
    ML_DSA_KEY *ret = NULL;
182
183
4
    if (src == NULL)
184
0
        return NULL;
185
186
    /* Prekeys with just a seed or private key are not dupable */
187
4
    if (src->pub_encoding == NULL
188
0
        && (src->priv_encoding != NULL || src->seed != NULL))
189
0
        return NULL;
190
191
4
    ret = OPENSSL_zalloc(sizeof(*ret));
192
4
    if (ret != NULL) {
193
4
        ret->libctx = src->libctx;
194
4
        ret->params = src->params;
195
4
        ret->prov_flags = src->prov_flags;
196
4
        if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
197
4
            if (src->pub_encoding != NULL) {
198
                /* The public components are present if the private key is present */
199
4
                memcpy(ret->rho, src->rho, sizeof(src->rho));
200
4
                memcpy(ret->tr, src->tr, sizeof(src->tr));
201
4
                if (src->t1.poly != NULL) {
202
4
                    if (!ossl_ml_dsa_key_pub_alloc(ret))
203
0
                        goto err;
204
4
                    vector_copy(&ret->t1, &src->t1);
205
4
                }
206
4
                if ((ret->pub_encoding = OPENSSL_memdup(src->pub_encoding,
207
4
                                                        src->params->pk_len)) == NULL)
208
0
                    goto err;
209
4
            }
210
4
            if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
211
4
                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
4
                if (src->seed != NULL
226
0
                    && (ret->seed = OPENSSL_memdup(src->seed,
227
0
                                                   ML_DSA_SEED_BYTES)) == NULL)
228
0
                    goto err;
229
4
            }
230
4
        }
231
4
        EVP_MD_up_ref(src->shake128_md);
232
4
        EVP_MD_up_ref(src->shake256_md);
233
4
        ret->shake128_md = src->shake128_md;
234
4
        ret->shake256_md = src->shake256_md;
235
4
    }
236
4
    return ret;
237
0
 err:
238
0
    ossl_ml_dsa_key_free(ret);
239
0
    return NULL;
240
4
}
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
89
{
257
89
    int key_checked = 0;
258
259
89
    if (key1->params != key2->params)
260
0
        return 0;
261
262
89
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
263
89
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
264
89
            if (key1->pub_encoding != NULL && key2->pub_encoding != NULL) {
265
89
                if (memcmp(key1->pub_encoding, key2->pub_encoding,
266
89
                                  key1->params->pk_len) != 0)
267
26
                    return 0;
268
63
                key_checked = 1;
269
63
            }
270
89
        }
271
63
        if (!key_checked
272
0
                && (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
63
        return key_checked;
281
63
    }
282
0
    return 1;
283
89
}
284
285
int ossl_ml_dsa_key_has(const ML_DSA_KEY *key, int selection)
286
305
{
287
305
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
288
        /* Note that the public key always exists if there is a private key */
289
305
        if (ossl_ml_dsa_key_get_pub(key) == NULL)
290
0
            return 0; /* No public key */
291
305
        if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
292
52
                && ossl_ml_dsa_key_get_priv(key) == NULL)
293
0
            return 0; /* No private key */
294
305
        return 1;
295
305
    }
296
0
    return 0;
297
305
}
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
968
{
314
968
    int ret = 0;
315
968
    const ML_DSA_PARAMS *params = key->params;
316
968
    uint32_t k = params->k, l = params->l;
317
968
    POLY *polys;
318
968
    MATRIX a_ntt;
319
968
    VECTOR s1_ntt;
320
968
    VECTOR t;
321
322
968
    polys = OPENSSL_malloc(sizeof(*polys) * (k + l + k * l));
323
968
    if (polys == NULL)
324
0
        return 0;
325
326
968
    vector_init(&t, polys, k);
327
968
    vector_init(&s1_ntt, t.poly + k, l);
328
968
    matrix_init(&a_ntt, s1_ntt.poly + l, k, l);
329
330
    /* Using rho generate A' = A in NTT form */
331
968
    if (!matrix_expand_A(md_ctx, key->shake128_md, key->rho, &a_ntt))
332
0
        goto err;
333
334
    /* t = NTT_inv(A' * NTT(s1)) + s2 */
335
968
    vector_copy(&s1_ntt, &key->s1);
336
968
    vector_ntt(&s1_ntt);
337
338
968
    matrix_mult_vector(&a_ntt, &s1_ntt, &t);
339
968
    vector_ntt_inverse(&t);
340
968
    vector_add(&t, &key->s2, &t);
341
342
    /* Compress t */
343
968
    vector_power2_round(&t, t1, t0);
344
345
    /* Zeroize secret */
346
968
    vector_zero(&s1_ntt);
347
968
    ret = 1;
348
968
err:
349
968
    OPENSSL_free(polys);
350
968
    return ret;
351
968
}
352
353
int ossl_ml_dsa_key_public_from_private(ML_DSA_KEY *key)
354
0
{
355
0
    int ret = 0;
356
0
    VECTOR t0;
357
0
    EVP_MD_CTX *md_ctx = NULL;
358
359
0
    if (!vector_alloc(&t0, key->params->k)) /* t0 is already in the private key */
360
0
        return 0;
361
0
    ret = ((md_ctx = EVP_MD_CTX_new())!= NULL)
362
0
        && ossl_ml_dsa_key_pub_alloc(key)  /* allocate space for t1 */
363
0
        && public_from_private(key, md_ctx, &key->t1, &t0)
364
0
        && vector_equal(&t0, &key->t0) /* compare the generated t0 to the expected */
365
0
        && ossl_ml_dsa_pk_encode(key)
366
0
        && shake_xof(md_ctx, key->shake256_md,
367
0
                     key->pub_encoding, key->params->pk_len,
368
0
                     key->tr, sizeof(key->tr));
369
0
    vector_free(&t0);
370
0
    EVP_MD_CTX_free(md_ctx);
371
0
    return ret;
372
0
}
373
374
int ossl_ml_dsa_key_pairwise_check(const ML_DSA_KEY *key)
375
0
{
376
0
    int ret = 0;
377
0
    VECTOR t1, t0;
378
0
    POLY *polys = NULL;
379
0
    uint32_t k = key->params->k;
380
0
    EVP_MD_CTX *md_ctx = NULL;
381
382
0
    if (key->pub_encoding == NULL || key->priv_encoding == 0)
383
0
        return 0;
384
385
0
    polys = OPENSSL_malloc(sizeof(*polys) * (2 * k));
386
0
    if (polys == NULL)
387
0
        return 0;
388
0
    md_ctx = EVP_MD_CTX_new();
389
0
    if (md_ctx == NULL)
390
0
        goto err;
391
392
0
    vector_init(&t1, polys, k);
393
0
    vector_init(&t0, polys + k, k);
394
0
    if (!public_from_private(key, md_ctx, &t1, &t0))
395
0
        goto err;
396
397
0
    ret = vector_equal(&t1, &key->t1) && vector_equal(&t0, &key->t0);
398
0
err:
399
0
    EVP_MD_CTX_free(md_ctx);
400
0
    OPENSSL_free(polys);
401
0
    return ret;
402
0
}
403
404
/*
405
 * @brief Generate a public-private key pair from a seed.
406
 * See FIPS 204, Algorithm 6 ML-DSA.KeyGen_internal().
407
 *
408
 * @param out The generated key (which contains params on input)
409
 *
410
 * @returns 1 on success or 0 on failure.
411
 */
412
static int keygen_internal(ML_DSA_KEY *out)
413
968
{
414
968
    int ret = 0;
415
968
    uint8_t augmented_seed[ML_DSA_SEED_BYTES + 2];
416
968
    uint8_t expanded_seed[ML_DSA_RHO_BYTES + ML_DSA_PRIV_SEED_BYTES + ML_DSA_K_BYTES];
417
968
    const uint8_t *const rho = expanded_seed; /* p = Public Random Seed */
418
968
    const uint8_t *const priv_seed = expanded_seed + ML_DSA_RHO_BYTES;
419
968
    const uint8_t *const K = priv_seed + ML_DSA_PRIV_SEED_BYTES;
420
968
    const ML_DSA_PARAMS *params = out->params;
421
968
    EVP_MD_CTX *md_ctx = NULL;
422
423
968
    if (out->seed == NULL
424
968
        || (md_ctx = EVP_MD_CTX_new()) == NULL
425
968
        || !ossl_ml_dsa_key_pub_alloc(out)
426
968
        || !ossl_ml_dsa_key_priv_alloc(out))
427
0
        goto err;
428
429
    /* augmented_seed = seed || k || l */
430
968
    memcpy(augmented_seed, out->seed, ML_DSA_SEED_BYTES);
431
968
    augmented_seed[ML_DSA_SEED_BYTES] = (uint8_t)params->k;
432
968
    augmented_seed[ML_DSA_SEED_BYTES + 1] = (uint8_t)params->l;
433
    /* Expand the seed into p[32], p'[64], K[32] */
434
968
    if (!shake_xof(md_ctx, out->shake256_md, augmented_seed, sizeof(augmented_seed),
435
968
                   expanded_seed, sizeof(expanded_seed)))
436
0
        goto err;
437
438
968
    memcpy(out->rho, rho, sizeof(out->rho));
439
968
    memcpy(out->K, K, sizeof(out->K));
440
441
968
    ret = vector_expand_S(md_ctx, out->shake256_md, params->eta, priv_seed, &out->s1, &out->s2)
442
968
        && public_from_private(out, md_ctx, &out->t1, &out->t0)
443
968
        && ossl_ml_dsa_pk_encode(out)
444
968
        && shake_xof(md_ctx, out->shake256_md, out->pub_encoding, out->params->pk_len,
445
968
                     out->tr, sizeof(out->tr))
446
968
        && ossl_ml_dsa_sk_encode(out);
447
448
968
err:
449
968
    if (out->seed != NULL && (out->prov_flags & ML_DSA_KEY_RETAIN_SEED) == 0) {
450
0
        OPENSSL_clear_free(out->seed, ML_DSA_SEED_BYTES);
451
0
        out->seed = NULL;
452
0
    }
453
968
    EVP_MD_CTX_free(md_ctx);
454
968
    OPENSSL_cleanse(augmented_seed, sizeof(augmented_seed));
455
968
    OPENSSL_cleanse(expanded_seed, sizeof(expanded_seed));
456
968
    return ret;
457
968
}
458
459
int ossl_ml_dsa_generate_key(ML_DSA_KEY *out)
460
968
{
461
968
    size_t seed_len = ML_DSA_SEED_BYTES;
462
968
    uint8_t *sk;
463
968
    int ret;
464
465
968
    if (out->seed == NULL) {
466
860
        if ((out->seed = OPENSSL_malloc(seed_len)) == NULL)
467
0
            return 0;
468
860
        if (RAND_priv_bytes_ex(out->libctx, out->seed, seed_len, 0) <= 0) {
469
0
            OPENSSL_free(out->seed);
470
0
            out->seed = NULL;
471
0
            return 0;
472
0
        }
473
860
    }
474
    /* We're generating from a seed, drop private prekey encoding */
475
968
    sk = out->priv_encoding;
476
968
    out->priv_encoding = NULL;
477
968
    if (sk == NULL) {
478
968
        ret = keygen_internal(out);
479
968
    } else {
480
0
        if ((ret = keygen_internal(out)) != 0
481
0
            && memcmp(out->priv_encoding, sk, out->params->sk_len) != 0) {
482
0
            ret = 0;
483
0
            ossl_ml_dsa_key_reset(out);
484
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
485
0
                           "explicit %s private key does not match seed",
486
0
                           out->params->alg);
487
0
        }
488
0
        OPENSSL_free(sk);
489
0
    }
490
968
    return ret;
491
968
}
492
493
/**
494
 * @brief This is used when a ML DSA key is used for an operation.
495
 * This checks that the algorithm is the same (i.e. uses the same parameters)
496
 *
497
 * @param key A ML_DSA key to use for an operation.
498
 * @param evp_type The algorithm nid associated with an operation
499
 *
500
 * @returns 1 if the algorithm matches, or 0 otherwise.
501
 */
502
503
int ossl_ml_dsa_key_matches(const ML_DSA_KEY *key, int evp_type)
504
766
{
505
766
    return (key->params->evp_type == evp_type);
506
766
}
507
508
/* Returns the public key data or NULL if there is no public key */
509
const uint8_t *ossl_ml_dsa_key_get_pub(const ML_DSA_KEY *key)
510
1.71k
{
511
1.71k
    return key->pub_encoding;
512
1.71k
}
513
514
/* Returns the encoded public key size */
515
size_t ossl_ml_dsa_key_get_pub_len(const ML_DSA_KEY *key)
516
1.06k
{
517
1.06k
    return key->params->pk_len;
518
1.06k
}
519
520
size_t ossl_ml_dsa_key_get_collision_strength_bits(const ML_DSA_KEY *key)
521
1.06k
{
522
1.06k
    return key->params->bit_strength;
523
1.06k
}
524
525
/* Returns the private key data or NULL if there is no private key */
526
const uint8_t *ossl_ml_dsa_key_get_priv(const ML_DSA_KEY *key)
527
1.87k
{
528
1.87k
    return key->priv_encoding;
529
1.87k
}
530
531
size_t ossl_ml_dsa_key_get_priv_len(const ML_DSA_KEY *key)
532
7
{
533
7
    return key->params->sk_len;
534
7
}
535
536
size_t ossl_ml_dsa_key_get_sig_len(const ML_DSA_KEY *key)
537
1.06k
{
538
1.06k
    return key->params->sig_len;
539
1.06k
}
540
541
OSSL_LIB_CTX *ossl_ml_dsa_key_get0_libctx(const ML_DSA_KEY *key)
542
0
{
543
0
    return key != NULL ? key->libctx : NULL;
544
0
}
545
546
const char *ossl_ml_dsa_key_get_name(const ML_DSA_KEY *key)
547
766
{
548
766
    return key->params->alg;
549
766
}