23
23
#include <openssl/store.h>
24
24
#include <openssl/core_names.h>
25
25
#include <openssl/param_build.h>
26
+ #include <wolfssl/wolfcrypt/ed25519.h>
27
+ #include <wolfssl/wolfcrypt/ed448.h>
26
28
27
29
#if defined(WP_HAVE_ED25519 ) || defined(WP_HAVE_ECD448 )
28
30
29
31
#ifndef ARRAY_SIZE
30
32
#define ARRAY_SIZE (a ) ((sizeof(a)/sizeof(a[0])))
31
33
#endif
32
34
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))
39
37
#endif
40
38
41
39
#ifdef WP_HAVE_ED25519
@@ -145,10 +143,10 @@ int test_ecx_sign_verify(void *data)
145
143
const unsigned char * p ;
146
144
147
145
#ifdef WP_HAVE_ED25519
148
- unsigned char sig_ed25519 [ED25519_SIGSIZE ];
146
+ unsigned char sig_ed25519 [ED25519_SIG_SIZE ];
149
147
#endif
150
148
#ifdef WP_HAVE_ED448
151
- unsigned char sig_ed448 [ED448_SIGSIZE ];
149
+ unsigned char sig_ed448 [ED448_SIG_SIZE ];
152
150
#endif
153
151
154
152
(void )data ;
@@ -179,7 +177,8 @@ int test_ecx_sign_verify(void *data)
179
177
p = types [i ].key ;
180
178
181
179
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 );
183
182
err = pkey == NULL ;
184
183
if (err ) {
185
184
PRINT_MSG ("could not create key" );
@@ -203,12 +202,14 @@ int test_ecx_sign_verify_raw_priv(void *data)
203
202
204
203
EVP_PKEY * pkey_ossl = NULL ;
205
204
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 )];
206
207
207
208
#ifdef WP_HAVE_ED25519
208
- unsigned char sig_ed25519 [ED25519_SIGSIZE ];
209
+ unsigned char sig_ed25519 [ED25519_SIG_SIZE ];
209
210
#endif
210
211
#ifdef WP_HAVE_ED448
211
- unsigned char sig_ed448 [ED448_SIGSIZE ];
212
+ unsigned char sig_ed448 [ED448_SIG_SIZE ];
212
213
#endif
213
214
214
215
struct {
@@ -233,6 +234,7 @@ int test_ecx_sign_verify_raw_priv(void *data)
233
234
PRINT_MSG ("Testing ECX sign/verify with raw keys (%s)" ,
234
235
types [i ].name );
235
236
237
+ /* Create private keys from the byte arrays */
236
238
if (err == 0 ) {
237
239
pkey_ossl = EVP_PKEY_new_raw_private_key_ex (osslLibCtx ,
238
240
types [i ].name , NULL , types [i ].key , types [i ].keyLen );
@@ -251,6 +253,7 @@ int test_ecx_sign_verify_raw_priv(void *data)
251
253
}
252
254
}
253
255
256
+ /* Compare keys */
254
257
if (err == 0 ) {
255
258
if (EVP_PKEY_cmp (pkey_wolf , pkey_ossl ) != 1 ) {
256
259
PRINT_MSG ("EVP_PKEY_cmp failed" );
@@ -262,6 +265,48 @@ int test_ecx_sign_verify_raw_priv(void *data)
262
265
}
263
266
}
264
267
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
+
265
310
if (err == 0 ) {
266
311
err = sign_verify (types [i ].sig , types [i ].sigLen , pkey_ossl ,
267
312
types [i ].name );
@@ -290,12 +335,14 @@ int test_ecx_sign_verify_raw_pub(void *data)
290
335
const unsigned char * p = NULL ;
291
336
unsigned char buf [128 ];
292
337
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 )];
293
340
294
341
#ifdef WP_HAVE_ED25519
295
- unsigned char sig_ed25519 [ED25519_SIGSIZE ];
342
+ unsigned char sig_ed25519 [ED25519_SIG_SIZE ];
296
343
#endif
297
344
#ifdef WP_HAVE_ED448
298
- unsigned char sig_ed448 [ED448_SIGSIZE ];
345
+ unsigned char sig_ed448 [ED448_SIG_SIZE ];
299
346
#endif
300
347
301
348
struct {
@@ -341,7 +388,8 @@ int test_ecx_sign_verify_raw_pub(void *data)
341
388
}
342
389
}
343
390
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 */
345
393
if (err == 0 ) {
346
394
PRINT_MSG ("Sign with OpenSSL (%s)" , types [i ].name );
347
395
err = test_digest_sign (pkey_der , osslLibCtx , buf , bufLen , NULL ,
@@ -379,6 +427,48 @@ int test_ecx_sign_verify_raw_pub(void *data)
379
427
}
380
428
}
381
429
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
+
382
472
/* Verify the signature with the public keys */
383
473
if (err == 0 ) {
384
474
PRINT_MSG ("Verify with OpenSSL (%s)" , types [i ].name );
@@ -412,4 +502,128 @@ int test_ecx_sign_verify_raw_pub(void *data)
412
502
return err ;
413
503
}
414
504
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
+
415
629
#endif /* defined(WP_HAVE_ED25519) || defined(WP_HAVE_ECD444) */
0 commit comments