-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsp_sm2_arm64.c
20335 lines (19896 loc) · 787 KB
/
sp_sm2_arm64.c
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
/* sp.c
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Implementation by Sean Parkinson. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH) || \
defined(WOLFSSL_HAVE_SP_ECC)
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/cpuid.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#ifdef RSA_LOW_MEM
#ifndef WOLFSSL_SP_SMALL
#define WOLFSSL_SP_SMALL
#endif
#endif
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_SP_NO_MALLOC)
#undef WOLFSSL_SP_SMALL_STACK
#define WOLFSSL_SP_SMALL_STACK
#endif
#include <wolfssl/wolfcrypt/sp.h>
#ifdef __IAR_SYSTEMS_ICC__
#define __asm__ asm
#define __volatile__ volatile
#define WOLFSSL_NO_VAR_ASSIGN_REG
#endif /* __IAR_SYSTEMS_ICC__ */
#ifdef __KEIL__
#define __asm__ __asm
#define __volatile__ volatile
#endif
#ifdef WOLFSSL_SP_ARM64_ASM
#define SP_PRINT_NUM(var, name, total, words, bits) \
do { \
int ii; \
fprintf(stderr, name "=0x"); \
for (ii = (((bits) + 63) / 64) - 1; ii >= 0; ii--) \
fprintf(stderr, SP_PRINT_FMT, (var)[ii]); \
fprintf(stderr, "\n"); \
} while (0)
#define SP_PRINT_VAL(var, name) \
fprintf(stderr, name "=0x" SP_PRINT_FMT "\n", var)
#define SP_PRINT_INT(var, name) \
fprintf(stderr, name "=%d\n", var)
#ifdef WOLFSSL_HAVE_SP_ECC
#ifdef WOLFSSL_SP_SM2
/* Point structure to use. */
typedef struct sp_point_256 {
/* X ordinate of point. */
sp_digit x[2 * 4];
/* Y ordinate of point. */
sp_digit y[2 * 4];
/* Z ordinate of point. */
sp_digit z[2 * 4];
/* Indicates point is at infinity. */
int infinity;
} sp_point_256;
/* The modulus (prime) of the curve SM2 P256. */
static const sp_digit p256_sm2_mod[4] = {
0xffffffffffffffffL,0xffffffff00000000L,0xffffffffffffffffL,
0xfffffffeffffffffL
};
/* The Montgomery normalizer for modulus of the curve P256. */
static const sp_digit p256_sm2_norm_mod[4] = {
0x0000000000000001L,0x00000000ffffffffL,0x0000000000000000L,
0x0000000100000000L
};
/* The Montgomery multiplier for modulus of the curve P256. */
static const sp_digit p256_sm2_mp_mod = 0x0000000000000001;
#if defined(WOLFSSL_VALIDATE_ECC_KEYGEN) || defined(HAVE_ECC_SIGN) || \
defined(HAVE_ECC_VERIFY)
/* The order of the curve P256. */
static const sp_digit p256_sm2_order[4] = {
0x53bbf40939d54123L,0x7203df6b21c6052bL,0xffffffffffffffffL,
0xfffffffeffffffffL
};
#endif
/* The order of the curve P256 minus 2. */
static const sp_digit p256_sm2_order2[4] = {
0x53bbf40939d54121L,0x7203df6b21c6052bL,0xffffffffffffffffL,
0xfffffffeffffffffL
};
#if defined(HAVE_ECC_SIGN)
/* The Montgomery normalizer for order of the curve P256. */
static const sp_digit p256_sm2_norm_order[4] = {
0xac440bf6c62abeddL,0x8dfc2094de39fad4L,0x0000000000000000L,
0x0000000100000000L
};
#endif
#if defined(HAVE_ECC_SIGN)
/* The Montgomery multiplier for order of the curve P256. */
static const sp_digit p256_sm2_mp_order = 0x327f9e8872350975L;
#endif
#ifdef WOLFSSL_SP_SMALL
/* The base point of curve P256. */
static const sp_point_256 p256_sm2_base = {
/* X ordinate */
{
0x715a4589334c74c7L,0x8fe30bbff2660be1L,0x5f9904466a39c994L,
0x32c4ae2c1f198119L,
(sp_digit)0, (sp_digit)0, (sp_digit)0, (sp_digit)0
},
/* Y ordinate */
{
0x02df32e52139f0a0L,0xd0a9877cc62a4740L,0x59bdcee36b692153L,
0xbc3736a2f4f6779cL,
(sp_digit)0, (sp_digit)0, (sp_digit)0, (sp_digit)0
},
/* Z ordinate */
{
0x0000000000000001L,0x0000000000000000L,0x0000000000000000L,
0x0000000000000000L,
(sp_digit)0, (sp_digit)0, (sp_digit)0, (sp_digit)0
},
/* infinity */
0
};
#endif /* WOLFSSL_SP_SMALL */
#if defined(HAVE_ECC_CHECK_KEY) || defined(HAVE_COMP_KEY)
static const sp_digit p256_sm2_b[4] = {
0xddbcbd414d940e93L,0xf39789f515ab8f92L,0x4d5a9e4bcf6509a7L,
0x28e9fa9e9d9f5e34L
};
#endif
#ifdef WOLFSSL_SP_SMALL
/* Multiply a and b into r. (r = a * b)
*
* r A single precision integer.
* a A single precision integer.
* b A single precision integer.
*/
static void sp_256_mul_sm2_4(sp_digit* r, const sp_digit* a, const sp_digit* b)
{
sp_digit tmp[8];
__asm__ __volatile__ (
"mov x5, xzr\n\t"
"mov x6, xzr\n\t"
"mov x7, xzr\n\t"
"mov x8, xzr\n\t"
"\n1:\n\t"
"subs x3, x5, 24\n\t"
"csel x3, xzr, x3, cc\n\t"
"sub x4, x5, x3\n\t"
"\n2:\n\t"
"ldr x10, [%[a], x3]\n\t"
"ldr x11, [%[b], x4]\n\t"
"mul x9, x10, x11\n\t"
"umulh x10, x10, x11\n\t"
"adds x6, x6, x9\n\t"
"adcs x7, x7, x10\n\t"
"adc x8, x8, xzr\n\t"
"add x3, x3, #8\n\t"
"sub x4, x4, #8\n\t"
"cmp x3, 32\n\t"
"b.eq 3f\n\t"
"cmp x3, x5\n\t"
"b.le 2b\n\t"
"\n3:\n\t"
"str x6, [%[r], x5]\n\t"
"mov x6, x7\n\t"
"mov x7, x8\n\t"
"mov x8, #0\n\t"
"add x5, x5, #8\n\t"
"cmp x5, 48\n\t"
"b.le 1b\n\t"
"str x6, [%[r], x5]\n\t"
:
: [r] "r" (tmp), [a] "r" (a), [b] "r" (b)
: "memory", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "cc"
);
XMEMCPY(r, tmp, sizeof(tmp));
}
#else
/* Multiply a and b into r. (r = a * b)
*
* r A single precision integer.
* a A single precision integer.
* b A single precision integer.
*/
SP_NOINLINE static void sp_256_mul_sm2_4(sp_digit* r, const sp_digit* a,
const sp_digit* b)
{
__asm__ __volatile__ (
"ldp x13, x14, [%[a], 0]\n\t"
"ldp x15, x16, [%[a], 16]\n\t"
"ldp x17, x19, [%[b], 0]\n\t"
"ldp x20, x21, [%[b], 16]\n\t"
"# A[0] * B[0]\n\t"
"umulh x6, x13, x17\n\t"
"mul x5, x13, x17\n\t"
"# A[2] * B[0]\n\t"
"umulh x8, x15, x17\n\t"
"mul x7, x15, x17\n\t"
"# A[1] * B[0]\n\t"
"mul x3, x14, x17\n\t"
"adds x6, x6, x3\n\t"
"umulh x4, x14, x17\n\t"
"adcs x7, x7, x4\n\t"
"adc x8, x8, xzr\n\t"
"# A[0] * B[2]\n\t"
"mul x3, x13, x20\n\t"
"adds x7, x7, x3\n\t"
"umulh x4, x13, x20\n\t"
"adcs x8, x8, x4\n\t"
"# A[1] * B[3]\n\t"
"mul x9, x14, x21\n\t"
"adcs x9, x9, xzr\n\t"
"umulh x10, x14, x21\n\t"
"adc x10, x10, xzr\n\t"
"# A[0] * B[1]\n\t"
"mul x3, x13, x19\n\t"
"adds x6, x6, x3\n\t"
"umulh x4, x13, x19\n\t"
"adcs x7, x7, x4\n\t"
"# A[2] * B[1]\n\t"
"mul x3, x15, x19\n\t"
"adcs x8, x8, x3\n\t"
"umulh x4, x15, x19\n\t"
"adcs x9, x9, x4\n\t"
"adc x10, x10, xzr\n\t"
"# A[1] * B[2]\n\t"
"mul x3, x14, x20\n\t"
"adds x8, x8, x3\n\t"
"umulh x4, x14, x20\n\t"
"adcs x9, x9, x4\n\t"
"adcs x10, x10, xzr\n\t"
"adc x11, xzr, xzr\n\t"
"# A[1] * B[1]\n\t"
"mul x3, x14, x19\n\t"
"adds x7, x7, x3\n\t"
"umulh x4, x14, x19\n\t"
"adcs x8, x8, x4\n\t"
"# A[3] * B[1]\n\t"
"mul x3, x16, x19\n\t"
"adcs x9, x9, x3\n\t"
"umulh x4, x16, x19\n\t"
"adcs x10, x10, x4\n\t"
"adc x11, x11, xzr\n\t"
"# A[2] * B[2]\n\t"
"mul x3, x15, x20\n\t"
"adds x9, x9, x3\n\t"
"umulh x4, x15, x20\n\t"
"adcs x10, x10, x4\n\t"
"# A[3] * B[3]\n\t"
"mul x3, x16, x21\n\t"
"adcs x11, x11, x3\n\t"
"umulh x12, x16, x21\n\t"
"adc x12, x12, xzr\n\t"
"# A[0] * B[3]\n\t"
"mul x3, x13, x21\n\t"
"adds x8, x8, x3\n\t"
"umulh x4, x13, x21\n\t"
"adcs x9, x9, x4\n\t"
"# A[2] * B[3]\n\t"
"mul x3, x15, x21\n\t"
"adcs x10, x10, x3\n\t"
"umulh x4, x15, x21\n\t"
"adcs x11, x11, x4\n\t"
"adc x12, x12, xzr\n\t"
"# A[3] * B[0]\n\t"
"mul x3, x16, x17\n\t"
"adds x8, x8, x3\n\t"
"umulh x4, x16, x17\n\t"
"adcs x9, x9, x4\n\t"
"# A[3] * B[2]\n\t"
"mul x3, x16, x20\n\t"
"adcs x10, x10, x3\n\t"
"umulh x4, x16, x20\n\t"
"adcs x11, x11, x4\n\t"
"adc x12, x12, xzr\n\t"
"stp x5, x6, [%[r], 0]\n\t"
"stp x7, x8, [%[r], 16]\n\t"
"stp x9, x10, [%[r], 32]\n\t"
"stp x11, x12, [%[r], 48]\n\t"
:
: [r] "r" (r), [a] "r" (a), [b] "r" (b)
: "memory", "x3", "x4", "x13", "x14", "x15", "x16", "x17", "x19", "x20", "x21", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "cc"
);
}
#endif /* WOLFSSL_SP_SMALL */
/* Square a and put result in r. (r = a * a)
*
* r A single precision integer.
* a A single precision integer.
*/
SP_NOINLINE static void sp_256_sqr_sm2_4(sp_digit* r, const sp_digit* a)
{
__asm__ __volatile__ (
"ldp x12, x13, [%[a], 0]\n\t"
"ldp x14, x15, [%[a], 16]\n\t"
"# A[0] * A[1]\n\t"
"umulh x6, x12, x13\n\t"
"mul x5, x12, x13\n\t"
"# A[0] * A[3]\n\t"
"umulh x8, x12, x15\n\t"
"mul x7, x12, x15\n\t"
"# A[0] * A[2]\n\t"
"mul x2, x12, x14\n\t"
"adds x6, x6, x2\n\t"
"umulh x3, x12, x14\n\t"
"adcs x7, x7, x3\n\t"
"# A[1] * A[3]\n\t"
"mul x2, x13, x15\n\t"
"adcs x8, x8, x2\n\t"
"umulh x9, x13, x15\n\t"
"adc x9, x9, xzr\n\t"
"# A[1] * A[2]\n\t"
"mul x2, x13, x14\n\t"
"adds x7, x7, x2\n\t"
"umulh x3, x13, x14\n\t"
"adcs x8, x8, x3\n\t"
"# A[2] * A[3]\n\t"
"mul x2, x14, x15\n\t"
"adcs x9, x9, x2\n\t"
"umulh x10, x14, x15\n\t"
"adc x10, x10, xzr\n\t"
"# Double\n\t"
"adds x5, x5, x5\n\t"
"adcs x6, x6, x6\n\t"
"adcs x7, x7, x7\n\t"
"adcs x8, x8, x8\n\t"
"adcs x9, x9, x9\n\t"
"adcs x10, x10, x10\n\t"
"adc x11, xzr, xzr\n\t"
"# A[0] * A[0]\n\t"
"umulh x3, x12, x12\n\t"
"mul x4, x12, x12\n\t"
"# A[1] * A[1]\n\t"
"mul x2, x13, x13\n\t"
"adds x5, x5, x3\n\t"
"umulh x3, x13, x13\n\t"
"adcs x6, x6, x2\n\t"
"# A[2] * A[2]\n\t"
"mul x2, x14, x14\n\t"
"adcs x7, x7, x3\n\t"
"umulh x3, x14, x14\n\t"
"adcs x8, x8, x2\n\t"
"# A[3] * A[3]\n\t"
"mul x2, x15, x15\n\t"
"adcs x9, x9, x3\n\t"
"umulh x3, x15, x15\n\t"
"adcs x10, x10, x2\n\t"
"adc x11, x11, x3\n\t"
"stp x4, x5, [%[r], 0]\n\t"
"stp x6, x7, [%[r], 16]\n\t"
"stp x8, x9, [%[r], 32]\n\t"
"stp x10, x11, [%[r], 48]\n\t"
:
: [r] "r" (r), [a] "r" (a)
: "memory", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "cc"
);
}
/* Add b to a into r. (r = a + b)
*
* r A single precision integer.
* a A single precision integer.
* b A single precision integer.
*/
static sp_digit sp_256_add_sm2_4(sp_digit* r, const sp_digit* a,
const sp_digit* b)
{
__asm__ __volatile__ (
"ldp x3, x4, [%[a], 0]\n\t"
"ldp x7, x8, [%[b], 0]\n\t"
"adds x3, x3, x7\n\t"
"ldp x5, x6, [%[a], 16]\n\t"
"adcs x4, x4, x8\n\t"
"ldp x9, x10, [%[b], 16]\n\t"
"adcs x5, x5, x9\n\t"
"stp x3, x4, [%[r], 0]\n\t"
"adcs x6, x6, x10\n\t"
"stp x5, x6, [%[r], 16]\n\t"
"adc %[r], xzr, xzr\n\t"
: [r] "+r" (r)
: [a] "r" (a), [b] "r" (b)
: "memory", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "cc"
);
return (sp_digit)r;
}
/* Sub b from a into r. (r = a - b)
*
* r A single precision integer.
* a A single precision integer.
* b A single precision integer.
*/
static sp_digit sp_256_sub_sm2_4(sp_digit* r, const sp_digit* a,
const sp_digit* b)
{
__asm__ __volatile__ (
"ldp x3, x4, [%[a], 0]\n\t"
"ldp x7, x8, [%[b], 0]\n\t"
"subs x3, x3, x7\n\t"
"ldp x5, x6, [%[a], 16]\n\t"
"sbcs x4, x4, x8\n\t"
"ldp x9, x10, [%[b], 16]\n\t"
"sbcs x5, x5, x9\n\t"
"stp x3, x4, [%[r], 0]\n\t"
"sbcs x6, x6, x10\n\t"
"stp x5, x6, [%[r], 16]\n\t"
"csetm %[r], cc\n\t"
: [r] "+r" (r)
: [a] "r" (a), [b] "r" (b)
: "memory", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "cc"
);
return (sp_digit)r;
}
/* Sub b from a into a. (a -= b)
*
* a A single precision integer and result.
* b A single precision integer.
*/
static sp_digit sp_256_sub_in_place_sm2_4(sp_digit* a, const sp_digit* b)
{
__asm__ __volatile__ (
"ldp x2, x3, [%[a], 0]\n\t"
"ldp x6, x7, [%[b], 0]\n\t"
"subs x2, x2, x6\n\t"
"ldp x4, x5, [%[a], 16]\n\t"
"sbcs x3, x3, x7\n\t"
"ldp x8, x9, [%[b], 16]\n\t"
"sbcs x4, x4, x8\n\t"
"stp x2, x3, [%[a], 0]\n\t"
"sbcs x5, x5, x9\n\t"
"stp x4, x5, [%[a], 16]\n\t"
"csetm %[a], cc\n\t"
: [a] "+r" (a)
: [b] "r" (b)
: "memory", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "cc"
);
return (sp_digit)a;
}
/* Conditionally subtract b from a using the mask m.
* m is -1 to subtract and 0 when not copying.
*
* r A single precision number representing condition subtract result.
* a A single precision number to subtract from.
* b A single precision number to subtract.
* m Mask value to apply.
*/
static sp_digit sp_256_cond_sub_sm2_4(sp_digit* r, const sp_digit* a, const sp_digit* b,
sp_digit m)
{
__asm__ __volatile__ (
"ldp x5, x7, [%[b], 0]\n\t"
"ldp x11, x12, [%[b], 16]\n\t"
"ldp x4, x6, [%[a], 0]\n\t"
"and x5, x5, %[m]\n\t"
"ldp x9, x10, [%[a], 16]\n\t"
"and x7, x7, %[m]\n\t"
"subs x4, x4, x5\n\t"
"and x11, x11, %[m]\n\t"
"sbcs x6, x6, x7\n\t"
"and x12, x12, %[m]\n\t"
"sbcs x9, x9, x11\n\t"
"stp x4, x6, [%[r], 0]\n\t"
"sbcs x10, x10, x12\n\t"
"stp x9, x10, [%[r], 16]\n\t"
"csetm %[r], cc\n\t"
: [r] "+r" (r)
: [a] "r" (a), [b] "r" (b), [m] "r" (m)
: "memory", "x4", "x6", "x5", "x7", "x8", "x9", "x10", "x11", "x12", "cc"
);
return (sp_digit)r;
}
/* Mul a by digit b into r. (r = a * b)
*
* r A single precision integer.
* a A single precision integer.
* b A single precision digit.
*/
static void sp_256_mul_d_sm2_4(sp_digit* r, const sp_digit* a,
sp_digit b)
{
__asm__ __volatile__ (
"# A[0] * B\n\t"
"ldp x2, x3, [%[a]]\n\t"
"ldp x4, x5, [%[a], 16]\n\t"
"umulh x7, %[b], x2\n\t"
"mul x2, %[b], x2\n\t"
"# A[1] * B\n\t"
"mul x8, %[b], x3\n\t"
"umulh x9, %[b], x3\n\t"
"adds x3, x7, x8\n\t"
"# A[2] * B\n\t"
"mul x8, %[b], x4\n\t"
"adc x7, xzr, x9\n\t"
"umulh x9, %[b], x4\n\t"
"adds x4, x7, x8\n\t"
"# A[3] * B\n\t"
"mul x8, %[b], x5\n\t"
"adc x7, xzr, x9\n\t"
"umulh x9, %[b], x5\n\t"
"adds x5, x7, x8\n\t"
"str x2, [%[r]]\n\t"
"adc x6, xzr, x9\n\t"
"stp x3, x4, [%[r], 8]\n\t"
"stp x5, x6, [%[r], 24]\n\t"
:
: [r] "r" (r), [a] "r" (a), [b] "r" (b)
: "memory", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "cc"
);
}
/* Divide the double width number (d1|d0) by the divisor. (d1|d0 / div)
*
* Assumes divisor has highest bit set.
*
* d1 The high order half of the number to divide.
* d0 The low order half of the number to divide.
* div The divisor.
* returns the result of the division.
*/
static sp_digit div_256_word_4(sp_digit d1, sp_digit d0, sp_digit div)
{
__asm__ __volatile__ (
"lsr x8, %[div], 32\n\t"
"add x5, x8, 1\n\t"
"udiv x3, %[d1], x5\n\t"
"lsl x7, %[div], 32\n\t"
"movz x9, #1, lsl 32\n\t"
"lsl x6, x3, 32\n\t"
"mul x4, %[div], x6\n\t"
"umulh x3, %[div], x6\n\t"
"subs %[d0], %[d0], x4\n\t"
"sbc %[d1], %[d1], x3\n\t"
"cmp %[d1], x5\n\t"
"cset x9, ge\n\t"
"csetm x10, ge\n\t"
"lsl x9, x9, #32\n\t"
"and x7, x7, x10\n\t"
"and x8, x8, x10\n\t"
"subs %[d0], %[d0], x7\n\t"
"add x6, x6, x9\n\t"
"sbc %[d1], %[d1], x8\n\t"
"extr x3, %[d1], %[d0], 32\n\t"
"udiv x3, x3, x5\n\t"
"add x6, x6, x3\n\t"
"mul x4, %[div], x3\n\t"
"umulh x3, %[div], x3\n\t"
"subs %[d0], %[d0], x4\n\t"
"sbc %[d1], %[d1], x3\n\t"
"extr x3, %[d1], %[d0], 32\n\t"
"udiv x3, x3, x5\n\t"
"add x6, x6, x3\n\t"
"mul x4, %[div], x3\n\t"
"sub %[d0], %[d0], x4\n\t"
"udiv x3, %[d0], %[div]\n\t"
"add %[d1], x6, x3\n\t"
: [d1] "+r" (d1), [d0] "+r" (d0)
: [div] "r" (div)
: "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "cc"
);
return d1;
}
/* AND m into each word of a and store in r.
*
* r A single precision integer.
* a A single precision integer.
* m Mask to AND against each digit.
*/
static void sp_256_mask_4(sp_digit* r, const sp_digit* a, sp_digit m)
{
#ifdef WOLFSSL_SP_SMALL
int i;
for (i=0; i<4; i++) {
r[i] = a[i] & m;
}
#else
r[0] = a[0] & m;
r[1] = a[1] & m;
r[2] = a[2] & m;
r[3] = a[3] & m;
#endif
}
/* Compare a with b in constant time.
*
* a A single precision integer.
* b A single precision integer.
* return -ve, 0 or +ve if a is less than, equal to or greater than b
* respectively.
*/
static sp_int64 sp_256_cmp_sm2_4(const sp_digit* a, const sp_digit* b)
{
#ifdef WOLFSSL_SP_SMALL
__asm__ __volatile__ (
"mov x3, #0\n\t"
"mov x2, #-1\n\t"
"mov x10, #4\n\t"
"add %[a], %[a], #16\n\t"
"add %[b], %[b], #16\n\t"
"1:\n\t"
"ldp x6, x7, [%[a]], -16\n\t"
"ldp x8, x9, [%[b]], -16\n\t"
"subs x7, x7, x9\n\t"
"csel x4, x2, xzr, lo\n\t"
"csetm x5, eq\n\t"
"orr x3, x3, x4\n\t"
"and x2, x2, x5\n\t"
"subs x6, x6, x8\n\t"
"csel x4, x2, xzr, lo\n\t"
"csetm x5, eq\n\t"
"orr x3, x3, x4\n\t"
"and x2, x2, x5\n\t"
"subs x10, x10, #2\n\t"
"b.ne 1b\n\t"
"cmp x2, #0\n\t"
"cset %[a], eq\n\t"
"orr %[a], %[a], x3\n\t"
: [a] "+r" (a), [b] "+r" (b)
:
: "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "cc"
);
#else
__asm__ __volatile__ (
"mov x3, #0\n\t"
"mov x2, #-1\n\t"
"ldp x6, x7, [%[a], 16]\n\t"
"ldp x8, x9, [%[b], 16]\n\t"
"subs x7, x7, x9\n\t"
"csel x4, x2, xzr, lo\n\t"
"csetm x5, eq\n\t"
"orr x3, x3, x4\n\t"
"and x2, x2, x5\n\t"
"subs x6, x6, x8\n\t"
"csel x4, x2, xzr, lo\n\t"
"csetm x5, eq\n\t"
"orr x3, x3, x4\n\t"
"and x2, x2, x5\n\t"
"ldp x6, x7, [%[a], 0]\n\t"
"ldp x8, x9, [%[b], 0]\n\t"
"subs x7, x7, x9\n\t"
"csel x4, x2, xzr, lo\n\t"
"csetm x5, eq\n\t"
"orr x3, x3, x4\n\t"
"and x2, x2, x5\n\t"
"subs x6, x6, x8\n\t"
"csel x4, x2, xzr, lo\n\t"
"csetm x5, eq\n\t"
"orr x3, x3, x4\n\t"
"and x2, x2, x5\n\t"
"cmp x2, #0\n\t"
"cset %[a], eq\n\t"
"orr %[a], %[a], x3\n\t"
: [a] "+r" (a)
: [b] "r" (b)
: "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "cc"
);
#endif
return (sp_int64)a;
}
/* Divide d in a and put remainder into r (m*d + r = a)
* m is not calculated as it is not needed at this time.
*
* a Number to be divided.
* d Number to divide with.
* m Multiplier result.
* r Remainder from the division.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_256_div_sm2_4(const sp_digit* a, const sp_digit* d,
sp_digit* m, sp_digit* r)
{
sp_digit t1[8], t2[5];
sp_digit div, r1;
int i;
(void)m;
div = d[3];
XMEMCPY(t1, a, sizeof(*t1) * 2 * 4);
r1 = sp_256_cmp_sm2_4(&t1[4], d) >= 0;
sp_256_cond_sub_sm2_4(&t1[4], &t1[4], d, (sp_digit)0 - r1);
for (i = 3; i >= 0; i--) {
volatile sp_digit mask = (sp_digit)0 - (t1[4 + i] == div);
sp_digit hi = t1[4 + i] + mask;
r1 = div_256_word_4(hi, t1[4 + i - 1], div);
r1 |= mask;
sp_256_mul_d_sm2_4(t2, d, r1);
t1[4 + i] += sp_256_sub_in_place_sm2_4(&t1[i], t2);
t1[4 + i] -= t2[4];
sp_256_mask_4(t2, d, t1[4 + i]);
t1[4 + i] += sp_256_add_sm2_4(&t1[i], &t1[i], t2);
sp_256_mask_4(t2, d, t1[4 + i]);
t1[4 + i] += sp_256_add_sm2_4(&t1[i], &t1[i], t2);
}
r1 = sp_256_cmp_sm2_4(t1, d) >= 0;
sp_256_cond_sub_sm2_4(r, t1, d, (sp_digit)0 - r1);
return MP_OKAY;
}
/* Reduce a modulo m into r. (r = a mod m)
*
* r A single precision number that is the reduced result.
* a A single precision number that is to be reduced.
* m A single precision number that is the modulus to reduce with.
* returns MP_OKAY indicating success.
*/
static WC_INLINE int sp_256_mod_sm2_4(sp_digit* r, const sp_digit* a, const sp_digit* m)
{
return sp_256_div_sm2_4(a, m, NULL, r);
}
/* Multiply a number by Montgomery normalizer mod modulus (prime).
*
* r The resulting Montgomery form number.
* a The number to convert.
* m The modulus (prime).
* returns MEMORY_E when memory allocation fails and MP_OKAY otherwise.
*/
static int sp_256_mod_mul_norm_sm2_4(sp_digit* r, const sp_digit* a,
const sp_digit* m)
{
sp_256_mul_sm2_4(r, a, p256_sm2_norm_mod);
return sp_256_mod_sm2_4(r, r, m);
}
/* Convert an mp_int to an array of sp_digit.
*
* r A single precision integer.
* size Maximum number of bytes to convert
* a A multi-precision integer.
*/
static void sp_256_from_mp(sp_digit* r, int size, const mp_int* a)
{
#if DIGIT_BIT == 64
int i;
sp_digit j = (sp_digit)0 - (sp_digit)a->used;
int o = 0;
for (i = 0; i < size; i++) {
sp_digit mask = (sp_digit)0 - (j >> 63);
r[i] = a->dp[o] & mask;
j++;
o += (int)(j >> 63);
}
#elif DIGIT_BIT > 64
unsigned int i;
int j = 0;
word32 s = 0;
r[0] = 0;
for (i = 0; i < (unsigned int)a->used && j < size; i++) {
r[j] |= ((sp_digit)a->dp[i] << s);
r[j] &= 0xffffffffffffffffl;
s = 64U - s;
if (j + 1 >= size) {
break;
}
/* lint allow cast of mismatch word32 and mp_digit */
r[++j] = (sp_digit)(a->dp[i] >> s); /*lint !e9033*/
while ((s + 64U) <= (word32)DIGIT_BIT) {
s += 64U;
r[j] &= 0xffffffffffffffffl;
if (j + 1 >= size) {
break;
}
if (s < (word32)DIGIT_BIT) {
/* lint allow cast of mismatch word32 and mp_digit */
r[++j] = (sp_digit)(a->dp[i] >> s); /*lint !e9033*/
}
else {
r[++j] = (sp_digit)0;
}
}
s = (word32)DIGIT_BIT - s;
}
for (j++; j < size; j++) {
r[j] = 0;
}
#else
unsigned int i;
int j = 0;
int s = 0;
r[0] = 0;
for (i = 0; i < (unsigned int)a->used && j < size; i++) {
r[j] |= ((sp_digit)a->dp[i]) << s;
if (s + DIGIT_BIT >= 64) {
r[j] &= 0xffffffffffffffffl;
if (j + 1 >= size) {
break;
}
s = 64 - s;
if (s == DIGIT_BIT) {
r[++j] = 0;
s = 0;
}
else {
r[++j] = a->dp[i] >> s;
s = DIGIT_BIT - s;
}
}
else {
s += DIGIT_BIT;
}
}
for (j++; j < size; j++) {
r[j] = 0;
}
#endif
}
/* Convert a point of type ecc_point to type sp_point_256.
*
* p Point of type sp_point_256 (result).
* pm Point of type ecc_point.
*/
static void sp_256_point_from_ecc_point_4(sp_point_256* p,
const ecc_point* pm)
{
XMEMSET(p->x, 0, sizeof(p->x));
XMEMSET(p->y, 0, sizeof(p->y));
XMEMSET(p->z, 0, sizeof(p->z));
sp_256_from_mp(p->x, 4, pm->x);
sp_256_from_mp(p->y, 4, pm->y);
sp_256_from_mp(p->z, 4, pm->z);
p->infinity = 0;
}
/* Convert an array of sp_digit to an mp_int.
*
* a A single precision integer.
* r A multi-precision integer.
*/
static int sp_256_to_mp(const sp_digit* a, mp_int* r)
{
int err;
err = mp_grow(r, (256 + DIGIT_BIT - 1) / DIGIT_BIT);
if (err == MP_OKAY) { /*lint !e774 case where err is always MP_OKAY*/
#if DIGIT_BIT == 64
XMEMCPY(r->dp, a, sizeof(sp_digit) * 4);
r->used = 4;
mp_clamp(r);
#elif DIGIT_BIT < 64
int i;
int j = 0;
int s = 0;
r->dp[0] = 0;
for (i = 0; i < 4; i++) {
r->dp[j] |= (mp_digit)(a[i] << s);
r->dp[j] &= ((sp_digit)1 << DIGIT_BIT) - 1;
s = DIGIT_BIT - s;
r->dp[++j] = (mp_digit)(a[i] >> s);
while (s + DIGIT_BIT <= 64) {
s += DIGIT_BIT;
r->dp[j++] &= ((sp_digit)1 << DIGIT_BIT) - 1;
if (s == SP_WORD_SIZE) {
r->dp[j] = 0;
}
else {
r->dp[j] = (mp_digit)(a[i] >> s);
}
}
s = 64 - s;
}
r->used = (256 + DIGIT_BIT - 1) / DIGIT_BIT;
mp_clamp(r);
#else
int i;
int j = 0;
int s = 0;
r->dp[0] = 0;
for (i = 0; i < 4; i++) {
r->dp[j] |= ((mp_digit)a[i]) << s;
if (s + 64 >= DIGIT_BIT) {
#if DIGIT_BIT != 32 && DIGIT_BIT != 64
r->dp[j] &= ((sp_digit)1 << DIGIT_BIT) - 1;
#endif
s = DIGIT_BIT - s;
r->dp[++j] = a[i] >> s;
s = 64 - s;
}
else {
s += 64;
}
}
r->used = (256 + DIGIT_BIT - 1) / DIGIT_BIT;
mp_clamp(r);
#endif
}
return err;
}
/* Convert a point of type sp_point_256 to type ecc_point.
*
* p Point of type sp_point_256.
* pm Point of type ecc_point (result).
* returns MEMORY_E when allocation of memory in ecc_point fails otherwise
* MP_OKAY.
*/
static int sp_256_point_to_ecc_point_4(const sp_point_256* p, ecc_point* pm)
{
int err;
err = sp_256_to_mp(p->x, pm->x);
if (err == MP_OKAY) {
err = sp_256_to_mp(p->y, pm->y);
}
if (err == MP_OKAY) {
err = sp_256_to_mp(p->z, pm->z);
}
return err;
}
/* Conditionally copy a into r using the mask m.
* m is -1 to copy and 0 when not.
*
* r A single precision number to copy over.
* a A single precision number to copy.
* m Mask value to apply.
*/
static void sp_256_cond_copy_sm2_4(sp_digit* r, const sp_digit* a, sp_digit m)
{
__asm__ __volatile__ (
"ldp x3, x4, [%[r], 0]\n\t"
"ldp x7, x8, [%[a], 0]\n\t"
"eor x7, x7, x3\n\t"
"ldp x5, x6, [%[r], 16]\n\t"
"eor x8, x8, x4\n\t"
"ldp x9, x10, [%[a], 16]\n\t"
"eor x9, x9, x5\n\t"
"eor x10, x10, x6\n\t"
"and x7, x7, %[m]\n\t"