Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/slh_dsa/slh_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 <string.h>
11
#include <openssl/err.h>
12
#include <openssl/core_dispatch.h>
13
#include <openssl/core_names.h>
14
#include <openssl/params.h>
15
#include <openssl/rand.h>
16
#include <openssl/proverr.h>
17
#include "slh_dsa_local.h"
18
#include "slh_dsa_key.h"
19
#include "internal/encoder.h"
20
21
static int slh_dsa_compute_pk_root(SLH_DSA_HASH_CTX *ctx, SLH_DSA_KEY *out, int verify);
22
23
static void slh_dsa_key_hash_cleanup(SLH_DSA_KEY *key)
24
145k
{
25
145k
    OPENSSL_free(key->propq);
26
145k
    if (key->md_big != key->md)
27
122k
        EVP_MD_free(key->md_big);
28
145k
    key->md_big = NULL;
29
145k
    EVP_MD_free(key->md);
30
145k
    EVP_MAC_free(key->hmac);
31
145k
    key->md = NULL;
32
145k
}
33
34
static int slh_dsa_key_hash_init(SLH_DSA_KEY *key)
35
145k
{
36
145k
    int is_shake = key->params->is_shake;
37
145k
    int security_category = key->params->security_category;
38
145k
    const char *digest_alg = is_shake ? "SHAKE-256" : "SHA2-256";
39
40
145k
    key->md = EVP_MD_fetch(key->libctx, digest_alg, key->propq);
41
145k
    if (key->md == NULL)
42
0
        return 0;
43
    /*
44
     * SHA2 algorithm(s) require SHA256 + HMAC_SHA(X) & MGF1(SHAX)
45
     * SHAKE algorithm(s) use SHAKE for all functions.
46
     */
47
145k
    if (is_shake == 0) {
48
73.2k
        if (security_category == 1) {
49
            /* For category 1 SHA2-256 is used for all hash operations */
50
23.6k
            key->md_big = key->md;
51
49.5k
        } else {
52
            /* Security categories 3 & 5 also need SHA-512 */
53
49.5k
            key->md_big = EVP_MD_fetch(key->libctx, "SHA2-512", key->propq);
54
49.5k
            if (key->md_big == NULL)
55
0
                goto err;
56
49.5k
        }
57
73.2k
        key->hmac = EVP_MAC_fetch(key->libctx, "HMAC", key->propq);
58
73.2k
        if (key->hmac == NULL)
59
0
            goto err;
60
73.2k
    }
61
145k
    key->adrs_func = ossl_slh_get_adrs_fn(is_shake == 0);
62
145k
    key->hash_func = ossl_slh_get_hash_fn(is_shake);
63
145k
    return 1;
64
0
err:
65
0
    return 0;
66
145k
}
67
68
static void slh_dsa_key_hash_dup(SLH_DSA_KEY *dst, const SLH_DSA_KEY *src)
69
20
{
70
20
    if (src->md_big != NULL && src->md_big != src->md)
71
5
        EVP_MD_up_ref(src->md_big);
72
20
    if (src->md != NULL)
73
20
        EVP_MD_up_ref(src->md);
74
20
    if (src->hmac != NULL)
75
7
        EVP_MAC_up_ref(src->hmac);
76
20
}
77
78
/**
79
 * @brief Return the libctx associated with a SLH_DSA_KEY object
80
 *
81
 * @param key A SLH_DSA_KEY to extract the libctx from.
82
 * @returns The new OSSL_LIB_CTX object on success, or NULL failure
83
 */
84
OSSL_LIB_CTX *ossl_slh_dsa_key_get0_libctx(const SLH_DSA_KEY *key)
85
0
{
86
0
    return key != NULL ? key->libctx : NULL;
87
0
}
88
89
/**
90
 * @brief Create a new SLH_DSA_KEY object
91
 *
92
 * @param libctx A OSSL_LIB_CTX object used for fetching algorithms.
93
 * @param propq The property query used for fetching algorithms
94
 * @param alg The algorithm name associated with the key type
95
 * @returns The new SLH_DSA_KEY object on success, or NULL on malloc failure
96
 */
97
SLH_DSA_KEY *ossl_slh_dsa_key_new(OSSL_LIB_CTX *libctx, const char *propq,
98
    const char *alg)
