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/kdf.h>
15 #include "internal/evp_int.h"
17 static int pkey_kdf_init(EVP_PKEY_CTX *ctx)
21 kctx = EVP_KDF_CTX_new_id(ctx->pmeth->pkey_id);
29 static void pkey_kdf_cleanup(EVP_PKEY_CTX *ctx)
31 EVP_KDF_CTX *kctx = ctx->data;
33 EVP_KDF_CTX_free(kctx);
36 static int pkey_kdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
38 EVP_KDF_CTX *kctx = ctx->data;
44 case EVP_PKEY_CTRL_PASS:
45 cmd = EVP_KDF_CTRL_SET_PASS;
47 case EVP_PKEY_CTRL_HKDF_SALT:
48 case EVP_PKEY_CTRL_SCRYPT_SALT:
49 cmd = EVP_KDF_CTRL_SET_SALT;
51 case EVP_PKEY_CTRL_TLS_MD:
52 case EVP_PKEY_CTRL_HKDF_MD:
53 cmd = EVP_KDF_CTRL_SET_MD;
55 case EVP_PKEY_CTRL_TLS_SECRET:
56 cmd = EVP_KDF_CTRL_SET_TLS_SECRET;
57 ret = EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_RESET_TLS_SEED);
61 case EVP_PKEY_CTRL_TLS_SEED:
62 cmd = EVP_KDF_CTRL_ADD_TLS_SEED;
64 case EVP_PKEY_CTRL_HKDF_KEY:
65 cmd = EVP_KDF_CTRL_SET_KEY;
67 case EVP_PKEY_CTRL_HKDF_INFO:
68 cmd = EVP_KDF_CTRL_ADD_HKDF_INFO;
70 case EVP_PKEY_CTRL_HKDF_MODE:
71 cmd = EVP_KDF_CTRL_SET_HKDF_MODE;
73 case EVP_PKEY_CTRL_SCRYPT_N:
74 cmd = EVP_KDF_CTRL_SET_SCRYPT_N;
76 case EVP_PKEY_CTRL_SCRYPT_R:
77 cmd = EVP_KDF_CTRL_SET_SCRYPT_R;
79 case EVP_PKEY_CTRL_SCRYPT_P:
80 cmd = EVP_KDF_CTRL_SET_SCRYPT_P;
82 case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
83 cmd = EVP_KDF_CTRL_SET_MAXMEM_BYTES;
90 case EVP_KDF_CTRL_SET_PASS:
91 case EVP_KDF_CTRL_SET_SALT:
92 case EVP_KDF_CTRL_SET_KEY:
93 case EVP_KDF_CTRL_SET_TLS_SECRET:
94 case EVP_KDF_CTRL_ADD_TLS_SEED:
95 case EVP_KDF_CTRL_ADD_HKDF_INFO:
96 return EVP_KDF_ctrl(kctx, cmd, (const unsigned char *)p2, (size_t)p1);
98 case EVP_KDF_CTRL_SET_MD:
99 return EVP_KDF_ctrl(kctx, cmd, (const EVP_MD *)p2);
101 case EVP_KDF_CTRL_SET_HKDF_MODE:
102 return EVP_KDF_ctrl(kctx, cmd, (int)p1);
104 case EVP_KDF_CTRL_SET_SCRYPT_R:
105 case EVP_KDF_CTRL_SET_SCRYPT_P:
106 u64_value = *(uint64_t *)p2;
107 if (u64_value > UINT32_MAX) {
108 EVPerr(EVP_F_PKEY_KDF_CTRL, EVP_R_PARAMETER_TOO_LARGE);
112 return EVP_KDF_ctrl(kctx, cmd, (uint32_t)u64_value);
114 case EVP_KDF_CTRL_SET_SCRYPT_N:
115 case EVP_KDF_CTRL_SET_MAXMEM_BYTES:
116 return EVP_KDF_ctrl(kctx, cmd, *(uint64_t *)p2);
123 static int pkey_kdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
126 EVP_KDF_CTX *kctx = ctx->data;
128 if (strcmp(type, "md") == 0)
129 return EVP_KDF_ctrl_str(kctx, "digest", value);
130 return EVP_KDF_ctrl_str(kctx, type, value);
133 static int pkey_kdf_derive_init(EVP_PKEY_CTX *ctx)
135 EVP_KDF_CTX *kctx = ctx->data;
142 * For fixed-output algorithms the keylen parameter is an "out" parameter
143 * otherwise it is an "in" parameter.
145 static int pkey_kdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
148 EVP_KDF_CTX *kctx = ctx->data;
149 size_t outlen = EVP_KDF_size(kctx);
151 if (outlen == 0 || outlen == SIZE_MAX) {
152 /* Variable-output algorithm */
156 /* Fixed-output algorithm */
161 return EVP_KDF_derive(kctx, key, *keylen);
164 #ifndef OPENSSL_NO_SCRYPT
165 const EVP_PKEY_METHOD scrypt_pkey_meth = {
189 pkey_kdf_derive_init,
196 const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
220 pkey_kdf_derive_init,
226 const EVP_PKEY_METHOD hkdf_pkey_meth = {
250 pkey_kdf_derive_init,