crypto.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. * Scatterlist Cryptographic API.
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  5. * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  6. * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
  9. * and Nettle, by Niels Möller.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. */
  17. #ifndef _LINUX_CRYPTO_H
  18. #define _LINUX_CRYPTO_H
  19. #include <asm/atomic.h>
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/uaccess.h>
  26. /*
  27. * Algorithm masks and types.
  28. */
  29. #define CRYPTO_ALG_TYPE_MASK 0x0000000f
  30. #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
  31. #define CRYPTO_ALG_TYPE_DIGEST 0x00000002
  32. #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000003
  33. #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
  34. #define CRYPTO_ALG_LARVAL 0x00000010
  35. #define CRYPTO_ALG_DEAD 0x00000020
  36. #define CRYPTO_ALG_DYING 0x00000040
  37. #define CRYPTO_ALG_ASYNC 0x00000080
  38. /*
  39. * Transform masks and values (for crt_flags).
  40. */
  41. #define CRYPTO_TFM_MODE_MASK 0x000000ff
  42. #define CRYPTO_TFM_REQ_MASK 0x000fff00
  43. #define CRYPTO_TFM_RES_MASK 0xfff00000
  44. #define CRYPTO_TFM_MODE_ECB 0x00000001
  45. #define CRYPTO_TFM_MODE_CBC 0x00000002
  46. #define CRYPTO_TFM_MODE_CFB 0x00000004
  47. #define CRYPTO_TFM_MODE_CTR 0x00000008
  48. #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
  49. #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
  50. #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
  51. #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
  52. #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
  53. #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
  54. #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
  55. /*
  56. * Miscellaneous stuff.
  57. */
  58. #define CRYPTO_UNSPEC 0
  59. #define CRYPTO_MAX_ALG_NAME 64
  60. #define CRYPTO_DIR_ENCRYPT 1
  61. #define CRYPTO_DIR_DECRYPT 0
  62. /*
  63. * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
  64. * declaration) is used to ensure that the crypto_tfm context structure is
  65. * aligned correctly for the given architecture so that there are no alignment
  66. * faults for C data types. In particular, this is required on platforms such
  67. * as arm where pointers are 32-bit aligned but there are data types such as
  68. * u64 which require 64-bit alignment.
  69. */
  70. #if defined(ARCH_KMALLOC_MINALIGN)
  71. #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
  72. #elif defined(ARCH_SLAB_MINALIGN)
  73. #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
  74. #endif
  75. #ifdef CRYPTO_MINALIGN
  76. #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
  77. #else
  78. #define CRYPTO_MINALIGN_ATTR
  79. #endif
  80. struct scatterlist;
  81. struct crypto_blkcipher;
  82. struct crypto_tfm;
  83. struct crypto_type;
  84. struct blkcipher_desc {
  85. struct crypto_blkcipher *tfm;
  86. void *info;
  87. u32 flags;
  88. };
  89. struct cipher_desc {
  90. struct crypto_tfm *tfm;
  91. void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  92. unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
  93. const u8 *src, unsigned int nbytes);
  94. void *info;
  95. };
  96. /*
  97. * Algorithms: modular crypto algorithm implementations, managed
  98. * via crypto_register_alg() and crypto_unregister_alg().
  99. */
  100. struct blkcipher_alg {
  101. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  102. unsigned int keylen);
  103. int (*encrypt)(struct blkcipher_desc *desc,
  104. struct scatterlist *dst, struct scatterlist *src,
  105. unsigned int nbytes);
  106. int (*decrypt)(struct blkcipher_desc *desc,
  107. struct scatterlist *dst, struct scatterlist *src,
  108. unsigned int nbytes);
  109. unsigned int min_keysize;
  110. unsigned int max_keysize;
  111. unsigned int ivsize;
  112. };
  113. struct cipher_alg {
  114. unsigned int cia_min_keysize;
  115. unsigned int cia_max_keysize;
  116. int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  117. unsigned int keylen);
  118. void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  119. void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  120. unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
  121. u8 *dst, const u8 *src,
  122. unsigned int nbytes) __deprecated;
  123. unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
  124. u8 *dst, const u8 *src,
  125. unsigned int nbytes) __deprecated;
  126. unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
  127. u8 *dst, const u8 *src,
  128. unsigned int nbytes) __deprecated;
  129. unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
  130. u8 *dst, const u8 *src,
  131. unsigned int nbytes) __deprecated;
  132. };
  133. struct digest_alg {
  134. unsigned int dia_digestsize;
  135. void (*dia_init)(struct crypto_tfm *tfm);
  136. void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
  137. unsigned int len);
  138. void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
  139. int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  140. unsigned int keylen);
  141. };
  142. struct compress_alg {
  143. int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
  144. unsigned int slen, u8 *dst, unsigned int *dlen);
  145. int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
  146. unsigned int slen, u8 *dst, unsigned int *dlen);
  147. };
  148. #define cra_blkcipher cra_u.blkcipher
  149. #define cra_cipher cra_u.cipher
  150. #define cra_digest cra_u.digest
  151. #define cra_compress cra_u.compress
  152. struct crypto_alg {
  153. struct list_head cra_list;
  154. struct list_head cra_users;
  155. u32 cra_flags;
  156. unsigned int cra_blocksize;
  157. unsigned int cra_ctxsize;
  158. unsigned int cra_alignmask;
  159. int cra_priority;
  160. atomic_t cra_refcnt;
  161. char cra_name[CRYPTO_MAX_ALG_NAME];
  162. char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  163. const struct crypto_type *cra_type;
  164. union {
  165. struct blkcipher_alg blkcipher;
  166. struct cipher_alg cipher;
  167. struct digest_alg digest;
  168. struct compress_alg compress;
  169. } cra_u;
  170. int (*cra_init)(struct crypto_tfm *tfm);
  171. void (*cra_exit)(struct crypto_tfm *tfm);
  172. void (*cra_destroy)(struct crypto_alg *alg);
  173. struct module *cra_module;
  174. };
  175. /*
  176. * Algorithm registration interface.
  177. */
  178. int crypto_register_alg(struct crypto_alg *alg);
  179. int crypto_unregister_alg(struct crypto_alg *alg);
  180. /*
  181. * Algorithm query interface.
  182. */
  183. #ifdef CONFIG_CRYPTO
  184. int crypto_alg_available(const char *name, u32 flags);
  185. #else
  186. static inline int crypto_alg_available(const char *name, u32 flags)
  187. {
  188. return 0;
  189. }
  190. #endif
  191. /*
  192. * Transforms: user-instantiated objects which encapsulate algorithms
  193. * and core processing logic. Managed via crypto_alloc_*() and
  194. * crypto_free_*(), as well as the various helpers below.
  195. */
  196. struct blkcipher_tfm {
  197. void *iv;
  198. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  199. unsigned int keylen);
  200. int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  201. struct scatterlist *src, unsigned int nbytes);
  202. int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  203. struct scatterlist *src, unsigned int nbytes);
  204. };
  205. struct cipher_tfm {
  206. void *cit_iv;
  207. unsigned int cit_ivsize;
  208. u32 cit_mode;
  209. int (*cit_setkey)(struct crypto_tfm *tfm,
  210. const u8 *key, unsigned int keylen);
  211. int (*cit_encrypt)(struct crypto_tfm *tfm,
  212. struct scatterlist *dst,
  213. struct scatterlist *src,
  214. unsigned int nbytes);
  215. int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
  216. struct scatterlist *dst,
  217. struct scatterlist *src,
  218. unsigned int nbytes, u8 *iv);
  219. int (*cit_decrypt)(struct crypto_tfm *tfm,
  220. struct scatterlist *dst,
  221. struct scatterlist *src,
  222. unsigned int nbytes);
  223. int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
  224. struct scatterlist *dst,
  225. struct scatterlist *src,
  226. unsigned int nbytes, u8 *iv);
  227. void (*cit_xor_block)(u8 *dst, const u8 *src);
  228. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  229. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  230. };
  231. struct digest_tfm {
  232. void (*dit_init)(struct crypto_tfm *tfm);
  233. void (*dit_update)(struct crypto_tfm *tfm,
  234. struct scatterlist *sg, unsigned int nsg);
  235. void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
  236. void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
  237. unsigned int nsg, u8 *out);
  238. int (*dit_setkey)(struct crypto_tfm *tfm,
  239. const u8 *key, unsigned int keylen);
  240. #ifdef CONFIG_CRYPTO_HMAC
  241. void *dit_hmac_block;
  242. #endif
  243. };
  244. struct compress_tfm {
  245. int (*cot_compress)(struct crypto_tfm *tfm,
  246. const u8 *src, unsigned int slen,
  247. u8 *dst, unsigned int *dlen);
  248. int (*cot_decompress)(struct crypto_tfm *tfm,
  249. const u8 *src, unsigned int slen,
  250. u8 *dst, unsigned int *dlen);
  251. };
  252. #define crt_blkcipher crt_u.blkcipher
  253. #define crt_cipher crt_u.cipher
  254. #define crt_digest crt_u.digest
  255. #define crt_compress crt_u.compress
  256. struct crypto_tfm {
  257. u32 crt_flags;
  258. union {
  259. struct blkcipher_tfm blkcipher;
  260. struct cipher_tfm cipher;
  261. struct digest_tfm digest;
  262. struct compress_tfm compress;
  263. } crt_u;
  264. struct crypto_alg *__crt_alg;
  265. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  266. };
  267. #define crypto_cipher crypto_tfm
  268. struct crypto_blkcipher {
  269. struct crypto_tfm base;
  270. };
  271. enum {
  272. CRYPTOA_UNSPEC,
  273. CRYPTOA_ALG,
  274. };
  275. struct crypto_attr_alg {
  276. char name[CRYPTO_MAX_ALG_NAME];
  277. };
  278. /*
  279. * Transform user interface.
  280. */
  281. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  282. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  283. void crypto_free_tfm(struct crypto_tfm *tfm);
  284. /*
  285. * Transform helpers which query the underlying algorithm.
  286. */
  287. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  288. {
  289. return tfm->__crt_alg->cra_name;
  290. }
  291. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  292. {
  293. return tfm->__crt_alg->cra_driver_name;
  294. }
  295. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  296. {
  297. return tfm->__crt_alg->cra_priority;
  298. }
  299. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  300. {
  301. return module_name(tfm->__crt_alg->cra_module);
  302. }
  303. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  304. {
  305. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  306. }
  307. static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
  308. __deprecated;
  309. static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
  310. {
  311. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  312. return tfm->__crt_alg->cra_cipher.cia_min_keysize;
  313. }
  314. static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
  315. __deprecated;
  316. static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
  317. {
  318. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  319. return tfm->__crt_alg->cra_cipher.cia_max_keysize;
  320. }
  321. static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated;
  322. static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
  323. {
  324. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  325. return tfm->crt_cipher.cit_ivsize;
  326. }
  327. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  328. {
  329. return tfm->__crt_alg->cra_blocksize;
  330. }
  331. static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
  332. {
  333. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  334. return tfm->__crt_alg->cra_digest.dia_digestsize;
  335. }
  336. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  337. {
  338. return tfm->__crt_alg->cra_alignmask;
  339. }
  340. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  341. {
  342. return tfm->crt_flags;
  343. }
  344. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  345. {
  346. tfm->crt_flags |= flags;
  347. }
  348. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  349. {
  350. tfm->crt_flags &= ~flags;
  351. }
  352. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  353. {
  354. return tfm->__crt_ctx;
  355. }
  356. static inline unsigned int crypto_tfm_ctx_alignment(void)
  357. {
  358. struct crypto_tfm *tfm;
  359. return __alignof__(tfm->__crt_ctx);
  360. }
  361. /*
  362. * API wrappers.
  363. */
  364. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  365. struct crypto_tfm *tfm)
  366. {
  367. return (struct crypto_blkcipher *)tfm;
  368. }
  369. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  370. struct crypto_tfm *tfm)
  371. {
  372. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  373. return __crypto_blkcipher_cast(tfm);
  374. }
  375. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  376. const char *alg_name, u32 type, u32 mask)
  377. {
  378. type &= ~CRYPTO_ALG_TYPE_MASK;
  379. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  380. mask |= CRYPTO_ALG_TYPE_MASK;
  381. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  382. }
  383. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  384. struct crypto_blkcipher *tfm)
  385. {
  386. return &tfm->base;
  387. }
  388. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  389. {
  390. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  391. }
  392. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  393. {
  394. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  395. }
  396. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  397. struct crypto_blkcipher *tfm)
  398. {
  399. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  400. }
  401. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  402. struct crypto_blkcipher *tfm)
  403. {
  404. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  405. }
  406. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  407. {
  408. return crypto_blkcipher_alg(tfm)->ivsize;
  409. }
  410. static inline unsigned int crypto_blkcipher_blocksize(
  411. struct crypto_blkcipher *tfm)
  412. {
  413. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  414. }
  415. static inline unsigned int crypto_blkcipher_alignmask(
  416. struct crypto_blkcipher *tfm)
  417. {
  418. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  419. }
  420. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  421. {
  422. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  423. }
  424. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  425. u32 flags)
  426. {
  427. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  428. }
  429. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  430. u32 flags)
  431. {
  432. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  433. }
  434. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  435. const u8 *key, unsigned int keylen)
  436. {
  437. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  438. key, keylen);
  439. }
  440. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  441. struct scatterlist *dst,
  442. struct scatterlist *src,
  443. unsigned int nbytes)
  444. {
  445. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  446. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  447. }
  448. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  449. struct scatterlist *dst,
  450. struct scatterlist *src,
  451. unsigned int nbytes)
  452. {
  453. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  454. }
  455. static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
  456. struct scatterlist *dst,
  457. struct scatterlist *src,
  458. unsigned int nbytes)
  459. {
  460. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  461. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  462. }
  463. static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
  464. struct scatterlist *dst,
  465. struct scatterlist *src,
  466. unsigned int nbytes)
  467. {
  468. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  469. }
  470. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  471. const u8 *src, unsigned int len)
  472. {
  473. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  474. }
  475. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  476. u8 *dst, unsigned int len)
  477. {
  478. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  479. }
  480. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  481. {
  482. return (struct crypto_cipher *)tfm;
  483. }
  484. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  485. {
  486. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  487. return __crypto_cipher_cast(tfm);
  488. }
  489. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  490. u32 type, u32 mask)
  491. {
  492. type &= ~CRYPTO_ALG_TYPE_MASK;
  493. type |= CRYPTO_ALG_TYPE_CIPHER;
  494. mask |= CRYPTO_ALG_TYPE_MASK;
  495. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  496. }
  497. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  498. {
  499. return tfm;
  500. }
  501. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  502. {
  503. crypto_free_tfm(crypto_cipher_tfm(tfm));
  504. }
  505. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  506. {
  507. return &crypto_cipher_tfm(tfm)->crt_cipher;
  508. }
  509. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  510. {
  511. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  512. }
  513. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  514. {
  515. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  516. }
  517. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  518. {
  519. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  520. }
  521. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  522. u32 flags)
  523. {
  524. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  525. }
  526. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  527. u32 flags)
  528. {
  529. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  530. }
  531. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  532. const u8 *key, unsigned int keylen)
  533. {
  534. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  535. key, keylen);
  536. }
  537. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  538. u8 *dst, const u8 *src)
  539. {
  540. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  541. dst, src);
  542. }
  543. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  544. u8 *dst, const u8 *src)
  545. {
  546. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  547. dst, src);
  548. }
  549. static inline void crypto_digest_init(struct crypto_tfm *tfm)
  550. {
  551. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  552. tfm->crt_digest.dit_init(tfm);
  553. }
  554. static inline void crypto_digest_update(struct crypto_tfm *tfm,
  555. struct scatterlist *sg,
  556. unsigned int nsg)
  557. {
  558. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  559. tfm->crt_digest.dit_update(tfm, sg, nsg);
  560. }
  561. static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
  562. {
  563. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  564. tfm->crt_digest.dit_final(tfm, out);
  565. }
  566. static inline void crypto_digest_digest(struct crypto_tfm *tfm,
  567. struct scatterlist *sg,
  568. unsigned int nsg, u8 *out)
  569. {
  570. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  571. tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
  572. }
  573. static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
  574. const u8 *key, unsigned int keylen)
  575. {
  576. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  577. return tfm->crt_digest.dit_setkey(tfm, key, keylen);
  578. }
  579. static int crypto_cipher_encrypt(struct crypto_tfm *tfm,
  580. struct scatterlist *dst,
  581. struct scatterlist *src,
  582. unsigned int nbytes) __deprecated;
  583. static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
  584. struct scatterlist *dst,
  585. struct scatterlist *src,
  586. unsigned int nbytes)
  587. {
  588. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  589. return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
  590. }
  591. static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
  592. struct scatterlist *dst,
  593. struct scatterlist *src,
  594. unsigned int nbytes, u8 *iv) __deprecated;
  595. static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
  596. struct scatterlist *dst,
  597. struct scatterlist *src,
  598. unsigned int nbytes, u8 *iv)
  599. {
  600. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  601. return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
  602. }
  603. static int crypto_cipher_decrypt(struct crypto_tfm *tfm,
  604. struct scatterlist *dst,
  605. struct scatterlist *src,
  606. unsigned int nbytes) __deprecated;
  607. static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
  608. struct scatterlist *dst,
  609. struct scatterlist *src,
  610. unsigned int nbytes)
  611. {
  612. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  613. return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
  614. }
  615. static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
  616. struct scatterlist *dst,
  617. struct scatterlist *src,
  618. unsigned int nbytes, u8 *iv) __deprecated;
  619. static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
  620. struct scatterlist *dst,
  621. struct scatterlist *src,
  622. unsigned int nbytes, u8 *iv)
  623. {
  624. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  625. return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
  626. }
  627. static void crypto_cipher_set_iv(struct crypto_tfm *tfm,
  628. const u8 *src, unsigned int len) __deprecated;
  629. static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
  630. const u8 *src, unsigned int len)
  631. {
  632. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  633. memcpy(tfm->crt_cipher.cit_iv, src, len);
  634. }
  635. static void crypto_cipher_get_iv(struct crypto_tfm *tfm,
  636. u8 *dst, unsigned int len) __deprecated;
  637. static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
  638. u8 *dst, unsigned int len)
  639. {
  640. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  641. memcpy(dst, tfm->crt_cipher.cit_iv, len);
  642. }
  643. static inline int crypto_comp_compress(struct crypto_tfm *tfm,
  644. const u8 *src, unsigned int slen,
  645. u8 *dst, unsigned int *dlen)
  646. {
  647. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
  648. return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
  649. }
  650. static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
  651. const u8 *src, unsigned int slen,
  652. u8 *dst, unsigned int *dlen)
  653. {
  654. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
  655. return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
  656. }
  657. /*
  658. * HMAC support.
  659. */
  660. #ifdef CONFIG_CRYPTO_HMAC
  661. void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
  662. void crypto_hmac_update(struct crypto_tfm *tfm,
  663. struct scatterlist *sg, unsigned int nsg);
  664. void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
  665. unsigned int *keylen, u8 *out);
  666. void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
  667. struct scatterlist *sg, unsigned int nsg, u8 *out);
  668. #endif /* CONFIG_CRYPTO_HMAC */
  669. #endif /* _LINUX_CRYPTO_H */