2 * Copyright 2018-2018 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
15 #include "internal/cryptlib.h"
16 #include "internal/evp_int.h"
17 #include "kdf_local.h"
19 /* See RFC 4253, Section 7.2 */
21 static void kdf_sshkdf_reset(EVP_KDF_IMPL *impl);
22 static int SSHKDF(const EVP_MD *evp_md,
23 const unsigned char *key, size_t key_len,
24 const unsigned char *xcghash, size_t xcghash_len,
25 const unsigned char *session_id, size_t session_id_len,
26 char type, unsigned char *okey, size_t okey_len);
28 struct evp_kdf_impl_st {
30 unsigned char *key; /* K */
32 unsigned char *xcghash; /* H */
35 unsigned char *session_id;
36 size_t session_id_len;
39 static EVP_KDF_IMPL *kdf_sshkdf_new(void)
43 if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
44 KDFerr(KDF_F_KDF_SSHKDF_NEW, ERR_R_MALLOC_FAILURE);
48 static void kdf_sshkdf_free(EVP_KDF_IMPL *impl)
50 kdf_sshkdf_reset(impl);
54 static void kdf_sshkdf_reset(EVP_KDF_IMPL *impl)
56 OPENSSL_clear_free(impl->key, impl->key_len);
57 OPENSSL_clear_free(impl->xcghash, impl->xcghash_len);
58 OPENSSL_clear_free(impl->session_id, impl->session_id_len);
59 memset(impl, 0, sizeof(*impl));
62 static int kdf_sshkdf_parse_buffer_arg(unsigned char **dst, size_t *dst_len,
65 const unsigned char *p;
68 p = va_arg(args, const unsigned char *);
69 len = va_arg(args, size_t);
70 OPENSSL_clear_free(*dst, *dst_len);
71 *dst = OPENSSL_memdup(p, len);
79 static int kdf_sshkdf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
84 case EVP_KDF_CTRL_SET_MD:
85 impl->md = va_arg(args, const EVP_MD *);
91 case EVP_KDF_CTRL_SET_KEY:
92 return kdf_sshkdf_parse_buffer_arg(&impl->key,
93 &impl->key_len, args);
95 case EVP_KDF_CTRL_SET_SSHKDF_XCGHASH:
96 return kdf_sshkdf_parse_buffer_arg(&impl->xcghash,
97 &impl->xcghash_len, args);
99 case EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID:
100 return kdf_sshkdf_parse_buffer_arg(&impl->session_id,
101 &impl->session_id_len, args);
103 case EVP_KDF_CTRL_SET_SSHKDF_TYPE:
104 t = va_arg(args, int);
105 if (t < 65 || t > 70) {
106 KDFerr(KDF_F_KDF_SSHKDF_CTRL, KDF_R_VALUE_ERROR);
110 impl->type = (char)t;
119 static int kdf_sshkdf_ctrl_str(EVP_KDF_IMPL *impl, const char *type,
123 KDFerr(KDF_F_KDF_SSHKDF_CTRL_STR, KDF_R_VALUE_MISSING);
127 if (strcmp(type, "md") == 0)
128 return kdf_md2ctrl(impl, kdf_sshkdf_ctrl, EVP_KDF_CTRL_SET_MD, value);
130 if (strcmp(type, "key") == 0)
131 return kdf_str2ctrl(impl, kdf_sshkdf_ctrl,
132 EVP_KDF_CTRL_SET_KEY, value);
134 if (strcmp(type, "hexkey") == 0)
135 return kdf_hex2ctrl(impl, kdf_sshkdf_ctrl,
136 EVP_KDF_CTRL_SET_KEY, value);
138 if (strcmp(type, "xcghash") == 0)
139 return kdf_str2ctrl(impl, kdf_sshkdf_ctrl,
140 EVP_KDF_CTRL_SET_SSHKDF_XCGHASH, value);
142 if (strcmp(type, "hexxcghash") == 0)
143 return kdf_hex2ctrl(impl, kdf_sshkdf_ctrl,
144 EVP_KDF_CTRL_SET_SSHKDF_XCGHASH, value);
146 if (strcmp(type, "session_id") == 0)
147 return kdf_str2ctrl(impl, kdf_sshkdf_ctrl,
148 EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID, value);
150 if (strcmp(type, "hexsession_id") == 0)
151 return kdf_hex2ctrl(impl, kdf_sshkdf_ctrl,
152 EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID, value);
154 if (strcmp(type, "type") == 0) {
155 if (strlen(value) != 1) {
156 KDFerr(KDF_F_KDF_SSHKDF_CTRL_STR, KDF_R_VALUE_ERROR);
160 return call_ctrl(kdf_sshkdf_ctrl, impl, EVP_KDF_CTRL_SET_SSHKDF_TYPE,
164 KDFerr(KDF_F_KDF_SSHKDF_CTRL_STR, KDF_R_UNKNOWN_PARAMETER_TYPE);
168 static size_t kdf_sshkdf_size(EVP_KDF_IMPL *impl)
173 static int kdf_sshkdf_derive(EVP_KDF_IMPL *impl, unsigned char *key,
176 if (impl->md == NULL) {
177 KDFerr(KDF_F_KDF_SSHKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
180 if (impl->key == NULL) {
181 KDFerr(KDF_F_KDF_SSHKDF_DERIVE, KDF_R_MISSING_KEY);
184 if (impl->xcghash == NULL) {
185 KDFerr(KDF_F_KDF_SSHKDF_DERIVE, KDF_R_MISSING_XCGHASH);
188 if (impl->session_id == NULL) {
189 KDFerr(KDF_F_KDF_SSHKDF_DERIVE, KDF_R_MISSING_SESSION_ID);
192 if (impl->type == 0) {
193 KDFerr(KDF_F_KDF_SSHKDF_DERIVE, KDF_R_MISSING_TYPE);
196 return SSHKDF(impl->md, impl->key, impl->key_len,
197 impl->xcghash, impl->xcghash_len,
198 impl->session_id, impl->session_id_len,
199 impl->type, key, keylen);
202 const EVP_KDF_METHOD sshkdf_kdf_meth = {
213 static int SSHKDF(const EVP_MD *evp_md,
214 const unsigned char *key, size_t key_len,
215 const unsigned char *xcghash, size_t xcghash_len,
216 const unsigned char *session_id, size_t session_id_len,
217 char type, unsigned char *okey, size_t okey_len)
219 EVP_MD_CTX *md = NULL;
220 unsigned char digest[EVP_MAX_MD_SIZE];
221 unsigned int dsize = 0;
225 md = EVP_MD_CTX_new();
229 if (!EVP_DigestInit_ex(md, evp_md, NULL))
232 if (!EVP_DigestUpdate(md, key, key_len))
235 if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
238 if (!EVP_DigestUpdate(md, &type, 1))
241 if (!EVP_DigestUpdate(md, session_id, session_id_len))
244 if (!EVP_DigestFinal_ex(md, digest, &dsize))
247 if (okey_len < dsize) {
248 memcpy(okey, digest, okey_len);
253 memcpy(okey, digest, dsize);
255 for (cursize = dsize; cursize < okey_len; cursize += dsize) {
257 if (!EVP_DigestInit_ex(md, evp_md, NULL))
260 if (!EVP_DigestUpdate(md, key, key_len))
263 if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
266 if (!EVP_DigestUpdate(md, okey, cursize))
269 if (!EVP_DigestFinal_ex(md, digest, &dsize))
272 if (okey_len < cursize + dsize) {
273 memcpy(okey + cursize, digest, okey_len - cursize);
278 memcpy(okey + cursize, digest, dsize);
285 OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE);