Skip to content

Commit 316724b

Browse files
authored
Merge pull request #417 from gasbytes/wp-ecx-get-security-bits-fix
2 parents a2d0f4f + 4899f7f commit 316724b

4 files changed

Lines changed: 103 additions & 9 deletions

File tree

src/wp_ecx_kmgmt.c

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

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

523527
return bits;

test/test_ecx.c

Lines changed: 87 additions & 4 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
@@ -656,7 +656,6 @@ static int test_ecx_null_sign_init_ex(OSSL_LIB_CTX *libCtx)
656656

657657
#ifndef WP_HAVE_ED25519
658658
(void)libCtx;
659-
(void)provider_name;
660659
PRINT_MSG("Skipping test - WP_HAVE_ED25519 not defined");
661660
return 0;
662661
#endif
@@ -895,5 +894,89 @@ int test_ecx_dup(void *data)
895894
return err;
896895
}
897896

898-
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD444) */
897+
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ED448) */
898+
899+
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_X448)
900+
901+
/*
902+
* Check that the correct security bits are provided for x25519 and x448
903+
*/
904+
int test_ecx_x_security_bits(void *data)
905+
{
906+
int err = 0;
907+
(void)data;
908+
909+
EVP_PKEY *pkey_ossl = NULL;
910+
EVP_PKEY *pkey_wolf = NULL;
911+
EVP_PKEY_CTX *ctx_ossl = NULL;
912+
EVP_PKEY_CTX *ctx_wolf = NULL;
913+
914+
struct {
915+
const char *name;
916+
int expectedBits;
917+
} types[] = {
918+
#ifdef WP_HAVE_X25519
919+
{ "X25519", 128 },
920+
#endif
921+
#ifdef WP_HAVE_X448
922+
{ "X448", 224 },
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 mismatch for %s:"
956+
" OpenSSL %d, wolfProvider %d", types[i].name, sec_ossl,
957+
sec_wolf);
958+
err = 1;
959+
}
960+
else if (sec_wolf != types[i].expectedBits) {
961+
PRINT_MSG("EVP_PKEY_get_security_bits failed for %s:"
962+
" expected %d, got %d", types[i].name,
963+
types[i].expectedBits, sec_wolf);
964+
err = 1;
965+
}
966+
}
967+
968+
EVP_PKEY_free(pkey_ossl);
969+
EVP_PKEY_free(pkey_wolf);
970+
EVP_PKEY_CTX_free(ctx_ossl);
971+
EVP_PKEY_CTX_free(ctx_wolf);
972+
pkey_ossl = NULL;
973+
pkey_wolf = NULL;
974+
ctx_ossl = NULL;
975+
ctx_wolf = NULL;
976+
}
977+
978+
return err;
979+
}
980+
981+
#endif /* defined(WP_HAVE_X25519) || defined(WP_HAVE_X448) */
899982

test/unit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ TEST_CASE test_case[] = {
486486
#endif
487487
TEST_DECL(test_ecx_dup, NULL),
488488
#endif
489+
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_X448)
490+
TEST_DECL(test_ecx_x_security_bits, NULL),
491+
#endif
489492

490493
TEST_DECL(test_pkcs7_x509_sign_verify, NULL),
491494
TEST_DECL(test_x509_cert, NULL),

test/unit.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,10 @@ int test_ecx_x25519_raw_priv_roundtrip(void *data);
484484
int test_ecx_dup(void *data);
485485
#endif
486486

487+
#if defined(WP_HAVE_X25519) || defined(WP_HAVE_X448)
488+
int test_ecx_x_security_bits(void *data);
489+
#endif
490+
487491
int test_pkcs7_x509_sign_verify(void *data);
488492
int test_x509_cert(void *data);
489493

0 commit comments

Comments
 (0)