comparison libtomcrypt/src/math/ltm_desc.c @ 1692:1051e4eea25a

Update LibTomMath to 1.2.0 (#84) * update C files * update other files * update headers * update makefiles * remove mp_set/get_double() * use ltm 1.2.0 API * update ltm_desc * use bundled tommath if system-tommath is too old * XMALLOC etc. were changed to MP_MALLOC etc.
author Steffen Jaeckel <s@jaeckel.eu>
date Tue, 26 May 2020 17:36:47 +0200
parents 6dba84798cd5
children
comparison
equal deleted inserted replaced
1691:2d3745d58843 1692:1051e4eea25a
13 #ifdef LTM_DESC 13 #ifdef LTM_DESC
14 14
15 #include <tommath.h> 15 #include <tommath.h>
16 16
17 static const struct { 17 static const struct {
18 int mpi_code, ltc_code; 18 mp_err mpi_code;
19 int ltc_code;
19 } mpi_to_ltc_codes[] = { 20 } mpi_to_ltc_codes[] = {
20 { MP_OKAY , CRYPT_OK}, 21 { MP_OKAY , CRYPT_OK},
21 { MP_MEM , CRYPT_MEM}, 22 { MP_MEM , CRYPT_MEM},
22 { MP_VAL , CRYPT_INVALID_ARG}, 23 { MP_VAL , CRYPT_INVALID_ARG},
24 { MP_ITER , CRYPT_INVALID_PACKET},
25 { MP_BUF , CRYPT_BUFFER_OVERFLOW},
23 }; 26 };
24 27
25 /** 28 /**
26 Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no) 29 Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no)
27 @param err The error to convert 30 @param err The error to convert
28 @return The equivalent LTC error code or CRYPT_ERROR if none found 31 @return The equivalent LTC error code or CRYPT_ERROR if none found
29 */ 32 */
30 static int mpi_to_ltc_error(int err) 33 static int mpi_to_ltc_error(mp_err err)
31 { 34 {
32 int x; 35 size_t x;
33 36
34 for (x = 0; x < (int)(sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0])); x++) { 37 for (x = 0; x < sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0]); x++) {
35 if (err == mpi_to_ltc_codes[x].mpi_code) { 38 if (err == mpi_to_ltc_codes[x].mpi_code) {
36 return mpi_to_ltc_codes[x].ltc_code; 39 return mpi_to_ltc_codes[x].ltc_code;
37 } 40 }
38 } 41 }
39 return CRYPT_ERROR; 42 return CRYPT_ERROR;
40 } 43 }
41 44
42 static int init(void **a) 45 static int init_mpi(void **a)
43 { 46 {
44 int err;
45
46 LTC_ARGCHK(a != NULL); 47 LTC_ARGCHK(a != NULL);
47 48
48 *a = XCALLOC(1, sizeof(mp_int)); 49 *a = XCALLOC(1, sizeof(mp_int));
49 if (*a == NULL) { 50 if (*a == NULL) {
50 return CRYPT_MEM; 51 return CRYPT_MEM;
51 } 52 } else {
52 53 return CRYPT_OK;
54 }
55 }
56
57 static int init(void **a)
58 {
59 int err;
60
61 LTC_ARGCHK(a != NULL);
62
63 if ((err = init_mpi(a)) != CRYPT_OK) {
64 return err;
65 }
53 if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) { 66 if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
54 XFREE(*a); 67 XFREE(*a);
55 } 68 }
56 return err; 69 return err;
57 } 70 }
77 return mpi_to_ltc_error(mp_copy(a, b)); 90 return mpi_to_ltc_error(mp_copy(a, b));
78 } 91 }
79 92
80 static int init_copy(void **a, void *b) 93 static int init_copy(void **a, void *b)
81 { 94 {
82 if (init(a) != CRYPT_OK) { 95 int err;
83 return CRYPT_MEM; 96 LTC_ARGCHK(a != NULL);
84 } 97 LTC_ARGCHK(b != NULL);
85 return copy(b, *a); 98 if ((err = init_mpi(a)) != CRYPT_OK) return err;
99 return mpi_to_ltc_error(mp_init_copy(*a, b));
86 } 100 }
87 101
88 /* ---- trivial ---- */ 102 /* ---- trivial ---- */
89 static int set_int(void *a, ltc_mp_digit b) 103 static int set_int(void *a, ltc_mp_digit b)
90 { 104 {
91 LTC_ARGCHK(a != NULL); 105 LTC_ARGCHK(a != NULL);
92 return mpi_to_ltc_error(mp_set_int(a, b)); 106 mp_set_u32(a, b);
107 return CRYPT_OK;
93 } 108 }
94 109
95 static unsigned long get_int(void *a) 110 static unsigned long get_int(void *a)
96 { 111 {
97 LTC_ARGCHK(a != NULL); 112 LTC_ARGCHK(a != NULL);
98 return mp_get_int(a); 113 return mp_get_ul(a);
99 } 114 }
100 115
101 static ltc_mp_digit get_digit(void *a, int n) 116 static ltc_mp_digit get_digit(void *a, int n)
102 { 117 {
103 mp_int *A; 118 mp_int *A;
114 return A->used; 129 return A->used;
115 } 130 }
116 131
117 static int compare(void *a, void *b) 132 static int compare(void *a, void *b)
118 { 133 {
119 int ret; 134 LTC_ARGCHK(a != NULL);
120 LTC_ARGCHK(a != NULL); 135 LTC_ARGCHK(b != NULL);
121 LTC_ARGCHK(b != NULL); 136 switch (mp_cmp(a, b)) {
122 ret = mp_cmp(a, b);
123 switch (ret) {
124 case MP_LT: return LTC_MP_LT; 137 case MP_LT: return LTC_MP_LT;
125 case MP_EQ: return LTC_MP_EQ; 138 case MP_EQ: return LTC_MP_EQ;
126 case MP_GT: return LTC_MP_GT; 139 case MP_GT: return LTC_MP_GT;
127 default: return 0; 140 default: return 0;
128 } 141 }
129 } 142 }
130 143
131 static int compare_d(void *a, ltc_mp_digit b) 144 static int compare_d(void *a, ltc_mp_digit b)
132 { 145 {
133 int ret; 146 LTC_ARGCHK(a != NULL);
134 LTC_ARGCHK(a != NULL); 147 switch (mp_cmp_d(a, b)) {
135 ret = mp_cmp_d(a, b);
136 switch (ret) {
137 case MP_LT: return LTC_MP_LT; 148 case MP_LT: return LTC_MP_LT;
138 case MP_EQ: return LTC_MP_EQ; 149 case MP_EQ: return LTC_MP_EQ;
139 case MP_GT: return LTC_MP_GT; 150 case MP_GT: return LTC_MP_GT;
140 default: return 0; 151 default: return 0;
141 } 152 }
173 /* write one */ 184 /* write one */
174 static int write_radix(void *a, char *b, int radix) 185 static int write_radix(void *a, char *b, int radix)
175 { 186 {
176 LTC_ARGCHK(a != NULL); 187 LTC_ARGCHK(a != NULL);
177 LTC_ARGCHK(b != NULL); 188 LTC_ARGCHK(b != NULL);
178 return mpi_to_ltc_error(mp_toradix(a, b, radix)); 189 return mpi_to_ltc_error(mp_to_radix(a, b, SIZE_MAX, NULL, radix));
179 } 190 }
180 191
181 /* get size as unsigned char string */ 192 /* get size as unsigned char string */
182 static unsigned long unsigned_size(void *a) 193 static unsigned long unsigned_size(void *a)
183 { 194 {
184 LTC_ARGCHK(a != NULL); 195 LTC_ARGCHK(a != NULL);
185 return mp_unsigned_bin_size(a); 196 return (unsigned long)mp_ubin_size(a);
186 } 197 }
187 198
188 /* store */ 199 /* store */
189 static int unsigned_write(void *a, unsigned char *b) 200 static int unsigned_write(void *a, unsigned char *b)
190 { 201 {
191 LTC_ARGCHK(a != NULL); 202 LTC_ARGCHK(a != NULL);
192 LTC_ARGCHK(b != NULL); 203 LTC_ARGCHK(b != NULL);
193 return mpi_to_ltc_error(mp_to_unsigned_bin(a, b)); 204 return mpi_to_ltc_error(mp_to_ubin(a, b, SIZE_MAX, NULL));
194 } 205 }
195 206
196 /* read */ 207 /* read */
197 static int unsigned_read(void *a, unsigned char *b, unsigned long len) 208 static int unsigned_read(void *a, unsigned char *b, unsigned long len)
198 { 209 {
199 LTC_ARGCHK(a != NULL); 210 LTC_ARGCHK(a != NULL);
200 LTC_ARGCHK(b != NULL); 211 LTC_ARGCHK(b != NULL);
201 return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len)); 212 return mpi_to_ltc_error(mp_from_ubin(a, b, (size_t)len));
202 } 213 }
203 214
204 /* add */ 215 /* add */
205 static int add(void *a, void *b, void *c) 216 static int add(void *a, void *b, void *c)
206 { 217 {
401 static int isprime(void *a, int b, int *c) 412 static int isprime(void *a, int b, int *c)
402 { 413 {
403 int err; 414 int err;
404 LTC_ARGCHK(a != NULL); 415 LTC_ARGCHK(a != NULL);
405 LTC_ARGCHK(c != NULL); 416 LTC_ARGCHK(c != NULL);
406 if (b == 0) { 417 b = mp_prime_rabin_miller_trials(mp_count_bits(a));
407 b = LTC_MILLER_RABIN_REPS;
408 } /* if */
409 err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c)); 418 err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
410 *c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO; 419 *c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
411 return err; 420 return err;
412 } 421 }
413 422
418 } 427 }
419 428
420 const ltc_math_descriptor ltm_desc = { 429 const ltc_math_descriptor ltm_desc = {
421 430
422 "LibTomMath", 431 "LibTomMath",
423 (int)DIGIT_BIT, 432 (int)MP_DIGIT_BIT,
424 433
425 &init, 434 &init,
426 &init_copy, 435 &init_copy,
427 &deinit, 436 &deinit,
428 437