@@ -316,7 +316,7 @@ static int km_ecdsa_nist_p521_init(struct crypto_akcipher *tfm)
316316}
317317#endif /* HAVE_ECC521 */
318318
319- /**
319+ /*
320320 * Verify an ecdsa_nist signature.
321321 *
322322 * The total size of req->src is src_len + dst_len:
@@ -326,7 +326,7 @@ static int km_ecdsa_nist_p521_init(struct crypto_akcipher *tfm)
326326 * dst should be null.
327327 * See kernel:
328328 * - include/crypto/akcipher.h
329- * * /
329+ */
330330static int km_ecdsa_verify (struct akcipher_request * req )
331331{
332332 struct crypto_akcipher * tfm = NULL ;
@@ -358,26 +358,18 @@ static int km_ecdsa_verify(struct akcipher_request *req)
358358 goto ecdsa_verify_end ;
359359 }
360360
361- sig = malloc (sig_len );
361+ sig = malloc (sig_len + hash_len );
362362 if (unlikely (sig == NULL )) {
363363 err = - ENOMEM ;
364364 goto ecdsa_verify_end ;
365365 }
366366
367- hash = malloc (hash_len );
368- if (unlikely (hash == NULL )) {
369- err = - ENOMEM ;
370- goto ecdsa_verify_end ;
371- }
367+ hash = sig + sig_len ;
372368
373- memset (sig , 0 , sig_len );
374- memset (hash , 0 , hash_len );
369+ memset (sig , 0 , sig_len + hash_len );
375370
376- /* copy sig from req->src to sig */
377- scatterwalk_map_and_copy (sig , req -> src , 0 , sig_len , 0 );
378-
379- /* copy hash from req->src to hash */
380- scatterwalk_map_and_copy (hash , req -> src , sig_len , hash_len , 0 );
371+ /* copy sig and hash from req->src to sig and contiguous hash buffer. */
372+ scatterwalk_map_and_copy (sig , req -> src , 0 , sig_len + hash_len , 0 );
381373
382374 err = wc_ecc_verify_hash (sig , sig_len , hash , hash_len , & result , ctx -> key );
383375
@@ -401,7 +393,6 @@ static int km_ecdsa_verify(struct akcipher_request *req)
401393
402394ecdsa_verify_end :
403395 if (sig != NULL ) { free (sig ); sig = NULL ; }
404- if (hash != NULL ) { free (hash ); hash = NULL ; }
405396
406397 #ifdef WOLFKM_DEBUG_ECDSA
407398 pr_info ("info: exiting km_ecdsa_verify: %d\n" , result );
@@ -416,7 +407,7 @@ static int linuxkm_test_ecdsa_nist_p192(void)
416407 /* reference value from kernel crypto/testmgr.h
417408 * OID_id_ecdsa_with_sha256 */
418409 /* 49 byte pub key */
419- const byte p192_pub [] = {
410+ static const byte p192_pub [] = {
420411 0x04 , 0xe2 , 0x51 , 0x24 , 0x9b , 0xf7 , 0xb6 , 0x32 ,
421412 0x82 , 0x39 , 0x66 , 0x3d , 0x5b , 0xec , 0x3b , 0xae ,
422413 0x0c , 0xd5 , 0xf2 , 0x67 , 0xd1 , 0xc7 , 0xe1 , 0x02 ,
@@ -427,15 +418,15 @@ static int linuxkm_test_ecdsa_nist_p192(void)
427418 };
428419
429420 /* 32 byte hash */
430- const byte hash [] = {
421+ static const byte hash [] = {
431422 0x35 , 0xec , 0xa1 , 0xa0 , 0x9e , 0x14 , 0xde , 0x33 ,
432423 0x03 , 0xb6 , 0xf6 , 0xbd , 0x0c , 0x2f , 0xb2 , 0xfd ,
433424 0x1f , 0x27 , 0x82 , 0xa5 , 0xd7 , 0x70 , 0x3f , 0xef ,
434425 0xa0 , 0x82 , 0x69 , 0x8e , 0x73 , 0x31 , 0x8e , 0xd7
435426 };
436427
437428 /* 55 byte sig */
438- const byte sig [] = {
429+ static const byte sig [] = {
439430 0x30 , 0x35 , 0x02 , 0x18 , 0x3f , 0x72 , 0x3f , 0x1f ,
440431 0x42 , 0xd2 , 0x3f , 0x1d , 0x6b , 0x1a , 0x58 , 0x56 ,
441432 0xf1 , 0x8f , 0xf7 , 0xfd , 0x01 , 0x48 , 0xfb , 0x5f ,
@@ -467,7 +458,7 @@ static int linuxkm_test_ecdsa_nist_p256(void)
467458 /* reference value from kernel crypto/testmgr.h
468459 * OID_id_ecdsa_with_sha256 */
469460 /* 65 byte pub key */
470- const byte p256_pub [] = {
461+ static const byte p256_pub [] = {
471462 0x04 , 0xf1 , 0xea , 0xc4 , 0x53 , 0xf3 , 0xb9 , 0x0e ,
472463 0x9f , 0x7e , 0xad , 0xe3 , 0xea , 0xd7 , 0x0e , 0x0f ,
473464 0xd6 , 0x98 , 0x9a , 0xca , 0x92 , 0x4d , 0x0a , 0x80 ,
@@ -480,15 +471,15 @@ static int linuxkm_test_ecdsa_nist_p256(void)
480471 };
481472
482473 /* 32 byte hash */
483- const byte hash [] = {
474+ static const byte hash [] = {
484475 0x8f , 0x43 , 0x43 , 0x46 , 0x64 , 0x8f , 0x6b , 0x96 ,
485476 0xdf , 0x89 , 0xdd , 0xa9 , 0x01 , 0xc5 , 0x17 , 0x6b ,
486477 0x10 , 0xa6 , 0xd8 , 0x39 , 0x61 , 0xdd , 0x3c , 0x1a ,
487478 0xc8 , 0x8b , 0x59 , 0xb2 , 0xdc , 0x32 , 0x7a , 0xa4
488479 };
489480
490481 /* 71 byte sig */
491- const byte sig [] = {
482+ static const byte sig [] = {
492483 0x30 , 0x45 , 0x02 , 0x20 , 0x08 , 0x31 , 0xfa , 0x74 ,
493484 0x0d , 0x1d , 0x21 , 0x5d , 0x09 , 0xdc , 0x29 , 0x63 ,
494485 0xa8 , 0x1a , 0xad , 0xfc , 0xac , 0x44 , 0xc3 , 0xe8 ,
@@ -520,7 +511,7 @@ static int linuxkm_test_ecdsa_nist_p384(void)
520511 /* reference value from kernel crypto/testmgr.h
521512 * OID_id_ecdsa_with_sha384 */
522513 /* 97 byte pub key */
523- const byte p384_pub [] = {
514+ static const byte p384_pub [] = {
524515 0x04 , 0x3a , 0x2f , 0x62 , 0xe7 , 0x1a , 0xcf , 0x24 ,
525516 0xd0 , 0x0b , 0x7c , 0xe0 , 0xed , 0x46 , 0x0a , 0x4f ,
526517 0x74 , 0x16 , 0x43 , 0xe9 , 0x1a , 0x25 , 0x7c , 0x55 ,
@@ -537,7 +528,7 @@ static int linuxkm_test_ecdsa_nist_p384(void)
537528 };
538529
539530 /* 48 byte hash */
540- const byte hash [] = {
531+ static const byte hash [] = {
541532 0x8d , 0xf2 , 0xc0 , 0xe9 , 0xa8 , 0xf3 , 0x8e , 0x44 ,
542533 0xc4 , 0x8c , 0x1a , 0xa0 , 0xb8 , 0xd7 , 0x17 , 0xdf ,
543534 0xf2 , 0x37 , 0x1b , 0xc6 , 0xe3 , 0xf5 , 0x62 , 0xcc ,
@@ -547,7 +538,7 @@ static int linuxkm_test_ecdsa_nist_p384(void)
547538 };
548539
549540 /* 104 byte sig */
550- const byte sig [] = {
541+ static const byte sig [] = {
551542 0x30 , 0x66 , 0x02 , 0x31 , 0x00 , 0x9b , 0x28 , 0x68 ,
552543 0xc0 , 0xa1 , 0xea , 0x8c , 0x50 , 0xee , 0x2e , 0x62 ,
553544 0x35 , 0x46 , 0xfa , 0x00 , 0xd8 , 0x2d , 0x7a , 0x91 ,
@@ -584,7 +575,7 @@ static int linuxkm_test_ecdsa_nist_p521(void)
584575 /* reference value from kernel crypto/testmgr.h
585576 * OID_id_ecdsa_with_sha521 */
586577 /* 133 byte pub key */
587- const byte p521_pub [] = {
578+ static const byte p521_pub [] = {
588579 0x04 , 0x00 , 0xc7 , 0x65 , 0xee , 0x0b , 0x86 , 0x7d ,
589580 0x8f , 0x02 , 0xf1 , 0x74 , 0x5b , 0xb0 , 0x4c , 0x3f ,
590581 0xa6 , 0x35 , 0x60 , 0x9f , 0x55 , 0x23 , 0x11 , 0xcc ,
@@ -605,7 +596,7 @@ static int linuxkm_test_ecdsa_nist_p521(void)
605596 };
606597
607598 /* 64 byte hash */
608- const byte hash [] = {
599+ static const byte hash [] = {
609600 0x5c , 0xa6 , 0xbc , 0x79 , 0xb8 , 0xa0 , 0x1e , 0x11 ,
610601 0x83 , 0xf7 , 0xe9 , 0x05 , 0xdf , 0xba , 0xf7 , 0x69 ,
611602 0x97 , 0x22 , 0x32 , 0xe4 , 0x94 , 0x7c , 0x65 , 0xbd ,
@@ -617,7 +608,7 @@ static int linuxkm_test_ecdsa_nist_p521(void)
617608 };
618609
619610 /* 139 byte sig */
620- const byte sig [] = {
611+ static const byte sig [] = {
621612 0x30 , 0x81 , 0x88 , 0x02 , 0x42 , 0x01 , 0x5c , 0x71 ,
622613 0x86 , 0x96 , 0xac , 0x21 , 0x33 , 0x7e , 0x4e , 0xaa ,
623614 0x86 , 0xec , 0xa8 , 0x05 , 0x03 , 0x52 , 0x56 , 0x63 ,
@@ -664,11 +655,26 @@ static int linuxkm_test_ecdsa_nist_driver(const char * driver,
664655 struct crypto_akcipher * tfm = NULL ;
665656 struct akcipher_request * req = NULL ;
666657 struct scatterlist src_tab [2 ];
658+ byte * param_copy = NULL ;
667659 byte * bad_sig = NULL ;
668- /**
660+
661+ /* Allocate param_copy -- scatterwalk_map_and_copy() unmaps the buffers in
662+ * the sg list, so we can't safely use the passed pointers directly.
663+ */
664+ param_copy = (byte * )malloc (sig_len + hash_len );
665+ if (! param_copy ) {
666+ pr_err ("error: allocating param_copy buffer failed.\n" );
667+ goto test_ecdsa_nist_end ;
668+ }
669+ memcpy (param_copy , sig , sig_len );
670+ sig = param_copy ;
671+ memcpy (param_copy + sig_len , hash , hash_len );
672+ hash = param_copy + sig_len ;
673+
674+ /*
669675 * Allocate the akcipher transform, and set up
670676 * the akcipher request.
671- * * /
677+ */
672678 tfm = crypto_alloc_akcipher (driver , 0 , 0 );
673679 if (IS_ERR (tfm )) {
674680 pr_err ("error: allocating akcipher algorithm %s failed: %ld\n" ,
@@ -699,14 +705,15 @@ static int linuxkm_test_ecdsa_nist_driver(const char * driver,
699705 }
700706 }
701707
702- /**
708+ /*
703709 * Set sig as src, and null as dst.
704710 * src_tab is:
705711 * src_tab[0]: signature
706712 * src_tab[1]: message (hash)
707713 *
708714 * src_len is sig size
709- * dst_len is hash size. */
715+ * dst_len is hash size.
716+ */
710717 sg_init_table (src_tab , 2 );
711718 sg_set_buf (& src_tab [0 ], sig , sig_len );
712719 sg_set_buf (& src_tab [1 ], hash , hash_len );
@@ -747,6 +754,7 @@ static int linuxkm_test_ecdsa_nist_driver(const char * driver,
747754test_ecdsa_nist_end :
748755 if (req ) { akcipher_request_free (req ); req = NULL ; }
749756 if (tfm ) { crypto_free_akcipher (tfm ); tfm = NULL ; }
757+ if (param_copy ) { free (param_copy ); }
750758 if (bad_sig ) { free (bad_sig ); bad_sig = NULL ; }
751759
752760 #ifdef WOLFKM_DEBUG_ECDSA
0 commit comments