Skip to content

Commit 36580b0

Browse files
move hpke-esque code out of tls
1 parent 5acdcf6 commit 36580b0

6 files changed

Lines changed: 170 additions & 126 deletions

File tree

src/ssl_ech.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,7 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
338338
totalLen += 2;
339339

340340
/* hpke_pub_key */
341-
switch (config->kemId) {
342-
case DHKEM_P256_HKDF_SHA256:
343-
totalLen += DHKEM_P256_ENC_LEN;
344-
break;
345-
case DHKEM_P384_HKDF_SHA384:
346-
totalLen += DHKEM_P384_ENC_LEN;
347-
break;
348-
case DHKEM_P521_HKDF_SHA512:
349-
totalLen += DHKEM_P521_ENC_LEN;
350-
break;
351-
case DHKEM_X25519_HKDF_SHA256:
352-
totalLen += DHKEM_X25519_ENC_LEN;
353-
break;
354-
case DHKEM_X448_HKDF_SHA512:
355-
totalLen += DHKEM_X448_ENC_LEN;
356-
break;
357-
}
341+
totalLen += wc_HpkeKemGetEncLen(config->kemId);
358342

359343
/* cipherSuitesLen */
360344
totalLen += 2;
@@ -693,16 +677,9 @@ int SetEchConfigsEx(WOLFSSL_EchConfig** outputConfigs, void* heap,
693677
break;
694678
}
695679

