3
|
1 /* ---- NUMBER THEORY ---- */ |
|
2 #ifdef MPI |
|
3 |
15
|
4 #include "ltc_tommath.h" |
3
|
5 |
|
6 /* in/out macros */ |
|
7 #define OUTPUT_BIGNUM(num, out, y, z) \ |
|
8 { \ |
|
9 if ((y + 4) > *outlen) { return CRYPT_BUFFER_OVERFLOW; } \ |
|
10 z = (unsigned long)mp_unsigned_bin_size(num); \ |
|
11 STORE32L(z, out+y); \ |
|
12 y += 4; \ |
|
13 if ((y + z) > *outlen) { return CRYPT_BUFFER_OVERFLOW; } \ |
|
14 if ((err = mp_to_unsigned_bin(num, out+y)) != MP_OKAY) { return mpi_to_ltc_error(err); } \ |
|
15 y += z; \ |
|
16 } |
|
17 |
|
18 |
|
19 #define INPUT_BIGNUM(num, in, x, y, inlen) \ |
|
20 { \ |
|
21 /* load value */ \ |
|
22 if ((y + 4) > inlen) { \ |
|
23 err = CRYPT_INVALID_PACKET; \ |
|
24 goto error; \ |
|
25 } \ |
|
26 LOAD32L(x, in+y); \ |
|
27 y += 4; \ |
|
28 \ |
|
29 /* sanity check... */ \ |
|
30 if ((x+y) > inlen) { \ |
|
31 err = CRYPT_INVALID_PACKET; \ |
|
32 goto error; \ |
|
33 } \ |
|
34 \ |
|
35 /* load it */ \ |
|
36 if ((err = mp_read_unsigned_bin(num, (unsigned char *)in+y, (int)x)) != MP_OKAY) {\ |
|
37 err = mpi_to_ltc_error(err); \ |
|
38 goto error; \ |
|
39 } \ |
|
40 y += x; \ |
|
41 if ((err = mp_shrink(num)) != MP_OKAY) { \ |
|
42 err = mpi_to_ltc_error(err); \ |
|
43 goto error; \ |
|
44 } \ |
|
45 } |
|
46 |
|
47 extern int is_prime(mp_int *, int *); |
|
48 extern int rand_prime(mp_int *N, long len, prng_state *prng, int wprng); |
|
49 |
|
50 #else |
|
51 #ifdef MRSA |
|
52 #error RSA requires the big int library |
|
53 #endif |
|
54 #ifdef MECC |
|
55 #error ECC requires the big int library |
|
56 #endif |
|
57 #ifdef MDH |
|
58 #error DH requires the big int library |
|
59 #endif |
|
60 #ifdef MDSA |
|
61 #error DSA requires the big int library |
|
62 #endif |
|
63 #endif /* MPI */ |
|
64 |
|
65 |
|
66 /* ---- PUBLIC KEY CRYPTO ---- */ |
|
67 |
|
68 #define PK_PRIVATE 0 /* PK private keys */ |
|
69 #define PK_PUBLIC 1 /* PK public keys */ |
|
70 #define PK_PRIVATE_OPTIMIZED 2 /* PK private key [rsa optimized] */ |
|
71 |
|
72 /* ---- PACKET ---- */ |
|
73 #ifdef PACKET |
|
74 |
|
75 extern void packet_store_header(unsigned char *dst, int section, int subsection); |
|
76 extern int packet_valid_header(unsigned char *src, int section, int subsection); |
|
77 |
|
78 #endif |
|
79 |
|
80 |
|
81 /* ---- RSA ---- */ |
|
82 #ifdef MRSA |
|
83 |
|
84 /* Min and Max RSA key sizes (in bits) */ |
|
85 #define MIN_RSA_SIZE 1024 |
|
86 #define MAX_RSA_SIZE 4096 |
|
87 |
|
88 /* Stack required for temps (plus padding) */ |
15
|
89 // #define RSA_STACK (8 + (MAX_RSA_SIZE/8)) |
3
|
90 |
|
91 typedef struct Rsa_key { |
|
92 int type; |
|
93 mp_int e, d, N, qP, pQ, dP, dQ, p, q; |
|
94 } rsa_key; |
|
95 |
|
96 extern int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); |
|
97 |
15
|
98 extern int rsa_exptmod(const unsigned char *in, unsigned long inlen, |
|
99 unsigned char *out, unsigned long *outlen, int which, |
|
100 prng_state *prng, int prng_idx, |
|
101 rsa_key *key); |
3
|
102 |
15
|
103 #ifdef RSA_TIMING |
3
|
104 |
15
|
105 extern int tim_exptmod(prng_state *prng, int prng_idx, |
|
106 mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m); |
3
|
107 |
15
|
108 #else |
3
|
109 |
15
|
110 #define tim_exptmod(prng, prng_idx, c, e, d, n, m) mpi_to_ltc_error(mp_exptmod(c, d, n, m)) |
3
|
111 |
15
|
112 #endif |
3
|
113 |
|
114 extern void rsa_free(rsa_key *key); |
|
115 |
15
|
116 int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen, |
|
117 unsigned char *outkey, unsigned long *outlen, |
|
118 const unsigned char *lparam, unsigned long lparamlen, |
|
119 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key); |
|
120 |
|
121 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, |
|
122 unsigned char *outkey, unsigned long *keylen, |
|
123 const unsigned char *lparam, unsigned long lparamlen, |
|
124 prng_state *prng, int prng_idx, |
|
125 int hash_idx, int *res, |
|
126 rsa_key *key); |
3
|
127 |
15
|
128 int rsa_sign_hash(const unsigned char *msghash, unsigned long msghashlen, |
|
129 unsigned char *sig, unsigned long *siglen, |
|
130 prng_state *prng, int prng_idx, |
|
131 int hash_idx, unsigned long saltlen, |
|
132 rsa_key *key); |
3
|
133 |
15
|
134 int rsa_verify_hash(const unsigned char *sig, unsigned long siglen, |
|
135 const unsigned char *msghash, unsigned long msghashlen, |
|
136 prng_state *prng, int prng_idx, |
|
137 int hash_idx, unsigned long saltlen, |
|
138 int *stat, rsa_key *key); |
3
|
139 |
15
|
140 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); |
|
141 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); |
|
142 |
3
|
143 #endif |
|
144 |
|
145 /* ---- DH Routines ---- */ |
|
146 #ifdef MDH |
|
147 |
|
148 typedef struct Dh_key { |
|
149 int idx, type; |
|
150 mp_int x, y; |
|
151 } dh_key; |
|
152 |
|
153 extern int dh_test(void); |
|
154 extern void dh_sizes(int *low, int *high); |
|
155 extern int dh_get_size(dh_key *key); |
|
156 |
|
157 extern int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key); |
|
158 extern void dh_free(dh_key *key); |
|
159 |
|
160 extern int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key); |
|
161 extern int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key); |
|
162 |
|
163 extern int dh_shared_secret(dh_key *private_key, dh_key *public_key, |
|
164 unsigned char *out, unsigned long *outlen); |
|
165 |
|
166 extern int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen, |
|
167 unsigned char *out, unsigned long *len, |
|
168 prng_state *prng, int wprng, int hash, |
|
169 dh_key *key); |
|
170 |
|
171 extern int dh_decrypt_key(const unsigned char *in, unsigned long inlen, |
|
172 unsigned char *outkey, unsigned long *keylen, |
|
173 dh_key *key); |
|
174 |
|
175 extern int dh_sign_hash(const unsigned char *in, unsigned long inlen, |
|
176 unsigned char *out, unsigned long *outlen, |
|
177 prng_state *prng, int wprng, dh_key *key); |
|
178 |
|
179 extern int dh_verify_hash(const unsigned char *sig, unsigned long siglen, |
|
180 const unsigned char *hash, unsigned long hashlen, |
|
181 int *stat, dh_key *key); |
|
182 |
|
183 |
|
184 #endif |
|
185 |
|
186 /* ---- ECC Routines ---- */ |
|
187 #ifdef MECC |
|
188 typedef struct { |
|
189 mp_int x, y; |
|
190 } ecc_point; |
|
191 |
|
192 typedef struct { |
|
193 int type, idx; |
|
194 ecc_point pubkey; |
|
195 mp_int k; |
|
196 } ecc_key; |
|
197 |
|
198 extern int ecc_test(void); |
|
199 extern void ecc_sizes(int *low, int *high); |
|
200 extern int ecc_get_size(ecc_key *key); |
|
201 |
|
202 extern int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); |
|
203 extern void ecc_free(ecc_key *key); |
|
204 |
|
205 extern int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); |
|
206 extern int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); |
|
207 |
|
208 extern int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, |
|
209 unsigned char *out, unsigned long *outlen); |
|
210 |
|
211 extern int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen, |
|
212 unsigned char *out, unsigned long *len, |
|
213 prng_state *prng, int wprng, int hash, |
|
214 ecc_key *key); |
|
215 |
|
216 extern int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, |
|
217 unsigned char *outkey, unsigned long *keylen, |
|
218 ecc_key *key); |
|
219 |
|
220 extern int ecc_sign_hash(const unsigned char *in, unsigned long inlen, |
|
221 unsigned char *out, unsigned long *outlen, |
|
222 prng_state *prng, int wprng, ecc_key *key); |
|
223 |
|
224 extern int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, |
|
225 const unsigned char *hash, unsigned long hashlen, |
|
226 int *stat, ecc_key *key); |
|
227 #endif |
|
228 |
|
229 #ifdef MDSA |
|
230 |
|
231 typedef struct { |
|
232 int type, qord; |
|
233 mp_int g, q, p, x, y; |
|
234 } dsa_key; |
|
235 |
|
236 extern int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); |
|
237 extern void dsa_free(dsa_key *key); |
|
238 |
|
239 extern int dsa_sign_hash(const unsigned char *in, unsigned long inlen, |
|
240 unsigned char *out, unsigned long *outlen, |
|
241 prng_state *prng, int wprng, dsa_key *key); |
|
242 |
|
243 extern int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, |
|
244 const unsigned char *hash, unsigned long inlen, |
|
245 int *stat, dsa_key *key); |
|
246 |
|
247 extern int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); |
|
248 |
|
249 extern int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); |
|
250 |
|
251 extern int dsa_verify_key(dsa_key *key, int *stat); |
|
252 |
|
253 #endif |