Coverage Report

Created: 2025-12-10 06:24

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