Coverage Report

Created: 2026-02-22 06:11

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