comparison mycrypt_pk.h @ 0:d7da3b1e1540 libtomcrypt

put back the 0.95 makefile which was inadvertently merged over
author Matt Johnston <matt@ucc.asn.au>
date Mon, 31 May 2004 18:21:40 +0000
parents
children 6362d3854bb4
comparison
equal deleted inserted replaced
-1:000000000000 0:d7da3b1e1540
1 /* ---- NUMBER THEORY ---- */
2 #ifdef MPI
3
4 #include "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 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) */
89 #define RSA_STACK (8 + (MAX_RSA_SIZE/8))
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
98 extern int rsa_exptmod(const unsigned char *in, unsigned long inlen,
99 unsigned char *out, unsigned long *outlen, int which,
100 rsa_key *key);
101
102 extern int rsa_pad(const unsigned char *in, unsigned long inlen,
103 unsigned char *out, unsigned long *outlen,
104 int wprng, prng_state *prng);
105
106 extern int rsa_signpad(const unsigned char *in, unsigned long inlen,
107 unsigned char *out, unsigned long *outlen);
108
109 extern int rsa_depad(const unsigned char *in, unsigned long inlen,
110 unsigned char *out, unsigned long *outlen);
111
112 extern int rsa_signdepad(const unsigned char *in, unsigned long inlen,
113 unsigned char *out, unsigned long *outlen);
114
115
116 extern void rsa_free(rsa_key *key);
117
118 extern int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen,
119 unsigned char *outkey, unsigned long *outlen,
120 prng_state *prng, int wprng, rsa_key *key);
121
122 extern int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
123 unsigned char *outkey, unsigned long *keylen,
124 rsa_key *key);
125
126 extern int rsa_sign_hash(const unsigned char *in, unsigned long inlen,
127 unsigned char *out, unsigned long *outlen,
128 rsa_key *key);
129
130 extern int rsa_verify_hash(const unsigned char *sig, unsigned long siglen,
131 const unsigned char *hash, int *stat, rsa_key *key);
132
133 extern int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
134 extern int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
135 #endif
136
137 /* ---- DH Routines ---- */
138 #ifdef MDH
139
140 typedef struct Dh_key {
141 int idx, type;
142 mp_int x, y;
143 } dh_key;
144
145 extern int dh_test(void);
146 extern void dh_sizes(int *low, int *high);
147 extern int dh_get_size(dh_key *key);
148
149 extern int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key);
150 extern void dh_free(dh_key *key);
151
152 extern int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
153 extern int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
154
155 extern int dh_shared_secret(dh_key *private_key, dh_key *public_key,
156 unsigned char *out, unsigned long *outlen);
157
158 extern int dh_encrypt_key(const unsigned char *inkey, unsigned long keylen,
159 unsigned char *out, unsigned long *len,
160 prng_state *prng, int wprng, int hash,
161 dh_key *key);
162
163 extern int dh_decrypt_key(const unsigned char *in, unsigned long inlen,
164 unsigned char *outkey, unsigned long *keylen,
165 dh_key *key);
166
167 extern int dh_sign_hash(const unsigned char *in, unsigned long inlen,
168 unsigned char *out, unsigned long *outlen,
169 prng_state *prng, int wprng, dh_key *key);
170
171 extern int dh_verify_hash(const unsigned char *sig, unsigned long siglen,
172 const unsigned char *hash, unsigned long hashlen,
173 int *stat, dh_key *key);
174
175
176 #endif
177
178 /* ---- ECC Routines ---- */
179 #ifdef MECC
180 typedef struct {
181 mp_int x, y;
182 } ecc_point;
183
184 typedef struct {
185 int type, idx;
186 ecc_point pubkey;
187 mp_int k;
188 } ecc_key;
189
190 extern int ecc_test(void);
191 extern void ecc_sizes(int *low, int *high);
192 extern int ecc_get_size(ecc_key *key);
193
194 extern int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
195 extern void ecc_free(ecc_key *key);
196
197 extern int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key);
198 extern int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
199
200 extern int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
201 unsigned char *out, unsigned long *outlen);
202
203 extern int ecc_encrypt_key(const unsigned char *inkey, unsigned long keylen,
204 unsigned char *out, unsigned long *len,
205 prng_state *prng, int wprng, int hash,
206 ecc_key *key);
207
208 extern int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
209 unsigned char *outkey, unsigned long *keylen,
210 ecc_key *key);
211
212 extern int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
213 unsigned char *out, unsigned long *outlen,
214 prng_state *prng, int wprng, ecc_key *key);
215
216 extern int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
217 const unsigned char *hash, unsigned long hashlen,
218 int *stat, ecc_key *key);
219 #endif
220
221 #ifdef MDSA
222
223 typedef struct {
224 int type, qord;
225 mp_int g, q, p, x, y;
226 } dsa_key;
227
228 extern int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
229 extern void dsa_free(dsa_key *key);
230
231 extern int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
232 unsigned char *out, unsigned long *outlen,
233 prng_state *prng, int wprng, dsa_key *key);
234
235 extern int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
236 const unsigned char *hash, unsigned long inlen,
237 int *stat, dsa_key *key);
238
239 extern int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
240
241 extern int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
242
243 extern int dsa_verify_key(dsa_key *key, int *stat);
244
245 #endif