-
Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathtfm.c
5936 lines (5150 loc) · 132 KB
/
tfm.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
/* tfm.c
*
* Copyright (C) 2006-2023 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
*/
/*
* Based on public domain TomsFastMath 0.10 by Tom St Denis, [email protected],
* http://math.libtomcrypt.com
*/
/**
* Edited by Moises Guimaraes ([email protected])
* to fit wolfSSL's needs.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* in case user set USE_FAST_MATH there */
#include <wolfssl/wolfcrypt/settings.h>
#ifdef USE_FAST_MATH
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/tfm.h>
#include <wolfcrypt/src/asm.c> /* will define asm MACROS or C ones */
#include <wolfssl/wolfcrypt/wolfmath.h> /* common functions */
#if defined(FREESCALE_LTC_TFM)
#include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
#endif
#ifdef WOLFSSL_DEBUG_MATH
#include <stdio.h>
#endif
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
#ifdef __cplusplus
extern "C" {
#endif
WOLFSSL_LOCAL int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_4096(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
#if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
/* math settings check */
word32 CheckRunTimeSettings(void)
{
return CTC_SETTINGS;
}
/* math settings size check */
word32 CheckRunTimeFastMath(void)
{
return FP_SIZE;
}
#endif
/* Functions */
int fp_add(fp_int *a, fp_int *b, fp_int *c)
{
int sa, sb;
int ret = FP_OKAY;
/* get sign of both inputs */
sa = a->sign;
sb = b->sign;
/* handle two cases, not four */
if (sa == sb) {
/* both positive or both negative */
/* add their magnitudes, copy the sign */
c->sign = sa;
ret = s_fp_add (a, b, c);
} else {
/* one positive, the other negative */
/* subtract the one with the greater magnitude from */
/* the one of the lesser magnitude. The result gets */
/* the sign of the one with the greater magnitude. */
if (fp_cmp_mag (a, b) == FP_LT) {
c->sign = sb;
s_fp_sub (b, a, c);
} else {
c->sign = sa;
s_fp_sub (a, b, c);
}
}
return ret;
}
/* unsigned addition */
int s_fp_add(fp_int *a, fp_int *b, fp_int *c)
{
int x, y, oldused;
fp_word t;
y = MAX(a->used, b->used);
oldused = MIN(c->used, FP_SIZE); /* help static analysis w/ largest size */
c->used = y;
t = 0;
for (x = 0; x < y; x++) {
t += ((fp_word)a->dp[x]) + ((fp_word)b->dp[x]);
c->dp[x] = (fp_digit)t;
t >>= DIGIT_BIT;
}
if (t != 0) {
if (x == FP_SIZE)
return FP_VAL;
c->dp[c->used++] = (fp_digit)t;
++x;
}
c->used = x;
/* zero any excess digits on the destination that we didn't write to */
for (; x < oldused; x++) {
c->dp[x] = 0;
}
fp_clamp(c);
return FP_OKAY;
}
/* c = a - b */
int fp_sub(fp_int *a, fp_int *b, fp_int *c)
{
int sa, sb;
int ret = FP_OKAY;
sa = a->sign;
sb = b->sign;
if (sa != sb) {
/* subtract a negative from a positive, OR */
/* subtract a positive from a negative. */
/* In either case, ADD their magnitudes, */
/* and use the sign of the first number. */
c->sign = sa;
ret = s_fp_add (a, b, c);
} else {
/* subtract a positive from a positive, OR */
/* subtract a negative from a negative. */
/* First, take the difference between their */
/* magnitudes, then... */
if (fp_cmp_mag (a, b) != FP_LT) {
/* Copy the sign from the first */
c->sign = sa;
/* The first has a larger or equal magnitude */
s_fp_sub (a, b, c);
} else {
/* The result has the *opposite* sign from */
/* the first number. */
c->sign = (sa == FP_ZPOS) ? FP_NEG : FP_ZPOS;
/* The second has a larger magnitude */
s_fp_sub (b, a, c);
}
}
return ret;
}
/* unsigned subtraction ||a|| >= ||b|| ALWAYS! */
void s_fp_sub(fp_int *a, fp_int *b, fp_int *c)
{
int x, oldbused, oldused;
fp_word t;
oldused = c->used;
oldbused = b->used;
c->used = a->used;
t = 0;
for (x = 0; x < oldbused; x++) {
t = ((fp_word)a->dp[x]) - (((fp_word)b->dp[x]) + t);
c->dp[x] = (fp_digit)t;
t = (t >> DIGIT_BIT)&1;
}
for (; x < a->used; x++) {
t = ((fp_word)a->dp[x]) - t;
c->dp[x] = (fp_digit)t;
t = (t >> DIGIT_BIT)&1;
}
/* zero any excess digits on the destination that we didn't write to */
for (; x < oldused; x++) {
c->dp[x] = 0;
}
fp_clamp(c);
}
/* c = a * b */
int fp_mul(fp_int *A, fp_int *B, fp_int *C)
{
int ret = 0;
int y, yy, oldused;
#if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
!defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
ret = esp_mp_mul(A, B, C);
if(ret != -2) return ret;
#endif
oldused = C->used;
y = MAX(A->used, B->used);
yy = MIN(A->used, B->used);
/* fail if we are out of range */
if (y + yy >= FP_SIZE) {
ret = FP_VAL;
goto clean;
}
/* pick a comba (unrolled 4/8/16/32 x or rolled) based on the size
of the largest input. We also want to avoid doing excess mults if the
inputs are not close to the next power of two. That is, for example,
if say y=17 then we would do (32-17)^2 = 225 unneeded multiplications
*/
#if defined(TFM_MUL3) && FP_SIZE >= 6
if (y <= 3) {
ret = fp_mul_comba3(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL4) && FP_SIZE >= 8
if (y == 4) {
ret = fp_mul_comba4(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL6) && FP_SIZE >= 12
if (y <= 6) {
ret = fp_mul_comba6(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL7) && FP_SIZE >= 14
if (y == 7) {
ret = fp_mul_comba7(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL8) && FP_SIZE >= 16
if (y == 8) {
ret = fp_mul_comba8(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL9) && FP_SIZE >= 18
if (y == 9) {
ret = fp_mul_comba9(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL12) && FP_SIZE >= 24
if (y <= 12) {
ret = fp_mul_comba12(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL17) && FP_SIZE >= 34
if (y <= 17) {
ret = fp_mul_comba17(A,B,C);
goto clean;
}
#endif
#if defined(TFM_SMALL_SET) && FP_SIZE >= 32
if (y <= 16) {
ret = fp_mul_comba_small(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL20) && FP_SIZE >= 40
if (y <= 20) {
ret = fp_mul_comba20(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL24) && FP_SIZE >= 48
if (yy >= 16 && y <= 24) {
ret = fp_mul_comba24(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL28) && FP_SIZE >= 56
if (yy >= 20 && y <= 28) {
ret = fp_mul_comba28(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL32) && FP_SIZE >= 64
if (yy >= 24 && y <= 32) {
ret = fp_mul_comba32(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL48) && FP_SIZE >= 96
if (yy >= 40 && y <= 48) {
ret = fp_mul_comba48(A,B,C);
goto clean;
}
#endif
#if defined(TFM_MUL64) && FP_SIZE >= 128
if (yy >= 56 && y <= 64) {
ret = fp_mul_comba64(A,B,C);
goto clean;
}
#endif
ret = fp_mul_comba(A,B,C);
clean:
/* zero any excess digits on the destination that we didn't write to */
for (y = C->used; y >= 0 && y < oldused; y++) {
C->dp[y] = 0;
}
return ret;
}
int fp_mul_2(fp_int * a, fp_int * b)
{
int x, oldused;
/* Make sure value to double and result are in range. */
if ((a->used > (FP_SIZE-1)) || ((a->used == (FP_SIZE - 1)) &&
((a->dp[FP_SIZE - 1] & ((fp_digit)1 << (DIGIT_BIT - 1))) != 0))) {
return FP_VAL;
}
oldused = b->used;
b->used = a->used;
{
fp_digit r, rr, *tmpa, *tmpb;
/* alias for source */
tmpa = a->dp;
/* alias for dest */
tmpb = b->dp;
/* carry */
r = 0;
for (x = 0; x < a->used; x++) {
/* get what will be the *next* carry bit from the
* MSB of the current digit
*/
rr = *tmpa >> ((fp_digit)(DIGIT_BIT - 1));
/* now shift up this digit, add in the carry [from the previous] */
*tmpb++ = ((*tmpa++ << ((fp_digit)1)) | r);
/* copy the carry that would be from the source
* digit into the next iteration
*/
r = rr;
}
/* new leading digit? */
if (r != 0) {
/* add a MSB which is always 1 at this point */
*tmpb = 1;
++(b->used);
}
/* zero any excess digits on the destination that we didn't write to */
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
}
}
b->sign = a->sign;
return FP_OKAY;
}
/* c = a * b */
int fp_mul_d(fp_int *a, fp_digit b, fp_int *c)
{
fp_word w;
int x, oldused;
oldused = c->used;
c->used = a->used;
c->sign = a->sign;
w = 0;
for (x = 0; x < a->used; x++) {
w = ((fp_word)a->dp[x]) * ((fp_word)b) + w;
c->dp[x] = (fp_digit)w;
w = w >> DIGIT_BIT;
}
if (w != 0) {
if (a->used == FP_SIZE)
return FP_VAL;
c->dp[c->used++] = (fp_digit) w;
++x;
}
/* zero any excess digits on the destination that we didn't write to */
/* also checking FP_SIZE here for static analysis */
for (; x < oldused && x < FP_SIZE; x++) {
c->dp[x] = 0;
}
fp_clamp(c);
return FP_OKAY;
}
/* c = a * 2**d */
int fp_mul_2d(fp_int *a, int b, fp_int *c)
{
fp_digit carry, carrytmp, shift;
int x;
/* copy it */
fp_copy(a, c);
/* handle whole digits */
if (b >= DIGIT_BIT) {
int ret = fp_lshd(c, b/DIGIT_BIT);
if (ret != FP_OKAY)
return ret;
}
b %= DIGIT_BIT;
/* shift the digits */
if (b != 0) {
carry = 0;
shift = DIGIT_BIT - b;
for (x = 0; x < c->used; x++) {
carrytmp = c->dp[x] >> shift;
c->dp[x] = (c->dp[x] << b) + carry;
carry = carrytmp;
}
/* store last carry if room */
if (carry && x < FP_SIZE) {
c->dp[c->used++] = carry;
}
if (x == FP_SIZE)
return FP_VAL;
}
fp_clamp(c);
return FP_OKAY;
}
/* generic PxQ multiplier */
#if defined(HAVE_INTEL_MULX)
WC_INLINE static int fp_mul_comba_mulx(fp_int *A, fp_int *B, fp_int *C)
{
int ix, iy, iz, pa;
fp_int *dst;
#ifndef WOLFSSL_SMALL_STACK
fp_int tmp[1];
#else
fp_int *tmp;
#endif
fp_digit carry;
/* Variables used but not seen by cppcheck. */
(void)ix; (void)iy; (void)iz;
#ifdef WOLFSSL_SMALL_STACK
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (tmp == NULL)
return FP_MEM;
#endif
/* get size of output and trim */
pa = A->used + B->used;
if (pa >= FP_SIZE) {
pa = FP_SIZE-1;
}
/* Always take branch to use tmp variable. This avoids a cache attack for
* determining if C equals A */
if (1) {
fp_init(tmp);
dst = tmp;
}
TFM_INTEL_MUL_COMBA(A, B, carry, dst) ;
dst->used = pa;
dst->sign = A->sign ^ B->sign;
fp_clamp(dst);
fp_copy(dst, C);
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
#endif
int fp_mul_comba(fp_int *A, fp_int *B, fp_int *C)
{
int ret = 0;
int ix, iy, iz, tx, ty, pa;
fp_digit c0, c1, c2, *tmpx, *tmpy;
fp_int *dst;
#ifndef WOLFSSL_SMALL_STACK
fp_int tmp[1];
#else
fp_int *tmp;
#endif
if (A->used + B->used >= FP_SIZE) return FP_VAL;
IF_HAVE_INTEL_MULX(ret = fp_mul_comba_mulx(A, B, C), return ret) ;
#ifdef WOLFSSL_SMALL_STACK
tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (tmp == NULL)
return FP_MEM;
#endif
COMBA_START;
COMBA_CLEAR;
/* get size of output and trim */
pa = A->used + B->used;
if (pa >= FP_SIZE) {
pa = FP_SIZE-1;
}
/* Always take branch to use tmp variable. This avoids a cache attack for
* determining if C equals A */
if (1) {
fp_init(tmp);
dst = tmp;
}
for (ix = 0; ix < pa; ix++) {
/* get offsets into the two bignums */
ty = MIN(ix, (B->used > 0 ? B->used - 1 : 0));
tx = ix - ty;
/* setup temp aliases */
tmpx = A->dp + tx;
tmpy = B->dp + ty;
/* this is the number of times the loop will iterate, essentially its
while (tx++ < a->used && ty-- >= 0) { ... }
*/
iy = MIN(A->used-tx, ty+1);
/* execute loop */
COMBA_FORWARD;
for (iz = 0; iz < iy; ++iz) {
fp_digit _tmpx = *tmpx++;
fp_digit _tmpy = *tmpy--;
MULADD(_tmpx, _tmpy);
}
/* store term */
COMBA_STORE(dst->dp[ix]);
}
COMBA_FINI;
dst->used = pa;
dst->sign = A->sign ^ B->sign;
fp_clamp(dst);
fp_copy(dst, C);
/* Variables used but not seen by cppcheck. */
(void)c0; (void)c1; (void)c2;
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
/* a/b => cb + d == a */
int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
{
int n, t, i, norm, neg;
int ret;
#ifndef WOLFSSL_SMALL_STACK
fp_int q[1], x[1], y[1], t1[1], t2[1];
#else
fp_int *q, *x, *y, *t1, *t2;
#endif
/* is divisor zero ? */
if (fp_iszero (b) == FP_YES) {
return FP_VAL;
}
/* if a < b then q=0, r = a */
if (fp_cmp_mag (a, b) == FP_LT)
{
if (d != NULL) {
fp_copy (a, d);
}
if (c != NULL) {
fp_zero (c);
}
return FP_OKAY;
}
#ifdef WOLFSSL_SMALL_STACK /* 0 1 2 3 4 */
/* allocate 5 elements of fp_int for q, x, y, t1, t2 */
q = (fp_int*)XMALLOC(sizeof(fp_int) * 5, NULL, DYNAMIC_TYPE_BIGINT);
if (q == NULL) {
return FP_MEM;
}
x = &q[1]; y = &q[2]; t1 = &q[3]; t2 = &q[4];
#endif
fp_init(q);
/* qb + d = a, and b is an integer > 0, therefore q <= a */
q->used = a->used;
fp_init(t1);
fp_init(t2);
/* Init a copy (y) of the input (b) and
** Init a copy (x) of the input (a)
**
** ALERT: Not calling fp_init_copy() as some compiler optimization settings
** such as -O2 will complain that (x) or (y) "may be used uninitialized".
** The fp_init() is here only to appease the compiler. */
fp_init(x);
fp_copy(a, x); /* copy (src = a) to (dst = x) */
fp_init(y);
fp_copy(b, y); /* copy (src = b) to (dst = y) */
/* fix the sign */
neg = (a->sign == b->sign) ? FP_ZPOS : FP_NEG;
x->sign = y->sign = FP_ZPOS;
/* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
norm = fp_count_bits(y) % DIGIT_BIT;
if (norm < (int)(DIGIT_BIT-1)) {
norm = (DIGIT_BIT-1) - norm;
ret = fp_mul_2d (x, norm, x);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
ret = fp_mul_2d (y, norm, y);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
} else {
norm = 0;
}
/* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
n = x->used - 1;
t = y->used - 1;
/* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
ret = fp_lshd (y, n - t); /* y = y*b**{n-t} */
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
while (fp_cmp (x, y) != FP_LT) {
++(q->dp[n - t]);
ret = fp_sub (x, y, x);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
}
/* reset y by shifting it back down */
fp_rshd (y, n - t);
/* step 3. for i from n down to (t + 1) */
for (i = n; i >= (t + 1); i--) {
if (i > x->used) {
continue;
}
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
if (x->dp[i] == y->dp[t]) {
q->dp[i - t - 1] = (fp_digit) ((((fp_word)1) << DIGIT_BIT) - 1);
} else {
fp_word tmp;
tmp = ((fp_word) x->dp[i]) << ((fp_word) DIGIT_BIT);
tmp |= ((fp_word) x->dp[i - 1]);
#ifdef WOLFSSL_LINUXKM
/* Linux kernel macro for in-place 64 bit integer division. */
do_div(tmp, (fp_word)y->dp[t]);
#else
tmp /= ((fp_word)y->dp[t]);
#endif
q->dp[i - t - 1] = (fp_digit) (tmp);
}
/* while (q{i-t-1} * (yt * b + y{t-1})) >
xi * b**2 + xi-1 * b + xi-2
do q{i-t-1} -= 1;
*/
q->dp[i - t - 1] = (q->dp[i - t - 1] + 1);
do {
q->dp[i - t - 1] = (q->dp[i - t - 1] - 1);
/* find left hand */
fp_zero (t1);
t1->dp[0] = (t - 1 < 0) ? 0 : y->dp[t - 1];
t1->dp[1] = y->dp[t];
t1->used = 2;
ret = fp_mul_d (t1, q->dp[i - t - 1], t1);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
/* find right hand */
t2->dp[0] = (i - 2 < 0) ? 0 : x->dp[i - 2];
t2->dp[1] = (i - 1 < 0) ? 0 : x->dp[i - 1];
t2->dp[2] = x->dp[i];
t2->used = 3;
} while (fp_cmp_mag(t1, t2) == FP_GT);
/* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
ret = fp_mul_d (y, q->dp[i - t - 1], t1);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
ret = fp_lshd (t1, i - t - 1);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
ret = fp_sub (x, t1, x);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
/* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
if (x->sign == FP_NEG) {
fp_copy (y, t1);
ret = fp_lshd (t1, i - t - 1);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
ret = fp_add (x, t1, x);
if (ret != FP_OKAY) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return ret;
}
q->dp[i - t - 1] = q->dp[i - t - 1] - 1;
}
}
/* now q is the quotient and x is the remainder
* [which we have to normalize]
*/
/* get sign before writing to c */
x->sign = x->used == 0 ? FP_ZPOS : a->sign;
if (c != NULL) {
fp_clamp (q);
fp_copy (q, c);
c->sign = neg;
}
if (d != NULL) {
fp_div_2d (x, norm, x, NULL);
/* zero any excess digits on the destination that we didn't write to */
for (i = b->used; i < x->used; i++) {
x->dp[i] = 0;
}
fp_clamp(x);
fp_copy (x, d);
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return FP_OKAY;
}
/* b = a/2 */
void fp_div_2(fp_int * a, fp_int * b)
{
int x, oldused;
oldused = b->used;
b->used = a->used;
{
fp_digit r, rr, *tmpa, *tmpb;
/* source alias */
tmpa = a->dp + b->used - 1;
/* dest alias */
tmpb = b->dp + b->used - 1;
/* carry */
r = 0;
for (x = b->used - 1; x >= 0; x--) {
/* get the carry for the next iteration */
rr = *tmpa & 1;
/* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
/* forward carry to next iteration */
r = rr;
}
/* zero any excess digits on the destination that we didn't write to */
tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) {
*tmpb++ = 0;
}
}
b->sign = a->sign;
fp_clamp (b);
}
/* c = a / 2 (mod b) - constant time (a < b and positive) */
int fp_div_2_mod_ct(fp_int *a, fp_int *b, fp_int *c)
{
fp_word w = 0;
fp_digit mask;
int i;
mask = 0 - (a->dp[0] & 1);
for (i = 0; i < b->used; i++) {
fp_digit mask_a = 0 - (i < a->used);
w += b->dp[i] & mask;
w += a->dp[i] & mask_a;
c->dp[i] = (fp_digit)w;
w >>= DIGIT_BIT;
}
for (i = 0; i < b->used-1; i++) {
c->dp[i] = (c->dp[i] >> 1) | (c->dp[i+1] << (DIGIT_BIT - 1));
}
c->dp[i] = (c->dp[i] >> 1) | ((fp_digit)w << (DIGIT_BIT - 1));
c->used = i + 1;
c->sign = FP_ZPOS;
fp_clamp(c);
return FP_OKAY;
}
/* c = a / 2**b */
void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d)
{
int D;
/* if the shift count is <= 0 then we do no work */
if (b <= 0) {
fp_copy (a, c);
if (d != NULL) {
fp_zero (d);
}
return;
}
/* get the remainder before a is changed in calculating c */
if (a == c && d != NULL) {
fp_mod_2d (a, b, d);
}
/* copy */
fp_copy(a, c);
/* shift by as many digits in the bit count */
if (b >= (int)DIGIT_BIT) {
fp_rshd (c, b / DIGIT_BIT);
}
/* shift any bit count < DIGIT_BIT */
D = (b % DIGIT_BIT);
if (D != 0) {
fp_rshb(c, D);
}
/* get the remainder if a is not changed in calculating c */
if (a != c && d != NULL) {
fp_mod_2d (a, b, d);
}
fp_clamp (c);
}
/* c = a mod b, 0 <= c < b */
int fp_mod(fp_int *a, fp_int *b, fp_int *c)
{
#ifndef WOLFSSL_SMALL_STACK
fp_int t[1];
#else
fp_int *t;
#endif
int err;
#ifdef WOLFSSL_SMALL_STACK
t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (t == NULL)
return FP_MEM;
#endif
fp_init(t);
err = fp_div(a, b, NULL, t);
if (err == FP_OKAY) {
if (!fp_iszero(t) && (t->sign != b->sign)) {
err = fp_add(t, b, c);
} else {
fp_copy(t, c);
}
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
#endif
return err;
}
/* c = a mod 2**d */
void fp_mod_2d(fp_int *a, int b, fp_int *c)
{
unsigned int x;
unsigned int bmax;
/* zero if count less than or equal to zero */
if (b <= 0) {
fp_zero(c);