Coverage Report

Created: 2026-08-17 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/slh_dsa/slh_xmss.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/crypto.h>
12
#include "slh_dsa_local.h"
13
#include "slh_dsa_key.h"
14
15
/**
16
 * @brief Compute the root Public key of a XMSS tree.
17
 * See FIPS 205 Section 6.1 Algorithm 9.
18
 * This is a recursive function that starts at an leaf index, that calculates
19
 * the hash of each parent using 2 child nodes.
20
 *
21
 * @param ctx Contains SLH_DSA algorithm functions and constants.
22
 * @param sk_seed A SLH-DSA private key seed of size |n|
23
 * @param nodeid The index of the target node being computed
24
 *               (which must be < 2^(hm - height)
25
 * @param h The height within the tree of the node being computed.
26
 *          (which must be <= hm) (hm is one of 3, 4, 8 or 9)
27
 *          At height=0 There are 2^hm leaf nodes,
28
 *          and the root node is at height = hm)
29
 * @param pk_seed A SLH-DSA public key seed of size |n|
30
 * @param adrs An ADRS object containing the layer address and tree address set
31
 *             to the XMSS tree within which the XMSS tree is being computed.
32
 * @param pk_out The generated public key of size |n|
33
 * @param pk_out_len The maximum size of |pk_out|
34
 * @returns 1 on success, or 0 on error.
35
 */
36
int ossl_slh_xmss_node(SLH_DSA_HASH_CTX *ctx, const uint8_t *sk_seed,
37
    uint32_t node_id, uint32_t h,
38
    const uint8_t *pk_seed, uint8_t *adrs,
39
    uint8_t *pk_out, size_t pk_out_len)
40
0
{
41
0
    const SLH_DSA_KEY *key = ctx->key;
42
0
    SLH_ADRS_FUNC_DECLARE(key, adrsf);
43
0
    int ret = 0;
44
45
0
    if (h == 0) {
46
        /* For leaf nodes generate the public key */
47
0
        adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_WOTS_HASH);
48
0
        adrsf->set_keypair_address(adrs, node_id);
49
0
        if (ossl_slh_wots_pk_gen(ctx, sk_seed, pk_seed, adrs,
50
0
                pk_out, pk_out_len))
51
0
            ret = 1;
52
0
    } else {
53
0
        uint8_t lnode[SLH_MAX_N], rnode[SLH_MAX_N];
54
55
0
        if (ossl_slh_xmss_node(ctx, sk_seed, 2 * node_id, h - 1, pk_seed, adrs,
56
0
                lnode, sizeof(lnode))
57
0
            && ossl_slh_xmss_node(ctx, sk_seed, 2 * node_id + 1, h - 1,
58
0
                pk_seed, adrs, rnode, sizeof(rnode))) {
59
0
            adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_TREE);
60
0
            adrsf->set_tree_height(adrs, h);
61
0
            adrsf->set_tree_index(adrs, node_id);
62
0
            ret = key->hash_func->H(ctx, pk_seed, adrs, lnode, rnode, pk_out, pk_out_len);
63
0
        }
64
0
        OPENSSL_cleanse(lnode, sizeof(lnode));
65
0
        OPENSSL_cleanse(rnode, sizeof(rnode));
66
0
    }
67
0
    return ret;
68
0
}
69
70
/**
71
 * @brief Generate an XMSS signature using a message and key.
72
 * See FIPS 205 Section 6.2 Algorithm 10
73
 *
74
 * The generated signature consists of:
75
 *  - A WOTS+ signature of size (2 * n + 3) * n
76
 *  - An array of authentication paths of size (XMSS tree_height) * n.
77
 *
78
 * @param ctx Contains SLH_DSA algorithm functions and constants.
79
 * @param msg A message of size |n| bytes to sign
80
 * @param sk_seed A private key seed of size |n|
81
 * @param node_id The index of a WOTS+ key within the XMSS tree to use for signing.
82
 * @param pk_seed A public key seed f size |n|
83
 * @param adrs An ADRS object containing the layer address and tree address set
84
 *              to the XMSS key being used to sign the message.
85
 * @param sig_wpkt A WPACKET object to write the generated XMSS signature to.
86
 * @returns 1 on success, or 0 on error.
87
 */
88
int ossl_slh_xmss_sign(SLH_DSA_HASH_CTX *ctx, const uint8_t *msg,
89
    const uint8_t *sk_seed, uint32_t node_id,
90
    const uint8_t *pk_seed, uint8_t *adrs, WPACKET *sig_wpkt)
