Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/math/ltm_desc.c
5971 views
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
*
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
5
*
6
* The library is free for all purposes without any express
7
* guarantee it works.
8
*/
9
10
#define DESC_DEF_ONLY
11
#include "tomcrypt.h"
12
13
#ifdef LTM_DESC
14
15
#include <tommath.h>
16
17
static const struct {
18
int mpi_code, ltc_code;
19
} mpi_to_ltc_codes[] = {
20
{ MP_OKAY , CRYPT_OK},
21
{ MP_MEM , CRYPT_MEM},
22
{ MP_VAL , CRYPT_INVALID_ARG},
23
};
24
25
/**
26
Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no)
27
@param err The error to convert
28
@return The equivalent LTC error code or CRYPT_ERROR if none found
29
*/
30
static int mpi_to_ltc_error(int err)
31
{
32
int x;
33
34
for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) {
35
if (err == mpi_to_ltc_codes[x].mpi_code) {
36
return mpi_to_ltc_codes[x].ltc_code;
37
}
38
}
39
return CRYPT_ERROR;
40
}
41
42
static int init(void **a)
43
{
44
int err;
45
46
LTC_ARGCHK(a != NULL);
47
48
*a = XCALLOC(1, sizeof(mp_int));
49
if (*a == NULL) {
50
return CRYPT_MEM;
51
}
52
53
if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
54
XFREE(*a);
55
}
56
return err;
57
}
58
59
static void deinit(void *a)
60
{
61
LTC_ARGCHKVD(a != NULL);
62
mp_clear(a);
63
XFREE(a);
64
}
65
66
static int neg(void *a, void *b)
67
{
68
LTC_ARGCHK(a != NULL);
69
LTC_ARGCHK(b != NULL);
70
return mpi_to_ltc_error(mp_neg(a, b));
71
}
72
73
static int copy(void *a, void *b)
74
{
75
LTC_ARGCHK(a != NULL);
76
LTC_ARGCHK(b != NULL);
77
return mpi_to_ltc_error(mp_copy(a, b));
78
}
79
80
static int init_copy(void **a, void *b)
81
{
82
if (init(a) != CRYPT_OK) {
83
return CRYPT_MEM;
84
}
85
return copy(b, *a);
86
}
87
88
/* ---- trivial ---- */
89
static int set_int(void *a, ltc_mp_digit b)
90
{
91
LTC_ARGCHK(a != NULL);
92
return mpi_to_ltc_error(mp_set_int(a, b));
93
}
94
95
static unsigned long get_int(void *a)
96
{
97
LTC_ARGCHK(a != NULL);
98
return mp_get_int(a);
99
}
100
101
static ltc_mp_digit get_digit(void *a, int n)
102
{
103
mp_int *A;
104
LTC_ARGCHK(a != NULL);
105
A = a;
106
return (n >= A->used || n < 0) ? 0 : A->dp[n];
107
}
108
109
static int get_digit_count(void *a)
110
{
111
mp_int *A;
112
LTC_ARGCHK(a != NULL);
113
A = a;
114
return A->used;
115
}
116
117
static int compare(void *a, void *b)
118
{
119
int ret;
120
LTC_ARGCHK(a != NULL);
121
LTC_ARGCHK(b != NULL);
122
ret = mp_cmp(a, b);
123
switch (ret) {
124
case MP_LT: return LTC_MP_LT;
125
case MP_EQ: return LTC_MP_EQ;
126
case MP_GT: return LTC_MP_GT;
127
default: return 0;
128
}
129
}
130
131
static int compare_d(void *a, ltc_mp_digit b)
132
{
133
int ret;
134
LTC_ARGCHK(a != NULL);
135
ret = mp_cmp_d(a, b);
136
switch (ret) {
137
case MP_LT: return LTC_MP_LT;
138
case MP_EQ: return LTC_MP_EQ;
139
case MP_GT: return LTC_MP_GT;
140
default: return 0;
141
}
142
}
143
144
static int count_bits(void *a)
145
{
146
LTC_ARGCHK(a != NULL);
147
return mp_count_bits(a);
148
}
149
150
static int count_lsb_bits(void *a)
151
{
152
LTC_ARGCHK(a != NULL);
153
return mp_cnt_lsb(a);
154
}
155
156
157
static int twoexpt(void *a, int n)
158
{
159
LTC_ARGCHK(a != NULL);
160
return mpi_to_ltc_error(mp_2expt(a, n));
161
}
162
163
/* ---- conversions ---- */
164
165
/* read ascii string */
166
static int read_radix(void *a, const char *b, int radix)
167
{
168
LTC_ARGCHK(a != NULL);
169
LTC_ARGCHK(b != NULL);
170
return mpi_to_ltc_error(mp_read_radix(a, b, radix));
171
}
172
173
/* write one */
174
static int write_radix(void *a, char *b, int radix)
175
{
176
LTC_ARGCHK(a != NULL);
177
LTC_ARGCHK(b != NULL);
178
return mpi_to_ltc_error(mp_toradix(a, b, radix));
179
}
180
181
/* get size as unsigned char string */
182
static unsigned long unsigned_size(void *a)
183
{
184
LTC_ARGCHK(a != NULL);
185
return mp_unsigned_bin_size(a);
186
}
187
188
/* store */
189
static int unsigned_write(void *a, unsigned char *b)
190
{
191
LTC_ARGCHK(a != NULL);
192
LTC_ARGCHK(b != NULL);
193
return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
194
}
195
196
/* read */
197
static int unsigned_read(void *a, unsigned char *b, unsigned long len)
198
{
199
LTC_ARGCHK(a != NULL);
200
LTC_ARGCHK(b != NULL);
201
return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len));
202
}
203
204
/* add */
205
static int add(void *a, void *b, void *c)
206
{
207
LTC_ARGCHK(a != NULL);
208
LTC_ARGCHK(b != NULL);
209
LTC_ARGCHK(c != NULL);
210
return mpi_to_ltc_error(mp_add(a, b, c));
211
}
212
213
static int addi(void *a, ltc_mp_digit b, void *c)
214
{
215
LTC_ARGCHK(a != NULL);
216
LTC_ARGCHK(c != NULL);
217
return mpi_to_ltc_error(mp_add_d(a, b, c));
218
}
219
220
/* sub */
221
static int sub(void *a, void *b, void *c)
222
{
223
LTC_ARGCHK(a != NULL);
224
LTC_ARGCHK(b != NULL);
225
LTC_ARGCHK(c != NULL);
226
return mpi_to_ltc_error(mp_sub(a, b, c));
227
}
228
229
static int subi(void *a, ltc_mp_digit b, void *c)
230
{
231
LTC_ARGCHK(a != NULL);
232
LTC_ARGCHK(c != NULL);
233
return mpi_to_ltc_error(mp_sub_d(a, b, c));
234
}
235
236
/* mul */
237
static int mul(void *a, void *b, void *c)
238
{
239
LTC_ARGCHK(a != NULL);
240
LTC_ARGCHK(b != NULL);
241
LTC_ARGCHK(c != NULL);
242
return mpi_to_ltc_error(mp_mul(a, b, c));
243
}
244
245
static int muli(void *a, ltc_mp_digit b, void *c)
246
{
247
LTC_ARGCHK(a != NULL);
248
LTC_ARGCHK(c != NULL);
249
return mpi_to_ltc_error(mp_mul_d(a, b, c));
250
}
251
252
/* sqr */
253
static int sqr(void *a, void *b)
254
{
255
LTC_ARGCHK(a != NULL);
256
LTC_ARGCHK(b != NULL);
257
return mpi_to_ltc_error(mp_sqr(a, b));
258
}
259
260
/* div */
261
static int divide(void *a, void *b, void *c, void *d)
262
{
263
LTC_ARGCHK(a != NULL);
264
LTC_ARGCHK(b != NULL);
265
return mpi_to_ltc_error(mp_div(a, b, c, d));
266
}
267
268
static int div_2(void *a, void *b)
269
{
270
LTC_ARGCHK(a != NULL);
271
LTC_ARGCHK(b != NULL);
272
return mpi_to_ltc_error(mp_div_2(a, b));
273
}
274
275
/* modi */
276
static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
277
{
278
mp_digit tmp;
279
int err;
280
281
LTC_ARGCHK(a != NULL);
282
LTC_ARGCHK(c != NULL);
283
284
if ((err = mpi_to_ltc_error(mp_mod_d(a, b, &tmp))) != CRYPT_OK) {
285
return err;
286
}
287
*c = tmp;
288
return CRYPT_OK;
289
}
290
291
/* gcd */
292
static int gcd(void *a, void *b, void *c)
293
{
294
LTC_ARGCHK(a != NULL);
295
LTC_ARGCHK(b != NULL);
296
LTC_ARGCHK(c != NULL);
297
return mpi_to_ltc_error(mp_gcd(a, b, c));
298
}
299
300
/* lcm */
301
static int lcm(void *a, void *b, void *c)
302
{
303
LTC_ARGCHK(a != NULL);
304
LTC_ARGCHK(b != NULL);
305
LTC_ARGCHK(c != NULL);
306
return mpi_to_ltc_error(mp_lcm(a, b, c));
307
}
308
309
static int addmod(void *a, void *b, void *c, void *d)
310
{
311
LTC_ARGCHK(a != NULL);
312
LTC_ARGCHK(b != NULL);
313
LTC_ARGCHK(c != NULL);
314
LTC_ARGCHK(d != NULL);
315
return mpi_to_ltc_error(mp_addmod(a,b,c,d));
316
}
317
318
static int submod(void *a, void *b, void *c, void *d)
319
{
320
LTC_ARGCHK(a != NULL);
321
LTC_ARGCHK(b != NULL);
322
LTC_ARGCHK(c != NULL);
323
LTC_ARGCHK(d != NULL);
324
return mpi_to_ltc_error(mp_submod(a,b,c,d));
325
}
326
327
static int mulmod(void *a, void *b, void *c, void *d)
328
{
329
LTC_ARGCHK(a != NULL);
330
LTC_ARGCHK(b != NULL);
331
LTC_ARGCHK(c != NULL);
332
LTC_ARGCHK(d != NULL);
333
return mpi_to_ltc_error(mp_mulmod(a,b,c,d));
334
}
335
336
static int sqrmod(void *a, void *b, void *c)
337
{
338
LTC_ARGCHK(a != NULL);
339
LTC_ARGCHK(b != NULL);
340
LTC_ARGCHK(c != NULL);
341
return mpi_to_ltc_error(mp_sqrmod(a,b,c));
342
}
343
344
/* invmod */
345
static int invmod(void *a, void *b, void *c)
346
{
347
LTC_ARGCHK(a != NULL);
348
LTC_ARGCHK(b != NULL);
349
LTC_ARGCHK(c != NULL);
350
return mpi_to_ltc_error(mp_invmod(a, b, c));
351
}
352
353
/* setup */
354
static int montgomery_setup(void *a, void **b)
355
{
356
int err;
357
LTC_ARGCHK(a != NULL);
358
LTC_ARGCHK(b != NULL);
359
*b = XCALLOC(1, sizeof(mp_digit));
360
if (*b == NULL) {
361
return CRYPT_MEM;
362
}
363
if ((err = mpi_to_ltc_error(mp_montgomery_setup(a, (mp_digit *)*b))) != CRYPT_OK) {
364
XFREE(*b);
365
}
366
return err;
367
}
368
369
/* get normalization value */
370
static int montgomery_normalization(void *a, void *b)
371
{
372
LTC_ARGCHK(a != NULL);
373
LTC_ARGCHK(b != NULL);
374
return mpi_to_ltc_error(mp_montgomery_calc_normalization(a, b));
375
}
376
377
/* reduce */
378
static int montgomery_reduce(void *a, void *b, void *c)
379
{
380
LTC_ARGCHK(a != NULL);
381
LTC_ARGCHK(b != NULL);
382
LTC_ARGCHK(c != NULL);
383
return mpi_to_ltc_error(mp_montgomery_reduce(a, b, *((mp_digit *)c)));
384
}
385
386
/* clean up */
387
static void montgomery_deinit(void *a)
388
{
389
XFREE(a);
390
}
391
392
static int exptmod(void *a, void *b, void *c, void *d)
393
{
394
LTC_ARGCHK(a != NULL);
395
LTC_ARGCHK(b != NULL);
396
LTC_ARGCHK(c != NULL);
397
LTC_ARGCHK(d != NULL);
398
return mpi_to_ltc_error(mp_exptmod(a,b,c,d));
399
}
400
401
static int isprime(void *a, int b, int *c)
402
{
403
int err;
404
LTC_ARGCHK(a != NULL);
405
LTC_ARGCHK(c != NULL);
406
if (b == 0) {
407
b = LTC_MILLER_RABIN_REPS;
408
} /* if */
409
err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
410
*c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
411
return err;
412
}
413
414
static int set_rand(void *a, int size)
415
{
416
LTC_ARGCHK(a != NULL);
417
return mpi_to_ltc_error(mp_rand(a, size));
418
}
419
420
const ltc_math_descriptor ltm_desc = {
421
422
"LibTomMath",
423
(int)DIGIT_BIT,
424
425
&init,
426
&init_copy,
427
&deinit,
428
429
&neg,
430
&copy,
431
432
&set_int,
433
&get_int,
434
&get_digit,
435
&get_digit_count,
436
&compare,
437
&compare_d,
438
&count_bits,
439
&count_lsb_bits,
440
&twoexpt,
441
442
&read_radix,
443
&write_radix,
444
&unsigned_size,
445
&unsigned_write,
446
&unsigned_read,
447
448
&add,
449
&addi,
450
&sub,
451
&subi,
452
&mul,
453
&muli,
454
&sqr,
455
&divide,
456
&div_2,
457
&modi,
458
&gcd,
459
&lcm,
460
461
&mulmod,
462
&sqrmod,
463
&invmod,
464
465
&montgomery_setup,
466
&montgomery_normalization,
467
&montgomery_reduce,
468
&montgomery_deinit,
469
470
&exptmod,
471
&isprime,
472
473
#ifdef LTC_MECC
474
#ifdef LTC_MECC_FP
475
&ltc_ecc_fp_mulmod,
476
#else
477
&ltc_ecc_mulmod,
478
#endif
479
&ltc_ecc_projective_add_point,
480
&ltc_ecc_projective_dbl_point,
481
&ltc_ecc_map,
482
#ifdef LTC_ECC_SHAMIR
483
#ifdef LTC_MECC_FP
484
&ltc_ecc_fp_mul2add,
485
#else
486
&ltc_ecc_mul2add,
487
#endif /* LTC_MECC_FP */
488
#else
489
NULL,
490
#endif /* LTC_ECC_SHAMIR */
491
#else
492
NULL, NULL, NULL, NULL, NULL,
493
#endif /* LTC_MECC */
494
495
#ifdef LTC_MRSA
496
&rsa_make_key,
497
&rsa_exptmod,
498
#else
499
NULL, NULL,
500
#endif
501
&addmod,
502
&submod,
503
504
&set_rand,
505
506
};
507
508
509
#endif
510
511