#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
defined(HAVE_AESGCM)
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/curve25519.h>
#include <wolfssl/wolfcrypt/curve448.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/sha512.h>
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/hpke.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
static const char* KEM_STR = "KEM";
static const int KEM_STR_LEN = 3;
static const char* HPKE_STR = "HPKE";
static const int HPKE_STR_LEN = 4;
static const char* HPKE_VERSION_STR = "HPKE-v1";
static const int HPKE_VERSION_STR_LEN = 7;
static const char* EAE_PRK_LABEL_STR = "eae_prk";
static const int EAE_PRK_LABEL_STR_LEN = 7;
static const char* SHARED_SECRET_LABEL_STR = "shared_secret";
static const int SHARED_SECRET_LABEL_STR_LEN = 13;
static const char* PSK_ID_HASH_LABEL_STR = "psk_id_hash";
static const int PSK_ID_HASH_LABEL_STR_LEN = 11;
static const char* INFO_HASH_LABEL_STR = "info_hash";
static const int INFO_HASH_LABEL_STR_LEN = 9;
static const char* SECRET_LABEL_STR = "secret";
static const int SECRET_LABEL_STR_LEN = 6;
static const char* KEY_LABEL_STR = "key";
static const int KEY_LABEL_STR_LEN = 3;
static const char* BASE_NONCE_LABEL_STR = "base_nonce";
static const int BASE_NONCE_LABEL_STR_LEN = 10;
static const char* EXP_LABEL_STR = "exp";
static const int EXP_LABEL_STR_LEN = 3;
static int I2OSP(int n, int w, byte* out)
{
int i;
if (w <= 0 || w > 32 || n < 0) {
return MP_VAL;
}
if (w < 4 && n > ((1 << (w * 8)) - 1)) {
return MP_VAL;
}
XMEMSET(out, 0, (size_t)w);
for (i = 0; i < w && n > 0; i++) {
out[w-(i + 1)] = (byte)n;
n >>= 8;
}
return 0;
}
int wc_HpkeInit(Hpke* hpke, int kem, int kdf, int aead, void* heap)
{
int ret;
byte* id;
if (hpke == NULL || kem == 0 || kdf == 0 || aead == 0) {
return BAD_FUNC_ARG;
}
XMEMSET(hpke, 0, sizeof(*hpke));
hpke->kem = (word16)kem;
hpke->kdf = (word16)kdf;
hpke->aead = (word16)aead;
hpke->heap = heap;
id = hpke->kem_suite_id;
XMEMCPY(id, KEM_STR, KEM_STR_LEN);
id += KEM_STR_LEN;
ret = I2OSP(kem, 2, id);
id = hpke->hpke_suite_id;
XMEMCPY(id, HPKE_STR, HPKE_STR_LEN);
id += HPKE_STR_LEN;
if (ret == 0) {
ret = I2OSP(kem, 2, id);
id += 2;
}
if (ret == 0) {
ret = I2OSP(kdf, 2, id);
id += 2;
}
if (ret == 0) {
ret = I2OSP(aead, 2, id);
}
if (ret == 0) {
switch (kem) {
#if defined(HAVE_ECC)
#if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && !defined(NO_SHA256)
case DHKEM_P256_HKDF_SHA256:
hpke->curveId = ECC_SECP256R1;
hpke->Nsecret = WC_SHA256_DIGEST_SIZE;
hpke->kemDigest = WC_SHA256;
ret = wc_ecc_get_curve_size_from_id(hpke->curveId);
if (ret < 0) {
break;
}
hpke->Ndh = (word32)ret;
ret = 0;
hpke->Npk = 1 + hpke->Ndh * 2;
break;
#endif
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA384)
case DHKEM_P384_HKDF_SHA384:
hpke->curveId = ECC_SECP384R1;
hpke->Nsecret = WC_SHA384_DIGEST_SIZE;
hpke->kemDigest = WC_SHA384;
ret = wc_ecc_get_curve_size_from_id(hpke->curveId);
if (ret < 0) {
break;
}
hpke->Ndh = (word32)ret;
ret = 0;
hpke->Npk = 1 + hpke->Ndh * 2;
break;
#endif
#if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA512)
case DHKEM_P521_HKDF_SHA512:
hpke->curveId = ECC_SECP521R1;
hpke->Nsecret = WC_SHA512_DIGEST_SIZE;
hpke->kemDigest = WC_SHA512;
ret = wc_ecc_get_curve_size_from_id(hpke->curveId);
if (ret < 0) {
break;
}
hpke->Ndh = (word32)ret;
ret = 0;
hpke->Npk = 1 + hpke->Ndh * 2;
break;
#endif
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
hpke->Nsecret = WC_SHA256_DIGEST_SIZE;
hpke->kemDigest = WC_SHA256;
hpke->Ndh = CURVE25519_KEYSIZE;
hpke->Npk = CURVE25519_PUB_KEY_SIZE;
break;
#endif
#if defined(HAVE_CURVE448) && defined(WOLFSSL_SHA512)
case DHKEM_X448_HKDF_SHA512:
hpke->Nsecret = WC_SHA512_DIGEST_SIZE;
hpke->kemDigest = WC_SHA512;
hpke->Ndh = 64;
hpke->Npk = CURVE448_PUB_KEY_SIZE;
ret = BAD_FUNC_ARG;
break;
#endif
default:
ret = BAD_FUNC_ARG;
break;
}
}
if (ret == 0) {
switch (kdf) {
#if !defined(NO_SHA256)
case HKDF_SHA256:
hpke->Nh = WC_SHA256_DIGEST_SIZE;
hpke->kdfDigest = WC_SHA256;
break;
#endif
#ifdef WOLFSSL_SHA384
case HKDF_SHA384:
hpke->Nh = WC_SHA384_DIGEST_SIZE;
hpke->kdfDigest = WC_SHA384;
break;
#endif
#ifdef WOLFSSL_SHA512
case HKDF_SHA512:
hpke->Nh = WC_SHA512_DIGEST_SIZE;
hpke->kdfDigest = WC_SHA512;
break;
#endif
default:
ret = BAD_FUNC_ARG;
break;
}
}
if (ret == 0) {
switch (aead) {
#ifdef WOLFSSL_AES_128
case HPKE_AES_128_GCM:
hpke->Nk = AES_128_KEY_SIZE;
hpke->Nn = GCM_NONCE_MID_SZ;
hpke->Nt = WC_AES_BLOCK_SIZE;
break;
#endif
#ifdef WOLFSSL_AES_256
case HPKE_AES_256_GCM:
hpke->Nk = AES_256_KEY_SIZE;
hpke->Nn = GCM_NONCE_MID_SZ;
hpke->Nt = WC_AES_BLOCK_SIZE;
break;
#endif
default:
ret = BAD_FUNC_ARG;
break;
}
}
return ret;
}
int wc_HpkeGenerateKeyPair(Hpke* hpke, void** keypair, WC_RNG* rng)
{
int ret = 0;
if (hpke == NULL || keypair == NULL || rng == NULL)
return BAD_FUNC_ARG;
switch (hpke->kem) {
#if defined(HAVE_ECC)
#if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && !defined(NO_SHA256)
case DHKEM_P256_HKDF_SHA256:
*keypair = wc_ecc_key_new(hpke->heap);
if (*keypair != NULL)
ret = wc_ecc_make_key_ex(rng, 32, (ecc_key*)*keypair,
ECC_SECP256R1);
break;
#endif
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA384)
case DHKEM_P384_HKDF_SHA384:
*keypair = wc_ecc_key_new(hpke->heap);
if (*keypair != NULL)
ret = wc_ecc_make_key_ex(rng, 48, (ecc_key*)*keypair,
ECC_SECP384R1);
break;
#endif
#if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA512)
case DHKEM_P521_HKDF_SHA512:
*keypair = wc_ecc_key_new(hpke->heap);
if (*keypair != NULL)
ret = wc_ecc_make_key_ex(rng, 66, (ecc_key*)*keypair,
ECC_SECP521R1);
break;
#endif
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
*keypair = XMALLOC(sizeof(curve25519_key), hpke->heap,
DYNAMIC_TYPE_CURVE25519);
if (*keypair != NULL) {
ret = wc_curve25519_init_ex((curve25519_key*)*keypair,
hpke->heap, INVALID_DEVID);
if (ret == 0)
ret = wc_curve25519_make_key(rng, 32,
(curve25519_key*)*keypair);
}
break;
#endif
#if defined(HAVE_CURVE448) && defined(WOLFSSL_SHA512)
case DHKEM_X448_HKDF_SHA512:
#endif
default:
ret = BAD_FUNC_ARG;
break;
}
if (ret == 0 && *keypair == NULL)
ret = MEMORY_E;
if (ret != 0 && *keypair != NULL) {
wc_HpkeFreeKey(hpke, hpke->kem, *keypair, hpke->heap);
*keypair = NULL;
}
return ret;
}
int wc_HpkeSerializePublicKey(Hpke* hpke, void* key, byte* out, word16* outSz)
{
int ret;
word32 tmpOutSz;
if (hpke == NULL || key == NULL || out == NULL || outSz == NULL) {
return BAD_FUNC_ARG;
}
tmpOutSz = *outSz;
switch (hpke->kem)
{
#if defined(HAVE_ECC)
case DHKEM_P256_HKDF_SHA256:
case DHKEM_P384_HKDF_SHA384:
case DHKEM_P521_HKDF_SHA512:
ret = wc_ecc_export_x963_ex((ecc_key*)key, out, &tmpOutSz, 0);
break;
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
ret = wc_curve25519_export_public_ex((curve25519_key*)key, out,
&tmpOutSz, EC25519_LITTLE_ENDIAN);
break;
#endif
#if defined(HAVE_CURVE448) && defined(WOLFSSL_SHA512)
case DHKEM_X448_HKDF_SHA512:
#endif
default:
ret = -1;
break;
}
*outSz = (word16)tmpOutSz;
return ret;
}
int wc_HpkeDeserializePublicKey(Hpke* hpke, void** key, const byte* in,
word16 inSz)
{
int ret = 0;
if (hpke == NULL || key == NULL || in == NULL) {
return BAD_FUNC_ARG;
}
if (inSz < (word32)hpke->Npk) {
return BUFFER_E;
}
switch (hpke->kem)
{
#if defined(HAVE_ECC)
case DHKEM_P256_HKDF_SHA256:
case DHKEM_P384_HKDF_SHA384:
case DHKEM_P521_HKDF_SHA512:
*key = wc_ecc_key_new(hpke->heap);
if (*key != NULL) {
ret = wc_ecc_import_x963_ex(in, inSz, (ecc_key*)*key,
hpke->curveId);
}
break;
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
*key = XMALLOC(sizeof(curve25519_key), hpke->heap,
DYNAMIC_TYPE_CURVE25519);
if (*key != NULL) {
ret = wc_curve25519_init_ex((curve25519_key*)*key, hpke->heap,
INVALID_DEVID);
if (ret == 0)
ret = wc_curve25519_import_public_ex(in, inSz,
(curve25519_key*)*key, EC25519_LITTLE_ENDIAN);
}
break;
#endif
#if defined(HAVE_CURVE448) && defined(WOLFSSL_SHA512)
case DHKEM_X448_HKDF_SHA512:
#endif
default:
ret = -1;
break;
}
if (ret == 0 && *key == NULL)
ret = MEMORY_E;
if (ret != 0 && *key != NULL) {
wc_HpkeFreeKey(hpke, hpke->kem, *key, hpke->heap);
*key = NULL;
}
return ret;
}
void wc_HpkeFreeKey(Hpke* hpke, word16 kem, void* keypair, void* heap)
{
switch (kem)
{
#if defined(HAVE_ECC)
case DHKEM_P256_HKDF_SHA256:
case DHKEM_P384_HKDF_SHA384:
case DHKEM_P521_HKDF_SHA512:
wc_ecc_key_free((ecc_key*)keypair);
break;
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
wc_curve25519_free((curve25519_key*)keypair);
XFREE(keypair, heap, DYNAMIC_TYPE_CURVE25519);
break;
#endif
#if defined(HAVE_CURVE448) && defined(WOLFSSL_SHA512)
case DHKEM_X448_HKDF_SHA512:
#endif
default:
break;
}
(void)hpke;
(void)heap;
}
static int wc_HpkeLabeledExtract(Hpke* hpke, byte* suite_id,
word32 suite_id_len, int digest, byte* salt, word32 salt_len, byte* label,
word32 label_len, byte* ikm, word32 ikm_len, byte* out)
{
int ret;
byte* labeled_ikm_p;
word32 remaining;
WC_DECLARE_VAR(labeled_ikm, byte, MAX_HPKE_LABEL_SZ, 0);
if (hpke == NULL) {
return BAD_FUNC_ARG;
}
remaining = (word32)MAX_HPKE_LABEL_SZ;
if ((word32)HPKE_VERSION_STR_LEN > remaining) {
return BUFFER_E;
}
remaining -= (word32)HPKE_VERSION_STR_LEN;
if (suite_id_len > remaining) {
return BUFFER_E;
}
remaining -= suite_id_len;
if (label_len > remaining) {
return BUFFER_E;
}
remaining -= label_len;
if (ikm_len > remaining) {
return BUFFER_E;
}
WC_ALLOC_VAR_EX(labeled_ikm, byte, MAX_HPKE_LABEL_SZ, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
XMEMCPY(labeled_ikm, HPKE_VERSION_STR, HPKE_VERSION_STR_LEN);
labeled_ikm_p = labeled_ikm + HPKE_VERSION_STR_LEN;
XMEMCPY(labeled_ikm_p, suite_id, suite_id_len);
labeled_ikm_p += suite_id_len;
XMEMCPY(labeled_ikm_p, label, label_len);
labeled_ikm_p += label_len;
if (ikm_len != 0) {
XMEMCPY(labeled_ikm_p, ikm, ikm_len);
labeled_ikm_p += ikm_len;
}
PRIVATE_KEY_UNLOCK();
ret = wc_HKDF_Extract(digest, salt, salt_len, labeled_ikm,
(word32)(size_t)(labeled_ikm_p - labeled_ikm), out);
PRIVATE_KEY_LOCK();
WC_FREE_VAR_EX(labeled_ikm, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
static int wc_HpkeLabeledExpand(Hpke* hpke, byte* suite_id, word32 suite_id_len,
int digest, byte* prk, word32 prk_len, byte* label, word32 label_len,
byte* info, word32 infoSz, word32 L, byte* out)
{
int ret;
byte* labeled_info_p;
word32 remaining;
WC_DECLARE_VAR(labeled_info, byte, MAX_HPKE_LABEL_SZ, 0);
if (hpke == NULL) {
return BAD_FUNC_ARG;
}
remaining = (word32)MAX_HPKE_LABEL_SZ;
if (2U + (word32)HPKE_VERSION_STR_LEN > remaining) {
return BUFFER_E;
}
remaining -= 2U + (word32)HPKE_VERSION_STR_LEN;
if (suite_id_len > remaining) {
return BUFFER_E;
}
remaining -= suite_id_len;
if (label_len > remaining) {
return BUFFER_E;
}
remaining -= label_len;
if (infoSz > remaining) {
return BUFFER_E;
}
WC_ALLOC_VAR_EX(labeled_info, byte, MAX_HPKE_LABEL_SZ, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
ret = I2OSP((int)L, 2, labeled_info);
labeled_info_p = labeled_info + 2;
if (ret == 0) {
XMEMCPY(labeled_info_p, HPKE_VERSION_STR, HPKE_VERSION_STR_LEN);
labeled_info_p += HPKE_VERSION_STR_LEN;
XMEMCPY(labeled_info_p, suite_id, suite_id_len);
labeled_info_p += suite_id_len;
XMEMCPY(labeled_info_p, label, label_len);
labeled_info_p += label_len;
XMEMCPY(labeled_info_p, info, infoSz);
labeled_info_p += infoSz;
PRIVATE_KEY_UNLOCK();
ret = wc_HKDF_Expand(digest, prk, prk_len, labeled_info,
(word32)(size_t)(labeled_info_p - labeled_info), out, L);
PRIVATE_KEY_LOCK();
}
WC_FREE_VAR_EX(labeled_info, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
static int wc_HpkeContextComputeNonce(Hpke* hpke, HpkeBaseContext* context,
byte* out)
{
int ret;
byte seq_bytes[HPKE_Nn_MAX];
if (hpke == NULL || context == NULL) {
return BAD_FUNC_ARG;
}
ret = I2OSP(context->seq, (int)hpke->Nn, seq_bytes);
if (ret == 0) {
xorbufout(out, context->base_nonce, seq_bytes, hpke->Nn);
}
return ret;
}
static int wc_HpkeExtractAndExpand( Hpke* hpke, byte* dh, word32 dh_len,
byte* kemContext, word32 kem_context_length, byte* sharedSecret)
{
int ret;
WC_DECLARE_VAR(eae_prk, byte, WC_MAX_DIGEST_SIZE, 0);
if (hpke == NULL) {
return BAD_FUNC_ARG;
}
WC_ALLOC_VAR_EX(eae_prk, byte, WC_MAX_DIGEST_SIZE, hpke->heap,
DYNAMIC_TYPE_DIGEST, return MEMORY_E);
ret = wc_HpkeLabeledExtract(hpke, hpke->kem_suite_id,
sizeof( hpke->kem_suite_id ), hpke->kemDigest, NULL, 0,
(byte*)EAE_PRK_LABEL_STR, EAE_PRK_LABEL_STR_LEN, dh, dh_len, eae_prk);
if ( ret == 0 ) {
ret = wc_HpkeLabeledExpand(hpke, hpke->kem_suite_id,
sizeof( hpke->kem_suite_id ), hpke->kemDigest, eae_prk,
hpke->Nsecret, (byte*)SHARED_SECRET_LABEL_STR,
SHARED_SECRET_LABEL_STR_LEN, kemContext, kem_context_length,
hpke->Nsecret, sharedSecret);
}
ForceZero(eae_prk, WC_MAX_DIGEST_SIZE);
WC_FREE_VAR_EX(eae_prk, hpke->heap, DYNAMIC_TYPE_DIGEST);
return ret;
}
static int wc_HpkeKeyScheduleBase(Hpke* hpke, HpkeBaseContext* context,
byte* sharedSecret, byte* info, word32 infoSz)
{
int ret;
#ifndef WOLFSSL_SMALL_STACK
byte key_schedule_context[1 + 2 * WC_MAX_DIGEST_SIZE];
byte secret[WC_MAX_DIGEST_SIZE];
#else
byte* key_schedule_context = NULL;
byte* secret = NULL;
#endif
if (hpke == NULL) {
return BAD_FUNC_ARG;
}
#ifdef WOLFSSL_SMALL_STACK
key_schedule_context = (byte*)XMALLOC((1 + 2 * WC_MAX_DIGEST_SIZE),
hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
secret = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, hpke->heap,
DYNAMIC_TYPE_DIGEST);
if (key_schedule_context == NULL || secret == NULL) {
XFREE(key_schedule_context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(secret, hpke->heap, DYNAMIC_TYPE_DIGEST);
return MEMORY_E;
}
#endif
context->seq = 0;
key_schedule_context[0] = 0;
ret = wc_HpkeLabeledExtract(hpke, hpke->hpke_suite_id,
sizeof( hpke->hpke_suite_id ), hpke->kdfDigest, NULL, 0,
(byte*)PSK_ID_HASH_LABEL_STR, PSK_ID_HASH_LABEL_STR_LEN, NULL, 0,
key_schedule_context + 1);
if (ret == 0) {
ret = wc_HpkeLabeledExtract(hpke, hpke->hpke_suite_id,
sizeof( hpke->hpke_suite_id ), hpke->kdfDigest, NULL, 0,
(byte*)INFO_HASH_LABEL_STR, INFO_HASH_LABEL_STR_LEN, info, infoSz,
key_schedule_context + 1 + hpke->Nh);
}
if (ret == 0) {
ret = wc_HpkeLabeledExtract(hpke, hpke->hpke_suite_id,
sizeof( hpke->hpke_suite_id ), hpke->kdfDigest, sharedSecret,
hpke->Nsecret, (byte*)SECRET_LABEL_STR, SECRET_LABEL_STR_LEN,
NULL, 0, secret);
}
if (ret == 0)
ret = wc_HpkeLabeledExpand(hpke, hpke->hpke_suite_id,
sizeof( hpke->hpke_suite_id ), hpke->kdfDigest, secret, hpke->Nh,
(byte*)KEY_LABEL_STR, KEY_LABEL_STR_LEN, key_schedule_context,
1 + 2 * hpke->Nh, hpke->Nk, context->key);
if (ret == 0) {
ret = wc_HpkeLabeledExpand(hpke, hpke->hpke_suite_id,
sizeof( hpke->hpke_suite_id ), hpke->kdfDigest, secret, hpke->Nh,
(byte*)BASE_NONCE_LABEL_STR, BASE_NONCE_LABEL_STR_LEN,
key_schedule_context, 1 + 2 * hpke->Nh, hpke->Nn,
context->base_nonce);
}
if (ret == 0) {
ret = wc_HpkeLabeledExpand(hpke, hpke->hpke_suite_id,
sizeof( hpke->hpke_suite_id ), hpke->kdfDigest, secret, hpke->Nh,
(byte*)EXP_LABEL_STR, EXP_LABEL_STR_LEN, key_schedule_context,
1 + 2 * hpke->Nh, hpke->Nh, context->exporter_secret);
}
ForceZero(key_schedule_context, 1 + 2 * WC_MAX_DIGEST_SIZE);
ForceZero(secret, WC_MAX_DIGEST_SIZE);
WC_FREE_VAR_EX(key_schedule_context, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER);
WC_FREE_VAR_EX(secret, hpke->heap, DYNAMIC_TYPE_DIGEST);
return ret;
}
static int wc_HpkeEncap(Hpke* hpke, void* ephemeralKey, void* receiverKey,
byte* sharedSecret)
{
int ret;
#if defined(ECC_TIMING_RESISTANT) && defined(HAVE_ECC)
WC_RNG* rng;
#endif
word32 dh_len;
word16 receiverPubKeySz;
word16 ephemeralPubKeySz;
#ifndef WOLFSSL_SMALL_STACK
byte dh[HPKE_Ndh_MAX];
byte kemContext[HPKE_Npk_MAX * 2];
#else
byte* dh = NULL;
byte* kemContext = NULL;
#endif
if (hpke == NULL || ephemeralKey == NULL || receiverKey == NULL ||
sharedSecret == NULL) {
return BAD_FUNC_ARG;
}
receiverPubKeySz = (word16)hpke->Npk;
ephemeralPubKeySz = (word16)hpke->Npk;
#ifdef WOLFSSL_SMALL_STACK
dh = (byte*)XMALLOC(hpke->Ndh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
kemContext = (byte*)XMALLOC(hpke->Npk * 2, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (dh == NULL || kemContext == NULL) {
XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
dh_len = hpke->Ndh;
switch (hpke->kem)
{
#if defined(HAVE_ECC)
case DHKEM_P256_HKDF_SHA256:
case DHKEM_P384_HKDF_SHA384:
case DHKEM_P521_HKDF_SHA512:
#ifdef ECC_TIMING_RESISTANT
rng = wc_rng_new(NULL, 0, hpke->heap);
if (rng == NULL) {
ret = RNG_FAILURE_E;
break;
}
wc_ecc_set_rng((ecc_key*)ephemeralKey, rng);
#endif
ret = wc_ecc_shared_secret((ecc_key*)ephemeralKey,
(ecc_key*)receiverKey, dh, &dh_len);
#ifdef ECC_TIMING_RESISTANT
wc_rng_free(rng);
#endif
break;
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
ret = wc_curve25519_shared_secret_ex((curve25519_key*)ephemeralKey,
(curve25519_key*)receiverKey, dh, &dh_len,
EC25519_LITTLE_ENDIAN);
break;
#endif
#if defined(HAVE_CURVE448) && defined(WOLFSSL_SHA512)
case DHKEM_X448_HKDF_SHA512:
#endif
default:
ret = -1;
break;
}
if (ret == 0) {
ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey,
kemContext, &ephemeralPubKeySz);
}
if (ret == 0) {
ret = wc_HpkeSerializePublicKey(hpke, receiverKey,
kemContext + ephemeralPubKeySz, &receiverPubKeySz);
}
if (ret == 0) {
ret = wc_HpkeExtractAndExpand(hpke, dh, dh_len, kemContext,
hpke->Npk * 2, sharedSecret);
}
ForceZero(dh, hpke->Ndh);
ForceZero(kemContext, hpke->Npk * 2);
WC_FREE_VAR_EX(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
WC_FREE_VAR_EX(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
static int wc_HpkeSetupBaseSender(Hpke* hpke, HpkeBaseContext* context,
void* ephemeralKey, void* receiverKey, byte* info, word32 infoSz)
{
int ret;
WC_DECLARE_VAR(sharedSecret, byte, HPKE_Nsecret_MAX, 0);
if (hpke == NULL) {
return BAD_FUNC_ARG;
}
#ifdef WOLFSSL_SMALL_STACK
sharedSecret = (byte*)XMALLOC(hpke->Nsecret, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (sharedSecret == NULL) {
return MEMORY_E;
}
#endif
ret = wc_HpkeEncap(hpke, ephemeralKey, receiverKey, sharedSecret);
if (ret == 0) {
ret = wc_HpkeKeyScheduleBase(hpke, context, sharedSecret, info,
infoSz);
}
ForceZero(sharedSecret, hpke->Nsecret);
WC_FREE_VAR_EX(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
int wc_HpkeInitSealContext(Hpke* hpke, HpkeBaseContext* context,
void* ephemeralKey, void* receiverKey, byte* info, word32 infoSz)
{
if (hpke == NULL || context == NULL || ephemeralKey == NULL ||
receiverKey == NULL || (info == NULL && infoSz > 0)) {
return BAD_FUNC_ARG;
}
XMEMSET(context, 0, sizeof(HpkeBaseContext));
return wc_HpkeSetupBaseSender(hpke, context, ephemeralKey, receiverKey,
info, infoSz);
}
int wc_HpkeContextSealBase(Hpke* hpke, HpkeBaseContext* context,
byte* aad, word32 aadSz, byte* plaintext, word32 ptSz, byte* out)
{
int ret;
byte nonce[HPKE_Nn_MAX];
WC_DECLARE_VAR(aes, Aes, 1, 0);
if (hpke == NULL || context == NULL || (aad == NULL && aadSz > 0) ||
plaintext == NULL || out == NULL) {
return BAD_FUNC_ARG;
}
if (context->seq == WC_MAX_SINT_OF(int))
return SEQ_OVERFLOW_E;
WC_ALLOC_VAR_EX(aes, Aes, 1, hpke->heap, DYNAMIC_TYPE_AES,
return MEMORY_E);
ret = wc_AesInit(aes, hpke->heap, INVALID_DEVID);
if (ret == 0) {
ret = wc_HpkeContextComputeNonce(hpke, context, nonce);
if (ret == 0) {
ret = wc_AesGcmSetKey(aes, context->key, hpke->Nk);
}
if (ret == 0) {
ret = wc_AesGcmEncrypt(aes, out, plaintext, ptSz, nonce,
hpke->Nn, out + ptSz, hpke->Nt, aad, aadSz);
}
if (ret == 0) {
context->seq++;
}
wc_AesFree(aes);
}
WC_FREE_VAR_EX(aes, hpke->heap, DYNAMIC_TYPE_AES);
return ret;
}
int wc_HpkeSealBase(Hpke* hpke, void* ephemeralKey, void* receiverKey,
byte* info, word32 infoSz, byte* aad, word32 aadSz, byte* plaintext,
word32 ptSz, byte* ciphertext)
{
int ret;
WC_DECLARE_VAR(context, HpkeBaseContext, 1, 0);
if (hpke == NULL || ephemeralKey == NULL || receiverKey == NULL ||
(info == NULL && infoSz != 0) || (aad == NULL && aadSz != 0) ||
plaintext == NULL || ciphertext == NULL) {
return BAD_FUNC_ARG;
}
WC_ALLOC_VAR_EX(context, HpkeBaseContext, 1, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
PRIVATE_KEY_UNLOCK();
ret = wc_HpkeSetupBaseSender(hpke, context, ephemeralKey, receiverKey, info,
infoSz);
if (ret == 0) {
ret = wc_HpkeContextSealBase(hpke, context, aad, aadSz, plaintext,
ptSz, ciphertext);
}
PRIVATE_KEY_LOCK();
ForceZero(context, sizeof(HpkeBaseContext));
WC_FREE_VAR_EX(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
static int wc_HpkeDecap(Hpke* hpke, void* receiverKey, const byte* pubKey,
word16 pubKeySz, byte* sharedSecret)
{
int ret;
#if defined(ECC_TIMING_RESISTANT) || defined(WOLFSSL_CURVE25519_BLINDING)
WC_RNG* rng;
#endif
word32 dh_len;
word16 receiverPubKeySz;
void* ephemeralKey = NULL;
#ifndef WOLFSSL_SMALL_STACK
byte dh[HPKE_Ndh_MAX];
byte kemContext[HPKE_Npk_MAX * 2];
#else
byte* dh = NULL;
byte* kemContext = NULL;
#endif
if (hpke == NULL || receiverKey == NULL) {
return BAD_FUNC_ARG;
}
receiverPubKeySz = (word16)hpke->Npk;
#ifdef WOLFSSL_SMALL_STACK
dh = (byte*)XMALLOC(hpke->Ndh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
kemContext = (byte*)XMALLOC(hpke->Npk * 2, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (dh == NULL || kemContext == NULL) {
XFREE(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
ret = wc_HpkeDeserializePublicKey(hpke, &ephemeralKey, pubKey, pubKeySz);
dh_len = hpke->Ndh;
if (ret == 0)
switch (hpke->kem)
{
#if defined(HAVE_ECC)
case DHKEM_P256_HKDF_SHA256:
case DHKEM_P384_HKDF_SHA384:
case DHKEM_P521_HKDF_SHA512:
#ifdef ECC_TIMING_RESISTANT
rng = wc_rng_new(NULL, 0, hpke->heap);
if (rng == NULL) {
ret = RNG_FAILURE_E;
break;
}
wc_ecc_set_rng((ecc_key*)receiverKey, rng);
#endif
ret = wc_ecc_shared_secret((ecc_key*)receiverKey,
(ecc_key*)ephemeralKey, dh, &dh_len);
#ifdef ECC_TIMING_RESISTANT
wc_rng_free(rng);
#endif
break;
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
#ifdef WOLFSSL_CURVE25519_BLINDING
rng = wc_rng_new(NULL, 0, hpke->heap);
if (rng == NULL) {
ret = RNG_FAILURE_E;
break;
}
wc_curve25519_set_rng((curve25519_key*)receiverKey, rng);
#endif
ret = wc_curve25519_shared_secret_ex(
(curve25519_key*)receiverKey, (curve25519_key*)ephemeralKey,
dh, &dh_len, EC25519_LITTLE_ENDIAN);
#ifdef WOLFSSL_CURVE25519_BLINDING
wc_rng_free(rng);
#endif
break;
#endif
#if defined(HAVE_CURVE448) && defined(WOLFSSL_SHA512)
case DHKEM_X448_HKDF_SHA512:
#endif
default:
ret = -1;
break;
}
if (ephemeralKey != NULL)
wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
if (ret == 0) {
XMEMCPY(kemContext, pubKey, hpke->Npk);
ret = wc_HpkeSerializePublicKey(hpke, receiverKey,
kemContext + hpke->Npk, &receiverPubKeySz);
}
if (ret == 0) {
ret = wc_HpkeExtractAndExpand(hpke, dh, dh_len, kemContext,
hpke->Npk * 2, sharedSecret);
}
ForceZero(dh, hpke->Ndh);
ForceZero(kemContext, hpke->Npk * 2);
WC_FREE_VAR_EX(dh, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
WC_FREE_VAR_EX(kemContext, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
static int wc_HpkeSetupBaseReceiver(Hpke* hpke, HpkeBaseContext* context,
void* receiverKey, const byte* pubKey, word16 pubKeySz, byte* info,
word32 infoSz)
{
int ret;
WC_DECLARE_VAR(sharedSecret, byte, HPKE_Nsecret_MAX, 0);
WC_ALLOC_VAR_EX(sharedSecret, byte, hpke->Nsecret, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
ret = wc_HpkeDecap(hpke, receiverKey, pubKey, pubKeySz, sharedSecret);
if (ret == 0) {
ret = wc_HpkeKeyScheduleBase(hpke, context, sharedSecret, info,
infoSz);
}
ForceZero(sharedSecret, hpke->Nsecret);
WC_FREE_VAR_EX(sharedSecret, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
int wc_HpkeInitOpenContext(Hpke* hpke, HpkeBaseContext* context,
void* receiverKey, const byte* pubKey, word16 pubKeySz, byte* info,
word32 infoSz)
{
if (hpke == NULL || context == NULL || receiverKey == NULL || pubKey == NULL
|| (info == NULL && infoSz > 0)) {
return BAD_FUNC_ARG;
}
return wc_HpkeSetupBaseReceiver(hpke, context, receiverKey, pubKey,
pubKeySz, info, infoSz);
}
int wc_HpkeContextOpenBase(Hpke* hpke, HpkeBaseContext* context, byte* aad,
word32 aadSz, byte* ciphertext, word32 ctSz, byte* out)
{
int ret;
byte nonce[HPKE_Nn_MAX];
WC_DECLARE_VAR(aes, Aes, 1, 0);
if (hpke == NULL || context == NULL || ciphertext == NULL || out == NULL) {
return BAD_FUNC_ARG;
}
if (context->seq == WC_MAX_SINT_OF(int))
return SEQ_OVERFLOW_E;
XMEMSET(nonce, 0, sizeof(nonce));
WC_ALLOC_VAR_EX(aes, Aes, 1, hpke->heap, DYNAMIC_TYPE_AES,
return MEMORY_E);
ret = wc_HpkeContextComputeNonce(hpke, context, nonce);
if (ret == 0)
ret = wc_AesInit(aes, hpke->heap, INVALID_DEVID);
if (ret == 0) {
ret = wc_AesGcmSetKey(aes, context->key, hpke->Nk);
if (ret == 0) {
ret = wc_AesGcmDecrypt(aes, out, ciphertext, ctSz, nonce,
hpke->Nn, ciphertext + ctSz, hpke->Nt, aad, aadSz);
}
if (ret == 0) {
context->seq++;
}
wc_AesFree(aes);
}
WC_FREE_VAR_EX(aes, hpke->heap, DYNAMIC_TYPE_AES);
return ret;
}
int wc_HpkeOpenBase(Hpke* hpke, void* receiverKey, const byte* pubKey,
word16 pubKeySz, byte* info, word32 infoSz, byte* aad, word32 aadSz,
byte* ciphertext, word32 ctSz, byte* plaintext)
{
int ret;
WC_DECLARE_VAR(context, HpkeBaseContext, 1, 0);
if (hpke == NULL || receiverKey == NULL || pubKey == NULL ||
pubKeySz == 0 || (info == NULL && infoSz != 0) ||
(aad == NULL && aadSz != 0) || plaintext == NULL ||
ciphertext == NULL) {
return BAD_FUNC_ARG;
}
WC_ALLOC_VAR_EX(context, HpkeBaseContext, 1, hpke->heap,
DYNAMIC_TYPE_TMP_BUFFER, return MEMORY_E);
PRIVATE_KEY_UNLOCK();
ret = wc_HpkeSetupBaseReceiver(hpke, context, receiverKey, pubKey,
pubKeySz, info, infoSz);
if (ret == 0) {
ret = wc_HpkeContextOpenBase(hpke, context, aad, aadSz, ciphertext,
ctSz, plaintext);
}
PRIVATE_KEY_LOCK();
ForceZero(context, sizeof(HpkeBaseContext));
WC_FREE_VAR_EX(context, hpke->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
WOLFSSL_LOCAL word16 wc_HpkeKemGetEncLen(word16 kemId)
{
switch (kemId)
{
#if defined(HAVE_ECC)
#if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && !defined(NO_SHA256)
case DHKEM_P256_HKDF_SHA256:
return DHKEM_P256_ENC_LEN;
#endif
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA384)
case DHKEM_P384_HKDF_SHA384:
return DHKEM_P384_ENC_LEN;
#endif
#if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA512)
case DHKEM_P521_HKDF_SHA512:
return DHKEM_P521_ENC_LEN;
#endif
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
return DHKEM_X25519_ENC_LEN;
#endif
default:
return 0;
}
}
WOLFSSL_LOCAL int wc_HpkeKemIsSupported(word16 kemId)
{
switch (kemId) {
#if defined(HAVE_ECC)
#if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && !defined(NO_SHA256)
case DHKEM_P256_HKDF_SHA256:
#endif
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA384)
case DHKEM_P384_HKDF_SHA384:
#endif
#if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && \
defined(WOLFSSL_SHA512)
case DHKEM_P521_HKDF_SHA512:
#endif
#endif
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
case DHKEM_X25519_HKDF_SHA256:
#endif
return 1;
default:
return 0;
}
}
WOLFSSL_LOCAL int wc_HpkeKdfIsSupported(word16 kdfId)
{
switch (kdfId) {
#if !defined(NO_SHA256)
case HKDF_SHA256:
#endif
#ifdef WOLFSSL_SHA384
case HKDF_SHA384:
#endif
#ifdef WOLFSSL_SHA512
case HKDF_SHA512:
#endif
return 1;
default:
return 0;
}
}
WOLFSSL_LOCAL int wc_HpkeAeadIsSupported(word16 aeadId)
{
switch (aeadId) {
#ifdef WOLFSSL_AES_128
case HPKE_AES_128_GCM:
#endif
#ifdef WOLFSSL_AES_256
case HPKE_AES_256_GCM:
#endif
return 1;
default:
return 0;
}
}
#endif