/src/openssl/providers/implementations/kdfs/snmpkdf.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 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 <stdlib.h> |
11 | | #include <stdarg.h> |
12 | | #include <string.h> |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/kdf.h> |
15 | | #include <openssl/sha.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/proverr.h> |
18 | | #include "internal/cryptlib.h" |
19 | | #include "internal/fips.h" |
20 | | #include "internal/numbers.h" |
21 | | #include "crypto/evp.h" |
22 | | #include "prov/provider_ctx.h" |
23 | | #include "prov/providercommon.h" |
24 | | #include "prov/implementations.h" |
25 | | #include "prov/provider_util.h" |
26 | | #include "providers/implementations/kdfs/snmpkdf.inc" |
27 | | |
28 | 2.45M | #define KDF_SNMP_PASSWORD_HASH_AMOUNT (1024 * 1024) |
29 | 149 | #define KDF_SNMP_MIN_PASSWORD_LEN 8 |
30 | | |
31 | | /* See RFC 3414, Appendix A.2.2 */ |
32 | | /* See NIST SP800-135 Section 6.8 */ |
33 | | static OSSL_FUNC_kdf_newctx_fn kdf_snmpkdf_new; |
34 | | static OSSL_FUNC_kdf_dupctx_fn kdf_snmpkdf_dup; |
35 | | static OSSL_FUNC_kdf_freectx_fn kdf_snmpkdf_free; |
36 | | static OSSL_FUNC_kdf_reset_fn kdf_snmpkdf_reset; |
37 | | static OSSL_FUNC_kdf_derive_fn kdf_snmpkdf_derive; |
38 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_snmpkdf_settable_ctx_params; |
39 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_snmpkdf_set_ctx_params; |
40 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_snmpkdf_gettable_ctx_params; |
41 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_snmpkdf_get_ctx_params; |
42 | | |
43 | | static int SNMPKDF(const EVP_MD *evp_md, |
44 | | const unsigned char *eid, size_t eid_len, |
45 | | unsigned char *password, size_t password_len, |
46 | | unsigned char *key, size_t keylen); |
47 | | |
48 | | typedef struct { |
49 | | /* Warning: Any changes to this structure may require you to update kdf_snmpkdf_dup */ |
50 | | void *provctx; |
51 | | PROV_DIGEST digest; |
52 | | unsigned char *eid; |
53 | | size_t eid_len; |
54 | | unsigned char *password; |
55 | | size_t password_len; |
56 | | } KDF_SNMPKDF; |
57 | | |
58 | | static void *kdf_snmpkdf_new(void *provctx) |
59 | 153 | { |
60 | 153 | KDF_SNMPKDF *ctx; |
61 | | |
62 | 153 | if (!ossl_prov_is_running()) |
63 | 0 | return NULL; |
64 | | |
65 | | #ifdef FIPS_MODULE |
66 | | if (!ossl_deferred_self_test(PROV_LIBCTX_OF(provctx), |
67 | | ST_ID_KDF_SNMPKDF)) |
68 | | return NULL; |
69 | | #endif |
70 | | |
71 | 153 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) |
72 | 153 | ctx->provctx = provctx; |
73 | 153 | return ctx; |
74 | 153 | } |
75 | | |
76 | | static void *kdf_snmpkdf_dup(void *vctx) |
77 | 0 | { |
78 | 0 | const KDF_SNMPKDF *src = (const KDF_SNMPKDF *)vctx; |
79 | 0 | KDF_SNMPKDF *dest; |
80 | |
|
81 | 0 | dest = kdf_snmpkdf_new(src->provctx); |
82 | 0 | if (dest != NULL) { |
83 | 0 | if (!ossl_prov_memdup(src->eid, src->eid_len, |
84 | 0 | &dest->eid, &dest->eid_len) |
85 | 0 | || !ossl_prov_memdup(src->password, src->password_len, |
86 | 0 | &dest->password, &dest->password_len) |
87 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
88 | 0 | goto err; |
89 | 0 | } |
90 | 0 | return dest; |
91 | | |
92 | 0 | err: |
93 | 0 | kdf_snmpkdf_free(dest); |
94 | 0 | return NULL; |
95 | 0 | } |
96 | | |
97 | | static void kdf_snmpkdf_free(void *vctx) |
98 | 153 | { |
99 | 153 | KDF_SNMPKDF *ctx = (KDF_SNMPKDF *)vctx; |
100 | | |
101 | 153 | if (ctx != NULL) { |
102 | 153 | kdf_snmpkdf_reset(ctx); |
103 | 153 | OPENSSL_free(ctx); |
104 | 153 | } |
105 | 153 | } |
106 | | |
107 | | static void kdf_snmpkdf_reset(void *vctx) |
108 | 153 | { |
109 | 153 | KDF_SNMPKDF *ctx = (KDF_SNMPKDF *)vctx; |
110 | 153 | void *provctx = ctx->provctx; |
111 | | |
112 | 153 | ossl_prov_digest_reset(&ctx->digest); |
113 | 153 | OPENSSL_clear_free(ctx->eid, ctx->eid_len); |
114 | 153 | OPENSSL_clear_free(ctx->password, ctx->password_len); |
115 | 153 | memset(ctx, 0, sizeof(*ctx)); |
116 | 153 | ctx->provctx = provctx; |
117 | 153 | } |
118 | | |
119 | | static int snmpkdf_set_membuf(unsigned char **dst, size_t *dst_len, |
120 | | const OSSL_PARAM *p) |
121 | 291 | { |
122 | 291 | OPENSSL_clear_free(*dst, *dst_len); |
123 | 291 | *dst = NULL; |
124 | 291 | *dst_len = 0; |
125 | 291 | return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len); |
126 | 291 | } |
127 | | |
128 | | static int kdf_snmpkdf_derive(void *vctx, unsigned char *key, size_t keylen, |
129 | | const OSSL_PARAM params[]) |
130 | 142 | { |
131 | 142 | KDF_SNMPKDF *ctx = (KDF_SNMPKDF *)vctx; |
132 | 142 | const EVP_MD *md; |
133 | | |
134 | 142 | if (!ossl_prov_is_running() || !kdf_snmpkdf_set_ctx_params(ctx, params)) |
135 | 0 | return 0; |
136 | | |
137 | 142 | md = ossl_prov_digest_md(&ctx->digest); |
138 | 142 | if (md == NULL) { |
139 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
140 | 0 | return 0; |
141 | 0 | } |
142 | 142 | if (ctx->eid == NULL) { |
143 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_EID); |
144 | 0 | return 0; |
145 | 0 | } |
146 | 142 | if (ctx->password == NULL) { |
147 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
148 | 0 | return 0; |
149 | 0 | } |
150 | | |
151 | 142 | return SNMPKDF(md, ctx->eid, ctx->eid_len, |
152 | 142 | ctx->password, ctx->password_len, |
153 | 142 | key, keylen); |
154 | 142 | } |
155 | | |
156 | | static int kdf_snmpkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
157 | 295 | { |
158 | 295 | struct snmp_set_ctx_params_st p; |
159 | 295 | KDF_SNMPKDF *ctx = vctx; |
160 | 295 | OSSL_LIB_CTX *libctx; |
161 | | #ifdef FIPS_MODULE |
162 | | const EVP_MD *md = NULL; |
163 | | #endif |
164 | | |
165 | 295 | if (params == NULL) |
166 | 142 | return 1; |
167 | | |
168 | 153 | if (ctx == NULL || !snmp_set_ctx_params_decoder(params, &p)) |
169 | 0 | return 0; |
170 | | |
171 | 153 | libctx = PROV_LIBCTX_OF(ctx->provctx); |
172 | 153 | if (p.digest != NULL) { |
173 | 153 | if (!ossl_prov_digest_load(&ctx->digest, p.digest, p.propq, libctx)) |
174 | 4 | return 0; |
175 | | #ifdef FIPS_MODULE |
176 | | md = ossl_prov_digest_md(&ctx->digest); |
177 | | if (!EVP_MD_is_a(md, SN_sha1)) |
178 | | return 0; |
179 | | #endif |
180 | 153 | } |
181 | | |
182 | 149 | if (p.pw != NULL) { |
183 | 149 | if (!snmpkdf_set_membuf(&ctx->password, &ctx->password_len, p.pw)) |
184 | 0 | return 0; |
185 | 149 | if ((ctx->password_len > KDF_SNMP_PASSWORD_HASH_AMOUNT) || (ctx->password_len < KDF_SNMP_MIN_PASSWORD_LEN)) |
186 | 7 | return 0; |
187 | 149 | } |
188 | | |
189 | 142 | if (p.eid != NULL && !snmpkdf_set_membuf(&ctx->eid, &ctx->eid_len, p.eid)) |
190 | 0 | return 0; |
191 | | |
192 | 142 | return 1; |
193 | 142 | } |
194 | | |
195 | | static const OSSL_PARAM *kdf_snmpkdf_settable_ctx_params(ossl_unused void *ctx, |
196 | | ossl_unused void *p_ctx) |
197 | 153 | { |
198 | 153 | return snmp_set_ctx_params_list; |
199 | 153 | } |
200 | | |
201 | | static size_t kdf_snmpkdf_size(KDF_SNMPKDF *ctx) |
202 | 0 | { |
203 | 0 | int len; |
204 | 0 | const EVP_MD *md = NULL; |
205 | |
|
206 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
207 | 0 | if (md == NULL) { |
208 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
209 | 0 | return 0; |
210 | 0 | } |
211 | 0 | len = EVP_MD_get_size(md); |
212 | 0 | return (len <= 0) ? 0 : (size_t)len; |
213 | 0 | } |
214 | | |
215 | | static int kdf_snmpkdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
216 | 0 | { |
217 | 0 | struct snmp_get_ctx_params_st p; |
218 | 0 | KDF_SNMPKDF *ctx = vctx; |
219 | |
|
220 | 0 | if (ctx == NULL || !snmp_get_ctx_params_decoder(params, &p)) |
221 | 0 | return 0; |
222 | | |
223 | 0 | if (p.size != NULL) { |
224 | 0 | size_t sz = kdf_snmpkdf_size(ctx); |
225 | |
|
226 | 0 | if (sz == 0) |
227 | 0 | return 0; |
228 | 0 | if (!OSSL_PARAM_set_size_t(p.size, sz)) |
229 | 0 | return 0; |
230 | 0 | } |
231 | 0 | return 1; |
232 | 0 | } |
233 | | |
234 | | static const OSSL_PARAM *kdf_snmpkdf_gettable_ctx_params(ossl_unused void *ctx, |
235 | | ossl_unused void *p_ctx) |
236 | 0 | { |
237 | 0 | return snmp_get_ctx_params_list; |
238 | 0 | } |
239 | | |
240 | | const OSSL_DISPATCH ossl_kdf_snmpkdf_functions[] = { |
241 | | { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_snmpkdf_new }, |
242 | | { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_snmpkdf_dup }, |
243 | | { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_snmpkdf_free }, |
244 | | { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_snmpkdf_reset }, |
245 | | { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_snmpkdf_derive }, |
246 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
247 | | (void (*)(void))kdf_snmpkdf_settable_ctx_params }, |
248 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void (*)(void))kdf_snmpkdf_set_ctx_params }, |
249 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
250 | | (void (*)(void))kdf_snmpkdf_gettable_ctx_params }, |
251 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void (*)(void))kdf_snmpkdf_get_ctx_params }, |
252 | | { 0, NULL } |
253 | | }; |
254 | | |
255 | | /* |
256 | | * SNMPKDF - In compliance with SP800-135 and RFC7860, calculate |
257 | | * a master key using the engine ID and password. |
258 | | * |
259 | | * Denote engineLength and passwordlen to be the lengths (in bytes) of an |
260 | | * snmpEngineID and a password, respectively. |
261 | | * |
262 | | * Let N = (1024*1024)/passwordlen |
263 | | * |
264 | | * Expanded_password = the leftmost 1048576 bytes of the string of N |
265 | | * repetitions of the password. |
266 | | * |
267 | | * Derived_password = SHA-1 (Expanded_password). The Derived_password |
268 | | * is the output of hashing the Expanded_password by SHA-1. |
269 | | * |
270 | | * Let Shared_key to be the key that the user shares with the authoritative |
271 | | * SNMP engine with ID snmpEngineID. The Shared_key is generated as follows: |
272 | | * |
273 | | * Shared_key = SHA-1(Derived_password || snmpEngineID || Derived_password). |
274 | | * |
275 | | * Input: |
276 | | * e_id - engine ID(eid) |
277 | | * e_len - engineID length |
278 | | * password - password |
279 | | * password_len - password length |
280 | | * okey - pointer to key output, FIPS testing limited to SHA-1. |
281 | | * keylen - key length |
282 | | * Output: |
283 | | * okey - filled with derived key |
284 | | * return - 1 on pass, 0 fail |
285 | | */ |
286 | | static int SNMPKDF(const EVP_MD *evp_md, |
287 | | const unsigned char *e_id, size_t e_len, |
288 | | unsigned char *password, size_t password_len, |
289 | | unsigned char *okey, size_t keylen) |
290 | 142 | { |
291 | 142 | EVP_MD_CTX *md = NULL; |
292 | 142 | unsigned char digest[EVP_MAX_MD_SIZE]; |
293 | 142 | size_t mdsize = 0, len = 0; |
294 | 142 | unsigned int md_len = 0; |
295 | 142 | int ret = 0; |
296 | | |
297 | | /* Limited to SHA-1 and SHA-2 hashes presently */ |
298 | 142 | if (okey == NULL || keylen == 0) |
299 | 0 | return 0; |
300 | | |
301 | 142 | md = EVP_MD_CTX_new(); |
302 | 142 | if (md == NULL) { |
303 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
304 | 0 | goto err; |
305 | 0 | } |
306 | | |
307 | 142 | mdsize = EVP_MD_get_size(evp_md); |
308 | 142 | if (mdsize <= 0 || mdsize > keylen) |
309 | 2 | goto err; |
310 | | |
311 | 140 | if (!EVP_DigestInit_ex(md, evp_md, NULL)) |
312 | 0 | goto err; |
313 | | |
314 | 2.45M | for (len = 0; len < KDF_SNMP_PASSWORD_HASH_AMOUNT - password_len; len += password_len) { |
315 | 2.45M | if (!EVP_DigestUpdate(md, password, password_len)) |
316 | 0 | goto err; |
317 | 2.45M | } |
318 | | |
319 | 140 | if (!EVP_DigestUpdate(md, password, KDF_SNMP_PASSWORD_HASH_AMOUNT - len) |
320 | 140 | || !EVP_DigestFinal_ex(md, digest, &md_len) |
321 | 140 | || !EVP_DigestInit_ex(md, evp_md, NULL) |
322 | 140 | || !EVP_DigestUpdate(md, digest, mdsize) |
323 | 140 | || !EVP_DigestUpdate(md, e_id, e_len) |
324 | 140 | || !EVP_DigestUpdate(md, digest, mdsize) |
325 | 140 | || !EVP_DigestFinal_ex(md, digest, &md_len)) |
326 | 0 | goto err; |
327 | | |
328 | 140 | memcpy(okey, digest, md_len); |
329 | | |
330 | 140 | ret = 1; |
331 | | |
332 | 142 | err: |
333 | 142 | EVP_MD_CTX_free(md); |
334 | 142 | OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE); |
335 | 142 | return ret; |
336 | 140 | } |