Skip to content

Commit 3cdc521

Browse files
authored
Merge pull request #8696 from douzzer/20250418-linuxkm-lkcapi-cleanup
20250418-linuxkm-lkcapi-cleanup
2 parents a21542c + 2e31cad commit 3cdc521

6 files changed

Lines changed: 163 additions & 138 deletions

File tree

linuxkm/lkcapi_ecdh_glue.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ static struct kpp_alg ecdh_nist_p384 = {
133133
.exit = km_ecdh_exit,
134134
};
135135

136-
/**
136+
/*
137137
* Set the secret. Kernel crypto expects secret is passed with
138138
* struct kpp_secret as header, followed by secret data as payload.
139139
* See these for more info:
140140
* - crypto/ecdh_helper.c
141141
* - include/crypto/kpp.h
142142
*
143143
* An empty payload means this function will gen the ecc key pair.
144-
* */
144+
*/
145145
static int km_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
146146
unsigned int len)
147147
{
@@ -360,12 +360,12 @@ static int km_ecdh_nist_p384_init(struct crypto_kpp *tfm)
360360
return km_ecdh_init(tfm, ECC_SECP384R1);
361361
}
362362

363-
/**
363+
/*
364364
* Generate the ecc public key:
365365
* - req->src should be null
366366
* - req->dst is where we place the public key.
367367
* The kernel api expects raw uncompressed pub key, without leading byte.
368-
* */
368+
*/
369369
static int km_ecdh_gen_pub(struct kpp_request *req)
370370
{
371371
struct crypto_kpp * tfm = NULL;
@@ -463,11 +463,11 @@ static int km_ecdh_gen_pub(struct kpp_request *req)
463463
return err;
464464
}
465465

