tomcrypt_private.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  2. /* SPDX-License-Identifier: Unlicense */
  3. #include "tomcrypt.h"
  4. #include <stdarg.h>
  5. #ifndef TOMCRYPT_PRIVATE_H_
  6. #define TOMCRYPT_PRIVATE_H_
  7. /*
  8. * Internal Macros
  9. */
  10. /* Static assertion */
  11. #define LTC_STATIC_ASSERT(msg, cond) typedef char ltc_static_assert_##msg[(cond) ? 1 : -1];
  12. #define LTC_PAD_MASK (0xF000U)
  13. /* only real 64bit, not ILP32 */
  14. #if defined(ENDIAN_64BITWORD) && !defined(ENDIAN_64BITWORD_ILP32)
  15. #define CONSTPTR(n) CONST64(n)
  16. #else
  17. #define CONSTPTR(n) n ## uL
  18. #endif
  19. LTC_STATIC_ASSERT(correct_CONSTPTR_size, sizeof(CONSTPTR(1)) == sizeof(void*))
  20. /* Poor-man's `uintptr_t` since we can't use stdint.h
  21. * c.f. https://github.com/DCIT/perl-CryptX/issues/95#issuecomment-1745280962 */
  22. typedef size_t ltc_uintptr;
  23. LTC_STATIC_ASSERT(correct_ltc_uintptr_size, sizeof(ltc_uintptr) == sizeof(void*))
  24. /* Aligns a `unsigned char` buffer `buf` to `n` bytes and returns that aligned address.
  25. * Make sure that the buffer that is passed is huge enough.
  26. */
  27. #define LTC_ALIGN_BUF(buf, n) ((void*)((ltc_uintptr)&((unsigned char*)(buf))[n - 1] & (~(CONSTPTR(n) - CONSTPTR(1)))))
  28. #define LTC_OID_MAX_STRLEN 256
  29. /* `NULL` as defined by the standard is not guaranteed to be of a pointer
  30. * type. In order to make sure that in vararg API's a pointer type is used,
  31. * define our own version and use that one internally.
  32. */
  33. #ifndef LTC_NULL
  34. #define LTC_NULL ((void *)0)
  35. #endif
  36. #define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
  37. /*
  38. * Internal Enums
  39. */
  40. enum ltc_oid_id {
  41. LTC_OID_UNDEF,
  42. LTC_OID_RSA,
  43. LTC_OID_DSA,
  44. LTC_OID_EC,
  45. LTC_OID_EC_PRIMEF,
  46. LTC_OID_X25519,
  47. LTC_OID_ED25519,
  48. LTC_OID_DH,
  49. LTC_OID_NUM
  50. };
  51. /*
  52. * Internal Types
  53. */
  54. typedef struct {
  55. int size;
  56. const char *name, *base, *prime;
  57. } ltc_dh_set_type;
  58. struct password {
  59. /* usually a `char*` but could also contain binary data
  60. * so use a `void*` + length to be on the safe side.
  61. */
  62. void *pw;
  63. unsigned long l;
  64. };
  65. typedef int (*fn_kdf_t)(const struct password *pwd,
  66. const unsigned char *salt, unsigned long salt_len,
  67. int iteration_count, int hash_idx,
  68. unsigned char *out, unsigned long *outlen);
  69. #if defined(LTC_PBES)
  70. typedef struct {
  71. /* KDF */
  72. fn_kdf_t kdf;
  73. /* Hash or HMAC */
  74. const char* h;
  75. /* cipher */
  76. const char* c;
  77. unsigned long keylen;
  78. /* not used for pbkdf2 */
  79. unsigned long blocklen;
  80. } pbes_properties;
  81. typedef struct
  82. {
  83. pbes_properties type;
  84. struct password pw;
  85. ltc_asn1_list *enc_data;
  86. ltc_asn1_list *salt;
  87. ltc_asn1_list *iv;
  88. unsigned long iterations;
  89. /* only used for RC2 */
  90. unsigned long key_bits;
  91. } pbes_arg;
  92. typedef struct {
  93. const pbes_properties *data;
  94. const char *oid;
  95. } oid_to_pbes;
  96. #endif
  97. /*
  98. * Internal functions
  99. */
  100. /* tomcrypt_cipher.h */
  101. int ecb_encrypt_block(const unsigned char *pt, unsigned char *ct, const symmetric_ECB *ecb);
  102. int ecb_decrypt_block(const unsigned char *ct, unsigned char *pt, const symmetric_ECB *ecb);
  103. void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
  104. int blowfish_expand(const unsigned char *key, int keylen,
  105. const unsigned char *data, int datalen,
  106. symmetric_key *skey);
  107. int blowfish_setup_with_data(const unsigned char *key, int keylen,
  108. const unsigned char *data, int datalen,
  109. symmetric_key *skey);
  110. /* tomcrypt_hash.h */
  111. /* a simple macro for making hash "process" functions */
  112. #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
  113. int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
  114. { \
  115. unsigned long n; \
  116. int err; \
  117. LTC_ARGCHK(md != NULL); \
  118. LTC_ARGCHK(in != NULL); \
  119. if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
  120. return CRYPT_INVALID_ARG; \
  121. } \
  122. if (((md-> state_var .length + inlen * 8) < md-> state_var .length) \
  123. || ((inlen * 8) < inlen)) { \
  124. return CRYPT_HASH_OVERFLOW; \
  125. } \
  126. while (inlen > 0) { \
  127. if (md-> state_var .curlen == 0 && inlen >= block_size) { \
  128. if ((err = compress_name (md, in)) != CRYPT_OK) { \
  129. return err; \
  130. } \
  131. md-> state_var .length += block_size * 8; \
  132. in += block_size; \
  133. inlen -= block_size; \
  134. } else { \
  135. n = MIN(inlen, (block_size - md-> state_var .curlen)); \
  136. XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
  137. md-> state_var .curlen += n; \
  138. in += n; \
  139. inlen -= n; \
  140. if (md-> state_var .curlen == block_size) { \
  141. if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
  142. return err; \
  143. } \
  144. md-> state_var .length += 8*block_size; \
  145. md-> state_var .curlen = 0; \
  146. } \
  147. } \
  148. } \
  149. return CRYPT_OK; \
  150. }
  151. /* tomcrypt_mac.h */
  152. int ocb3_int_ntz(unsigned long x);
  153. void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
  154. #ifdef LTC_OMAC
  155. int omac_vprocess(omac_state *omac, const unsigned char *in, unsigned long inlen, va_list args);
  156. #endif
  157. /* tomcrypt_math.h */
  158. #if !defined(DESC_DEF_ONLY)
  159. #define LTC_MP_DIGIT_BIT ltc_mp.bits_per_digit
  160. /* some handy macros */
  161. #define ltc_mp_init(a) ltc_mp.init(a)
  162. #define ltc_mp_clear(a) ltc_mp.deinit(a)
  163. #define ltc_mp_init_copy(a, b) ltc_mp.init_copy(a, b)
  164. #define ltc_mp_neg(a, b) ltc_mp.neg(a, b)
  165. #define ltc_mp_copy(a, b) ltc_mp.copy(a, b)
  166. #define ltc_mp_set(a, b) ltc_mp.set_int(a, b)
  167. #define ltc_mp_set_int(a, b) ltc_mp.set_int(a, b)
  168. #define ltc_mp_get_int(a) ltc_mp.get_int(a)
  169. #define ltc_mp_get_digit(a, n) ltc_mp.get_digit(a, n)
  170. #define ltc_mp_get_digit_count(a) ltc_mp.get_digit_count(a)
  171. #define ltc_mp_cmp(a, b) ltc_mp.compare(a, b)
  172. #define ltc_mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
  173. #define ltc_mp_count_bits(a) ltc_mp.count_bits(a)
  174. #define ltc_mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
  175. #define ltc_mp_2expt(a, b) ltc_mp.twoexpt(a, b)
  176. #define ltc_mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
  177. #define ltc_mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
  178. #define ltc_mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
  179. #define ltc_mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
  180. #define ltc_mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
  181. #define ltc_mp_add(a, b, c) ltc_mp.add(a, b, c)
  182. #define ltc_mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
  183. #define ltc_mp_sub(a, b, c) ltc_mp.sub(a, b, c)
  184. #define ltc_mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
  185. #define ltc_mp_mul(a, b, c) ltc_mp.mul(a, b, c)
  186. #define ltc_mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
  187. #define ltc_mp_sqr(a, b) ltc_mp.sqr(a, b)
  188. #define ltc_mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
  189. #define ltc_mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
  190. #define ltc_mp_div_2(a, b) ltc_mp.div_2(a, b)
  191. #define ltc_mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
  192. #define ltc_mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
  193. #define ltc_mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
  194. #define ltc_mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
  195. #define ltc_mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
  196. #define ltc_mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
  197. #define ltc_mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
  198. #define ltc_mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
  199. #define ltc_mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
  200. #define ltc_mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
  201. #define ltc_mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
  202. #define ltc_mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
  203. #define ltc_mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
  204. #define ltc_mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
  205. #define ltc_mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
  206. #define ltc_mp_iszero(a) (ltc_mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
  207. #define ltc_mp_isodd(a) (ltc_mp_get_digit_count(a) > 0 ? (ltc_mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
  208. #define ltc_mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
  209. #define ltc_mp_tohex(a, b) ltc_mp_toradix(a, b, 16)
  210. #define ltc_mp_rand(a, b) ltc_mp.rand(a, b)
  211. #endif
  212. /* tomcrypt_misc.h */
  213. typedef enum {
  214. /** Use `\r\n` as line separator */
  215. BASE64_PEM_CRLF = 1,
  216. /** Create output with 72 chars line length */
  217. BASE64_PEM_SSH = 2,
  218. } base64_pem_flags;
  219. int base64_encode_pem(const unsigned char *in, unsigned long inlen,
  220. char *out, unsigned long *outlen,
  221. unsigned int flags);
  222. /* PEM related */
  223. #ifdef LTC_PEM
  224. enum cipher_mode {
  225. cm_modes = 0x00ff,
  226. cm_flags = 0xff00,
  227. /* Flags */
  228. cm_openssh = 0x0100,
  229. cm_1bit = 0x0200,
  230. cm_8bit = 0x0400,
  231. /* Modes */
  232. cm_none = 0x0000,
  233. cm_cbc = 0x0001,
  234. cm_cfb = 0x0002,
  235. cm_ctr = 0x0003,
  236. cm_ofb = 0x0004,
  237. cm_stream = 0x0005,
  238. cm_gcm = 0x0006,
  239. cm_cfb1 = cm_cfb | cm_1bit,
  240. cm_cfb8 = cm_cfb | cm_8bit,
  241. cm_stream_openssh = cm_stream | cm_openssh,
  242. };
  243. struct blockcipher_info {
  244. const char *name;
  245. const char *algo;
  246. unsigned long keylen;
  247. enum cipher_mode mode;
  248. /* should use `MAXBLOCKSIZE` here, but all supported
  249. * blockciphers require max 16 bytes IV */
  250. char iv[16 * 2 + 1];
  251. };
  252. struct str {
  253. char *p;
  254. unsigned long len;
  255. };
  256. #define SET_STR(n, s) n.p = s, n.len = XSTRLEN(s)
  257. #define SET_CSTR(n, s) n.p = (char*)s, n.len = (sizeof s) - 1
  258. #define COPY_STR(n, s, l) do { XMEMCPY(n.p, s, l); n.len = l; } while(0)
  259. #define RESET_STR(n) do { n.p = NULL; n.len = 0; } while(0)
  260. enum more_headers {
  261. no,
  262. yes,
  263. maybe
  264. };
  265. enum pem_flags {
  266. pf_encrypted = 0x01u,
  267. pf_pkcs8 = 0x02u,
  268. pf_public = 0x04u,
  269. pf_x509 = 0x08u,
  270. pf_encrypted_pkcs8 = pf_encrypted | pf_pkcs8,
  271. };
  272. struct pem_header_id {
  273. struct str start, end;
  274. enum more_headers has_more_headers;
  275. enum pem_flags flags;
  276. enum ltc_pka_id pka;
  277. int (*decrypt)(void *, unsigned long *, void *);
  278. };
  279. struct pem_headers {
  280. const struct pem_header_id *id;
  281. int encrypted;
  282. struct blockcipher_info info;
  283. struct password *pw;
  284. };
  285. struct bufp {
  286. /* `end` points to one byte after the last
  287. * element of the allocated buffer
  288. */
  289. char *start, *work, *end;
  290. };
  291. #define SET_BUFP(n, d, l) n.start = (char*)d, n.work = (char*)d, n.end = (char*)d + l + 1
  292. struct get_char {
  293. int (*get)(struct get_char*);
  294. union {
  295. #ifndef LTC_NO_FILE
  296. FILE *f;
  297. #endif /* LTC_NO_FILE */
  298. struct bufp buf;
  299. } data;
  300. struct str unget_buf;
  301. char unget_buf_[LTC_PEM_DECODE_BUFSZ];
  302. int prev_get;
  303. };
  304. #endif
  305. /* others */
  306. void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
  307. void password_free(struct password *pw, const struct password_ctx *ctx);
  308. #if defined(LTC_PBES)
  309. int pbes_decrypt(const pbes_arg *arg, unsigned char *dec_data, unsigned long *dec_size);
  310. int pbes1_extract(const ltc_asn1_list *s, pbes_arg *res);
  311. int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res);
  312. #endif
  313. #ifdef LTC_PEM
  314. int pem_decrypt(unsigned char *data, unsigned long *datalen,
  315. unsigned char *key, unsigned long keylen,
  316. unsigned char *iv, unsigned long ivlen,
  317. unsigned char *tag, unsigned long taglen,
  318. const struct blockcipher_info *info,
  319. enum padding_type padding);
  320. #ifndef LTC_NO_FILE
  321. int pem_get_char_from_file(struct get_char *g);
  322. #endif /* LTC_NO_FILE */
  323. int pem_get_char_from_buf(struct get_char *g);
  324. int pem_read(void *asn1_cert, unsigned long *asn1_len, struct pem_headers *hdr, struct get_char *g);
  325. #endif
  326. /* tomcrypt_pk.h */
  327. int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
  328. int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
  329. int pk_get_oid(enum ltc_oid_id id, const char **st);
  330. int pk_get_pka_id(enum ltc_oid_id id, enum ltc_pka_id *pka);
  331. int pk_get_oid_id(enum ltc_pka_id pka, enum ltc_oid_id *oid);
  332. #ifdef LTC_DER
  333. int pk_get_oid_from_asn1(const ltc_asn1_list *oid, enum ltc_oid_id *id);
  334. #endif
  335. int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
  336. int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
  337. int pk_oid_cmp_with_ulong(const char *o1, const unsigned long *o2, unsigned long o2size);
  338. /* ---- DH Routines ---- */
  339. #ifdef LTC_MRSA
  340. int rsa_init(rsa_key *key);
  341. void rsa_shrink_key(rsa_key *key);
  342. int rsa_make_key_bn_e(prng_state *prng, int wprng, int size, void *e,
  343. rsa_key *key); /* used by op-tee */
  344. int rsa_import_pkcs1(const unsigned char *in, unsigned long inlen, rsa_key *key);
  345. int rsa_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, rsa_key *key);
  346. #endif /* LTC_MRSA */
  347. /* ---- DH Routines ---- */
  348. #ifdef LTC_MDH
  349. extern const ltc_dh_set_type ltc_dh_sets[];
  350. int dh_init(dh_key *key);
  351. int dh_check_pubkey(const dh_key *key);
  352. int dh_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, dh_key *key);
  353. #endif /* LTC_MDH */
  354. /* ---- ECC Routines ---- */
  355. #ifdef LTC_MECC
  356. int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
  357. int ecc_copy_curve(const ecc_key *srckey, ecc_key *key);
  358. int ecc_set_curve_by_size(int size, ecc_key *key);
  359. int ecc_get_curve_names(const char *oid, const char * const **names);
  360. int ecc_import_subject_public_key_info(const unsigned char *in, unsigned long inlen, ecc_key *key);
  361. #ifdef LTC_DER
  362. int ecc_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, ecc_key *key);
  363. #endif
  364. int ecc_import_with_curve(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
  365. int ecc_import_with_oid(const unsigned char *in, unsigned long inlen, unsigned long *oid, unsigned long oid_len, int type, ecc_key *key);
  366. int ecc_sign_hash_rfc7518_internal(const unsigned char *in,
  367. unsigned long inlen,
  368. unsigned char *out,
  369. unsigned long *outlen,
  370. ltc_ecc_sig_opts *opts,
  371. const ecc_key *key);
  372. int ecc_verify_hash_rfc7518_internal(const unsigned char *sig,
  373. unsigned long siglen,
  374. const unsigned char *hash,
  375. unsigned long hashlen,
  376. int *stat,
  377. const ecc_key *key);
  378. #ifdef LTC_DER
  379. int ecc_verify_hash_x962(const unsigned char *sig,
  380. unsigned long siglen,
  381. const unsigned char *hash,
  382. unsigned long hashlen,
  383. int *stat,
  384. const ecc_key *key);
  385. int ecc_sign_hash_x962(const unsigned char *in,
  386. unsigned long inlen,
  387. unsigned char *out,
  388. unsigned long *outlen,
  389. ltc_ecc_sig_opts *opts,
  390. const ecc_key *key);
  391. #endif
  392. #if defined(LTC_SSH)
  393. int ecc_sign_hash_rfc5656(const unsigned char *in,
  394. unsigned long inlen,
  395. unsigned char *out,
  396. unsigned long *outlen,
  397. ltc_ecc_sig_opts *opts,
  398. const ecc_key *key);
  399. int ecc_verify_hash_rfc5656(const unsigned char *sig,
  400. unsigned long siglen,
  401. const unsigned char *hash,
  402. unsigned long hashlen,
  403. int *stat,
  404. const ecc_key *key);
  405. #endif
  406. int ecc_sign_hash_eth27(const unsigned char *in, unsigned long inlen,
  407. unsigned char *out, unsigned long *outlen,
  408. ltc_ecc_sig_opts *opts, const ecc_key *key);
  409. int ecc_verify_hash_eth27(const unsigned char *sig, unsigned long siglen,
  410. const unsigned char *hash, unsigned long hashlen,
  411. int *stat, const ecc_key *key);
  412. int ecc_sign_hash_internal(const unsigned char *in, unsigned long inlen,
  413. void *r, void *s, ltc_ecc_sig_opts *opts, const ecc_key *key);
  414. int ecc_verify_hash_internal(void *r, void *s,
  415. const unsigned char *hash, unsigned long hashlen,
  416. int *stat, const ecc_key *key);
  417. int ecc_rfc6979_key(const ecc_key *priv, const unsigned char *in, unsigned long inlen, const char *rfc6979_hash_alg, ecc_key *key);
  418. #ifdef LTC_SSH
  419. int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key);
  420. #endif
  421. /* low level functions */
  422. ecc_point *ltc_ecc_new_point(void);
  423. void ltc_ecc_del_point(ecc_point *p);
  424. int ltc_ecc_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p);
  425. int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst);
  426. int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y);
  427. int ltc_ecc_is_point_at_infinity(const ecc_point *P, const void *modulus, int *retval);
  428. int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
  429. int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
  430. int ltc_ecc_verify_key(const ecc_key *key);
  431. /* point ops (mp == montgomery digit) */
  432. #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
  433. /* R = 2P */
  434. int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R,
  435. const void *ma, const void *modulus, void *mp);
  436. /* R = P + Q */
  437. int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_point *R,
  438. const void *ma, const void *modulus, void *mp);
  439. #endif
  440. #if defined(LTC_MECC_FP)
  441. /* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
  442. int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
  443. /* functions for saving/loading/freeing/adding to fixed point cache */
  444. int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
  445. int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
  446. void ltc_ecc_fp_free(void);
  447. int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
  448. /* lock/unlock all points currently in fixed point cache */
  449. void ltc_ecc_fp_tablelock(int lock);
  450. #endif
  451. /* R = kG */
  452. int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R,
  453. const void *a, const void *modulus, int map);
  454. #ifdef LTC_ECC_SHAMIR
  455. /* kA*A + kB*B = C */
  456. int ltc_ecc_mul2add(const ecc_point *A, void *kA,
  457. const ecc_point *B, void *kB,
  458. ecc_point *C,
  459. const void *ma,
  460. const void *modulus);
  461. #ifdef LTC_MECC_FP
  462. /* Shamir's trick with optimized point multiplication using fixed point cache */
  463. int ltc_ecc_fp_mul2add(const ecc_point *A, void *kA,
  464. const ecc_point *B, void *kB,
  465. ecc_point *C,
  466. const void *ma,
  467. const void *modulus);
  468. #endif
  469. #endif
  470. /* map P to affine from projective */
  471. int ltc_ecc_map(ecc_point *P, const void *modulus, void *mp);
  472. #endif /* LTC_MECC */
  473. #ifdef LTC_MDSA
  474. int dsa_int_init(dsa_key *key);
  475. int dsa_int_validate(const dsa_key *key, int *stat);
  476. int dsa_int_validate_xy(const dsa_key *key, int *stat);
  477. int dsa_int_validate_pqg(const dsa_key *key, int *stat);
  478. int dsa_int_validate_primes(const dsa_key *key, int *stat);
  479. int dsa_import_pkcs1(const unsigned char *in, unsigned long inlen, dsa_key *key);
  480. int dsa_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, dsa_key *key);
  481. #endif /* LTC_MDSA */
  482. #ifdef LTC_CURVE25519
  483. int tweetnacl_crypto_sign(
  484. unsigned char *sm,unsigned long long *smlen,
  485. const unsigned char *m,unsigned long long mlen,
  486. const unsigned char *sk,const unsigned char *pk,
  487. const unsigned char *ctx,unsigned long long cs);
  488. int tweetnacl_crypto_sign_open(
  489. int *stat,
  490. unsigned char *m,unsigned long long *mlen,
  491. const unsigned char *sm,unsigned long long smlen,
  492. const unsigned char *ctx, unsigned long long cs,
  493. const unsigned char *pk);
  494. int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, unsigned char *pk,unsigned char *sk);
  495. int tweetnacl_crypto_sk_to_pk(unsigned char *pk, const unsigned char *sk);
  496. int tweetnacl_crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
  497. int tweetnacl_crypto_scalarmult_base(unsigned char *q,const unsigned char *n);
  498. int tweetnacl_crypto_ph(unsigned char *out, const unsigned char *msg, unsigned long long msglen);
  499. int ed25519_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
  500. curve25519_key *key);
  501. int x25519_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
  502. curve25519_key *key);
  503. int ec25519_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
  504. enum ltc_oid_id id,
  505. curve25519_key *key);
  506. int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
  507. const password_ctx *pw_ctx,
  508. enum ltc_oid_id id,
  509. curve25519_key *key);
  510. int ec25519_export( unsigned char *out, unsigned long *outlen,
  511. int which,
  512. const curve25519_key *key);
  513. int ec25519_crypto_ctx( unsigned char *out, unsigned long *outlen,
  514. unsigned char flag,
  515. const unsigned char *ctx, unsigned long ctxlen);
  516. #endif /* LTC_CURVE25519 */
  517. #ifdef LTC_DER
  518. #define LTC_ASN1_IS_TYPE(e, t) (((e) != NULL) && ((e)->type == (t)))
  519. /* DER handling */
  520. int der_decode_custom_type_ex(const unsigned char *in, unsigned long inlen,
  521. ltc_asn1_list *root,
  522. ltc_asn1_list *list, unsigned long outlen, unsigned int flags);
  523. int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsigned long *outlen);
  524. int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, ltc_asn1_list *id);
  525. int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen);
  526. int der_encode_asn1_length(unsigned long len, unsigned char* out, unsigned long* outlen);
  527. int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen);
  528. int der_length_asn1_length(unsigned long len, unsigned long *outlen);
  529. int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
  530. unsigned long *outlen, unsigned long *payloadlen);
  531. int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords,
  532. unsigned long *outlen, unsigned long *datalen);
  533. int der_ia5_char_encode(int c);
  534. int der_ia5_value_decode(int v);
  535. int der_printable_char_encode(int c);
  536. int der_printable_value_decode(int v);
  537. unsigned long der_utf8_charsize(const wchar_t c);
  538. typedef int (*der_flexi_handler)(const ltc_asn1_list*, void*);
  539. typedef struct der_flexi_check {
  540. ltc_asn1_type t;
  541. int optional;
  542. ltc_asn1_list **pp;
  543. der_flexi_handler handler;
  544. void *userdata;
  545. } der_flexi_check;
  546. #define LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, Opt, Hndl, Udata) \
  547. do { \
  548. int LTC_SDFC_temp##__LINE__ = (index); \
  549. list[LTC_SDFC_temp##__LINE__].t = Type; \
  550. list[LTC_SDFC_temp##__LINE__].pp = P; \
  551. list[LTC_SDFC_temp##__LINE__].optional = Opt; \
  552. list[LTC_SDFC_temp##__LINE__].handler = (der_flexi_handler)Hndl; \
  553. list[LTC_SDFC_temp##__LINE__].userdata = Udata; \
  554. } while (0)
  555. #define LTC_SET_DER_FLEXI_CHECK(list, index, Type, P) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, 0, NULL, NULL)
  556. #define LTC_SET_DER_FLEXI_CHECK_OPT(list, index, Type, P) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, P, 1, NULL, NULL)
  557. #define LTC_SET_DER_FLEXI_HANDLER(list, index, Type, Hndl, Udata) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, NULL, 0, Hndl, Udata)
  558. #define LTC_SET_DER_FLEXI_HANDLER_OPT(list, index, Type, Hndl, Udata) LTC_PRIV_SET_DER_FLEXI_CHECK(list, index, Type, NULL, 1, Hndl, Udata)
  559. extern const ltc_asn1_type der_asn1_tag_to_type_map[];
  560. extern const unsigned long der_asn1_tag_to_type_map_sz;
  561. extern const int der_asn1_type_to_identifier_map[];
  562. extern const unsigned long der_asn1_type_to_identifier_map_sz;
  563. int der_flexi_sequence_cmp(const ltc_asn1_list *flexi, der_flexi_check *check);
  564. int der_decode_sequence_multi_ex(const unsigned char *in, unsigned long inlen, unsigned int flags, ...)
  565. LTC_NULL_TERMINATED;
  566. int der_teletex_char_encode(int c);
  567. int der_teletex_value_decode(int v);
  568. int der_utf8_valid_char(const wchar_t c);
  569. typedef int (*public_key_decode_cb)(const unsigned char *in, unsigned long inlen, void *ctx);
  570. int x509_decode_public_key_from_certificate(const unsigned char *in, unsigned long inlen,
  571. enum ltc_oid_id algorithm, ltc_asn1_type param_type,
  572. ltc_asn1_list* parameters, unsigned long *parameters_len,
  573. public_key_decode_cb callback, void *ctx);
  574. int x509_decode_spki(const unsigned char *in, unsigned long inlen, ltc_asn1_list **out, ltc_asn1_list **spki);
  575. /* SUBJECT PUBLIC KEY INFO */
  576. int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
  577. enum ltc_oid_id algorithm, const void* public_key, unsigned long public_key_len,
  578. ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
  579. int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
  580. enum ltc_oid_id algorithm, void *public_key, unsigned long *public_key_len,
  581. ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
  582. int x509_get_pka(ltc_asn1_list *pub, enum ltc_pka_id *pka);
  583. int x509_import_spki(const unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k, ltc_asn1_list **root);
  584. int pk_oid_cmp_with_asn1(const char *o1, const ltc_asn1_list *o2);
  585. #endif /* LTC_DER */
  586. /* tomcrypt_pkcs.h */
  587. #ifdef LTC_PKCS_8
  588. /* Public-Key Cryptography Standards (PKCS) #8:
  589. * Private-Key Information Syntax Specification Version 1.2
  590. * https://tools.ietf.org/html/rfc5208
  591. *
  592. * PrivateKeyInfo ::= SEQUENCE {
  593. * version Version,
  594. * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
  595. * privateKey PrivateKey,
  596. * attributes [0] IMPLICIT Attributes OPTIONAL }
  597. * where:
  598. * - Version ::= INTEGER
  599. * - PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
  600. * - PrivateKey ::= OCTET STRING
  601. * - Attributes ::= SET OF Attribute
  602. *
  603. * EncryptedPrivateKeyInfo ::= SEQUENCE {
  604. * encryptionAlgorithm EncryptionAlgorithmIdentifier,
  605. * encryptedData EncryptedData }
  606. * where:
  607. * - EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
  608. * - EncryptedData ::= OCTET STRING
  609. */
  610. int pkcs8_decode_flexi(const unsigned char *in, unsigned long inlen,
  611. const password_ctx *pw_ctx,
  612. ltc_asn1_list **decoded_list);
  613. int pkcs8_get_children(const ltc_asn1_list *decoded_list, enum ltc_oid_id *pka,
  614. ltc_asn1_list **alg_id, ltc_asn1_list **priv_key);
  615. #endif /* LTC_PKCS_8 */
  616. #ifdef LTC_PKCS_12
  617. int pkcs12_utf8_to_utf16(const unsigned char *in, unsigned long inlen,
  618. unsigned char *out, unsigned long *outlen);
  619. int pkcs12_kdf( int hash_id,
  620. const unsigned char *pw, unsigned long pwlen,
  621. const unsigned char *salt, unsigned long saltlen,
  622. unsigned int iterations, unsigned char purpose,
  623. unsigned char *out, unsigned long outlen);
  624. #endif /* LTC_PKCS_12 */
  625. /* tomcrypt_prng.h */
  626. #define LTC_PRNG_EXPORT(which) \
  627. int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
  628. { \
  629. unsigned long len = which ## _desc.export_size; \
  630. \
  631. LTC_ARGCHK(prng != NULL); \
  632. LTC_ARGCHK(out != NULL); \
  633. LTC_ARGCHK(outlen != NULL); \
  634. \
  635. if (*outlen < len) { \
  636. *outlen = len; \
  637. return CRYPT_BUFFER_OVERFLOW; \
  638. } \
  639. \
  640. if (which ## _read(out, len, prng) != len) { \
  641. return CRYPT_ERROR_READPRNG; \
  642. } \
  643. \
  644. *outlen = len; \
  645. return CRYPT_OK; \
  646. }
  647. /* extract a byte portably */
  648. #ifdef _MSC_VER
  649. #define LTC_BYTE(x, n) ((unsigned char)((x) >> (8 * (n))))
  650. #else
  651. #define LTC_BYTE(x, n) (((x) >> (8 * (n))) & 255)
  652. #endif
  653. /*
  654. * On Windows, choose whether to use CryptGenRandom() [older Windows versions]
  655. * or BCryptGenRandom() [newer Windows versions].
  656. * If CryptGenRandom() is desired, define LTC_NO_WIN32_BCRYPT when building.
  657. */
  658. #if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600
  659. #if !defined(LTC_NO_WIN32_BCRYPT)
  660. #define LTC_WIN32_BCRYPT
  661. #endif
  662. #endif
  663. #endif /* TOMCRYPT_PRIVATE_H_ */