cipher-internal.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* cipher-internal.h - Internal defs for cipher.c
  2. * Copyright (C) 2011 Free Software Foundation, Inc.
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef G10_CIPHER_INTERNAL_H
  20. #define G10_CIPHER_INTERNAL_H
  21. #include "./poly1305-internal.h"
  22. /* The maximum supported size of a block in bytes. */
  23. #define MAX_BLOCKSIZE 16
  24. /* The length for an OCB block. Although OCB supports any block
  25. length it does not make sense to use a 64 bit blocklen (and cipher)
  26. because this reduces the security margin to an unacceptable state.
  27. Thus we require a cipher with 128 bit blocklength. */
  28. #define OCB_BLOCK_LEN (128/8)
  29. /* The size of the pre-computed L table for OCB. This takes the same
  30. size as the table used for GCM and thus we don't save anything by
  31. not using such a table. */
  32. #define OCB_L_TABLE_SIZE 16
  33. /* Check the above constants. */
  34. #if OCB_BLOCK_LEN > MAX_BLOCKSIZE
  35. # error OCB_BLOCKLEN > MAX_BLOCKSIZE
  36. #endif
  37. /* Magic values for the context structure. */
  38. #define CTX_MAGIC_NORMAL 0x24091964
  39. #define CTX_MAGIC_SECURE 0x46919042
  40. /* Try to use 16 byte aligned cipher context for better performance.
  41. We use the aligned attribute, thus it is only possible to implement
  42. this with gcc. */
  43. #undef NEED_16BYTE_ALIGNED_CONTEXT
  44. #ifdef HAVE_GCC_ATTRIBUTE_ALIGNED
  45. # define NEED_16BYTE_ALIGNED_CONTEXT 1
  46. #endif
  47. /* Undef this symbol to trade GCM speed for 256 bytes of memory per context */
  48. #define GCM_USE_TABLES 1
  49. /* GCM_USE_INTEL_PCLMUL indicates whether to compile GCM with Intel PCLMUL
  50. code. */
  51. #undef GCM_USE_INTEL_PCLMUL
  52. #if defined(ENABLE_PCLMUL_SUPPORT) && defined(GCM_USE_TABLES)
  53. # if ((defined(__i386__) && SIZEOF_UNSIGNED_LONG == 4) || defined(__x86_64__))
  54. # if __GNUC__ >= 4
  55. # define GCM_USE_INTEL_PCLMUL 1
  56. # endif
  57. # endif
  58. #endif /* GCM_USE_INTEL_PCLMUL */
  59. /* GCM_USE_INTEL_VPCLMUL_AVX2 indicates whether to compile GCM with Intel
  60. VPCLMUL/AVX2 code. */
  61. #undef GCM_USE_INTEL_VPCLMUL_AVX2
  62. #if defined(__x86_64__) && defined(GCM_USE_INTEL_PCLMUL) && \
  63. defined(ENABLE_AVX2_SUPPORT) && defined(HAVE_GCC_INLINE_ASM_VAES_VPCLMUL)
  64. # define GCM_USE_INTEL_VPCLMUL_AVX2 1
  65. #endif /* GCM_USE_INTEL_VPCLMUL_AVX2 */
  66. /* GCM_USE_INTEL_VPCLMUL_AVX512 indicates whether to compile GCM with Intel
  67. VPCLMUL/AVX512 code. */
  68. #undef GCM_USE_INTEL_VPCLMUL_AVX512
  69. #if defined(__x86_64__) && defined(GCM_USE_INTEL_VPCLMUL_AVX2) && \
  70. defined(ENABLE_AVX512_SUPPORT) && defined(HAVE_GCC_INLINE_ASM_AVX512)
  71. # define GCM_USE_INTEL_VPCLMUL_AVX512 1
  72. #endif /* GCM_USE_INTEL_VPCLMUL_AVX512 */
  73. /* GCM_USE_ARM_PMULL indicates whether to compile GCM with ARMv8 PMULL code. */
  74. #undef GCM_USE_ARM_PMULL
  75. #if defined(ENABLE_ARM_CRYPTO_SUPPORT) && defined(GCM_USE_TABLES)
  76. # if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \
  77. && defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \
  78. && defined(HAVE_GCC_INLINE_ASM_AARCH32_CRYPTO)
  79. # define GCM_USE_ARM_PMULL 1
  80. # elif defined(__AARCH64EL__) && \
  81. defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) && \
  82. defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO)
  83. # define GCM_USE_ARM_PMULL 1
  84. # endif
  85. #endif /* GCM_USE_ARM_PMULL */
  86. /* GCM_USE_ARM_NEON indicates whether to compile GCM with ARMv7 NEON code. */
  87. #undef GCM_USE_ARM_NEON
  88. #if defined(GCM_USE_TABLES)
  89. #if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) && \
  90. defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) && \
  91. defined(HAVE_GCC_INLINE_ASM_NEON)
  92. # define GCM_USE_ARM_NEON 1
  93. #endif
  94. #endif /* GCM_USE_ARM_NEON */
  95. /* GCM_USE_S390X_CRYPTO indicates whether to enable zSeries code. */
  96. #undef GCM_USE_S390X_CRYPTO
  97. #if defined(HAVE_GCC_INLINE_ASM_S390X)
  98. # define GCM_USE_S390X_CRYPTO 1
  99. #endif /* GCM_USE_S390X_CRYPTO */
  100. /* GCM_USE_PPC_VPMSUM indicates whether to compile GCM with PPC Power 8
  101. * polynomial multiplication instruction. */
  102. #undef GCM_USE_PPC_VPMSUM
  103. #if defined(GCM_USE_TABLES)
  104. #if defined(ENABLE_PPC_CRYPTO_SUPPORT) && defined(__powerpc64__) && \
  105. defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \
  106. defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) && __GNUC__ >= 4
  107. # define GCM_USE_PPC_VPMSUM 1
  108. # define NEED_16BYTE_ALIGNED_CONTEXT 1 /* this also aligns gcm_table */
  109. #endif
  110. #endif /* GCM_USE_PPC_VPMSUM */
  111. typedef unsigned int (*ghash_fn_t) (gcry_cipher_hd_t c, byte *result,
  112. const byte *buf, size_t nblocks);
  113. /* A structure with function pointers for mode operations. */
  114. typedef struct cipher_mode_ops
  115. {
  116. gcry_err_code_t (*encrypt)(gcry_cipher_hd_t c, unsigned char *outbuf,
  117. size_t outbuflen, const unsigned char *inbuf,
  118. size_t inbuflen);
  119. gcry_err_code_t (*decrypt)(gcry_cipher_hd_t c, unsigned char *outbuf,
  120. size_t outbuflen, const unsigned char *inbuf,
  121. size_t inbuflen);
  122. gcry_err_code_t (*setiv)(gcry_cipher_hd_t c, const unsigned char *iv,
  123. size_t ivlen);
  124. gcry_err_code_t (*authenticate)(gcry_cipher_hd_t c,
  125. const unsigned char *abuf, size_t abuflen);
  126. gcry_err_code_t (*get_tag)(gcry_cipher_hd_t c, unsigned char *outtag,
  127. size_t taglen);
  128. gcry_err_code_t (*check_tag)(gcry_cipher_hd_t c, const unsigned char *intag,
  129. size_t taglen);
  130. } cipher_mode_ops_t;
  131. /* A structure with function pointers for bulk operations. The cipher
  132. algorithm setkey function initializes them when bulk operations are
  133. available and the actual encryption routines use them if they are
  134. not NULL. */
  135. typedef struct cipher_bulk_ops
  136. {
  137. void (*ecb_crypt)(void *context, void *outbuf_arg, const void *inbuf_arg,
  138. size_t nblocks, int encrypt);
  139. void (*cfb_enc)(void *context, unsigned char *iv, void *outbuf_arg,
  140. const void *inbuf_arg, size_t nblocks);
  141. void (*cfb_dec)(void *context, unsigned char *iv, void *outbuf_arg,
  142. const void *inbuf_arg, size_t nblocks);
  143. void (*cbc_enc)(void *context, unsigned char *iv, void *outbuf_arg,
  144. const void *inbuf_arg, size_t nblocks, int cbc_mac);
  145. void (*cbc_dec)(void *context, unsigned char *iv, void *outbuf_arg,
  146. const void *inbuf_arg, size_t nblocks);
  147. void (*ofb_enc)(void *context, unsigned char *iv, void *outbuf_arg,
  148. const void *inbuf_arg, size_t nblocks);
  149. void (*ctr_enc)(void *context, unsigned char *iv, void *outbuf_arg,
  150. const void *inbuf_arg, size_t nblocks);
  151. void (*ctr32le_enc)(void *context, unsigned char *iv, void *outbuf_arg,
  152. const void *inbuf_arg, size_t nblocks);
  153. size_t (*ocb_crypt)(gcry_cipher_hd_t c, void *outbuf_arg,
  154. const void *inbuf_arg, size_t nblocks, int encrypt);
  155. size_t (*ocb_auth)(gcry_cipher_hd_t c, const void *abuf_arg, size_t nblocks);
  156. void (*xts_crypt)(void *context, unsigned char *tweak, void *outbuf_arg,
  157. const void *inbuf_arg, size_t nblocks, int encrypt);
  158. size_t (*gcm_crypt)(gcry_cipher_hd_t c, void *outbuf_arg,
  159. const void *inbuf_arg, size_t nblocks, int encrypt);
  160. } cipher_bulk_ops_t;
  161. /* A VIA processor with the Padlock engine as well as the Intel AES_NI
  162. instructions require an alignment of most data on a 16 byte
  163. boundary. Because we trick out the compiler while allocating the
  164. context, the align attribute as used in rijndael.c does not work on
  165. its own. Thus we need to make sure that the entire context
  166. structure is a aligned on that boundary. We achieve this by
  167. defining a new type and use that instead of our usual alignment
  168. type. */
  169. typedef union
  170. {
  171. PROPERLY_ALIGNED_TYPE foo;
  172. #ifdef NEED_16BYTE_ALIGNED_CONTEXT
  173. char bar[16] __attribute__ ((aligned (16)));
  174. #endif
  175. char c[1];
  176. } cipher_context_alignment_t;
  177. /* Storage structure for CMAC, for CMAC and EAX modes. */
  178. typedef struct {
  179. /* The initialization vector. Also contains tag after finalization. */
  180. union {
  181. cipher_context_alignment_t iv_align;
  182. unsigned char iv[MAX_BLOCKSIZE];
  183. } u_iv;
  184. /* Subkeys for tag creation, not cleared by gcry_cipher_reset. */
  185. unsigned char subkeys[2][MAX_BLOCKSIZE];
  186. /* Space to save partial input lengths for MAC. */
  187. unsigned char macbuf[MAX_BLOCKSIZE];
  188. int mac_unused; /* Number of unprocessed bytes in MACBUF. */
  189. unsigned int tag:1; /* Set to 1 if tag has been finalized. */
  190. } gcry_cmac_context_t;
  191. /* The handle structure. */
  192. struct gcry_cipher_handle
  193. {
  194. int magic;
  195. size_t actual_handle_size; /* Allocated size of this handle. */
  196. size_t handle_offset; /* Offset to the malloced block. */
  197. gcry_cipher_spec_t *spec;
  198. /* The algorithm id. This is a hack required because the module
  199. interface does not easily allow to retrieve this value. */
  200. int algo;
  201. /* A structure with function pointers for mode operations. */
  202. cipher_mode_ops_t mode_ops;
  203. /* A structure with function pointers for bulk operations. Due to
  204. limitations of the module system (we don't want to change the
  205. API) we need to keep these function pointers here. */
  206. cipher_bulk_ops_t bulk;
  207. int mode;
  208. unsigned int flags;
  209. struct {
  210. int geniv_method;
  211. unsigned char fixed[MAX_BLOCKSIZE];
  212. unsigned char dynamic[MAX_BLOCKSIZE];
  213. size_t fixed_iv_len;
  214. size_t dynamic_iv_len;
  215. } aead;
  216. struct {
  217. unsigned int key:1; /* Set to 1 if a key has been set. */
  218. unsigned int iv:1; /* Set to 1 if a IV has been set. */
  219. unsigned int tag:1; /* Set to 1 if a tag is finalized. */
  220. unsigned int finalize:1; /* Next encrypt/decrypt has the final data. */
  221. unsigned int allow_weak_key:1; /* Set to 1 if weak keys are allowed. */
  222. } marks;
  223. /* The initialization vector. For best performance we make sure
  224. that it is properly aligned. In particular some implementations
  225. of bulk operations expect an 16 byte aligned IV. IV is also used
  226. to store CBC-MAC in CCM mode; counter IV is stored in U_CTR. For
  227. OCB mode it is used for the offset value. */
  228. union {
  229. cipher_context_alignment_t iv_align;
  230. unsigned char iv[MAX_BLOCKSIZE];
  231. } u_iv;
  232. /* The counter for CTR mode. This field is also used by AESWRAP and
  233. thus we can't use the U_IV union. For OCB mode it is used for
  234. the checksum. */
  235. union {
  236. cipher_context_alignment_t iv_align;
  237. unsigned char ctr[MAX_BLOCKSIZE];
  238. } u_ctr;
  239. /* Space to save an IV or CTR for chaining operations. */
  240. unsigned char lastiv[MAX_BLOCKSIZE];
  241. int unused; /* Number of unused bytes in LASTIV. */
  242. union {
  243. /* Mode specific storage for CCM mode. */
  244. struct {
  245. u64 encryptlen;
  246. u64 aadlen;
  247. unsigned int authlen;
  248. /* Space to save partial input lengths for MAC. */
  249. unsigned char macbuf[GCRY_CCM_BLOCK_LEN];
  250. int mac_unused; /* Number of unprocessed bytes in MACBUF. */
  251. unsigned char s0[GCRY_CCM_BLOCK_LEN];
  252. unsigned int nonce:1; /* Set to 1 if nonce has been set. */
  253. unsigned int lengths:1; /* Set to 1 if CCM length parameters has been
  254. processed. */
  255. } ccm;
  256. /* Mode specific storage for Poly1305 mode. */
  257. struct {
  258. /* byte counter for AAD. */
  259. u32 aadcount[2];
  260. /* byte counter for data. */
  261. u32 datacount[2];
  262. unsigned int aad_finalized:1;
  263. unsigned int bytecount_over_limits:1;
  264. poly1305_context_t ctx;
  265. } poly1305;
  266. /* Mode specific storage for CMAC mode. */
  267. gcry_cmac_context_t cmac;
  268. /* Mode specific storage for EAX mode. */
  269. struct {
  270. /* CMAC for header (AAD). */
  271. gcry_cmac_context_t cmac_header;
  272. /* CMAC for ciphertext. */
  273. gcry_cmac_context_t cmac_ciphertext;
  274. } eax;
  275. /* Mode specific storage for GCM mode and GCM-SIV mode. */
  276. struct {
  277. /* The interim tag for GCM mode. */
  278. union {
  279. cipher_context_alignment_t iv_align;
  280. unsigned char tag[MAX_BLOCKSIZE];
  281. } u_tag;
  282. /* Space to save partial input lengths for MAC. */
  283. unsigned char macbuf[GCRY_CCM_BLOCK_LEN];
  284. int mac_unused; /* Number of unprocessed bytes in MACBUF. */
  285. /* byte counters for GCM */
  286. u32 aadlen[2];
  287. u32 datalen[2];
  288. /* encrypted tag counter */
  289. unsigned char tagiv[MAX_BLOCKSIZE];
  290. unsigned int ghash_data_finalized:1;
  291. unsigned int ghash_aad_finalized:1;
  292. unsigned int datalen_over_limits:1;
  293. unsigned int disallow_encryption_because_of_setiv_in_fips_mode:1;
  294. /* --- Following members are not cleared in gcry_cipher_reset --- */
  295. /* GHASH multiplier from key. */
  296. union {
  297. cipher_context_alignment_t iv_align;
  298. unsigned char key[MAX_BLOCKSIZE];
  299. } u_ghash_key;
  300. /* Pre-calculated table for GCM. */
  301. #ifdef GCM_USE_TABLES
  302. #if (SIZEOF_UNSIGNED_LONG == 8 || defined(__x86_64__))
  303. #define GCM_TABLES_USE_U64 1
  304. u64 gcm_table[4 * 16];
  305. #else
  306. #undef GCM_TABLES_USE_U64
  307. u32 gcm_table[8 * 16];
  308. #endif
  309. #endif
  310. /* GHASH implementation in use. */
  311. ghash_fn_t ghash_fn;
  312. /* POLYVAL implementation in use (GCM-SIV). */
  313. ghash_fn_t polyval_fn;
  314. /* Key length used for GCM-SIV key generating key. */
  315. unsigned int siv_keylen;
  316. /* Flags for accelerated implementations. */
  317. unsigned int hw_impl_flags;
  318. } gcm;
  319. /* Mode specific storage for OCB mode. */
  320. struct {
  321. /* --- Following members are not cleared in gcry_cipher_reset --- */
  322. /* Helper variables and pre-computed table of L values. */
  323. unsigned char L_star[OCB_BLOCK_LEN];
  324. unsigned char L_dollar[OCB_BLOCK_LEN];
  325. unsigned char L0L1[OCB_BLOCK_LEN];
  326. unsigned char L[OCB_L_TABLE_SIZE][OCB_BLOCK_LEN];
  327. /* --- Following members are cleared in gcry_cipher_reset --- */
  328. /* The tag is valid if marks.tag has been set. */
  329. unsigned char tag[OCB_BLOCK_LEN];
  330. /* A buffer to hold the offset for the AAD processing. */
  331. unsigned char aad_offset[OCB_BLOCK_LEN];
  332. /* A buffer to hold the current sum of AAD processing. We can't
  333. use tag here because tag may already hold the preprocessed
  334. checksum of the data. */
  335. unsigned char aad_sum[OCB_BLOCK_LEN];
  336. /* A buffer to store AAD data not yet processed. */
  337. unsigned char aad_leftover[OCB_BLOCK_LEN];
  338. /* Number of data/aad blocks processed so far. */
  339. u64 data_nblocks;
  340. u64 aad_nblocks;
  341. /* Number of valid bytes in AAD_LEFTOVER. */
  342. unsigned char aad_nleftover;
  343. /* Length of the tag. Fixed for now but may eventually be
  344. specified using a set of gcry_cipher_flags. */
  345. unsigned char taglen;
  346. /* Flags indicating that the final data/aad block has been
  347. processed. */
  348. unsigned int data_finalized:1;
  349. unsigned int aad_finalized:1;
  350. } ocb;
  351. /* Mode specific storage for XTS mode. */
  352. struct {
  353. /* Pointer to tweak cipher context, allocated after actual
  354. * cipher context. */
  355. char *tweak_context;
  356. } xts;
  357. /* Mode specific storage for SIV mode. */
  358. struct {
  359. /* Tag used for decryption. */
  360. unsigned char dec_tag[GCRY_SIV_BLOCK_LEN];
  361. /* S2V state. */
  362. unsigned char s2v_d[GCRY_SIV_BLOCK_LEN];
  363. /* Number of AAD elements processed. */
  364. unsigned int aad_count:8;
  365. /* Flags for SIV state. */
  366. unsigned int dec_tag_set:1;
  367. /* --- Following members are not cleared in gcry_cipher_reset --- */
  368. /* S2V CMAC state. */
  369. gcry_cmac_context_t s2v_cmac;
  370. unsigned char s2v_zero_block[GCRY_SIV_BLOCK_LEN];
  371. /* Pointer to CTR cipher context, allocated after actual
  372. * cipher context. */
  373. char *ctr_context;
  374. } siv;
  375. /* Mode specific storage for WRAP mode. */
  376. struct {
  377. unsigned char plen[4];
  378. } wrap;
  379. } u_mode;
  380. /* What follows are two contexts of the cipher in use. The first
  381. one needs to be aligned well enough for the cipher operation
  382. whereas the second one is a copy created by cipher_setkey and
  383. used by cipher_reset. That second copy has no need for proper
  384. aligment because it is only accessed by memcpy. */
  385. cipher_context_alignment_t context;
  386. };
  387. /*-- cipher-cbc.c --*/
  388. gcry_err_code_t _gcry_cipher_cbc_encrypt
  389. /* */ (gcry_cipher_hd_t c,
  390. unsigned char *outbuf, size_t outbuflen,
  391. const unsigned char *inbuf, size_t inbuflen);
  392. gcry_err_code_t _gcry_cipher_cbc_decrypt
  393. /* */ (gcry_cipher_hd_t c,
  394. unsigned char *outbuf, size_t outbuflen,
  395. const unsigned char *inbuf, size_t inbuflen);
  396. gcry_err_code_t _gcry_cipher_cbc_cts_encrypt
  397. /* */ (gcry_cipher_hd_t c,
  398. unsigned char *outbuf, size_t outbuflen,
  399. const unsigned char *inbuf, size_t inbuflen);
  400. gcry_err_code_t _gcry_cipher_cbc_cts_decrypt
  401. /* */ (gcry_cipher_hd_t c,
  402. unsigned char *outbuf, size_t outbuflen,
  403. const unsigned char *inbuf, size_t inbuflen);
  404. /*-- cipher-cfb.c --*/
  405. gcry_err_code_t _gcry_cipher_cfb_encrypt
  406. /* */ (gcry_cipher_hd_t c,
  407. unsigned char *outbuf, size_t outbuflen,
  408. const unsigned char *inbuf, size_t inbuflen);
  409. gcry_err_code_t _gcry_cipher_cfb_decrypt
  410. /* */ (gcry_cipher_hd_t c,
  411. unsigned char *outbuf, size_t outbuflen,
  412. const unsigned char *inbuf, size_t inbuflen);
  413. gcry_err_code_t _gcry_cipher_cfb8_encrypt
  414. /* */ (gcry_cipher_hd_t c,
  415. unsigned char *outbuf, size_t outbuflen,
  416. const unsigned char *inbuf, size_t inbuflen);
  417. gcry_err_code_t _gcry_cipher_cfb8_decrypt
  418. /* */ (gcry_cipher_hd_t c,
  419. unsigned char *outbuf, size_t outbuflen,
  420. const unsigned char *inbuf, size_t inbuflen);
  421. /*-- cipher-ofb.c --*/
  422. gcry_err_code_t _gcry_cipher_ofb_encrypt
  423. /* */ (gcry_cipher_hd_t c,
  424. unsigned char *outbuf, size_t outbuflen,
  425. const unsigned char *inbuf, size_t inbuflen);
  426. /*-- cipher-ctr.c --*/
  427. gcry_err_code_t _gcry_cipher_ctr_encrypt_ctx
  428. /* */ (gcry_cipher_hd_t c,
  429. unsigned char *outbuf, size_t outbuflen,
  430. const unsigned char *inbuf, size_t inbuflen,
  431. void *algo_context);
  432. gcry_err_code_t _gcry_cipher_ctr_encrypt
  433. /* */ (gcry_cipher_hd_t c,
  434. unsigned char *outbuf, size_t outbuflen,
  435. const unsigned char *inbuf, size_t inbuflen);
  436. /*-- cipher-aeswrap.c --*/
  437. gcry_err_code_t _gcry_cipher_keywrap_encrypt
  438. /* */ (gcry_cipher_hd_t c,
  439. byte *outbuf, size_t outbuflen,
  440. const byte *inbuf, size_t inbuflen);
  441. gcry_err_code_t _gcry_cipher_keywrap_encrypt_padding
  442. /* */ (gcry_cipher_hd_t c,
  443. byte *outbuf, size_t outbuflen,
  444. const byte *inbuf, size_t inbuflen);
  445. gcry_err_code_t _gcry_cipher_keywrap_decrypt_auto
  446. /* */ (gcry_cipher_hd_t c,
  447. byte *outbuf, size_t outbuflen,
  448. const byte *inbuf, size_t inbuflen);
  449. /*-- cipher-ccm.c --*/
  450. gcry_err_code_t _gcry_cipher_ccm_encrypt
  451. /* */ (gcry_cipher_hd_t c,
  452. unsigned char *outbuf, size_t outbuflen,
  453. const unsigned char *inbuf, size_t inbuflen);
  454. gcry_err_code_t _gcry_cipher_ccm_decrypt
  455. /* */ (gcry_cipher_hd_t c,
  456. unsigned char *outbuf, size_t outbuflen,
  457. const unsigned char *inbuf, size_t inbuflen);
  458. gcry_err_code_t _gcry_cipher_ccm_set_nonce
  459. /* */ (gcry_cipher_hd_t c, const unsigned char *nonce,
  460. size_t noncelen);
  461. gcry_err_code_t _gcry_cipher_ccm_authenticate
  462. /* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen);
  463. gcry_err_code_t _gcry_cipher_ccm_set_lengths
  464. /* */ (gcry_cipher_hd_t c, u64 encryptedlen, u64 aadlen, u64 taglen);
  465. gcry_err_code_t _gcry_cipher_ccm_get_tag
  466. /* */ (gcry_cipher_hd_t c,
  467. unsigned char *outtag, size_t taglen);
  468. gcry_err_code_t _gcry_cipher_ccm_check_tag
  469. /* */ (gcry_cipher_hd_t c,
  470. const unsigned char *intag, size_t taglen);
  471. /*-- cipher-cmac.c --*/
  472. gcry_err_code_t _gcry_cmac_generate_subkeys
  473. /* */ (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx);
  474. gcry_err_code_t _gcry_cmac_write
  475. /* */ (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx,
  476. const byte * inbuf, size_t inlen);
  477. gcry_err_code_t _gcry_cmac_final
  478. /* */ (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx);
  479. void _gcry_cmac_reset (gcry_cmac_context_t *ctx);
  480. /*-- cipher-eax.c --*/
  481. gcry_err_code_t _gcry_cipher_eax_encrypt
  482. /* */ (gcry_cipher_hd_t c,
  483. unsigned char *outbuf, size_t outbuflen,
  484. const unsigned char *inbuf, size_t inbuflen);
  485. gcry_err_code_t _gcry_cipher_eax_decrypt
  486. /* */ (gcry_cipher_hd_t c,
  487. unsigned char *outbuf, size_t outbuflen,
  488. const unsigned char *inbuf, size_t inbuflen);
  489. gcry_err_code_t _gcry_cipher_eax_set_nonce
  490. /* */ (gcry_cipher_hd_t c,
  491. const unsigned char *nonce, size_t noncelen);
  492. gcry_err_code_t _gcry_cipher_eax_authenticate
  493. /* */ (gcry_cipher_hd_t c,
  494. const unsigned char *aadbuf, size_t aadbuflen);
  495. gcry_err_code_t _gcry_cipher_eax_get_tag
  496. /* */ (gcry_cipher_hd_t c,
  497. unsigned char *outtag, size_t taglen);
  498. gcry_err_code_t _gcry_cipher_eax_check_tag
  499. /* */ (gcry_cipher_hd_t c,
  500. const unsigned char *intag, size_t taglen);
  501. gcry_err_code_t _gcry_cipher_eax_setkey
  502. /* */ (gcry_cipher_hd_t c);
  503. /*-- cipher-gcm.c --*/
  504. gcry_err_code_t _gcry_cipher_gcm_encrypt
  505. /* */ (gcry_cipher_hd_t c,
  506. unsigned char *outbuf, size_t outbuflen,
  507. const unsigned char *inbuf, size_t inbuflen);
  508. gcry_err_code_t _gcry_cipher_gcm_decrypt
  509. /* */ (gcry_cipher_hd_t c,
  510. unsigned char *outbuf, size_t outbuflen,
  511. const unsigned char *inbuf, size_t inbuflen);
  512. gcry_err_code_t _gcry_cipher_gcm_setiv
  513. /* */ (gcry_cipher_hd_t c,
  514. const unsigned char *iv, size_t ivlen);
  515. gcry_err_code_t _gcry_cipher_gcm_authenticate
  516. /* */ (gcry_cipher_hd_t c,
  517. const unsigned char *aadbuf, size_t aadbuflen);
  518. gcry_err_code_t _gcry_cipher_gcm_get_tag
  519. /* */ (gcry_cipher_hd_t c,
  520. unsigned char *outtag, size_t taglen);
  521. gcry_err_code_t _gcry_cipher_gcm_check_tag
  522. /* */ (gcry_cipher_hd_t c,
  523. const unsigned char *intag, size_t taglen);
  524. void _gcry_cipher_gcm_setkey
  525. /* */ (gcry_cipher_hd_t c);
  526. void _gcry_cipher_gcm_setupM
  527. /* */ (gcry_cipher_hd_t c);
  528. /*-- cipher-poly1305.c --*/
  529. gcry_err_code_t _gcry_cipher_poly1305_encrypt
  530. /* */ (gcry_cipher_hd_t c,
  531. unsigned char *outbuf, size_t outbuflen,
  532. const unsigned char *inbuf, size_t inbuflen);
  533. gcry_err_code_t _gcry_cipher_poly1305_decrypt
  534. /* */ (gcry_cipher_hd_t c,
  535. unsigned char *outbuf, size_t outbuflen,
  536. const unsigned char *inbuf, size_t inbuflen);
  537. gcry_err_code_t _gcry_cipher_poly1305_setiv
  538. /* */ (gcry_cipher_hd_t c,
  539. const unsigned char *iv, size_t ivlen);
  540. gcry_err_code_t _gcry_cipher_poly1305_authenticate
  541. /* */ (gcry_cipher_hd_t c,
  542. const unsigned char *aadbuf, size_t aadbuflen);
  543. gcry_err_code_t _gcry_cipher_poly1305_get_tag
  544. /* */ (gcry_cipher_hd_t c,
  545. unsigned char *outtag, size_t taglen);
  546. gcry_err_code_t _gcry_cipher_poly1305_check_tag
  547. /* */ (gcry_cipher_hd_t c,
  548. const unsigned char *intag, size_t taglen);
  549. void _gcry_cipher_poly1305_setkey
  550. /* */ (gcry_cipher_hd_t c);
  551. /*-- chacha20.c --*/
  552. gcry_err_code_t _gcry_chacha20_poly1305_encrypt
  553. /* */ (gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
  554. size_t length);
  555. gcry_err_code_t _gcry_chacha20_poly1305_decrypt
  556. /* */ (gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
  557. size_t length);
  558. /*-- cipher-ocb.c --*/
  559. gcry_err_code_t _gcry_cipher_ocb_encrypt
  560. /* */ (gcry_cipher_hd_t c,
  561. unsigned char *outbuf, size_t outbuflen,
  562. const unsigned char *inbuf, size_t inbuflen);
  563. gcry_err_code_t _gcry_cipher_ocb_decrypt
  564. /* */ (gcry_cipher_hd_t c,
  565. unsigned char *outbuf, size_t outbuflen,
  566. const unsigned char *inbuf, size_t inbuflen);
  567. gcry_err_code_t _gcry_cipher_ocb_set_nonce
  568. /* */ (gcry_cipher_hd_t c, const unsigned char *nonce,
  569. size_t noncelen);
  570. gcry_err_code_t _gcry_cipher_ocb_authenticate
  571. /* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen);
  572. gcry_err_code_t _gcry_cipher_ocb_get_tag
  573. /* */ (gcry_cipher_hd_t c,
  574. unsigned char *outtag, size_t taglen);
  575. gcry_err_code_t _gcry_cipher_ocb_check_tag
  576. /* */ (gcry_cipher_hd_t c,
  577. const unsigned char *intag, size_t taglen);
  578. void _gcry_cipher_ocb_setkey
  579. /* */ (gcry_cipher_hd_t c);
  580. /*-- cipher-xts.c --*/
  581. gcry_err_code_t _gcry_cipher_xts_encrypt
  582. /* */ (gcry_cipher_hd_t c, unsigned char *outbuf, size_t outbuflen,
  583. const unsigned char *inbuf, size_t inbuflen);
  584. gcry_err_code_t _gcry_cipher_xts_decrypt
  585. /* */ (gcry_cipher_hd_t c, unsigned char *outbuf, size_t outbuflen,
  586. const unsigned char *inbuf, size_t inbuflen);
  587. /*-- cipher-siv.c --*/
  588. gcry_err_code_t _gcry_cipher_siv_encrypt
  589. /* */ (gcry_cipher_hd_t c,
  590. unsigned char *outbuf, size_t outbuflen,
  591. const unsigned char *inbuf, size_t inbuflen);
  592. gcry_err_code_t _gcry_cipher_siv_decrypt
  593. /* */ (gcry_cipher_hd_t c,
  594. unsigned char *outbuf, size_t outbuflen,
  595. const unsigned char *inbuf, size_t inbuflen);
  596. gcry_err_code_t _gcry_cipher_siv_set_nonce
  597. /* */ (gcry_cipher_hd_t c, const unsigned char *nonce,
  598. size_t noncelen);
  599. gcry_err_code_t _gcry_cipher_siv_authenticate
  600. /* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen);
  601. gcry_err_code_t _gcry_cipher_siv_set_decryption_tag
  602. /* */ (gcry_cipher_hd_t c, const byte *tag, size_t taglen);
  603. gcry_err_code_t _gcry_cipher_siv_get_tag
  604. /* */ (gcry_cipher_hd_t c,
  605. unsigned char *outtag, size_t taglen);
  606. gcry_err_code_t _gcry_cipher_siv_check_tag
  607. /* */ (gcry_cipher_hd_t c,
  608. const unsigned char *intag, size_t taglen);
  609. gcry_err_code_t _gcry_cipher_siv_setkey
  610. /* */ (gcry_cipher_hd_t c,
  611. const unsigned char *ctrkey, size_t ctrkeylen);
  612. /*-- cipher-gcm-siv.c --*/
  613. gcry_err_code_t _gcry_cipher_gcm_siv_encrypt
  614. /* */ (gcry_cipher_hd_t c,
  615. unsigned char *outbuf, size_t outbuflen,
  616. const unsigned char *inbuf, size_t inbuflen);
  617. gcry_err_code_t _gcry_cipher_gcm_siv_decrypt
  618. /* */ (gcry_cipher_hd_t c,
  619. unsigned char *outbuf, size_t outbuflen,
  620. const unsigned char *inbuf, size_t inbuflen);
  621. gcry_err_code_t _gcry_cipher_gcm_siv_set_nonce
  622. /* */ (gcry_cipher_hd_t c, const unsigned char *nonce,
  623. size_t noncelen);
  624. gcry_err_code_t _gcry_cipher_gcm_siv_authenticate
  625. /* */ (gcry_cipher_hd_t c, const unsigned char *abuf, size_t abuflen);
  626. gcry_err_code_t _gcry_cipher_gcm_siv_set_decryption_tag
  627. /* */ (gcry_cipher_hd_t c, const byte *tag, size_t taglen);
  628. gcry_err_code_t _gcry_cipher_gcm_siv_get_tag
  629. /* */ (gcry_cipher_hd_t c,
  630. unsigned char *outtag, size_t taglen);
  631. gcry_err_code_t _gcry_cipher_gcm_siv_check_tag
  632. /* */ (gcry_cipher_hd_t c,
  633. const unsigned char *intag, size_t taglen);
  634. gcry_err_code_t _gcry_cipher_gcm_siv_setkey
  635. /* */ (gcry_cipher_hd_t c, unsigned int keylen);
  636. /* Return the L-value for block N. Note: 'cipher_ocb.c' ensures that N
  637. * will never be multiple of 65536 (1 << OCB_L_TABLE_SIZE), thus N can
  638. * be directly passed to _gcry_ctz() function and resulting index will
  639. * never overflow the table. */
  640. static inline const unsigned char *
  641. ocb_get_l (gcry_cipher_hd_t c, u64 n)
  642. {
  643. unsigned long ntz;
  644. #if ((defined(__i386__) || defined(__x86_64__)) && __GNUC__ >= 4)
  645. /* Assumes that N != 0. */
  646. asm ("rep;bsfl %k[low], %k[ntz]\n\t"
  647. : [ntz] "=r" (ntz)
  648. : [low] "r" ((unsigned long)n)
  649. : "cc");
  650. #else
  651. ntz = _gcry_ctz (n);
  652. #endif
  653. return c->u_mode.ocb.L[ntz];
  654. }
  655. /* Return bit-shift of blocksize. */
  656. static inline unsigned int _gcry_blocksize_shift(gcry_cipher_hd_t c)
  657. {
  658. /* Only blocksizes 8 and 16 are used. Return value in such way
  659. * that compiler can optimize calling functions based on this. */
  660. return c->spec->blocksize == 8 ? 3 : 4;
  661. }
  662. /* Optimized function for adding value to cipher block. */
  663. static inline void
  664. cipher_block_add(void *_dstsrc, unsigned int add, size_t blocksize)
  665. {
  666. byte *dstsrc = _dstsrc;
  667. u64 s[2];
  668. if (blocksize == 8)
  669. {
  670. buf_put_be64(dstsrc + 0, buf_get_be64(dstsrc + 0) + add);
  671. }
  672. else /* blocksize == 16 */
  673. {
  674. s[0] = buf_get_be64(dstsrc + 8);
  675. s[1] = buf_get_be64(dstsrc + 0);
  676. s[0] += add;
  677. s[1] += (s[0] < add);
  678. buf_put_be64(dstsrc + 8, s[0]);
  679. buf_put_be64(dstsrc + 0, s[1]);
  680. }
  681. }
  682. /* Optimized function for cipher block copying */
  683. static inline void
  684. cipher_block_cpy(void *_dst, const void *_src, size_t blocksize)
  685. {
  686. byte *dst = _dst;
  687. const byte *src = _src;
  688. u64 s[2];
  689. if (blocksize == 8)
  690. {
  691. buf_put_he64(dst + 0, buf_get_he64(src + 0));
  692. }
  693. else /* blocksize == 16 */
  694. {
  695. s[0] = buf_get_he64(src + 0);
  696. s[1] = buf_get_he64(src + 8);
  697. buf_put_he64(dst + 0, s[0]);
  698. buf_put_he64(dst + 8, s[1]);
  699. }
  700. }
  701. /* Optimized function for cipher block xoring */
  702. static inline void
  703. cipher_block_xor(void *_dst, const void *_src1, const void *_src2,
  704. size_t blocksize)
  705. {
  706. byte *dst = _dst;
  707. const byte *src1 = _src1;
  708. const byte *src2 = _src2;
  709. u64 s1[2];
  710. u64 s2[2];
  711. if (blocksize == 8)
  712. {
  713. buf_put_he64(dst + 0, buf_get_he64(src1 + 0) ^ buf_get_he64(src2 + 0));
  714. }
  715. else /* blocksize == 16 */
  716. {
  717. s1[0] = buf_get_he64(src1 + 0);
  718. s1[1] = buf_get_he64(src1 + 8);
  719. s2[0] = buf_get_he64(src2 + 0);
  720. s2[1] = buf_get_he64(src2 + 8);
  721. buf_put_he64(dst + 0, s1[0] ^ s2[0]);
  722. buf_put_he64(dst + 8, s1[1] ^ s2[1]);
  723. }
  724. }
  725. /* Optimized function for in-place cipher block xoring */
  726. static inline void
  727. cipher_block_xor_1(void *_dst, const void *_src, size_t blocksize)
  728. {
  729. cipher_block_xor (_dst, _dst, _src, blocksize);
  730. }
  731. /* Optimized function for cipher block xoring with two destination cipher
  732. blocks. Used mainly by CFB mode encryption. */
  733. static inline void
  734. cipher_block_xor_2dst(void *_dst1, void *_dst2, const void *_src,
  735. size_t blocksize)
  736. {
  737. byte *dst1 = _dst1;
  738. byte *dst2 = _dst2;
  739. const byte *src = _src;
  740. u64 d2[2];
  741. u64 s[2];
  742. if (blocksize == 8)
  743. {
  744. d2[0] = buf_get_he64(dst2 + 0) ^ buf_get_he64(src + 0);
  745. buf_put_he64(dst2 + 0, d2[0]);
  746. buf_put_he64(dst1 + 0, d2[0]);
  747. }
  748. else /* blocksize == 16 */
  749. {
  750. s[0] = buf_get_he64(src + 0);
  751. s[1] = buf_get_he64(src + 8);
  752. d2[0] = buf_get_he64(dst2 + 0);
  753. d2[1] = buf_get_he64(dst2 + 8);
  754. d2[0] = d2[0] ^ s[0];
  755. d2[1] = d2[1] ^ s[1];
  756. buf_put_he64(dst2 + 0, d2[0]);
  757. buf_put_he64(dst2 + 8, d2[1]);
  758. buf_put_he64(dst1 + 0, d2[0]);
  759. buf_put_he64(dst1 + 8, d2[1]);
  760. }
  761. }
  762. /* Optimized function for combined cipher block xoring and copying.
  763. Used by mainly CBC mode decryption. */
  764. static inline void
  765. cipher_block_xor_n_copy_2(void *_dst_xor, const void *_src_xor,
  766. void *_srcdst_cpy, const void *_src_cpy,
  767. size_t blocksize)
  768. {
  769. byte *dst_xor = _dst_xor;
  770. byte *srcdst_cpy = _srcdst_cpy;
  771. const byte *src_xor = _src_xor;
  772. const byte *src_cpy = _src_cpy;
  773. u64 sc[2];
  774. u64 sx[2];
  775. u64 sdc[2];
  776. if (blocksize == 8)
  777. {
  778. sc[0] = buf_get_he64(src_cpy + 0);
  779. buf_put_he64(dst_xor + 0,
  780. buf_get_he64(srcdst_cpy + 0) ^ buf_get_he64(src_xor + 0));
  781. buf_put_he64(srcdst_cpy + 0, sc[0]);
  782. }
  783. else /* blocksize == 16 */
  784. {
  785. sc[0] = buf_get_he64(src_cpy + 0);
  786. sc[1] = buf_get_he64(src_cpy + 8);
  787. sx[0] = buf_get_he64(src_xor + 0);
  788. sx[1] = buf_get_he64(src_xor + 8);
  789. sdc[0] = buf_get_he64(srcdst_cpy + 0);
  790. sdc[1] = buf_get_he64(srcdst_cpy + 8);
  791. sx[0] ^= sdc[0];
  792. sx[1] ^= sdc[1];
  793. buf_put_he64(dst_xor + 0, sx[0]);
  794. buf_put_he64(dst_xor + 8, sx[1]);
  795. buf_put_he64(srcdst_cpy + 0, sc[0]);
  796. buf_put_he64(srcdst_cpy + 8, sc[1]);
  797. }
  798. }
  799. /* Optimized function for combined cipher block byte-swapping. */
  800. static inline void
  801. cipher_block_bswap (void *_dst_bswap, const void *_src_bswap,
  802. size_t blocksize)
  803. {
  804. byte *dst_bswap = _dst_bswap;
  805. const byte *src_bswap = _src_bswap;
  806. u64 t[2];
  807. if (blocksize == 8)
  808. {
  809. buf_put_le64(dst_bswap, buf_get_be64(src_bswap));
  810. }
  811. else
  812. {
  813. t[0] = buf_get_be64(src_bswap + 0);
  814. t[1] = buf_get_be64(src_bswap + 8);
  815. buf_put_le64(dst_bswap + 8, t[0]);
  816. buf_put_le64(dst_bswap + 0, t[1]);
  817. }
  818. }
  819. /* Optimized function for combined cipher block xoring and copying.
  820. Used by mainly CFB mode decryption. */
  821. static inline void
  822. cipher_block_xor_n_copy(void *_dst_xor, void *_srcdst_cpy, const void *_src,
  823. size_t blocksize)
  824. {
  825. cipher_block_xor_n_copy_2(_dst_xor, _src, _srcdst_cpy, _src, blocksize);
  826. }
  827. #endif /*G10_CIPHER_INTERNAL_H*/