]>
Commit | Line | Data |
---|---|---|
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" |
116d2510 SL |
17 | #include "e_os.h" |
18 | #include <string.h> | |
19 | #include <openssl/core_names.h> | |
20 | #include <openssl/dh.h> | |
21 | #include <openssl/evp.h> | |
22 | #include <openssl/asn1.h> | |
23 | #include <openssl/kdf.h> | |
24 | #include <internal/provider.h> | |
25 | #include <crypto/dh.h> | |
e968561d | 26 | |
116d2510 | 27 | /* Key derivation function from X9.63/SECG */ |
19dbb742 SL |
28 | int ossl_dh_kdf_X9_42_asn1(unsigned char *out, size_t outlen, |
29 | const unsigned char *Z, size_t Zlen, | |
30 | const char *cek_alg, | |
31 | const unsigned char *ukm, size_t ukmlen, | |
32 | const EVP_MD *md, | |
33 | OSSL_LIB_CTX *libctx, const char *propq) | |
0f113f3e | 34 | { |
116d2510 | 35 | int ret = 0; |
1aec7716 | 36 | EVP_KDF_CTX *kctx = NULL; |
7707526b | 37 | EVP_KDF *kdf = NULL; |
7707526b P |
38 | OSSL_PARAM params[5], *p = params; |
39 | const char *mdname = EVP_MD_name(md); | |
1aec7716 | 40 | |
89cccbea | 41 | kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_X942KDF_ASN1, propq); |
116d2510 SL |
42 | kctx = EVP_KDF_CTX_new(kdf); |
43 | if (kctx == NULL) | |
0f113f3e | 44 | goto err; |
116d2510 | 45 | |
7707526b | 46 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, |
8b6ffd40 | 47 | (char *)mdname, 0); |
7707526b P |
48 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, |
49 | (unsigned char *)Z, Zlen); | |
50 | if (ukm != NULL) | |
51 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_UKM, | |
52 | (unsigned char *)ukm, ukmlen); | |
53 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG, | |
116d2510 | 54 | (char *)cek_alg, 0); |
7707526b | 55 | *p = OSSL_PARAM_construct_end(); |
36fae6e8 | 56 | ret = EVP_KDF_derive(kctx, out, outlen, params) > 0; |
1aec7716 | 57 | err: |
660c5344 | 58 | EVP_KDF_CTX_free(kctx); |
7707526b | 59 | EVP_KDF_free(kdf); |
1aec7716 | 60 | return ret; |
0f113f3e | 61 | } |
116d2510 | 62 | |
7fe32ef6 | 63 | #if !defined(FIPS_MODULE) |
116d2510 SL |
64 | int DH_KDF_X9_42(unsigned char *out, size_t outlen, |
65 | const unsigned char *Z, size_t Zlen, | |
66 | ASN1_OBJECT *key_oid, | |
67 | const unsigned char *ukm, size_t ukmlen, const EVP_MD *md) | |
68 | { | |
69 | int nid; | |
70 | const char *key_alg = NULL; | |
71 | const OSSL_PROVIDER *prov = EVP_MD_provider(md); | |
a829b735 | 72 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
116d2510 SL |
73 | |
74 | nid = OBJ_obj2nid(key_oid); | |
75 | if (nid == NID_undef) | |
76 | return 0; | |
77 | key_alg = OBJ_nid2sn(nid); | |
78 | if (key_alg == NULL) | |
79 | return 0; | |
80 | ||
19dbb742 SL |
81 | return ossl_dh_kdf_X9_42_asn1(out, outlen, Z, Zlen, key_alg, |
82 | ukm, ukmlen, md, libctx, NULL); | |
116d2510 | 83 | } |
7fe32ef6 | 84 | #endif /* !defined(FIPS_MODULE) */ |