tomcrypt_custom.h 11 KB

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