/src/openssl/crypto/slh_dsa/slh_hash.c
Line | Count | Source (jump to first uncovered line) |
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 "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 | | #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 | 0 | { |
41 | 0 | return (EVP_DigestInit_ex2(ctx, NULL, NULL) == 1 |
42 | 0 | && EVP_DigestUpdate(ctx, in1, in1_len) == 1 |
43 | 0 | && EVP_DigestUpdate(ctx, in2, in2_len) == 1 |
44 | 0 | && EVP_DigestUpdate(ctx, in3, in3_len) == 1 |
45 | 0 | && EVP_DigestFinalXOF(ctx, out, out_len) == 1); |
46 | 0 | } |
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 | 0 | { |
55 | 0 | return (EVP_DigestInit_ex2(ctx, NULL, NULL) == 1 |
56 | 0 | && EVP_DigestUpdate(ctx, in1, in1_len) == 1 |
57 | 0 | && EVP_DigestUpdate(ctx, in2, in2_len) == 1 |
58 | 0 | && EVP_DigestUpdate(ctx, in3, in3_len) == 1 |
59 | 0 | && EVP_DigestUpdate(ctx, in4, in4_len) == 1 |
60 | 0 | && EVP_DigestFinalXOF(ctx, out, out_len) == 1); |
61 | 0 | } |
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 | 0 | { |
70 | 0 | const SLH_DSA_PARAMS *params = ctx->key->params; |
71 | 0 | size_t m = params->m; |
72 | 0 | size_t n = params->n; |
73 | |
|
74 | 0 | return xof_digest_4(ctx->md_ctx, r, n, pk_seed, n, pk_root, n, |
75 | 0 | msg, msg_len, out, m); |
76 | 0 | } |
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 | 0 | { |
83 | 0 | const SLH_DSA_PARAMS *params = ctx->key->params; |
84 | 0 | size_t n = params->n; |
85 | |
|
86 | 0 | return xof_digest_3(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE, |
87 | 0 | sk_seed, n, out, n); |
88 | 0 | } |
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 | 0 | { |
95 | 0 | unsigned char out[SLH_MAX_N]; |
96 | 0 | const SLH_DSA_PARAMS *params = ctx->key->params; |
97 | 0 | size_t n = params->n; |
98 | |
|
99 | 0 | return xof_digest_3(ctx->md_ctx, sk_prf, n, opt_rand, n, msg, msg_len, out, n) |
100 | 0 | && WPACKET_memcpy(pkt, out, n); |
101 | 0 | } |
102 | | |
103 | | static int |
104 | | slh_f_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *pk_seed, const uint8_t *adrs, |
105 | | const uint8_t *m1, size_t m1_len, uint8_t *out, size_t out_len) |
106 | 0 | { |
107 | 0 | const SLH_DSA_PARAMS *params = ctx->key->params; |
108 | 0 | size_t n = params->n; |
109 | |
|
110 | 0 | return xof_digest_3(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE, m1, m1_len, out, n); |
111 | 0 | } |
112 | | |
113 | | static int |
114 | | slh_h_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *pk_seed, const uint8_t *adrs, |
115 | | const uint8_t *m1, const uint8_t *m2, uint8_t *out, size_t out_len) |
116 | 0 | { |
117 | 0 | const SLH_DSA_PARAMS *params = ctx->key->params; |
118 | 0 | size_t n = params->n; |
119 | |
|
120 | 0 | return xof_digest_4(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE, m1, n, m2, n, out, n); |
121 | 0 | } |
122 | | |
123 | | static int |
124 | | slh_t_shake(SLH_DSA_HASH_CTX *ctx, const uint8_t *pk_seed, const uint8_t *adrs, |
125 | | const uint8_t *ml, size_t ml_len, uint8_t *out, size_t out_len) |
126 | 0 | { |
127 | 0 | const SLH_DSA_PARAMS *params = ctx->key->params; |
128 | 0 | size_t n = params->n; |
129 | |
|
130 | 0 | return xof_digest_3(ctx->md_ctx, pk_seed, n, adrs, SLH_ADRS_SIZE, ml, ml_len, out, n); |
131 | 0 | } |
132 | | |
133 | | static ossl_inline int |
134 | | digest_4(EVP_MD_CTX *ctx, |
135 | | const uint8_t *in1, size_t in1_len, const uint8_t *in2, size_t in2_len, |
136 | | const uint8_t *in3, size_t in3_len, const uint8_t *in4, size_t in4_len, |
137 | | uint8_t *out) |
138 | 0 | { |
139 | 0 | return (EVP_DigestInit_ex2(ctx, NULL, NULL) == 1 |
140 | 0 | && EVP_DigestUpdate(ctx, in1, in1_len) == 1 |
141 | 0 | && EVP_DigestUpdate(ctx, in2, in2_len) == 1 |
142 | 0 | && EVP_DigestUpdate(ctx, in3, in3_len) == 1 |
143 | 0 | && EVP_DigestUpdate(ctx, in4, in4_len) == 1 |
144 | 0 | && EVP_DigestFinal_ex(ctx, out, NULL) == 1); |
145 | 0 | } |
146 | | |
147 | | /* FIPS 205 Section 11.2.1 and 11.2.2 */ |
148 | | |
149 | | static int |
150 | | slh_hmsg_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *r, const uint8_t *pk_seed, |
151 | | const uint8_t *pk_root, const uint8_t *msg, size_t msg_len, |
152 | | uint8_t *out, size_t out_len) |
153 | 0 | { |
154 | 0 | const SLH_DSA_PARAMS *params = hctx->key->params; |
155 | 0 | size_t m = params->m; |
156 | 0 | size_t n = params->n; |
157 | 0 | uint8_t seed[2 * SLH_MAX_N + MAX_DIGEST_SIZE]; |
158 | 0 | int sz = EVP_MD_get_size(hctx->key->md_big); |
159 | 0 | size_t seed_len = (size_t)sz + 2 * n; |
160 | |
|
161 | 0 | memcpy(seed, r, n); |
162 | 0 | memcpy(seed + n, pk_seed, n); |
163 | 0 | return digest_4(hctx->md_big_ctx, r, n, pk_seed, n, pk_root, n, msg, msg_len, |
164 | 0 | seed + 2 * n) |
165 | 0 | && (PKCS1_MGF1(out, m, seed, seed_len, hctx->key->md_big) == 0); |
166 | 0 | } |
167 | | |
168 | | static int |
169 | | slh_prf_msg_sha2(SLH_DSA_HASH_CTX *hctx, |
170 | | const uint8_t *sk_prf, const uint8_t *opt_rand, |
171 | | const uint8_t *msg, size_t msg_len, WPACKET *pkt) |
172 | 0 | { |
173 | 0 | int ret; |
174 | 0 | const SLH_DSA_KEY *key = hctx->key; |
175 | 0 | EVP_MAC_CTX *mctx = hctx->hmac_ctx; |
176 | 0 | const SLH_DSA_PARAMS *prms = key->params; |
177 | 0 | size_t n = prms->n; |
178 | 0 | uint8_t mac[MAX_DIGEST_SIZE] = {0}; |
179 | 0 | OSSL_PARAM *p = NULL; |
180 | 0 | OSSL_PARAM params[3]; |
181 | | |
182 | | /* |
183 | | * Due to the way HMAC works, it is not possible to do this code early |
184 | | * in hmac_ctx_new() since it requires a key in order to set the digest. |
185 | | * So we do a lazy update here on the first call. |
186 | | */ |
187 | 0 | if (hctx->hmac_digest_used == 0) { |
188 | 0 | p = params; |
189 | | /* The underlying digest to be used */ |
190 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, |
191 | 0 | (char *)EVP_MD_get0_name(key->md_big), 0); |
192 | 0 | if (key->propq != NULL) |
193 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES, |
194 | 0 | (char *)key->propq, 0); |
195 | 0 | *p = OSSL_PARAM_construct_end(); |
196 | 0 | p = params; |
197 | 0 | hctx->hmac_digest_used = 1; |
198 | 0 | } |
199 | |
|
200 | 0 | ret = EVP_MAC_init(mctx, sk_prf, n, p) == 1 |
201 | 0 | && EVP_MAC_update(mctx, opt_rand, n) == 1 |
202 | 0 | && EVP_MAC_update(mctx, msg, msg_len) == 1 |
203 | 0 | && EVP_MAC_final(mctx, mac, NULL, sizeof(mac)) == 1 |
204 | 0 | && WPACKET_memcpy(pkt, mac, n); /* Truncate output to n bytes */ |
205 | 0 | return ret; |
206 | 0 | } |
207 | | |
208 | | static ossl_inline int |
209 | | do_hash(EVP_MD_CTX *ctx, size_t n, const uint8_t *pk_seed, const uint8_t *adrs, |
210 | | const uint8_t *m, size_t m_len, size_t b, uint8_t *out, size_t out_len) |
211 | 0 | { |
212 | 0 | int ret; |
213 | 0 | uint8_t zeros[128] = { 0 }; |
214 | 0 | uint8_t digest[MAX_DIGEST_SIZE]; |
215 | |
|
216 | 0 | ret = digest_4(ctx, pk_seed, n, zeros, b - n, adrs, SLH_ADRSC_SIZE, |
217 | 0 | m, m_len, digest); |
218 | | /* Truncated returned value is n = 16 bytes */ |
219 | 0 | memcpy(out, digest, n); |
220 | 0 | return ret; |
221 | 0 | } |
222 | | |
223 | | static int |
224 | | slh_prf_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, |
225 | | const uint8_t *sk_seed, const uint8_t *adrs, |
226 | | uint8_t *out, size_t out_len) |
227 | 0 | { |
228 | 0 | size_t n = hctx->key->params->n; |
229 | |
|
230 | 0 | return do_hash(hctx->md_ctx, n, pk_seed, adrs, sk_seed, n, |
231 | 0 | OSSL_SLH_DSA_SHA2_NUM_ZEROS_H_AND_T_BOUND1, out, out_len); |
232 | 0 | } |
233 | | |
234 | | static int |
235 | | slh_f_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs, |
236 | | const uint8_t *m1, size_t m1_len, uint8_t *out, size_t out_len) |
237 | 0 | { |
238 | 0 | return do_hash(hctx->md_ctx, hctx->key->params->n, pk_seed, adrs, m1, m1_len, |
239 | 0 | OSSL_SLH_DSA_SHA2_NUM_ZEROS_H_AND_T_BOUND1, out, out_len); |
240 | 0 | } |
241 | | |
242 | | static int |
243 | | slh_h_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs, |
244 | | const uint8_t *m1, const uint8_t *m2, uint8_t *out, size_t out_len) |
245 | 0 | { |
246 | 0 | uint8_t m[SLH_MAX_N * 2]; |
247 | 0 | const SLH_DSA_PARAMS *prms = hctx->key->params; |
248 | 0 | size_t n = prms->n; |
249 | |
|
250 | 0 | memcpy(m, m1, n); |
251 | 0 | memcpy(m + n, m2, n); |
252 | 0 | return do_hash(hctx->md_big_ctx, n, pk_seed, adrs, m, 2 * n, |
253 | 0 | prms->sha2_h_and_t_bound, out, out_len); |
254 | 0 | } |
255 | | |
256 | | static int |
257 | | slh_t_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs, |
258 | | const uint8_t *ml, size_t ml_len, uint8_t *out, size_t out_len) |
259 | 0 | { |
260 | 0 | const SLH_DSA_PARAMS *prms = hctx->key->params; |
261 | |
|
262 | 0 | return do_hash(hctx->md_big_ctx, prms->n, pk_seed, adrs, ml, ml_len, |
263 | 0 | prms->sha2_h_and_t_bound, out, out_len); |
264 | 0 | } |
265 | | |
266 | | const SLH_HASH_FUNC *ossl_slh_get_hash_fn(int is_shake) |
267 | 24 | { |
268 | 24 | static const SLH_HASH_FUNC methods[] = { |
269 | 24 | { |
270 | 24 | slh_hmsg_shake, |
271 | 24 | slh_prf_shake, |
272 | 24 | slh_prf_msg_shake, |
273 | 24 | slh_f_shake, |
274 | 24 | slh_h_shake, |
275 | 24 | slh_t_shake |
276 | 24 | }, |
277 | 24 | { |
278 | 24 | slh_hmsg_sha2, |
279 | 24 | slh_prf_sha2, |
280 | 24 | slh_prf_msg_sha2, |
281 | 24 | slh_f_sha2, |
282 | 24 | slh_h_sha2, |
283 | 24 | slh_t_sha2 |
284 | 24 | } |
285 | 24 | }; |
286 | 24 | return &methods[is_shake ? 0 : 1]; |
287 | 24 | } |