99
288k
{
100
288k
    SLH_DSA_KEY *ret;
101
288k
    const SLH_DSA_PARAMS *params = ossl_slh_dsa_params_get(alg);
102
103
288k
    if (params == NULL)
104
0
        return NULL;
105
106
288k
    ret = OPENSSL_zalloc(sizeof(*ret));
107
288k
    if (ret != NULL) {
108
288k
        ret->libctx = libctx;
109
288k
        ret->params = params;
110
288k
        if (propq != NULL) {
111
285k
            ret->propq = OPENSSL_strdup(propq);
112
285k
            if (ret->propq == NULL)
113
0
                goto err;
114
285k
        }
115
288k
        if (!slh_dsa_key_hash_init(ret))
116
0
            goto err;
117
288k
    }
118
288k
    return ret;
119
0
err:
120
0
    ossl_slh_dsa_key_free(ret);
121
0
    return NULL;
122
288k
}
123
124
/**
125
 * @brief Destroy a SLH_DSA_KEY object
126
 */
127
void ossl_slh_dsa_key_free(SLH_DSA_KEY *key)
128
2.24M
{
129
2.24M
    if (key == NULL)
130
1.95M
        return;
131
132
288k
    slh_dsa_key_hash_cleanup(key);
133
288k
    OPENSSL_cleanse(&key->priv, sizeof(key->priv) >> 1);
134
288k
    OPENSSL_free(key);
135
288k
}
136
137
/**
138
 * @brief Duplicate a key
139
 *
140
 * @param src A SLH_DSA_KEY object to copy
141
 * @param selection to select public and/or private components. Selecting the
142
 *                  private key will also select the public key
143
 * @returns The duplicated key, or NULL on failure.
144
 */
145
SLH_DSA_KEY *ossl_slh_dsa_key_dup(const SLH_DSA_KEY *src, int selection)
146
24
{
147
24
    SLH_DSA_KEY *ret = NULL;
148
149
24
    if (src == NULL)
150
0
        return NULL;
151
152
24
    ret = OPENSSL_zalloc(sizeof(*ret));
153
24
    if (ret != NULL) {
154
24
        *ret = *src; /* this copies everything including the keydata in priv[] */
155
24
        ret->propq = NULL;
156
24
        ret->pub = NULL;
157
24
        ret->has_priv = 0;
158
24
        slh_dsa_key_hash_dup(ret, src);
159
24
        if (src->propq != NULL) {
160
24
            ret->propq = OPENSSL_strdup(src->propq);
161
24
            if (ret->propq == NULL)
162
0
                goto err;
163
24
        }
164
24
        if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
165
            /* The public components are present if the private key is present */
166
24
            if (src->pub != NULL)
167
24
                ret->pub = SLH_DSA_PUB(ret);
168
24
            if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
169
24
                ret->has_priv = src->has_priv;
170
24
        }
171
24
    }
172
24
    return ret;
173
0
err:
174
0
    ossl_slh_dsa_key_free(ret);
175
0
    return NULL;
176
24
}
177
178
/**
179
 * @brief Are 2 keys equal?
180
 *
181
 * To be equal the keys must have the same key data and algorithm name.
182
 *
183
 * @param key1 A SLH_DSA_KEY object
184
 * @param key2 A SLH_DSA_KEY object
185
 * @param selection to select public and/or private component comparison.
186
 * @returns 1 if the keys are equal otherwise it returns 0.
187
 */
188
int ossl_slh_dsa_key_equal(const SLH_DSA_KEY *key1, const SLH_DSA_KEY *key2,
189
    int selection)
190
723
{
191
723
    int key_checked = 0;
192
193
    /* The parameter sets must match - i.e. The same algorithm name */
194
723
    if (key1->params != key2->params)
195
0
        return 0;
196
197
723
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
198
723
        if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
199
723
            if (key1->pub != NULL && key2->pub != NULL) {
200
723
                if (memcmp(key1->pub, key2->pub, key1->params->pk_len) != 0)
201
161
                    return 0;
202
562
                key_checked = 1;
203
562
            }
204
723
        }
205
562
        if (!key_checked
206
0
            && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
207
0
            if (key1->has_priv && key2->has_priv) {
208
0
                if (CRYPTO_memcmp(key1->priv, key2->priv,
209
0
                        key1->params->pk_len)
210
0
                    != 0)
211
0
                    return 0;
212
0
                key_checked = 1;
213
0
            }
214
0
        }
215
562
        return key_checked;
216
562
    }
217
0
    return 1;
218
723
}
219
220
int ossl_slh_dsa_key_has(const SLH_DSA_KEY *key, int selection)
221
1.75k
{
222
1.75k
    if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
223
1.75k
        if (key->pub == NULL)
224
0
            return 0; /* No public key */
225
1.75k
        if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
226
73
            && key->has_priv == 0)
227
0
            return 0; /* No private key */
228
1.75k
        return 1;
229
1.75k
    }
230
0
    return 0;
