comparison libtomcrypt/src/headers/tomcrypt_pk.h @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children e9dba7abd939
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9
1 /* ---- NUMBER THEORY ---- */ 10 /* ---- NUMBER THEORY ---- */
2 11
3 enum { 12 enum {
4 PK_PUBLIC=0, 13 PK_PUBLIC=0,
5 PK_PRIVATE=1 14 PK_PRIVATE=1
6 }; 15 };
7 16
17 /* Indicates standard output formats that can be read e.g. by OpenSSL or GnuTLS */
18 #define PK_STD 0x1000
19
8 int rand_prime(void *N, long len, prng_state *prng, int wprng); 20 int rand_prime(void *N, long len, prng_state *prng, int wprng);
21
22 #ifdef LTC_SOURCE
23 /* internal helper functions */
24 int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
25 int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
26
27 enum public_key_algorithms {
28 PKA_RSA,
29 PKA_DSA
30 };
31
32 typedef struct Oid {
33 unsigned long OID[16];
34 /** Number of OID digits in use */
35 unsigned long OIDlen;
36 } oid_st;
37
38 int pk_get_oid(int pk, oid_st *st);
39 #endif /* LTC_SOURCE */
9 40
10 /* ---- RSA ---- */ 41 /* ---- RSA ---- */
11 #ifdef LTC_MRSA 42 #ifdef LTC_MRSA
12 43
13 /* Min and Max RSA key sizes (in bits) */ 44 /** RSA PKCS style key */
14 #define MIN_RSA_SIZE 1024
15 #define MAX_RSA_SIZE 4096
16
17 /** RSA LTC_PKCS style key */
18 typedef struct Rsa_key { 45 typedef struct Rsa_key {
19 /** Type of key, PK_PRIVATE or PK_PUBLIC */ 46 /** Type of key, PK_PRIVATE or PK_PUBLIC */
20 int type; 47 int type;
21 /** The public exponent */ 48 /** The public exponent */
22 void *e; 49 void *e;
23 /** The private exponent */ 50 /** The private exponent */
24 void *d; 51 void *d;
25 /** The modulus */ 52 /** The modulus */
26 void *N; 53 void *N;
27 /** The p factor of N */ 54 /** The p factor of N */
28 void *p; 55 void *p;
29 /** The q factor of N */ 56 /** The q factor of N */
30 void *q; 57 void *q;
31 /** The 1/q mod p CRT param */ 58 /** The 1/q mod p CRT param */
32 void *qP; 59 void *qP;
33 /** The d mod (p - 1) CRT param */ 60 /** The d mod (p - 1) CRT param */
34 void *dP; 61 void *dP;
35 /** The d mod (q - 1) CRT param */ 62 /** The d mod (q - 1) CRT param */
36 void *dQ; 63 void *dQ;
37 } rsa_key; 64 } rsa_key;
38 65
39 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key); 66 int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key);
40 67
68 int rsa_get_size(rsa_key *key);
69
41 int rsa_exptmod(const unsigned char *in, unsigned long inlen, 70 int rsa_exptmod(const unsigned char *in, unsigned long inlen,
42 unsigned char *out, unsigned long *outlen, int which, 71 unsigned char *out, unsigned long *outlen, int which,
43 rsa_key *key); 72 rsa_key *key);
44 73
45 void rsa_free(rsa_key *key); 74 void rsa_free(rsa_key *key);
46 75
47 /* These use LTC_PKCS #1 v2.0 padding */ 76 /* These use PKCS #1 v2.0 padding */
48 #define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \ 77 #define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, _key) \
49 rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_LTC_PKCS_1_OAEP, _key) 78 rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _prng, _prng_idx, _hash_idx, LTC_PKCS_1_OAEP, _key)
50 79
51 #define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \ 80 #define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \
52 rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_LTC_PKCS_1_OAEP, _stat, _key) 81 rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_PKCS_1_OAEP, _stat, _key)
53 82
54 #define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \ 83 #define rsa_sign_hash(_in, _inlen, _out, _outlen, _prng, _prng_idx, _hash_idx, _saltlen, _key) \
55 rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key) 84 rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_PKCS_1_PSS, _prng, _prng_idx, _hash_idx, _saltlen, _key)
56 85
57 #define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \ 86 #define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \
58 rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key) 87 rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key)
59 88
60 /* These can be switched between LTC_PKCS #1 v2.x and LTC_PKCS #1 v1.5 paddings */ 89 #define rsa_sign_saltlen_get_max(_hash_idx, _key) \
90 rsa_sign_saltlen_get_max_ex(LTC_PKCS_1_PSS, _hash_idx, _key)
91
92 /* These can be switched between PKCS #1 v2.x and PKCS #1 v1.5 paddings */
61 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, 93 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
62 unsigned char *out, unsigned long *outlen, 94 unsigned char *out, unsigned long *outlen,
63 const unsigned char *lparam, unsigned long lparamlen, 95 const unsigned char *lparam, unsigned long lparamlen,
64 prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key); 96 prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key);
65 97
80 const unsigned char *hash, unsigned long hashlen, 112 const unsigned char *hash, unsigned long hashlen,
81 int padding, 113 int padding,
82 int hash_idx, unsigned long saltlen, 114 int hash_idx, unsigned long saltlen,
83 int *stat, rsa_key *key); 115 int *stat, rsa_key *key);
84 116
85 /* LTC_PKCS #1 import/export */ 117 int rsa_sign_saltlen_get_max_ex(int padding, int hash_idx, rsa_key *key);
118
119 /* PKCS #1 import/export */
86 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key); 120 int rsa_export(unsigned char *out, unsigned long *outlen, int type, rsa_key *key);
87 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key); 121 int rsa_import(const unsigned char *in, unsigned long inlen, rsa_key *key);
88 122
123 int rsa_import_x509(const unsigned char *in, unsigned long inlen, rsa_key *key);
124 int rsa_import_pkcs8(const unsigned char *in, unsigned long inlen,
125 const void *passwd, unsigned long passwdlen, rsa_key *key);
126
127 int rsa_set_key(const unsigned char *N, unsigned long Nlen,
128 const unsigned char *e, unsigned long elen,
129 const unsigned char *d, unsigned long dlen,
130 rsa_key *key);
131 int rsa_set_factors(const unsigned char *p, unsigned long plen,
132 const unsigned char *q, unsigned long qlen,
133 rsa_key *key);
134 int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen,
135 const unsigned char *dQ, unsigned long dQlen,
136 const unsigned char *qP, unsigned long qPlen,
137 rsa_key *key);
89 #endif 138 #endif
90 139
91 /* ---- Katja ---- */ 140 /* ---- Katja ---- */
92 #ifdef MKAT 141 #ifdef LTC_MKAT
93 142
94 /* Min and Max KAT key sizes (in bits) */ 143 /* Min and Max KAT key sizes (in bits) */
95 #define MIN_KAT_SIZE 1024 144 #define MIN_KAT_SIZE 1024
96 #define MAX_KAT_SIZE 4096 145 #define MAX_KAT_SIZE 4096
97 146
98 /** Katja LTC_PKCS style key */ 147 /** Katja PKCS style key */
99 typedef struct KAT_key { 148 typedef struct KAT_key {
100 /** Type of key, PK_PRIVATE or PK_PUBLIC */ 149 /** Type of key, PK_PRIVATE or PK_PUBLIC */
101 int type; 150 int type;
102 /** The private exponent */ 151 /** The private exponent */
103 void *d; 152 void *d;
104 /** The modulus */ 153 /** The modulus */
105 void *N; 154 void *N;
106 /** The p factor of N */ 155 /** The p factor of N */
107 void *p; 156 void *p;
108 /** The q factor of N */ 157 /** The q factor of N */
109 void *q; 158 void *q;
110 /** The 1/q mod p CRT param */ 159 /** The 1/q mod p CRT param */
111 void *qP; 160 void *qP;
112 /** The d mod (p - 1) CRT param */ 161 /** The d mod (p - 1) CRT param */
113 void *dP; 162 void *dP;
114 /** The d mod (q - 1) CRT param */ 163 /** The d mod (q - 1) CRT param */
115 void *dQ; 164 void *dQ;
116 /** The pq param */ 165 /** The pq param */
117 void *pq; 166 void *pq;
118 } katja_key; 167 } katja_key;
123 unsigned char *out, unsigned long *outlen, int which, 172 unsigned char *out, unsigned long *outlen, int which,
124 katja_key *key); 173 katja_key *key);
125 174
126 void katja_free(katja_key *key); 175 void katja_free(katja_key *key);
127 176
128 /* These use LTC_PKCS #1 v2.0 padding */ 177 /* These use PKCS #1 v2.0 padding */
129 int katja_encrypt_key(const unsigned char *in, unsigned long inlen, 178 int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
130 unsigned char *out, unsigned long *outlen, 179 unsigned char *out, unsigned long *outlen,
131 const unsigned char *lparam, unsigned long lparamlen, 180 const unsigned char *lparam, unsigned long lparamlen,
132 prng_state *prng, int prng_idx, int hash_idx, katja_key *key); 181 prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
133 182
134 int katja_decrypt_key(const unsigned char *in, unsigned long inlen, 183 int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
135 unsigned char *out, unsigned long *outlen, 184 unsigned char *out, unsigned long *outlen,
136 const unsigned char *lparam, unsigned long lparamlen, 185 const unsigned char *lparam, unsigned long lparamlen,
137 int hash_idx, int *stat, 186 int hash_idx, int *stat,
138 katja_key *key); 187 katja_key *key);
139 188
140 /* LTC_PKCS #1 import/export */ 189 /* PKCS #1 import/export */
141 int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key); 190 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); 191 int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
143 192
144 #endif 193 #endif
194
195 /* ---- DH Routines ---- */
196 #ifdef LTC_MDH
197
198 typedef struct {
199 int type;
200 void *x;
201 void *y;
202 void *base;
203 void *prime;
204 } dh_key;
205
206 int dh_get_groupsize(dh_key *key);
207
208 int dh_export(unsigned char *out, unsigned long *outlen, int type, dh_key *key);
209 int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key);
210
211 int dh_set_pg(const unsigned char *p, unsigned long plen,
212 const unsigned char *g, unsigned long glen,
213 dh_key *key);
214 int dh_set_pg_dhparam(const unsigned char *dhparam, unsigned long dhparamlen, dh_key *key);
215 int dh_set_pg_groupsize(int groupsize, dh_key *key);
216
217 int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *key);
218 int dh_generate_key(prng_state *prng, int wprng, dh_key *key);
219
220 int dh_shared_secret(dh_key *private_key, dh_key *public_key,
221 unsigned char *out, unsigned long *outlen);
222
223 void dh_free(dh_key *key);
224
225 int dh_export_key(void *out, unsigned long *outlen, int type, dh_key *key);
226
227 #ifdef LTC_SOURCE
228 typedef struct {
229 int size;
230 const char *name, *base, *prime;
231 } ltc_dh_set_type;
232
233 extern const ltc_dh_set_type ltc_dh_sets[];
234
235 /* internal helper functions */
236 int dh_check_pubkey(dh_key *key);
237 #endif
238
239 #endif /* LTC_MDH */
240
145 241
146 /* ---- ECC Routines ---- */ 242 /* ---- ECC Routines ---- */
147 #ifdef LTC_MECC 243 #ifdef LTC_MECC
148 244
149 /* size of our temp buffers for exported keys */ 245 /* size of our temp buffers for exported keys */
156 typedef struct { 252 typedef struct {
157 /** The size of the curve in octets */ 253 /** The size of the curve in octets */
158 int size; 254 int size;
159 255
160 /** name of curve */ 256 /** name of curve */
161 char *name; 257 const char *name;
162 258
163 /** The prime that defines the field the curve is in (encoded in hex) */ 259 /** The prime that defines the field the curve is in (encoded in hex) */
164 char *prime; 260 const char *prime;
165 261
166 /** The fields B param (hex) */ 262 /** The fields B param (hex) */
167 char *B; 263 const char *B;
168 264
169 /** The order of the curve (hex) */ 265 /** The order of the curve (hex) */
170 char *order; 266 const char *order;
171 267
172 /** The x co-ordinate of the base point on the curve (hex) */ 268 /** The x co-ordinate of the base point on the curve (hex) */
173 char *Gx; 269 const char *Gx;
174 270
175 /** The y co-ordinate of the base point on the curve (hex) */ 271 /** The y co-ordinate of the base point on the curve (hex) */
176 char *Gy; 272 const char *Gy;
177 } ltc_ecc_set_type; 273 } ltc_ecc_set_type;
178 274
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 */ 275 /** 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 { 276 typedef struct {
181 /** The x co-ordinate */ 277 /** The x co-ordinate */
194 int type; 290 int type;
195 291
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 */ 292 /** 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; 293 int idx;
198 294
199 /** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */ 295 /** pointer to domain parameters; either points to NIST curves (identified by idx >= 0) or user supplied curve */
200 const ltc_ecc_set_type *dp; 296 const ltc_ecc_set_type *dp;
201 297
202 /** The public key */ 298 /** The public key */
203 ecc_point pubkey; 299 ecc_point pubkey;
204 300
205 /** The private key */ 301 /** The private key */
223 319
224 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen); 320 int ecc_ansi_x963_export(ecc_key *key, unsigned char *out, unsigned long *outlen);
225 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key); 321 int ecc_ansi_x963_import(const unsigned char *in, unsigned long inlen, ecc_key *key);
226 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp); 322 int ecc_ansi_x963_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, ltc_ecc_set_type *dp);
227 323
228 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key, 324 int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,
229 unsigned char *out, unsigned long *outlen); 325 unsigned char *out, unsigned long *outlen);
230 326
231 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen, 327 int ecc_encrypt_key(const unsigned char *in, unsigned long inlen,
232 unsigned char *out, unsigned long *outlen, 328 unsigned char *out, unsigned long *outlen,
233 prng_state *prng, int wprng, int hash, 329 prng_state *prng, int wprng, int hash,
234 ecc_key *key); 330 ecc_key *key);
235 331
236 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen, 332 int ecc_decrypt_key(const unsigned char *in, unsigned long inlen,
237 unsigned char *out, unsigned long *outlen, 333 unsigned char *out, unsigned long *outlen,
238 ecc_key *key); 334 ecc_key *key);
239 335
240 int ecc_sign_hash(const unsigned char *in, unsigned long inlen, 336 int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,
241 unsigned char *out, unsigned long *outlen, 337 unsigned char *out, unsigned long *outlen,
338 prng_state *prng, int wprng, ecc_key *key);
339
340 int ecc_sign_hash(const unsigned char *in, unsigned long inlen,
341 unsigned char *out, unsigned long *outlen,
242 prng_state *prng, int wprng, ecc_key *key); 342 prng_state *prng, int wprng, ecc_key *key);
243 343
344 int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,
345 const unsigned char *hash, unsigned long hashlen,
346 int *stat, ecc_key *key);
347
244 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, 348 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
245 const unsigned char *hash, unsigned long hashlen, 349 const unsigned char *hash, unsigned long hashlen,
246 int *stat, ecc_key *key); 350 int *stat, ecc_key *key);
247 351
248 /* low level functions */ 352 /* low level functions */
249 ecc_point *ltc_ecc_new_point(void); 353 ecc_point *ltc_ecc_new_point(void);
250 void ltc_ecc_del_point(ecc_point *p); 354 void ltc_ecc_del_point(ecc_point *p);
251 int ltc_ecc_is_valid_idx(int n); 355 int ltc_ecc_is_valid_idx(int n);
252 356
253 /* point ops (mp == montgomery digit) */ 357 /* point ops (mp == montgomery digit) */
254 #if !defined(LTC_MECC_ACCEL) || defined(LTM_LTC_DESC) || defined(GMP_LTC_DESC) 358 #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
255 /* R = 2P */ 359 /* R = 2P */
256 int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp); 360 int ltc_ecc_projective_dbl_point(ecc_point *P, ecc_point *R, void *modulus, void *mp);
257 361
258 /* R = P + Q */ 362 /* R = P + Q */
259 int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp); 363 int ltc_ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R, void *modulus, void *mp);
307 #define LTC_MDSA_MAX_GROUP 512 411 #define LTC_MDSA_MAX_GROUP 512
308 412
309 /** DSA key structure */ 413 /** DSA key structure */
310 typedef struct { 414 typedef struct {
311 /** The key type, PK_PRIVATE or PK_PUBLIC */ 415 /** The key type, PK_PRIVATE or PK_PUBLIC */
312 int type; 416 int type;
313 417
314 /** The order of the sub-group used in octets */ 418 /** The order of the sub-group used in octets */
315 int qord; 419 int qord;
316 420
317 /** The generator */ 421 /** The generator */
329 /** The public key */ 433 /** The public key */
330 void *y; 434 void *y;
331 } dsa_key; 435 } dsa_key;
332 436
333 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key); 437 int dsa_make_key(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
438
439 int dsa_set_pqg(const unsigned char *p, unsigned long plen,
440 const unsigned char *q, unsigned long qlen,
441 const unsigned char *g, unsigned long glen,
442 dsa_key *key);
443 int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamlen, dsa_key *key);
444 int dsa_generate_pqg(prng_state *prng, int wprng, int group_size, int modulus_size, dsa_key *key);
445
446 int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key *key);
447 int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key);
448
334 void dsa_free(dsa_key *key); 449 void dsa_free(dsa_key *key);
335 450
336 int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen, 451 int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
337 void *r, void *s, 452 void *r, void *s,
338 prng_state *prng, int wprng, dsa_key *key); 453 prng_state *prng, int wprng, dsa_key *key);
340 int dsa_sign_hash(const unsigned char *in, unsigned long inlen, 455 int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
341 unsigned char *out, unsigned long *outlen, 456 unsigned char *out, unsigned long *outlen,
342 prng_state *prng, int wprng, dsa_key *key); 457 prng_state *prng, int wprng, dsa_key *key);
343 458
344 int dsa_verify_hash_raw( void *r, void *s, 459 int dsa_verify_hash_raw( void *r, void *s,
345 const unsigned char *hash, unsigned long hashlen, 460 const unsigned char *hash, unsigned long hashlen,
346 int *stat, dsa_key *key); 461 int *stat, dsa_key *key);
347 462
348 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, 463 int dsa_verify_hash(const unsigned char *sig, unsigned long siglen,
349 const unsigned char *hash, unsigned long hashlen, 464 const unsigned char *hash, unsigned long hashlen,
350 int *stat, dsa_key *key); 465 int *stat, dsa_key *key);
351 466
352 int dsa_encrypt_key(const unsigned char *in, unsigned long inlen, 467 int dsa_encrypt_key(const unsigned char *in, unsigned long inlen,
353 unsigned char *out, unsigned long *outlen, 468 unsigned char *out, unsigned long *outlen,
354 prng_state *prng, int wprng, int hash, 469 prng_state *prng, int wprng, int hash,
355 dsa_key *key); 470 dsa_key *key);
356 471
357 int dsa_decrypt_key(const unsigned char *in, unsigned long inlen, 472 int dsa_decrypt_key(const unsigned char *in, unsigned long inlen,
358 unsigned char *out, unsigned long *outlen, 473 unsigned char *out, unsigned long *outlen,
359 dsa_key *key); 474 dsa_key *key);
360 475
361 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key); 476 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
362 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key); 477 int dsa_export(unsigned char *out, unsigned long *outlen, int type, dsa_key *key);
363 int dsa_verify_key(dsa_key *key, int *stat); 478 int dsa_verify_key(dsa_key *key, int *stat);
364 479 #ifdef LTC_SOURCE
480 /* internal helper functions */
481 int dsa_int_validate_xy(dsa_key *key, int *stat);
482 int dsa_int_validate_pqg(dsa_key *key, int *stat);
483 int dsa_int_validate_primes(dsa_key *key, int *stat);
484 #endif
365 int dsa_shared_secret(void *private_key, void *base, 485 int dsa_shared_secret(void *private_key, void *base,
366 dsa_key *public_key, 486 dsa_key *public_key,
367 unsigned char *out, unsigned long *outlen); 487 unsigned char *out, unsigned long *outlen);
368 #endif 488 #endif
369 489
370 #ifdef LTC_DER 490 #ifdef LTC_DER
371 /* DER handling */ 491 /* DER handling */
372 492
373 enum { 493 typedef enum ltc_asn1_type_ {
494 /* 0 */
374 LTC_ASN1_EOL, 495 LTC_ASN1_EOL,
375 LTC_ASN1_BOOLEAN, 496 LTC_ASN1_BOOLEAN,
376 LTC_ASN1_INTEGER, 497 LTC_ASN1_INTEGER,
377 LTC_ASN1_SHORT_INTEGER, 498 LTC_ASN1_SHORT_INTEGER,
378 LTC_ASN1_BIT_STRING, 499 LTC_ASN1_BIT_STRING,
500 /* 5 */
379 LTC_ASN1_OCTET_STRING, 501 LTC_ASN1_OCTET_STRING,
380 LTC_ASN1_NULL, 502 LTC_ASN1_NULL,
381 LTC_ASN1_OBJECT_IDENTIFIER, 503 LTC_ASN1_OBJECT_IDENTIFIER,
382 LTC_ASN1_IA5_STRING, 504 LTC_ASN1_IA5_STRING,
383 LTC_ASN1_PRINTABLE_STRING, 505 LTC_ASN1_PRINTABLE_STRING,
506 /* 10 */
384 LTC_ASN1_UTF8_STRING, 507 LTC_ASN1_UTF8_STRING,
385 LTC_ASN1_UTCTIME, 508 LTC_ASN1_UTCTIME,
386 LTC_ASN1_CHOICE, 509 LTC_ASN1_CHOICE,
387 LTC_ASN1_SEQUENCE, 510 LTC_ASN1_SEQUENCE,
388 LTC_ASN1_SET, 511 LTC_ASN1_SET,
389 LTC_ASN1_SETOF 512 /* 15 */
390 }; 513 LTC_ASN1_SETOF,
514 LTC_ASN1_RAW_BIT_STRING,
515 LTC_ASN1_TELETEX_STRING,
516 LTC_ASN1_CONSTRUCTED,
517 LTC_ASN1_CONTEXT_SPECIFIC,
518 /* 20 */
519 LTC_ASN1_GENERALIZEDTIME,
520 } ltc_asn1_type;
391 521
392 /** A LTC ASN.1 list type */ 522 /** A LTC ASN.1 list type */
393 typedef struct ltc_asn1_list_ { 523 typedef struct ltc_asn1_list_ {
394 /** The LTC ASN.1 enumerated type identifier */ 524 /** The LTC ASN.1 enumerated type identifier */
395 int type; 525 ltc_asn1_type type;
396 /** The data to encode or place for decoding */ 526 /** The data to encode or place for decoding */
397 void *data; 527 void *data;
398 /** The size of the input or resulting output */ 528 /** The size of the input or resulting output */
399 unsigned long size; 529 unsigned long size;
400 /** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */ 530 /** The used flag, this is used by the CHOICE ASN.1 type to indicate which choice was made */
409 ltc_asn1_list *LTC_MACRO_list = (list); \ 539 ltc_asn1_list *LTC_MACRO_list = (list); \
410 LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \ 540 LTC_MACRO_list[LTC_MACRO_temp].type = (Type); \
411 LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \ 541 LTC_MACRO_list[LTC_MACRO_temp].data = (void*)(Data); \
412 LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \ 542 LTC_MACRO_list[LTC_MACRO_temp].size = (Size); \
413 LTC_MACRO_list[LTC_MACRO_temp].used = 0; \ 543 LTC_MACRO_list[LTC_MACRO_temp].used = 0; \
414 } while (0); 544 } while (0)
415 545
416 /* SEQUENCE */ 546 /* SEQUENCE */
417 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen, 547 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
418 unsigned char *out, unsigned long *outlen, int type_of); 548 unsigned char *out, unsigned long *outlen, int type_of);
419 549
420 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE) 550 #define der_encode_sequence(list, inlen, out, outlen) der_encode_sequence_ex(list, inlen, out, outlen, LTC_ASN1_SEQUENCE)
421 551
422 int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen, 552 int der_decode_sequence_ex(const unsigned char *in, unsigned long inlen,
423 ltc_asn1_list *list, unsigned long outlen, int ordered); 553 ltc_asn1_list *list, unsigned long outlen, int ordered);
424 554
425 #define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1) 555 #define der_decode_sequence(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 1)
426 556
427 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen, 557 int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
428 unsigned long *outlen); 558 unsigned long *outlen);
559
560
561 #ifdef LTC_SOURCE
562 /* internal helper functions */
563 int der_length_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
564 unsigned long *outlen, unsigned long *payloadlen);
565 /* SUBJECT PUBLIC KEY INFO */
566 int der_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
567 unsigned int algorithm, void* public_key, unsigned long public_key_len,
568 unsigned long parameters_type, void* parameters, unsigned long parameters_len);
569
570 int der_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
571 unsigned int algorithm, void* public_key, unsigned long* public_key_len,
572 unsigned long parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
573 #endif /* LTC_SOURCE */
429 574
430 /* SET */ 575 /* SET */
431 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0) 576 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, 0)
432 #define der_length_set der_length_sequence 577 #define der_length_set der_length_sequence
433 int der_encode_set(ltc_asn1_list *list, unsigned long inlen, 578 int der_encode_set(ltc_asn1_list *list, unsigned long inlen,
434 unsigned char *out, unsigned long *outlen); 579 unsigned char *out, unsigned long *outlen);
435 580
436 int der_encode_setof(ltc_asn1_list *list, unsigned long inlen, 581 int der_encode_setof(ltc_asn1_list *list, unsigned long inlen,
437 unsigned char *out, unsigned long *outlen); 582 unsigned char *out, unsigned long *outlen);
438 583
439 /* VA list handy helpers with triplets of <type, size, data> */ 584 /* VA list handy helpers with triplets of <type, size, data> */
440 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...); 585 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
441 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...); 586 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
442 587
443 /* FLEXI DECODER handle unknown list decoder */ 588 /* FLEXI DECODER handle unknown list decoder */
444 int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out); 589 int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
445 void der_free_sequence_flexi(ltc_asn1_list *list); 590 #define der_free_sequence_flexi der_sequence_free
446 void der_sequence_free(ltc_asn1_list *in); 591 void der_sequence_free(ltc_asn1_list *in);
592 void der_sequence_shrink(ltc_asn1_list *in);
447 593
448 /* BOOLEAN */ 594 /* BOOLEAN */
449 int der_length_boolean(unsigned long *outlen); 595 int der_length_boolean(unsigned long *outlen);
450 int der_encode_boolean(int in, 596 int der_encode_boolean(int in,
451 unsigned char *out, unsigned long *outlen); 597 unsigned char *out, unsigned long *outlen);
452 int der_decode_boolean(const unsigned char *in, unsigned long inlen, 598 int der_decode_boolean(const unsigned char *in, unsigned long inlen,
453 int *out); 599 int *out);
454 /* INTEGER */ 600 /* INTEGER */
455 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen); 601 int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen);
456 int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num); 602 int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num);
457 int der_length_integer(void *num, unsigned long *len); 603 int der_length_integer(void *num, unsigned long *len);
458 604
463 609
464 /* BIT STRING */ 610 /* BIT STRING */
465 int der_encode_bit_string(const unsigned char *in, unsigned long inlen, 611 int der_encode_bit_string(const unsigned char *in, unsigned long inlen,
466 unsigned char *out, unsigned long *outlen); 612 unsigned char *out, unsigned long *outlen);
467 int der_decode_bit_string(const unsigned char *in, unsigned long inlen, 613 int der_decode_bit_string(const unsigned char *in, unsigned long inlen,
614 unsigned char *out, unsigned long *outlen);
615 int der_encode_raw_bit_string(const unsigned char *in, unsigned long inlen,
616 unsigned char *out, unsigned long *outlen);
617 int der_decode_raw_bit_string(const unsigned char *in, unsigned long inlen,
468 unsigned char *out, unsigned long *outlen); 618 unsigned char *out, unsigned long *outlen);
469 int der_length_bit_string(unsigned long nbits, unsigned long *outlen); 619 int der_length_bit_string(unsigned long nbits, unsigned long *outlen);
470 620
471 /* OCTET STRING */ 621 /* OCTET STRING */
472 int der_encode_octet_string(const unsigned char *in, unsigned long inlen, 622 int der_encode_octet_string(const unsigned char *in, unsigned long inlen,
491 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen); 641 int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
492 642
493 int der_ia5_char_encode(int c); 643 int der_ia5_char_encode(int c);
494 int der_ia5_value_decode(int v); 644 int der_ia5_value_decode(int v);
495 645
496 /* Printable STRING */ 646 /* TELETEX STRING */
647 int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
648 unsigned char *out, unsigned long *outlen);
649 int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
650
651 #ifdef LTC_SOURCE
652 /* internal helper functions */
653 int der_teletex_char_encode(int c);
654 int der_teletex_value_decode(int v);
655 #endif /* LTC_SOURCE */
656
657
658 /* PRINTABLE STRING */
497 int der_encode_printable_string(const unsigned char *in, unsigned long inlen, 659 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
498 unsigned char *out, unsigned long *outlen); 660 unsigned char *out, unsigned long *outlen);
499 int der_decode_printable_string(const unsigned char *in, unsigned long inlen, 661 int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
500 unsigned char *out, unsigned long *outlen); 662 unsigned char *out, unsigned long *outlen);
501 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen); 663 int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
502 664
503 int der_printable_char_encode(int c); 665 int der_printable_char_encode(int c);
504 int der_printable_value_decode(int v); 666 int der_printable_value_decode(int v);
505 667
506 /* UTF-8 */ 668 /* UTF-8 */
507 #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR) 669 #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
508 #include <wchar.h> 670 #include <wchar.h>
671 #if defined(__WCHAR_MAX__)
672 #define LTC_WCHAR_MAX __WCHAR_MAX__
673 #elif defined(WCHAR_MAX)
674 #define LTC_WCHAR_MAX WCHAR_MAX
675 #endif
676 /* please note that it might happen that LTC_WCHAR_MAX is undefined */
509 #else 677 #else
510 typedef ulong32 wchar_t; 678 typedef ulong32 wchar_t;
679 #define LTC_WCHAR_MAX 0xFFFFFFFF
511 #endif 680 #endif
512 681
513 int der_encode_utf8_string(const wchar_t *in, unsigned long inlen, 682 int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
514 unsigned char *out, unsigned long *outlen); 683 unsigned char *out, unsigned long *outlen);
515 684
516 int der_decode_utf8_string(const unsigned char *in, unsigned long inlen, 685 int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
517 wchar_t *out, unsigned long *outlen); 686 wchar_t *out, unsigned long *outlen);
518 unsigned long der_utf8_charsize(const wchar_t c); 687 unsigned long der_utf8_charsize(const wchar_t c);
688 #ifdef LTC_SOURCE
689 /* internal helper functions */
690 int der_utf8_valid_char(const wchar_t c);
691 #endif /* LTC_SOURCE */
519 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen); 692 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
520 693
521 694
522 /* CHOICE */ 695 /* CHOICE */
523 int der_decode_choice(const unsigned char *in, unsigned long *inlen, 696 int der_decode_choice(const unsigned char *in, unsigned long *inlen,
534 off_dir, /* timezone offset direction 0 == +, 1 == - */ 707 off_dir, /* timezone offset direction 0 == +, 1 == - */
535 off_hh, /* timezone offset hours */ 708 off_hh, /* timezone offset hours */
536 off_mm; /* timezone offset minutes */ 709 off_mm; /* timezone offset minutes */
537 } ltc_utctime; 710 } ltc_utctime;
538 711
539 int der_encode_utctime(ltc_utctime *utctime, 712 int der_encode_utctime(ltc_utctime *utctime,
540 unsigned char *out, unsigned long *outlen); 713 unsigned char *out, unsigned long *outlen);
541 714
542 int der_decode_utctime(const unsigned char *in, unsigned long *inlen, 715 int der_decode_utctime(const unsigned char *in, unsigned long *inlen,
543 ltc_utctime *out); 716 ltc_utctime *out);
544 717
545 int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen); 718 int der_length_utctime(ltc_utctime *utctime, unsigned long *outlen);
546 719
547 720 /* GeneralizedTime */
548 #endif 721 typedef struct {
549 722 unsigned YYYY, /* year */
550 /* $Source$ */ 723 MM, /* month */
551 /* $Revision$ */ 724 DD, /* day */
552 /* $Date$ */ 725 hh, /* hour */
726 mm, /* minute */
727 ss, /* second */
728 fs, /* fractional seconds */
729 off_dir, /* timezone offset direction 0 == +, 1 == - */
730 off_hh, /* timezone offset hours */
731 off_mm; /* timezone offset minutes */
732 } ltc_generalizedtime;
733
734 int der_encode_generalizedtime(ltc_generalizedtime *gtime,
735 unsigned char *out, unsigned long *outlen);
736
737 int der_decode_generalizedtime(const unsigned char *in, unsigned long *inlen,
738 ltc_generalizedtime *out);
739
740 int der_length_generalizedtime(ltc_generalizedtime *gtime, unsigned long *outlen);
741
742
743 #endif
744
745 /* ref: $Format:%D$ */
746 /* git commit: $Format:%H$ */
747 /* commit time: $Format:%ai$ */