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 |
143
|
47 int is_prime(mp_int *, int *); |
|
48 int rand_prime(mp_int *N, long len, prng_state *prng, int wprng); |
3
|
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 |
|
71 /* ---- PACKET ---- */ |
|
72 #ifdef PACKET |
|
73 |
143
|
74 void packet_store_header(unsigned char *dst, int section, int subsection); |
|
75 int packet_valid_header(unsigned char *src, int section, int subsection); |
3
|
76 |
|
77 #endif |
|
78 |
|
79 |
|
80 /* ---- RSA ---- */ |
|
81 #ifdef MRSA |
|
82 |
|
83 /* Min and Max RSA key sizes (in bits) */ |
|
84 #define MIN_RSA_SIZE 1024 |
|
85 #define MAX_RSA_SIZE 4096 |
|
86 |
|
87 /* Stack required for temps (plus padding) */ |
15
|
88 // #define RSA_STACK (8 + (MAX_RSA_SIZE/8)) |
3
|
89 |
|
90 typedef struct Rsa_key { |
|
91 int type; |
143
|
92 mp_int e, d, N, p, q, qP, dP, dQ; |
3
|
93 } rsa_key; |
|
94 |
143
|
95 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); |
3
|
96 |
143
|
97 int rsa_exptmod(const unsigned char *in, unsigned long inlen, |
15
|
98 unsigned char *out, unsigned long *outlen, int which, |
|
99 prng_state *prng, int prng_idx, |
|
100 rsa_key *key); |
3
|
101 |
15
|
102 #ifdef RSA_TIMING |
3
|
103 |
143
|
104 int tim_exptmod(prng_state *prng, int prng_idx, |
15
|
105 mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m); |
3
|
106 |
15
|
107 #else |
3
|
108 |
15
|
109 #define tim_exptmod(prng, prng_idx, c, e, d, n, m) mpi_to_ltc_error(mp_exptmod(c, d, n, m)) |
3
|
110 |
15
|
111 #endif |
3
|
112 |
143
|
113 void rsa_free(rsa_key *key); |
3
|
114 |
143
|
115 /* These use PKCS #1 v2.0 padding */ |
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 |
143
|
140 /* these use PKCS #1 v1.5 padding */ |
|
141 int rsa_v15_encrypt_key(const unsigned char *inkey, unsigned long inlen, |
|
142 unsigned char *outkey, unsigned long *outlen, |
|
143 prng_state *prng, int prng_idx, |
|
144 rsa_key *key); |
|
145 |
|
146 int rsa_v15_decrypt_key(const unsigned char *in, unsigned long inlen, |
|
147 unsigned char *outkey, unsigned long keylen, |
|
148 prng_state *prng, int prng_idx, |
|
149 int *res, rsa_key *key); |
|
150 |
|
151 int rsa_v15_sign_hash(const unsigned char *msghash, unsigned long msghashlen, |
|
152 unsigned char *sig, unsigned long *siglen, |
|
153 prng_state *prng, int prng_idx, |
|
154 int hash_idx, rsa_key *key); |
|
155 |
|
156 int rsa_v15_verify_hash(const unsigned char *sig, unsigned long siglen, |
|
157 const unsigned char *msghash, unsigned long msghashlen, |
|
158 prng_state *prng, int prng_idx, |
|
159 int hash_idx, int *stat, |
|
160 rsa_key *key); |
|
161 |
|
162 |
|
163 /* PKCS #1 import/export */ |
15
|
164 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); |
|
165 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); |
|
166 |
3
|
167 #endif |
|
168 |
|
169 /* ---- DH Routines ---- */ |
|
170 #ifdef MDH |
|
171 |
|
172 typedef struct Dh_key { |
|
173 int idx, type; |
|
174 mp_int x, y; |
|
175 } dh_key; |
|
176 |
143
|
177 int dh_test(void); |
|
178 void dh_sizes(int *low, int *high); |
|
179 int dh_get_size(dh_key *key); |
3
|
180 |
143
|
181 int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key); |
|
182 void dh_free(dh_key *key); |
3
|
183 |
143
|
184 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key); |
|
185 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key); |
3
|
186 |
143
|
187 int dh_shared_secret(dh_key *private_key, dh_key *public_key, |
3
|
188 unsigned char *out, unsigned long *outlen); |
|
189 |
143
|
190 int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen, |
3
|
191 unsigned char *out, unsigned long *len, |
|
192 prng_state *prng, int wprng, int hash, |
|
193 dh_key *key); |
|
194 |
143
|
195 int dh_decrypt_key(const unsigned char *in, unsigned long inlen, |
3
|
196 unsigned char *outkey, unsigned long *keylen, |
|
197 dh_key *key); |
|
198 |
143
|
199 int dh_sign_hash(const unsigned char *in, unsigned long inlen, |
3
|
200 unsigned char *out, unsigned long *outlen, |
|
201 prng_state *prng, int wprng, dh_key *key); |
|
202 |
143
|
203 int dh_verify_hash(const unsigned char *sig, unsigned long siglen, |
3
|
204 const unsigned char *hash, unsigned long hashlen, |
|
205 int *stat, dh_key *key); |
|
206 |
|
207 |
|
208 #endif |
|
209 |
|
210 /* ---- ECC Routines ---- */ |
|
211 #ifdef MECC |
|
212 typedef struct { |
|
213 mp_int x, y; |
|
214 } ecc_point; |
|
215 |
|
216 typedef struct { |
|
217 int type, idx; |
|
218 ecc_point pubkey; |
|
219 mp_int k; |
|
220 } ecc_key; |
|
221 |
143
|
222 int ecc_test(void); |
|
223 void ecc_sizes(int *low, int *high); |
|
224 int ecc_get_size(ecc_key *key); |
3
|
225 |
143
|
226 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); |
|
227 void ecc_free(ecc_key *key); |
3
|
228 |
143
|
229 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); |
|
230 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); |
3
|
231 |
143
|
232 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, |
3
|
233 unsigned char *out, unsigned long *outlen); |
|
234 |
143
|
235 int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen, |
3
|
236 unsigned char *out, unsigned long *len, |
|
237 prng_state *prng, int wprng, int hash, |
|
238 ecc_key *key); |
|
239 |
143
|
240 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, |
3
|
241 unsigned char *outkey, unsigned long *keylen, |
|
242 ecc_key *key); |
|
243 |
143
|
244 int ecc_sign_hash(const unsigned char *in, unsigned long inlen, |
3
|
245 unsigned char *out, unsigned long *outlen, |
|
246 prng_state *prng, int wprng, ecc_key *key); |
|
247 |
143
|
248 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, |
3
|
249 const unsigned char *hash, unsigned long hashlen, |
|
250 int *stat, ecc_key *key); |
|
251 #endif |
|
252 |
|
253 #ifdef MDSA |
|
254 |
|
255 typedef struct { |
|
256 int type, qord; |
|
257 mp_int g, q, p, x, y; |
|
258 } dsa_key; |
|
259 |
143
|
260 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); |
|
261 void dsa_free(dsa_key *key); |
3
|
262 |
143
|
263 int dsa_sign_hash(const unsigned char *in, unsigned long inlen, |
3
|
264 unsigned char *out, unsigned long *outlen, |
|
265 prng_state *prng, int wprng, dsa_key *key); |
|
266 |
143
|
267 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, |
3
|
268 const unsigned char *hash, unsigned long inlen, |
|
269 int *stat, dsa_key *key); |
|
270 |
143
|
271 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); |
3
|
272 |
143
|
273 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); |
3
|
274 |
143
|
275 int dsa_verify_key(dsa_key *key, int *stat); |
3
|
276 |
|
277 #endif |
143
|
278 |
|
279 /* DER handling */ |
|
280 int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen); |
|
281 int der_decode_integer(const unsigned char *in, unsigned long *inlen, mp_int *num); |
|
282 int der_length_integer(mp_int *num, unsigned long *len); |
|
283 int der_put_multi_integer(unsigned char *dst, unsigned long *outlen, mp_int *num, ...); |
|
284 int der_get_multi_integer(const unsigned char *src, unsigned long *inlen, mp_int *num, ...); |
|
285 |