Skip to content

Commit cee4b2b

Browse files
authored
Merge pull request #10713 from SparkiDev/curve25519_hibit_mask
X25519: standard requires masking of top bit
2 parents 70dad95 + e017e6c commit cee4b2b

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ WOLFSSL_VALIDATE_DH_KEYGEN
971971
WOLFSSL_WC_SLHDSA_RECURSIVE
972972
WOLFSSL_WC_XMSS_NO_SHA256
973973
WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME
974+
WOLFSSL_X25519_NO_MASK_PEER
974975
WOLFSSL_X509_STORE_ALLOW_NON_CA_INTERMEDIATE
975976
WOLFSSL_X509_STORE_CERTS
976977
WOLFSSL_X509_TRUSTED_CERTIFICATE_CALLBACK

tests/api/test_curve25519.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ int test_wc_curve25519_shared_secret_ex(void)
307307
WC_RNG rng;
308308
byte out[CURVE25519_KEYSIZE];
309309
word32 outLen = sizeof(out);
310+
#ifndef WOLFSSL_X25519_NO_MASK_PEER
311+
byte outHiBit[CURVE25519_KEYSIZE];
312+
word32 outHiBitLen = sizeof(out);
313+
#endif
310314
int endian = EC25519_BIG_ENDIAN;
311315

312316
ExpectIntEQ(wc_curve25519_init(&private_key), 0);
@@ -336,11 +340,25 @@ int test_wc_curve25519_shared_secret_ex(void)
336340
ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
337341
NULL, endian), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
338342

343+
#ifdef WOLFSSL_X25519_NO_MASK_PEER
339344
/* curve25519.c is checking for public_key size less than or equal to 0x7f,
340345
* increasing to 0x8f checks for error being returned */
341-
public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
346+
if (EXPECT_SUCCESS()) {
347+
public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
348+
}
342349
ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
343350
&outLen, endian), WC_NO_ERR_TRACE(ECC_BAD_ARG_E));
351+
#else
352+
ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
353+
&outLen, endian), 0);
354+
if (EXPECT_SUCCESS()) {
355+
public_key.p.point[CURVE25519_KEYSIZE-1] |= 0x80;
356+
}
357+
ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key,
358+
outHiBit, &outHiBitLen, endian), 0);
359+
ExpectIntEQ(outLen, outHiBitLen);
360+
ExpectBufEQ(out, outHiBit, outLen);
361+
#endif
344362

345363
outLen = outLen - 2;
346364
ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,

wolfcrypt/src/curve25519.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,12 @@ int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
710710
return ECC_BAD_ARG_E;
711711
}
712712

713+
#ifdef WOLFSSL_X25519_NO_MASK_PEER
713714
/* avoid implementation fingerprinting - make sure signed bit is not set */
714715
if (public_key->p.point[CURVE25519_KEYSIZE-1] & 0x80) {
715716
return ECC_BAD_ARG_E;
716717
}
718+
#endif
717719

718720
#ifdef WOLF_CRYPTO_CB
719721
if (private_key->devId != INVALID_DEVID) {
@@ -768,15 +770,24 @@ int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
768770
else
769771
#endif /* WOLFSSL_SE050 */
770772
{
773+
#ifdef WOLFSSL_X25519_NO_MASK_PEER
774+
byte* pubVal = public_key->p.point;
775+
#else
776+
byte pubVal[CURVE25519_KEYSIZE];
777+
778+
XMEMCPY(pubVal, public_key->p.point, CURVE25519_KEYSIZE);
779+
pubVal[CURVE25519_KEYSIZE-1] &= 0x7f;
780+
#endif
781+
771782
#ifndef WOLFSSL_CURVE25519_BLINDING
772783
SAVE_VECTOR_REGISTERS(return _svr_ret;);
773784

774-
ret = curve25519(o.point, private_key->k, public_key->p.point);
785+
ret = curve25519(o.point, private_key->k, pubVal);
775786

776787
RESTORE_VECTOR_REGISTERS();
777788
#else
778-
ret = curve25519_smul_blind(o.point, private_key->k,
779-
public_key->p.point, private_key->rng);
789+
ret = curve25519_smul_blind(o.point, private_key->k, pubVal,
790+
private_key->rng);
780791
#endif
781792
}
782793
#endif /* FREESCALE_LTC_ECC */

0 commit comments

Comments
 (0)