Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/crypto/slh_dsa/slh_hash.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 "internal/deprecated.h" /* PKCS1_MGF1() */
11
12
#include <string.h>
13
#include <openssl/evp.h>
14
#include <openssl/core_names.h>
15
#include <openssl/rsa.h> /* PKCS1_MGF1() */
16
#include "slh_dsa_local.h"
17
#include "slh_dsa_key.h"
18
19
26.4M
#define MAX_DIGEST_SIZE 64 /* SHA-512 is used for security category 3 & 5 */
20
21
static OSSL_SLH_HASHFUNC_H_MSG slh_hmsg_sha2;
22
static OSSL_SLH_HASHFUNC_PRF slh_prf_sha2;
23
static OSSL_SLH_HASHFUNC_PRF_MSG slh_prf_msg_sha2;
24
static OSSL_SLH_HASHFUNC_F slh_f_sha2;
25
static OSSL_SLH_HASHFUNC_H slh_h_sha2;
26
static OSSL_SLH_HASHFUNC_T slh_t_sha2;
27
28
static OSSL_SLH_HASHFUNC_H_MSG slh_hmsg_shake;
29
static OSSL_SLH_HASHFUNC_PRF slh_prf_shake;
30
static OSSL_SLH_HASHFUNC_PRF_MSG slh_prf_msg_shake;
31
static OSSL_SLH_HASHFUNC_F slh_f_shake;
32
static OSSL_SLH_HASHFUNC_H slh_h_shake;
33
static OSSL_SLH_HASHFUNC_T slh_t_shake;
34
35
static ossl_inline int xof_digest_3(EVP_MD_CTX *ctx,
36
    const uint8_t *in1, size_t in1_len,
37
    const uint8_t *in2, size_t in2_len,
38
    const uint8_t *in3, size_t in3_len,
39
    uint8_t *out, size_t out_len)
40
322M
{
41
322M
    return (EVP_DigestInit_ex2(ctx, NULL, NULL) == 1
42
322M
        && EVP_DigestUpdate(ctx, in1, in1_len) == 1
43
322M
        && EVP_DigestUpdate(ctx, in2, in2_len) == 1
44
322M
        && EVP_DigestUpdate(ctx, in3, in3_len) == 1
45
322M
        && EVP_DigestFinalXOF(ctx, out, out_len) == 1);
46
322M
}
47
48
static ossl_inline int xof_digest_4(EVP_MD_CTX *ctx,
49
    const uint8_t *in1, size_t in1_len,
50
    const uint8_t *in2, size_t in2_len,
51
    const uint8_t *in3, size_t in3_len,
52
    const uint8_t *in4, size_t in4_len,
53
    uint8_t *out, size_t out_len)
54
22.4M
{
55
22.4M
    return (EVP_DigestInit_ex2(ctx, NULL, NULL) == 1
56
22.4M
        && EVP_DigestUpdate(ctx, in1, in1_len) == 1
57
22.4M
        && EVP_DigestUpdate(ctx, in2, in2_len) == 1
58
22.4M
        && EVP_DigestUpdate(ctx, in3, in3_len) == 1
59
22.4M
        && EVP_DigestUpdate(ctx, in4, in4_len) == 1
60
22.4M
        && EVP_DigestFinalXOF(ctx, out, out_len) == 1);
61
22.4M
}
62
63
/* See FIPS 205 Section 11.1 */
64
static int
65
slh_hmsg_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *r,
66
    const uint8_t *pk_seed, const uint8_t *pk_root,
67
    const uint8_t *msg, size_t msg_len,
68
    uint8_t *out, size_t out_len)
69
690
{
70
690
    const SLH_DSA_PARAMS *params = ctx->key->params;
71
690
    size_t m = params->m;
72
690
    size_t n = params->n;
73
74
690
    return xof_digest_4(ctx->md_ctx, r, n, pk_seed, n, pk_root, n,
75
690
        msg, msg_len, out, m);
76
690
}
77
78
static int
79
slh_prf_shake(SLH_DSA_HASH_CTX *ctx,
80
    const uint8_t *pk_seed, const uint8_t *sk_seed,
81
    const uint8_t *adrs, uint8_t *out, size_t out_len)
82
59.8M
{
83
59.8M
    const SLH_DSA_PARAMS *params = ctx->key->params;
84
59.8M
    size_t n = params->n;
85
86
59.8M
    return xof_digest_3(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE,
87
59.8M
        sk_seed, n, out, n);
88
59.8M
}
89
90
static int
91
slh_prf_msg_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *sk_prf,
92
    const uint8_t *opt_rand, const uint8_t *msg, size_t msg_len,
93
    WPACKET *pkt)
