summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2024-02-01 17:11:58 +0000
committertb <>2024-02-01 17:11:58 +0000
commit555b4e1a5af97f780be7eba8d1827c5ffe71a9fe (patch)
treee2070c4e4f6c2df530a294429ad28079551d7d77 /src
parent8f9e116015d3586265906c84fe53915385d7de67 (diff)
downloadopenbsd-555b4e1a5af97f780be7eba8d1827c5ffe71a9fe.tar.gz
openbsd-555b4e1a5af97f780be7eba8d1827c5ffe71a9fe.tar.bz2
openbsd-555b4e1a5af97f780be7eba8d1827c5ffe71a9fe.zip
Inline EVP_PBE_find() in its last two callers
This API was already cleaned up quite a bit, but it is unused in the ecosystem and the two internal callers can be simplified a lot when inlining the lookups. EVP_PBE_CipherInit() can walk the table of "outer" PBEs and reach into the matching pbe for its cipher_nid, md_nid and keygen(). PKCS5_v2_PBKDF2_keyivgen() uses EVP_PBE_find() as a way to mapping a PRF (given by the nid of an HMAC with some digest) to the digest's nid. This can be done by a simple switch. Move MD5 to the top and GOST to the end in that switch and wrap the latter in OPENSSL_NO_GOST, so it will go away once we define OPENSSL_NO_GOST. ok beck
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/evp/evp_pbe.c209
1 files changed, 69 insertions, 140 deletions
diff --git a/src/lib/libcrypto/evp/evp_pbe.c b/src/lib/libcrypto/evp/evp_pbe.c
index d34fc7053e..bb0c227ae7 100644
--- a/src/lib/libcrypto/evp/evp_pbe.c
+++ b/src/lib/libcrypto/evp/evp_pbe.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp_pbe.c,v 1.40 2024/01/27 17:20:20 tb Exp $ */ 1/* $OpenBSD: evp_pbe.c,v 1.41 2024/02/01 17:11:58 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
@@ -72,14 +72,12 @@
72 72
73/* Password based encryption (PBE) functions */ 73/* Password based encryption (PBE) functions */
74 74
75struct pbe_config { 75static const struct pbe_config {
76 int pbe_nid; 76 int pbe_nid;
77 int cipher_nid; 77 int cipher_nid;
78 int md_nid; 78 int md_nid;
79 EVP_PBE_KEYGEN *keygen; 79 EVP_PBE_KEYGEN *keygen;
80}; 80} pbe_outer[] = {
81
82static const struct pbe_config pbe_outer[] = {
83 { 81 {
84 .pbe_nid = NID_pbeWithMD2AndDES_CBC, 82 .pbe_nid = NID_pbeWithMD2AndDES_CBC,
85 .cipher_nid = NID_des_cbc, 83 .cipher_nid = NID_des_cbc,
@@ -168,146 +166,28 @@ static const struct pbe_config pbe_outer[] = {
168 166
169#define N_PBE_OUTER (sizeof(pbe_outer) / sizeof(pbe_outer[0])) 167#define N_PBE_OUTER (sizeof(pbe_outer) / sizeof(pbe_outer[0]))
170 168
171static const struct pbe_config pbe_prf[] = {
172 {
173 .pbe_nid = NID_hmacWithSHA1,
174 .cipher_nid = -1,
175 .md_nid = NID_sha1,
176 },
177 {
178 .pbe_nid = NID_hmacWithMD5,
179 .cipher_nid = -1,
180 .md_nid = NID_md5,
181 },
182 {
183 .pbe_nid = NID_hmacWithSHA224,
184 .cipher_nid = -1,
185 .md_nid = NID_sha224,
186 },
187 {
188 .pbe_nid = NID_hmacWithSHA256,
189 .cipher_nid = -1,
190 .md_nid = NID_sha256,
191 },
192 {
193 .pbe_nid = NID_hmacWithSHA384,
194 .cipher_nid = -1,
195 .md_nid = NID_sha384,
196 },
197 {
198 .pbe_nid = NID_hmacWithSHA512,
199 .cipher_nid = -1,
200 .md_nid = NID_sha512,
201 },
202 {
203 .pbe_nid = NID_id_HMACGostR3411_94,
204 .cipher_nid = -1,
205 .md_nid = NID_id_GostR3411_94,
206 },
207 {
208 .pbe_nid = NID_id_tc26_hmac_gost_3411_12_256,
209 .cipher_nid = -1,
210 .md_nid = NID_id_tc26_gost3411_2012_256,
211 },
212 {
213 .pbe_nid = NID_id_tc26_hmac_gost_3411_12_512,
214 .cipher_nid = -1,
215 .md_nid = NID_id_tc26_gost3411_2012_512,
216 },
217 {
218 .pbe_nid = NID_hmacWithSHA512_224,
219 .cipher_nid = -1,
220 .md_nid = NID_sha512_224,
221 },
222 {
223 .pbe_nid = NID_hmacWithSHA512_256,
224 .cipher_nid = -1,
225 .md_nid = NID_sha512_256,
226 },
227 {
228 .pbe_nid = NID_hmac_sha3_224,
229 .cipher_nid = -1,
230 .md_nid = NID_sha3_224,
231 },
232 {
233 .pbe_nid = NID_hmac_sha3_256,
234 .cipher_nid = -1,
235 .md_nid = NID_sha3_256,
236 },
237 {
238 .pbe_nid = NID_hmac_sha3_384,
239 .cipher_nid = -1,
240 .md_nid = NID_sha3_384,
241 },
242 {
243 .pbe_nid = NID_hmac_sha3_512,
244 .cipher_nid = -1,
245 .md_nid = NID_sha3_512,
246 },
247};
248
249#define N_PBE_PRF (sizeof(pbe_prf) / sizeof(pbe_prf[0]))
250
251int
252EVP_PBE_find(int type, int pbe_nid, int *out_cipher_nid, int *out_md_nid,
253 EVP_PBE_KEYGEN **out_keygen)
254{
255 const struct pbe_config *pbe = NULL;
256 size_t i;
257
258 if (out_cipher_nid != NULL)
259 *out_cipher_nid = NID_undef;
260 if (out_md_nid != NULL)
261 *out_md_nid = NID_undef;
262 if (out_keygen != NULL)
263 *out_keygen = NULL;
264
265 if (pbe_nid == NID_undef)
266 return 0;
267
268 if (type == EVP_PBE_TYPE_OUTER) {
269 for (i = 0; i < N_PBE_OUTER; i++) {
270 if (pbe_nid == pbe_outer[i].pbe_nid) {
271 pbe = &pbe_outer[i];
272 break;
273 }
274 }
275 } else if (type == EVP_PBE_TYPE_PRF) {
276 for (i = 0; i < N_PBE_PRF; i++) {
277 if (pbe_nid == pbe_prf[i].pbe_nid) {
278 pbe = &pbe_prf[i];
279 break;
280 }
281 }
282 }
283 if (pbe == NULL)
284 return 0;
285
286 if (out_cipher_nid != NULL)
287 *out_cipher_nid = pbe->cipher_nid;
288 if (out_md_nid != NULL)
289 *out_md_nid = pbe->md_nid;
290 if (out_keygen != NULL)
291 *out_keygen = pbe->keygen;
292
293 return 1;
294}
295
296int 169int
297EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen, 170EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
298 ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de) 171 ASN1_TYPE *param, EVP_CIPHER_CTX *ctx, int en_de)
299{ 172{
173 const struct pbe_config *cfg = NULL;
300 const EVP_CIPHER *cipher = NULL; 174 const EVP_CIPHER *cipher = NULL;
301 const EVP_MD *md = NULL; 175 const EVP_MD *md = NULL;
302 int pbe_nid, cipher_nid, md_nid; 176 int pbe_nid;
303 EVP_PBE_KEYGEN *keygen; 177 size_t i;
304 178
305 if ((pbe_nid = OBJ_obj2nid(pbe_obj)) == NID_undef) { 179 if ((pbe_nid = OBJ_obj2nid(pbe_obj)) == NID_undef) {
306 EVPerror(EVP_R_UNKNOWN_PBE_ALGORITHM); 180 EVPerror(EVP_R_UNKNOWN_PBE_ALGORITHM);
307 return 0; 181 return 0;
308 } 182 }
309 if (!EVP_PBE_find(EVP_PBE_TYPE_OUTER, pbe_nid, &cipher_nid, &md_nid, 183
310 &keygen)) { 184 for (i = 0; i < N_PBE_OUTER; i++) {
185 if (pbe_nid == pbe_outer[i].pbe_nid) {
186 cfg = &pbe_outer[i];
187 break;
188 }
189 }
190 if (cfg == NULL) {
311 EVPerror(EVP_R_UNKNOWN_PBE_ALGORITHM); 191 EVPerror(EVP_R_UNKNOWN_PBE_ALGORITHM);
312 ERR_asprintf_error_data("NID=%d", pbe_nid); 192 ERR_asprintf_error_data("NID=%d", pbe_nid);
313 return 0; 193 return 0;
@@ -318,20 +198,20 @@ EVP_PBE_CipherInit(ASN1_OBJECT *pbe_obj, const char *pass, int passlen,
318 if (passlen == -1) 198 if (passlen == -1)
319 passlen = strlen(pass); 199 passlen = strlen(pass);
320 200
321 if (cipher_nid != -1) { 201 if (cfg->cipher_nid != -1) {
322 if ((cipher = EVP_get_cipherbynid(cipher_nid)) == NULL) { 202 if ((cipher = EVP_get_cipherbynid(cfg->cipher_nid)) == NULL) {
323 EVPerror(EVP_R_UNKNOWN_CIPHER); 203 EVPerror(EVP_R_UNKNOWN_CIPHER);
324 return 0; 204 return 0;
325 } 205 }
326 } 206 }
327 if (md_nid != -1) { 207 if (cfg->md_nid != -1) {
328 if ((md = EVP_get_digestbynid(md_nid)) == NULL) { 208 if ((md = EVP_get_digestbynid(cfg->md_nid)) == NULL) {
329 EVPerror(EVP_R_UNKNOWN_DIGEST); 209 EVPerror(EVP_R_UNKNOWN_DIGEST);
330 return 0; 210 return 0;
331 } 211 }
332 } 212 }
333 213
334 if (!keygen(ctx, pass, passlen, param, cipher, md, en_de)) { 214 if (!cfg->keygen(ctx, pass, passlen, param, cipher, md, en_de)) {
335 EVPerror(EVP_R_KEYGEN_FAILURE); 215 EVPerror(EVP_R_KEYGEN_FAILURE);
336 return 0; 216 return 0;
337 } 217 }
@@ -575,6 +455,47 @@ PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
575 return ret; 455 return ret;
576} 456}
577 457
458static int
459md_nid_from_prf_nid(int nid)
460{
461 switch (nid) {
462 case NID_hmacWithMD5:
463 return NID_md5;
464 case NID_hmacWithSHA1:
465 return NID_sha1;
466 case NID_hmacWithSHA224:
467 return NID_sha224;
468 case NID_hmacWithSHA256:
469 return NID_sha256;
470 case NID_hmacWithSHA384:
471 return NID_sha384;
472 case NID_hmacWithSHA512:
473 return NID_sha512;
474 case NID_hmacWithSHA512_224:
475 return NID_sha512_224;
476 case NID_hmacWithSHA512_256:
477 return NID_sha512_256;
478 case NID_hmac_sha3_224:
479 return NID_sha3_224;
480 case NID_hmac_sha3_256:
481 return NID_sha3_256;
482 case NID_hmac_sha3_384:
483 return NID_sha3_384;
484 case NID_hmac_sha3_512:
485 return NID_sha3_512;
486#ifndef OPENSSL_NO_GOST
487 case NID_id_HMACGostR3411_94:
488 return NID_id_GostR3411_94;
489 case NID_id_tc26_hmac_gost_3411_12_256:
490 return NID_id_tc26_gost3411_2012_256;
491 case NID_id_tc26_hmac_gost_3411_12_512:
492 return NID_id_tc26_gost3411_2012_512;
493#endif
494 default:
495 return NID_undef;
496 }
497}
498
578int 499int
579PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen, 500PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
580 ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, int en_de) 501 ASN1_TYPE *param, const EVP_CIPHER *c, const EVP_MD *md, int en_de)
@@ -626,7 +547,7 @@ PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
626 else 547 else
627 prf_nid = NID_hmacWithSHA1; 548 prf_nid = NID_hmacWithSHA1;
628 549
629 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, NULL)) { 550 if ((hmac_md_nid = md_nid_from_prf_nid(prf_nid)) == NID_undef) {
630 EVPerror(EVP_R_UNSUPPORTED_PRF); 551 EVPerror(EVP_R_UNSUPPORTED_PRF);
631 goto err; 552 goto err;
632 } 553 }
@@ -725,6 +646,14 @@ LCRYPTO_ALIAS(PKCS12_PBE_keyivgen);
725 */ 646 */
726 647
727int 648int
649EVP_PBE_find(int type, int pbe_nid, int *out_cipher_nid, int *out_md_nid,
650 EVP_PBE_KEYGEN **out_keygen)
651{
652 EVPerror(ERR_R_DISABLED);
653 return 0;
654}
655
656int
728EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid, 657EVP_PBE_alg_add_type(int pbe_type, int pbe_nid, int cipher_nid, int md_nid,
729 EVP_PBE_KEYGEN *keygen) 658 EVP_PBE_KEYGEN *keygen)
730{ 659{