Skip to content

Commit 6d9a812

Browse files
committed
fix wp_ecx_get_security_bits threasholds so x25519 returns 128 and x448 returns 224
1 parent e37d979 commit 6d9a812

4 files changed

Lines changed: 85 additions & 2 deletions

File tree

src/wp_ecx_kmgmt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,10 @@ static int wp_ecx_get_security_bits(wp_Ecx* ecx)
511511
{
512512
int bits = 0;
513513

514-
if (ecx->data->bits >= 456) {
514+
if (ecx->data->bits >= 448) {
515515
bits = 224;
516516
}
517-
else if (ecx->data->bits >= 256) {
517+
else if (ecx->data->bits >= 255) {
518518
bits = 128;
519519
}
520520

test/test_ecx.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,3 +897,79 @@ int test_ecx_dup(void *data)
897897

898898
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD444) */
899899

900+
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_X448)
901+
902+
/*
903+
* Check that the correct security bits are provided for x25519 and x448
904+
*/
905+
int test_ecx_x_security_bits(void *data)
906+
{
907+
int err = 0;
908+
(void)data;
909+
910+
EVP_PKEY *pkey_ossl = NULL;
911+
EVP_PKEY *pkey_wolf = NULL;
912+
EVP_PKEY_CTX *ctx_ossl = NULL;
913+
EVP_PKEY_CTX *ctx_wolf = NULL;
914+
915+
struct {
916+
const char *name;
917+
} types[] = {
918+
#ifdef WP_HAVE_X25519
919+
{ "X25519" },
920+
#endif
921+
#ifdef WP_HAVE_X448
922+
{ "X448" },
923+
#endif
924+
};
925+
926+
for (unsigned i = 0; i < ARRAY_SIZE(types) && err == 0; i++) {
927+
if (err == 0) {
928+
ctx_ossl = EVP_PKEY_CTX_new_from_name(osslLibCtx, types[i].name,
929+
NULL);
930+
err = ctx_ossl == NULL;
931+
}
932+
if (err == 0) {
933+
ctx_wolf = EVP_PKEY_CTX_new_from_name(wpLibCtx, types[i].name,
934+
NULL);
935+
err = ctx_wolf == NULL;
936+
}
937+
if (err == 0) {
938+
err = EVP_PKEY_keygen_init(ctx_ossl) != 1;
939+
}
940+
if (err == 0) {
941+
err = EVP_PKEY_keygen_init(ctx_wolf) != 1;
942+
}
943+
if (err == 0) {
944+
pkey_ossl = NULL;
945+
err = EVP_PKEY_generate(ctx_ossl, &pkey_ossl) != 1;
946+
}
947+
if (err == 0) {
948+
pkey_wolf = NULL;
949+
err = EVP_PKEY_generate(ctx_wolf, &pkey_wolf) != 1;
950+
}
951+
if (err == 0) {
952+
int sec_ossl = EVP_PKEY_get_security_bits(pkey_ossl);
953+
int sec_wolf = EVP_PKEY_get_security_bits(pkey_wolf);
954+
if (sec_ossl != sec_wolf) {
955+
PRINT_MSG("EVP_PKEY_get_security_bits failed for %s:"
956+
" expected %d, got %d", types[i].name, sec_ossl, sec_wolf);
957+
err = 1;
958+
}
959+
}
960+
961+
EVP_PKEY_free(pkey_ossl);
962+
EVP_PKEY_free(pkey_wolf);
963+
EVP_PKEY_CTX_free(ctx_ossl);
964+
EVP_PKEY_CTX_free(ctx_wolf);
965+
pkey_ossl = NULL;
966+
pkey_wolf = NULL;
967+
ctx_ossl = NULL;
968+
ctx_wolf = NULL;
969+
}
970+
971+
return err;
972+
}
973+
974+
#endif /* defined(WP_HAVE_X25519) || defined(WP_HAVE_X448) */
975+

test/unit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ TEST_CASE test_case[] = {
472472
#endif
473473
TEST_DECL(test_ecx_dup, NULL),
474474
#endif
475+
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_X448)
476+
TEST_DECL(test_ecx_x_security_bits, NULL),
477+
#endif
475478

476479
TEST_DECL(test_pkcs7_x509_sign_verify, NULL),
477480
TEST_DECL(test_x509_cert, NULL),

test/unit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ int test_ecx_x25519_raw_priv_roundtrip(void *data);
470470
int test_ecx_dup(void *data);
471471
#endif
472472

473+
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_X448)
474+
int test_ecx_x_security_bits(void *data);
475+
#endif
476+
473477
int test_pkcs7_x509_sign_verify(void *data);
474478
int test_x509_cert(void *data);
475479

0 commit comments

Comments
 (0)