94
210
{
95
210
    int ret;
96
210
    unsigned char out[SLH_MAX_N];
97
210
    const SLH_DSA_PARAMS *params = ctx->key->params;
98
210
    size_t n = params->n;
99
100
210
    ret = xof_digest_3(ctx->md_ctx, sk_prf, n, opt_rand, n, msg, msg_len, out, n)
101
210
        && WPACKET_memcpy(pkt, out, n);
102
210
    OPENSSL_cleanse(out, sizeof(out));
103
210
    return ret;
104
210
}
105
106
static int
107
slh_f_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *pk_seed, const uint8_t *adrs,
108
    const uint8_t *m1, size_t m1_len, uint8_t *out, size_t out_len)
109
305M
{
110
305M
    const SLH_DSA_PARAMS *params = ctx->key->params;
111
305M
    size_t n = params->n;
112
113
305M
    return xof_digest_3(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE, m1, m1_len, out, n);
114
305M
}
115
116
static int
117
slh_h_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *pk_seed, const uint8_t *adrs,
118
    const uint8_t *m1, const uint8_t *m2, uint8_t *out, size_t out_len)
119
43.2M
{
120
43.2M
    const SLH_DSA_PARAMS *params = ctx->key->params;
121
43.2M
    size_t n = params->n;
122
123
43.2M
    return xof_digest_4(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE, m1, n, m2, n, out, n);
124
43.2M
}
125
126
static int
127
slh_t_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *pk_seed, const uint8_t *adrs,
128
    const uint8_t *ml, size_t ml_len, uint8_t *out, size_t out_len)
129
346k
{
130
346k
    const SLH_DSA_PARAMS *params = ctx->key->params;
131
346k
    size_t n = params->n;
132
133
346k
    return xof_digest_3(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE, ml, ml_len, out, n);
134
346k
}
135
136
static ossl_inline int
137
digest_4(EVP_MD_CTX *ctx,
138
    const uint8_t *in1, size_t in1_len, const uint8_t *in2, size_t in2_len,
139
    const uint8_t *in3, size_t in3_len, const uint8_t *in4, size_t in4_len,
140
    uint8_t *out)
141
443M
{
142
443M
    return (EVP_DigestInit_ex2(ctx, NULL, NULL) == 1
143
443M
        && EVP_DigestUpdate(ctx, in1, in1_len) == 1
144
443M
        && EVP_DigestUpdate(ctx, in2, in2_len) == 1
145
443M
        && EVP_DigestUpdate(ctx, in3, in3_len) == 1
146
443M
        && EVP_DigestUpdate(ctx, in4, in4_len) == 1
147
443M
        && EVP_DigestFinal_ex(ctx, out, NULL) == 1);
148
443M
}
149
150
/* FIPS 205 Section 11.2.1 and 11.2.2 */
151
152
static int
153
slh_hmsg_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *r, const uint8_t *pk_seed,
154
    const uint8_t *pk_root, const uint8_t *msg, size_t msg_len,
155
    uint8_t *out, size_t out_len)
156
900
{
157
900
    int ret;
158
900
    const SLH_DSA_PARAMS *params = hctx->key->params;
159
900
    size_t m = params->m;
160
900
    size_t n = params->n;
161
900
    uint8_t seed[2 * SLH_MAX_N + MAX_DIGEST_SIZE];
162
900
    int sz = EVP_MD_get_size(hctx->key->md_big);
163
900
    size_t seed_len = (size_t)sz + 2 * n;
164
165
900
    if (sz <= 0)
166
0
        return 0;
167
168
900
    memcpy(seed, r, n);
169
900
    memcpy(seed + n, pk_seed, n);
170
900
    ret = digest_4(hctx->md_big_ctx, r, n, pk_seed, n, pk_root, n, msg, msg_len,
171
900
              seed + 2 * n)
172
900
        && (PKCS1_MGF1(out, m, seed, seed_len, hctx->key->md_big) == 0);
173
900
    OPENSSL_cleanse(seed, sizeof(seed));
174
900
    return ret;
175
900
}
176
177
static int
178
slh_prf_msg_sha2(SLH_DSA_HASH_CTX *hctx,
179
    const uint8_t *sk_prf, const uint8_t *opt_rand,
180
    const uint8_t *msg, size_t msg_len, WPACKET *pkt)
181
450
{
182
450
    int ret;
183
450
    const SLH_DSA_KEY *key = hctx->key;
184
450
    EVP_MAC_CTX *mctx = hctx->hmac_ctx;
185
450
    const SLH_DSA_PARAMS *prms = key->params;
186
450
    size_t n = prms->n;
187
450
    uint8_t mac[MAX_DIGEST_SIZE] = { 0 };
188
450
    OSSL_PARAM *p = NULL;
189
450
    OSSL_PARAM params[3];
190
191
    /*
192
     * Due to the way HMAC works, it is not possible to do this code early
193
     * in hmac_ctx_new() since it requires a key in order to set the digest.
194
     * So we do a lazy update here on the first call.
195
     */
196
450
    if (hctx->hmac_digest_used == 0) {
197
450
        p = params;
198
        /* The underlying digest to be used */
199
450
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
200
450
            (char *)EVP_MD_get0_name(key->md_big), 0);
201
450
        if (key->propq != NULL)
202
0
            *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
203
0
                (char *)key->propq, 0);
