openssl/providers/implementations/exchange/ecx_exch.c

180 lines
4.8 KiB
C
Raw Normal View History

/*
* Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
*
* 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 <openssl/crypto.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/proverr.h>
#include "internal/cryptlib.h"
#include "crypto/ecx.h"
#include "prov/implementations.h"
#include "prov/providercommon.h"
static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
static OSSL_FUNC_keyexch_init_fn ecx_init;
static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
static OSSL_FUNC_keyexch_derive_fn ecx_derive;
static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
/*
* What's passed as an actual key is defined by the KEYMGMT interface.
* We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
* we use that here too.
*/
typedef struct {
size_t keylen;
ECX_KEY *key;
ECX_KEY *peerkey;
} PROV_ECX_CTX;
static void *ecx_newctx(void *provctx, size_t keylen)
{
PROV_ECX_CTX *ctx;
if (!ossl_prov_is_running())
return NULL;
ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
if (ctx == NULL)
return NULL;
ctx->keylen = keylen;
return ctx;
}
static void *x25519_newctx(void *provctx)
{
return ecx_newctx(provctx, X25519_KEYLEN);
}
static void *x448_newctx(void *provctx)
{
return ecx_newctx(provctx, X448_KEYLEN);
}
static int ecx_init(void *vecxctx, void *vkey,
ossl_unused const OSSL_PARAM params[])
{
PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
ECX_KEY *key = vkey;
if (!ossl_prov_is_running())
return 0;
if (ecxctx == NULL
|| key == NULL
|| key->keylen != ecxctx->keylen
|| !ossl_ecx_key_up_ref(key)) {
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
return 0;
}
ossl_ecx_key_free(ecxctx->key);
ecxctx->key = key;
return 1;
}
static int ecx_set_peer(void *vecxctx, void *vkey)
{
PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
ECX_KEY *key = vkey;
if (!ossl_prov_is_running())
return 0;
if (ecxctx == NULL
|| key == NULL
|| key->keylen != ecxctx->keylen
|| !ossl_ecx_key_up_ref(key)) {
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
return 0;
}
ossl_ecx_key_free(ecxctx->peerkey);
ecxctx->peerkey = key;
return 1;
}
static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
size_t outlen)
{
PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
if (!ossl_prov_is_running())
return 0;
Add HPKE DHKEM provider support for EC, X25519 and X448. The code is derived from @sftcd's work in PR #17172. This PR puts the DHKEM algorithms into the provider layer as KEM algorithms for EC and ECX. This PR only implements the DHKEM component of HPKE as specified in RFC 9180. crypto/hpke/hpke_util.c has been added for fuctions that will be shared between DHKEM and HPKE. API's for EVP_PKEY_auth_encapsulate_init() and EVP_PKEY_auth_decapsulate_init() have been added to support authenticated encapsulation. auth_init() functions were chosen rather that a EVP_PKEY_KEM_set_auth() interface to support future algorithms that could possibly need different init functions. Internal code has been refactored, so that it can be shared between the DHKEM and other systems. Since DHKEM operates on low level keys it needs to be able to do low level ECDH and ECXDH calls without converting the keys back into EVP_PKEY/EVP_PKEY_CTX form. See ossl_ecx_compute_key(), ossl_ec_public_from_private() DHKEM requires API's to derive a key using a seed (IKM). This did not sit well inside the DHKEM itself as dispatch functions. This functionality fits better inside the EC and ECX keymanagers keygen, since they are just variations of keygen where the private key is generated in a different manner. This should mainly be used for testing purposes. See ossl_ec_generate_key_dhkem(). It supports this by allowing a settable param to be passed to keygen (See OSSL_PKEY_PARAM_DHKEM_IKM). The keygen calls code within ec and ecx dhkem implementation to handle this. See ossl_ecx_dhkem_derive_private() and ossl_ec_dhkem_derive_private(). These 2 functions are also used by the EC/ECX DHKEM implementations to generate the sender ephemeral keys. Reviewed-by: Hugo Landau <hlandau@openssl.org> Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/19068)
2022-08-26 09:54:35 +08:00
return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen,
secret, secretlen, outlen);
}
static void ecx_freectx(void *vecxctx)
{
PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
ossl_ecx_key_free(ecxctx->key);
ossl_ecx_key_free(ecxctx->peerkey);
OPENSSL_free(ecxctx);
}
static void *ecx_dupctx(void *vecxctx)
{
PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
PROV_ECX_CTX *dstctx;
if (!ossl_prov_is_running())
return NULL;
dstctx = OPENSSL_zalloc(sizeof(*srcctx));
if (dstctx == NULL)
return NULL;
*dstctx = *srcctx;
if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
OPENSSL_free(dstctx);
return NULL;
}
if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
ossl_ecx_key_free(dstctx->key);
OPENSSL_free(dstctx);
return NULL;
}
return dstctx;
}
const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
{ OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
{ OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
{ OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
{ OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
{ OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
{ OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
OSSL_DISPATCH_END
};
const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
{ OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
{ OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
{ OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
{ OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
{ OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
{ OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
OSSL_DISPATCH_END
};