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);
132 pkctx->collected_seed = NULL;
135 case EVP_PKEY_CTRL_TLS_SEED:
136 cmd = T_OCTET_STRING;
137 name = OSSL_KDF_PARAM_SEED;
138 collector = &pkctx->collected_seed;
140 case EVP_PKEY_CTRL_HKDF_KEY:
141 cmd = T_OCTET_STRING;
142 name = OSSL_KDF_PARAM_KEY;
144 case EVP_PKEY_CTRL_HKDF_INFO:
145 cmd = T_OCTET_STRING;
146 name = OSSL_KDF_PARAM_INFO;
147 collector = &pkctx->collected_info;
149 case EVP_PKEY_CTRL_HKDF_MODE:
151 name = OSSL_KDF_PARAM_MODE;
153 case EVP_PKEY_CTRL_SCRYPT_N:
155 name = OSSL_KDF_PARAM_SCRYPT_N;
157 case EVP_PKEY_CTRL_SCRYPT_R:
158 cmd = T_UINT64; /* Range checking occurs on the provider side */
159 name = OSSL_KDF_PARAM_SCRYPT_R;
161 case EVP_PKEY_CTRL_SCRYPT_P:
162 cmd = T_UINT64; /* Range checking occurs on the provider side */
163 name = OSSL_KDF_PARAM_SCRYPT_P;
165 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
167 name = OSSL_KDF_PARAM_SCRYPT_MAXMEM;
173 if (collector != NULL) {
176 return collect(collector, p2, p1);
178 OPENSSL_assert("You shouldn't be here");
187 OSSL_PARAM_construct_octet_string(name, (unsigned char *)p2,
192 mdname = EVP_MD_name((const EVP_MD *)p2);
193 params[0] = OSSL_PARAM_construct_utf8_string(name, (char *)mdname,
198 * These are special because the helper macros pass a pointer to the
199 * stack, so a local copy is required.
202 params[0] = OSSL_PARAM_construct_int(name, &p1);
206 params[0] = OSSL_PARAM_construct_uint64(name, (uint64_t *)p2);
210 return EVP_KDF_CTX_set_params(kctx, params);
213 static int pkey_kdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
216 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
217 EVP_KDF_CTX *kctx = pkctx->kctx;
218 const EVP_KDF *kdf = EVP_KDF_CTX_kdf(kctx);
219 BUF_MEM **collector = NULL;
220 const OSSL_PARAM *defs = EVP_KDF_CTX_settable_params(kdf);
221 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
224 /* Deal with ctrl name aliasing */
225 if (strcmp(type, "md") == 0)
226 type = OSSL_KDF_PARAM_DIGEST;
227 /* scrypt uses 'N', params uses 'n' */
228 if (strcmp(type, "N") == 0)
229 type = OSSL_KDF_PARAM_SCRYPT_N;
231 if (!OSSL_PARAM_allocate_from_text(¶ms[0], defs, type,
232 value, strlen(value)))
236 * We do the same special casing of seed and info here as in
239 if (strcmp(params[0].key, OSSL_KDF_PARAM_SEED) == 0)
240 collector = &pkctx->collected_seed;
241 else if (strcmp(params[0].key, OSSL_KDF_PARAM_INFO) == 0)
242 collector = &pkctx->collected_info;
244 if (collector != NULL)
245 ok = collect(collector, params[0].data, params[0].data_size);
247 ok = EVP_KDF_CTX_set_params(kctx, params);
248 OPENSSL_free(params[0].data);
252 static int pkey_kdf_derive_init(EVP_PKEY_CTX *ctx)
254 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
256 pkey_kdf_free_collected(pkctx);
257 if (pkctx->kctx != NULL)
258 EVP_KDF_reset(pkctx->kctx);
263 * For fixed-output algorithms the keylen parameter is an "out" parameter
264 * otherwise it is an "in" parameter.
266 static int pkey_kdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
269 EVP_PKEY_KDF_CTX *pkctx = ctx->data;
270 EVP_KDF_CTX *kctx = pkctx->kctx;
271 size_t outlen = EVP_KDF_size(kctx);
274 if (pkctx->collected_seed != NULL) {
275 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
278 OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
279 pkctx->collected_seed->data,
280 pkctx->collected_seed->length);
282 r = EVP_KDF_CTX_set_params(kctx, params);
283 pkey_kdf_free_collected(pkctx);
287 if (pkctx->collected_info != NULL) {
288 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
291 OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
292 pkctx->collected_info->data,
293 pkctx->collected_info->length);
295 r = EVP_KDF_CTX_set_params(kctx, params);
296 pkey_kdf_free_collected(pkctx);
300 if (outlen == 0 || outlen == SIZE_MAX) {
301 /* Variable-output algorithm */
305 /* Fixed-output algorithm */
310 return EVP_KDF_derive(kctx, key, *keylen);
313 #ifndef OPENSSL_NO_SCRYPT
314 const EVP_PKEY_METHOD scrypt_pkey_meth = {
338 pkey_kdf_derive_init,
345 const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
369 pkey_kdf_derive_init,
375 const EVP_PKEY_METHOD hkdf_pkey_meth = {
399 pkey_kdf_derive_init,