Mercurial > dropbear
comparison libtomcrypt/src/headers/tomcrypt_pk.h @ 285:1b9e69c058d2
propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 08 Mar 2006 13:23:58 +0000 |
parents | |
children | 0cbe8f6dbf9e |
comparison
equal
deleted
inserted
replaced
281:997e6f7dc01e | 285:1b9e69c058d2 |
---|---|
1 /* ---- NUMBER THEORY ---- */ | |
2 #ifdef MPI | |
3 | |
4 #include "ltc_tommath.h" | |
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 int is_prime(mp_int *, int *); | |
48 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 | |
71 /* ---- PACKET ---- */ | |
72 #ifdef PACKET | |
73 | |
74 void packet_store_header(unsigned char *dst, int section, int subsection); | |
75 int packet_valid_header(unsigned char *src, int section, int subsection); | |
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 typedef struct Rsa_key { | |
88 int type; | |
89 mp_int e, d, N, p, q, qP, dP, dQ; | |
90 } rsa_key; | |
91 | |
92 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); | |
93 | |
94 int rsa_exptmod(const unsigned char *in, unsigned long inlen, | |
95 unsigned char *out, unsigned long *outlen, int which, | |
96 rsa_key *key); | |
97 | |
98 void rsa_free(rsa_key *key); | |
99 | |
100 /* These use PKCS #1 v2.0 padding */ | |
101 int rsa_encrypt_key(const unsigned char *in, unsigned long inlen, | |
102 unsigned char *out, unsigned long *outlen, | |
103 const unsigned char *lparam, unsigned long lparamlen, | |
104 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key); | |
105 | |
106 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, | |
107 unsigned char *out, unsigned long *outlen, | |
108 const unsigned char *lparam, unsigned long lparamlen, | |
109 int hash_idx, int *stat, | |
110 rsa_key *key); | |
111 | |
112 int rsa_sign_hash(const unsigned char *in, unsigned long inlen, | |
113 unsigned char *out, unsigned long *outlen, | |
114 prng_state *prng, int prng_idx, | |
115 int hash_idx, unsigned long saltlen, | |
116 rsa_key *key); | |
117 | |
118 int rsa_verify_hash(const unsigned char *sig, unsigned long siglen, | |
119 const unsigned char *hash, unsigned long hashlen, | |
120 int hash_idx, unsigned long saltlen, | |
121 int *stat, rsa_key *key); | |
122 | |
123 /* PKCS #1 import/export */ | |
124 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); | |
125 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); | |
126 | |
127 #endif | |
128 | |
129 /* ---- DH Routines ---- */ | |
130 #ifdef MDH | |
131 | |
132 typedef struct Dh_key { | |
133 int idx, type; | |
134 mp_int x, y; | |
135 } dh_key; | |
136 | |
137 int dh_test(void); | |
138 void dh_sizes(int *low, int *high); | |
139 int dh_get_size(dh_key *key); | |
140 | |
141 int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key); | |
142 void dh_free(dh_key *key); | |
143 | |
144 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key); | |
145 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key); | |
146 | |
147 int dh_shared_secret(dh_key *private_key, dh_key *public_key, | |
148 unsigned char *out, unsigned long *outlen); | |
149 | |
150 int dh_encrypt_key(const unsigned char *in, unsigned long keylen, | |
151 unsigned char *out, unsigned long *outlen, | |
152 prng_state *prng, int wprng, int hash, | |
153 dh_key *key); | |
154 | |
155 int dh_decrypt_key(const unsigned char *in, unsigned long inlen, | |
156 unsigned char *out, unsigned long *outlen, | |
157 dh_key *key); | |
158 | |
159 int dh_sign_hash(const unsigned char *in, unsigned long inlen, | |
160 unsigned char *out, unsigned long *outlen, | |
161 prng_state *prng, int wprng, dh_key *key); | |
162 | |
163 int dh_verify_hash(const unsigned char *sig, unsigned long siglen, | |
164 const unsigned char *hash, unsigned long hashlen, | |
165 int *stat, dh_key *key); | |
166 | |
167 | |
168 #endif | |
169 | |
170 /* ---- ECC Routines ---- */ | |
171 #ifdef MECC | |
172 typedef struct { | |
173 mp_int x, y, z; | |
174 } ecc_point; | |
175 | |
176 typedef struct { | |
177 int type, idx; | |
178 ecc_point pubkey; | |
179 mp_int k; | |
180 } ecc_key; | |
181 | |
182 int ecc_test(void); | |
183 void ecc_sizes(int *low, int *high); | |
184 int ecc_get_size(ecc_key *key); | |
185 | |
186 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); | |
187 void ecc_free(ecc_key *key); | |
188 | |
189 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); | |
190 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); | |
191 | |
192 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, | |
193 unsigned char *out, unsigned long *outlen); | |
194 | |
195 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen, | |
196 unsigned char *out, unsigned long *outlen, | |
197 prng_state *prng, int wprng, int hash, | |
198 ecc_key *key); | |
199 | |
200 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, | |
201 unsigned char *out, unsigned long *outlen, | |
202 ecc_key *key); | |
203 | |
204 int ecc_sign_hash(const unsigned char *in, unsigned long inlen, | |
205 unsigned char *out, unsigned long *outlen, | |
206 prng_state *prng, int wprng, ecc_key *key); | |
207 | |
208 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, | |
209 const unsigned char *hash, unsigned long hashlen, | |
210 int *stat, ecc_key *key); | |
211 | |
212 #endif | |
213 | |
214 #ifdef MDSA | |
215 | |
216 typedef struct { | |
217 int type, qord; | |
218 mp_int g, q, p, x, y; | |
219 } dsa_key; | |
220 | |
221 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); | |
222 void dsa_free(dsa_key *key); | |
223 | |
224 | |
225 int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen, | |
226 mp_int *r, mp_int *s, | |
227 prng_state *prng, int wprng, dsa_key *key); | |
228 | |
229 int dsa_sign_hash(const unsigned char *in, unsigned long inlen, | |
230 unsigned char *out, unsigned long *outlen, | |
231 prng_state *prng, int wprng, dsa_key *key); | |
232 | |
233 int dsa_verify_hash_raw( mp_int *r, mp_int *s, | |
234 const unsigned char *hash, unsigned long hashlen, | |
235 int *stat, dsa_key *key); | |
236 | |
237 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, | |
238 const unsigned char *hash, unsigned long hashlen, | |
239 int *stat, dsa_key *key); | |
240 | |
241 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); | |
242 | |
243 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); | |
244 | |
245 int dsa_verify_key(dsa_key *key, int *stat); | |
246 | |
247 #endif | |
248 | |
249 #ifdef LTC_DER | |
250 /* DER handling */ | |
251 | |
252 enum { | |
253 LTC_ASN1_EOL, | |
254 LTC_ASN1_INTEGER, | |
255 LTC_ASN1_SHORT_INTEGER, | |
256 LTC_ASN1_BIT_STRING, | |
257 LTC_ASN1_OCTET_STRING, | |
258 LTC_ASN1_NULL, | |
259 LTC_ASN1_OBJECT_IDENTIFIER, | |
260 LTC_ASN1_IA5_STRING, | |
261 LTC_ASN1_PRINTABLE_STRING, | |
262 LTC_ASN1_UTCTIME, | |
263 | |
264 LTC_ASN1_CHOICE, | |
265 LTC_ASN1_SEQUENCE | |
266 }; | |
267 | |
268 typedef struct { | |
269 int type; | |
270 void *data; | |
271 unsigned long size; | |
272 int used; | |
273 } ltc_asn1_list; | |
274 | |
275 #define LTC_SET_ASN1(list, index, Type, Data, Size) \ | |
276 do { \ | |
277 int LTC_MACRO_temp = (index); \ | |
278 ltc_asn1_list *LTC_MACRO_list = (list); \ | |
279 LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \ | |
280 LTC_MACRO_list[LTC_MACRO_temp].data = (Data); \ | |
281 LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \ | |
282 LTC_MACRO_list[LTC_MACRO_temp].used = 0; \ | |
283 } while (0); | |
284 | |
285 /* SEQUENCE */ | |
286 int der_encode_sequence(ltc_asn1_list *list, unsigned long inlen, | |
287 unsigned char *out, unsigned long *outlen); | |
288 | |
289 int der_decode_sequence(const unsigned char *in, unsigned long inlen, | |
290 ltc_asn1_list *list, unsigned long outlen); | |
291 | |
292 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen, | |
293 unsigned long *outlen); | |
294 | |
295 /* VA list handy helpers */ | |
296 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...); | |
297 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...); | |
298 | |
299 /* INTEGER */ | |
300 int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen); | |
301 int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num); | |
302 int der_length_integer(mp_int *num, unsigned long *len); | |
303 | |
304 /* INTEGER -- handy for 0..2^32-1 values */ | |
305 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num); | |
306 int der_encode_short_integer(unsigned long num, unsigned char *out, unsigned long *outlen); | |
307 int der_length_short_integer(unsigned long num, unsigned long *outlen); | |
308 | |
309 /* BIT STRING */ | |
310 int der_encode_bit_string(const unsigned char *in, unsigned long inlen, | |
311 unsigned char *out, unsigned long *outlen); | |
312 int der_decode_bit_string(const unsigned char *in, unsigned long inlen, | |
313 unsigned char *out, unsigned long *outlen); | |
314 int der_length_bit_string(unsigned long nbits, unsigned long *outlen); | |
315 | |
316 /* OCTET STRING */ | |
317 int der_encode_octet_string(const unsigned char *in, unsigned long inlen, | |
318 unsigned char *out, unsigned long *outlen); | |
319 int der_decode_octet_string(const unsigned char *in, unsigned long inlen, | |
320 unsigned char *out, unsigned long *outlen); | |
321 int der_length_octet_string(unsigned long noctets, unsigned long *outlen); | |
322 | |
323 /* OBJECT IDENTIFIER */ | |
324 int der_encode_object_identifier(unsigned long *words, unsigned long nwords, | |
325 unsigned char *out, unsigned long *outlen); | |
326 int der_decode_object_identifier(const unsigned char *in, unsigned long inlen, | |
327 unsigned long *words, unsigned long *outlen); | |
328 int der_length_object_identifier(unsigned long *words, unsigned long nwords, unsigned long *outlen); | |
329 unsigned long der_object_identifier_bits(unsigned long x); | |
330 | |
331 /* IA5 STRING */ | |
332 int der_encode_ia5_string(const unsigned char *in, unsigned long inlen, | |
333 unsigned char *out, unsigned long *outlen); | |
334 int der_decode_ia5_string(const unsigned char *in, unsigned long inlen, | |
335 unsigned char *out, unsigned long *outlen); | |
336 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen); | |
337 | |
338 int der_ia5_char_encode(int c); | |
339 int der_ia5_value_decode(int v); | |
340 | |
341 /* Printable STRING */ | |
342 int der_encode_printable_string(const unsigned char *in, unsigned long inlen, | |
343 unsigned char *out, unsigned long *outlen); | |
344 int der_decode_printable_string(const unsigned char *in, unsigned long inlen, | |
345 unsigned char *out, unsigned long *outlen); | |
346 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen); | |
347 | |
348 int der_printable_char_encode(int c); | |
349 int der_printable_value_decode(int v); | |
350 | |
351 /* CHOICE */ | |
352 int der_decode_choice(const unsigned char *in, unsigned long *inlen, | |
353 ltc_asn1_list *list, unsigned long outlen); | |
354 | |
355 /* UTCTime */ | |
356 typedef struct { | |
357 unsigned YY, /* year */ | |
358 MM, /* month */ | |
359 DD, /* day */ | |
360 hh, /* hour */ | |
361 mm, /* minute */ | |
362 ss, /* second */ | |
363 off_dir, /* timezone offset direction 0 == +, 1 == - */ | |
364 off_hh, /* timezone offset hours */ | |
365 off_mm; /* timezone offset minutes */ | |
366 } ltc_utctime; | |
367 | |
368 int der_encode_utctime(ltc_utctime *utctime, | |
369 unsigned char *out, unsigned long *outlen); | |
370 | |
371 int der_decode_utctime(const unsigned char *in, unsigned long *inlen, | |
372 ltc_utctime *out); | |
373 | |
374 int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen); | |
375 | |
376 | |
377 #endif | |
378 | |
379 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_pk.h,v $ */ | |
380 /* $Revision: 1.30 $ */ | |
381 /* $Date: 2005/06/19 11:23:03 $ */ |