Add X9.42 KDF.
[openssl.git] / crypto / ec / ecdh_kdf.c
1 /*
2  * Copyright 2015-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 <string.h>
11 #include <openssl/ec.h>
12 #include <openssl/evp.h>
13 #include <openssl/kdf.h>
14 #include "ec_lcl.h"
15
16 /* Key derivation function from X9.63/SECG */
17 int ecdh_KDF_X9_63(unsigned char *out, size_t outlen,
18                    const unsigned char *Z, size_t Zlen,
19                    const unsigned char *sinfo, size_t sinfolen,
20                    const EVP_MD *md)
21 {
22     int ret;
23     EVP_KDF_CTX *kctx = NULL;
24
25     kctx = EVP_KDF_CTX_new(EVP_get_kdfbyname(SN_x963kdf));
26     ret =
27         kctx != NULL
28         && EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, md) > 0
29         && EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, Z, Zlen) > 0
30         && EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SHARED_INFO, sinfo, sinfolen) > 0
31         && EVP_KDF_derive(kctx, out, outlen) > 0;
32
33     EVP_KDF_CTX_free(kctx);
34     return ret;
35 }
36
37 /*-
38  * The old name for ecdh_KDF_X9_63
39  * Retained for ABI compatibility
40  */
41 #if !OPENSSL_API_3
42 int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
43                    const unsigned char *Z, size_t Zlen,
44                    const unsigned char *sinfo, size_t sinfolen,
45                    const EVP_MD *md)
46 {
47     return ecdh_KDF_X9_63(out, outlen, Z, Zlen, sinfo, sinfolen, md);
48 }
49 #endif