1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
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},
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
30 static int mpi_to_ltc_error(int err)
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;
42 static int init(void **a)
46 LTC_ARGCHK(a != NULL);
48 *a = XCALLOC(1, sizeof(mp_int));
53 if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
59 static void deinit(void *a)
61 LTC_ARGCHKVD(a != NULL);
66 static int neg(void *a, void *b)
68 LTC_ARGCHK(a != NULL);
69 LTC_ARGCHK(b != NULL);
70 return mpi_to_ltc_error(mp_neg(a, b));
73 static int copy(void *a, void *b)
75 LTC_ARGCHK(a != NULL);
76 LTC_ARGCHK(b != NULL);
77 return mpi_to_ltc_error(mp_copy(a, b));
80 static int init_copy(void **a, void *b)
82 if (init(a) != CRYPT_OK) {
88 /* ---- trivial ---- */
89 static int set_int(void *a, ltc_mp_digit b)
91 LTC_ARGCHK(a != NULL);
92 return mpi_to_ltc_error(mp_set_int(a, b));
95 static unsigned long get_int(void *a)
97 LTC_ARGCHK(a != NULL);
101 static ltc_mp_digit get_digit(void *a, int n)
104 LTC_ARGCHK(a != NULL);
106 return (n >= A->used || n < 0) ? 0 : A->dp[n];
109 static int get_digit_count(void *a)
112 LTC_ARGCHK(a != NULL);
117 static int compare(void *a, void *b)
120 LTC_ARGCHK(a != NULL);
121 LTC_ARGCHK(b != NULL);
124 case MP_LT: return LTC_MP_LT;
125 case MP_EQ: return LTC_MP_EQ;
126 case MP_GT: return LTC_MP_GT;
131 static int compare_d(void *a, ltc_mp_digit b)
134 LTC_ARGCHK(a != NULL);
135 ret = mp_cmp_d(a, b);
137 case MP_LT: return LTC_MP_LT;
138 case MP_EQ: return LTC_MP_EQ;
139 case MP_GT: return LTC_MP_GT;
144 static int count_bits(void *a)
146 LTC_ARGCHK(a != NULL);
147 return mp_count_bits(a);
150 static int count_lsb_bits(void *a)
152 LTC_ARGCHK(a != NULL);
153 return mp_cnt_lsb(a);
157 static int twoexpt(void *a, int n)
159 LTC_ARGCHK(a != NULL);
160 return mpi_to_ltc_error(mp_2expt(a, n));
163 /* ---- conversions ---- */
165 /* read ascii string */
166 static int read_radix(void *a, const char *b, int radix)
168 LTC_ARGCHK(a != NULL);
169 LTC_ARGCHK(b != NULL);
170 return mpi_to_ltc_error(mp_read_radix(a, b, radix));
174 static int write_radix(void *a, char *b, int radix)
176 LTC_ARGCHK(a != NULL);
177 LTC_ARGCHK(b != NULL);
178 return mpi_to_ltc_error(mp_toradix(a, b, radix));
181 /* get size as unsigned char string */
182 static unsigned long unsigned_size(void *a)
184 LTC_ARGCHK(a != NULL);
185 return mp_unsigned_bin_size(a);
189 static int unsigned_write(void *a, unsigned char *b)
191 LTC_ARGCHK(a != NULL);
192 LTC_ARGCHK(b != NULL);
193 return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
197 static int unsigned_read(void *a, unsigned char *b, unsigned long len)
199 LTC_ARGCHK(a != NULL);
200 LTC_ARGCHK(b != NULL);
201 return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len));
205 static int add(void *a, void *b, void *c)
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));
213 static int addi(void *a, ltc_mp_digit b, void *c)
215 LTC_ARGCHK(a != NULL);
216 LTC_ARGCHK(c != NULL);
217 return mpi_to_ltc_error(mp_add_d(a, b, c));
221 static int sub(void *a, void *b, void *c)
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));
229 static int subi(void *a, ltc_mp_digit b, void *c)
231 LTC_ARGCHK(a != NULL);
232 LTC_ARGCHK(c != NULL);
233 return mpi_to_ltc_error(mp_sub_d(a, b, c));
237 static int mul(void *a, void *b, void *c)
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));
245 static int muli(void *a, ltc_mp_digit b, void *c)
247 LTC_ARGCHK(a != NULL);
248 LTC_ARGCHK(c != NULL);
249 return mpi_to_ltc_error(mp_mul_d(a, b, c));
253 static int sqr(void *a, void *b)
255 LTC_ARGCHK(a != NULL);
256 LTC_ARGCHK(b != NULL);
257 return mpi_to_ltc_error(mp_sqr(a, b));
261 static int divide(void *a, void *b, void *c, void *d)
263 LTC_ARGCHK(a != NULL);
264 LTC_ARGCHK(b != NULL);
265 return mpi_to_ltc_error(mp_div(a, b, c, d));
268 static int div_2(void *a, void *b)
270 LTC_ARGCHK(a != NULL);
271 LTC_ARGCHK(b != NULL);
272 return mpi_to_ltc_error(mp_div_2(a, b));
276 static int modi(void *a, ltc_mp_digit b, ltc_mp_digit *c)
281 LTC_ARGCHK(a != NULL);
282 LTC_ARGCHK(c != NULL);
284 if ((err = mpi_to_ltc_error(mp_mod_d(a, b, &tmp))) != CRYPT_OK) {
292 static int gcd(void *a, void *b, void *c)
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));
301 static int lcm(void *a, void *b, void *c)
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));
309 static int addmod(void *a, void *b, void *c, void *d)
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));
318 static int submod(void *a, void *b, void *c, void *d)
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));
327 static int mulmod(void *a, void *b, void *c, void *d)
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));
336 static int sqrmod(void *a, void *b, void *c)
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));
345 static int invmod(void *a, void *b, void *c)
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));
354 static int montgomery_setup(void *a, void **b)
357 LTC_ARGCHK(a != NULL);
358 LTC_ARGCHK(b != NULL);
359 *b = XCALLOC(1, sizeof(mp_digit));
363 if ((err = mpi_to_ltc_error(mp_montgomery_setup(a, (mp_digit *)*b))) != CRYPT_OK) {
369 /* get normalization value */
370 static int montgomery_normalization(void *a, void *b)
372 LTC_ARGCHK(a != NULL);
373 LTC_ARGCHK(b != NULL);
374 return mpi_to_ltc_error(mp_montgomery_calc_normalization(a, b));
378 static int montgomery_reduce(void *a, void *b, void *c)
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)));
387 static void montgomery_deinit(void *a)
392 static int exptmod(void *a, void *b, void *c, void *d)
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));
401 static int isprime(void *a, int b, int *c)
404 LTC_ARGCHK(a != NULL);
405 LTC_ARGCHK(c != NULL);
407 b = LTC_MILLER_RABIN_REPS;
409 err = mpi_to_ltc_error(mp_prime_is_prime(a, b, c));
410 *c = (*c == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
414 static int set_rand(void *a, int size)
416 LTC_ARGCHK(a != NULL);
417 return mpi_to_ltc_error(mp_rand(a, size));
420 const ltc_math_descriptor ltm_desc = {
466 &montgomery_normalization,
479 <c_ecc_projective_add_point,
480 <c_ecc_projective_dbl_point,
482 #ifdef LTC_ECC_SHAMIR
487 #endif /* LTC_MECC_FP */
490 #endif /* LTC_ECC_SHAMIR */
492 NULL, NULL, NULL, NULL, NULL,
493 #endif /* LTC_MECC */
511 /* ref: $Format:%D$ */
512 /* git commit: $Format:%H$ */
513 /* commit time: $Format:%ai$ */