Mercurial > dropbear
comparison src/headers/tomcrypt_pk.h @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
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 /* Stack required for temps (plus padding) */ | |
88 // #define RSA_STACK (8 + (MAX_RSA_SIZE/8)) | |
89 | |
90 typedef struct Rsa_key { | |
91 int type; | |
92 mp_int e, d, N, p, q, qP, dP, dQ; | |
93 } rsa_key; | |
94 | |
95 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); | |
96 | |
97 int rsa_exptmod(const unsigned char *in, unsigned long inlen, | |
98 unsigned char *out, unsigned long *outlen, int which, | |
99 rsa_key *key); | |
100 | |
101 void rsa_free(rsa_key *key); | |
102 | |
103 /* These use PKCS #1 v2.0 padding */ | |
104 int rsa_encrypt_key(const unsigned char *in, unsigned long inlen, | |
105 unsigned char *out, unsigned long *outlen, | |
106 const unsigned char *lparam, unsigned long lparamlen, | |
107 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key); | |
108 | |
109 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, | |
110 unsigned char *out, unsigned long *outlen, | |
111 const unsigned char *lparam, unsigned long lparamlen, | |
112 int hash_idx, int *stat, | |
113 rsa_key *key); | |
114 | |
115 int rsa_sign_hash(const unsigned char *in, unsigned long inlen, | |
116 unsigned char *out, unsigned long *outlen, | |
117 prng_state *prng, int prng_idx, | |
118 int hash_idx, unsigned long saltlen, | |
119 rsa_key *key); | |
120 | |
121 int rsa_verify_hash(const unsigned char *sig, unsigned long siglen, | |
122 const unsigned char *hash, unsigned long hashlen, | |
123 int hash_idx, unsigned long saltlen, | |
124 int *stat, rsa_key *key); | |
125 | |
126 /* these use PKCS #1 v1.5 padding */ | |
127 int rsa_v15_encrypt_key(const unsigned char *in, unsigned long inlen, | |
128 unsigned char *out, unsigned long *outlen, | |
129 prng_state *prng, int prng_idx, | |
130 rsa_key *key); | |
131 | |
132 int rsa_v15_decrypt_key(const unsigned char *in, unsigned long inlen, | |
133 unsigned char *out, unsigned long outlen, | |
134 int *stat, rsa_key *key); | |
135 | |
136 int rsa_v15_sign_hash(const unsigned char *in, unsigned long inlen, | |
137 unsigned char *out, unsigned long *siglen, | |
138 int hash_idx, rsa_key *key); | |
139 | |
140 int rsa_v15_verify_hash(const unsigned char *sig, unsigned long siglen, | |
141 const unsigned char *hash, unsigned long hashlen, | |
142 int hash_idx, int *stat, | |
143 rsa_key *key); | |
144 | |
145 | |
146 /* PKCS #1 import/export */ | |
147 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); | |
148 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); | |
149 | |
150 #endif | |
151 | |
152 /* ---- DH Routines ---- */ | |
153 #ifdef MDH | |
154 | |
155 typedef struct Dh_key { | |
156 int idx, type; | |
157 mp_int x, y; | |
158 } dh_key; | |
159 | |
160 int dh_test(void); | |
161 void dh_sizes(int *low, int *high); | |
162 int dh_get_size(dh_key *key); | |
163 | |
164 int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key); | |
165 void dh_free(dh_key *key); | |
166 | |
167 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key); | |
168 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key); | |
169 | |
170 int dh_shared_secret(dh_key *private_key, dh_key *public_key, | |
171 unsigned char *out, unsigned long *outlen); | |
172 | |
173 int dh_encrypt_key(const unsigned char *in, unsigned long keylen, | |
174 unsigned char *out, unsigned long *outlen, | |
175 prng_state *prng, int wprng, int hash, | |
176 dh_key *key); | |
177 | |
178 int dh_decrypt_key(const unsigned char *in, unsigned long inlen, | |
179 unsigned char *out, unsigned long *outlen, | |
180 dh_key *key); | |
181 | |
182 int dh_sign_hash(const unsigned char *in, unsigned long inlen, | |
183 unsigned char *out, unsigned long *outlen, | |
184 prng_state *prng, int wprng, dh_key *key); | |
185 | |
186 int dh_verify_hash(const unsigned char *sig, unsigned long siglen, | |
187 const unsigned char *hash, unsigned long hashlen, | |
188 int *stat, dh_key *key); | |
189 | |
190 | |
191 #endif | |
192 | |
193 /* ---- ECC Routines ---- */ | |
194 #ifdef MECC | |
195 typedef struct { | |
196 mp_int x, y, z; | |
197 } ecc_point; | |
198 | |
199 typedef struct { | |
200 int type, idx; | |
201 ecc_point pubkey; | |
202 mp_int k; | |
203 } ecc_key; | |
204 | |
205 int ecc_test(void); | |
206 void ecc_sizes(int *low, int *high); | |
207 int ecc_get_size(ecc_key *key); | |
208 | |
209 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); | |
210 void ecc_free(ecc_key *key); | |
211 | |
212 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); | |
213 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key); | |
214 | |
215 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, | |
216 unsigned char *out, unsigned long *outlen); | |
217 | |
218 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen, | |
219 unsigned char *out, unsigned long *outlen, | |
220 prng_state *prng, int wprng, int hash, | |
221 ecc_key *key); | |
222 | |
223 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, | |
224 unsigned char *out, unsigned long *outlen, | |
225 ecc_key *key); | |
226 | |
227 int ecc_sign_hash(const unsigned char *in, unsigned long inlen, | |
228 unsigned char *out, unsigned long *outlen, | |
229 prng_state *prng, int wprng, ecc_key *key); | |
230 | |
231 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, | |
232 const unsigned char *hash, unsigned long hashlen, | |
233 int *stat, ecc_key *key); | |
234 | |
235 #endif | |
236 | |
237 #ifdef MDSA | |
238 | |
239 typedef struct { | |
240 int type, qord; | |
241 mp_int g, q, p, x, y; | |
242 } dsa_key; | |
243 | |
244 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); | |
245 void dsa_free(dsa_key *key); | |
246 | |
247 int dsa_sign_hash(const unsigned char *in, unsigned long inlen, | |
248 unsigned char *out, unsigned long *outlen, | |
249 prng_state *prng, int wprng, dsa_key *key); | |
250 | |
251 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, | |
252 const unsigned char *hash, unsigned long hashlen, | |
253 int *stat, dsa_key *key); | |
254 | |
255 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); | |
256 | |
257 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); | |
258 | |
259 int dsa_verify_key(dsa_key *key, int *stat); | |
260 | |
261 #endif | |
262 | |
263 #ifdef LTC_DER | |
264 /* DER handling */ | |
265 int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen); | |
266 int der_decode_integer(const unsigned char *in, unsigned long *inlen, mp_int *num); | |
267 int der_length_integer(mp_int *num, unsigned long *len); | |
268 int der_put_multi_integer(unsigned char *dst, unsigned long *outlen, mp_int *num, ...); | |
269 int der_get_multi_integer(const unsigned char *src, unsigned long *inlen, mp_int *num, ...); | |
270 #endif |