/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 | if (sz <= 0) |
162 | 0 | return 0; |
163 | | |
164 | 0 | memcpy(seed, r, n); |
165 | 0 | memcpy(seed + n, pk_seed, n); |
166 | 0 | return digest_4(hctx->md_big_ctx, r, n, pk_seed, n, pk_root, n, msg, msg_len, |
167 | 0 | seed + 2 * n) |
168 | 0 | && (PKCS1_MGF1(out, (long)m, seed, (long)seed_len, hctx->key->md_big) == 0); |
169 | 0 | } |
170 | | |
171 | | static int |
172 | | slh_prf_msg_sha2(SLH_DSA_HASH_CTX *hctx, |
173 | | const uint8_t *sk_prf, const uint8_t *opt_rand, |
174 | | const uint8_t *msg, size_t msg_len, WPACKET *pkt) |
175 | 0 | { |
176 | 0 | int ret; |
177 | 0 | const SLH_DSA_KEY *key = hctx->key; |
178 | 0 | EVP_MAC_CTX *mctx = hctx->hmac_ctx; |
179 | 0 | const SLH_DSA_PARAMS *prms = key->params; |
180 | 0 | size_t n = prms->n; |
181 | 0 | uint8_t mac[MAX_DIGEST_SIZE] = {0}; |
182 | 0 | OSSL_PARAM *p = NULL; |
183 | 0 | OSSL_PARAM params[3]; |
184 | | |
185 | | /* |
186 | | * Due to the way HMAC works, it is not possible to do this code early |
187 | | * in hmac_ctx_new() since it requires a key in order to set the digest. |
188 | | * So we do a lazy update here on the first call. |
189 | | */ |
190 | 0 | if (hctx->hmac_digest_used == 0) { |
191 | 0 | p = params; |
192 | | /* The underlying digest to be used */ |
193 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, |
194 | 0 | (char *)EVP_MD_get0_name(key->md_big), 0); |
195 | 0 | if (key->propq != NULL) |
196 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES, |
197 | 0 | (char *)key->propq, 0); |
198 | 0 | *p = OSSL_PARAM_construct_end(); |
199 | 0 | p = params; |
200 | 0 | hctx->hmac_digest_used = 1; |
201 | 0 | } |
202 | |
|
203 | 0 | ret = EVP_MAC_init(mctx, sk_prf, n, p) == 1 |
204 | 0 | && EVP_MAC_update(mctx, opt_rand, n) == 1 |
205 | 0 | && EVP_MAC_update(mctx, msg, msg_len) == 1 |
206 | 0 | && EVP_MAC_final(mctx, mac, NULL, sizeof(mac)) == 1 |
207 | 0 | && WPACKET_memcpy(pkt, mac, n); /* Truncate output to n bytes */ |
208 | 0 | return ret; |
209 | 0 | } |
210 | | |
211 | | static ossl_inline int |
212 | | do_hash(EVP_MD_CTX *ctx, size_t n, const uint8_t *pk_seed, const uint8_t *adrs, |
213 | | const uint8_t *m, size_t m_len, size_t b, uint8_t *out, size_t out_len) |
214 | 0 | { |
215 | 0 | int ret; |
216 | 0 | uint8_t zeros[128] = { 0 }; |
217 | 0 | uint8_t digest[MAX_DIGEST_SIZE]; |
218 | |
|
219 | 0 | ret = digest_4(ctx, pk_seed, n, zeros, b - n, adrs, SLH_ADRSC_SIZE, |
220 | 0 | m, m_len, digest); |
221 | | /* Truncated returned value is n = 16 bytes */ |
222 | 0 | memcpy(out, digest, n); |
223 | 0 | return ret; |
224 | 0 | } |
225 | | |
226 | | static int |
227 | | slh_prf_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, |
228 | | const uint8_t *sk_seed, const uint8_t *adrs, |
229 | | uint8_t *out, size_t out_len) |
230 | 0 | { |
231 | 0 | size_t n = hctx->key->params->n; |
232 | |
|
233 | 0 | return do_hash(hctx->md_ctx, n, pk_seed, adrs, sk_seed, n, |
234 | 0 | OSSL_SLH_DSA_SHA2_NUM_ZEROS_H_AND_T_BOUND1, out, out_len); |
235 | 0 | } |
236 | | |
237 | | static int |
238 | | slh_f_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs, |
239 | | const uint8_t *m1, size_t m1_len, uint8_t *out, size_t out_len) |
240 | 0 | { |
241 | 0 | return do_hash(hctx->md_ctx, hctx->key->params->n, pk_seed, adrs, m1, m1_len, |
242 | 0 | OSSL_SLH_DSA_SHA2_NUM_ZEROS_H_AND_T_BOUND1, out, out_len); |
243 | 0 | } |
244 | | |
245 | | static int |
246 | | slh_h_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs, |
247 | | const uint8_t *m1, const uint8_t *m2, uint8_t *out, size_t out_len) |
248 | 0 | { |
249 | 0 | uint8_t m[SLH_MAX_N * 2]; |
250 | 0 | const SLH_DSA_PARAMS *prms = hctx->key->params; |
251 | 0 | size_t n = prms->n; |
252 | |
|
253 | 0 | memcpy(m, m1, n); |
254 | 0 | memcpy(m + n, m2, n); |
255 | 0 | return do_hash(hctx->md_big_ctx, n, pk_seed, adrs, m, 2 * n, |
256 | 0 | prms->sha2_h_and_t_bound, out, out_len); |
257 | 0 | } |
258 | | |
259 | | static int |
260 | | slh_t_sha2(SLH_DSA_HASH_CTX *hctx, const uint8_t *pk_seed, const uint8_t *adrs, |
261 | | const uint8_t *ml, size_t ml_len, uint8_t *out, size_t out_len) |
262 | 0 | { |
263 | 0 | const SLH_DSA_PARAMS *prms = hctx->key->params; |
264 | |
|
265 | 0 | return do_hash(hctx->md_big_ctx, prms->n, pk_seed, adrs, ml, ml_len, |
266 | 0 | prms->sha2_h_and_t_bound, out, out_len); |
267 | 0 | } |
268 | | |
269 | | const SLH_HASH_FUNC *ossl_slh_get_hash_fn(int is_shake) |
270 | 0 | { |
271 | 0 | static const SLH_HASH_FUNC methods[] = { |
272 | 0 | { |
273 | 0 | slh_hmsg_shake, |
274 | 0 | slh_prf_shake, |
275 | 0 | slh_prf_msg_shake, |
276 | 0 | slh_f_shake, |
277 | 0 | slh_h_shake, |
278 | 0 | slh_t_shake |
279 | 0 | }, |
280 | 0 | { |
281 | 0 | slh_hmsg_sha2, |
282 | 0 | slh_prf_sha2, |
283 | 0 | slh_prf_msg_sha2, |
284 | 0 | slh_f_sha2, |
285 | 0 | slh_h_sha2, |
286 | 0 | slh_t_sha2 |
287 | 0 | } |
288 | 0 | }; |
289 | 0 | return &methods[is_shake ? 0 : 1]; |
290 | 0 | } |