231
1.75k
}
232
233
int ossl_slh_dsa_key_pairwise_check(const SLH_DSA_KEY *key)
234
24
{
235
24
    int ret;
236
24
    SLH_DSA_HASH_CTX *ctx = NULL;
237
238
24
    if (key->pub == NULL || key->has_priv == 0)
239
0
        return 0;
240
241
24
    ctx = ossl_slh_dsa_hash_ctx_new(key);
242
24
    if (ctx == NULL)
243
0
        return 0;
244
24
    ret = slh_dsa_compute_pk_root(ctx, (SLH_DSA_KEY *)key, 1);
245
24
    ossl_slh_dsa_hash_ctx_free(ctx);
246
24
    return ret;
247
24
}
248
249
void ossl_slh_dsa_key_reset(SLH_DSA_KEY *key)
250
101
{
251
101
    key->pub = NULL;
252
101
    if (key->has_priv) {
253
56
        key->has_priv = 0;
254
56
        OPENSSL_cleanse(key->priv, sizeof(key->priv));
255
56
    }
256
101
}
257
258
/**
259
 * @brief Load a SLH_DSA key from raw data.
260
 *
261
 * @param key An SLH_DSA key to load into
262
 * @param params An array of parameters containing key data.
263
 * @param include_private Set to 1 to optionally include the private key data
264
 *                        if it exists.
265
 * @returns 1 on success, or 0 on failure.
266
 */
267
int ossl_slh_dsa_key_fromdata(SLH_DSA_KEY *key, const OSSL_PARAM *param_pub,
268
    const OSSL_PARAM *param_priv,
269
    int include_private)
270
727
{
271
727
    size_t priv_len, key_len, data_len = 0;
272
727
    void *p;
273
274
727
    if (key == NULL)
275
0
        return 0;
276
277
    /* The private key consists of 4 elements SK_SEED, SK_PRF, PK_SEED and PK_ROOT */
278
727
    priv_len = ossl_slh_dsa_key_get_priv_len(key);
279
    /* The size of either SK_SEED + SK_PRF OR PK_SEED + PK_ROOT */
280
727
    key_len = priv_len >> 1;
281
282
    /* Private key is optional */
283
727
    if (include_private) {
284
727
        if (param_priv != NULL) {
285
727
            p = key->priv;
286
727
            if (!OSSL_PARAM_get_octet_string(param_priv, &p, priv_len, &data_len))
287
59
                return 0;
288
            /* If the data read includes all 4 elements then we are finished */
289
668
            if (data_len == priv_len) {
290
538
                key->has_priv = 1;
291
538
                key->pub = SLH_DSA_PUB(key);
292
538
                return 1;
293
538
            }
294
            /* Otherwise it must be just SK_SEED + SK_PRF */
295
130
            if (data_len != key_len)
296
45
                goto err;
297
85
            key->has_priv = 1;
298
85
        }
299
727
    }
300
    /*
301
     * In the case where the passed in private key does not contain the public key
302
     * there MUST be a separate public key, since the private key cannot exist
303
     * without the public key elements. NOTE that this does not accept half of
304
     * the public key, (Keygen must be used for this case currently).
305
     */
306
85
    p = SLH_DSA_PUB(key);
307
85
    if (param_pub == NULL
308
85
        || !OSSL_PARAM_get_octet_string(param_pub, &p, key_len, &data_len)
309
48
        || data_len != key_len)
310
56
        goto err;
311
29
    key->pub = p;
312
29
    return 1;
313
101
err:
314
    /*
315
     * A private key of unexpected length may have been copied into |priv|
316
     * before |has_priv| was set, in which case the reset below would not
317
     * erase it, so cleanse unconditionally.
318
     */
319
101
    OPENSSL_cleanse(key->priv, sizeof(key->priv));
320
101
    ossl_slh_dsa_key_reset(key);
321
101
    return 0;
322
85
}
323
324
/**
325
 * Generate the public key root from private key (seed and prf) and public key seed.
326
 * See FIPS 205 Section 9.1 Algorithm 18
327
 *
328
 * @param ctx Contains SLH_DSA algorithm functions and constants.
329
 * @param out An SLH_DSA key containing the private key (seed and prf) and public key seed.
330
 *            The public root key is written to this key.
331
 * @param validate If set to 1 the computed public key is not written to the key,
332
 *                 but will be compared to the existing value.
333
 * @returns 1 if the root key is generated or compared successfully, or 0 on error.
334
 */
335
static int slh_dsa_compute_pk_root(SLH_DSA_HASH_CTX *ctx, SLH_DSA_KEY *out,
336
    int validate)
