comparison libtomcrypt/src/headers/tomcrypt_pk.h @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
1 /* ---- NUMBER THEORY ---- */ 1 /* ---- NUMBER THEORY ---- */
2 #ifdef MPI 2
3 3 enum {
4 #include "ltc_tommath.h" 4 PK_PUBLIC=0,
5 5 PK_PRIVATE=1
6 /* in/out macros */ 6 };
7 #define OUTPUT_BIGNUM(num, out, y, z) \ 7
8 { \ 8 int rand_prime(void *N, long len, prng_state *prng, int wprng);
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 9
80 /* ---- RSA ---- */ 10 /* ---- RSA ---- */
81 #ifdef MRSA 11 #ifdef MRSA
82 12
83 /* Min and Max RSA key sizes (in bits) */ 13 /* Min and Max RSA key sizes (in bits) */
84 #define MIN_RSA_SIZE 1024 14 #define MIN_RSA_SIZE 1024
85 #define MAX_RSA_SIZE 4096 15 #define MAX_RSA_SIZE 4096
86 16
17 /** RSA PKCS style key */
87 typedef struct Rsa_key { 18 typedef struct Rsa_key {
19 /** Type of key, PK_PRIVATE or PK_PUBLIC */
88 int type; 20 int type;
89 mp_int e, d, N, p, q, qP, dP, dQ; 21 /** The public exponent */
22 void *e;
23 /** The private exponent */
24 void *d;
25 /** The modulus */
26 void *N;
27 /** The p factor of N */
28 void *p;
29 /** The q factor of N */
30 void *q;
31 /** The 1/q mod p CRT param */
32 void *qP;
33 /** The d mod (p - 1) CRT param */
34 void *dP;
35 /** The d mod (q - 1) CRT param */
36 void *dQ;
90 } rsa_key; 37 } rsa_key;
91 38
92 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); 39 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
93 40
94 int rsa_exptmod(const unsigned char *in, unsigned long inlen, 41 int rsa_exptmod(const unsigned char *in, unsigned long inlen,
96 rsa_key *key); 43 rsa_key *key);
97 44
98 void rsa_free(rsa_key *key); 45 void rsa_free(rsa_key *key);
99 46
100 /* These use PKCS #1 v2.0 padding */ 47 /* These use PKCS #1 v2.0 padding */
101 int rsa_encrypt_key(const unsigned char *in, unsigned long inlen, 48 #define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
102 unsigned char *out, unsigned long *outlen, 49 rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
103 const unsigned char *lparam, unsigned long lparamlen, 50
104 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key); 51 #define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
105 52 rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
106 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, 53
107 unsigned char *out, unsigned long *outlen, 54 #define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
108 const unsigned char *lparam, unsigned long lparamlen, 55 rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
109 int hash_idx, int *stat, 56
110 rsa_key *key); 57 #define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
111 58 rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
112 int rsa_sign_hash(const unsigned char *in, unsigned long inlen, 59
113 unsigned char *out, unsigned long *outlen, 60 /* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
114 prng_state *prng, int prng_idx, 61 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
115 int hash_idx, unsigned long saltlen, 62 unsigned char *out, unsigned long *outlen,
116 rsa_key *key); 63 const unsigned char *lparam, unsigned long lparamlen,
117 64 prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
118 int rsa_verify_hash(const unsigned char *sig, unsigned long siglen, 65
119 const unsigned char *hash, unsigned long hashlen, 66 int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
120 int hash_idx, unsigned long saltlen, 67 unsigned char *out, unsigned long *outlen,
121 int *stat, rsa_key *key); 68 const unsigned char *lparam, unsigned long lparamlen,
69 int hash_idx, int padding,
70 int *stat, rsa_key *key);
71
72 int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen,
73 unsigned char *out, unsigned long *outlen,
74 int padding,
75 prng_state *prng, int prng_idx,
76 int hash_idx, unsigned long saltlen,
77 rsa_key *key);
78
79 int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen,
80 const unsigned char *hash, unsigned long hashlen,
81 int padding,
82 int hash_idx, unsigned long saltlen,
83 int *stat, rsa_key *key);
122 84
123 /* PKCS #1 import/export */ 85 /* PKCS #1 import/export */
124 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); 86 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); 87 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
126 88
127 #endif 89 #endif
128 90
129 /* ---- DH Routines ---- */ 91 /* ---- Katja ---- */
130 #ifdef MDH 92 #ifdef MKAT
131 93
132 typedef struct Dh_key { 94 /* Min and Max KAT key sizes (in bits) */
133 int idx, type; 95 #define MIN_KAT_SIZE 1024
134 mp_int x, y; 96 #define MAX_KAT_SIZE 4096
135 } dh_key; 97
136 98 /** Katja PKCS style key */
137 int dh_test(void); 99 typedef struct KAT_key {
138 void dh_sizes(int *low, int *high); 100 /** Type of key, PK_PRIVATE or PK_PUBLIC */
139 int dh_get_size(dh_key *key); 101 int type;
140 102 /** The private exponent */
141 int dh_make_key(prng_state *prng, int wprng, int keysize, dh_key *key); 103 void *d;
142 void dh_free(dh_key *key); 104 /** The modulus */
143 105 void *N;
144 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key); 106 /** The p factor of N */
145 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key); 107 void *p;
146 108 /** The q factor of N */
147 int dh_shared_secret(dh_key *private_key, dh_key *public_key, 109 void *q;
148 unsigned char *out, unsigned long *outlen); 110 /** The 1/q mod p CRT param */
149 111 void *qP;
150 int dh_encrypt_key(const unsigned char *in, unsigned long keylen, 112 /** The d mod (p - 1) CRT param */
151 unsigned char *out, unsigned long *outlen, 113 void *dP;
152 prng_state *prng, int wprng, int hash, 114 /** The d mod (q - 1) CRT param */
153 dh_key *key); 115 void *dQ;
154 116 /** The pq param */
155 int dh_decrypt_key(const unsigned char *in, unsigned long inlen, 117 void *pq;
156 unsigned char *out, unsigned long *outlen, 118 } katja_key;
157 dh_key *key); 119
158 120 int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
159 int dh_sign_hash(const unsigned char *in, unsigned long inlen, 121
160 unsigned char *out, unsigned long *outlen, 122 int katja_exptmod(const unsigned char *in, unsigned long inlen,
161 prng_state *prng, int wprng, dh_key *key); 123 unsigned char *out, unsigned long *outlen, int which,
162 124 katja_key *key);
163 int dh_verify_hash(const unsigned char *sig, unsigned long siglen, 125
164 const unsigned char *hash, unsigned long hashlen, 126 void katja_free(katja_key *key);
165 int *stat, dh_key *key); 127
166 128 /* These use PKCS #1 v2.0 padding */
167 129 int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
130 unsigned char *out, unsigned long *outlen,
131 const unsigned char *lparam, unsigned long lparamlen,
132 prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
133
134 int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
135 unsigned char *out, unsigned long *outlen,
136 const unsigned char *lparam, unsigned long lparamlen,
137 int hash_idx, int *stat,
138 katja_key *key);
139
140 /* PKCS #1 import/export */
141 int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
142 int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
143
168 #endif 144 #endif
169 145
170 /* ---- ECC Routines ---- */ 146 /* ---- ECC Routines ---- */
171 #ifdef MECC 147 #ifdef MECC
148
149 /* size of our temp buffers for exported keys */
150 #define ECC_BUF_SIZE 256
151
152 /* max private key size */
153 #define ECC_MAXSIZE 66
154
155 /** Structure defines a NIST GF(p) curve */
172 typedef struct { 156 typedef struct {
173 mp_int x, y, z; 157 /** The size of the curve in octets */
158 int size;
159
160 /** name of curve */
161 char *name;
162
163 /** The prime that defines the field the curve is in (encoded in hex) */
164 char *prime;
165
166 /** The fields B param (hex) */
167 char *B;
168
169 /** The order of the curve (hex) */
170 char *order;
171
172 /** The x co-ordinate of the base point on the curve (hex) */
173 char *Gx;
174
175 /** The y co-ordinate of the base point on the curve (hex) */
176 char *Gy;
177 } ltc_ecc_set_type;
178
179 /** A point on a ECC curve, stored in Jacbobian format such that (x,y,z) => (x/z^2, y/z^3, 1) when interpretted as affine */
180 typedef struct {
181 /** The x co-ordinate */
182 void *x;
183
184 /** The y co-ordinate */
185 void *y;
186
187 /** The z co-ordinate */
188 void *z;
174 } ecc_point; 189 } ecc_point;
175 190
191 /** An ECC key */
176 typedef struct { 192 typedef struct {
177 int type, idx; 193 /** Type of key, PK_PRIVATE or PK_PUBLIC */
194 int type;
195
196 /** Index into the ltc_ecc_sets[] for the parameters of this curve; if -1, then this key is using user supplied curve in dp */
197 int idx;
198
199 /** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
200 const ltc_ecc_set_type *dp;
201
202 /** The public key */
178 ecc_point pubkey; 203 ecc_point pubkey;
179 mp_int k; 204
205 /** The private key */
206 void *k;
180 } ecc_key; 207 } ecc_key;
181 208
182 int ecc_test(void); 209 /** the ECC params provided */
210 extern const ltc_ecc_set_type ltc_ecc_sets[];
211
212 int ecc_test(void);
183 void ecc_sizes(int *low, int *high); 213 void ecc_sizes(int *low, int *high);
184 int ecc_get_size(ecc_key *key); 214 int ecc_get_size(ecc_key *key);
185 215
186 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key); 216 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
217 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp);
187 void ecc_free(ecc_key *key); 218 void ecc_free(ecc_key *key);
188 219
189 int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key); 220 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); 221 int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
191 222 int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp);
192 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, 223
193 unsigned char *out, unsigned long *outlen); 224 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
194 225 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
195 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen, 226 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
196 unsigned char *out, unsigned long *outlen, 227
197 prng_state *prng, int wprng, int hash, 228 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
198 ecc_key *key); 229 unsigned char *out, unsigned long *outlen);
199 230
200 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, 231 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
201 unsigned char *out, unsigned long *outlen, 232 unsigned char *out, unsigned long *outlen,
202 ecc_key *key); 233 prng_state *prng, int wprng, int hash,
203 234 ecc_key *key);
204 int ecc_sign_hash(const unsigned char *in, unsigned long inlen, 235
205 unsigned char *out, unsigned long *outlen, 236 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
206 prng_state *prng, int wprng, ecc_key *key); 237 unsigned char *out, unsigned long *outlen,
207 238 ecc_key *key);
208 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, 239
209 const unsigned char *hash, unsigned long hashlen, 240 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
210 int *stat, ecc_key *key); 241 unsigned char *out, unsigned long *outlen,
242 prng_state *prng, int wprng, ecc_key *key);
243
244 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
245 const unsigned char *hash, unsigned long hashlen,
246 int *stat, ecc_key *key);
247
248 /* low level functions */
249 ecc_point *ltc_ecc_new_point(void);
250 void ltc_ecc_del_point(ecc_point *p);
251 int ltc_ecc_is_valid_idx(int n);
252
253 /* point ops (mp == montgomery digit) */
254 #if !defined(MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
255 /* R = 2P */
256 int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
257
258 /* R = P + Q */
259 int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
260 #endif
261
262 #if defined(MECC_FP)
263 int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
264 int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
265 int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
266 void ltc_ecc_fp_free(void);
267 #endif
268
269 /* R = kG */
270 int ltc_ecc_mulmod(void *k, ecc_point *G, ecc_point *R, void *modulus, int map);
271
272 #ifdef LTC_ECC_SHAMIR
273 /* kA*A + kB*B = C */
274 int ltc_ecc_mul2add(ecc_point *A, void *kA,
275 ecc_point *B, void *kB,
276 ecc_point *C,
277 void *modulus);
278
279 #ifdef MECC_FP
280 int ltc_ecc_fp_mul2add(ecc_point *A, void *kA,
281 ecc_point *B, void *kB,
282 ecc_point *C, void *modulus);
283 #endif
284
285 #endif
286
287
288 /* map P to affine from projective */
289 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
211 290
212 #endif 291 #endif
213 292
214 #ifdef MDSA 293 #ifdef MDSA
215 294
295 /* Max diff between group and modulus size in bytes */
296 #define MDSA_DELTA 512
297
298 /* Max DSA group size in bytes (default allows 4k-bit groups) */
299 #define MDSA_MAX_GROUP 512
300
301 /** DSA key structure */
216 typedef struct { 302 typedef struct {
217 int type, qord; 303 /** The key type, PK_PRIVATE or PK_PUBLIC */
218 mp_int g, q, p, x, y; 304 int type;
305
306 /** The order of the sub-group used in octets */
307 int qord;
308
309 /** The generator */
310 void *g;
311
312 /** The prime used to generate the sub-group */
313 void *q;
314
315 /** The large prime that generats the field the contains the sub-group */
316 void *p;
317
318 /** The private key */
319 void *x;
320
321 /** The public key */
322 void *y;
219 } dsa_key; 323 } dsa_key;
220 324
221 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); 325 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); 326 void dsa_free(dsa_key *key);
223 327
224
225 int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen, 328 int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
226 mp_int *r, mp_int *s, 329 void *r, void *s,
227 prng_state *prng, int wprng, dsa_key *key); 330 prng_state *prng, int wprng, dsa_key *key);
228 331
229 int dsa_sign_hash(const unsigned char *in, unsigned long inlen, 332 int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
230 unsigned char *out, unsigned long *outlen, 333 unsigned char *out, unsigned long *outlen,
231 prng_state *prng, int wprng, dsa_key *key); 334 prng_state *prng, int wprng, dsa_key *key);
232 335
233 int dsa_verify_hash_raw( mp_int *r, mp_int *s, 336 int dsa_verify_hash_raw( void *r, void *s,
234 const unsigned char *hash, unsigned long hashlen, 337 const unsigned char *hash, unsigned long hashlen,
235 int *stat, dsa_key *key); 338 int *stat, dsa_key *key);
236 339
237 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, 340 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
238 const unsigned char *hash, unsigned long hashlen, 341 const unsigned char *hash, unsigned long hashlen,
239 int *stat, dsa_key *key); 342 int *stat, dsa_key *key);
240 343
344 int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
345 unsigned char *out, unsigned long *outlen,
346 prng_state *prng, int wprng, int hash,
347 dsa_key *key);
348
349 int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
350 unsigned char *out, unsigned long *outlen,
351 dsa_key *key);
352
241 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); 353 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); 354 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); 355 int dsa_verify_key(dsa_key *key, int *stat);
246 356
357 int dsa_shared_secret(void *private_key, void *base,
358 dsa_key *public_key,
359 unsigned char *out, unsigned long *outlen);
247 #endif 360 #endif
248 361
249 #ifdef LTC_DER 362 #ifdef LTC_DER
250 /* DER handling */ 363 /* DER handling */
251 364
252 enum { 365 enum {
253 LTC_ASN1_EOL, 366 LTC_ASN1_EOL,
367 LTC_ASN1_BOOLEAN,
254 LTC_ASN1_INTEGER, 368 LTC_ASN1_INTEGER,
255 LTC_ASN1_SHORT_INTEGER, 369 LTC_ASN1_SHORT_INTEGER,
256 LTC_ASN1_BIT_STRING, 370 LTC_ASN1_BIT_STRING,
257 LTC_ASN1_OCTET_STRING, 371 LTC_ASN1_OCTET_STRING,
258 LTC_ASN1_NULL, 372 LTC_ASN1_NULL,
259 LTC_ASN1_OBJECT_IDENTIFIER, 373 LTC_ASN1_OBJECT_IDENTIFIER,
260 LTC_ASN1_IA5_STRING, 374 LTC_ASN1_IA5_STRING,
261 LTC_ASN1_PRINTABLE_STRING, 375 LTC_ASN1_PRINTABLE_STRING,
376 LTC_ASN1_UTF8_STRING,
262 LTC_ASN1_UTCTIME, 377 LTC_ASN1_UTCTIME,
263
264 LTC_ASN1_CHOICE, 378 LTC_ASN1_CHOICE,
265 LTC_ASN1_SEQUENCE 379 LTC_ASN1_SEQUENCE,
380 LTC_ASN1_SET,
381 LTC_ASN1_SETOF
266 }; 382 };
267 383
268 typedef struct { 384 /** A LTC ASN.1 list type */
385 typedef struct ltc_asn1_list_ {
386 /** The LTC ASN.1 enumerated type identifier */
269 int type; 387 int type;
388 /** The data to encode or place for decoding */
270 void *data; 389 void *data;
390 /** The size of the input or resulting output */
271 unsigned long size; 391 unsigned long size;
392 /** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
272 int used; 393 int used;
394 /** prev/next entry in the list */
395 struct ltc_asn1_list_ *prev, *next, *child, *parent;
273 } ltc_asn1_list; 396 } ltc_asn1_list;
274 397
275 #define LTC_SET_ASN1(list, index, Type, Data, Size) \ 398 #define LTC_SET_ASN1(list, index, Type, Data, Size) \
276 do { \ 399 do { \
277 int LTC_MACRO_temp = (index); \ 400 int LTC_MACRO_temp = (index); \
278 ltc_asn1_list *LTC_MACRO_list = (list); \ 401 ltc_asn1_list *LTC_MACRO_list = (list); \
279 LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \ 402 LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
280 LTC_MACRO_list[LTC_MACRO_temp].data = (Data); \ 403 LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
281 LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \ 404 LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
282 LTC_MACRO_list[LTC_MACRO_temp].used = 0; \ 405 LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
283 } while (0); 406 } while (0);
284 407
285 /* SEQUENCE */ 408 /* SEQUENCE */
286 int der_encode_sequence(ltc_asn1_list *list, unsigned long inlen, 409 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
287 unsigned char *out, unsigned long *outlen); 410 unsigned char *out, unsigned long *outlen, int type_of);
288 411
289 int der_decode_sequence(const unsigned char *in, unsigned long inlen, 412 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
290 ltc_asn1_list *list, unsigned long outlen); 413
414 int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
415 ltc_asn1_list *list, unsigned long outlen, int ordered);
416
417 #define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
291 418
292 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen, 419 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
293 unsigned long *outlen); 420 unsigned long *outlen);
294 421
295 /* VA list handy helpers */ 422 /* SET */
423 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
424 #define der_length_set der_length_sequence
425 int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
426 unsigned char *out, unsigned long *outlen);
427
428 int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
429 unsigned char *out, unsigned long *outlen);
430
431 /* VA list handy helpers with triplets of <type, size, data> */
296 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...); 432 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
297 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...); 433 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
298 434
435 /* FLEXI DECODER handle unknown list decoder */
436 int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
437 void der_free_sequence_flexi(ltc_asn1_list *list);
438 void der_sequence_free(ltc_asn1_list *in);
439
440 /* BOOLEAN */
441 int der_length_boolean(unsigned long *outlen);
442 int der_encode_boolean(int in,
443 unsigned char *out, unsigned long *outlen);
444 int der_decode_boolean(const unsigned char *in, unsigned long inlen,
445 int *out);
299 /* INTEGER */ 446 /* INTEGER */
300 int der_encode_integer(mp_int *num, unsigned char *out, unsigned long *outlen); 447 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
301 int der_decode_integer(const unsigned char *in, unsigned long inlen, mp_int *num); 448 int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
302 int der_length_integer(mp_int *num, unsigned long *len); 449 int der_length_integer(void *num, unsigned long *len);
303 450
304 /* INTEGER -- handy for 0..2^32-1 values */ 451 /* INTEGER -- handy for 0..2^32-1 values */
305 int der_decode_short_integer(const unsigned char *in, unsigned long inlen, unsigned long *num); 452 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); 453 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); 454 int der_length_short_integer(unsigned long num, unsigned long *outlen);
345 unsigned char *out, unsigned long *outlen); 492 unsigned char *out, unsigned long *outlen);
346 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen); 493 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
347 494
348 int der_printable_char_encode(int c); 495 int der_printable_char_encode(int c);
349 int der_printable_value_decode(int v); 496 int der_printable_value_decode(int v);
497
498 /* UTF-8 */
499 #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED)) && !defined(LTC_NO_WCHAR)
500 #include <wchar.h>
501 #else
502 typedef ulong32 wchar_t;
503 #endif
504
505 int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
506 unsigned char *out, unsigned long *outlen);
507
508 int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
509 wchar_t *out, unsigned long *outlen);
510 unsigned long der_utf8_charsize(const wchar_t c);
511 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
512
350 513
351 /* CHOICE */ 514 /* CHOICE */
352 int der_decode_choice(const unsigned char *in, unsigned long *inlen, 515 int der_decode_choice(const unsigned char *in, unsigned long *inlen,
353 ltc_asn1_list *list, unsigned long outlen); 516 ltc_asn1_list *list, unsigned long outlen);
354 517
375 538
376 539
377 #endif 540 #endif
378 541
379 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_pk.h,v $ */ 542 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_pk.h,v $ */
380 /* $Revision: 1.30 $ */ 543 /* $Revision: 1.77 $ */
381 /* $Date: 2005/06/19 11:23:03 $ */ 544 /* $Date: 2006/12/03 00:39:56 $ */