91
0
{
92
0
    const SLH_DSA_KEY *key = ctx->key;
93
0
    SLH_ADRS_FUNC_DECLARE(key, adrsf);
94
0
    SLH_ADRS_DECLARE(tmp_adrs);
95
0
    size_t n = key->params->n;
96
0
    uint32_t h, hm = key->params->hm;
97
0
    uint32_t id = node_id;
98
0
    uint8_t *auth_path; /* Pointer to a buffer offset inside |sig_wpkt| */
99
0
    size_t auth_path_len = n;
100
101
    /*
102
     * This code reverses the order of the FIPS 205 code so that it does the
103
     * sign first. This simplifies the WPACKET writing.
104
     */
105
0
    adrsf->copy(tmp_adrs, adrs);
106
0
    adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_WOTS_HASH);
107
0
    adrsf->set_keypair_address(adrs, node_id);
108
0
    if (!ossl_slh_wots_sign(ctx, msg, sk_seed, pk_seed, adrs, sig_wpkt))
109
0
        return 0;
110
111
0
    adrsf->copy(adrs, tmp_adrs);
112
0
    for (h = 0; h < hm; ++h) {
113
0
        if (!WPACKET_allocate_bytes(sig_wpkt, auth_path_len, &auth_path)
114
0
            || !ossl_slh_xmss_node(ctx, sk_seed, id ^ 1, h, pk_seed, adrs,
115
0
                auth_path, auth_path_len))
116
0
            return 0;
117
0
        id >>= 1;
118
0
    }
119
0
    return 1;
120
0
}
121
122
/**
123
 * @brief Compute a candidate XMSS public key from a message and XMSS signature
124
 * See FIPS 205 Section 6.3 Algorithm 11
125
 *
126
 * * The signature consists of:
127
 *  - A WOTS+ signature of size (2 * n + 3) * n
128
 *  - An array of authentication paths of size (XMSS tree height) * n.
129
 *
130
 * @param ctx Contains SLH_DSA algorithm functions and constants.
131
 * @param node_id Must be set to the |node_id| used in xmss_sign().
132
 * @param sig_rpkt A Packet to read a XMSS signature from.
133
 * @param msg A message of size |n| bytes
134
 * @param sk_seed A private key seed of size |n|
135
 * @param pk_seed A public key seed of size |n|
136
 * @param adrs An ADRS object containing a layer address and tree address of an
137
 *             XMSS key used for signing the message.
138
 * @param pk_out The returned candidate XMSS public key of size |n|.
139
 * @param pk_out_len The maximum size of |pk_out|.
140
 * @returns 1 on success, or 0 on error.
141
 */
142
int ossl_slh_xmss_pk_from_sig(SLH_DSA_HASH_CTX *ctx, uint32_t node_id,
143
    PACKET *sig_rpkt, const uint8_t *msg,
144
    const uint8_t *pk_seed, uint8_t *adrs,
145
    uint8_t *pk_out, size_t pk_out_len)
146
0
{
147
0
    const SLH_DSA_KEY *key = ctx->key;
148
0
    SLH_HASH_FUNC_DECLARE(key, hashf);
149
0
    SLH_ADRS_FUNC_DECLARE(key, adrsf);
150
0
    SLH_HASH_FN_DECLARE(hashf, H);
151
0
    SLH_ADRS_FN_DECLARE(adrsf, set_tree_index);
152
0
    SLH_ADRS_FN_DECLARE(adrsf, set_tree_height);
153
0
    uint32_t k;
154
0
    size_t n = key->params->n;
155
0
    uint32_t hm = key->params->hm;
156
0
    uint8_t *node = pk_out;
157
0
    const uint8_t *auth_path; /* Pointer to buffer offset in |pkt_sig| */
158
159
0
    adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_WOTS_HASH);
160
0
    adrsf->set_keypair_address(adrs, node_id);
161
0
    if (!ossl_slh_wots_pk_from_sig(ctx, sig_rpkt, msg, pk_seed, adrs,
162
0
            node, pk_out_len))
163
0
        return 0;
164
165
0
    adrsf->set_type_and_clear(adrs, SLH_ADRS_TYPE_TREE);
166
167
0
    for (k = 0; k < hm; ++k) {
168
0
        if (!PACKET_get_bytes(sig_rpkt, &auth_path, n))
169
0
            return 0;
170
0
        set_tree_height(adrs, k + 1);
171
0
        if ((node_id & 1) == 0) { /* even */
172
0
            node_id >>= 1;
173
0
            set_tree_index(adrs, node_id);
174
0
            if (!H(ctx, pk_seed, adrs, node, auth_path, node, pk_out_len))
175
0
                return 0;
176
0
        } else { /* odd */
177
0
            node_id = (node_id - 1) >> 1;
178
0
            set_tree_index(adrs, node_id);
179
0
            if (!H(ctx, pk_seed, adrs, auth_path, node, node, pk_out_len))
180
0
                return 0;
181
0
        }
182
0
    }
183
0
    return 1;
184
0
}