comparison libtomcrypt/src/math/ltm_desc.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children 1051e4eea25a
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 9
12 #define DESC_DEF_ONLY 10 #define DESC_DEF_ONLY
13 #include "tomcrypt.h" 11 #include "tomcrypt.h"
14 12
23 { MP_MEM , CRYPT_MEM}, 21 { MP_MEM , CRYPT_MEM},
24 { MP_VAL , CRYPT_INVALID_ARG}, 22 { MP_VAL , CRYPT_INVALID_ARG},
25 }; 23 };
26 24
27 /** 25 /**
28 Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no) 26 Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no)
29 @param err The error to convert 27 @param err The error to convert
30 @return The equivalent LTC error code or CRYPT_ERROR if none found 28 @return The equivalent LTC error code or CRYPT_ERROR if none found
31 */ 29 */
32 static int mpi_to_ltc_error(int err) 30 static int mpi_to_ltc_error(int err)
33 { 31 {
34 int x; 32 int x;
35 33
36 for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) { 34 for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) {
37 if (err == mpi_to_ltc_codes[x].mpi_code) { 35 if (err == mpi_to_ltc_codes[x].mpi_code) {
38 return mpi_to_ltc_codes[x].ltc_code; 36 return mpi_to_ltc_codes[x].ltc_code;
39 } 37 }
40 } 38 }
41 return CRYPT_ERROR; 39 return CRYPT_ERROR;
42 } 40 }
49 47
50 *a = XCALLOC(1, sizeof(mp_int)); 48 *a = XCALLOC(1, sizeof(mp_int));
51 if (*a == NULL) { 49 if (*a == NULL) {
52 return CRYPT_MEM; 50 return CRYPT_MEM;
53 } 51 }
54 52
55 if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) { 53 if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
56 XFREE(*a); 54 XFREE(*a);
57 } 55 }
58 return err; 56 return err;
59 } 57 }
86 } 84 }
87 return copy(b, *a); 85 return copy(b, *a);
88 } 86 }
89 87
90 /* ---- trivial ---- */ 88 /* ---- trivial ---- */
91 static int set_int(void *a, unsigned long b) 89 static int set_int(void *a, ltc_mp_digit b)
92 { 90 {
93 LTC_ARGCHK(a != NULL); 91 LTC_ARGCHK(a != NULL);
94 return mpi_to_ltc_error(mp_set_int(a, b)); 92 return mpi_to_ltc_error(mp_set_int(a, b));
95 } 93 }
96 94
98 { 96 {
99 LTC_ARGCHK(a != NULL); 97 LTC_ARGCHK(a != NULL);
100 return mp_get_int(a); 98 return mp_get_int(a);
101 } 99 }
102 100
103 static unsigned long get_digit(void *a, int n) 101 static ltc_mp_digit get_digit(void *a, int n)
104 { 102 {
105 mp_int *A; 103 mp_int *A;
106 LTC_ARGCHK(a != NULL); 104 LTC_ARGCHK(a != NULL);
107 A = a; 105 A = a;
108 return (n >= A->used || n < 0) ? 0 : A->dp[n]; 106 return (n >= A->used || n < 0) ? 0 : A->dp[n];
113 mp_int *A; 111 mp_int *A;
114 LTC_ARGCHK(a != NULL); 112 LTC_ARGCHK(a != NULL);
115 A = a; 113 A = a;
116 return A->used; 114 return A->used;
117 } 115 }
118 116
119 static int compare(void *a, void *b) 117 static int compare(void *a, void *b)
120 { 118 {
121 int ret; 119 int ret;
122 LTC_ARGCHK(a != NULL); 120 LTC_ARGCHK(a != NULL);
123 LTC_ARGCHK(b != NULL); 121 LTC_ARGCHK(b != NULL);
124 ret = mp_cmp(a, b); 122 ret = mp_cmp(a, b);
125 switch (ret) { 123 switch (ret) {
126 case MP_LT: return LTC_MP_LT; 124 case MP_LT: return LTC_MP_LT;
127 case MP_EQ: return LTC_MP_EQ; 125 case MP_EQ: return LTC_MP_EQ;
128 case MP_GT: return LTC_MP_GT; 126 case MP_GT: return LTC_MP_GT;
129 } 127 default: return 0;
130 return 0; 128 }
131 } 129 }
132 130
133 static int compare_d(void *a, unsigned long b) 131 static int compare_d(void *a, ltc_mp_digit b)
134 { 132 {
135 int ret; 133 int ret;
136 LTC_ARGCHK(a != NULL); 134 LTC_ARGCHK(a != NULL);
137 ret = mp_cmp_d(a, b); 135 ret = mp_cmp_d(a, b);
138 switch (ret) { 136 switch (ret) {
139 case MP_LT: return LTC_MP_LT; 137 case MP_LT: return LTC_MP_LT;
140 case MP_EQ: return LTC_MP_EQ; 138 case MP_EQ: return LTC_MP_EQ;
141 case MP_GT: return LTC_MP_GT; 139 case MP_GT: return LTC_MP_GT;
142 } 140 default: return 0;
143 return 0; 141 }
144 } 142 }
145 143
146 static int count_bits(void *a) 144 static int count_bits(void *a)
147 { 145 {
148 LTC_ARGCHK(a != NULL); 146 LTC_ARGCHK(a != NULL);
209 LTC_ARGCHK(a != NULL); 207 LTC_ARGCHK(a != NULL);
210 LTC_ARGCHK(b != NULL); 208 LTC_ARGCHK(b != NULL);
211 LTC_ARGCHK(c != NULL); 209 LTC_ARGCHK(c != NULL);
212 return mpi_to_ltc_error(mp_add(a, b, c)); 210 return mpi_to_ltc_error(mp_add(a, b, c));
213 } 211 }
214 212
215 static int addi(void *a, unsigned long b, void *c) 213 static int addi(void *a, ltc_mp_digit b, void *c)
216 { 214 {
217 LTC_ARGCHK(a != NULL); 215 LTC_ARGCHK(a != NULL);
218 LTC_ARGCHK(c != NULL); 216 LTC_ARGCHK(c != NULL);
219 return mpi_to_ltc_error(mp_add_d(a, b, c)); 217 return mpi_to_ltc_error(mp_add_d(a, b, c));
220 } 218 }
226 LTC_ARGCHK(b != NULL); 224 LTC_ARGCHK(b != NULL);
227 LTC_ARGCHK(c != NULL); 225 LTC_ARGCHK(c != NULL);
228 return mpi_to_ltc_error(mp_sub(a, b, c)); 226 return mpi_to_ltc_error(mp_sub(a, b, c));
229 } 227 }
230 228
231 static int subi(void *a, unsigned long b, void *c) 229 static int subi(void *a, ltc_mp_digit b, void *c)
232 { 230 {
233 LTC_ARGCHK(a != NULL); 231 LTC_ARGCHK(a != NULL);
234 LTC_ARGCHK(c != NULL); 232 LTC_ARGCHK(c != NULL);
235 return mpi_to_ltc_error(mp_sub_d(a, b, c)); 233 return mpi_to_ltc_error(mp_sub_d(a, b, c));
236 } 234 }
242 LTC_ARGCHK(b != NULL); 240 LTC_ARGCHK(b != NULL);
243 LTC_ARGCHK(c != NULL); 241 LTC_ARGCHK(c != NULL);
244 return mpi_to_ltc_error(mp_mul(a, b, c)); 242 return mpi_to_ltc_error(mp_mul(a, b, c));
245 } 243 }
246 244
247 static int muli(void *a, unsigned long b, void *c) 245 static int muli(void *a, ltc_mp_digit b, void *c)
248 { 246 {
249 LTC_ARGCHK(a != NULL); 247 LTC_ARGCHK(a != NULL);
250 LTC_ARGCHK(c != NULL); 248 LTC_ARGCHK(c != NULL);
251 return mpi_to_ltc_error(mp_mul_d(a, b, c)); 249 return mpi_to_ltc_error(mp_mul_d(a, b, c));
252 } 250 }
273 LTC_ARGCHK(b != NULL); 271 LTC_ARGCHK(b != NULL);
274 return mpi_to_ltc_error(mp_div_2(a, b)); 272 return mpi_to_ltc_error(mp_div_2(a, b));
275 } 273 }
276 274
277 /* modi */ 275 /* modi */
278 static int modi(void *a, unsigned long b, unsigned long *c) 276 static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
279 { 277 {
280 mp_digit tmp; 278 mp_digit tmp;
281 int err; 279 int err;
282 280
283 LTC_ARGCHK(a != NULL); 281 LTC_ARGCHK(a != NULL);
286 if ((err = mpi_to_ltc_error(mp_mod_d(a, b, &tmp))) != CRYPT_OK) { 284 if ((err = mpi_to_ltc_error(mp_mod_d(a, b, &tmp))) != CRYPT_OK) {
287 return err; 285 return err;
288 } 286 }
289 *c = tmp; 287 *c = tmp;
290 return CRYPT_OK; 288 return CRYPT_OK;
291 } 289 }
292 290
293 /* gcd */ 291 /* gcd */
294 static int gcd(void *a, void *b, void *c) 292 static int gcd(void *a, void *b, void *c)
295 { 293 {
296 LTC_ARGCHK(a != NULL); 294 LTC_ARGCHK(a != NULL);
304 { 302 {
305 LTC_ARGCHK(a != NULL); 303 LTC_ARGCHK(a != NULL);
306 LTC_ARGCHK(b != NULL); 304 LTC_ARGCHK(b != NULL);
307 LTC_ARGCHK(c != NULL); 305 LTC_ARGCHK(c != NULL);
308 return mpi_to_ltc_error(mp_lcm(a, b, c)); 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));
309 } 325 }
310 326
311 static int mulmod(void *a, void *b, void *c, void *d) 327 static int mulmod(void *a, void *b, void *c, void *d)
312 { 328 {
313 LTC_ARGCHK(a != NULL); 329 LTC_ARGCHK(a != NULL);
378 LTC_ARGCHK(a != NULL); 394 LTC_ARGCHK(a != NULL);
379 LTC_ARGCHK(b != NULL); 395 LTC_ARGCHK(b != NULL);
380 LTC_ARGCHK(c != NULL); 396 LTC_ARGCHK(c != NULL);
381 LTC_ARGCHK(d != NULL); 397 LTC_ARGCHK(d != NULL);
382 return mpi_to_ltc_error(mp_exptmod(a,b,c,d)); 398 return mpi_to_ltc_error(mp_exptmod(a,b,c,d));
383 } 399 }
384 400
385 static int isprime(void *a, int *b) 401 static int isprime(void *a, int b, int *c)
386 { 402 {
387 int err; 403 int err;
388 LTC_ARGCHK(a != NULL); 404 LTC_ARGCHK(a != NULL);
389 LTC_ARGCHK(b != NULL); 405 LTC_ARGCHK(c != NULL);
390 err = mpi_to_ltc_error(mp_prime_is_prime(a, 8, b)); 406 if (b == 0) {
391 *b = (*b == MP_YES) ? LTC_MP_YES : LTC_MP_NO; 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;
392 return err; 411 return err;
393 } 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 }
394 419
395 const ltc_math_descriptor ltm_desc = { 420 const ltc_math_descriptor ltm_desc = {
396 421
397 "LibTomMath", 422 "LibTomMath",
398 (int)DIGIT_BIT, 423 (int)DIGIT_BIT,
434 &lcm, 459 &lcm,
435 460
436 &mulmod, 461 &mulmod,
437 &sqrmod, 462 &sqrmod,
438 &invmod, 463 &invmod,
439 464
440 &montgomery_setup, 465 &montgomery_setup,
441 &montgomery_normalization, 466 &montgomery_normalization,
442 &montgomery_reduce, 467 &montgomery_reduce,
443 &montgomery_deinit, 468 &montgomery_deinit,
444 469
446 &isprime, 471 &isprime,
447 472
448 #ifdef LTC_MECC 473 #ifdef LTC_MECC
449 #ifdef LTC_MECC_FP 474 #ifdef LTC_MECC_FP
450 &ltc_ecc_fp_mulmod, 475 &ltc_ecc_fp_mulmod,
451 #else 476 #else
452 &ltc_ecc_mulmod, 477 &ltc_ecc_mulmod,
453 #endif 478 #endif
454 &ltc_ecc_projective_add_point, 479 &ltc_ecc_projective_add_point,
455 &ltc_ecc_projective_dbl_point, 480 &ltc_ecc_projective_dbl_point,
456 &ltc_ecc_map, 481 &ltc_ecc_map,
469 494
470 #ifdef LTC_MRSA 495 #ifdef LTC_MRSA
471 &rsa_make_key, 496 &rsa_make_key,
472 &rsa_exptmod, 497 &rsa_exptmod,
473 #else 498 #else
474 NULL, NULL 499 NULL, NULL,
475 #endif 500 #endif
501 &addmod,
502 &submod,
503
504 &set_rand,
505
476 }; 506 };
477 507
478 508
479 #endif 509 #endif
480 510
481 /* $Source$ */ 511 /* ref: $Format:%D$ */
482 /* $Revision$ */ 512 /* git commit: $Format:%H$ */
483 /* $Date$ */ 513 /* commit time: $Format:%ai$ */