1f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/** math functions **/
2f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
3f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define LTC_MP_LT   -1
4f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define LTC_MP_EQ    0
5f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define LTC_MP_GT    1
6f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
7f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define LTC_MP_NO    0
8f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define LTC_MP_YES   1
9f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
10f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#ifndef MECC
11f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   typedef void ecc_point;
12f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
13f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
14f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* Dropbear has its own rsa_key. We just comment this out. */
15f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#if 0
16f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#ifndef MRSA
17f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   typedef void rsa_key;
18f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
19f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
20f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
21f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/** math descriptor */
22f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projecttypedef struct {
23f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Name of the math provider */
24f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   char *name;
25f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
26f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Bits per digit, amount of bits must fit in an unsigned long */
27f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int  bits_per_digit;
28f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
29f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- init/deinit functions ---- */
30f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
31f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** initialize a bignum
32f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param   a     The number to initialize
33f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return  CRYPT_OK on success
34f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
35f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*init)(void **a);
36f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
37f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** init copy
38f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param  dst    The number to initialize and write to
39f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param  src    The number to copy from
40f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
41f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
42f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*init_copy)(void **dst, void *src);
43f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
44f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** deinit
45f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param   a    The number to free
46f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
47f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
48f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   void (*deinit)(void *a);
49f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
50f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- data movement ---- */
51f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
52f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** negate
53f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param   src   The number to negate
54f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param   dst   The destination
55f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
56f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
57f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*neg)(void *src, void *dst);
58f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
59f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** copy
60f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param   src   The number to copy from
61f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param   dst   The number to write to
62f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
63f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
64f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*copy)(void *src, void *dst);
65f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
66f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- trivial low level functions ---- */
67f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
68f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** set small constant
69f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param a    Number to write to
70f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param n    Source upto bits_per_digit (actually meant for very small constants)
71f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on succcess
72f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
73f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*set_int)(void *a, unsigned long n);
74f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
75f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** get small constant
76f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param a    Number to read, only fetches upto bits_per_digit from the number
77f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return  The lower bits_per_digit of the integer (unsigned)
78f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
79f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   unsigned long (*get_int)(void *a);
80f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
81f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** get digit n
82f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a  The number to read from
83f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param n  The number of the digit to fetch
84f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return  The bits_per_digit  sized n'th digit of a
85f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
86f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   unsigned long (*get_digit)(void *a, int n);
87f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
88f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Get the number of digits that represent the number
89f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The number to count
90f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return The number of digits used to represent the number
91f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
92f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*get_digit_count)(void *a);
93f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
94f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** compare two integers
95f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The left side integer
96f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The right side integer
97f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return LTC_MP_LT if a < b, LTC_MP_GT if a > b and LTC_MP_EQ otherwise.  (signed comparison)
98f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
99f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*compare)(void *a, void *b);
100f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
101f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** compare against int
102f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The left side integer
103f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The right side integer (upto bits_per_digit)
104f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return LTC_MP_LT if a < b, LTC_MP_GT if a > b and LTC_MP_EQ otherwise.  (signed comparison)
105f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
106f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*compare_d)(void *a, unsigned long n);
107f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
108f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Count the number of bits used to represent the integer
109f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The integer to count
110f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return The number of bits required to represent the integer
111f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
112f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*count_bits)(void * a);
113f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
114f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Count the number of LSB bits which are zero
115f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The integer to count
116f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return The number of contiguous zero LSB bits
117f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
118f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*count_lsb_bits)(void *a);
119f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
120f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Compute a power of two
121f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a  The integer to store the power in
122f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param n  The power of two you want to store (a = 2^n)
123f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
124f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
125f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*twoexpt)(void *a , int n);
126f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
127f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- radix conversions ---- */
128f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
129f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** read ascii string
130f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a     The integer to store into
131f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param str   The string to read
132f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param radix The radix the integer has been represented in (2-64)
133f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
134f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
135f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*read_radix)(void *a, const char *str, int radix);
136f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
137f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** write number to string
138f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a     The integer to store
139f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param str   The destination for the string
140f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param radix The radix the integer is to be represented in (2-64)
141f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
142f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
143f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*write_radix)(void *a, char *str, int radix);
144f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
145f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** get size as unsigned char string
146f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a     The integer to get the size (when stored in array of octets)
147f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return The length of the integer
148f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
149f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   unsigned long (*unsigned_size)(void *a);
150f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
151f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** store an integer as an array of octets
152f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param src   The integer to store
153f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param dst   The buffer to store the integer in
154f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
155f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
156f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*unsigned_write)(void *src, unsigned char *dst);
157f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
158f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** read an array of octets and store as integer
159f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param dst   The integer to load
160f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param src   The array of octets
161f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param len   The number of octets
162f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
163f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
164f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*unsigned_read)(void *dst, unsigned char *src, unsigned long len);
165f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
166f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- basic math ---- */
167f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
168f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** add two integers
169f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The first source integer
170f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The second source integer
171f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param c   The destination of "a + b"
172f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
173f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
174f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*add)(void *a, void *b, void *c);
175f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
176f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
177f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** add two integers
178f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The first source integer
179f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The second source integer (single digit of upto bits_per_digit in length)
180f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param c   The destination of "a + b"
181f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
182f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
183f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*addi)(void *a, unsigned long b, void *c);
184f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
185f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** subtract two integers
186f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The first source integer
187f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The second source integer
188f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param c   The destination of "a - b"
189f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
190f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
191f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*sub)(void *a, void *b, void *c);
192f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
193f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** subtract two integers
194f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The first source integer
195f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The second source integer (single digit of upto bits_per_digit in length)
196f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param c   The destination of "a - b"
197f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
198f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
199f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*subi)(void *a, unsigned long b, void *c);
200f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
201f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** multiply two integers
202f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The first source integer
203f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The second source integer (single digit of upto bits_per_digit in length)
204f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param c   The destination of "a * b"
205f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
206f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
207f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*mul)(void *a, void *b, void *c);
208f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
209f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** multiply two integers
210f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a   The first source integer
211f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b   The second source integer (single digit of upto bits_per_digit in length)
212f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param c   The destination of "a * b"
213f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
214f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
215f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*muli)(void *a, unsigned long b, void *c);
216f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
217f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Square an integer
218f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a    The integer to square
219f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b    The destination
220f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
221f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
222f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*sqr)(void *a, void *b);
223f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
224f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Divide an integer
225f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param a    The dividend
226f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param b    The divisor
227f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param c    The quotient (can be NULL to signify don't care)
228f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @param d    The remainder (can be NULL to signify don't care)
229f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project     @return CRYPT_OK on success
230f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
231f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*mpdiv)(void *a, void *b, void *c, void *d);
232f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
233f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** divide by two
234f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  a   The integer to divide (shift right)
235f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  b   The destination
236f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
237f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
238f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*div_2)(void *a, void *b);
239f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
240f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Get remainder (small value)
241f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  a    The integer to reduce
242f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  b    The modulus (upto bits_per_digit in length)
243f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  c    The destination for the residue
244f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
245f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
246f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*modi)(void *a, unsigned long b, unsigned long *c);
247f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
248f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** gcd
249f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  a     The first integer
250f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  b     The second integer
251f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  c     The destination for (a, b)
252f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
253f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
254f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*gcd)(void *a, void *b, void *c);
255f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
256f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** lcm
257f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  a     The first integer
258f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  b     The second integer
259f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  c     The destination for [a, b]
260f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
261f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
262f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*lcm)(void *a, void *b, void *c);
263f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
264f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Modular multiplication
265f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  a     The first source
266f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  b     The second source
267f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  c     The modulus
268f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  d     The destination (a*b mod c)
269f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
270f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
271f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*mulmod)(void *a, void *b, void *c, void *d);
272f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
273f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Modular squaring
274f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  a     The first source
275f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  b     The modulus
276f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  c     The destination (a*a mod b)
277f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
278f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
279f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*sqrmod)(void *a, void *b, void *c);
280f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
281f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Modular inversion
282f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  a     The value to invert
283f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  b     The modulus
284f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param  c     The destination (1/a mod b)
285f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
286f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
287f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*invmod)(void *, void *, void *);
288f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
289f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- reduction ---- */
290f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
291f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** setup montgomery
292f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param a  The modulus
293f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param b  The destination for the reduction digit
294f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
295f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
296f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*montgomery_setup)(void *a, void **b);
297f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
298f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** get normalization value
299f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param a   The destination for the normalization value
300f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param b   The modulus
301f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return  CRYPT_OK on success
302f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
303f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*montgomery_normalization)(void *a, void *b);
304f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
305f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** reduce a number
306f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param a   The number [and dest] to reduce
307f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param b   The modulus
308f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param c   The value "b" from montgomery_setup()
309f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
310f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
311f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*montgomery_reduce)(void *a, void *b, void *c);
312f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
313f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** clean up  (frees memory)
314f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param a   The value "b" from montgomery_setup()
315f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
316f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
317f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   void (*montgomery_deinit)(void *a);
318f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
319f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- exponentiation ---- */
320f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
321f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Modular exponentiation
322f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param a    The base integer
323f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param b    The power (can be negative) integer
324f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param c    The modulus integer
325f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param d    The destination
326f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
327f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
328f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*exptmod)(void *a, void *b, void *c, void *d);
329f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
330f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Primality testing
331f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param a     The integer to test
332f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param b     The destination of the result (FP_YES if prime)
333f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
334f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
335f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*isprime)(void *a, int *b);
336f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
337f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ----  (optional) ecc point math ---- */
338f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
339f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** ECC GF(p) point multiplication (from the NIST curves)
340f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param k   The integer to multiply the point by
341f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param G   The point to multiply
342f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param R   The destination for kG
343f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param modulus  The modulus for the field
344f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param map Boolean indicated whether to map back to affine or not (can be ignored if you work in affine only)
345f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
346f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
347f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*ecc_ptmul)(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
348f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
349f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** ECC GF(p) point addition
350f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param P    The first point
351f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param Q    The second point
352f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param R    The destination of P + Q
353f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param modulus  The modulus
354f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param mp   The "b" value from montgomery_setup()
355f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
356f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
357f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*ecc_ptadd)(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
358f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
359f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** ECC GF(p) point double
360f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param P    The first point
361f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param R    The destination of 2P
362f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param modulus  The modulus
363f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param mp   The "b" value from montgomery_setup()
364f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
365f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
366f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*ecc_ptdbl)(ecc_point *P, ecc_point *R, void *modulus, void *mp);
367f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
368f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** ECC mapping from projective to affine, currently uses (x,y,z) => (x/z^2, y/z^3, 1)
369f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param P     The point to map
370f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param modulus The modulus
371f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param mp    The "b" value from montgomery_setup()
372f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
373f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @remark  The mapping can be different but keep in mind a ecc_point only has three
374f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project                integers (x,y,z) so if you use a different mapping you have to make it fit.
375f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
376f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*ecc_map)(ecc_point *P, void *modulus, void *mp);
377f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
378f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** Computes kA*A + kB*B = C using Shamir's Trick
379f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param A        First point to multiply
380f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param kA       What to multiple A by
381f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param B        Second point to multiply
382f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param kB       What to multiple B by
383f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param C        [out] Destination point (can overlap with A or B
384f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param modulus  Modulus for curve
385f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK on success
386f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
387f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*ecc_mul2add)(ecc_point *A, void *kA,
388f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project                      ecc_point *B, void *kB,
389f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project                      ecc_point *C,
390f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project                           void *modulus);
391f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
392f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* Dropbear has its own rsa code */
393f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#if 0
394f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* ---- (optional) rsa optimized math (for internal CRT) ---- */
395f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
396f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** RSA Key Generation
397f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param prng     An active PRNG state
398f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param wprng    The index of the PRNG desired
399f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param size     The size of the modulus (key size) desired (octets)
400f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param e        The "e" value (public key).  e==65537 is a good choice
401f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @param key      [out] Destination of a newly created private key pair
402f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project       @return CRYPT_OK if successful, upon error all allocated ram is freed
403f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    */
404f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project    int (*rsa_keygen)(prng_state *prng, int wprng, int size, long e, rsa_key *key);
405f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
406f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
407f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   /** RSA exponentiation
408f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param in       The octet array representing the base
409f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param inlen    The length of the input
410f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param out      The destination (to be stored in an octet array format)
411f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param outlen   The length of the output buffer and the resulting size (zero padded to the size of the modulus)
412f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param which    PK_PUBLIC for public RSA and PK_PRIVATE for private RSA
413f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @param key      The RSA key to use
414f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project      @return CRYPT_OK on success
415f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   */
416f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project   int (*rsa_me)(const unsigned char *in,   unsigned long inlen,
417f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project                       unsigned char *out,  unsigned long *outlen, int which,
418f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project                       rsa_key *key);
419f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
420f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project} ltc_math_descriptor;
421f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
422f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectextern ltc_math_descriptor ltc_mp;
423f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
424f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectint ltc_init_multi(void **a, ...);
425f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectvoid ltc_deinit_multi(void *a, ...);
426f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
427f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#ifdef LTM_DESC
428f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectextern const ltc_math_descriptor ltm_desc;
429f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
430f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
431f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#ifdef TFM_DESC
432f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectextern const ltc_math_descriptor tfm_desc;
433f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
434f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
435f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#ifdef GMP_DESC
436f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Projectextern const ltc_math_descriptor gmp_desc;
437f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
438f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
439f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#if !defined(DESC_DEF_ONLY) && defined(LTC_SOURCE)
440f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
441f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define MP_DIGIT_BIT                 ltc_mp.bits_per_digit
442f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
443f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* some handy macros */
444f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_init(a)                   ltc_mp.init(a)
445f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_init_multi                ltc_init_multi
446f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_clear(a)                  ltc_mp.deinit(a)
447f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_clear_multi               ltc_deinit_multi
448f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_init_copy(a, b)           ltc_mp.init_copy(a, b)
449f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
450f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_neg(a, b)                 ltc_mp.neg(a, b)
451f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_copy(a, b)                ltc_mp.copy(a, b)
452f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
453f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_set(a, b)                 ltc_mp.set_int(a, b)
454f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_set_int(a, b)             ltc_mp.set_int(a, b)
455f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_get_int(a)                ltc_mp.get_int(a)
456f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_get_digit(a, n)           ltc_mp.get_digit(a, n)
457f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_get_digit_count(a)        ltc_mp.get_digit_count(a)
458f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_cmp(a, b)                 ltc_mp.compare(a, b)
459f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_cmp_d(a, b)               ltc_mp.compare_d(a, b)
460f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_count_bits(a)             ltc_mp.count_bits(a)
461f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_cnt_lsb(a)                ltc_mp.count_lsb_bits(a)
462f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_2expt(a, b)               ltc_mp.twoexpt(a, b)
463f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
464f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_read_radix(a, b, c)       ltc_mp.read_radix(a, b, c)
465f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_toradix(a, b, c)          ltc_mp.write_radix(a, b, c)
466f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_unsigned_bin_size(a)      ltc_mp.unsigned_size(a)
467f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_to_unsigned_bin(a, b)     ltc_mp.unsigned_write(a, b)
468f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
469f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
470f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_add(a, b, c)              ltc_mp.add(a, b, c)
471f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_add_d(a, b, c)            ltc_mp.addi(a, b, c)
472f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_sub(a, b, c)              ltc_mp.sub(a, b, c)
473f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_sub_d(a, b, c)            ltc_mp.subi(a, b, c)
474f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_mul(a, b, c)              ltc_mp.mul(a, b, c)
475f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_mul_d(a, b, c)            ltc_mp.muli(a, b, c)
476f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_sqr(a, b)                 ltc_mp.sqr(a, b)
477f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_div(a, b, c, d)           ltc_mp.mpdiv(a, b, c, d)
478f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_div_2(a, b)               ltc_mp.div_2(a, b)
479f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_mod(a, b, c)              ltc_mp.mpdiv(a, b, NULL, c)
480f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_mod_d(a, b, c)            ltc_mp.modi(a, b, c)
481f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_gcd(a, b, c)              ltc_mp.gcd(a, b, c)
482f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_lcm(a, b, c)              ltc_mp.lcm(a, b, c)
483f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
484f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_mulmod(a, b, c, d)        ltc_mp.mulmod(a, b, c, d)
485f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_sqrmod(a, b, c)           ltc_mp.sqrmod(a, b, c)
486f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_invmod(a, b, c)           ltc_mp.invmod(a, b, c)
487f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
488f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_montgomery_setup(a, b)    ltc_mp.montgomery_setup(a, b)
489f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
490f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_montgomery_reduce(a, b, c)   ltc_mp.montgomery_reduce(a, b, c)
491f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_montgomery_free(a)        ltc_mp.montgomery_deinit(a)
492f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
493f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_exptmod(a,b,c,d)          ltc_mp.exptmod(a,b,c,d)
494f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_prime_is_prime(a, b, c)   ltc_mp.isprime(a, c)
495f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
496f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_iszero(a)                 (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
497f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_isodd(a)                  (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
498f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_exch(a, b)                do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0);
499f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
500f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#define mp_tohex(a, b)               mp_toradix(a, b, 16)
501f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
502f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project#endif
503f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project
504f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_math.h,v $ */
505f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Revision: 1.43 $ */
506f7fc46c63fdc8f39234fea409b8dbe116d73ebf8The Android Open Source Project/* $Date: 2006/12/02 19:23:13 $ */
507