696-
/* check that we support this config */
697-
for (j = 0; j < HPKE_SUPPORTED_KEM_LEN; j++) {
698-
if (hpkeSupportedKem[j] == workingConfig->kemId)
699-
break;
700-
}
701-
702680
/* KEM or ciphersuite not supported, free this config and then try to
703681
* parse another */
704-
if (j >= HPKE_SUPPORTED_KEM_LEN ||
705-
EchConfigGetSupportedCipherSuite(workingConfig) < 0) {
682+
if (EchConfigGetSupportedCipherSuite(workingConfig) < 0) {
706683
XFREE(workingConfig->cipherSuites, heap, DYNAMIC_TYPE_TMP_BUFFER);
707684
XFREE(workingConfig->publicName, heap, DYNAMIC_TYPE_TMP_BUFFER);
708685
XFREE(workingConfig->raw, heap, DYNAMIC_TYPE_TMP_BUFFER);

src/tls.c

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13259,24 +13259,7 @@ static int TLSX_ECH_Use(WOLFSSL_EchConfig* echConfig, TLSX** extensions,
1325913259
/* configId */
1326013260
ech->configId = echConfig->configId;
1326113261
/* encLen */
13262-
switch (echConfig->kemId)
13263-
{
13264-
case DHKEM_P256_HKDF_SHA256:
13265-
ech->encLen = DHKEM_P256_ENC_LEN;
13266-
break;
13267-
case DHKEM_P384_HKDF_SHA384:
13268-
ech->encLen = DHKEM_P384_ENC_LEN;
13269-
break;
13270-
case DHKEM_P521_HKDF_SHA512:
13271-
ech->encLen = DHKEM_P521_ENC_LEN;
13272-
break;
13273-
case DHKEM_X25519_HKDF_SHA256:
13274-
ech->encLen = DHKEM_X25519_ENC_LEN;
13275-
break;
13276-
case DHKEM_X448_HKDF_SHA512:
13277-
ech->encLen = DHKEM_X448_ENC_LEN;
13278-
break;
13279-
}
13262+
ech->encLen = wc_HpkeKemGetEncLen(echConfig->kemId);
1328013263
/* setup hpke */
1328113264
ech->hpke = (Hpke*)XMALLOC(sizeof(Hpke), heap, DYNAMIC_TYPE_TMP_BUFFER);
1328213265
if (ech->hpke == NULL) {
@@ -13288,8 +13271,13 @@ static int TLSX_ECH_Use(WOLFSSL_EchConfig* echConfig, TLSX** extensions,
1328813271
/* setup the ephemeralKey */
1328913272
if (ret == 0)
1329013273
ret = wc_HpkeGenerateKeyPair(ech->hpke, &ech->ephemeralKey, rng);
13291-
if (ret == 0)
13274+
if (ret == 0) {
1329213275
ret = TLSX_Push(extensions, TLSX_ECH, ech, heap);
13276+
if (ret != 0) {
13277+
wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, ech->ephemeralKey,
13278+
ech->hpke->heap);
13279+
}
13280+
}
1329313281
if (ret != 0) {
1329413282
XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
1329513283
XFREE(ech, heap, DYNAMIC_TYPE_TMP_BUFFER);
@@ -13916,36 +13904,14 @@ static int TLSX_ExtractEch(WOLFSSL_ECH* ech, WOLFSSL_EchConfig* echConfig,
1391613904
byte* aad, word32 aadLen, void* heap)
1391713905
{
1391813906
int ret = 0;
13919-
int expectedEncLen;
1392013907
int i;
1392113908
word32 rawConfigLen = 0;
1392213909
byte* info = NULL;
1392313910
word32 infoLen = 0;
1392413911
if (ech == NULL || echConfig == NULL || aad == NULL)
1392513912
return BAD_FUNC_ARG;
1392613913
/* verify the kem and key len */
13927-
switch (echConfig->kemId)
13928-
{
13929-
case DHKEM_P256_HKDF_SHA256:
13930-
expectedEncLen = DHKEM_P256_ENC_LEN;
13931-
break;
13932-
case DHKEM_P384_HKDF_SHA384:
13933-
expectedEncLen = DHKEM_P384_ENC_LEN;
13934-
break;
13935-
case DHKEM_P521_HKDF_SHA512:
13936-
expectedEncLen = DHKEM_P521_ENC_LEN;
13937-
break;
13938-
case DHKEM_X25519_HKDF_SHA256:
13939-
expectedEncLen = DHKEM_X25519_ENC_LEN;
13940-
break;
13941-
case DHKEM_X448_HKDF_SHA512:
13942-
expectedEncLen = DHKEM_X448_ENC_LEN;
13943-
break;
13944-
default:
13945-
expectedEncLen = 0;
13946-
break;
13947-
}
13948-
if (expectedEncLen != ech->encLen)
13914+
if (wc_HpkeKemGetEncLen(echConfig->kemId) != ech->encLen)
1394913915
return BAD_FUNC_ARG;
1395013916
/* verify the cipher suite */
1395113917
for (i = 0; i < echConfig->numCipherSuites; i++) {
@@ -14229,11 +14195,12 @@ static int TLSX_ECH_Parse(WOLFSSL* ssl, const byte* readBuf, word16 size,
1422914195
static void TLSX_ECH_Free(WOLFSSL_ECH* ech, void* heap)
1423014196
{
1423114197
XFREE(ech->innerClientHello, heap, DYNAMIC_TYPE_TMP_BUFFER);
14232-
if (ech->ephemeralKey != NULL)
14233-
wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, ech->ephemeralKey,
14234-
ech->hpke->heap);
14235-
if (ech->hpke != NULL)
14198+
if (ech->hpke != NULL) {
14199+
if (ech->ephemeralKey != NULL)
14200+
wc_HpkeFreeKey(ech->hpke, ech->hpke->kem, ech->ephemeralKey,
14201+
ech->hpke->heap);
1423614202
XFREE(ech->hpke, heap, DYNAMIC_TYPE_TMP_BUFFER);
14203+
}
1423714204
if (ech->hpkeContext != NULL)
1423814205
XFREE(ech->hpkeContext, heap, DYNAMIC_TYPE_TMP_BUFFER);
1423914206
if (ech->privateName != NULL)

src/tls13.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3782,26 +3782,17 @@ static byte helloRetryRequestRandom[] = {
37823782
/* returns the index of the first supported cipher suite, -1 if none */
37833783
int EchConfigGetSupportedCipherSuite(WOLFSSL_EchConfig* config)
37843784
{
3785-
int i, j, supported = 0;
3785+
int i = 0;
37863786

3787-
for (i = 0; i < config->numCipherSuites; i++) {
3788-
supported = 0;
3789-
3790-
for (j = 0; j < HPKE_SUPPORTED_KDF_LEN; j++) {
3791-
if (config->cipherSuites[i].kdfId == hpkeSupportedKdf[j])
3792-
break;
3793-
}
3794-
3795-
if (j < HPKE_SUPPORTED_KDF_LEN)
3796-
for (j = 0; j < HPKE_SUPPORTED_AEAD_LEN; j++) {
3797-
if (config->cipherSuites[i].aeadId == hpkeSupportedAead[j]) {
3798-
supported = 1;
3799-
break;
3800-
}
3801-
}
3787+
if (!wc_HpkeKemIsSupported(config->kemId)) {
3788+
return WOLFSSL_FATAL_ERROR;
3789+
}
38023790

3803-
if (supported)
3791+
for (i = 0; i < config->numCipherSuites; i++) {
3792+
if (wc_HpkeKdfIsSupported(config->cipherSuites[i].kdfId) &&
3793+
wc_HpkeAeadIsSupported(config->cipherSuites[i].aeadId)) {
38043794
return i;
3795+
}
38053796
}
38063797

38073798
return WOLFSSL_FATAL_ERROR;

tests/api.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14547,15 +14547,41 @@ static int test_wolfSSL_Tls13_ECH_all_algos(void)
1454714547
int j;
1454814548
int k;
1454914549
static const word16 kems[] = {
14550+
#if defined(HAVE_ECC)
14551+
#if (defined(WOLFSSL_SHA224) || !defined(NO_SHA256))
1455014552
DHKEM_P256_HKDF_SHA256,
14553+
#endif
14554+
#if defined(WOLFSSL_SHA384)
1455114555
DHKEM_P384_HKDF_SHA384,
14556+
#endif
14557+
#if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
1455214558
DHKEM_P521_HKDF_SHA512,
14559+
#endif
14560+
#endif /* HAVE_ECC */
14561+
#if defined(HAVE_CURVE25519) && (defined(WOLFSSL_SHA224) || !defined(NO_SHA256))
1455314562
DHKEM_X25519_HKDF_SHA256,
14563+
#endif
14564+
};
14565+
static const word16 kdfs[] = {
14566+
#if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
14567+
HKDF_SHA256,
14568+
#endif
14569+
#ifdef WOLFSSL_SHA384
14570+
HKDF_SHA384,
14571+
#endif
14572+
#ifdef WOLFSSL_SHA512
14573+
HKDF_SHA512,
14574+
#endif
14575+
};
14576+
static const word16 aeads[] = {
14577+
#ifdef WOLFSSL_AES_128
14578+
HPKE_AES_128_GCM,
14579+
#endif
14580+
#ifdef WOLFSSL_AES_256
14581+
HPKE_AES_256_GCM,
14582+
#endif
1455414583
};
14555-
static const word16 kdfs[] = { HKDF_SHA256, HKDF_SHA384, HKDF_SHA512 };
14556-
static const word16 aeads[] = { HPKE_AES_128_GCM, HPKE_AES_256_GCM };
1455714584

14558-
/* test each KEM with default KDF and AEAD */
1455914585
for (i = 0; i < (int)(sizeof(kems) / sizeof(*kems)); i++) {
1456014586
echCbTestKemID = kems[i];
1456114587
for (j = 0; j < (int)(sizeof(kdfs) / sizeof(*kdfs)); j++) {

0 commit comments

Comments
 (0)