1 /** math functions **/2 moduletomcrypt.math;
3 4 importcore.stdc.config;
5 importtomcrypt.prng;
6 importtomcrypt.pk;
7 8 extern(C) nothrow:
9 10 enumLTC_MP_LT = -1;
11 enumLTC_MP_EQ = 0;
12 enumLTC_MP_GT = 1;
13 14 enumLTC_MP_NO = 0;
15 enumLTC_MP_YES = 1;
16 17 version(LTC_MECC) {}
18 else19 {
20 aliasvoidecc_point;
21 }
22 23 version(LTC_MRSA) {}
24 else25 {
26 aliasvoidrsa_key;
27 }
28 29 /** math descriptor */30 structltc_math_descriptor31 {
32 /** Name of the math provider */33 char *name;
34 35 /** Bits per digit, amount of bits must fit in an c_ulong */36 intbits_per_digit;
37 38 /* ---- init/deinit functions ---- */39 40 /** initialize a bignum
41 @param a The number to initialize
42 @return CRYPT_OK on success
43 */44 intfunction(void **a) nothrowinit;
45 46 /** init copy
47 @param dst The number to initialize and write to
48 @param src The number to copy from
49 @return CRYPT_OK on success
50 */51 intfunction(void **dst, void *src) nothrowinit_copy;
52 53 /** deinit
54 @param a The number to free
55 @return CRYPT_OK on success
56 */57 voidfunction(void *a) nothrowdeinit;
58 59 /* ---- data movement ---- */60 61 /** negate
62 @param src The number to negate
63 @param dst The destination
64 @return CRYPT_OK on success
65 */66 intfunction(void *src, void *dst) nothrowneg;
67 68 /** copy
69 @param src The number to copy from
70 @param dst The number to write to
71 @return CRYPT_OK on success
72 */73 intfunction(void *src, void *dst) nothrowcopy;
74 75 /* ---- trivial low level functions ---- */76 77 /** set small constant
78 @param a Number to write to
79 @param n Source upto bits_per_digit (actually meant for very small constants)
80 @return CRYPT_OK on succcess
81 */82 intfunction(void *a, c_ulongn) nothrowset_int;
83 84 /** get small constant
85 @param a Number to read, only fetches upto bits_per_digit from the number
86 @return The lower bits_per_digit of the integer (unsigned)
87 */88 c_ulongfunction(void *a) nothrowget_int;
89 90 /** get digit n
91 @param a The number to read from
92 @param n The number of the digit to fetch
93 @return The bits_per_digit sized n'th digit of a
94 */95 c_ulongfunction(void *a, intn) nothrowget_digit;
96 97 /** Get the number of digits that represent the number
98 @param a The number to count
99 @return The number of digits used to represent the number
100 */101 intfunction(void *a) nothrowget_digit_count;
102 103 /** compare two integers
104 @param a The left side integer
105 @param b The right side integer
106 @return LTC_MP_LT if a < b, LTC_MP_GT if a > b and LTC_MP_EQ otherwise. (signed comparison)
107 */108 intfunction(void *a, void *b) nothrowcompare;
109 110 /** compare against int
111 @param a The left side integer
112 @param b The right side integer (upto bits_per_digit)
113 @return LTC_MP_LT if a < b, LTC_MP_GT if a > b and LTC_MP_EQ otherwise. (signed comparison)
114 */115 intfunction(void *a, c_ulongn) nothrowcompare_d;
116 117 /** Count the number of bits used to represent the integer
118 @param a The integer to count
119 @return The number of bits required to represent the integer
120 */121 intfunction(void * a) nothrowcount_bits;
122 123 /** Count the number of LSB bits which are zero
124 @param a The integer to count
125 @return The number of contiguous zero LSB bits
126 */127 intfunction(void *a) nothrowcount_lsb_bits;
128 129 /** Compute a power of two
130 @param a The integer to store the power in
131 @param n The power of two you want to store (a = 2^n)
132 @return CRYPT_OK on success
133 */134 intfunction(void *a , intn) nothrowtwoexpt;
135 136 /* ---- radix conversions ---- */137 138 /** read ascii string
139 @param a The integer to store into
140 @param str The string to read
141 @param radix The radix the integer has been represented in (2-64)
142 @return CRYPT_OK on success
143 */144 intfunction(void *a, constchar *str, intradix) nothrowread_radix;
145 146 /** write number to string
147 @param a The integer to store
148 @param str The destination for the string
149 @param radix The radix the integer is to be represented in (2-64)
150 @return CRYPT_OK on success
151 */152 intfunction(void *a, char *str, intradix) nothrowwrite_radix;
153 154 /** get size as unsigned char string
155 @param a The integer to get the size (when stored in array of octets)
156 @return The length of the integer
157 */158 c_ulongfunction(void *a) nothrowunsigned_size;
159 160 /** store an integer as an array of octets
161 @param src The integer to store
162 @param dst The buffer to store the integer in
163 @return CRYPT_OK on success
164 */165 intfunction(void *src, ubyte* dst) nothrowunsigned_write;
166 167 /** read an array of octets and store as integer
168 @param dst The integer to load
169 @param src The array of octets
170 @param len The number of octets
171 @return CRYPT_OK on success
172 */173 intfunction(void *dst, ubyte* src, c_ulonglen) nothrowunsigned_read;
174 175 /* ---- basic math ---- */176 177 /** add two integers
178 @param a The first source integer
179 @param b The second source integer
180 @param c The destination of "a + b"
181 @return CRYPT_OK on success
182 */183 intfunction(void *a, void *b, void *c) nothrowadd;
184 185 186 /** add two integers
187 @param a The first source integer
188 @param b The second source integer (single digit of upto bits_per_digit in length)
189 @param c The destination of "a + b"
190 @return CRYPT_OK on success
191 */192 intfunction(void *a, c_ulongb, void *c) nothrowaddi;
193 194 /** subtract two integers
195 @param a The first source integer
196 @param b The second source integer
197 @param c The destination of "a - b"
198 @return CRYPT_OK on success
199 */200 intfunction(void *a, void *b, void *c) nothrowsub;
201 202 /** subtract two integers
203 @param a The first source integer
204 @param b The second source integer (single digit of upto bits_per_digit in length)
205 @param c The destination of "a - b"
206 @return CRYPT_OK on success
207 */208 intfunction(void *a, c_ulongb, void *c) nothrowsubi;
209 210 /** multiply two integers
211 @param a The first source integer
212 @param b The second source integer (single digit of upto bits_per_digit in length)
213 @param c The destination of "a * b"
214 @return CRYPT_OK on success
215 */216 intfunction(void *a, void *b, void *c) nothrowmul;
217 218 /** multiply two integers
219 @param a The first source integer
220 @param b The second source integer (single digit of upto bits_per_digit in length)
221 @param c The destination of "a * b"
222 @return CRYPT_OK on success
223 */224 intfunction(void *a, c_ulongb, void *c) nothrowmuli;
225 226 /** Square an integer
227 @param a The integer to square
228 @param b The destination
229 @return CRYPT_OK on success
230 */231 intfunction(void *a, void *b) nothrowsqr;
232 233 /** Divide an integer
234 @param a The dividend
235 @param b The divisor
236 @param c The quotient (can be NULL to signify don't care)
237 @param d The remainder (can be NULL to signify don't care)
238 @return CRYPT_OK on success
239 */240 intfunction(void *a, void *b, void *c, void *d) nothrowmpdiv;
241 242 /** divide by two
243 @param a The integer to divide (shift right)
244 @param b The destination
245 @return CRYPT_OK on success
246 */247 intfunction(void *a, void *b) nothrowdiv_2;
248 249 /** Get remainder (small value)
250 @param a The integer to reduce
251 @param b The modulus (upto bits_per_digit in length)
252 @param c The destination for the residue
253 @return CRYPT_OK on success
254 */255 intfunction(void *a, c_ulongb, c_ulong *c) nothrowmodi;
256 257 /** gcd
258 @param a The first integer
259 @param b The second integer
260 @param c The destination for (a, b)
261 @return CRYPT_OK on success
262 */263 intfunction(void *a, void *b, void *c) nothrowgcd;
264 265 /** lcm
266 @param a The first integer
267 @param b The second integer
268 @param c The destination for [a, b]
269 @return CRYPT_OK on success
270 */271 intfunction(void *a, void *b, void *c) nothrowlcm;
272 273 /** Modular multiplication
274 @param a The first source
275 @param b The second source
276 @param c The modulus
277 @param d The destination (a*b mod c)
278 @return CRYPT_OK on success
279 */280 intfunction(void *a, void *b, void *c, void *d) nothrowmulmod;
281 282 /** Modular squaring
283 @param a The first source
284 @param b The modulus
285 @param c The destination (a*a mod b)
286 @return CRYPT_OK on success
287 */288 intfunction(void *a, void *b, void *c) nothrowsqrmod;
289 290 /** Modular inversion
291 @param a The value to invert
292 @param b The modulus
293 @param c The destination (1/a mod b)
294 @return CRYPT_OK on success
295 */296 intfunction(void *, void *, void *) nothrowinvmod;
297 298 /* ---- reduction ---- */299 300 /** setup montgomery
301 @param a The modulus
302 @param b The destination for the reduction digit
303 @return CRYPT_OK on success
304 */305 intfunction(void *a, void **b) nothrowmontgomery_setup;
306 307 /** get normalization value
308 @param a The destination for the normalization value
309 @param b The modulus
310 @return CRYPT_OK on success
311 */312 intfunction(void *a, void *b) nothrowmontgomery_normalization;
313 314 /** reduce a number
315 @param a The number [and dest] to reduce
316 @param b The modulus
317 @param c The value "b" from montgomery_setup()
318 @return CRYPT_OK on success
319 */320 intfunction(void *a, void *b, void *c) nothrowmontgomery_reduce;
321 322 /** clean up (frees memory)
323 @param a The value "b" from montgomery_setup()
324 @return CRYPT_OK on success
325 */326 voidfunction(void *a) nothrowmontgomery_deinit;
327 328 /* ---- exponentiation ---- */329 330 /** Modular exponentiation
331 @param a The base integer
332 @param b The power (can be negative) integer
333 @param c The modulus integer
334 @param d The destination
335 @return CRYPT_OK on success
336 */337 intfunction(void *a, void *b, void *c, void *d) nothrowexptmod;
338 339 /** Primality testing
340 @param a The integer to test
341 @param b The destination of the result (FP_YES if prime)
342 @return CRYPT_OK on success
343 */344 intfunction(void *a, int *b) nothrowisprime;
345 346 /* ---- (optional) ecc point math ---- */347 348 /** ECC GF(p) point multiplication (from the NIST curves)
349 @param k The integer to multiply the point by
350 @param G The point to multiply
351 @param R The destination for kG
352 @param modulus The modulus for the field
353 @param map Boolean indicated whether to map back to affine or not (can be ignored if you work in affine only)
354 @return CRYPT_OK on success
355 */356 intfunction(void *k, ecc_point *G, ecc_point *R, void *modulus, intmap) nothrowecc_ptmul;
357 358 /** ECC GF(p) point addition
359 @param P The first point
360 @param Q The second point
361 @param R The destination of P + Q
362 @param modulus The modulus
363 @param mp The "b" value from montgomery_setup()
364 @return CRYPT_OK on success
365 */366 intfunction(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp) nothrowecc_ptadd;
367 368 /** ECC GF(p) point double
369 @param P The first point
370 @param R The destination of 2P
371 @param modulus The modulus
372 @param mp The "b" value from montgomery_setup()
373 @return CRYPT_OK on success
374 */375 intfunction(ecc_point *P, ecc_point *R, void *modulus, void *mp) nothrowecc_ptdbl;
376 377 /** ECC mapping from projective to affine, currently uses (x,y,z) => (x/z^2, y/z^3, 1)
378 @param P The point to map
379 @param modulus The modulus
380 @param mp The "b" value from montgomery_setup()
381 @return CRYPT_OK on success
382 @remark The mapping can be different but keep in mind a ecc_point only has three
383 integers (x,y,z) so if you use a different mapping you have to make it fit.
384 */385 intfunction(ecc_point *P, void *modulus, void *mp) nothrowecc_map;
386 387 /** Computes kA*A + kB*B = C using Shamir's Trick
388 @param A First point to multiply
389 @param kA What to multiple A by
390 @param B Second point to multiply
391 @param kB What to multiple B by
392 @param C [out] Destination point (can overlap with A or B
393 @param modulus Modulus for curve
394 @return CRYPT_OK on success
395 */396 intfunction(ecc_point *A, void *kA,
397 ecc_point *B, void *kB,
398 ecc_point *C,
399 void *modulus) nothrowecc_mul2add;
400 401 /* ---- (optional) rsa optimized math (for internal CRT) ---- */402 403 /** RSA Key Generation
404 @param prng An active PRNG state
405 @param wprng The index of the PRNG desired
406 @param size The size of the modulus (key size) desired (octets)
407 @param e The "e" value (public key). e==65537 is a good choice
408 @param key [out] Destination of a newly created private key pair
409 @return CRYPT_OK if successful, upon error all allocated ram is freed
410 */411 intfunction(prng_state *prng, intwprng, intsize, longe, rsa_key *key) nothrowrsa_keygen;
412 413 414 /** RSA exponentiation
415 @param in The octet array representing the base
416 @param inlen The length of the input
417 @param out The destination (to be stored in an octet array format)
418 @param outlen The length of the output buffer and the resulting size (zero padded to the size of the modulus)
419 @param which PK_PUBLIC for public RSA and PK_PRIVATE for private RSA
420 @param key The RSA key to use
421 @return CRYPT_OK on success
422 */423 intfunction(constubyte* _in, c_ulonginlen,
424 ubyte* _out, c_ulong *outlen, intwhich,
425 rsa_key *key) nothrowrsa_me;
426 }
427 428 extern__gsharedltc_math_descriptorltc_mp;
429 430 intltc_init_multi(void **a, ...);
431 voidltc_deinit_multi(void *a, ...);
432 433 version(LTM_DESC)
434 {
435 externconst__gsharedltc_math_descriptorltm_desc;
436 }
437 438 version(TFM_DESC)
439 {
440 externconst__gsharedltc_math_descriptortfm_desc;
441 }
442 443 version(GMP_DESC)
444 {
445 externconst__gsharedltc_math_descriptorgmp_desc;
446 }
447 448 version(DESC_DEF_ONLY) {}
449 elseversion(LTC_SOURCE)
450 {
451 452 aliasMP_DIGIT_BIT = ltc_mp.bits_per_digit;
453 454 /* some handy macros */455 intmp_init(void **a) { returnltc_mp.init(a); }
456 aliasmp_init_multi = ltc_init_multi;
457 voidmp_clear(void *a) { ltc_mp.deinit(a); }
458 aliasmp_clear_multi = ltc_deinit_multi;
459 intmp_init_copy(void **dst, void *src) { returnltc_mp.init_copy(dst, src); }
460 461 intmp_neg(void *src, void *dst) { returnltc_mp.neg(src, dst); }
462 intmp_copy(void *src, void *dst) { returnltc_mp.copy(src, dst); }
463 464 intmp_set(void *a, c_ulongn) { returnltc_mp.set_int(a, n); }
465 aliasmp_set_int = mp_set;
466 467 c_ulongmp_get_int(void *a) { returnltc_mp.get_int(a); }
468 c_ulongmp_get_digit(void *a, intn) { returnltc_mp.get_digit(a, n); }
469 intmp_get_digit_count(void *a) { returnltc_mp.get_digit_count(a); }
470 intmp_cmp(void *a, void *b) { returnltc_mp.compare(a, b); }
471 intmp_cmp_d(void *a, c_ulongn) { returnltc_mp.compare_d(a, n); }
472 intmp_count_bits(void * a) { returnltc_mp.count_bits(a); }
473 intmp_cnt_lsb(void *a) { returnltc_mp.count_lsb_bits(a); }
474 intmp_2expt(void *a , intn) { returnltc_mp.twoexpt(a, n); }
475 476 intmp_read_radix(void *a, constchar *str, intradix) { returnltc_mp.read_radix(a, str, radix); }
477 intmp_toradix(void *a, char *str, intradix) { returnltc_mp.write_radix(a, str, radix); }
478 c_ulongmp_unsigned_bin_size(void *a) { returnltc_mp.unsigned_size(a); }
479 intmp_to_unsigned_bin(void *src, ubyte* dst) { returnltc_mp.unsigned_write(src, dst); }
480 intmp_read_unsigned_bin(void *dst, ubyte* src, c_ulonglen) { returnltc_mp.unsigned_read(dst, src, len); }
481 482 intmp_add(void *a, void *b, void *c) { returnltc_mp.add(a, b, c); }
483 intmp_add_d(void *a, c_ulongb, void *c) { returnltc_mp.addi(a, b, c); }
484 intmp_sub(void *a, void *b, void *c) { returnltc_mp.sub(a, b, c); }
485 intmp_sub_d(void *a, c_ulongb, void *c) { returnltc_mp.subi(a, b, c); }
486 intmp_mul(void *a, void *b, void *c) { returnltc_mp.mul(a, b, c); }
487 intmp_mul_d(void *a, c_ulongb, void *c) { returnltc_mp.muli(a, b, c); }
488 intmp_sqr(void *a, void *b) { returnltc_mp.sqr(a, b); }
489 intmp_div(void *a, void *b, void *c, void *d) { returnltc_mp.mpdiv(a, b, c, d); }
490 intmp_div_2(void *a, void *b) { returnltc_mp.div_2(a, b); }
491 intmp_mod(void *a, void *b, void *c) { returnltc_mp.mpdiv(a, b, null, c);}
492 intmp_mod_d(void *a, c_ulongb, c_ulong *c) { returnltc_mp.modi(a, b, c); }
493 intmp_gcd(void *a, void *b, void *c) { returnltc_mp.gcd(a, b, c); }
494 intmp_lcm(void *a, void *b, void *c) { returnltc_mp.lcm(a, b, c); }
495 496 intmp_mulmod(void *a, void *b, void *c, void *d) { returnltc_mp.mulmod(a, b, c, d); }
497 intmp_sqrmod(void *a, void *b, void *c) { returnltc_mp.sqrmod(a, b, c); }
498 intmp_invmod(void *a, void *b, void *c) { returnltc_mp.invmod(a, b, c); }
499 500 intmp_montgomery_setup(void *a, void **b) { returnltc_mp.montgomery_setup(a, b);}
501 intmp_montgomery_normalization(void *a, void *b) { returnltc_mp.montgomery_normalization(a, b);}
502 intmp_montgomery_reduce(void *a, void *b, void *c) { returnltc_mp.montgomery_reduce(a, b, c);}
503 voidmp_montgomery_free(void *a) { returnltc_mp.montgomery_deinit(a);}
504 505 intmp_exptmod(void *a, void *b, void *c, void *d) { returnltc_mp.exptmod(a, b, c, d);}
506 intmp_prime_is_prime(void *a, int *b) { returnltc_mp.isprime(a, b);}
507 508 boolmp_iszero(void* a) {return (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO);}
509 boolmp_isodd(void *a) {return (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO);}
510 voidmp_exch(T)(Ta, Tb) {void *ABC__tmp = a; a = b; b = ABC__tmp;}
511 512 intmp_tohex(void *a, char *str) {returnmp_toradix(a, str, 16);}
513 }
514 515 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_math.h,v $ */516 /* $Revision: 1.44 $ */517 /* $Date: 2007/05/12 14:32:35 $ */