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
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
20 int mpi_code, ltc_code;
21 } mpi_to_ltc_codes[] = {
22 { MP_OKAY , CRYPT_OK},
23 { MP_MEM , CRYPT_MEM},
24 { MP_VAL , CRYPT_INVALID_ARG},
28 Convert a MPI error to a LTC error (Possibly the most powerful function ever! Oh wait... no)
29 @param err The error to convert
30 @return The equivalent LTC error code or CRYPT_ERROR if none found
32 static int mpi_to_ltc_error(int err)
36 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) {
38 return mpi_to_ltc_codes[x].ltc_code;
44 static int init(void **a)
48 LTC_ARGCHK(a != NULL);
50 *a = XCALLOC(1, sizeof(mp_int));
55 if ((err = mpi_to_ltc_error(mp_init(*a))) != CRYPT_OK) {
61 static void deinit(void *a)
63 LTC_ARGCHKVD(a != NULL);
68 static int neg(void *a, void *b)
70 LTC_ARGCHK(a != NULL);
71 LTC_ARGCHK(b != NULL);
72 return mpi_to_ltc_error(mp_neg(a, b));
75 static int copy(void *a, void *b)
77 LTC_ARGCHK(a != NULL);
78 LTC_ARGCHK(b != NULL);
79 return mpi_to_ltc_error(mp_copy(a, b));
82 static int init_copy(void **a, void *b)
84 if (init(a) != CRYPT_OK) {
90 /* ---- trivial ---- */
91 static int set_int(void *a, unsigned long b)
93 LTC_ARGCHK(a != NULL);
94 return mpi_to_ltc_error(mp_set_int(a, b));
97 static unsigned long get_int(void *a)
99 LTC_ARGCHK(a != NULL);
100 return mp_get_int(a);
103 static unsigned long get_digit(void *a, int n)
106 LTC_ARGCHK(a != NULL);
108 return (n >= A->used || n < 0) ? 0 : A->dp[n];
111 static int get_digit_count(void *a)
114 LTC_ARGCHK(a != NULL);
119 static int compare(void *a, void *b)
122 LTC_ARGCHK(a != NULL);
123 LTC_ARGCHK(b != NULL);
126 case MP_LT: return LTC_MP_LT;
127 case MP_EQ: return LTC_MP_EQ;
128 case MP_GT: return LTC_MP_GT;
133 static int compare_d(void *a, unsigned long b)
136 LTC_ARGCHK(a != NULL);
137 ret = mp_cmp_d(a, b);
139 case MP_LT: return LTC_MP_LT;
140 case MP_EQ: return LTC_MP_EQ;
141 case MP_GT: return LTC_MP_GT;
146 static int count_bits(void *a)
148 LTC_ARGCHK(a != NULL);
149 return mp_count_bits(a);
152 static int count_lsb_bits(void *a)
154 LTC_ARGCHK(a != NULL);
155 return mp_cnt_lsb(a);
159 static int twoexpt(void *a, int n)
161 LTC_ARGCHK(a != NULL);
162 return mpi_to_ltc_error(mp_2expt(a, n));
165 /* ---- conversions ---- */
167 /* read ascii string */
168 static int read_radix(void *a, const char *b, int radix)
170 LTC_ARGCHK(a != NULL);
171 LTC_ARGCHK(b != NULL);
172 return mpi_to_ltc_error(mp_read_radix(a, b, radix));
176 static int write_radix(void *a, char *b, int radix)
178 LTC_ARGCHK(a != NULL);
179 LTC_ARGCHK(b != NULL);
180 return mpi_to_ltc_error(mp_toradix(a, b, radix));
183 /* get size as unsigned char string */
184 static unsigned long unsigned_size(void *a)
186 LTC_ARGCHK(a != NULL);
187 return mp_unsigned_bin_size(a);
191 static int unsigned_write(void *a, unsigned char *b)
193 LTC_ARGCHK(a != NULL);
194 LTC_ARGCHK(b != NULL);
195 return mpi_to_ltc_error(mp_to_unsigned_bin(a, b));
199 static int unsigned_read(void *a, unsigned char *b, unsigned long len)
201 LTC_ARGCHK(a != NULL);
202 LTC_ARGCHK(b != NULL);
203 return mpi_to_ltc_error(mp_read_unsigned_bin(a, b, len));
207 static int add(void *a, void *b, void *c)
209 LTC_ARGCHK(a != NULL);
210 LTC_ARGCHK(b != NULL);
211 LTC_ARGCHK(c != NULL);
212 return mpi_to_ltc_error(mp_add(a, b, c));
215 static int addi(void *a, unsigned long b, void *c)
217 LTC_ARGCHK(a != NULL);
218 LTC_ARGCHK(c != NULL);
219 return mpi_to_ltc_error(mp_add_d(a, b, c));
223 static int sub(void *a, void *b, void *c)
225 LTC_ARGCHK(a != NULL);
226 LTC_ARGCHK(b != NULL);
227 LTC_ARGCHK(c != NULL);
228 return mpi_to_ltc_error(mp_sub(a, b, c));
231 static int subi(void *a, unsigned long b, void *c)
233 LTC_ARGCHK(a != NULL);
234 LTC_ARGCHK(c != NULL);
235 return mpi_to_ltc_error(mp_sub_d(a, b, c));
239 static int mul(void *a, void *b, void *c)
241 LTC_ARGCHK(a != NULL);
242 LTC_ARGCHK(b != NULL);
243 LTC_ARGCHK(c != NULL);
244 return mpi_to_ltc_error(mp_mul(a, b, c));
247 static int muli(void *a, unsigned long b, void *c)
249 LTC_ARGCHK(a != NULL);
250 LTC_ARGCHK(c != NULL);
251 return mpi_to_ltc_error(mp_mul_d(a, b, c));
255 static int sqr(void *a, void *b)
257 LTC_ARGCHK(a != NULL);
258 LTC_ARGCHK(b != NULL);
259 return mpi_to_ltc_error(mp_sqr(a, b));
263 static int divide(void *a, void *b, void *c, void *d)
265 LTC_ARGCHK(a != NULL);
266 LTC_ARGCHK(b != NULL);
267 return mpi_to_ltc_error(mp_div(a, b, c, d));
270 static int div_2(void *a, void *b)
272 LTC_ARGCHK(a != NULL);
273 LTC_ARGCHK(b != NULL);
274 return mpi_to_ltc_error(mp_div_2(a, b));
278 static int modi(void *a, unsigned long b, unsigned long *c)
283 LTC_ARGCHK(a != NULL);
284 LTC_ARGCHK(c != NULL);
286 if ((err = mpi_to_ltc_error(mp_mod_d(a, b, &tmp))) != CRYPT_OK) {
294 static int gcd(void *a, void *b, void *c)
296 LTC_ARGCHK(a != NULL);
297 LTC_ARGCHK(b != NULL);
298 LTC_ARGCHK(c != NULL);
299 return mpi_to_ltc_error(mp_gcd(a, b, c));
303 static int lcm(void *a, void *b, void *c)
305 LTC_ARGCHK(a != NULL);
306 LTC_ARGCHK(b != NULL);
307 LTC_ARGCHK(c != NULL);
308 return mpi_to_ltc_error(mp_lcm(a, b, c));
311 static int mulmod(void *a, void *b, void *c, void *d)
313 LTC_ARGCHK(a != NULL);
314 LTC_ARGCHK(b != NULL);
315 LTC_ARGCHK(c != NULL);
316 LTC_ARGCHK(d != NULL);
317 return mpi_to_ltc_error(mp_mulmod(a,b,c,d));
320 static int sqrmod(void *a, void *b, void *c)
322 LTC_ARGCHK(a != NULL);
323 LTC_ARGCHK(b != NULL);
324 LTC_ARGCHK(c != NULL);
325 return mpi_to_ltc_error(mp_sqrmod(a,b,c));
329 static int invmod(void *a, void *b, void *c)
331 LTC_ARGCHK(a != NULL);
332 LTC_ARGCHK(b != NULL);
333 LTC_ARGCHK(c != NULL);
334 return mpi_to_ltc_error(mp_invmod(a, b, c));
338 static int montgomery_setup(void *a, void **b)
341 LTC_ARGCHK(a != NULL);
342 LTC_ARGCHK(b != NULL);
343 *b = XCALLOC(1, sizeof(mp_digit));
347 if ((err = mpi_to_ltc_error(mp_montgomery_setup(a, (mp_digit *)*b))) != CRYPT_OK) {
353 /* get normalization value */
354 static int montgomery_normalization(void *a, void *b)
356 LTC_ARGCHK(a != NULL);
357 LTC_ARGCHK(b != NULL);
358 return mpi_to_ltc_error(mp_montgomery_calc_normalization(a, b));
362 static int montgomery_reduce(void *a, void *b, void *c)
364 LTC_ARGCHK(a != NULL);
365 LTC_ARGCHK(b != NULL);
366 LTC_ARGCHK(c != NULL);
367 return mpi_to_ltc_error(mp_montgomery_reduce(a, b, *((mp_digit *)c)));
371 static void montgomery_deinit(void *a)
376 static int exptmod(void *a, void *b, void *c, void *d)
378 LTC_ARGCHK(a != NULL);
379 LTC_ARGCHK(b != NULL);
380 LTC_ARGCHK(c != NULL);
381 LTC_ARGCHK(d != NULL);
382 return mpi_to_ltc_error(mp_exptmod(a,b,c,d));
385 static int isprime(void *a, int *b)
388 LTC_ARGCHK(a != NULL);
389 LTC_ARGCHK(b != NULL);
390 err = mpi_to_ltc_error(mp_prime_is_prime(a, 8, b));
391 *b = (*b == MP_YES) ? LTC_MP_YES : LTC_MP_NO;
395 const ltc_math_descriptor ltm_desc = {
441 &montgomery_normalization,
454 <c_ecc_projective_add_point,
455 <c_ecc_projective_dbl_point,
457 #ifdef LTC_ECC_SHAMIR
465 #endif /* LTC_ECC_SHAMIR */
467 NULL, NULL, NULL, NULL, NULL,
481 /* $Source: /cvs/libtom/libtomcrypt/src/math/ltm_desc.c,v $ */
482 /* $Revision: 1.29 $ */
483 /* $Date: 2006/12/03 00:39:56 $ */