]> Dogcows Code - chaz/p5-File-KDBX-XS/blob - libtomcrypt/src/headers/tomcrypt_private.h
initial commit
[chaz/p5-File-KDBX-XS] / libtomcrypt / src / headers / tomcrypt_private.h
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 #include "tomcrypt.h"
5
6 /*
7 * Internal Macros
8 */
9
10 #define LTC_PAD_MASK (0xF000U)
11
12 /*
13 * Internal Enums
14 */
15
16 enum ltc_oid_id {
17 PKA_RSA,
18 PKA_DSA,
19 PKA_EC,
20 PKA_EC_PRIMEF,
21 PKA_X25519,
22 PKA_ED25519,
23 };
24
25 /*
26 * Internal Types
27 */
28
29 typedef struct {
30 int size;
31 const char *name, *base, *prime;
32 } ltc_dh_set_type;
33
34
35 typedef int (*fn_kdf_t)(const unsigned char *password, unsigned long password_len,
36 const unsigned char *salt, unsigned long salt_len,
37 int iteration_count, int hash_idx,
38 unsigned char *out, unsigned long *outlen);
39
40 typedef struct {
41 /* KDF */
42 fn_kdf_t kdf;
43 /* Hash or HMAC */
44 const char* h;
45 /* cipher */
46 const char* c;
47 unsigned long keylen;
48 /* not used for pbkdf2 */
49 unsigned long blocklen;
50 } pbes_properties;
51
52 typedef struct
53 {
54 pbes_properties type;
55 const void *pwd;
56 unsigned long pwdlen;
57 ltc_asn1_list *enc_data;
58 ltc_asn1_list *salt;
59 ltc_asn1_list *iv;
60 unsigned long iterations;
61 /* only used for RC2 */
62 unsigned long key_bits;
63 } pbes_arg;
64
65 /*
66 * Internal functions
67 */
68
69
70 /* tomcrypt_cipher.h */
71
72 void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
73 int blowfish_expand(const unsigned char *key, int keylen,
74 const unsigned char *data, int datalen,
75 symmetric_key *skey);
76 int blowfish_setup_with_data(const unsigned char *key, int keylen,
77 const unsigned char *data, int datalen,
78 symmetric_key *skey);
79
80 /* tomcrypt_hash.h */
81
82 /* a simple macro for making hash "process" functions */
83 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
84 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
85 { \
86 unsigned long n; \
87 int err; \
88 LTC_ARGCHK(md != NULL); \
89 LTC_ARGCHK(in != NULL); \
90 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
91 return CRYPT_INVALID_ARG; \
92 } \
93 if ((md-> state_var .length + inlen * 8) < md-> state_var .length) { \
94 return CRYPT_HASH_OVERFLOW; \
95 } \
96 while (inlen > 0) { \
97 if (md-> state_var .curlen == 0 && inlen >= block_size) { \
98 if ((err = compress_name (md, in)) != CRYPT_OK) { \
99 return err; \
100 } \
101 md-> state_var .length += block_size * 8; \
102 in += block_size; \
103 inlen -= block_size; \
104 } else { \
105 n = MIN(inlen, (block_size - md-> state_var .curlen)); \
106 XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
107 md-> state_var .curlen += n; \
108 in += n; \
109 inlen -= n; \
110 if (md-> state_var .curlen == block_size) { \
111 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
112 return err; \
113 } \
114 md-> state_var .length += 8*block_size; \
115 md-> state_var .curlen = 0; \
116 } \
117 } \
118 } \
119 return CRYPT_OK; \
120 }
121
122
123 /* tomcrypt_mac.h */
124
125 int ocb3_int_ntz(unsigned long x);
126 void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
127
128
129 /* tomcrypt_math.h */
130
131 #if !defined(DESC_DEF_ONLY)
132
133 #define MP_DIGIT_BIT ltc_mp.bits_per_digit
134
135 /* some handy macros */
136 #define mp_init(a) ltc_mp.init(a)
137 #define mp_init_multi ltc_init_multi
138 #define mp_clear(a) ltc_mp.deinit(a)
139 #define mp_clear_multi ltc_deinit_multi
140 #define mp_cleanup_multi ltc_cleanup_multi
141 #define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
142
143 #define mp_neg(a, b) ltc_mp.neg(a, b)
144 #define mp_copy(a, b) ltc_mp.copy(a, b)
145
146 #define mp_set(a, b) ltc_mp.set_int(a, b)
147 #define mp_set_int(a, b) ltc_mp.set_int(a, b)
148 #define mp_get_int(a) ltc_mp.get_int(a)
149 #define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
150 #define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
151 #define mp_cmp(a, b) ltc_mp.compare(a, b)
152 #define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
153 #define mp_count_bits(a) ltc_mp.count_bits(a)
154 #define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
155 #define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
156
157 #define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
158 #define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
159 #define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
160 #define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
161 #define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
162
163 #define mp_add(a, b, c) ltc_mp.add(a, b, c)
164 #define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
165 #define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
166 #define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
167 #define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
168 #define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
169 #define mp_sqr(a, b) ltc_mp.sqr(a, b)
170 #define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
171 #define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
172 #define mp_div_2(a, b) ltc_mp.div_2(a, b)
173 #define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
174 #define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
175 #define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
176 #define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
177
178 #define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
179 #define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
180 #define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
181 #define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
182 #define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
183
184 #define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
185 #define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
186 #define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
187 #define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
188
189 #define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
190 #define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
191
192 #define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
193 #define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
194 #define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
195
196 #define mp_tohex(a, b) mp_toradix(a, b, 16)
197
198 #define mp_rand(a, b) ltc_mp.rand(a, b)
199
200 #endif
201
202
203 /* tomcrypt_misc.h */
204
205 void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
206
207 int pbes_decrypt(const pbes_arg *arg, unsigned char *dec_data, unsigned long *dec_size);
208
209 int pbes1_extract(const ltc_asn1_list *s, pbes_arg *res);
210 int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res);
211
212
213 /* tomcrypt_pk.h */
214
215 int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
216 int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
217
218 int pk_get_oid(enum ltc_oid_id id, const char **st);
219 int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
220 int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
221
222 /* ---- DH Routines ---- */
223 #ifdef LTC_MRSA
224 int rsa_init(rsa_key *key);
225 void rsa_shrink_key(rsa_key *key);
226 int rsa_make_key_bn_e(prng_state *prng, int wprng, int size, void *e,
227 rsa_key *key); /* used by op-tee */
228 #endif /* LTC_MRSA */
229
230 /* ---- DH Routines ---- */
231 #ifdef LTC_MDH
232 extern const ltc_dh_set_type ltc_dh_sets[];
233
234 int dh_check_pubkey(const dh_key *key);
235 #endif /* LTC_MDH */
236
237 /* ---- ECC Routines ---- */
238 #ifdef LTC_MECC
239 int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
240 int ecc_copy_curve(const ecc_key *srckey, ecc_key *key);
241 int ecc_set_curve_by_size(int size, ecc_key *key);
242 int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key);
243
244 #ifdef LTC_SSH
245 int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key);
246 #endif
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_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p);
252 int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst);
253 int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y);
254 int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval);
255 int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
256 int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
257 int ltc_ecc_verify_key(const ecc_key *key);
258
259 /* point ops (mp == montgomery digit) */
260 #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
261 /* R = 2P */
262 int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, void *ma, void *modulus, void *mp);
263
264 /* R = P + Q */
265 int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_point *R, void *ma, void *modulus, void *mp);
266 #endif
267
268 #if defined(LTC_MECC_FP)
269 /* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
270 int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
271
272 /* functions for saving/loading/freeing/adding to fixed point cache */
273 int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
274 int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
275 void ltc_ecc_fp_free(void);
276 int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
277
278 /* lock/unlock all points currently in fixed point cache */
279 void ltc_ecc_fp_tablelock(int lock);
280 #endif
281
282 /* R = kG */
283 int ltc_ecc_mulmod(void *k, const ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
284
285 #ifdef LTC_ECC_SHAMIR
286 /* kA*A + kB*B = C */
287 int ltc_ecc_mul2add(const ecc_point *A, void *kA,
288 const ecc_point *B, void *kB,
289 ecc_point *C,
290 void *ma,
291 void *modulus);
292
293 #ifdef LTC_MECC_FP
294 /* Shamir's trick with optimized point multiplication using fixed point cache */
295 int ltc_ecc_fp_mul2add(const ecc_point *A, void *kA,
296 const ecc_point *B, void *kB,
297 ecc_point *C,
298 void *ma,
299 void *modulus);
300 #endif
301
302 #endif
303
304
305 /* map P to affine from projective */
306 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
307 #endif /* LTC_MECC */
308
309 #ifdef LTC_MDSA
310 int dsa_int_validate_xy(const dsa_key *key, int *stat);
311 int dsa_int_validate_pqg(const dsa_key *key, int *stat);
312 int dsa_int_validate_primes(const dsa_key *key, int *stat);
313 #endif /* LTC_MDSA */
314
315
316 #ifdef LTC_CURVE25519
317
318 int tweetnacl_crypto_sign(
319 unsigned char *sm,unsigned long long *smlen,
320 const unsigned char *m,unsigned long long mlen,
321 const unsigned char *sk, const unsigned char *pk);
322 int tweetnacl_crypto_sign_open(
323 int *stat,
324 unsigned char *m,unsigned long long *mlen,
325 const unsigned char *sm,unsigned long long smlen,
326 const unsigned char *pk);
327 int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, unsigned char *pk,unsigned char *sk);
328 int tweetnacl_crypto_sk_to_pk(unsigned char *pk, const unsigned char *sk);
329 int tweetnacl_crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
330 int tweetnacl_crypto_scalarmult_base(unsigned char *q,const unsigned char *n);
331
332 typedef int (*sk_to_pk)(unsigned char *pk ,const unsigned char *sk);
333 int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
334 const void *pwd, unsigned long pwdlen,
335 enum ltc_oid_id id, sk_to_pk fp,
336 curve25519_key *key);
337 int ec25519_export( unsigned char *out, unsigned long *outlen,
338 int which,
339 const curve25519_key *key);
340 #endif /* LTC_CURVE25519 */
341
342 #ifdef LTC_DER
343
344 #define LTC_ASN1_IS_TYPE(e, t) (((e) != NULL) && ((e)->type == (t)))
345
346 /* DER handling */
347 int der_decode_custom_type_ex(const unsigned char *in, unsigned long inlen,
348 ltc_asn1_list *root,
349 ltc_asn1_list *list, unsigned long outlen, unsigned int flags);
350
351 int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsigned long *outlen);
352 int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, ltc_asn1_list *id);
353 int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen);
354
355 int der_encode_asn1_length(unsigned long len, unsigned char* out, unsigned long* outlen);
356 int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen);
357 int der_length_asn1_length(unsigned long len, unsigned long *outlen);
358
359 int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
360 unsigned long *outlen, unsigned long *payloadlen);
361
362 extern const ltc_asn1_type der_asn1_tag_to_type_map[];
363 extern const unsigned long der_asn1_tag_to_type_map_sz;
364
365 extern const int der_asn1_type_to_identifier_map[];
366 extern const unsigned long der_asn1_type_to_identifier_map_sz;
367
368 int der_decode_sequence_multi_ex(const unsigned char *in, unsigned long inlen, unsigned int flags, ...);
369
370 int der_teletex_char_encode(int c);
371 int der_teletex_value_decode(int v);
372
373 int der_utf8_valid_char(const wchar_t c);
374
375 typedef int (*public_key_decode_cb)(const unsigned char *in, unsigned long inlen, void *ctx);
376
377 int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen,
378 enum ltc_oid_id algorithm, ltc_asn1_type param_type,
379 ltc_asn1_list* parameters, unsigned long *parameters_len,
380 public_key_decode_cb callback, void *ctx);
381
382 /* SUBJECT PUBLIC KEY INFO */
383 int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
384 unsigned int algorithm, const void* public_key, unsigned long public_key_len,
385 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
386
387 int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
388 unsigned int algorithm, void* public_key, unsigned long* public_key_len,
389 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
390
391 int pk_oid_cmp_with_ulong(const char *o1, const unsigned long *o2, unsigned long o2size);
392 int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2);
393
394 #endif /* LTC_DER */
395
396 /* tomcrypt_pkcs.h */
397
398 #ifdef LTC_PKCS_8
399
400 int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
401 const void *pwd, unsigned long pwdlen,
402 ltc_asn1_list **decoded_list);
403
404 #endif /* LTC_PKCS_8 */
405
406
407 #ifdef LTC_PKCS_12
408
409 int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen,
410 unsigned char *out, unsigned long *outlen);
411
412 int pkcs12_kdf( int hash_id,
413 const unsigned char *pw, unsigned long pwlen,
414 const unsigned char *salt, unsigned long saltlen,
415 unsigned int iterations, unsigned char purpose,
416 unsigned char *out, unsigned long outlen);
417
418 #endif /* LTC_PKCS_12 */
419
420 /* tomcrypt_prng.h */
421
422 #define LTC_PRNG_EXPORT(which) \
423 int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
424 { \
425 unsigned long len = which ## _desc.export_size; \
426 \
427 LTC_ARGCHK(prng != NULL); \
428 LTC_ARGCHK(out != NULL); \
429 LTC_ARGCHK(outlen != NULL); \
430 \
431 if (*outlen < len) { \
432 *outlen = len; \
433 return CRYPT_BUFFER_OVERFLOW; \
434 } \
435 \
436 if (which ## _read(out, len, prng) != len) { \
437 return CRYPT_ERROR_READPRNG; \
438 } \
439 \
440 *outlen = len; \
441 return CRYPT_OK; \
442 }
443
444 /* extract a byte portably */
445 #ifdef _MSC_VER
446 #define LTC_BYTE(x, n) ((unsigned char)((x) >> (8 * (n))))
447 #else
448 #define LTC_BYTE(x, n) (((x) >> (8 * (n))) & 255)
449 #endif
This page took 0.054627 seconds and 4 git commands to generate.