forked from terrantsh/RSA2048
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbignum.c
360 lines (298 loc) · 7.94 KB
/
bignum.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
/*****************************************************************************
Filename : bignum.c
Author : Terrantsh ([email protected])
Date : 2018-8-31 10:31:23
Description : 整理数据
*****************************************************************************/
#include <string.h>
#include "bignum.h"
static bn_t bn_sub_digit_mul(bn_t *a, bn_t *b, bn_t c, bn_t *d, uint32_t digits);
static bn_t bn_add_digit_mul(bn_t *a, bn_t *b, bn_t c, bn_t *d, uint32_t digits);
static uint32_t bn_digit_bits(bn_t a);
void bn_decode(bn_t *bn, uint32_t digits, uint8_t *hexarr, uint32_t size)
{
bn_t t;
int j;
uint32_t i, u;
for(i=0,j=size-1; i<digits && j>=0; i++) {
t = 0;
for(u=0; j>=0 && u<BN_DIGIT_BITS; j--, u+=8) {
t |= ((bn_t)hexarr[j]) << u;
}
bn[i] = t;
}
for(; i<digits; i++) {
bn[i] = 0;
}
}
void bn_encode(uint8_t *hexarr, uint32_t size, bn_t *bn, uint32_t digits)
{
bn_t t;
int j;
uint32_t i, u;
for(i=0,j=size-1; i<digits && j>=0; i++) {
t = bn[i];
for(u=0; j>=0 && u<BN_DIGIT_BITS; j--, u+=8) {
hexarr[j] = (uint8_t)(t >> u);
}
}
for(; j>=0; j--) {
hexarr[j] = 0;
}
}
void bn_assign(bn_t *a, bn_t *b, uint32_t digits)
{
uint32_t i;
for(i=0; i<digits; i++) {
a[i] = b[i];
}
}
void bn_assign_zero(bn_t *a, uint32_t digits)
{
uint32_t i;
for(i=0; i<digits; i++) {
a[i] = 0;
}
}
bn_t bn_add(bn_t *a, bn_t *b, bn_t *c, uint32_t digits)
{
bn_t ai, carry;
uint32_t i;
carry = 0;
for(i=0; i<digits; i++) {
if((ai = b[i] + carry) < carry) {
ai = c[i];
} else if((ai += c[i]) < c[i]) {
carry = 1;
} else {
carry = 0;
}
a[i] = ai;
}
return carry;
}
bn_t bn_sub(bn_t *a, bn_t *b, bn_t *c, uint32_t digits)
{
bn_t ai, borrow;
uint32_t i;
borrow = 0;
for(i=0; i<digits; i++) {
if((ai = b[i] - borrow) > (BN_MAX_DIGIT - borrow)) {
ai = BN_MAX_DIGIT - c[i];
} else if((ai -= c[i]) > (BN_MAX_DIGIT - c[i])) {
borrow = 1;
} else {
borrow = 0;
}
a[i] = ai;
}
return borrow;
}
void bn_mul(bn_t *a, bn_t *b, bn_t *c, uint32_t digits)
{
bn_t t[2*BN_MAX_DIGITS];
uint32_t bdigits, cdigits, i;
bn_assign_zero(t, 2*digits);
bdigits = bn_digits(b, digits);
cdigits = bn_digits(c, digits);
for(i=0; i<bdigits; i++) {
t[i+cdigits] += bn_add_digit_mul(&t[i], &t[i], b[i], c, cdigits);
}
bn_assign(a, t, 2*digits);
// Clear potentially sensitive information
memset((uint8_t *)t, 0, sizeof(t));
}
void bn_div(bn_t *a, bn_t *b, bn_t *c, uint32_t cdigits, bn_t *d, uint32_t ddigits)
{
dbn_t tmp;
bn_t ai, t, cc[2*BN_MAX_DIGITS+1], dd[BN_MAX_DIGITS];
int i;
uint32_t dddigits, shift;
dddigits = bn_digits(d, ddigits);
if(dddigits == 0)
return;
shift = BN_DIGIT_BITS - bn_digit_bits(d[dddigits-1]);
bn_assign_zero(cc, dddigits);
cc[cdigits] = bn_shift_l(cc, c, shift, cdigits);
bn_shift_l(dd, d, shift, dddigits);
t = dd[dddigits-1];
bn_assign_zero(a, cdigits);
i = cdigits - dddigits;
for(; i>=0; i--) {
if(t == BN_MAX_DIGIT) {
ai = cc[i+dddigits];
} else {
tmp = cc[i+dddigits-1];
tmp += (dbn_t)cc[i+dddigits] << BN_DIGIT_BITS;
ai = tmp / (t + 1);
}
cc[i+dddigits] -= bn_sub_digit_mul(&cc[i], &cc[i], ai, dd, dddigits);
// printf("cc[%d]: %08X\n", i, cc[i+dddigits]);
while(cc[i+dddigits] || (bn_cmp(&cc[i], dd, dddigits) >= 0)) {
ai++;
cc[i+dddigits] -= bn_sub(&cc[i], &cc[i], dd, dddigits);
}
a[i] = ai;
// printf("ai[%d]: %08X\n", i, ai);
}
bn_assign_zero(b, ddigits);
bn_shift_r(b, cc, shift, dddigits);
// Clear potentially sensitive information
memset((uint8_t *)cc, 0, sizeof(cc));
memset((uint8_t *)dd, 0, sizeof(dd));
}
bn_t bn_shift_l(bn_t *a, bn_t *b, uint32_t c, uint32_t digits)
{
bn_t bi, carry;
uint32_t i, t;
if(c >= BN_DIGIT_BITS)
return 0;
t = BN_DIGIT_BITS - c;
carry = 0;
for(i=0; i<digits; i++) {
bi = b[i];
a[i] = (bi << c) | carry;
carry = c ? (bi >> t) : 0;
}
return carry;
}
bn_t bn_shift_r(bn_t *a, bn_t *b, uint32_t c, uint32_t digits)
{
bn_t bi, carry;
int i;
uint32_t t;
if(c >= BN_DIGIT_BITS)
return 0;
t = BN_DIGIT_BITS - c;
carry = 0;
i = digits - 1;
for(; i>=0; i--) {
bi = b[i];
a[i] = (bi >> c) | carry;
carry = c ? (bi << t) : 0;
}
return carry;
}
void bn_mod(bn_t *a, bn_t *b, uint32_t bdigits, bn_t *c, uint32_t cdigits)
{
bn_t t[2*BN_MAX_DIGITS] = {0};
bn_div(t, a, b, bdigits, c, cdigits);
// Clear potentially sensitive information
memset((uint8_t *)t, 0, sizeof(t));
}
void bn_mod_mul(bn_t *a, bn_t *b, bn_t *c, bn_t *d, uint32_t digits)
{
bn_t t[2*BN_MAX_DIGITS];
bn_mul(t, b, c, digits);
bn_mod(a, t, 2*digits, d, digits);
// Clear potentially sensitive information
memset((uint8_t *)t, 0, sizeof(t));
}
void bn_mod_exp(bn_t *a, bn_t *b, bn_t *c, uint32_t cdigits, bn_t *d, uint32_t ddigits)
{
bn_t bpower[3][BN_MAX_DIGITS], ci, t[BN_MAX_DIGITS];
int i;
uint32_t ci_bits, j, s;
bn_assign(bpower[0], b, ddigits);
bn_mod_mul(bpower[1], bpower[0], b, d, ddigits);
bn_mod_mul(bpower[2], bpower[1], b, d, ddigits);
BN_ASSIGN_DIGIT(t, 1, ddigits);
cdigits = bn_digits(c, cdigits);
i = cdigits - 1;
for(; i>=0; i--) {
ci = c[i];
ci_bits = BN_DIGIT_BITS;
if(i == (int)(cdigits - 1)) {
while(!DIGIT_2MSB(ci)) {
ci <<= 2;
ci_bits -= 2;
}
}
for(j=0; j<ci_bits; j+=2) {
bn_mod_mul(t, t, t, d, ddigits);
bn_mod_mul(t, t, t, d, ddigits);
if((s = DIGIT_2MSB(ci)) != 0) {
bn_mod_mul(t, t, bpower[s-1], d, ddigits);
}
ci <<= 2;
}
}
bn_assign(a, t, ddigits);
// Clear potentially sensitive information
memset((uint8_t *)bpower, 0, sizeof(bpower));
memset((uint8_t *)t, 0, sizeof(t));
}
int bn_cmp(bn_t *a, bn_t *b, uint32_t digits)
{
int i;
for(i=digits-1; i>=0; i--) {
if(a[i] > b[i]) return 1;
if(a[i] < b[i]) return -1;
}
return 0;
}
uint32_t bn_digits(bn_t *a, uint32_t digits)
{
int i;
for(i=digits-1; i>=0; i--) {
if(a[i]) break;
}
return (i + 1);
}
static bn_t bn_add_digit_mul(bn_t *a, bn_t *b, bn_t c, bn_t *d, uint32_t digits)
{
dbn_t result;
bn_t carry, rh, rl;
uint32_t i;
if(c == 0)
return 0;
carry = 0;
for(i=0; i<digits; i++) {
result = (dbn_t)c * d[i];
rl = result & BN_MAX_DIGIT;
rh = (result >> BN_DIGIT_BITS) & BN_MAX_DIGIT;
if((a[i] = b[i] + carry) < carry) {
carry = 1;
} else {
carry = 0;
}
if((a[i] += rl) < rl) {
carry++;
}
carry += rh;
}
return carry;
}
static bn_t bn_sub_digit_mul(bn_t *a, bn_t *b, bn_t c, bn_t *d, uint32_t digits)
{
dbn_t result;
bn_t borrow, rh, rl;
uint32_t i;
if(c == 0)
return 0;
borrow = 0;
for(i=0; i<digits; i++) {
result = (dbn_t)c * d[i];
rl = result & BN_MAX_DIGIT;
rh = (result >> BN_DIGIT_BITS) & BN_MAX_DIGIT;
if((a[i] = b[i] - borrow) > (BN_MAX_DIGIT - borrow)) {
borrow = 1;
} else {
borrow = 0;
}
if((a[i] -= rl) > (BN_MAX_DIGIT - rl)) {
borrow++;
}
borrow += rh;
}
return borrow;
}
static uint32_t bn_digit_bits(bn_t a)
{
uint32_t i;
for(i=0; i<BN_DIGIT_BITS; i++) {
if(a == 0) break;
a >>= 1;
}
return i;
}