tomcrypt_custom.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #ifndef TOMCRYPT_CUSTOM_H_
  10. #define TOMCRYPT_CUSTOM_H_
  11. /* macros for various libc functions you can change for embedded targets */
  12. #ifndef XMALLOC
  13. #define XMALLOC malloc
  14. #endif
  15. #ifndef XREALLOC
  16. #define XREALLOC realloc
  17. #endif
  18. #ifndef XCALLOC
  19. #define XCALLOC calloc
  20. #endif
  21. #ifndef XFREE
  22. #define XFREE free
  23. #endif
  24. #ifndef XMEMSET
  25. #define XMEMSET memset
  26. #endif
  27. #ifndef XMEMCPY
  28. #define XMEMCPY memcpy
  29. #endif
  30. #ifndef XMEMMOVE
  31. #define XMEMMOVE memmove
  32. #endif
  33. #ifndef XMEMCMP
  34. #define XMEMCMP memcmp
  35. #endif
  36. /* A memory compare function that has to run in constant time,
  37. * c.f. mem_neq() API summary.
  38. */
  39. #ifndef XMEM_NEQ
  40. #define XMEM_NEQ mem_neq
  41. #endif
  42. #ifndef XSTRCMP
  43. #define XSTRCMP strcmp
  44. #endif
  45. #ifndef XCLOCK
  46. #define XCLOCK clock
  47. #endif
  48. #ifndef XQSORT
  49. #define XQSORT qsort
  50. #endif
  51. #if ( defined(malloc) || defined(realloc) || defined(calloc) || defined(free) || \
  52. defined(memset) || defined(memcpy) || defined(memcmp) || defined(strcmp) || \
  53. defined(clock) || defined(qsort) ) && !defined(LTC_NO_PROTOTYPES)
  54. #define LTC_NO_PROTOTYPES
  55. #endif
  56. /* shortcut to disable automatic inclusion */
  57. #if defined LTC_NOTHING && !defined LTC_EASY
  58. #define LTC_NO_MATH
  59. #define LTC_NO_CIPHERS
  60. #define LTC_NO_MODES
  61. #define LTC_NO_HASHES
  62. #define LTC_NO_MACS
  63. #define LTC_NO_PRNGS
  64. #define LTC_NO_PK
  65. #define LTC_NO_PKCS
  66. #define LTC_NO_MISC
  67. #endif /* LTC_NOTHING */
  68. /* Easy button? */
  69. #ifdef LTC_EASY
  70. #define LTC_NO_CIPHERS
  71. #define LTC_RIJNDAEL
  72. #define LTC_BLOWFISH
  73. #define LTC_DES
  74. #define LTC_CAST5
  75. #define LTC_NO_MODES
  76. #define LTC_ECB_MODE
  77. #define LTC_CBC_MODE
  78. #define LTC_CTR_MODE
  79. #define LTC_NO_HASHES
  80. #define LTC_SHA1
  81. #define LTC_SHA3
  82. #define LTC_SHA512
  83. #define LTC_SHA384
  84. #define LTC_SHA256
  85. #define LTC_SHA224
  86. #define LTC_HASH_HELPERS
  87. #define LTC_NO_MACS
  88. #define LTC_HMAC
  89. #define LTC_OMAC
  90. #define LTC_CCM_MODE
  91. #define LTC_NO_PRNGS
  92. #define LTC_SPRNG
  93. #define LTC_YARROW
  94. #define LTC_DEVRANDOM
  95. #define LTC_TRY_URANDOM_FIRST
  96. #define LTC_RNG_GET_BYTES
  97. #define LTC_RNG_MAKE_PRNG
  98. #define LTC_NO_PK
  99. #define LTC_MRSA
  100. #define LTC_MECC
  101. #define LTC_NO_MISC
  102. #define LTC_BASE64
  103. #endif
  104. /* The minimal set of functionality to run the tests */
  105. #ifdef LTC_MINIMAL
  106. #define LTC_RIJNDAEL
  107. #define LTC_SHA256
  108. #define LTC_YARROW
  109. #define LTC_CTR_MODE
  110. #define LTC_RNG_MAKE_PRNG
  111. #define LTC_RNG_GET_BYTES
  112. #define LTC_DEVRANDOM
  113. #define LTC_TRY_URANDOM_FIRST
  114. #undef LTC_NO_FILE
  115. #endif
  116. /* Enable self-test test vector checking */
  117. #ifndef LTC_NO_TEST
  118. #define LTC_TEST
  119. #endif
  120. /* Enable extended self-tests */
  121. /* #define LTC_TEST_EXT */
  122. /* Use small code where possible */
  123. /* #define LTC_SMALL_CODE */
  124. /* clean the stack of functions which put private information on stack */
  125. /* #define LTC_CLEAN_STACK */
  126. /* disable all file related functions */
  127. /* #define LTC_NO_FILE */
  128. /* disable all forms of ASM */
  129. /* #define LTC_NO_ASM */
  130. /* disable FAST mode */
  131. /* #define LTC_NO_FAST */
  132. /* disable BSWAP on x86 */
  133. /* #define LTC_NO_BSWAP */
  134. /* ---> math provider? <--- */
  135. #ifndef LTC_NO_MATH
  136. /* LibTomMath */
  137. /* #define LTM_DESC */
  138. /* TomsFastMath */
  139. /* #define TFM_DESC */
  140. /* GNU Multiple Precision Arithmetic Library */
  141. /* #define GMP_DESC */
  142. #endif /* LTC_NO_MATH */
  143. /* ---> Symmetric Block Ciphers <--- */
  144. #ifndef LTC_NO_CIPHERS
  145. #define LTC_BLOWFISH
  146. #define LTC_RC2
  147. #define LTC_RC5
  148. #define LTC_RC6
  149. #define LTC_SAFERP
  150. #define LTC_RIJNDAEL
  151. #define LTC_XTEA
  152. /* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format
  153. * (saves 4KB of ram), _ALL_TABLES enables all tables during setup */
  154. #define LTC_TWOFISH
  155. #ifndef LTC_NO_TABLES
  156. #define LTC_TWOFISH_TABLES
  157. /* #define LTC_TWOFISH_ALL_TABLES */
  158. #else
  159. #define LTC_TWOFISH_SMALL
  160. #endif
  161. /* #define LTC_TWOFISH_SMALL */
  162. /* LTC_DES includes EDE triple-DES */
  163. #define LTC_DES
  164. #define LTC_CAST5
  165. #define LTC_NOEKEON
  166. #define LTC_SKIPJACK
  167. #define LTC_SAFER
  168. #define LTC_KHAZAD
  169. #define LTC_ANUBIS
  170. #define LTC_ANUBIS_TWEAK
  171. #define LTC_KSEED
  172. #define LTC_KASUMI
  173. #define LTC_MULTI2
  174. #define LTC_CAMELLIA
  175. /* stream ciphers */
  176. #define LTC_CHACHA
  177. #define LTC_RC4_STREAM
  178. #define LTC_SOBER128_STREAM
  179. #endif /* LTC_NO_CIPHERS */
  180. /* ---> Block Cipher Modes of Operation <--- */
  181. #ifndef LTC_NO_MODES
  182. #define LTC_CFB_MODE
  183. #define LTC_OFB_MODE
  184. #define LTC_ECB_MODE
  185. #define LTC_CBC_MODE
  186. #define LTC_CTR_MODE
  187. /* F8 chaining mode */
  188. #define LTC_F8_MODE
  189. /* LRW mode */
  190. #define LTC_LRW_MODE
  191. #ifndef LTC_NO_TABLES
  192. /* like GCM mode this will enable 16 8x128 tables [64KB] that make
  193. * seeking very fast.
  194. */
  195. #define LTC_LRW_TABLES
  196. #endif
  197. /* XTS mode */
  198. #define LTC_XTS_MODE
  199. #endif /* LTC_NO_MODES */
  200. /* ---> One-Way Hash Functions <--- */
  201. #ifndef LTC_NO_HASHES
  202. #define LTC_CHC_HASH
  203. #define LTC_WHIRLPOOL
  204. #define LTC_SHA3
  205. #define LTC_SHA512
  206. #define LTC_SHA512_256
  207. #define LTC_SHA512_224
  208. #define LTC_SHA384
  209. #define LTC_SHA256
  210. #define LTC_SHA224
  211. #define LTC_TIGER
  212. #define LTC_SHA1
  213. #define LTC_MD5
  214. #define LTC_MD4
  215. #define LTC_MD2
  216. #define LTC_RIPEMD128
  217. #define LTC_RIPEMD160
  218. #define LTC_RIPEMD256
  219. #define LTC_RIPEMD320
  220. #define LTC_BLAKE2S
  221. #define LTC_BLAKE2B
  222. #define LTC_HASH_HELPERS
  223. #endif /* LTC_NO_HASHES */
  224. /* ---> MAC functions <--- */
  225. #ifndef LTC_NO_MACS
  226. #define LTC_HMAC
  227. #define LTC_OMAC
  228. #define LTC_PMAC
  229. #define LTC_XCBC
  230. #define LTC_F9_MODE
  231. #define LTC_PELICAN
  232. #define LTC_POLY1305
  233. #define LTC_BLAKE2SMAC
  234. #define LTC_BLAKE2BMAC
  235. /* ---> Encrypt + Authenticate Modes <--- */
  236. #define LTC_EAX_MODE
  237. #define LTC_OCB_MODE
  238. #define LTC_OCB3_MODE
  239. #define LTC_CCM_MODE
  240. #define LTC_GCM_MODE
  241. #define LTC_CHACHA20POLY1305_MODE
  242. /* Use 64KiB tables */
  243. #ifndef LTC_NO_TABLES
  244. #define LTC_GCM_TABLES
  245. #endif
  246. /* USE SSE2? requires GCC works on x86_32 and x86_64*/
  247. #ifdef LTC_GCM_TABLES
  248. /* #define LTC_GCM_TABLES_SSE2 */
  249. #endif
  250. #endif /* LTC_NO_MACS */
  251. /* --> Pseudo Random Number Generators <--- */
  252. #ifndef LTC_NO_PRNGS
  253. /* Yarrow */
  254. #define LTC_YARROW
  255. /* a PRNG that simply reads from an available system source */
  256. #define LTC_SPRNG
  257. /* The RC4 stream cipher based PRNG */
  258. #define LTC_RC4
  259. /* The ChaCha20 stream cipher based PRNG */
  260. #define LTC_CHACHA20_PRNG
  261. /* Fortuna PRNG */
  262. #define LTC_FORTUNA
  263. /* Greg's SOBER128 stream cipher based PRNG */
  264. #define LTC_SOBER128
  265. /* the *nix style /dev/random device */
  266. #define LTC_DEVRANDOM
  267. /* try /dev/urandom before trying /dev/random
  268. * are you sure you want to disable this? http://www.2uo.de/myths-about-urandom/ */
  269. #define LTC_TRY_URANDOM_FIRST
  270. /* rng_get_bytes() */
  271. #define LTC_RNG_GET_BYTES
  272. /* rng_make_prng() */
  273. #define LTC_RNG_MAKE_PRNG
  274. /* enable the ltc_rng hook to integrate e.g. embedded hardware RNG's easily */
  275. /* #define LTC_PRNG_ENABLE_LTC_RNG */
  276. #endif /* LTC_NO_PRNGS */
  277. #ifdef LTC_YARROW
  278. /* which descriptor of AES to use? */
  279. /* 0 = rijndael_enc 1 = aes_enc, 2 = rijndael [full], 3 = aes [full] */
  280. #ifdef ENCRYPT_ONLY
  281. #define LTC_YARROW_AES 0
  282. #else
  283. #define LTC_YARROW_AES 2
  284. #endif
  285. #endif
  286. #ifdef LTC_FORTUNA
  287. #ifndef LTC_FORTUNA_WD
  288. /* reseed every N calls to the read function */
  289. #define LTC_FORTUNA_WD 10
  290. #endif
  291. #ifndef LTC_FORTUNA_POOLS
  292. /* number of pools (4..32) can save a bit of ram by lowering the count */
  293. #define LTC_FORTUNA_POOLS 32
  294. #endif
  295. #endif /* LTC_FORTUNA */
  296. /* ---> Public Key Crypto <--- */
  297. #ifndef LTC_NO_PK
  298. /* Include RSA support */
  299. #define LTC_MRSA
  300. /* Include Diffie-Hellman support */
  301. /* is_prime fails for GMP */
  302. #define LTC_MDH
  303. /* Supported Key Sizes */
  304. #define LTC_DH768
  305. #define LTC_DH1024
  306. #define LTC_DH1536
  307. #define LTC_DH2048
  308. #ifndef TFM_DESC
  309. /* tfm has a problem in fp_isprime for larger key sizes */
  310. #define LTC_DH3072
  311. #define LTC_DH4096
  312. #define LTC_DH6144
  313. #define LTC_DH8192
  314. #endif
  315. /* Include Katja (a Rabin variant like RSA) */
  316. /* #define LTC_MKAT */
  317. /* Digital Signature Algorithm */
  318. #define LTC_MDSA
  319. /* ECC */
  320. #define LTC_MECC
  321. /* use Shamir's trick for point mul (speeds up signature verification) */
  322. #define LTC_ECC_SHAMIR
  323. #if defined(TFM_DESC) && defined(LTC_MECC)
  324. #define LTC_MECC_ACCEL
  325. #endif
  326. /* do we want fixed point ECC */
  327. /* #define LTC_MECC_FP */
  328. #endif /* LTC_NO_PK */
  329. #if defined(LTC_MRSA) && !defined(LTC_NO_RSA_BLINDING)
  330. /* Enable RSA blinding when doing private key operations by default */
  331. #define LTC_RSA_BLINDING
  332. #endif /* LTC_NO_RSA_BLINDING */
  333. #if defined(LTC_MRSA) && !defined(LTC_NO_RSA_CRT_HARDENING)
  334. /* Enable RSA CRT hardening when doing private key operations by default */
  335. #define LTC_RSA_CRT_HARDENING
  336. #endif /* LTC_NO_RSA_CRT_HARDENING */
  337. #if defined(LTC_MECC) && !defined(LTC_NO_ECC_TIMING_RESISTANT)
  338. /* Enable ECC timing resistant version by default */
  339. #define LTC_ECC_TIMING_RESISTANT
  340. #endif
  341. /* define these PK sizes out of LTC_NO_PK
  342. * to have them always defined
  343. */
  344. #if defined(LTC_MRSA)
  345. /* Min and Max RSA key sizes (in bits) */
  346. #ifndef MIN_RSA_SIZE
  347. #define MIN_RSA_SIZE 1024
  348. #endif
  349. #ifndef MAX_RSA_SIZE
  350. #define MAX_RSA_SIZE 4096
  351. #endif
  352. #endif
  353. /* in cases where you want ASN.1/DER functionality, but no
  354. * RSA, you can define this externally if 1024 is not enough
  355. */
  356. #if defined(LTC_MRSA)
  357. #define LTC_DER_MAX_PUBKEY_SIZE MAX_RSA_SIZE
  358. #elif !defined(LTC_DER_MAX_PUBKEY_SIZE)
  359. /* this includes DSA */
  360. #define LTC_DER_MAX_PUBKEY_SIZE 1024
  361. #endif
  362. /* PKCS #1 (RSA) and #5 (Password Handling) stuff */
  363. #ifndef LTC_NO_PKCS
  364. #define LTC_PKCS_1
  365. #define LTC_PKCS_5
  366. /* Include ASN.1 DER (required by DSA/RSA) */
  367. #define LTC_DER
  368. #endif /* LTC_NO_PKCS */
  369. /* misc stuff */
  370. #ifndef LTC_NO_MISC
  371. /* Various tidbits of modern neatoness */
  372. #define LTC_BASE64
  373. /* ... and it's URL safe version */
  374. #define LTC_BASE64_URL
  375. /* Keep LTC_NO_HKDF for compatibility reasons
  376. * superseeded by LTC_NO_MISC*/
  377. #ifndef LTC_NO_HKDF
  378. /* HKDF Key Derivation/Expansion stuff */
  379. #define LTC_HKDF
  380. #endif /* LTC_NO_HKDF */
  381. #define LTC_ADLER32
  382. #define LTC_CRC32
  383. #endif /* LTC_NO_MISC */
  384. /* cleanup */
  385. #ifdef LTC_MECC
  386. /* Supported ECC Key Sizes */
  387. #ifndef LTC_NO_CURVES
  388. #define LTC_ECC112
  389. #define LTC_ECC128
  390. #define LTC_ECC160
  391. #define LTC_ECC192
  392. #define LTC_ECC224
  393. #define LTC_ECC256
  394. #define LTC_ECC384
  395. #define LTC_ECC521
  396. #endif
  397. #endif
  398. #if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_MKAT)
  399. /* Include the MPI functionality? (required by the PK algorithms) */
  400. #define LTC_MPI
  401. #ifndef LTC_PK_MAX_RETRIES
  402. /* iterations limit for retry-loops */
  403. #define LTC_PK_MAX_RETRIES 20
  404. #endif
  405. #endif
  406. #ifdef LTC_MRSA
  407. #define LTC_PKCS_1
  408. #endif
  409. #if defined(LTC_PELICAN) && !defined(LTC_RIJNDAEL)
  410. #error Pelican-MAC requires LTC_RIJNDAEL
  411. #endif
  412. #if defined(LTC_EAX_MODE) && !(defined(LTC_CTR_MODE) && defined(LTC_OMAC))
  413. #error LTC_EAX_MODE requires CTR and LTC_OMAC mode
  414. #endif
  415. #if defined(LTC_YARROW) && !defined(LTC_CTR_MODE)
  416. #error LTC_YARROW requires LTC_CTR_MODE chaining mode to be defined!
  417. #endif
  418. #if defined(LTC_DER) && !defined(LTC_MPI)
  419. #error ASN.1 DER requires MPI functionality
  420. #endif
  421. #if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC) || defined(LTC_MKAT)) && !defined(LTC_DER)
  422. #error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled
  423. #endif
  424. #if defined(LTC_CHACHA20POLY1305_MODE) && (!defined(LTC_CHACHA) || !defined(LTC_POLY1305))
  425. #error LTC_CHACHA20POLY1305_MODE requires LTC_CHACHA + LTC_POLY1305
  426. #endif
  427. #if defined(LTC_CHACHA20_PRNG) && !defined(LTC_CHACHA)
  428. #error LTC_CHACHA20_PRNG requires LTC_CHACHA
  429. #endif
  430. #if defined(LTC_RC4) && !defined(LTC_RC4_STREAM)
  431. #error LTC_RC4 requires LTC_RC4_STREAM
  432. #endif
  433. #if defined(LTC_SOBER128) && !defined(LTC_SOBER128_STREAM)
  434. #error LTC_SOBER128 requires LTC_SOBER128_STREAM
  435. #endif
  436. #if defined(LTC_BLAKE2SMAC) && !defined(LTC_BLAKE2S)
  437. #error LTC_BLAKE2SMAC requires LTC_BLAKE2S
  438. #endif
  439. #if defined(LTC_BLAKE2BMAC) && !defined(LTC_BLAKE2B)
  440. #error LTC_BLAKE2BMAC requires LTC_BLAKE2B
  441. #endif
  442. #if defined(LTC_SPRNG) && !defined(LTC_RNG_GET_BYTES)
  443. #error LTC_SPRNG requires LTC_RNG_GET_BYTES
  444. #endif
  445. #if defined(LTC_NO_MATH) && (defined(LTM_DESC) || defined(TFM_DESC) || defined(GMP_DESC))
  446. #error LTC_NO_MATH defined, but also a math descriptor
  447. #endif
  448. /* THREAD management */
  449. #ifdef LTC_PTHREAD
  450. #include <pthread.h>
  451. #define LTC_MUTEX_GLOBAL(x) pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER;
  452. #define LTC_MUTEX_PROTO(x) extern pthread_mutex_t x;
  453. #define LTC_MUTEX_TYPE(x) pthread_mutex_t x;
  454. #define LTC_MUTEX_INIT(x) LTC_ARGCHK(pthread_mutex_init(x, NULL) == 0);
  455. #define LTC_MUTEX_LOCK(x) LTC_ARGCHK(pthread_mutex_lock(x) == 0);
  456. #define LTC_MUTEX_UNLOCK(x) LTC_ARGCHK(pthread_mutex_unlock(x) == 0);
  457. #else
  458. /* default no functions */
  459. #define LTC_MUTEX_GLOBAL(x)
  460. #define LTC_MUTEX_PROTO(x)
  461. #define LTC_MUTEX_TYPE(x)
  462. #define LTC_MUTEX_INIT(x)
  463. #define LTC_MUTEX_LOCK(x)
  464. #define LTC_MUTEX_UNLOCK(x)
  465. #endif
  466. /* Debuggers */
  467. /* define this if you use Valgrind, note: it CHANGES the way SOBER-128 and RC4 work (see the code) */
  468. /* #define LTC_VALGRIND */
  469. #endif
  470. #ifndef LTC_NO_FILE
  471. /* buffer size for reading from a file via fread(..) */
  472. #ifndef LTC_FILE_READ_BUFSIZE
  473. #define LTC_FILE_READ_BUFSIZE 8192
  474. #endif
  475. #endif
  476. /* ref: $Format:%D$ */
  477. /* git commit: $Format:%H$ */
  478. /* commit time: $Format:%ai$ */