summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/evp/p_lib.c
diff options
context:
space:
mode:
authortb <>2021-03-29 15:57:23 +0000
committertb <>2021-03-29 15:57:23 +0000
commitd9dfab150e9c80a3bafbf4effd23e943ab9ba197 (patch)
treebf7d2d4408b27c2ebee7a4c8281f8986c9add8a9 /src/lib/libcrypto/evp/p_lib.c
parent2e8ea05ba51067fc5bc08b0749d727cb74a13b62 (diff)
downloadopenbsd-d9dfab150e9c80a3bafbf4effd23e943ab9ba197.tar.gz
openbsd-d9dfab150e9c80a3bafbf4effd23e943ab9ba197.tar.bz2
openbsd-d9dfab150e9c80a3bafbf4effd23e943ab9ba197.zip
Prepare to provide EVP_PKEY_new_CMAC_key()
sebastia ran into this when attempting to update security/hcxtools. This will be tested via wycheproof.go once the symbol is public. ok jsing, tested by sebastia
Diffstat (limited to 'src/lib/libcrypto/evp/p_lib.c')
-rw-r--r--src/lib/libcrypto/evp/p_lib.c51
1 files changed, 43 insertions, 8 deletions
diff --git a/src/lib/libcrypto/evp/p_lib.c b/src/lib/libcrypto/evp/p_lib.c
index 13a9d65f28..9577b10ea1 100644
--- a/src/lib/libcrypto/evp/p_lib.c
+++ b/src/lib/libcrypto/evp/p_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: p_lib.c,v 1.25 2019/03/17 18:17:45 tb Exp $ */ 1/* $OpenBSD: p_lib.c,v 1.26 2021/03/29 15:57:23 tb Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -61,6 +61,7 @@
61#include <openssl/opensslconf.h> 61#include <openssl/opensslconf.h>
62 62
63#include <openssl/bn.h> 63#include <openssl/bn.h>
64#include <openssl/cmac.h>
64#include <openssl/err.h> 65#include <openssl/err.h>
65#include <openssl/evp.h> 66#include <openssl/evp.h>
66#include <openssl/objects.h> 67#include <openssl/objects.h>
@@ -216,10 +217,14 @@ EVP_PKEY_up_ref(EVP_PKEY *pkey)
216 */ 217 */
217 218
218static int 219static int
219pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len) 220pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, int len)
220{ 221{
221 const EVP_PKEY_ASN1_METHOD *ameth; 222 const EVP_PKEY_ASN1_METHOD *ameth;
222 ENGINE *e = NULL; 223 ENGINE **eptr = NULL;
224
225 if (e == NULL)
226 eptr = &e;
227
223 if (pkey) { 228 if (pkey) {
224 if (pkey->pkey.ptr) 229 if (pkey->pkey.ptr)
225 EVP_PKEY_free_it(pkey); 230 EVP_PKEY_free_it(pkey);
@@ -234,11 +239,11 @@ pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
234#endif 239#endif
235 } 240 }
236 if (str) 241 if (str)
237 ameth = EVP_PKEY_asn1_find_str(&e, str, len); 242 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
238 else 243 else
239 ameth = EVP_PKEY_asn1_find(&e, type); 244 ameth = EVP_PKEY_asn1_find(eptr, type);
240#ifndef OPENSSL_NO_ENGINE 245#ifndef OPENSSL_NO_ENGINE
241 if (pkey == NULL) 246 if (pkey == NULL && eptr != NULL)
242 ENGINE_finish(e); 247 ENGINE_finish(e);
243#endif 248#endif
244 if (!ameth) { 249 if (!ameth) {
@@ -258,13 +263,43 @@ pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
258int 263int
259EVP_PKEY_set_type(EVP_PKEY *pkey, int type) 264EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
260{ 265{
261 return pkey_set_type(pkey, type, NULL, -1); 266 return pkey_set_type(pkey, NULL, type, NULL, -1);
267}
268
269EVP_PKEY *
270EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, size_t len,
271 const EVP_CIPHER *cipher)
272{
273 EVP_PKEY *ret = NULL;
274 CMAC_CTX *cmctx = NULL;
275
276 if ((ret = EVP_PKEY_new()) == NULL)
277 goto err;
278 if ((cmctx = CMAC_CTX_new()) == NULL)
279 goto err;
280
281 if (!pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1))
282 goto err;
283
284 if (!CMAC_Init(cmctx, priv, len, cipher, e)) {
285 EVPerror(EVP_R_KEY_SETUP_FAILED);
286 goto err;
287 }
288
289 ret->pkey.ptr = (char *)cmctx;
290
291 return ret;
292
293 err:
294 EVP_PKEY_free(ret);
295 CMAC_CTX_free(cmctx);
296 return NULL;
262} 297}
263 298
264int 299int
265EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) 300EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
266{ 301{
267 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len); 302 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
268} 303}
269 304
270int 305int