comparison libtommath/bn_mp_invmod_slow.c @ 1655:f52919ffd3b1

update ltm to 1.1.0 and enable FIPS 186.4 compliant key-generation (#79) * make key-generation compliant to FIPS 186.4 * fix includes in tommath_class.h * update fuzzcorpus instead of error-out * fixup fuzzing make-targets * update Makefile.in * apply necessary patches to ltm sources * clean-up not required ltm files * update to vanilla ltm 1.1.0 this already only contains the required files * remove set/get double
author Steffen Jaeckel <s_jaeckel@gmx.de>
date Mon, 16 Sep 2019 15:50:38 +0200
parents 8bba51a55704
children
comparison
equal deleted inserted replaced
1654:cc0fc5131c5c 1655:f52919ffd3b1
1 #include <tommath_private.h> 1 #include "tommath_private.h"
2 #ifdef BN_MP_INVMOD_SLOW_C 2 #ifdef BN_MP_INVMOD_SLOW_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis 3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 * 4 *
5 * LibTomMath is a library that provides multiple-precision 5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality. 6 * integer arithmetic as well as number theoretic functionality.
7 * 7 *
8 * The library was designed directly after the MPI library by 8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with 9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place. 10 * additional optimizations in place.
11 * 11 *
12 * The library is free for all purposes without any express 12 * SPDX-License-Identifier: Unlicense
13 * guarantee it works.
14 *
15 * Tom St Denis, [email protected], http://libtom.org
16 */ 13 */
17 14
18 /* hac 14.61, pp608 */ 15 /* hac 14.61, pp608 */
19 int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) 16 int mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
20 { 17 {
21 mp_int x, y, u, v, A, B, C, D; 18 mp_int x, y, u, v, A, B, C, D;
22 int res; 19 int res;
23 20
24 /* b cannot be negative */ 21 /* b cannot be negative */
25 if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) { 22 if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
26 return MP_VAL; 23 return MP_VAL;
27 } 24 }
28 25
29 /* init temps */ 26 /* init temps */
30 if ((res = mp_init_multi(&x, &y, &u, &v, 27 if ((res = mp_init_multi(&x, &y, &u, &v,
31 &A, &B, &C, &D, NULL)) != MP_OKAY) { 28 &A, &B, &C, &D, NULL)) != MP_OKAY) {
32 return res; 29 return res;
33 } 30 }
34 31
35 /* x = a, y = b */ 32 /* x = a, y = b */
36 if ((res = mp_mod(a, b, &x)) != MP_OKAY) { 33 if ((res = mp_mod(a, b, &x)) != MP_OKAY) {
37 goto LBL_ERR; 34 goto LBL_ERR;
38 } 35 }
39 if ((res = mp_copy (b, &y)) != MP_OKAY) { 36 if ((res = mp_copy(b, &y)) != MP_OKAY) {
40 goto LBL_ERR; 37 goto LBL_ERR;
41 } 38 }
42 39
43 /* 2. [modified] if x,y are both even then return an error! */ 40 /* 2. [modified] if x,y are both even then return an error! */
44 if ((mp_iseven (&x) == MP_YES) && (mp_iseven (&y) == MP_YES)) { 41 if ((mp_iseven(&x) == MP_YES) && (mp_iseven(&y) == MP_YES)) {
45 res = MP_VAL; 42 res = MP_VAL;
46 goto LBL_ERR; 43 goto LBL_ERR;
47 } 44 }
48 45
49 /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ 46 /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
50 if ((res = mp_copy (&x, &u)) != MP_OKAY) { 47 if ((res = mp_copy(&x, &u)) != MP_OKAY) {
51 goto LBL_ERR; 48 goto LBL_ERR;
52 } 49 }
53 if ((res = mp_copy (&y, &v)) != MP_OKAY) { 50 if ((res = mp_copy(&y, &v)) != MP_OKAY) {
54 goto LBL_ERR; 51 goto LBL_ERR;
55 } 52 }
56 mp_set (&A, 1); 53 mp_set(&A, 1uL);
57 mp_set (&D, 1); 54 mp_set(&D, 1uL);
58 55
59 top: 56 top:
60 /* 4. while u is even do */ 57 /* 4. while u is even do */
61 while (mp_iseven (&u) == MP_YES) { 58 while (mp_iseven(&u) == MP_YES) {
62 /* 4.1 u = u/2 */ 59 /* 4.1 u = u/2 */
63 if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { 60 if ((res = mp_div_2(&u, &u)) != MP_OKAY) {
64 goto LBL_ERR;
65 }
66 /* 4.2 if A or B is odd then */
67 if ((mp_isodd (&A) == MP_YES) || (mp_isodd (&B) == MP_YES)) {
68 /* A = (A+y)/2, B = (B-x)/2 */
69 if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
70 goto LBL_ERR; 61 goto LBL_ERR;
71 } 62 }
72 if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { 63 /* 4.2 if A or B is odd then */
64 if ((mp_isodd(&A) == MP_YES) || (mp_isodd(&B) == MP_YES)) {
65 /* A = (A+y)/2, B = (B-x)/2 */
66 if ((res = mp_add(&A, &y, &A)) != MP_OKAY) {
67 goto LBL_ERR;
68 }
69 if ((res = mp_sub(&B, &x, &B)) != MP_OKAY) {
70 goto LBL_ERR;
71 }
72 }
73 /* A = A/2, B = B/2 */
74 if ((res = mp_div_2(&A, &A)) != MP_OKAY) {
73 goto LBL_ERR; 75 goto LBL_ERR;
74 } 76 }
75 } 77 if ((res = mp_div_2(&B, &B)) != MP_OKAY) {
76 /* A = A/2, B = B/2 */
77 if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
78 goto LBL_ERR;
79 }
80 if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
81 goto LBL_ERR;
82 }
83 }
84
85 /* 5. while v is even do */
86 while (mp_iseven (&v) == MP_YES) {
87 /* 5.1 v = v/2 */
88 if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
89 goto LBL_ERR;
90 }
91 /* 5.2 if C or D is odd then */
92 if ((mp_isodd (&C) == MP_YES) || (mp_isodd (&D) == MP_YES)) {
93 /* C = (C+y)/2, D = (D-x)/2 */
94 if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
95 goto LBL_ERR; 78 goto LBL_ERR;
96 } 79 }
97 if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { 80 }
81
82 /* 5. while v is even do */
83 while (mp_iseven(&v) == MP_YES) {
84 /* 5.1 v = v/2 */
85 if ((res = mp_div_2(&v, &v)) != MP_OKAY) {
98 goto LBL_ERR; 86 goto LBL_ERR;
99 } 87 }
100 } 88 /* 5.2 if C or D is odd then */
101 /* C = C/2, D = D/2 */ 89 if ((mp_isodd(&C) == MP_YES) || (mp_isodd(&D) == MP_YES)) {
102 if ((res = mp_div_2 (&C, &C)) != MP_OKAY) { 90 /* C = (C+y)/2, D = (D-x)/2 */
91 if ((res = mp_add(&C, &y, &C)) != MP_OKAY) {
92 goto LBL_ERR;
93 }
94 if ((res = mp_sub(&D, &x, &D)) != MP_OKAY) {
95 goto LBL_ERR;
96 }
97 }
98 /* C = C/2, D = D/2 */
99 if ((res = mp_div_2(&C, &C)) != MP_OKAY) {
100 goto LBL_ERR;
101 }
102 if ((res = mp_div_2(&D, &D)) != MP_OKAY) {
103 goto LBL_ERR;
104 }
105 }
106
107 /* 6. if u >= v then */
108 if (mp_cmp(&u, &v) != MP_LT) {
109 /* u = u - v, A = A - C, B = B - D */
110 if ((res = mp_sub(&u, &v, &u)) != MP_OKAY) {
111 goto LBL_ERR;
112 }
113
114 if ((res = mp_sub(&A, &C, &A)) != MP_OKAY) {
115 goto LBL_ERR;
116 }
117
118 if ((res = mp_sub(&B, &D, &B)) != MP_OKAY) {
119 goto LBL_ERR;
120 }
121 } else {
122 /* v - v - u, C = C - A, D = D - B */
123 if ((res = mp_sub(&v, &u, &v)) != MP_OKAY) {
124 goto LBL_ERR;
125 }
126
127 if ((res = mp_sub(&C, &A, &C)) != MP_OKAY) {
128 goto LBL_ERR;
129 }
130
131 if ((res = mp_sub(&D, &B, &D)) != MP_OKAY) {
132 goto LBL_ERR;
133 }
134 }
135
136 /* if not zero goto step 4 */
137 if (mp_iszero(&u) == MP_NO)
138 goto top;
139
140 /* now a = C, b = D, gcd == g*v */
141
142 /* if v != 1 then there is no inverse */
143 if (mp_cmp_d(&v, 1uL) != MP_EQ) {
144 res = MP_VAL;
103 goto LBL_ERR; 145 goto LBL_ERR;
104 } 146 }
105 if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
106 goto LBL_ERR;
107 }
108 }
109 147
110 /* 6. if u >= v then */ 148 /* if its too low */
111 if (mp_cmp (&u, &v) != MP_LT) { 149 while (mp_cmp_d(&C, 0uL) == MP_LT) {
112 /* u = u - v, A = A - C, B = B - D */
113 if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
114 goto LBL_ERR;
115 }
116
117 if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
118 goto LBL_ERR;
119 }
120
121 if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
122 goto LBL_ERR;
123 }
124 } else {
125 /* v - v - u, C = C - A, D = D - B */
126 if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
127 goto LBL_ERR;
128 }
129
130 if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
131 goto LBL_ERR;
132 }
133
134 if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
135 goto LBL_ERR;
136 }
137 }
138
139 /* if not zero goto step 4 */
140 if (mp_iszero (&u) == MP_NO)
141 goto top;
142
143 /* now a = C, b = D, gcd == g*v */
144
145 /* if v != 1 then there is no inverse */
146 if (mp_cmp_d (&v, 1) != MP_EQ) {
147 res = MP_VAL;
148 goto LBL_ERR;
149 }
150
151 /* if its too low */
152 while (mp_cmp_d(&C, 0) == MP_LT) {
153 if ((res = mp_add(&C, b, &C)) != MP_OKAY) { 150 if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
154 goto LBL_ERR; 151 goto LBL_ERR;
155 } 152 }
156 } 153 }
157 154
158 /* too big */ 155 /* too big */
159 while (mp_cmp_mag(&C, b) != MP_LT) { 156 while (mp_cmp_mag(&C, b) != MP_LT) {
160 if ((res = mp_sub(&C, b, &C)) != MP_OKAY) { 157 if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
161 goto LBL_ERR; 158 goto LBL_ERR;
162 } 159 }
163 } 160 }
164 161
165 /* C is now the inverse */ 162 /* C is now the inverse */
166 mp_exch (&C, c); 163 mp_exch(&C, c);
167 res = MP_OKAY; 164 res = MP_OKAY;
168 LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL); 165 LBL_ERR:
169 return res; 166 mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
167 return res;
170 } 168 }
171 #endif 169 #endif
172 170
173 /* ref: $Format:%D$ */ 171 /* ref: HEAD -> master, tag: v1.1.0 */
174 /* git commit: $Format:%H$ */ 172 /* git commit: 08549ad6bc8b0cede0b357a9c341c5c6473a9c55 */
175 /* commit time: $Format:%ai$ */ 173 /* commit time: 2019-01-28 20:32:32 +0100 */