466-
/**
466+
/*
467467
* Generate ecc shared secret.
468468
* - req->src has raw pub key from other party.
469469
* - req->dst is shared secret output buffer.
470-
* */
470+
*/
471471
static int km_ecdh_compute_shared_secret(struct kpp_request *req)
472472
{
473473
struct crypto_kpp * tfm = NULL;
@@ -596,7 +596,7 @@ static int linuxkm_test_ecdh_nist_p192(void)
596596
{
597597
int rc = 0;
598598
/* reference values from kernel crypto/testmgr.h */
599-
const byte secret[] = {
599+
static const byte secret[] = {
600600
#ifdef LITTLE_ENDIAN_ORDER
601601
0x02, 0x00, /* type */
602602
0x1e, 0x00, /* len */
@@ -613,7 +613,7 @@ static int linuxkm_test_ecdh_nist_p192(void)
613613
};
614614

615615
/* 48 byte pub key */
616-
const byte b_pub[] = {
616+
static const byte b_pub[] = {
617617
0xc3, 0xba, 0x67, 0x4b, 0x71, 0xec, 0xd0, 0x76,
618618
0x7a, 0x99, 0x75, 0x64, 0x36, 0x13, 0x9a, 0x94,
619619
0x5d, 0x8b, 0xdc, 0x60, 0x90, 0x91, 0xfd, 0x3f,
@@ -622,7 +622,7 @@ static int linuxkm_test_ecdh_nist_p192(void)
622622
0x07, 0xd6, 0xbd, 0x1c, 0xe6, 0x8d, 0x9d, 0x67
623623
};
624624

625-
const byte expected_a_pub[] = {
625+
static const byte expected_a_pub[] = {
626626
0x1a, 0x04, 0xdb, 0xa5, 0xe1, 0xdd, 0x4e, 0x79,
627627
0xa3, 0xe6, 0xef, 0x0e, 0x5c, 0x80, 0x49, 0x85,
628628
0xfa, 0x78, 0xb4, 0xef, 0x49, 0xbd, 0x4c, 0x7c,
@@ -632,7 +632,7 @@ static int linuxkm_test_ecdh_nist_p192(void)
632632
};
633633

634634
/* 24 byte shared secret */
635-
const byte shared_secret[] = {
635+
static const byte shared_secret[] = {
636636
0xf4, 0x57, 0xcc, 0x4f, 0x1f, 0x4e, 0x31, 0xcc,
637637
0xe3, 0x40, 0x60, 0xc8, 0x06, 0x93, 0xc6, 0x2e,
638638
0x99, 0x80, 0x81, 0x28, 0xaf, 0xc5, 0x51, 0x74
@@ -651,7 +651,7 @@ static int linuxkm_test_ecdh_nist_p256(void)
651651
{
652652
int rc = 0;
653653
/* reference values from kernel crypto/testmgr.h */
654-
const byte secret[] = {
654+
static const byte secret[] = {
655655
#ifdef LITTLE_ENDIAN_ORDER
656656
0x02, 0x00, /* type */
657657
0x26, 0x00, /* len */
@@ -668,7 +668,7 @@ static int linuxkm_test_ecdh_nist_p256(void)
668668
};
669669

670670
/* 64 byte pub key */
671-
const byte b_pub[] = {
671+
static const byte b_pub[] = {
672672
0xcc, 0xb4, 0xda, 0x74, 0xb1, 0x47, 0x3f, 0xea,
673673
0x6c, 0x70, 0x9e, 0x38, 0x2d, 0xc7, 0xaa, 0xb7,
674674
0x29, 0xb2, 0x47, 0x03, 0x19, 0xab, 0xdd, 0x34,
@@ -679,7 +679,7 @@ static int linuxkm_test_ecdh_nist_p256(void)
679679
0x6f, 0xdb, 0xa9, 0xaa, 0xfc, 0x77, 0x81, 0xf3,
680680
};
681681

682-
const byte expected_a_pub[] = {
682+
static const byte expected_a_pub[] = {
683683
0x1a, 0x7f, 0xeb, 0x52, 0x00, 0xbd, 0x3c, 0x31,
684684
0x7d, 0xb6, 0x70, 0xc1, 0x86, 0xa6, 0xc7, 0xc4,
685685
0x3b, 0xc5, 0x5f, 0x6c, 0x6f, 0x58, 0x3c, 0xf5,
@@ -691,7 +691,7 @@ static int linuxkm_test_ecdh_nist_p256(void)
691691
};
692692

693693
/* 32 byte shared secret */
694-
const byte shared_secret[] = {
694+
static const byte shared_secret[] = {
695695
0xea, 0x17, 0x6f, 0x7e, 0x6e, 0x57, 0x26, 0x38,
696696
0x8b, 0xfb, 0x41, 0xeb, 0xba, 0xc8, 0x6d, 0xa5,
697697
0xa8, 0x72, 0xd1, 0xff, 0xc9, 0x47, 0x3d, 0xaa,
@@ -710,7 +710,7 @@ static int linuxkm_test_ecdh_nist_p384(void)
710710
{
711711
int rc = 0;
712712
/* reference values from kernel crypto/testmgr.h */
713-
const byte secret[] = {
713+
static const byte secret[] = {
714714
#ifdef LITTLE_ENDIAN_ORDER
715715
0x02, 0x00, /* type */
716716
0x36, 0x00, /* len */
@@ -729,7 +729,7 @@ static int linuxkm_test_ecdh_nist_p384(void)
729729
};
730730

731731
/* 96 byte pub key */
732-
const byte b_pub[] = {
732+
static const byte b_pub[] = {
733733
0xE5, 0x58, 0xDB, 0xEF, 0x53, 0xEE, 0xCD, 0xE3,
734734
0xD3, 0xFC, 0xCF, 0xC1, 0xAE, 0xA0, 0x8A, 0x89,
735735
0xA9, 0x87, 0x47, 0x5D, 0x12, 0xFD, 0x95, 0x0D,
@@ -745,7 +745,7 @@ static int linuxkm_test_ecdh_nist_p384(void)
745745
};
746746

747747
/* 96 byte pub key */
748-
const byte expected_a_pub[] = {
748+
static const byte expected_a_pub[] = {
749749
0x66, 0x78, 0x42, 0xD7, 0xD1, 0x80, 0xAC, 0x2C,
750750
0xDE, 0x6F, 0x74, 0xF3, 0x75, 0x51, 0xF5, 0x57,
751751
0x55, 0xC7, 0x64, 0x5C, 0x20, 0xEF, 0x73, 0xE3,
@@ -761,7 +761,7 @@ static int linuxkm_test_ecdh_nist_p384(void)
761761
};
762762

763763
/* 48 byte shared secret */
764-
const byte shared_secret[] = {
764+
static const byte shared_secret[] = {
765765
0x11, 0x18, 0x73, 0x31, 0xC2, 0x79, 0x96, 0x2D,
766766
0x93, 0xD6, 0x04, 0x24, 0x3F, 0xD5, 0x92, 0xCB,
767767
0x9D, 0x0A, 0x92, 0x6F, 0x42, 0x2E, 0x47, 0x18,
@@ -796,10 +796,10 @@ static int linuxkm_test_ecdh_nist_driver(const char * driver,
796796
byte * dst_buf = NULL;
797797
unsigned int src_len = pub_len;
798798
unsigned int dst_len = 0;
799-
/**
799+
/*
800800
* Allocate the kpp transform, and set up
801801
* the kpp request.
802-
* */
802+
*/
803803
tfm = crypto_alloc_kpp(driver, 0, 0);
804804
if (IS_ERR(tfm)) {
805805
pr_err("error: allocating kpp algorithm %s failed: %ld\n",

linuxkm/lkcapi_ecdsa_glue.c

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
330330
static 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

402394
ecdsa_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,
747754
test_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

Comments
 (0)