]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecdh_kdf.c
Deprecate ECDH_KDF_X9_62()
[thirdparty/openssl.git] / crypto / ec / ecdh_kdf.c
CommitLineData
25af7a5d 1/*
ffd89124 2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
25af7a5d 3 *
4f22f405
RS
4 * Licensed under the OpenSSL license (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
25af7a5d
DSH
8 */
9
25af7a5d 10#include <string.h>
768c53e1 11#include <openssl/ec.h>
25af7a5d 12#include <openssl/evp.h>
ffd89124 13#include "ec_lcl.h"
25af7a5d 14
ffd89124 15/* Key derivation function from X9.63/SECG */
579a7590 16/* Way more than we will ever need */
0f113f3e 17#define ECDH_KDF_MAX (1 << 30)
25af7a5d 18
ffd89124 19int ecdh_KDF_X9_63(unsigned char *out, size_t outlen,
0f113f3e
MC
20 const unsigned char *Z, size_t Zlen,
21 const unsigned char *sinfo, size_t sinfolen,
22 const EVP_MD *md)
23{
6e59a892 24 EVP_MD_CTX *mctx = NULL;
0f113f3e
MC
25 int rv = 0;
26 unsigned int i;
27 size_t mdlen;
28 unsigned char ctr[4];
29 if (sinfolen > ECDH_KDF_MAX || outlen > ECDH_KDF_MAX
30 || Zlen > ECDH_KDF_MAX)
31 return 0;
bfb0641f 32 mctx = EVP_MD_CTX_new();
6e59a892
RL
33 if (mctx == NULL)
34 return 0;
0f113f3e 35 mdlen = EVP_MD_size(md);
0f113f3e
MC
36 for (i = 1;; i++) {
37 unsigned char mtmp[EVP_MAX_MD_SIZE];
d166ed8c
DSH
38 if (!EVP_DigestInit_ex(mctx, md, NULL))
39 goto err;
0f113f3e
MC
40 ctr[3] = i & 0xFF;
41 ctr[2] = (i >> 8) & 0xFF;
42 ctr[1] = (i >> 16) & 0xFF;
43 ctr[0] = (i >> 24) & 0xFF;
6e59a892 44 if (!EVP_DigestUpdate(mctx, Z, Zlen))
0f113f3e 45 goto err;
6e59a892 46 if (!EVP_DigestUpdate(mctx, ctr, sizeof(ctr)))
0f113f3e 47 goto err;
6e59a892 48 if (!EVP_DigestUpdate(mctx, sinfo, sinfolen))
0f113f3e
MC
49 goto err;
50 if (outlen >= mdlen) {
6e59a892 51 if (!EVP_DigestFinal(mctx, out, NULL))
0f113f3e
MC
52 goto err;
53 outlen -= mdlen;
54 if (outlen == 0)
55 break;
56 out += mdlen;
57 } else {
6e59a892 58 if (!EVP_DigestFinal(mctx, mtmp, NULL))
0f113f3e
MC
59 goto err;
60 memcpy(out, mtmp, outlen);
61 OPENSSL_cleanse(mtmp, mdlen);
62 break;
63 }
64 }
65 rv = 1;
66 err:
bfb0641f 67 EVP_MD_CTX_free(mctx);
0f113f3e
MC
68 return rv;
69}
ffd89124
AS
70
71/*-
72 * The old name for ecdh_KDF_X9_63
73 * Retained for ABI compatibility
74 */
9453b196 75#if OPENSSL_API_COMPAT < 0x10200000L
ffd89124
AS
76int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
77 const unsigned char *Z, size_t Zlen,
78 const unsigned char *sinfo, size_t sinfolen,
79 const EVP_MD *md)
80{
81 return ecdh_KDF_X9_63(out, outlen, Z, Zlen, sinfo, sinfolen, md);
82}
9453b196 83#endif