2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include <openssl/evp.h>
13 #include <openssl/err.h>
14 #include <openssl/buffer.h>
15 #include <openssl/kdf.h>
16 #include <openssl/core.h>
17 #include <openssl/core_names.h>
18 #include <openssl/params.h>
19 #include "internal/numbers.h"
20 #include "internal/evp_int.h"
27 * EVP_PKEY implementations collect bits of certain data
29 BUF_MEM *collected_seed;
30 BUF_MEM *collected_info;
33 static void pkey_kdf_free_collected(EVP_PKEY_KDF_CTX *pkctx)
35 BUF_MEM_free(pkctx->collected_seed);
36 pkctx->collected_seed = NULL;
37 BUF_MEM_free(pkctx->collected_info);
38 pkctx->collected_info = NULL;
41 static int pkey_kdf_init(EVP_PKEY_CTX *ctx)
43 EVP_PKEY_KDF_CTX *pkctx;
45 const char *kdf_name = OBJ_nid2sn(ctx->pmeth->pkey_id);
48 pkctx = OPENSSL_zalloc(sizeof(*pkctx));
52 kdf = EVP_KDF_fetch(NULL, kdf_name, NULL);
53 kctx = EVP_KDF_CTX_new(kdf);
65 static void pkey_kdf_cleanup(EVP_PKEY_CTX *ctx)
67 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
69 EVP_KDF_CTX_free(pkctx->kctx);
70 pkey_kdf_free_collected(pkctx);
74 static int collect(BUF_MEM **collector, void *data, size_t datalen)
78 if (*collector == NULL)
79 *collector = BUF_MEM_new();
80 if (*collector == NULL) {
81 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
85 i = (*collector)->length; /* BUF_MEM_grow() changes it! */
87 * The i + datalen check is to distinguish between BUF_MEM_grow()
88 * signaling an error and BUF_MEM_grow() simply returning the (zero)
91 if (!BUF_MEM_grow(*collector, i + datalen)
95 memcpy((*collector)->data + i, data, datalen);
99 static int pkey_kdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
101 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
102 EVP_KDF_CTX *kctx = pkctx->kctx;
103 enum { T_OCTET_STRING, T_UINT64, T_DIGEST, T_INT } cmd;
104 const char *name, *mdname;
105 BUF_MEM **collector = NULL;
106 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
109 case EVP_PKEY_CTRL_PASS:
110 cmd = T_OCTET_STRING;
111 name = OSSL_KDF_PARAM_PASSWORD;
113 case EVP_PKEY_CTRL_HKDF_SALT:
114 case EVP_PKEY_CTRL_SCRYPT_SALT:
115 cmd = T_OCTET_STRING;
116 name = OSSL_KDF_PARAM_SALT;
118 case EVP_PKEY_CTRL_TLS_MD:
119 case EVP_PKEY_CTRL_HKDF_MD:
121 name = OSSL_KDF_PARAM_DIGEST;
123 case EVP_PKEY_CTRL_TLS_SECRET:
124 cmd = T_OCTET_STRING;
125 name = OSSL_KDF_PARAM_SECRET;
127 * Perform the semantics described in
128 * EVP_PKEY_CTX_add1_tls1_prf_seed(3)
130 if (ctx->pmeth->pkey_id == NID_tls1_prf)
131 BUF_MEM_free(pkctx->collected_seed);
133 case EVP_PKEY_CTRL_TLS_SEED:
134 cmd = T_OCTET_STRING;
135 name = OSSL_KDF_PARAM_SEED;
136 collector = &pkctx->collected_seed;
138 case EVP_PKEY_CTRL_HKDF_KEY:
139 cmd = T_OCTET_STRING;
140 name = OSSL_KDF_PARAM_KEY;
142 case EVP_PKEY_CTRL_HKDF_INFO:
143 cmd = T_OCTET_STRING;
144 name = OSSL_KDF_PARAM_INFO;
145 collector = &pkctx->collected_info;
147 case EVP_PKEY_CTRL_HKDF_MODE:
149 name = OSSL_KDF_PARAM_MODE;
151 case EVP_PKEY_CTRL_SCRYPT_N:
153 name = OSSL_KDF_PARAM_SCRYPT_N;
155 case EVP_PKEY_CTRL_SCRYPT_R:
156 cmd = T_UINT64; /* Range checking occurs on the provider side */
157 name = OSSL_KDF_PARAM_SCRYPT_R;
159 case EVP_PKEY_CTRL_SCRYPT_P:
160 cmd = T_UINT64; /* Range checking occurs on the provider side */
161 name = OSSL_KDF_PARAM_SCRYPT_P;
163 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
165 name = OSSL_KDF_PARAM_SCRYPT_MAXMEM;
171 if (collector != NULL) {
174 return collect(collector, p2, p1);
176 OPENSSL_assert("You shouldn't be here");
185 OSSL_PARAM_construct_octet_string(name, (unsigned char *)p2,
190 mdname = EVP_MD_name((const EVP_MD *)p2);
191 params[0] = OSSL_PARAM_construct_utf8_string(name, (char *)mdname,
196 * These are special because the helper macros pass a pointer to the
197 * stack, so a local copy is required.
200 params[0] = OSSL_PARAM_construct_int(name, &p1);
204 params[0] = OSSL_PARAM_construct_uint64(name, (uint64_t *)p2);
208 return EVP_KDF_CTX_set_params(kctx, params);
211 static int pkey_kdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
214 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
215 EVP_KDF_CTX *kctx = pkctx->kctx;
216 const EVP_KDF *kdf = EVP_KDF_CTX_kdf(kctx);
217 BUF_MEM **collector = NULL;
218 const OSSL_PARAM *defs = EVP_KDF_CTX_settable_params(kdf);
219 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
222 /* Deal with ctrl name aliasing */
223 if (strcmp(type, "md") == 0)
224 type = OSSL_KDF_PARAM_DIGEST;
225 /* scrypt uses 'N', params uses 'n' */
226 if (strcmp(type, "N") == 0)
227 type = OSSL_KDF_PARAM_SCRYPT_N;
229 if (!OSSL_PARAM_allocate_from_text(¶ms[0], defs, type,
230 value, strlen(value)))
234 * We do the same special casing of seed and info here as in
237 if (strcmp(params[0].key, OSSL_KDF_PARAM_SEED) == 0)
238 collector = &pkctx->collected_seed;
239 else if (strcmp(params[0].key, OSSL_KDF_PARAM_INFO) == 0)
240 collector = &pkctx->collected_info;
242 if (collector != NULL)
243 ok = collect(collector, params[0].data, params[0].data_size);
245 ok = EVP_KDF_CTX_set_params(kctx, params);
246 OPENSSL_free(params[0].data);
250 static int pkey_kdf_derive_init(EVP_PKEY_CTX *ctx)
252 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
254 pkey_kdf_free_collected(pkctx);
255 if (pkctx->kctx != NULL)
256 EVP_KDF_reset(pkctx->kctx);
261 * For fixed-output algorithms the keylen parameter is an "out" parameter
262 * otherwise it is an "in" parameter.
264 static int pkey_kdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
267 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
268 EVP_KDF_CTX *kctx = pkctx->kctx;
269 size_t outlen = EVP_KDF_size(kctx);
272 if (pkctx->collected_seed != NULL) {
273 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
276 OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
277 pkctx->collected_seed->data,
278 pkctx->collected_seed->length);
280 r = EVP_KDF_CTX_set_params(kctx, params);
281 pkey_kdf_free_collected(pkctx);
285 if (pkctx->collected_info != NULL) {
286 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
289 OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
290 pkctx->collected_info->data,
291 pkctx->collected_info->length);
293 r = EVP_KDF_CTX_set_params(kctx, params);
294 pkey_kdf_free_collected(pkctx);
298 if (outlen == 0 || outlen == SIZE_MAX) {
299 /* Variable-output algorithm */
303 /* Fixed-output algorithm */
308 return EVP_KDF_derive(kctx, key, *keylen);
311 #ifndef OPENSSL_NO_SCRYPT
312 const EVP_PKEY_METHOD scrypt_pkey_meth = {
336 pkey_kdf_derive_init,
343 const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
367 pkey_kdf_derive_init,
373 const EVP_PKEY_METHOD hkdf_pkey_meth = {
397 pkey_kdf_derive_init,