337
1.97k
{
338
1.97k
    const SLH_DSA_KEY *key = ctx->key;
339
1.97k
    SLH_ADRS_FUNC_DECLARE(key, adrsf);
340
1.97k
    SLH_ADRS_DECLARE(adrs);
341
1.97k
    const SLH_DSA_PARAMS *params = key->params;
342
1.97k
    size_t n = params->n;
343
1.97k
    uint8_t pk_root[SLH_DSA_MAX_N], *dst;
344
345
1.97k
    adrsf->zero(adrs);
346
1.97k
    adrsf->set_layer_address(adrs, params->d - 1);
347
348
1.97k
    dst = validate ? pk_root : SLH_DSA_PK_ROOT(out);
349
350
    /* Generate the ROOT public key */
351
1.97k
    return ossl_slh_xmss_node(ctx, SLH_DSA_SK_SEED(key), 0, params->hm,
352
1.97k
               SLH_DSA_PK_SEED(key), adrs, dst, n)
353
1.97k
        && (validate == 0 || memcmp(dst, SLH_DSA_PK_ROOT(out), n) == 0);
354
1.97k
}
355
356
/**
357
 * @brief Generate a SLH_DSA keypair. The private key seed and prf as well as the
358
 * public key seed are generated using an approved DRBG's. The public key root is
359
 * calculated using these generated values.
360
 * See FIPS 205 Section 10.1 Algorithm 21
361
 *
362
 * @param ctx Contains SLH_DSA algorithm functions and constants
363
 * @param out An SLH_DSA key to write key pair data to.
364
 * @param lib_ctx A library context for fetching RAND algorithms
365
 * @param entropy Optional entropy to use instead of using a DRBG.
366
 *        Required for ACVP testing. It may be NULL.
367
 * @param entropy_len the size of |entropy|. If set it must be at least 3 * |n|.
368
 * @returns 1 if the key is generated or 0 otherwise.
369
 */
370
int ossl_slh_dsa_generate_key(SLH_DSA_HASH_CTX *ctx, SLH_DSA_KEY *out,
371
    OSSL_LIB_CTX *lib_ctx,
372
    const uint8_t *entropy, size_t entropy_len)
373
1.01k
{
374
1.01k
    size_t n = out->params->n;
375
1.01k
    size_t secret_key_len = 2 * n; /* The length of SK_SEED + SK_PRF */
376
1.01k
    size_t pk_seed_len = n; /* The length of PK_SEED */
377
1.01k
    size_t entropy_len_expected = secret_key_len + pk_seed_len;
378
1.01k
    uint8_t *priv = SLH_DSA_PRIV(out);
379
1.01k
    uint8_t *pub = SLH_DSA_PUB(out);
380
381
1.01k
    if (entropy != NULL && entropy_len != 0) {
382
0
        if (entropy_len != entropy_len_expected)
383
0
            goto err;
384
0
        memcpy(priv, entropy, entropy_len_expected);
385
1.01k
    } else {
386
1.01k
        if (RAND_priv_bytes_ex(lib_ctx, priv, secret_key_len, 0) <= 0
387
1.01k
            || RAND_bytes_ex(lib_ctx, pub, pk_seed_len, 0) <= 0)
388
0
            goto err;
389
1.01k
    }
390
1.01k
    if (!slh_dsa_compute_pk_root(ctx, out, 0))
391
0
        goto err;
392
1.01k
    out->pub = pub;
393
1.01k
    out->has_priv = 1;
394
1.01k
    return 1;
395
0
err:
396
0
    out->pub = NULL;
397
0
    out->has_priv = 0;
398
0
    OPENSSL_cleanse(priv, secret_key_len);
399
0
    return 0;
400
1.01k
}
401
402
/**
403
 * @brief This is used when a SLH key is used for an operation.
404
 * This checks that the algorithm is the same (i.e. uses the same parameters)
405
 *
406
 * @param ctx Contains SLH_DSA algorithm functions and constants to be used for
407
 *            an operation.
408
 * @param key A SLH_DSA key to use for an operation.
409
 *
410
 * @returns 1 if the algorithm matches, or 0 otherwise.
411
 */
