Skip to content

Commit 640959e

Browse files
authored
Merge pull request #130 from padelsbach/wp_more_ecx_tests
Add more ECX tests, fix minor issues
2 parents 3f71aed + 1ff3b1e commit 640959e

File tree

4 files changed

+241
-22
lines changed

4 files changed

+241
-22
lines changed

src/wp_ecx_kmgmt.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ typedef int (*WP_ECX_CHECK_KEY)(void* key);
9797
typedef struct wp_EcxData {
9898
/** Type of key. */
9999
int keyType;
100-
/** Number of bits in curve. */
100+
/** Cryptographic length in bits. */
101101
int bits;
102102
/** Length of curve in bytes. */
103103
int len;
@@ -193,6 +193,7 @@ typedef struct wp_EcxGenCtx {
193193

194194
/* Prototype for ECX generation initialization. */
195195
static int wp_ecx_gen_set_params(wp_EcxGenCtx* ctx, const OSSL_PARAM params[]);
196+
static size_t wp_ecx_export_keypair_alloc_size(wp_Ecx* ecx, int priv);
196197

197198
/*
198199
* ECX key
@@ -455,10 +456,10 @@ static int wp_ecx_get_security_bits(wp_Ecx* ecx)
455456
{
456457
int bits = 0;
457458

458-
if (ecx->data->bits >= 448) {
459-
bits = 192;
459+
if (ecx->data->bits >= 456) {
460+
bits = 224;
460461
}
461-
else if (ecx->data->bits >= 255) {
462+
else if (ecx->data->bits >= 256) {
462463
bits = 128;
463464
}
464465

@@ -548,8 +549,10 @@ static int wp_ecx_get_params(wp_Ecx* ecx, OSSL_PARAM params[])
548549
OSSL_PARAM* p;
549550

550551
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE);
551-
if ((p != NULL) && !OSSL_PARAM_set_int(p, ecx->data->len)) {
552-
ok = 0;
552+
if (p != NULL) {
553+
if (!OSSL_PARAM_set_int(p, (int)wp_ecx_export_keypair_alloc_size(ecx, 1))) {
554+
ok = 0;
555+
}
553556
}
554557
if (ok) {
555558
p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS);
@@ -1540,7 +1543,7 @@ static int wp_ed25519_import_private(const byte* in, word32 inLen,
15401543
/** Ed25519 data and wolfSSL functions. */
15411544
static const wp_EcxData ed25519Data = {
15421545
WP_KEY_TYPE_ED25519,
1543-
255,
1546+
ED25519_KEY_SIZE * 8,
15441547
ED25519_KEY_SIZE,
15451548

15461549
(WP_ECX_INIT)&wc_ed25519_init,
@@ -1674,7 +1677,7 @@ static int wp_ed448_import_private(const byte* in, word32 inLen,
16741677
/** Ed448 data and wolfSSL functions. */
16751678
static const wp_EcxData ed448Data = {
16761679
WP_KEY_TYPE_ED448,
1677-
448,
1680+
ED448_KEY_SIZE * 8,
16781681
ED448_KEY_SIZE,
16791682

16801683
(WP_ECX_INIT)&wc_ed448_init,

test/test_ecx.c

Lines changed: 228 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@
2323
#include <openssl/store.h>
2424
#include <openssl/core_names.h>
2525
#include <openssl/param_build.h>
26+
#include <wolfssl/wolfcrypt/ed25519.h>
27+
#include <wolfssl/wolfcrypt/ed448.h>
2628

2729
#if defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD448)
2830

2931
#ifndef ARRAY_SIZE
3032
#define ARRAY_SIZE(a) ((sizeof(a)/sizeof(a[0])))
3133
#endif
3234

33-
#ifndef ED25519_SIGSIZE
34-
#define ED25519_SIGSIZE 64
35-
#endif
36-
37-
#ifndef ED448_SIGSIZE
38-
#define ED448_SIGSIZE 114
35+
#ifndef MAX
36+
#define MAX(a,b) ((a) > (b) ? (a) : (b))
3937
#endif
4038

4139
#ifdef WP_HAVE_ED25519
@@ -145,10 +143,10 @@ int test_ecx_sign_verify(void *data)
145143
const unsigned char *p;
146144

147145
#ifdef WP_HAVE_ED25519
148-
unsigned char sig_ed25519[ED25519_SIGSIZE];
146+
unsigned char sig_ed25519[ED25519_SIG_SIZE];
149147
#endif
150148
#ifdef WP_HAVE_ED448
151-
unsigned char sig_ed448[ED448_SIGSIZE];
149+
unsigned char sig_ed448[ED448_SIG_SIZE];
152150
#endif
153151

154152
(void)data;
@@ -179,7 +177,8 @@ int test_ecx_sign_verify(void *data)
179177
p = types[i].key;
180178

181179
if (err == 0) {
182-
pkey = d2i_PrivateKey(types[i].type, NULL, &p, types[i].keyLen);
180+
pkey = d2i_PrivateKey_ex(types[i].type, NULL, &p, types[i].keyLen,
181+
wpLibCtx, NULL);
183182
err = pkey == NULL;
184183
if (err) {
185184
PRINT_MSG("could not create key");
@@ -203,12 +202,14 @@ int test_ecx_sign_verify_raw_priv(void *data)
203202

204203
EVP_PKEY *pkey_ossl = NULL;
205204
EVP_PKEY *pkey_wolf = NULL;
205+
unsigned char readback_ossl[MAX(ED25519_KEY_SIZE, ED448_KEY_SIZE)];
206+
unsigned char readback_wolf[MAX(ED25519_KEY_SIZE, ED448_KEY_SIZE)];
206207

207208
#ifdef WP_HAVE_ED25519
208-
unsigned char sig_ed25519[ED25519_SIGSIZE];
209+
unsigned char sig_ed25519[ED25519_SIG_SIZE];
209210
#endif
210211
#ifdef WP_HAVE_ED448
211-
unsigned char sig_ed448[ED448_SIGSIZE];
212+
unsigned char sig_ed448[ED448_SIG_SIZE];
212213
#endif
213214

214215
struct {
@@ -233,6 +234,7 @@ int test_ecx_sign_verify_raw_priv(void *data)
233234
PRINT_MSG("Testing ECX sign/verify with raw keys (%s)",
234235
types[i].name);
235236

237+
/* Create private keys from the byte arrays */
236238
if (err == 0) {
237239
pkey_ossl = EVP_PKEY_new_raw_private_key_ex(osslLibCtx,
238240
types[i].name, NULL, types[i].key, types[i].keyLen);
@@ -251,6 +253,7 @@ int test_ecx_sign_verify_raw_priv(void *data)
251253
}
252254
}
253255

256+
/* Compare keys */
254257
if (err == 0) {
255258
if (EVP_PKEY_cmp(pkey_wolf, pkey_ossl) != 1) {
256259
PRINT_MSG("EVP_PKEY_cmp failed");
@@ -262,6 +265,48 @@ int test_ecx_sign_verify_raw_priv(void *data)
262265
}
263266
}
264267

268+
/* Verify readback of the key */
269+
if (err == 0) {
270+
PRINT_MSG("Verify readback of private key with OpenSSL (%s)", types[i].name);
271+
size_t readbackLen_ossl = types[i].keyLen;
272+
/* EVP_PKEY_get_raw_private_key returns 1 for success */
273+
err = EVP_PKEY_get_raw_private_key(pkey_ossl, readback_ossl, &readbackLen_ossl);
274+
err = err != 1;
275+
if (err) {
276+
PRINT_MSG("EVP_PKEY_get_raw_private_key failed");
277+
err = 1;
278+
}
279+
if (readbackLen_ossl != types[i].keyLen) {
280+
PRINT_MSG("EVP_PKEY_get_raw_private_key length mismatch");
281+
err = 1;
282+
}
283+
if (memcmp(readback_ossl, types[i].key, readbackLen_ossl) != 0) {
284+
PRINT_MSG("EVP_PKEY_get_raw_private_key data mismatch");
285+
err = 1;
286+
}
287+
}
288+
289+
/* Compare key with WolfSSL */
290+
if (err == 0) {
291+
PRINT_MSG("Verify readback of private key with WolfSSL (%s)", types[i].name);
292+
size_t readbackLen_wolf = types[i].keyLen;
293+
/* EVP_PKEY_get_raw_private_key returns 1 for success */
294+
err = EVP_PKEY_get_raw_private_key(pkey_wolf, readback_wolf, &readbackLen_wolf);
295+
err = err != 1;
296+
if (err) {
297+
PRINT_MSG("EVP_PKEY_get_raw_private_key failed");
298+
err = 1;
299+
}
300+
if (readbackLen_wolf != types[i].keyLen) {
301+
PRINT_MSG("EVP_PKEY_get_raw_private_key length mismatch");
302+
err = 1;
303+
}
304+
if (memcmp(readback_wolf, types[i].key, readbackLen_wolf) != 0) {
305+
PRINT_MSG("EVP_PKEY_get_raw_private_key data mismatch");
306+
err = 1;
307+
}
308+
}
309+
265310
if (err == 0) {
266311
err = sign_verify(types[i].sig, types[i].sigLen, pkey_ossl,
267312
types[i].name);
@@ -290,12 +335,14 @@ int test_ecx_sign_verify_raw_pub(void *data)
290335
const unsigned char *p = NULL;
291336
unsigned char buf[128];
292337
size_t bufLen = 0;
338+
unsigned char readback_ossl[MAX(ED25519_KEY_SIZE, ED448_KEY_SIZE)];
339+
unsigned char readback_wolf[MAX(ED25519_KEY_SIZE, ED448_KEY_SIZE)];
293340

294341
#ifdef WP_HAVE_ED25519
295-
unsigned char sig_ed25519[ED25519_SIGSIZE];
342+
unsigned char sig_ed25519[ED25519_SIG_SIZE];
296343
#endif
297344
#ifdef WP_HAVE_ED448
298-
unsigned char sig_ed448[ED448_SIGSIZE];
345+
unsigned char sig_ed448[ED448_SIG_SIZE];
299346
#endif
300347

301348
struct {
@@ -341,7 +388,8 @@ int test_ecx_sign_verify_raw_pub(void *data)
341388
}
342389
}
343390

344-
/* Use OpenSSL to sign the block of random bytes */
391+
/* Use OpenSSL to sign the block of random bytes. We will use this
392+
* signature to verify with the public key */
345393
if (err == 0) {
346394
PRINT_MSG("Sign with OpenSSL (%s)", types[i].name);
347395
err = test_digest_sign(pkey_der, osslLibCtx, buf, bufLen, NULL,
@@ -379,6 +427,48 @@ int test_ecx_sign_verify_raw_pub(void *data)
379427
}
380428
}
381429

430+
/* Verify readback of the key with OpenSSL */
431+
if (err == 0) {
432+
PRINT_MSG("Verify readback of public key with OpenSSL (%s)", types[i].name);
433+
size_t readbackLen_ossl = types[i].pubKeyLen;
434+
/* EVP_PKEY_get_raw_public_key returns 1 for success */
435+
err = EVP_PKEY_get_raw_public_key(pkey_ossl, readback_ossl, &readbackLen_ossl);
436+
err = err != 1;
437+
if (err) {
438+
PRINT_MSG("EVP_PKEY_get_raw_public_key failed");
439+
err = 1;
440+
}
441+
if (readbackLen_ossl != types[i].pubKeyLen) {
442+
PRINT_MSG("EVP_PKEY_get_raw_public_key length mismatch");
443+
err = 1;
444+
}
445+
if (memcmp(readback_ossl, types[i].pubKey, readbackLen_ossl) != 0) {
446+
PRINT_MSG("EVP_PKEY_get_raw_public_key data mismatch");
447+
err = 1;
448+
}
449+
}
450+
451+
/* Verify readback of the key with WolfSSL */
452+
if (err == 0) {
453+
PRINT_MSG("Verify readback of public key with WolfSSL (%s)", types[i].name);
454+
size_t readbackLen_wolf = types[i].pubKeyLen;
455+
/* EVP_PKEY_get_raw_public_key returns 1 for success */
456+
err = EVP_PKEY_get_raw_public_key(pkey_wolf, readback_wolf, &readbackLen_wolf);
457+
err = err != 1;
458+
if (err) {
459+
PRINT_MSG("EVP_PKEY_get_raw_public_key failed");
460+
err = 1;
461+
}
462+
if (readbackLen_wolf != types[i].pubKeyLen) {
463+
PRINT_MSG("EVP_PKEY_get_raw_public_key length mismatch");
464+
err = 1;
465+
}
466+
if (memcmp(readback_wolf, types[i].pubKey, readbackLen_wolf) != 0) {
467+
PRINT_MSG("EVP_PKEY_get_raw_public_key data mismatch");
468+
err = 1;
469+
}
470+
}
471+
382472
/* Verify the signature with the public keys */
383473
if (err == 0) {
384474
PRINT_MSG("Verify with OpenSSL (%s)", types[i].name);
@@ -412,4 +502,128 @@ int test_ecx_sign_verify_raw_pub(void *data)
412502
return err;
413503
}
414504

505+
int test_ecx_misc(void *data)
506+
{
507+
int err = 0;
508+
(void)data;
509+
510+
EVP_PKEY *pkey_ossl = NULL;
511+
EVP_PKEY *pkey_wolf = NULL;
512+
EVP_PKEY_CTX *ctx_ossl = NULL;
513+
EVP_PKEY_CTX *ctx_wolf = NULL;
514+
515+
struct {
516+
int type;
517+
const char* name;
518+
} types[] = {
519+
#ifdef WP_HAVE_ED25519
520+
{ EVP_PKEY_ED25519, "ED25519" },
521+
#endif
522+
#ifdef WP_HAVE_ED448
523+
{ EVP_PKEY_ED448, "ED448" },
524+
#endif
525+
};
526+
527+
for (unsigned i = 0; i < ARRAY_SIZE(types) && err == 0; i++) {
528+
/* Generate context */
529+
if (err == 0) {
530+
ctx_ossl = EVP_PKEY_CTX_new_from_name(osslLibCtx, types[i].name, NULL);
531+
err = ctx_ossl == NULL;
532+
}
533+
if (err == 0) {
534+
ctx_wolf = EVP_PKEY_CTX_new_from_name(wpLibCtx, types[i].name, NULL);
535+
err = ctx_wolf == NULL;
536+
}
537+
538+
/* Init context */
539+
if (err == 0) {
540+
err = EVP_PKEY_keygen_init(ctx_ossl);
541+
err = err != 1;
542+
}
543+
if (err == 0) {
544+
err = EVP_PKEY_keygen_init(ctx_wolf);
545+
err = err != 1;
546+
}
547+
548+
/* Generate keys */
549+
if (err == 0) {
550+
pkey_ossl = NULL;
551+
err = EVP_PKEY_generate(ctx_ossl, &pkey_ossl);
552+
err = err != 1;
553+
}
554+
if (err == 0) {
555+
pkey_wolf = NULL;
556+
err = EVP_PKEY_generate(ctx_wolf, &pkey_wolf);
557+
err = err != 1;
558+
}
559+
560+
/* Compare various util routines */
561+
if (err == 0) {
562+
int id_ossl = EVP_PKEY_get_id(pkey_ossl);
563+
int id_wolf = EVP_PKEY_get_id(pkey_wolf);
564+
if (id_ossl != id_wolf) {
565+
PRINT_MSG("EVP_PKEY_get_id failed %d %d", id_ossl, id_wolf);
566+
err = 1;
567+
}
568+
}
569+
570+
if (err == 0) {
571+
int base_id_ossl = EVP_PKEY_get_base_id(pkey_ossl);
572+
int base_id_wolf = EVP_PKEY_get_base_id(pkey_wolf);
573+
if (base_id_ossl != base_id_wolf) {
574+
PRINT_MSG("EVP_PKEY_get_base_id failed %d %d",
575+
base_id_ossl, base_id_wolf);
576+
err = 1;
577+
}
578+
}
579+
580+
if (err == 0) {
581+
int bits_ossl = EVP_PKEY_get_bits(pkey_ossl);
582+
int bits_wolf = EVP_PKEY_get_bits(pkey_wolf);
583+
if (bits_ossl != bits_wolf) {
584+
PRINT_MSG("EVP_PKEY_get_bits failed %d %d",
585+
bits_ossl, bits_wolf);
586+
err = 1;
587+
}
588+
}
589+
590+
if (err == 0) {
591+
int sec_ossl = EVP_PKEY_get_security_bits(pkey_ossl);
592+
int sec_wolf = EVP_PKEY_get_security_bits(pkey_wolf);
593+
if (sec_ossl != sec_wolf) {
594+
PRINT_MSG("EVP_PKEY_get_security_bits failed %d %d",
595+
sec_ossl, sec_wolf);
596+
err = 1;
597+
}
598+
}
599+
600+
if (err == 0) {
601+
int size_ossl = EVP_PKEY_get_size(pkey_ossl);
602+
int size_wolf = EVP_PKEY_get_size(pkey_wolf);
603+
if (size_ossl != size_wolf) {
604+
PRINT_MSG("EVP_PKEY_get_size failed %d %d",
605+
size_ossl, size_wolf);
606+
err = 1;
607+
}
608+
}
609+
610+
if (err == 0) {
611+
int sign_ossl = EVP_PKEY_can_sign(pkey_ossl);
612+
int sign_wolf = EVP_PKEY_can_sign(pkey_wolf);
613+
if (sign_ossl != sign_wolf) {
614+
PRINT_MSG("EVP_PKEY_can_sign failed %d %d",
615+
sign_ossl, sign_wolf);
616+
err = 1;
617+
}
618+
}
619+
620+
EVP_PKEY_free(pkey_ossl);
621+
EVP_PKEY_free(pkey_wolf);
622+
EVP_PKEY_CTX_free(ctx_ossl);
623+
EVP_PKEY_CTX_free(ctx_wolf);
624+
}
625+
626+
return err;
627+
}
628+
415629
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD444) */

0 commit comments

Comments
 (0)