Skip to content

Commit 3b27018

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

4 files changed

Lines changed: 103 additions & 8 deletions

File tree

src/wp_ecx_kmgmt.c

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

514-
if (ecx->data->bits >= 456) {
515-
bits = 224;
516-
}
517-
else if (ecx->data->bits >= 256) {
518-
bits = 128;
514+
switch (ecx->data->keyType) {
515+
case WP_KEY_TYPE_X448:
516+
case WP_KEY_TYPE_ED448:
517+
bits = 224;
518+
break;
519+
case WP_KEY_TYPE_X25519:
520+
case WP_KEY_TYPE_ED25519:
521+
bits = 128;
522+
break;
519523
}
520524

521525
return bits;

test/test_ecx.c

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
#include <wolfssl/wolfcrypt/ed25519.h>
2727
#include <wolfssl/wolfcrypt/ed448.h>
2828

29-
#if defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD448)
30-
3129
#ifndef ARRAY_SIZE
3230
#define ARRAY_SIZE(a) ((sizeof(a)/sizeof(a[0])))
3331
#endif
3432

33+
#if defined(WP_HAVE_ED25519) || defined(WP_HAVE_ED448)
34+
3535
#ifndef MAX
3636
#define MAX(a,b) ((a) > (b) ? (a) : (b))
3737
#endif
@@ -895,5 +895,89 @@ int test_ecx_dup(void *data)
895895
return err;
896896
}
897897

898-
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD444) */
898+
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ED448) */
899+
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+
int expectedBits;
918+
} types[] = {
919+
#ifdef WP_HAVE_X25519
920+
{ "X25519", 128 },
921+
#endif
922+
#ifdef WP_HAVE_X448
923+
{ "X448", 224 },
924+
#endif
925+
};
926+
927+
for (unsigned i = 0; i < ARRAY_SIZE(types) && err == 0; i++) {
928+
if (err == 0) {
929+
ctx_ossl = EVP_PKEY_CTX_new_from_name(osslLibCtx, types[i].name,
930+
NULL);
931+
err = ctx_ossl == NULL;
932+
}
933+
if (err == 0) {
934+
ctx_wolf = EVP_PKEY_CTX_new_from_name(wpLibCtx, types[i].name,
935+
NULL);
936+
err = ctx_wolf == NULL;
937+
}
938+
if (err == 0) {
939+
err = EVP_PKEY_keygen_init(ctx_ossl) != 1;
940+
}
941+
if (err == 0) {
942+
err = EVP_PKEY_keygen_init(ctx_wolf) != 1;
943+
}
944+
if (err == 0) {
945+
pkey_ossl = NULL;
946+
err = EVP_PKEY_generate(ctx_ossl, &pkey_ossl) != 1;
947+
}
948+
if (err == 0) {
949+
pkey_wolf = NULL;
950+
err = EVP_PKEY_generate(ctx_wolf, &pkey_wolf) != 1;
951+
}
952+
if (err == 0) {
953+
int sec_ossl = EVP_PKEY_get_security_bits(pkey_ossl);
954+
int sec_wolf = EVP_PKEY_get_security_bits(pkey_wolf);
955+
if (sec_ossl != sec_wolf) {
956+
PRINT_MSG("EVP_PKEY_get_security_bits mismatch for %s:"
957+
" OpenSSL %d, wolfProvider %d", types[i].name, sec_ossl,
958+
sec_wolf);
959+
err = 1;
960+
}
961+
else if (sec_wolf != types[i].expectedBits) {
962+
PRINT_MSG("EVP_PKEY_get_security_bits failed for %s:"
963+
" expected %d, got %d", types[i].name,
964+
types[i].expectedBits, sec_wolf);
965+
err = 1;
966+
}
967+
}
968+
969+
EVP_PKEY_free(pkey_ossl);
970+
EVP_PKEY_free(pkey_wolf);
971+
EVP_PKEY_CTX_free(ctx_ossl);
972+
EVP_PKEY_CTX_free(ctx_wolf);
973+
pkey_ossl = NULL;
974+
pkey_wolf = NULL;
975+
ctx_ossl = NULL;
976+
ctx_wolf = NULL;
977+
}
978+
979+
return err;
980+
}
981+
982+
#endif /* defined(WP_HAVE_X25519) || defined(WP_HAVE_X448) */
899983

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)