]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_kdf.c
Update copyright year
[thirdparty/openssl.git] / crypto / dh / dh_kdf.c
CommitLineData
dc1ce3bc 1/*
33388b44 2 * Copyright 2013-2020 The OpenSSL Project Authors. All Rights Reserved.
dc1ce3bc 3 *
e38873f5 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
dc1ce3bc
DSH
8 */
9
ada66e78
P
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
bef7a815 16#include "e_os.h"
e968561d
DB
17
18#ifndef OPENSSL_NO_CMS
1aec7716 19# include <string.h>
7707526b 20# include <openssl/core_names.h>
1aec7716
SL
21# include <openssl/dh.h>
22# include <openssl/evp.h>
23# include <openssl/asn1.h>
24# include <openssl/kdf.h>
7707526b 25# include <internal/provider.h>
dc1ce3bc 26
0f113f3e
MC
27int 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{
1aec7716
SL
32 int ret = 0, nid;
33 EVP_KDF_CTX *kctx = NULL;
7707526b 34 EVP_KDF *kdf = NULL;
1aec7716 35 const char *oid_sn;
7707526b
P
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);
1aec7716
SL
40
41 nid = OBJ_obj2nid(key_oid);
42 if (nid == NID_undef)
0f113f3e 43 return 0;
1aec7716
SL
44 oid_sn = OBJ_nid2sn(nid);
45 if (oid_sn == NULL)
6e59a892 46 return 0;
1aec7716 47
64115f05 48 kdf = EVP_KDF_fetch(provctx, OSSL_KDF_NAME_X942KDF, NULL);
7707526b 49 if ((kctx = EVP_KDF_CTX_new(kdf)) == NULL)
0f113f3e 50 goto err;
7707526b 51 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
8b6ffd40 52 (char *)mdname, 0);
7707526b
P
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,
8b6ffd40 59 (char *)oid_sn, 0);
7707526b
P
60 *p = OSSL_PARAM_construct_end();
61 ret = EVP_KDF_CTX_set_params(kctx, params) > 0
1aec7716
SL
62 && EVP_KDF_derive(kctx, out, outlen) > 0;
63err:
64 EVP_KDF_CTX_free(kctx);
7707526b 65 EVP_KDF_free(kdf);
1aec7716 66 return ret;
0f113f3e 67}
1aec7716 68#endif /* OPENSSL_NO_CMS */