-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathwolfcrypt_glue.c
More file actions
1266 lines (1062 loc) · 35.4 KB
/
Copy pathwolfcrypt_glue.c
File metadata and controls
1266 lines (1062 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020-2026 wolfSSL Inc. <info@wolfssl.com>
*/
#include <crypto/scatterwalk.h>
#include <linux/random.h>
#include <crypto/internal/rng.h>
#include "wolfcrypt_glue.h"
#if defined(WG_USE_PUBLIC_KEY_COMPRESSION) && !defined(HAVE_COMP_KEY)
#error WG_USE_PUBLIC_KEY_COMPRESSION requires HAVE_COMP_KEY
#endif
#ifdef WC_DRBG_BANKREF
#include <crypto/rng.h>
#else
static struct wc_rng_inst *get_drbg(struct wc_linuxkm_drbg_ctx *ctx);
static void put_drbg(struct wc_rng_inst *drbg);
#endif
int wc_hmac_oneshot_prealloc(struct Hmac *wc_hmac, const int type, byte *out, const size_t out_space, const byte *message,
const size_t message_len, const byte *key, const size_t key_len)
{
int ret;
ret = wc_HmacSizeByType(type);
if (ret < 0)
WC_DEBUG_PR_NEG_RET(ret);
if (out_space < (size_t)ret) {
WC_DEBUG_PR("out_space=%zu ret=%d\n", out_space, ret);
WC_DEBUG_PR_NEG_RET(-ENOBUFS);
}
if ((key_len > UINT_MAX) || (message_len > UINT_MAX))
WC_DEBUG_PR_NEG_RET(-EINVAL);
ret = wc_HmacInit(wc_hmac, NULL /* heap */, INVALID_DEVID);
if (ret < 0)
WC_DEBUG_PR_NEG_RET(ret);
ret = wc_HmacSetKey(wc_hmac, type, key, (word32)key_len);
if (ret == 0)
ret = wc_HmacUpdate(wc_hmac, message, (word32)message_len);
if (ret == 0)
ret = wc_HmacFinal(wc_hmac, out);
wc_HmacFree(wc_hmac);
WC_DEBUG_PR_NEG_RET(ret);
}
int wc_hmac_oneshot(const int type, byte *out, const size_t out_space, const byte *message,
const size_t message_len, const byte *key, const size_t key_len)
{
/* sizeof(struct Hmac) is 832 if SHA3 is enabled. */
struct Hmac *wc_hmac = (struct Hmac *)malloc(sizeof(*wc_hmac));
int ret;
if (! wc_hmac)
WC_DEBUG_PR_NEG_RET(-ENOMEM);
ret = wc_hmac_oneshot_prealloc(wc_hmac, type, out, out_space, message,
message_len, key, key_len);
free(wc_hmac);
WC_DEBUG_PR_NEG_RET(ret);
}
static const byte ZeroNonce[AES_IV_SIZE] = {};
int wc_AesGcm_Appended_Tag_Encrypt(Aes* aes, byte* out, word32 out_space,
const byte* in, word32 in_sz,
const byte* iv, word32 iv_sz,
const byte* authIn, word32 authIn_sz,
const word32 authTag_len)
{
if (out_space < authTag_len)
WC_DEBUG_PR_NEG_RET(-EINVAL);
if (out_space - authTag_len < in_sz)
WC_DEBUG_PR_NEG_RET(-ENOBUFS);
if (! iv) {
iv = ZeroNonce;
iv_sz = sizeof(ZeroNonce);
}
WC_DEBUG_PR_NEG_RET(wc_AesGcmEncrypt(aes, out, in, in_sz, iv, iv_sz,
out + in_sz, authTag_len,
authIn, authIn_sz));
}
int wc_AesGcm_Appended_Tag_Decrypt(Aes* aes, byte* out, word32 out_space,
const byte* in, word32 in_sz,
const byte* iv, word32 iv_sz,
const byte* authIn, word32 authIn_sz,
const word32 authTag_len)
{
if (in_sz < authTag_len)
WC_DEBUG_PR_NEG_RET(-EINVAL);
if (out_space < in_sz - authTag_len)
WC_DEBUG_PR_NEG_RET(-ENOBUFS);
if (! iv) {
iv = ZeroNonce;
iv_sz = sizeof(ZeroNonce);
}
WC_DEBUG_PR_NEG_RET(wc_AesGcmDecrypt(aes, out, in, in_sz - authTag_len, iv, iv_sz,
in + in_sz - authTag_len, authTag_len,
authIn, authIn_sz));
}
static __always_inline int wc_AesGcm_oneshot_crypt(byte* out, size_t out_space, const byte* key, size_t keySz, const byte* in, size_t inSz,
const byte* iv, size_t ivSz,
const byte* authIn, size_t authInSz,
size_t authTagSz,
int decrypt_p)
{
int ret;
Aes *aes;
if ((out_space > UINT_MAX) ||
(keySz > UINT_MAX) ||
(inSz > UINT_MAX) ||
(ivSz > UINT_MAX) ||
(authInSz > UINT_MAX) ||
(authTagSz > UINT_MAX))
{
WC_DEBUG_PR_NEG_RET(-EINVAL);
}
aes = (Aes *)malloc(sizeof(*aes));
if (! aes)
WC_DEBUG_PR_NEG_RET(-ENOMEM);
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
if (ret != 0)
goto out;
ret = wc_AesGcmSetKey(aes, key, keySz);
if (ret == 0) {
if (decrypt_p)
ret = wc_AesGcm_Appended_Tag_Decrypt(aes, out, out_space, in, inSz, iv, ivSz,
authIn, authInSz, authTagSz);
else
ret = wc_AesGcm_Appended_Tag_Encrypt(aes, out, out_space, in, inSz, iv, ivSz,
authIn, authInSz, authTagSz);
}
wc_AesFree(aes);
out:
free(aes);
WC_DEBUG_PR_NEG_RET(ret);
}
int wc_AesGcm_oneshot_encrypt(byte* out, size_t out_space, const byte* key, size_t keySz, const byte* in, size_t inSz,
const byte* iv, size_t ivSz,
const byte* authIn, size_t authInSz, size_t authTagSz)
{
WC_DEBUG_PR_NEG_RET(wc_AesGcm_oneshot_crypt(out, out_space, key, keySz, in, inSz, iv, ivSz, authIn, authInSz, authTagSz, 0));
}
int wc_AesGcm_oneshot_decrypt(byte* out, size_t out_space, const byte* key, size_t keySz, const byte* in, size_t inSz,
const byte* iv, size_t ivSz,
const byte* authIn, size_t authInSz, size_t authTagSz)
{
WC_DEBUG_PR_NEG_RET(wc_AesGcm_oneshot_crypt(out, out_space, key, keySz, in, inSz, iv, ivSz, authIn, authInSz, authTagSz, 1));
}
#ifdef WOLFSSL_AESGCM_STREAM
static __always_inline bool wc_AesGcm_crypt_sg_inplace(struct scatterlist *src, const size_t src_len,
const u8 *ad, const size_t ad_len,
u64 nonce,
const u8 *key, const size_t key_len,
int isDecrypt)
{
int ret = -1;
struct sg_mapping_iter miter;
int miter_needs_stop = 0;
unsigned int flags;
ssize_t sl;
Aes *aes = NULL;
byte full_nonce[AES_IV_SIZE];
if (WARN_ON((src_len > UINT_MAX) ||
(ad_len > UINT_MAX) ||
(key_len > UINT_MAX)))
{
ret = -EINVAL;
WC_DEBUG_PR_CODEPOINT();
goto out_aes_uninited;
}
aes = (Aes *)XMALLOC(sizeof *aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (! aes) {
ret = -ENOMEM;
WC_DEBUG_PR_CODEPOINT();
goto out_aes_uninited;
}
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
if (ret != 0) {
ret = -EINVAL;
WC_DEBUG_PR_CODEPOINT();
goto out_aes_uninited;
}
memset(full_nonce, 0, sizeof(full_nonce));
#ifdef BIG_ENDIAN_ORDER
nonce = cpu_to_le64(nonce);
#endif
memcpy(full_nonce + 4, (u8 *)&nonce, sizeof(nonce));
if (isDecrypt)
ret = wc_AesGcmDecryptInit(aes, key, (word32)key_len,
full_nonce, (word32)sizeof(full_nonce));
else
ret = wc_AesGcmEncryptInit(aes, key, (word32)key_len,
full_nonce, (word32)sizeof(full_nonce));
wc_ForceZero(full_nonce, sizeof full_nonce);
if (ret != 0) {
WC_DEBUG_PR_CODEPOINT();
goto out;
}
if (ad) {
if (isDecrypt)
ret = wc_AesGcmDecryptUpdate(aes, NULL, NULL,
0, ad, ad_len);
else
ret = wc_AesGcmEncryptUpdate(aes, NULL, NULL,
0, ad, ad_len);
if (ret != 0) {
WC_DEBUG_PR_CODEPOINT();
goto out;
}
}
flags = SG_MITER_TO_SG | SG_MITER_ATOMIC;
sg_miter_start(&miter, src, sg_nents(src), flags);
miter_needs_stop = 1;
for (sl = (ssize_t)src_len; sl > 0 && sg_miter_next(&miter); sl -= miter.length) {
size_t length = min_t(size_t, sl, (ssize_t)miter.length);
if (isDecrypt)
ret = wc_AesGcmDecryptUpdate(aes, miter.addr, miter.addr,
length, NULL, 0);
else
ret = wc_AesGcmEncryptUpdate(aes, miter.addr, miter.addr,
length, NULL, 0);
if (ret != 0) {
WC_DEBUG_PR_CODEPOINT();
goto out;
}
}
/* the remaining length (sl) really will be conditionally negative after
* iteration -- this is Jason Donenfeld's algorithm from
* chacha20poly1305_crypt_sg_inplace() in Linux
* lib/crypto/chacha20poly1305.c.
*/
if (sl <= -WC_AES_BLOCK_SIZE) {
if (isDecrypt)
ret = wc_AesGcmDecryptFinal(aes, miter.addr + (ssize_t)miter.length + sl, WC_AES_BLOCK_SIZE);
else
ret = wc_AesGcmEncryptFinal(aes, miter.addr + (ssize_t)miter.length + sl, WC_AES_BLOCK_SIZE);
if (ret < 0) {
WC_DEBUG_PR_CODEPOINT();
goto out;
}
}
sg_miter_stop(&miter);
miter_needs_stop = 0;
if (sl > -WC_AES_BLOCK_SIZE) {
byte AuthTagBuf[WC_AES_BLOCK_SIZE];
if (isDecrypt) {
scatterwalk_map_and_copy(AuthTagBuf, src, src_len,
sizeof AuthTagBuf, 0 /* isEncrypt */);
ret = wc_AesGcmDecryptFinal(aes, AuthTagBuf, WC_AES_BLOCK_SIZE);
if (ret < 0)
goto out;
} else {
ret = wc_AesGcmEncryptFinal(aes, AuthTagBuf, WC_AES_BLOCK_SIZE);
if (ret < 0)
goto out;
scatterwalk_map_and_copy(AuthTagBuf, src, src_len,
sizeof AuthTagBuf, 1 /* isEncrypt */);
}
}
ret = 0;
out:
wc_AesFree(aes);
out_aes_uninited:
if (miter_needs_stop)
sg_miter_stop(&miter);
free(aes);
WC_DEBUG_PR_IF_NEG(ret);
return ret == 0;
}
#else /* !WOLFSSL_AESGCM_STREAM */
static __always_inline bool wc_AesGcm_crypt_sg_inplace(struct scatterlist *src, const size_t src_len,
const u8 *ad, const size_t ad_len,
u64 nonce,
const u8 *key, const size_t key_len,
int isDecrypt)
{
int ret = -1;
struct sg_mapping_iter miter;
unsigned int flags;
Aes *aes = NULL;
byte full_nonce[AES_IV_SIZE];
if (WARN_ON((src_len > UINT_MAX) ||
(ad_len > UINT_MAX) ||
(key_len > UINT_MAX)))
{
ret = -EINVAL;
WC_DEBUG_PR_CODEPOINT();
goto out_aes_uninited;
}
if (sg_nents(src) < 1) {
ret = -EINVAL;
WC_DEBUG_PR_CODEPOINT();
goto out_aes_uninited;
}
aes = (Aes *)XMALLOC(sizeof *aes, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (! aes) {
ret = -ENOMEM;
goto out_aes_uninited;
}
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
if (ret != 0) {
ret = -EINVAL;
WC_DEBUG_PR_CODEPOINT();
goto out_aes_uninited;
}
memset(full_nonce, 0, sizeof(full_nonce));
#ifdef BIG_ENDIAN_ORDER
nonce = cpu_to_le64(nonce);
#endif
memcpy(full_nonce + 4, (u8 *)&nonce, sizeof(nonce));
flags = SG_MITER_TO_SG | SG_MITER_ATOMIC;
ret = wc_AesGcmSetKey(aes, key, (word32)key_len);
if (ret) {
ret = -EINVAL;
WC_DEBUG_PR_CODEPOINT();
goto out;
}
if (sg_nents(src) == 1) {
size_t length;
sg_miter_start(&miter, src, sg_nents(src), flags);
if ((sg_nents(src) == 1) && (! sg_miter_next(&miter))) {
sg_miter_stop(&miter);
ret = -EINVAL;
WC_DEBUG_PR_CODEPOINT();
goto out;
}
if (miter.length < src_len + WC_AES_BLOCK_SIZE) {
sg_miter_stop(&miter);
goto copy_after_all;
}
length = min_t(size_t, src_len, miter.length);
if (isDecrypt) {
ret = wc_AesGcmDecrypt(aes, miter.addr,
miter.addr, (word32)length,
full_nonce, (word32)sizeof(full_nonce),
miter.addr + length, WC_AES_BLOCK_SIZE,
ad, (word32)ad_len);
}
else {
ret = wc_AesGcmEncrypt(aes, miter.addr,
miter.addr, (word32)length,
full_nonce, (word32)sizeof(full_nonce),
miter.addr + length, WC_AES_BLOCK_SIZE,
ad, (word32)ad_len);
}
sg_miter_stop(&miter);
goto out;
}
copy_after_all:
{
byte *buf = (byte *)XMALLOC(src_len + WC_AES_BLOCK_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (! buf) {
ret = -ENOMEM;
WC_DEBUG_PR_CODEPOINT();
goto out;
}
if (isDecrypt) {
scatterwalk_map_and_copy(buf, src, 0, src_len + WC_AES_BLOCK_SIZE, 0);
ret = wc_AesGcmDecrypt(aes, buf,
buf, (word32)src_len,
full_nonce, (word32)sizeof(full_nonce),
buf + src_len, WC_AES_BLOCK_SIZE,
ad, (word32)ad_len);
if (ret == 0)
scatterwalk_map_and_copy(buf, src, 0, src_len, 1);
wc_ForceZero(buf, src_len + WC_AES_BLOCK_SIZE);
}
else {
scatterwalk_map_and_copy(buf, src, 0, src_len, 0);
ret = wc_AesGcmEncrypt(aes, buf,
buf, (word32)src_len,
full_nonce, (word32)sizeof(full_nonce),
buf + src_len, WC_AES_BLOCK_SIZE,
ad, (word32)ad_len);
if (ret == 0)
scatterwalk_map_and_copy(buf, src, 0, src_len + WC_AES_BLOCK_SIZE, 1);
else
wc_ForceZero(buf, src_len + WC_AES_BLOCK_SIZE);
}
free(buf);
}
out:
wc_AesFree(aes);
out_aes_uninited:
free(aes);
wc_ForceZero(full_nonce, sizeof full_nonce);
WC_DEBUG_PR_IF_NEG(ret);
return ret == 0;
}
#endif /* !WOLFSSL_AESGCM_STREAM */
bool wc_AesGcm_encrypt_sg_inplace(struct scatterlist *src, size_t src_len,
const u8 *ad, const size_t ad_len,
const u64 nonce,
const u8 *key,
const size_t key_len)
{
WC_DEBUG_PR_FALSE_RET(wc_AesGcm_crypt_sg_inplace(src, src_len, ad, ad_len,
nonce, key, key_len, 0));
}
bool wc_AesGcm_decrypt_sg_inplace(struct scatterlist *src, size_t src_len,
const u8 *ad, const size_t ad_len,
const u64 nonce,
const u8 *key,
const size_t key_len)
{
WC_DEBUG_PR_FALSE_RET(wc_AesGcm_crypt_sg_inplace(src, src_len - WC_AES_BLOCK_SIZE,
ad, ad_len, nonce, key, key_len, 1));
}
int wc_ecc_make_keypair_exim(u8 *private, const size_t private_len,
u8 *public, const size_t public_len,
const int curve_id, int compressed)
{
#ifndef WC_DRBG_BANKREF
struct wc_rng_inst *rng_inst = NULL;
#endif
WC_RNG *rng = NULL;
ecc_key *key = NULL;
int key_inited = 0;
int ret;
if ((private_len > UINT_MAX) ||
(public_len > UINT_MAX))
{
WC_DEBUG_PR_NEG_RET(BAD_FUNC_ARG);
}
key = (ecc_key *)malloc(sizeof(*key));
if (! key) {
ret = MEMORY_E;
goto out;
}
ret = wc_ecc_init(key);
if (ret)
goto out;
key_inited = 1;
#ifdef WC_DRBG_BANKREF
ret = wc_rng_new_bankref(wc_wg_drbg, &rng);
if (ret)
goto out;
#else
rng_inst = get_drbg(&wc_wg_drbg);
if (! rng_inst) {
ret = SYSLIB_FAILED_E;
goto out;
}
rng = &rng_inst->rng;
#endif
ret = wc_ecc_make_key_ex(
rng,
0 /* keysize -- use curve_id to designate the curve. */,
key,
curve_id);
if (ret)
goto out;
{
word32 outLen = (word32)private_len;
PRIVATE_KEY_UNLOCK();
ret = wc_ecc_export_private_only(key, private, &outLen);
PRIVATE_KEY_LOCK();
if (ret)
goto out;
if (outLen != (word32)private_len) {
ret = WC_KEY_SIZE_E;
goto out;
}
}
{
word32 outLen = (word32)public_len;
#ifndef HAVE_COMP_KEY
if (compressed)
WC_DEBUG_PR_NEG_RET(BAD_FUNC_ARG);
#endif
PRIVATE_KEY_UNLOCK();
#ifdef HAVE_COMP_KEY
ret = wc_ecc_export_x963_ex(key, public, &outLen, compressed);
#else
ret = wc_ecc_export_x963(key, public, &outLen);
#endif
PRIVATE_KEY_LOCK();
if (ret)
goto out;
if (outLen != (word32)public_len) {
ret = WC_KEY_SIZE_E;
goto out;
}
}
ret = 0;
out:
#ifdef WC_DRBG_BANKREF
wc_rng_free(rng);
#else
if (rng_inst)
put_drbg(rng_inst);
#endif
if (key) {
if (key_inited)
wc_ecc_free(key);
free(key);
}
WC_DEBUG_PR_NEG_RET(ret);
}
int wc_ecc_private_to_public_exim(const u8 *private, const size_t private_len,
u8 *public, const size_t public_len,
const int curve_id, int compressed)
{
ecc_key *key = NULL;
int key_inited = 0;
int ret;
if ((private_len > UINT_MAX) ||
(public_len > UINT_MAX))
{
WC_DEBUG_PR_NEG_RET(BAD_FUNC_ARG);
}
key = (ecc_key *)malloc(sizeof(*key));
if (! key) {
ret = MEMORY_E;
goto out;
}
ret = wc_ecc_init(key);
if (ret)
goto out;
key_inited = 1;
ret = wc_ecc_import_private_key_ex(private, (word32)private_len,
NULL, 0, key, curve_id);
if (ret)
goto out;
{
#ifndef WC_DRBG_BANKREF
struct wc_rng_inst *rng_inst;
#endif
WC_RNG *rng;
#ifdef WC_DRBG_BANKREF
ret = wc_rng_new_bankref(wc_wg_drbg, &rng);
if (ret)
goto out;
#else
rng_inst = get_drbg(&wc_wg_drbg);
if (! rng_inst) {
ret = SYSLIB_FAILED_E;
goto out;
}
rng = &rng_inst->rng;
#endif
ret = wc_ecc_make_pub_ex(key, NULL /* pubOut */, rng);
#ifdef WC_DRBG_BANKREF
wc_rng_free(rng);
#else
put_drbg(rng_inst);
#endif
}
if (ret)
goto out;
{
word32 outLen = (word32)public_len;
#ifndef HAVE_COMP_KEY
if (compressed)
WC_DEBUG_PR_NEG_RET(BAD_FUNC_ARG);
#endif
PRIVATE_KEY_UNLOCK();
#ifdef HAVE_COMP_KEY
ret = wc_ecc_export_x963_ex(key, public, &outLen, compressed);
#else
ret = wc_ecc_export_x963(key, public, &outLen);
#endif
PRIVATE_KEY_LOCK();
if (ret)
goto out;
if (outLen != (word32)public_len) {
ret = WC_KEY_SIZE_E;
goto out;
}
}
out:
if (key) {
if (key_inited)
wc_ecc_free(key);
free(key);
}
WC_DEBUG_PR_NEG_RET(ret);
}
int wc_ecc_shared_secret_exim(u8 *secret, size_t secret_len,
const u8 *private, size_t private_len,
const u8 *public, size_t public_len,
int curve_id)
{
ecc_key *privKey = NULL, *pubKey = NULL;
int privKey_inited = 0, pubKey_inited = 0;
int ret;
#ifdef ECC_TIMING_RESISTANT
#ifndef WC_DRBG_BANKREF
struct wc_rng_inst *rng_inst = NULL;
#endif
WC_RNG *rng = NULL;
#endif
if ((secret_len > UINT_MAX) ||
(private_len > UINT_MAX) ||
(public_len > UINT_MAX))
{
WC_DEBUG_PR_NEG_RET(-EINVAL);
}
privKey = (ecc_key *)malloc(sizeof(*privKey));
if (! privKey) {
ret = MEMORY_E;
goto out;
}
pubKey = (ecc_key *)malloc(sizeof(*pubKey));
if (! pubKey) {
ret = MEMORY_E;
goto out;
}
ret = wc_ecc_init(privKey);
if (ret != 0)
goto out;
privKey_inited = 1;
ret = wc_ecc_init(pubKey);
if (ret != 0)
goto out;
pubKey_inited = 1;
#ifdef ECC_TIMING_RESISTANT
#ifdef WC_DRBG_BANKREF
ret = wc_rng_new_bankref(wc_wg_drbg, &rng);
if (ret) {
ret = -EFAULT;
goto out;
}
#else
rng_inst = get_drbg(&wc_wg_drbg);
if (! rng_inst) {
ret = -EFAULT;
goto out;
}
rng = &rng_inst->rng;
#endif
ret = wc_ecc_set_rng(privKey, rng);
if (ret != 0)
goto out;
#endif /* ECC_TIMING_RESISTANT */
ret = wc_ecc_import_private_key_ex(private, (word32)private_len,
NULL, 0, privKey, curve_id);
if (ret != 0)
goto out;
ret = wc_ecc_import_x963_ex(public, (word32)public_len, pubKey, curve_id);
if (ret != 0)
goto out;
{
word32 secret_len_copy = (word32)secret_len;
PRIVATE_KEY_UNLOCK();
ret = wc_ecc_shared_secret(privKey, pubKey, secret, &secret_len_copy);
PRIVATE_KEY_LOCK();
if ((ret == 0) && (secret_len_copy != (word32)secret_len))
ret = WC_KEY_SIZE_E;
}
out:
#ifdef ECC_TIMING_RESISTANT
#ifdef WC_DRBG_BANKREF
wc_rng_free(rng);
#else
if (rng_inst)
put_drbg(rng_inst);
#endif
#endif
if (privKey) {
if (privKey_inited)
wc_ecc_free(privKey);
free(privKey);
}
if (pubKey) {
if (pubKey_inited)
wc_ecc_free(pubKey);
free(pubKey);
}
WC_DEBUG_PR_NEG_RET(ret);
}
#ifdef WC_DRBG_BANKREF
struct wc_rng_bank *wc_wg_drbg;
static int wc_wg_drbg_is_global_default;
static int linuxkm_affinity_lock(void *arg) {
(void)arg;
if (preempt_count() != 0)
return ALREADY_E;
#if defined(CONFIG_SMP) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0))
migrate_disable(); /* this actually makes irq_count() nonzero, so that
* DISABLE_VECTOR_REGISTERS() is superfluous, but
* don't depend on that.
*/
#endif
local_bh_disable();
return 0;
}
static int linuxkm_affinity_get_id(void *arg, int *id) {
(void)arg;
*id = raw_smp_processor_id();
return 0;
}
static int linuxkm_affinity_unlock(void *arg) {
(void)arg;
local_bh_enable();
#if defined(CONFIG_SMP) && (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0))
migrate_enable();
#endif
return 0;
}
int wc_linuxkm_drbg_init_ctx(struct wc_rng_bank **ctx) {
int ret;
#ifdef LINUXKM_LKCAPI_REGISTER_HASH_DRBG_DEFAULT
#ifdef WC_RNG_BANK_DEFAULT_SUPPORT
ret = wc_rng_bank_default_checkout(ctx);
if (ret != 0)
goto new_bank;
#else /* !WC_RNG_BANK_DEFAULT_SUPPORT */
struct crypto_rng *current_crypto_default_rng;
ret = crypto_get_default_rng();
if (ret != 0)
goto new_bank;
current_crypto_default_rng = crypto_default_rng;
if (current_crypto_default_rng == NULL) {
crypto_put_default_rng();
goto new_bank;
}
if (! wc_linux_kernel_rng_is_wolfcrypt(current_crypto_default_rng)) {
crypto_put_default_rng();
goto new_bank;
}
*ctx = (struct wc_rng_bank *)crypto_rng_ctx(current_crypto_default_rng);
#endif /* !WC_RNG_BANK_DEFAULT_SUPPORT */
wc_wg_drbg_is_global_default = 1;
return 0;
new_bank:
#endif /* LINUXKM_LKCAPI_REGISTER_HASH_DRBG_DEFAULT */
wc_wg_drbg_is_global_default = 0;
ret = wc_rng_bank_new(
ctx,
nr_cpu_ids + 4,
WC_RNG_BANK_FLAG_NO_VECTOR_OPS,
30 /* timeout_secs */,
NULL /* heap */,
INVALID_DEVID);
if (ret == 0) {
ret = wc_rng_bank_set_affinity_handlers(
*ctx,
linuxkm_affinity_lock,
linuxkm_affinity_get_id,
linuxkm_affinity_unlock,
NULL);
if (ret != 0) {
(void)wc_rng_bank_free(ctx);
pr_err("ERROR: wc_rng_bank_set_affinity_handlers() in wc_linuxkm_drbg_init_ctx() returned err %d\n", ret);
WC_DUMP_BACKTRACE_NONDEBUG;
}
}
return (ret == 0) ? ret : -ECANCELED;
}
void wc_linuxkm_drbg_ctx_clear(struct wc_rng_bank **ctx) {
if ((ctx == NULL) || (*ctx == NULL))
return;
#ifdef LINUXKM_LKCAPI_REGISTER_HASH_DRBG_DEFAULT
if (wc_wg_drbg_is_global_default) {
#ifdef WC_RNG_BANK_DEFAULT_SUPPORT
int ret = wc_rng_bank_default_checkin(ctx);
if (ret != 0)
pr_err("ERROR: wc_rng_bank_default_checkin() in wc_linuxkm_drbg_ctx_clear() failed with code %d.\n", ret);
#else /* !WC_RNG_BANK_DEFAULT_SUPPORT */
*ctx = NULL;
crypto_put_default_rng();
#endif /* !WC_RNG_BANK_DEFAULT_SUPPORT */
wc_wg_drbg_is_global_default = 0;
}
else
#endif
(void)wc_rng_bank_free(ctx);
}
static struct wc_rng_bank_inst *linuxkm_get_drbg(struct wc_rng_bank *ctx) {
int err;
struct wc_rng_bank_inst *ret;
word32 flags =
WC_RNG_BANK_FLAG_CAN_FAIL_OVER_INST |
WC_RNG_BANK_FLAG_CAN_WAIT |
WC_RNG_BANK_FLAG_PREFER_AFFINITY_INST |
WC_RNG_BANK_FLAG_NO_VECTOR_OPS;
err = wc_rng_bank_checkout(ctx, &ret, 0, 30 /* timeout_secs */, flags);
if (err != 0) {
pr_err("ERROR: wc_rng_bank_checkout() in linuxkm_get_drbg() returned err %d.\n", err);
WC_DUMP_BACKTRACE_NONDEBUG;
return NULL;
}
return ret;
}
static void linuxkm_put_drbg(struct wc_rng_bank *ctx, struct wc_rng_bank_inst **drbg) {
int ret = wc_rng_bank_checkin(ctx, drbg);
if (ret != 0) {
pr_err("ERROR: wc_rng_bank_checkin() in linuxkm_put_drbg() returned err %d.\n", ret);
WC_DUMP_BACKTRACE_NONDEBUG;
}
}
int wc_linuxkm_drbg_generate(struct wc_rng_bank **ctx,
const u8 *src, unsigned int slen,
u8 *dst, unsigned int dlen,
int nofail_p)
{
int ret, retried = 0;
struct wc_rng_bank_inst *drbg;
(void)nofail_p;
if ((ctx == NULL) || (*ctx == NULL)) {
pr_err_once("BUG: linuxkm_get_drbg() called with null ctx.");
return -EFAULT;
}
drbg = linuxkm_get_drbg(*ctx);
if (! drbg) {
pr_err_once("BUG: linuxkm_get_drbg() failed.");
return -EFAULT;
}
if (slen > 0) {
ret = wc_RNG_DRBG_Reseed(WC_RNG_BANK_INST_TO_RNG(drbg), src, slen);
if (ret != 0) {
pr_warn_once("WARNING: wc_RNG_DRBG_Reseed returned %d\n",ret);
ret = -EINVAL;
goto out;
}
}
for (;;) {
#define RNG_MAX_BLOCK_LEN_ROUNDED (RNG_MAX_BLOCK_LEN & ~0xfU)
if (dlen > RNG_MAX_BLOCK_LEN_ROUNDED) {
ret = wc_RNG_GenerateBlock(WC_RNG_BANK_INST_TO_RNG(drbg), dst, RNG_MAX_BLOCK_LEN_ROUNDED);
if (ret == 0) {
dlen -= RNG_MAX_BLOCK_LEN_ROUNDED;
dst += RNG_MAX_BLOCK_LEN_ROUNDED;
}
}
#undef RNG_MAX_BLOCK_LEN_ROUNDED
else {
ret = wc_RNG_GenerateBlock(WC_RNG_BANK_INST_TO_RNG(drbg), dst, dlen);
if (ret == 0)
dlen = 0;
}
if (dlen == 0)
break;
if (ret == 0)
continue;
if (unlikely(ret == WC_NO_ERR_TRACE(RNG_FAILURE_E)) && (! retried)) {
if (slen > 0)
break;
retried = 1;