2018-06-22 05:16:18 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2019-04-22 15:18:56 +08:00
|
|
|
* Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved.
|
2018-06-22 05:16:18 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/kdf.h>
|
2019-08-21 16:54:35 +08:00
|
|
|
#include <openssl/core.h>
|
|
|
|
#include <openssl/core_names.h>
|
2019-09-28 06:45:33 +08:00
|
|
|
#include "crypto/evp.h"
|
2018-06-22 05:16:18 +08:00
|
|
|
#include "internal/numbers.h"
|
2019-08-21 16:54:35 +08:00
|
|
|
#include "internal/provider.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "evp_local.h"
|
2018-06-22 05:16:18 +08:00
|
|
|
|
2020-06-18 16:30:48 +08:00
|
|
|
EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf)
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
2019-05-16 09:43:41 +08:00
|
|
|
EVP_KDF_CTX *ctx = NULL;
|
2019-04-22 15:18:56 +08:00
|
|
|
|
2019-05-16 09:43:41 +08:00
|
|
|
if (kdf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
|
2019-08-21 16:54:35 +08:00
|
|
|
if (ctx == NULL
|
2021-05-14 11:08:42 +08:00
|
|
|
|| (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL
|
2019-08-21 16:54:35 +08:00
|
|
|
|| !EVP_KDF_up_ref(kdf)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2019-08-21 16:54:35 +08:00
|
|
|
if (ctx != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
kdf->freectx(ctx->algctx);
|
2019-04-22 15:18:56 +08:00
|
|
|
OPENSSL_free(ctx);
|
|
|
|
ctx = NULL;
|
|
|
|
} else {
|
|
|
|
ctx->meth = kdf;
|
|
|
|
}
|
|
|
|
return ctx;
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:30:48 +08:00
|
|
|
void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
2021-02-16 01:31:36 +08:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
2021-05-14 11:08:42 +08:00
|
|
|
ctx->meth->freectx(ctx->algctx);
|
|
|
|
ctx->algctx = NULL;
|
2021-02-16 01:31:36 +08:00
|
|
|
EVP_KDF_free(ctx->meth);
|
|
|
|
OPENSSL_free(ctx);
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:30:48 +08:00
|
|
|
EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
2019-08-21 16:54:35 +08:00
|
|
|
EVP_KDF_CTX *dst;
|
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL)
|
2019-08-21 16:54:35 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dst = OPENSSL_malloc(sizeof(*dst));
|
|
|
|
if (dst == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2019-08-21 16:54:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(dst, src, sizeof(*dst));
|
|
|
|
if (!EVP_KDF_up_ref(dst->meth)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2019-08-21 16:54:35 +08:00
|
|
|
OPENSSL_free(dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
dst->algctx = src->meth->dupctx(src->algctx);
|
|
|
|
if (dst->algctx == NULL) {
|
2020-06-18 16:30:48 +08:00
|
|
|
EVP_KDF_CTX_free(dst);
|
2019-08-21 16:54:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return dst;
|
2019-04-22 15:18:56 +08:00
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
|
2021-06-01 19:18:04 +08:00
|
|
|
int evp_kdf_get_number(const EVP_KDF *kdf)
|
2019-09-23 17:16:21 +08:00
|
|
|
{
|
|
|
|
return kdf->name_id;
|
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
const char *EVP_KDF_get0_name(const EVP_KDF *kdf)
|
2020-09-22 07:25:35 +08:00
|
|
|
{
|
2021-04-16 22:22:03 +08:00
|
|
|
return kdf->type_name;
|
2020-09-22 07:25:35 +08:00
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
const char *EVP_KDF_get0_description(const EVP_KDF *kdf)
|
EVP: Add EVP_<TYPE>_description()
The following operation types are covered:
EVP_MD, EVP_CIPHER, EVP_MAC, EVP_RAND, EVP_KEYMGMT, EVP_SIGNATURE,
EVP_ASYM_CIPHER, EVP_KEM, EVP_KEYEXCH, EVP_KDF. Also EVP_PKEY.
For EVP_MD and EVP_CIPHER, OBJ_nid2ln() is used as a fallback for
legacy implementations.
For EVP_PKEY, the info field of the EVP_PKEY_ASN1_METHOD is used as a
fallback for legacy implementations.
Fixes #14514
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14656)
2021-03-16 21:23:54 +08:00
|
|
|
{
|
|
|
|
return kdf->description;
|
|
|
|
}
|
|
|
|
|
EVP: add missing common functionality
This adds the missing functions that should be common for all
fetchable EVP sub-APIs:
EVP_KEYMGMT_is_a(), EVP_KEYMGMT_do_all_provided(), EVP_KEYEXCH_is_a(),
EVP_KEYEXCH_do_all_provided(), EVP_KDF_is_a(), EVP_MD_is_a(),
EVP_SIGNATURE_do_all_provided(), EVP_SIGNATURE_is_a().
This also renames EVP_MD_do_all_ex(), EVP_CIPHER_do_all_ex(),
EVP_KDF_do_all_ex(), EVP_MAC_do_all_ex() to change '_ex'
to '_provided'.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/9979)
2019-09-23 16:33:26 +08:00
|
|
|
int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name)
|
|
|
|
{
|
2020-01-15 08:04:37 +08:00
|
|
|
return evp_is_a(kdf->prov, kdf->name_id, NULL, name);
|
EVP: add missing common functionality
This adds the missing functions that should be common for all
fetchable EVP sub-APIs:
EVP_KEYMGMT_is_a(), EVP_KEYMGMT_do_all_provided(), EVP_KEYEXCH_is_a(),
EVP_KEYEXCH_do_all_provided(), EVP_KDF_is_a(), EVP_MD_is_a(),
EVP_SIGNATURE_do_all_provided(), EVP_SIGNATURE_is_a().
This also renames EVP_MD_do_all_ex(), EVP_CIPHER_do_all_ex(),
EVP_KDF_do_all_ex(), EVP_MAC_do_all_ex() to change '_ex'
to '_provided'.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/9979)
2019-09-23 16:33:26 +08:00
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf)
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
2019-08-21 16:54:35 +08:00
|
|
|
return kdf->prov;
|
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
|
2020-06-18 16:30:48 +08:00
|
|
|
const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
|
2019-08-21 16:54:35 +08:00
|
|
|
{
|
|
|
|
return ctx->meth;
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
|
|
|
|
2020-10-13 12:33:01 +08:00
|
|
|
void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx)
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
|
|
|
|
2019-04-22 15:18:56 +08:00
|
|
|
if (ctx->meth->reset != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
ctx->meth->reset(ctx->algctx);
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
|
|
|
|
2020-10-13 12:30:12 +08:00
|
|
|
size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx)
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
2019-08-21 16:54:35 +08:00
|
|
|
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
|
2021-06-02 12:42:56 +08:00
|
|
|
size_t s = 0;
|
2018-06-22 05:16:18 +08:00
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2019-08-21 16:54:35 +08:00
|
|
|
*params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
|
|
|
|
if (ctx->meth->get_ctx_params != NULL
|
2021-05-14 11:08:42 +08:00
|
|
|
&& ctx->meth->get_ctx_params(ctx->algctx, params))
|
2019-08-21 16:54:35 +08:00
|
|
|
return s;
|
|
|
|
if (ctx->meth->get_params != NULL
|
|
|
|
&& ctx->meth->get_params(params))
|
|
|
|
return s;
|
|
|
|
return 0;
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
|
|
|
|
2021-02-26 08:06:11 +08:00
|
|
|
int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen,
|
|
|
|
const OSSL_PARAM params[])
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->derive(ctx->algctx, key, keylen, params);
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 16:54:35 +08:00
|
|
|
/*
|
|
|
|
* The {get,set}_params functions return 1 if there is no corresponding
|
|
|
|
* function in the implementation. This is the same as if there was one,
|
|
|
|
* but it didn't recognise any of the given params, i.e. nothing in the
|
|
|
|
* bag of parameters was useful.
|
|
|
|
*/
|
|
|
|
int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
2019-08-21 16:54:35 +08:00
|
|
|
if (kdf->get_params != NULL)
|
|
|
|
return kdf->get_params(params);
|
|
|
|
return 1;
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:30:48 +08:00
|
|
|
int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
|
2018-06-22 05:16:18 +08:00
|
|
|
{
|
2019-08-21 16:54:35 +08:00
|
|
|
if (ctx->meth->get_ctx_params != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->get_ctx_params(ctx->algctx, params);
|
2019-08-21 16:54:35 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2018-06-22 05:16:18 +08:00
|
|
|
|
2020-06-18 16:30:48 +08:00
|
|
|
int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
|
2019-08-21 16:54:35 +08:00
|
|
|
{
|
|
|
|
if (ctx->meth->set_ctx_params != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->set_ctx_params(ctx->algctx, params);
|
2019-08-21 16:54:35 +08:00
|
|
|
return 1;
|
2018-06-22 05:16:18 +08:00
|
|
|
}
|
2019-09-23 16:56:13 +08:00
|
|
|
|
2021-02-20 01:03:43 +08:00
|
|
|
int EVP_KDF_names_do_all(const EVP_KDF *kdf,
|
|
|
|
void (*fn)(const char *name, void *data),
|
|
|
|
void *data)
|
2019-09-23 16:56:13 +08:00
|
|
|
{
|
|
|
|
if (kdf->prov != NULL)
|
2021-02-20 01:03:43 +08:00
|
|
|
return evp_names_do_all(kdf->prov, kdf->name_id, fn, data);
|
|
|
|
|
|
|
|
return 1;
|
2019-09-23 16:56:13 +08:00
|
|
|
}
|