comparison mycrypt_pk.h @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 6362d3854bb4
children c2b93763dac9
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
42 err = mpi_to_ltc_error(err); \ 42 err = mpi_to_ltc_error(err); \
43 goto error; \ 43 goto error; \
44 } \ 44 } \
45 } 45 }
46 46
47 extern int is_prime(mp_int *, int *); 47 int is_prime(mp_int *, int *);
48 extern int rand_prime(mp_int *N, long len, prng_state *prng, int wprng); 48 int rand_prime(mp_int *N, long len, prng_state *prng, int wprng);
49 49
50 #else 50 #else
51 #ifdef MRSA 51 #ifdef MRSA
52 #error RSA requires the big int library 52 #error RSA requires the big int library
53 #endif 53 #endif
65 65
66 /* ---- PUBLIC KEY CRYPTO ---- */ 66 /* ---- PUBLIC KEY CRYPTO ---- */
67 67
68 #define PK_PRIVATE 0 /* PK private keys */ 68 #define PK_PRIVATE 0 /* PK private keys */
69 #define PK_PUBLIC 1 /* PK public keys */ 69 #define PK_PUBLIC 1 /* PK public keys */
70 #define PK_PRIVATE_OPTIMIZED 2 /* PK private key [rsa optimized] */
71 70
72 /* ---- PACKET ---- */ 71 /* ---- PACKET ---- */
73 #ifdef PACKET 72 #ifdef PACKET
74 73
75 extern void packet_store_header(unsigned char *dst, int section, int subsection); 74 void packet_store_header(unsigned char *dst, int section, int subsection);
76 extern int packet_valid_header(unsigned char *src, int section, int subsection); 75 int packet_valid_header(unsigned char *src, int section, int subsection);
77 76
78 #endif 77 #endif
79 78
80 79
81 /* ---- RSA ---- */ 80 /* ---- RSA ---- */
88 /* Stack required for temps (plus padding) */ 87 /* Stack required for temps (plus padding) */
89 // #define RSA_STACK (8 + (MAX_RSA_SIZE/8)) 88 // #define RSA_STACK (8 + (MAX_RSA_SIZE/8))
90 89
91 typedef struct Rsa_key { 90 typedef struct Rsa_key {
92 int type; 91 int type;
93 mp_int e, d, N, qP, pQ, dP, dQ, p, q; 92 mp_int e, d, N, p, q, qP, dP, dQ;
94 } rsa_key; 93 } rsa_key;
95 94
96 extern int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); 95 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
97 96
98 extern int rsa_exptmod(const unsigned char *in, unsigned long inlen, 97 int rsa_exptmod(const unsigned char *in, unsigned long inlen,
99 unsigned char *out, unsigned long *outlen, int which, 98 unsigned char *out, unsigned long *outlen, int which,
100 prng_state *prng, int prng_idx, 99 prng_state *prng, int prng_idx,
101 rsa_key *key); 100 rsa_key *key);
102 101
103 #ifdef RSA_TIMING 102 #ifdef RSA_TIMING
104 103
105 extern int tim_exptmod(prng_state *prng, int prng_idx, 104 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); 105 mp_int *c, mp_int *e, mp_int *d, mp_int *n, mp_int *m);
107 106
108 #else 107 #else
109 108
110 #define tim_exptmod(prng, prng_idx, c, e, d, n, m) mpi_to_ltc_error(mp_exptmod(c, d, n, m)) 109 #define tim_exptmod(prng, prng_idx, c, e, d, n, m) mpi_to_ltc_error(mp_exptmod(c, d, n, m))
111 110
112 #endif 111 #endif
113 112
114 extern void rsa_free(rsa_key *key); 113 void rsa_free(rsa_key *key);
115 114
115 /* These use PKCS #1 v2.0 padding */
116 int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen, 116 int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen,
117 unsigned char *outkey, unsigned long *outlen, 117 unsigned char *outkey, unsigned long *outlen,
118 const unsigned char *lparam, unsigned long lparamlen, 118 const unsigned char *lparam, unsigned long lparamlen,
119 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key); 119 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key);
120 120
135 const unsigned char *msghash, unsigned long msghashlen, 135 const unsigned char *msghash, unsigned long msghashlen,
136 prng_state *prng, int prng_idx, 136 prng_state *prng, int prng_idx,
137 int hash_idx, unsigned long saltlen, 137 int hash_idx, unsigned long saltlen,
138 int *stat, rsa_key *key); 138 int *stat, rsa_key *key);
139 139
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 */
140 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); 164 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); 165 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
142 166
143 #endif 167 #endif
144 168
148 typedef struct Dh_key { 172 typedef struct Dh_key {
149 int idx, type; 173 int idx, type;
150 mp_int x, y; 174 mp_int x, y;
151 } dh_key; 175 } dh_key;
152 176
153 extern int dh_test(void); 177 int dh_test(void);
154 extern void dh_sizes(int *low, int *high); 178 void dh_sizes(int *low, int *high);
155 extern int dh_get_size(dh_key *key); 179 int dh_get_size(dh_key *key);
156 180
157 extern int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key); 181 int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
158 extern void dh_free(dh_key *key); 182 void dh_free(dh_key *key);
159 183
160 extern int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key); 184 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); 185 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
162 186
163 extern int dh_shared_secret(dh_key *private_key, dh_key *public_key, 187 int dh_shared_secret(dh_key *private_key, dh_key *public_key,
164 unsigned char *out, unsigned long *outlen); 188 unsigned char *out, unsigned long *outlen);
165 189
166 extern int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen, 190 int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
167 unsigned char *out, unsigned long *len, 191 unsigned char *out, unsigned long *len,
168 prng_state *prng, int wprng, int hash, 192 prng_state *prng, int wprng, int hash,
169 dh_key *key); 193 dh_key *key);
170 194
171 extern int dh_decrypt_key(const unsigned char *in, unsigned long inlen, 195 int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
172 unsigned char *outkey, unsigned long *keylen, 196 unsigned char *outkey, unsigned long *keylen,
173 dh_key *key); 197 dh_key *key);
174 198
175 extern int dh_sign_hash(const unsigned char *in, unsigned long inlen, 199 int dh_sign_hash(const unsigned char *in, unsigned long inlen,
176 unsigned char *out, unsigned long *outlen, 200 unsigned char *out, unsigned long *outlen,
177 prng_state *prng, int wprng, dh_key *key); 201 prng_state *prng, int wprng, dh_key *key);
178 202
179 extern int dh_verify_hash(const unsigned char *sig, unsigned long siglen, 203 int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
180 const unsigned char *hash, unsigned long hashlen, 204 const unsigned char *hash, unsigned long hashlen,
181 int *stat, dh_key *key); 205 int *stat, dh_key *key);
182 206
183 207
184 #endif 208 #endif
193 int type, idx; 217 int type, idx;
194 ecc_point pubkey; 218 ecc_point pubkey;
195 mp_int k; 219 mp_int k;
196 } ecc_key; 220 } ecc_key;
197 221
198 extern int ecc_test(void); 222 int ecc_test(void);
199 extern void ecc_sizes(int *low, int *high); 223 void ecc_sizes(int *low, int *high);
200 extern int ecc_get_size(ecc_key *key); 224 int ecc_get_size(ecc_key *key);
201 225
202 extern int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); 226 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
203 extern void ecc_free(ecc_key *key); 227 void ecc_free(ecc_key *key);
204 228
205 extern int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); 229 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); 230 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
207 231
208 extern int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, 232 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
209 unsigned char *out, unsigned long *outlen); 233 unsigned char *out, unsigned long *outlen);
210 234
211 extern int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen, 235 int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
212 unsigned char *out, unsigned long *len, 236 unsigned char *out, unsigned long *len,
213 prng_state *prng, int wprng, int hash, 237 prng_state *prng, int wprng, int hash,
214 ecc_key *key); 238 ecc_key *key);
215 239
216 extern int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, 240 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
217 unsigned char *outkey, unsigned long *keylen, 241 unsigned char *outkey, unsigned long *keylen,
218 ecc_key *key); 242 ecc_key *key);
219 243
220 extern int ecc_sign_hash(const unsigned char *in, unsigned long inlen, 244 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
221 unsigned char *out, unsigned long *outlen, 245 unsigned char *out, unsigned long *outlen,
222 prng_state *prng, int wprng, ecc_key *key); 246 prng_state *prng, int wprng, ecc_key *key);
223 247
224 extern int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, 248 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
225 const unsigned char *hash, unsigned long hashlen, 249 const unsigned char *hash, unsigned long hashlen,
226 int *stat, ecc_key *key); 250 int *stat, ecc_key *key);
227 #endif 251 #endif
228 252
229 #ifdef MDSA 253 #ifdef MDSA
231 typedef struct { 255 typedef struct {
232 int type, qord; 256 int type, qord;
233 mp_int g, q, p, x, y; 257 mp_int g, q, p, x, y;
234 } dsa_key; 258 } dsa_key;
235 259
236 extern int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); 260 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); 261 void dsa_free(dsa_key *key);
238 262
239 extern int dsa_sign_hash(const unsigned char *in, unsigned long inlen, 263 int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
240 unsigned char *out, unsigned long *outlen, 264 unsigned char *out, unsigned long *outlen,
241 prng_state *prng, int wprng, dsa_key *key); 265 prng_state *prng, int wprng, dsa_key *key);
242 266
243 extern int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, 267 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
244 const unsigned char *hash, unsigned long inlen, 268 const unsigned char *hash, unsigned long inlen,
245 int *stat, dsa_key *key); 269 int *stat, dsa_key *key);
246 270
247 extern int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); 271 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
248 272
249 extern int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); 273 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
250 274
251 extern int dsa_verify_key(dsa_key *key, int *stat); 275 int dsa_verify_key(dsa_key *key, int *stat);
252 276
253 #endif 277 #endif
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