summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorslontis <shane.lontis@oracle.com>2022-08-26 11:54:35 +1000
committerHugo Landau <hlandau@openssl.org>2022-09-23 09:24:47 +0100
commit78c44b05945be07eae86f0164b9b777e2de2295b (patch)
tree1c2f721a3bc8405b86f6aac30326265609de7968 /include
parent257cade411ef9217305c5db47f40e5dacdb99c71 (diff)
downloadopenssl-new-78c44b05945be07eae86f0164b9b777e2de2295b.tar.gz
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)
Diffstat (limited to 'include')
-rw-r--r--include/crypto/ec.h3
-rw-r--r--include/crypto/ecx.h3
-rw-r--r--include/crypto/hpke.h47
-rw-r--r--include/openssl/core_dispatch.h8
-rw-r--r--include/openssl/core_names.h5
-rw-r--r--include/openssl/evp.h5
6 files changed, 69 insertions, 2 deletions
diff --git a/include/crypto/ec.h b/include/crypto/ec.h
index 62163b31ac..bb3cb0f979 100644
--- a/include/crypto/ec.h
+++ b/include/crypto/ec.h
@@ -95,6 +95,7 @@ char *ossl_ec_pt_format_id2name(int id);
char *ossl_ec_check_group_type_id2name(int flags);
int ossl_ec_set_check_group_type_from_name(EC_KEY *ec, const char *name);
-
+int ossl_ec_generate_key_dhkem(EC_KEY *eckey,
+ const unsigned char *ikm, size_t ikmlen);
# endif /* OPENSSL_NO_EC */
#endif
diff --git a/include/crypto/ecx.h b/include/crypto/ecx.h
index 48b95fa5ba..79026b6c41 100644
--- a/include/crypto/ecx.h
+++ b/include/crypto/ecx.h
@@ -83,6 +83,9 @@ unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key);
void ossl_ecx_key_free(ECX_KEY *key);
int ossl_ecx_key_up_ref(ECX_KEY *key);
ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection);
+int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen,
+ unsigned char *secret, size_t *secretlen,
+ size_t outlen);
int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
const uint8_t peer_public_value[32]);
diff --git a/include/crypto/hpke.h b/include/crypto/hpke.h
new file mode 100644
index 0000000000..e3596fdb90
--- /dev/null
+++ b/include/crypto/hpke.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2022 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
+ */
+
+#ifndef OSSL_CRYPTO_HPKE_H
+# define OSSL_CRYPTO_HPKE_H
+# pragma once
+
+/* Constants from RFC 9180 Section 7.1 and 7.3 */
+#define OSSL_HPKE_MAX_SECRET 64
+#define OSSL_HPKE_MAX_PUBLIC 133
+#define OSSL_HPKE_MAX_PRIVATE 66
+#define OSSL_HPKE_MAX_NONCE 12
+#define OSSL_HPKE_MAX_KDF_INPUTLEN 64
+
+int ossl_hpke_kdf_extract(EVP_KDF_CTX *kctx,
+ unsigned char *prk, size_t prklen,
+ const unsigned char *salt, size_t saltlen,
+ const unsigned char *ikm, size_t ikmlen);
+
+int ossl_hpke_kdf_expand(EVP_KDF_CTX *kctx,
+ unsigned char *okm, size_t okmlen,
+ const unsigned char *prk, size_t prklen,
+ const unsigned char *info, size_t infolen);
+
+int ossl_hpke_labeled_extract(EVP_KDF_CTX *kctx,
+ unsigned char *prk, size_t prklen,
+ const unsigned char *salt, size_t saltlen,
+ const unsigned char *suiteid, size_t suiteidlen,
+ const char *label,
+ const unsigned char *ikm, size_t ikmlen);
+int ossl_hpke_labeled_expand(EVP_KDF_CTX *kctx,
+ unsigned char *okm, size_t okmlen,
+ const unsigned char *prk, size_t prklen,
+ const unsigned char *suiteid, size_t suiteidlen,
+ const char *label,
+ const unsigned char *info, size_t infolen);
+
+EVP_KDF_CTX *ossl_kdf_ctx_create(const char *kdfname, const char *mdname,
+ OSSL_LIB_CTX *libctx, const char *propq);
+
+#endif
diff --git a/include/openssl/core_dispatch.h b/include/openssl/core_dispatch.h
index 11e3c861aa..114e2667ce 100644
--- a/include/openssl/core_dispatch.h
+++ b/include/openssl/core_dispatch.h
@@ -815,16 +815,24 @@ OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, asym_cipher_settable_ctx_params,
# define OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS 9
# define OSSL_FUNC_KEM_SET_CTX_PARAMS 10
# define OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS 11
+# define OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT 12
+# define OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT 13
OSSL_CORE_MAKE_FUNC(void *, kem_newctx, (void *provctx))
OSSL_CORE_MAKE_FUNC(int, kem_encapsulate_init, (void *ctx, void *provkey,
const OSSL_PARAM params[]))
+OSSL_CORE_MAKE_FUNC(int, kem_auth_encapsulate_init, (void *ctx, void *provkey,
+ void *authprivkey,
+ const OSSL_PARAM params[]))
OSSL_CORE_MAKE_FUNC(int, kem_encapsulate, (void *ctx,
unsigned char *out, size_t *outlen,
unsigned char *secret,
size_t *secretlen))
OSSL_CORE_MAKE_FUNC(int, kem_decapsulate_init, (void *ctx, void *provkey,
const OSSL_PARAM params[]))
+OSSL_CORE_MAKE_FUNC(int, kem_auth_decapsulate_init, (void *ctx, void *provkey,
+ void *authpubkey,
+ const OSSL_PARAM params[]))
OSSL_CORE_MAKE_FUNC(int, kem_decapsulate, (void *ctx,
unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen))
diff --git a/include/openssl/core_names.h b/include/openssl/core_names.h
index 75ec85f22a..487b928e44 100644
--- a/include/openssl/core_names.h
+++ b/include/openssl/core_names.h
@@ -413,6 +413,9 @@ extern "C" {
#define OSSL_PKEY_PARAM_RSA_MGF1_DIGEST OSSL_PKEY_PARAM_MGF1_DIGEST
#define OSSL_PKEY_PARAM_RSA_PSS_SALTLEN "saltlen"
+/* EC, X25519 and X448 Key generation parameters */
+#define OSSL_PKEY_PARAM_DHKEM_IKM "dhkem-ikm"
+
/* Key generation parameters */
#define OSSL_PKEY_PARAM_FFC_TYPE "type"
#define OSSL_PKEY_PARAM_FFC_PBITS "pbits"
@@ -507,9 +510,11 @@ extern "C" {
/* KEM parameters */
#define OSSL_KEM_PARAM_OPERATION "operation"
+#define OSSL_KEM_PARAM_IKME "ikme"
/* OSSL_KEM_PARAM_OPERATION values */
#define OSSL_KEM_PARAM_OPERATION_RSASVE "RSASVE"
+#define OSSL_KEM_PARAM_OPERATION_DHKEM "DHKEM"
/* Capabilities */
diff --git a/include/openssl/evp.h b/include/openssl/evp.h
index d9b3db6ae6..8a9f61d73e 100644
--- a/include/openssl/evp.h
+++ b/include/openssl/evp.h
@@ -1925,14 +1925,17 @@ int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
+int EVP_PKEY_auth_encapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpriv,
+ const OSSL_PARAM params[]);
int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
unsigned char *wrappedkey, size_t *wrappedkeylen,
unsigned char *genkey, size_t *genkeylen);
int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
+int EVP_PKEY_auth_decapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpub,
+ const OSSL_PARAM params[]);
int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
unsigned char *unwrapped, size_t *unwrappedlen,
const unsigned char *wrapped, size_t wrappedlen);
-
typedef int EVP_PKEY_gen_cb(EVP_PKEY_CTX *ctx);
int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx);