]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/dh/dh_kdf.c
e054aaeff96ce2f2b96f421af0f8510ddf02350b
[thirdparty/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 /*
11 * DH low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include "e_os.h"
17
18 #ifndef OPENSSL_NO_CMS
19 # include <string.h>
20 # include <openssl/core_names.h>
21 # include <openssl/dh.h>
22 # include <openssl/evp.h>
23 # include <openssl/asn1.h>
24 # include <openssl/kdf.h>
25 # include <internal/provider.h>
26
27 int DH_KDF_X9_42(unsigned char *out, size_t outlen,
28 const unsigned char *Z, size_t Zlen,
29 ASN1_OBJECT *key_oid,
30 const unsigned char *ukm, size_t ukmlen, const EVP_MD *md)
31 {
32 int ret = 0, nid;
33 EVP_KDF_CTX *kctx = NULL;
34 EVP_KDF *kdf = NULL;
35 const char *oid_sn;
36 OSSL_PARAM params[5], *p = params;
37 const char *mdname = EVP_MD_name(md);
38 const OSSL_PROVIDER *prov = EVP_MD_provider(md);
39 OPENSSL_CTX *provctx = ossl_provider_library_context(prov);
40
41 nid = OBJ_obj2nid(key_oid);
42 if (nid == NID_undef)
43 return 0;
44 oid_sn = OBJ_nid2sn(nid);
45 if (oid_sn == NULL)
46 return 0;
47
48 kdf = EVP_KDF_fetch(provctx, OSSL_KDF_NAME_X942KDF, NULL);
49 if ((kctx = EVP_KDF_CTX_new(kdf)) == NULL)
50 goto err;
51 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
52 (char *)mdname, 0);
53 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
54 (unsigned char *)Z, Zlen);
55 if (ukm != NULL)
56 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_UKM,
57 (unsigned char *)ukm, ukmlen);
58 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
59 (char *)oid_sn, 0);
60 *p = OSSL_PARAM_construct_end();
61 ret = EVP_KDF_CTX_set_params(kctx, params) > 0
62 && EVP_KDF_derive(kctx, out, outlen) > 0;
63 err:
64 EVP_KDF_CTX_free(kctx);
65 EVP_KDF_free(kdf);
66 return ret;
67 }
68 #endif /* OPENSSL_NO_CMS */