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