204
450
        *p = OSSL_PARAM_construct_end();
205
450
        p = params;
206
450
        hctx->hmac_digest_used = 1;
207
450
    }
208
209
450
    ret = EVP_MAC_init(mctx, sk_prf, n, p) == 1
210
450
        && EVP_MAC_update(mctx, opt_rand, n) == 1
211
450
        && EVP_MAC_update(mctx, msg, msg_len) == 1
212
450
        && EVP_MAC_final(mctx, mac, NULL, sizeof(mac)) == 1
213
450
        && WPACKET_memcpy(pkt, mac, n); /* Truncate output to n bytes */
214
450
    OPENSSL_cleanse(mac, sizeof(mac));
215
450
    return ret;
216
450
}
217
218
/*
219
 * The |digest| scratch storage in the hash context is used in place of a
220
 * local stack buffer, and is erased when the hash context is freed
221
 * (FIPS 205 section 3.1).  On the PRF path it holds a derived chain secret.
222
 */
223
static ossl_inline int
224
do_hash(SLH_DSA_HASH_CTX *hctx, EVP_MD_CTX *ctx, size_t n,
225
    const uint8_t *pk_seed, const uint8_t *adrs,
226
    const uint8_t *m, size_t m_len, size_t b, uint8_t *out, size_t out_len)
227
443M
{
228
443M
    int ret;
229
443M
    uint8_t zeros[128] = { 0 };
230
443M
    uint8_t *digest = hctx->scratch;
231
232
443M
    ret = digest_4(ctx, pk_seed, n, zeros, b - n, adrs, SLH_ADRSC_SIZE,
233
443M
        m, m_len, digest);
234
    /* Truncated returned value is n = 16 bytes */
235
443M
    memcpy(out, digest, n);
236
443M
    return ret;
237
443M
}
238
239
static int
240
slh_prf_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed,
241
    const uint8_t *sk_seed, const uint8_t *adrs,
242
    uint8_t *out, size_t out_len)
243
48.5M
{
244
48.5M
    size_t n = hctx->key->params->n;
245
246
48.5M
    return do_hash(hctx, hctx->md_ctx, n, pk_seed, adrs, sk_seed, n,
247
48.5M
        OSSL_SLH_DSA_SHA2_NUM_ZEROS_H_AND_T_BOUND1, out, out_len);
248
48.5M
}
249
250
static int
251
slh_f_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs,
252
    const uint8_t *m1, size_t m1_len, uint8_t *out, size_t out_len)
253
367M
{
254
367M
    return do_hash(hctx, hctx->md_ctx, hctx->key->params->n, pk_seed, adrs,
255
367M
        m1, m1_len, OSSL_SLH_DSA_SHA2_NUM_ZEROS_H_AND_T_BOUND1, out, out_len);
256
367M
}
257
258
static int
259
slh_h_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs,
260
    const uint8_t *m1, const uint8_t *m2, uint8_t *out, size_t out_len)
261
26.4M
{
262
    /* The concatenated children go in the scratch after the digest */
263
26.4M
    uint8_t *m = hctx->scratch + MAX_DIGEST_SIZE;
264
26.4M
    const SLH_DSA_PARAMS *prms = hctx->key->params;
265
26.4M
    size_t n = prms->n;
266
267
26.4M
    memcpy(m, m1, n);
268
26.4M
    memcpy(m + n, m2, n);
269
26.4M
    return do_hash(hctx, hctx->md_big_ctx, n, pk_seed, adrs, m, 2 * n,
270
26.4M
        prms->sha2_h_and_t_bound, out, out_len);
271
26.4M
}
272
273
static int
274
slh_t_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs,
275
    const uint8_t *ml, size_t ml_len, uint8_t *out, size_t out_len)
276
492k
{
277
492k
    const SLH_DSA_PARAMS *prms = hctx->key->params;
278
279
492k
    return do_hash(hctx, hctx->md_big_ctx, prms->n, pk_seed, adrs, ml, ml_len,
280
492k
        prms->sha2_h_and_t_bound, out, out_len);
281
492k
}
282
283
const SLH_HASH_FUNC *ossl_slh_get_hash_fn(int is_shake)
284
145k
{
285
145k
    static const SLH_HASH_FUNC methods[] = {
286
145k
        { slh_hmsg_shake,
287
145k
            slh_prf_shake,
288
145k
            slh_prf_msg_shake,
289
145k
            slh_f_shake,
290
145k
            slh_h_shake,
291
145k
            slh_t_shake },
292
145k
        { slh_hmsg_sha2,
293
145k
            slh_prf_sha2,
294
145k
            slh_prf_msg_sha2,
295
145k
            slh_f_sha2,
296
145k
            slh_h_sha2,
297
145k
            slh_t_sha2 }
298
145k
    };
299
145k
    return &methods[is_shake ? 0 : 1];
300
145k
}