Fix 9bf682f which broke nistp224_method
[openssl.git] / crypto / dh / dh_kdf.c
1 /*
2  * Copyright 2013-2019 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 "e_os.h"
11
12 #ifndef OPENSSL_NO_CMS
13 # include <string.h>
14 # include <openssl/dh.h>
15 # include <openssl/evp.h>
16 # include <openssl/asn1.h>
17 # include <openssl/kdf.h>
18
19 int DH_KDF_X9_42(unsigned char *out, size_t outlen,
20                  const unsigned char *Z, size_t Zlen,
21                  ASN1_OBJECT *key_oid,
22                  const unsigned char *ukm, size_t ukmlen, const EVP_MD *md)
23 {
24     int ret = 0, nid;
25     EVP_KDF_CTX *kctx = NULL;
26     const EVP_KDF *kdf = NULL;
27     const char *oid_sn;
28
29     nid = OBJ_obj2nid(key_oid);
30     if (nid == NID_undef)
31         return 0;
32     oid_sn = OBJ_nid2sn(nid);
33     if (oid_sn == NULL)
34         return 0;
35
36     kdf = EVP_get_kdfbyname(SN_x942kdf);
37     if (kdf == NULL)
38         goto err;
39     kctx = EVP_KDF_CTX_new(kdf);
40     ret =
41         kctx != NULL
42         && EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, md) > 0
43         && EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, Z, Zlen) > 0
44         && (ukm == NULL
45             || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_UKM, ukm, ukmlen) > 0)
46         && EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_CEK_ALG, oid_sn) > 0
47         && EVP_KDF_derive(kctx, out, outlen) > 0;
48 err:
49     EVP_KDF_CTX_free(kctx);
50     return ret;
51 }
52 #endif /* OPENSSL_NO_CMS */