2016-01-19 21:57:19 +08:00
|
|
|
/*
|
2018-03-20 21:00:17 +08:00
|
|
|
* Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2016-01-19 21:57:19 +08:00
|
|
|
*
|
2018-12-06 20:41:54 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:26 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2016-01-19 21:57:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2018-06-22 05:16:18 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2016-01-19 21:57:19 +08:00
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
#include <openssl/evp.h>
|
2018-06-22 05:16:18 +08:00
|
|
|
#include <openssl/kdf.h>
|
2016-01-19 21:57:19 +08:00
|
|
|
#include "internal/evp_int.h"
|
2018-06-22 05:16:18 +08:00
|
|
|
#include "kdf_local.h"
|
2016-01-19 21:57:19 +08:00
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl);
|
2016-01-19 21:57:19 +08:00
|
|
|
static int tls1_prf_alg(const EVP_MD *md,
|
|
|
|
const unsigned char *sec, size_t slen,
|
|
|
|
const unsigned char *seed, size_t seed_len,
|
|
|
|
unsigned char *out, size_t olen);
|
|
|
|
|
|
|
|
#define TLS1_PRF_MAXBUF 1024
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
/* TLS KDF kdf context structure */
|
2016-01-19 21:57:19 +08:00
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
struct evp_kdf_impl_st {
|
2016-01-19 21:57:19 +08:00
|
|
|
/* Digest to use for PRF */
|
|
|
|
const EVP_MD *md;
|
|
|
|
/* Secret value to use for PRF */
|
|
|
|
unsigned char *sec;
|
|
|
|
size_t seclen;
|
|
|
|
/* Buffer of concatenated seed data */
|
|
|
|
unsigned char seed[TLS1_PRF_MAXBUF];
|
|
|
|
size_t seedlen;
|
2018-06-22 05:16:18 +08:00
|
|
|
};
|
2016-01-19 21:57:19 +08:00
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
static EVP_KDF_IMPL *kdf_tls1_prf_new(void)
|
2016-01-19 21:57:19 +08:00
|
|
|
{
|
2018-06-22 05:16:18 +08:00
|
|
|
EVP_KDF_IMPL *impl;
|
2016-01-19 21:57:19 +08:00
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
|
|
|
|
KDFerr(KDF_F_KDF_TLS1_PRF_NEW, ERR_R_MALLOC_FAILURE);
|
|
|
|
return impl;
|
|
|
|
}
|
2016-01-19 21:57:19 +08:00
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
static void kdf_tls1_prf_free(EVP_KDF_IMPL *impl)
|
|
|
|
{
|
|
|
|
kdf_tls1_prf_reset(impl);
|
|
|
|
OPENSSL_free(impl);
|
2016-01-19 21:57:19 +08:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
static void kdf_tls1_prf_reset(EVP_KDF_IMPL *impl)
|
2016-01-19 21:57:19 +08:00
|
|
|
{
|
2018-06-22 05:16:18 +08:00
|
|
|
OPENSSL_clear_free(impl->sec, impl->seclen);
|
|
|
|
OPENSSL_cleanse(impl->seed, impl->seedlen);
|
|
|
|
memset(impl, 0, sizeof(*impl));
|
2016-01-19 21:57:19 +08:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
static int kdf_tls1_prf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
|
2016-01-19 21:57:19 +08:00
|
|
|
{
|
2018-06-22 05:16:18 +08:00
|
|
|
const unsigned char *p;
|
|
|
|
size_t len;
|
|
|
|
const EVP_MD *md;
|
2016-01-19 21:57:19 +08:00
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
switch (cmd) {
|
|
|
|
case EVP_KDF_CTRL_SET_MD:
|
|
|
|
md = va_arg(args, const EVP_MD *);
|
|
|
|
if (md == NULL)
|
2016-01-19 21:57:19 +08:00
|
|
|
return 0;
|
2018-06-22 05:16:18 +08:00
|
|
|
|
|
|
|
impl->md = md;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case EVP_KDF_CTRL_SET_TLS_SECRET:
|
|
|
|
p = va_arg(args, const unsigned char *);
|
|
|
|
len = va_arg(args, size_t);
|
|
|
|
OPENSSL_clear_free(impl->sec, impl->seclen);
|
|
|
|
impl->sec = OPENSSL_memdup(p, len);
|
|
|
|
if (impl->sec == NULL)
|
2016-01-19 21:57:19 +08:00
|
|
|
return 0;
|
2018-06-22 05:16:18 +08:00
|
|
|
|
|
|
|
impl->seclen = len;
|
2016-01-19 21:57:19 +08:00
|
|
|
return 1;
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
case EVP_KDF_CTRL_RESET_TLS_SEED:
|
|
|
|
OPENSSL_cleanse(impl->seed, impl->seedlen);
|
|
|
|
impl->seedlen = 0;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case EVP_KDF_CTRL_ADD_TLS_SEED:
|
|
|
|
p = va_arg(args, const unsigned char *);
|
|
|
|
len = va_arg(args, size_t);
|
|
|
|
if (len == 0 || p == NULL)
|
2016-01-19 21:57:19 +08:00
|
|
|
return 1;
|
2018-06-22 05:16:18 +08:00
|
|
|
|
|
|
|
if (len > (TLS1_PRF_MAXBUF - impl->seedlen))
|
2016-01-19 21:57:19 +08:00
|
|
|
return 0;
|
2018-06-22 05:16:18 +08:00
|
|
|
|
|
|
|
memcpy(impl->seed + impl->seedlen, p, len);
|
|
|
|
impl->seedlen += len;
|
2016-01-19 21:57:19 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
static int kdf_tls1_prf_ctrl_str(EVP_KDF_IMPL *impl,
|
|
|
|
const char *type, const char *value)
|
2016-03-01 22:58:33 +08:00
|
|
|
{
|
2016-03-02 12:11:02 +08:00
|
|
|
if (value == NULL) {
|
2018-06-22 05:16:18 +08:00
|
|
|
KDFerr(KDF_F_KDF_TLS1_PRF_CTRL_STR, KDF_R_VALUE_MISSING);
|
2016-03-01 22:58:33 +08:00
|
|
|
return 0;
|
2016-03-02 12:11:02 +08:00
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
if (strcmp(type, "digest") == 0)
|
|
|
|
return kdf_md2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_SET_MD, value);
|
2016-03-01 22:58:33 +08:00
|
|
|
|
|
|
|
if (strcmp(type, "secret") == 0)
|
2018-06-22 05:16:18 +08:00
|
|
|
return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl,
|
|
|
|
EVP_KDF_CTRL_SET_TLS_SECRET, value);
|
|
|
|
|
2016-03-01 22:58:33 +08:00
|
|
|
if (strcmp(type, "hexsecret") == 0)
|
2018-06-22 05:16:18 +08:00
|
|
|
return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl,
|
|
|
|
EVP_KDF_CTRL_SET_TLS_SECRET, value);
|
|
|
|
|
2016-03-01 22:58:33 +08:00
|
|
|
if (strcmp(type, "seed") == 0)
|
2018-06-22 05:16:18 +08:00
|
|
|
return kdf_str2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
|
|
|
|
value);
|
|
|
|
|
2016-03-01 22:58:33 +08:00
|
|
|
if (strcmp(type, "hexseed") == 0)
|
2018-06-22 05:16:18 +08:00
|
|
|
return kdf_hex2ctrl(impl, kdf_tls1_prf_ctrl, EVP_KDF_CTRL_ADD_TLS_SEED,
|
|
|
|
value);
|
2017-07-22 23:43:05 +08:00
|
|
|
|
2016-03-01 22:58:33 +08:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:16:18 +08:00
|
|
|
static int kdf_tls1_prf_derive(EVP_KDF_IMPL *impl, unsigned char *key,
|
|
|
|
size_t keylen)
|
2016-01-19 21:57:19 +08:00
|
|
|
{
|
2018-06-22 05:16:18 +08:00
|
|
|
if (impl->md == NULL) {
|
|
|
|
KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
|
2017-07-27 03:49:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
if (impl->sec == NULL) {
|
|
|
|
KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SECRET);
|
2017-08-02 00:32:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
if (impl->seedlen == 0) {
|
|
|
|
KDFerr(KDF_F_KDF_TLS1_PRF_DERIVE, KDF_R_MISSING_SEED);
|
2016-01-19 21:57:19 +08:00
|
|
|
return 0;
|
2016-03-02 12:11:02 +08:00
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
return tls1_prf_alg(impl->md, impl->sec, impl->seclen,
|
|
|
|
impl->seed, impl->seedlen,
|
|
|
|
key, keylen);
|
2016-01-19 21:57:19 +08:00
|
|
|
}
|
|
|
|
|
2019-04-22 15:18:56 +08:00
|
|
|
const EVP_KDF tls1_prf_kdf_meth = {
|
2018-06-22 05:16:18 +08:00
|
|
|
EVP_KDF_TLS1_PRF,
|
|
|
|
kdf_tls1_prf_new,
|
|
|
|
kdf_tls1_prf_free,
|
|
|
|
kdf_tls1_prf_reset,
|
|
|
|
kdf_tls1_prf_ctrl,
|
|
|
|
kdf_tls1_prf_ctrl_str,
|
|
|
|
NULL,
|
|
|
|
kdf_tls1_prf_derive
|
2016-01-19 21:57:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int tls1_prf_P_hash(const EVP_MD *md,
|
|
|
|
const unsigned char *sec, size_t sec_len,
|
|
|
|
const unsigned char *seed, size_t seed_len,
|
|
|
|
unsigned char *out, size_t olen)
|
|
|
|
{
|
|
|
|
int chunk;
|
2018-11-02 22:51:19 +08:00
|
|
|
EVP_MAC_CTX *ctx = NULL, *ctx_tmp = NULL, *ctx_init = NULL;
|
2016-01-19 21:57:19 +08:00
|
|
|
unsigned char A1[EVP_MAX_MD_SIZE];
|
|
|
|
size_t A1_len;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
chunk = EVP_MD_size(md);
|
2017-08-02 22:29:31 +08:00
|
|
|
if (!ossl_assert(chunk > 0))
|
2017-06-21 22:55:38 +08:00
|
|
|
goto err;
|
2016-01-19 21:57:19 +08:00
|
|
|
|
2018-11-02 22:51:19 +08:00
|
|
|
ctx = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
|
|
|
|
ctx_tmp = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
|
|
|
|
ctx_init = EVP_MAC_CTX_new_id(EVP_MAC_HMAC);
|
2016-01-19 21:57:19 +08:00
|
|
|
if (ctx == NULL || ctx_tmp == NULL || ctx_init == NULL)
|
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_FLAGS, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW) != 1)
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_MD, md) != 1)
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_KEY, sec, sec_len) != 1)
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (!EVP_MAC_init(ctx_init))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (!EVP_MAC_CTX_copy(ctx, ctx_init))
|
|
|
|
goto err;
|
|
|
|
if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
|
|
|
|
goto err;
|
|
|
|
if (!EVP_MAC_final(ctx, A1, &A1_len))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/* Reinit mac contexts */
|
2018-11-02 22:51:19 +08:00
|
|
|
if (!EVP_MAC_CTX_copy(ctx, ctx_init))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (!EVP_MAC_update(ctx, A1, A1_len))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (olen > (size_t)chunk && !EVP_MAC_CTX_copy(ctx_tmp, ctx))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (olen > (size_t)chunk) {
|
|
|
|
size_t mac_len;
|
2018-11-02 22:51:19 +08:00
|
|
|
if (!EVP_MAC_final(ctx, out, &mac_len))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
|
|
|
out += mac_len;
|
|
|
|
olen -= mac_len;
|
|
|
|
/* calc the next A1 value */
|
2018-11-02 22:51:19 +08:00
|
|
|
if (!EVP_MAC_final(ctx_tmp, A1, &A1_len))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
|
|
|
} else { /* last one */
|
|
|
|
|
2018-11-02 22:51:19 +08:00
|
|
|
if (!EVP_MAC_final(ctx, A1, &A1_len))
|
2016-01-19 21:57:19 +08:00
|
|
|
goto err;
|
|
|
|
memcpy(out, A1, olen);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
err:
|
2018-11-02 22:51:19 +08:00
|
|
|
EVP_MAC_CTX_free(ctx);
|
|
|
|
EVP_MAC_CTX_free(ctx_tmp);
|
|
|
|
EVP_MAC_CTX_free(ctx_init);
|
2016-01-19 21:57:19 +08:00
|
|
|
OPENSSL_cleanse(A1, sizeof(A1));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tls1_prf_alg(const EVP_MD *md,
|
|
|
|
const unsigned char *sec, size_t slen,
|
|
|
|
const unsigned char *seed, size_t seed_len,
|
|
|
|
unsigned char *out, size_t olen)
|
|
|
|
{
|
|
|
|
if (EVP_MD_type(md) == NID_md5_sha1) {
|
|
|
|
size_t i;
|
|
|
|
unsigned char *tmp;
|
|
|
|
if (!tls1_prf_P_hash(EVP_md5(), sec, slen/2 + (slen & 1),
|
2018-06-22 05:16:18 +08:00
|
|
|
seed, seed_len, out, olen))
|
2016-01-19 21:57:19 +08:00
|
|
|
return 0;
|
|
|
|
|
2018-04-03 23:31:16 +08:00
|
|
|
if ((tmp = OPENSSL_malloc(olen)) == NULL) {
|
|
|
|
KDFerr(KDF_F_TLS1_PRF_ALG, ERR_R_MALLOC_FAILURE);
|
2016-01-19 21:57:19 +08:00
|
|
|
return 0;
|
2018-04-03 23:31:16 +08:00
|
|
|
}
|
2016-01-19 21:57:19 +08:00
|
|
|
if (!tls1_prf_P_hash(EVP_sha1(), sec + slen/2, slen/2 + (slen & 1),
|
2018-06-22 05:16:18 +08:00
|
|
|
seed, seed_len, tmp, olen)) {
|
2016-01-19 21:57:19 +08:00
|
|
|
OPENSSL_clear_free(tmp, olen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i < olen; i++)
|
|
|
|
out[i] ^= tmp[i];
|
|
|
|
OPENSSL_clear_free(tmp, olen);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!tls1_prf_P_hash(md, sec, slen, seed, seed_len, out, olen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|