1 /* ====================================================================
2 * Copyright (c) 2016 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include <openssl/hmac.h>
52 #include <openssl/kdf.h>
53 #include <openssl/evp.h>
54 #include "internal/cryptlib.h"
55 #include "internal/evp_int.h"
57 #define HKDF_MAXBUF 1024
59 static unsigned char *HKDF(const EVP_MD *evp_md,
60 const unsigned char *salt, size_t salt_len,
61 const unsigned char *key, size_t key_len,
62 const unsigned char *info, size_t info_len,
63 unsigned char *okm, size_t okm_len);
65 static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
66 const unsigned char *salt, size_t salt_len,
67 const unsigned char *key, size_t key_len,
68 unsigned char *prk, size_t *prk_len);
70 static unsigned char *HKDF_Expand(const EVP_MD *evp_md,
71 const unsigned char *prk, size_t prk_len,
72 const unsigned char *info, size_t info_len,
73 unsigned char *okm, size_t okm_len);
81 unsigned char info[HKDF_MAXBUF];
85 static int pkey_hkdf_init(EVP_PKEY_CTX *ctx)
89 kctx = OPENSSL_zalloc(sizeof(*kctx));
98 static void pkey_hkdf_cleanup(EVP_PKEY_CTX *ctx)
100 HKDF_PKEY_CTX *kctx = ctx->data;
101 OPENSSL_clear_free(kctx->salt, kctx->salt_len);
102 OPENSSL_clear_free(kctx->key, kctx->key_len);
103 OPENSSL_cleanse(kctx->info, kctx->info_len);
107 static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
109 HKDF_PKEY_CTX *kctx = ctx->data;
112 case EVP_PKEY_CTRL_HKDF_MD:
119 case EVP_PKEY_CTRL_HKDF_SALT:
120 if (p1 == 0 || p2 == NULL)
126 if (kctx->salt != NULL)
127 OPENSSL_clear_free(kctx->salt, kctx->salt_len);
129 kctx->salt = OPENSSL_memdup(p2, p1);
130 if (kctx->salt == NULL)
136 case EVP_PKEY_CTRL_HKDF_KEY:
140 if (kctx->key != NULL)
141 OPENSSL_clear_free(kctx->key, kctx->key_len);
143 kctx->key = OPENSSL_memdup(p2, p1);
144 if (kctx->key == NULL)
150 case EVP_PKEY_CTRL_HKDF_INFO:
151 if (p1 == 0 || p2 == NULL)
154 if (p1 < 0 || p1 > (int)(HKDF_MAXBUF - kctx->info_len))
157 memcpy(kctx->info + kctx->info_len, p2, p1);
158 kctx->info_len += p1;
167 static int pkey_hkdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
170 if (strcmp(type, "md") == 0)
171 return EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_get_digestbyname(value));
173 if (strcmp(type, "salt") == 0)
174 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_SALT, value);
176 if (strcmp(type, "hexsalt") == 0)
177 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_SALT, value);
179 if (strcmp(type, "key") == 0)
180 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_KEY, value);
182 if (strcmp(type, "hexkey") == 0)
183 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_KEY, value);
185 if (strcmp(type, "info") == 0)
186 return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_INFO, value);
188 if (strcmp(type, "hexinfo") == 0)
189 return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_INFO, value);
194 static int pkey_hkdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
197 HKDF_PKEY_CTX *kctx = ctx->data;
199 if (kctx->md == NULL || kctx->key == NULL)
202 if (HKDF(kctx->md, kctx->salt, kctx->salt_len, kctx->key, kctx->key_len,
203 kctx->info, kctx->info_len, key, *keylen) == NULL)
211 const EVP_PKEY_METHOD hkdf_pkey_meth = {
241 static unsigned char *HKDF(const EVP_MD *evp_md,
242 const unsigned char *salt, size_t salt_len,
243 const unsigned char *key, size_t key_len,
244 const unsigned char *info, size_t info_len,
245 unsigned char *okm, size_t okm_len)
247 unsigned char prk[EVP_MAX_MD_SIZE];
250 if (!HKDF_Extract(evp_md, salt, salt_len, key, key_len, prk, &prk_len))
253 return HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
256 static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
257 const unsigned char *salt, size_t salt_len,
258 const unsigned char *key, size_t key_len,
259 unsigned char *prk, size_t *prk_len)
261 unsigned int tmp_len;
263 if (!HMAC(evp_md, salt, salt_len, key, key_len, prk, &tmp_len))
270 static unsigned char *HKDF_Expand(const EVP_MD *evp_md,
271 const unsigned char *prk, size_t prk_len,
272 const unsigned char *info, size_t info_len,
273 unsigned char *okm, size_t okm_len)
279 unsigned char prev[EVP_MAX_MD_SIZE];
281 size_t done_len = 0, dig_len = EVP_MD_size(evp_md);
283 size_t n = okm_len / dig_len;
284 if (okm_len % dig_len)
290 if ((hmac = HMAC_CTX_new()) == NULL)
293 if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
296 for (i = 1; i <= n; i++) {
298 const unsigned char ctr = i;
301 if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
304 if (!HMAC_Update(hmac, prev, dig_len))
308 if (!HMAC_Update(hmac, info, info_len))
311 if (!HMAC_Update(hmac, &ctr, 1))
314 if (!HMAC_Final(hmac, prev, NULL))
317 copy_len = (done_len + dig_len > okm_len) ?
321 memcpy(okm + done_len, prev, copy_len);
323 done_len += copy_len;