Usages of KDFs converted to use the name macros
[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/core_names.h>
15 # include <openssl/dh.h>
16 # include <openssl/evp.h>
17 # include <openssl/asn1.h>
18 # include <openssl/kdf.h>
19 # include <internal/provider.h>
20
21 int DH_KDF_X9_42(unsigned char *out, size_t outlen,
22                  const unsigned char *Z, size_t Zlen,
23                  ASN1_OBJECT *key_oid,
24                  const unsigned char *ukm, size_t ukmlen, const EVP_MD *md)
25 {
26     int ret = 0, nid;
27     EVP_KDF_CTX *kctx = NULL;
28     EVP_KDF *kdf = NULL;
29     const char *oid_sn;
30     OSSL_PARAM params[5], *p = params;
31     const char *mdname = EVP_MD_name(md);
32     const OSSL_PROVIDER *prov = EVP_MD_provider(md);
33     OPENSSL_CTX *provctx = ossl_provider_library_context(prov);
34
35     nid = OBJ_obj2nid(key_oid);
36     if (nid == NID_undef)
37         return 0;
38     oid_sn = OBJ_nid2sn(nid);
39     if (oid_sn == NULL)
40         return 0;
41
42     kdf = EVP_KDF_fetch(provctx, OSSL_KDF_NAME_X942KDF, NULL);
43     if ((kctx = EVP_KDF_CTX_new(kdf)) == NULL)
44         goto err;
45     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
46                                             (char *)mdname, strlen(mdname) + 1);
47     *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
48                                              (unsigned char *)Z, Zlen);
49     if (ukm != NULL)
50         *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_UKM,
51                                                  (unsigned char *)ukm, ukmlen);
52     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
53                                             (char *)oid_sn, strlen(oid_sn) + 1);
54     *p = OSSL_PARAM_construct_end();
55     ret = EVP_KDF_CTX_set_params(kctx, params) > 0
56         && EVP_KDF_derive(kctx, out, outlen) > 0;
57 err:
58     EVP_KDF_CTX_free(kctx);
59     EVP_KDF_free(kdf);
60     return ret;
61 }
62 #endif /* OPENSSL_NO_CMS */