Skip to content

Commit 25dcc00

Browse files
small changes:
- better ifdef's in hpke api.c tests - updated ssl_ech.c to use wc_HpkeKemGetEncLen in both locations - removed Ndh check in hpke.c, made it inline with the ecc cases
1 parent cbb7bfc commit 25dcc00

3 files changed

Lines changed: 34 additions & 45 deletions

File tree

src/ssl_ech.c

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
316316
{
317317
int i;
318318
word16 totalLen = 0;
319+
word16 kemEncLen;
319320
word16 publicNameLen;
320321

321322
if (config == NULL || (output == NULL && outputLen == NULL))
@@ -338,7 +339,10 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
338339
totalLen += 2;
339340

340341
/* hpke_pub_key */
341-
totalLen += wc_HpkeKemGetEncLen(config->kemId);
342+
kemEncLen = wc_HpkeKemGetEncLen(config->kemId);
343+
if (kemEncLen == 0)
344+
return BAD_FUNC_ARG;
345+
totalLen += kemEncLen;
342346

343347
/* cipherSuitesLen */
344348
totalLen += 2;
@@ -378,38 +382,10 @@ int GetEchConfig(WOLFSSL_EchConfig* config, byte* output, word32* outputLen)
378382
output += 2;
379383

380384
/* length and key itself */
381-
switch (config->kemId) {
382-
case DHKEM_P256_HKDF_SHA256:
383-
c16toa(DHKEM_P256_ENC_LEN, output);
384-
output += 2;
385-
XMEMCPY(output, config->receiverPubkey, DHKEM_P256_ENC_LEN);
386-
output += DHKEM_P256_ENC_LEN;
387-
break;
388-
case DHKEM_P384_HKDF_SHA384:
389-
c16toa(DHKEM_P384_ENC_LEN, output);
390-
output += 2;
391-
XMEMCPY(output, config->receiverPubkey, DHKEM_P384_ENC_LEN);
392-
output += DHKEM_P384_ENC_LEN;
393-
break;
394-
case DHKEM_P521_HKDF_SHA512:
395-
c16toa(DHKEM_P521_ENC_LEN, output);
396-
output += 2;
397-
XMEMCPY(output, config->receiverPubkey, DHKEM_P521_ENC_LEN);
398-
output += DHKEM_P521_ENC_LEN;
399-
break;
400-
case DHKEM_X25519_HKDF_SHA256:
401-
c16toa(DHKEM_X25519_ENC_LEN, output);
402-
output += 2;
403-
XMEMCPY(output, config->receiverPubkey, DHKEM_X25519_ENC_LEN);
404-
output += DHKEM_X25519_ENC_LEN;
405-
break;
406-
case DHKEM_X448_HKDF_SHA512:
407-
c16toa(DHKEM_X448_ENC_LEN, output);
408-
output += 2;
409-
XMEMCPY(output, config->receiverPubkey, DHKEM_X448_ENC_LEN);
410-
output += DHKEM_X448_ENC_LEN;
411-
break;
412-
}
385+
c16toa(kemEncLen, output);
386+
output += 2;
387+
XMEMCPY(output, config->receiverPubkey, kemEncLen);
388+
output += kemEncLen;
413389

414390
/* cipherSuites len */
415391
c16toa(config->numCipherSuites * 4, output);

tests/api.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14572,22 +14572,24 @@ static int test_wolfSSL_Tls13_ECH_all_algos(void)
1457214572
int k;
1457314573
static const word16 kems[] = {
1457414574
#if defined(HAVE_ECC)
14575-
#if (defined(WOLFSSL_SHA224) || !defined(NO_SHA256))
14575+
#if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && !defined(NO_SHA256)
1457614576
DHKEM_P256_HKDF_SHA256,
1457714577
#endif
14578-
#if defined(WOLFSSL_SHA384)
14578+
#if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
14579+
defined(WOLFSSL_SHA384)
1457914580
DHKEM_P384_HKDF_SHA384,
1458014581
#endif
14581-
#if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512))
14582+
#if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && \
14583+
defined(WOLFSSL_SHA512)
1458214584
DHKEM_P521_HKDF_SHA512,
1458314585
#endif
1458414586
#endif /* HAVE_ECC */
14585-
#if defined(HAVE_CURVE25519) && (defined(WOLFSSL_SHA224) || !defined(NO_SHA256))
14587+
#if defined(HAVE_CURVE25519) && !defined(NO_SHA256)
1458614588
DHKEM_X25519_HKDF_SHA256,
1458714589
#endif
1458814590
};
1458914591
static const word16 kdfs[] = {
14590-
#if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
14592+
#if !defined(NO_SHA256)
1459114593
HKDF_SHA256,
1459214594
#endif
1459314595
#ifdef WOLFSSL_SHA384

wolfcrypt/src/hpke.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,12 @@ int wc_HpkeInit(Hpke* hpke, int kem, int kdf, int aead, void* heap)
153153
hpke->curveId = ECC_SECP256R1;
154154
hpke->Nsecret = WC_SHA256_DIGEST_SIZE;
155155
hpke->kemDigest = WC_SHA256;
156-
hpke->Ndh = (word32)wc_ecc_get_curve_size_from_id(hpke->curveId);
156+
ret = wc_ecc_get_curve_size_from_id(hpke->curveId);
157+
if (ret < 0) {
158+
break;
159+
}
160+
hpke->Ndh = (word32)ret;
161+
ret = 0;
157162
hpke->Npk = 1 + hpke->Ndh * 2;
158163
break;
159164
#endif
@@ -164,7 +169,12 @@ int wc_HpkeInit(Hpke* hpke, int kem, int kdf, int aead, void* heap)
164169
hpke->curveId = ECC_SECP384R1;
165170
hpke->Nsecret = WC_SHA384_DIGEST_SIZE;
166171
hpke->kemDigest = WC_SHA384;
167-
hpke->Ndh = (word32)wc_ecc_get_curve_size_from_id(hpke->curveId);
172+
ret = wc_ecc_get_curve_size_from_id(hpke->curveId);
173+
if (ret < 0) {
174+
break;
175+
}
176+
hpke->Ndh = (word32)ret;
177+
ret = 0;
168178
hpke->Npk = 1 + hpke->Ndh * 2;
169179
break;
170180
#endif
@@ -175,7 +185,12 @@ int wc_HpkeInit(Hpke* hpke, int kem, int kdf, int aead, void* heap)
175185
hpke->curveId = ECC_SECP521R1;
176186
hpke->Nsecret = WC_SHA512_DIGEST_SIZE;
177187
hpke->kemDigest = WC_SHA512;
178-
hpke->Ndh = (word32)wc_ecc_get_curve_size_from_id(hpke->curveId);
188+
ret = wc_ecc_get_curve_size_from_id(hpke->curveId);
189+
if (ret < 0) {
190+
break;
191+
}
192+
hpke->Ndh = (word32)ret;
193+
ret = 0;
179194
hpke->Npk = 1 + hpke->Ndh * 2;
180195
break;
181196
#endif
@@ -260,10 +275,6 @@ int wc_HpkeInit(Hpke* hpke, int kem, int kdf, int aead, void* heap)
260275
}
261276
}
262277

263-
if ((int)hpke->Ndh < 0) {
264-
return (int)hpke->Ndh;
265-
}
266-
267278
return ret;
268279
}
269280

0 commit comments

Comments
 (0)