412
int ossl_slh_dsa_key_type_matches(const SLH_DSA_KEY *key, const char *alg)
413
2.26k
{
414
2.26k
    return (OPENSSL_strcasecmp(key->params->alg, alg) == 0);
415
2.26k
}
416
417
/* Returns the public key data or NULL if there is no public key */
418
const uint8_t *ossl_slh_dsa_key_get_pub(const SLH_DSA_KEY *key)
419
4.01k
{
420
4.01k
    return key->pub;
421
4.01k
}
422
423
/* Returns the constant 2 * |n| which is the size of PK_SEED + PK_ROOT */
424
size_t ossl_slh_dsa_key_get_pub_len(const SLH_DSA_KEY *key)
425
288k
{
426
288k
    return 2 * key->params->n;
427
288k
}
428
429
/* Returns the private key data or NULL if there is no private key */
430
const uint8_t *ossl_slh_dsa_key_get_priv(const SLH_DSA_KEY *key)
431
3.96k
{
432
3.96k
    return key->has_priv ? key->priv : NULL;
433
3.96k
}
434
435
/*
436
 * Returns the constant 4 * |n| which is the size of both
437
 * the private and public key components.
438
 * SK_SEED + SK_ROOT + PK_SEED + PK_ROOT
439
 */
440
size_t ossl_slh_dsa_key_get_priv_len(const SLH_DSA_KEY *key)
441
1.43k
{
442
1.43k
    return 4 * key->params->n;
443
1.43k
}
444
445
size_t ossl_slh_dsa_key_get_n(const SLH_DSA_KEY *key)
446
3.62k
{
447
3.62k
    return key->params->n;
448
3.62k
}
449
450
int ossl_slh_dsa_key_get_security_category(const SLH_DSA_KEY *key)
451
2.06k
{
452
2.06k
    return key->params->security_category;
453
2.06k
}
454
455
size_t ossl_slh_dsa_key_get_sig_len(const SLH_DSA_KEY *key)
456
2.73k
{
457
2.73k
    return key->params->sig_len;
458
2.73k
}
459
const char *ossl_slh_dsa_key_get_name(const SLH_DSA_KEY *key)
460
4.23k
{
461
4.23k
    return key->params->alg;
462
4.23k
}
463
int ossl_slh_dsa_key_get_type(const SLH_DSA_KEY *key)
464
2.26k
{
465
2.26k
    return key->params->type;
466
2.26k
}
467
468
int ossl_slh_dsa_set_priv(SLH_DSA_KEY *key, const uint8_t *priv, size_t priv_len)
469
93
{
470
93
    if (ossl_slh_dsa_key_get_priv_len(key) != priv_len)
471
44
        return 0;
472
49
    memcpy(key->priv, priv, priv_len);
473
49
    key->has_priv = 1;
474
49
    key->pub = SLH_DSA_PUB(key);
475
49
    return 1;
476
93
}
477
478
int ossl_slh_dsa_set_pub(SLH_DSA_KEY *key, const uint8_t *pub, size_t pub_len)
479
158
{
480
158
    if (ossl_slh_dsa_key_get_pub_len(key) != pub_len)
481
16
        return 0;
482
142
    key->pub = SLH_DSA_PUB(key);
483
142
    memcpy(key->pub, pub, pub_len);
484
142
    key->has_priv = 0;
485
142
    return 1;
486
158
}
487
488
#ifndef FIPS_MODULE
489
int ossl_slh_dsa_key_to_text(BIO *out, const SLH_DSA_KEY *key, int selection)
490
97
{
491
97
    const char *name;
492
493
97
    if (out == NULL || key == NULL) {
494
0
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
495
0
        return 0;
496
0
    }
497
97
    name = ossl_slh_dsa_key_get_name(key);
498
97
    if (ossl_slh_dsa_key_get_pub(key) == NULL) {
499
        /* Regardless of the |selection|, there must be a public key */
500
0
        ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY,
501
0
            "no %s key material available", name);
502
0
        return 0;
503
0
    }
504
505
97
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
506
49
        if (ossl_slh_dsa_key_get_priv(key) == NULL) {
507
0
            ERR_raise_data(ERR_LIB_PROV, PROV_R_MISSING_KEY,
508
0
                "no %s key material available", name);
509
0
            return 0;
510
0
        }
511
49
        if (BIO_printf(out, "%s Private-Key:\n", name) <= 0)
512
0
            return 0;
513
49
        if (!ossl_bio_print_labeled_buf(out, "priv:", ossl_slh_dsa_key_get_priv(key),
514
49
                ossl_slh_dsa_key_get_priv_len(key)))
515
0
            return 0;
516
49
    } else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
517
24
        if (BIO_printf(out, "%s Public-Key:\n", name) <= 0)
518
0
            return 0;
519
24
    }
520
521
97
    if (!ossl_bio_print_labeled_buf(out, "pub:", ossl_slh_dsa_key_get_pub(key),
522
97
            ossl_slh_dsa_key_get_pub_len(key)))
523
0
        return 0;
524
525
97
    return 1;
526
97
}
527
#endif /* FIPS_MODULE */