crypto.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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_HASH 0x00000003
  33. #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
  34. #define CRYPTO_ALG_TYPE_COMPRESS 0x00000005
  35. #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
  36. #define CRYPTO_ALG_LARVAL 0x00000010
  37. #define CRYPTO_ALG_DEAD 0x00000020
  38. #define CRYPTO_ALG_DYING 0x00000040
  39. #define CRYPTO_ALG_ASYNC 0x00000080
  40. /*
  41. * Set this bit if and only if the algorithm requires another algorithm of
  42. * the same type to handle corner cases.
  43. */
  44. #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
  45. /*
  46. * Transform masks and values (for crt_flags).
  47. */
  48. #define CRYPTO_TFM_REQ_MASK 0x000fff00
  49. #define CRYPTO_TFM_RES_MASK 0xfff00000
  50. #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
  51. #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
  52. #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
  53. #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
  54. #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
  55. #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
  56. #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
  57. #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
  58. /*
  59. * Miscellaneous stuff.
  60. */
  61. #define CRYPTO_MAX_ALG_NAME 64
  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_ablkcipher;
  82. struct crypto_async_request;
  83. struct crypto_blkcipher;
  84. struct crypto_hash;
  85. struct crypto_queue;
  86. struct crypto_tfm;
  87. struct crypto_type;
  88. typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
  89. struct crypto_async_request {
  90. struct list_head list;
  91. crypto_completion_t complete;
  92. void *data;
  93. struct crypto_tfm *tfm;
  94. u32 flags;
  95. };
  96. struct ablkcipher_request {
  97. struct crypto_async_request base;
  98. unsigned int nbytes;
  99. void *info;
  100. struct scatterlist *src;
  101. struct scatterlist *dst;
  102. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  103. };
  104. struct blkcipher_desc {
  105. struct crypto_blkcipher *tfm;
  106. void *info;
  107. u32 flags;
  108. };
  109. struct cipher_desc {
  110. struct crypto_tfm *tfm;
  111. void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  112. unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
  113. const u8 *src, unsigned int nbytes);
  114. void *info;
  115. };
  116. struct hash_desc {
  117. struct crypto_hash *tfm;
  118. u32 flags;
  119. };
  120. /*
  121. * Algorithms: modular crypto algorithm implementations, managed
  122. * via crypto_register_alg() and crypto_unregister_alg().
  123. */
  124. struct ablkcipher_alg {
  125. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  126. unsigned int keylen);
  127. int (*encrypt)(struct ablkcipher_request *req);
  128. int (*decrypt)(struct ablkcipher_request *req);
  129. struct crypto_queue *queue;
  130. unsigned int min_keysize;
  131. unsigned int max_keysize;
  132. unsigned int ivsize;
  133. };
  134. struct blkcipher_alg {
  135. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  136. unsigned int keylen);
  137. int (*encrypt)(struct blkcipher_desc *desc,
  138. struct scatterlist *dst, struct scatterlist *src,
  139. unsigned int nbytes);
  140. int (*decrypt)(struct blkcipher_desc *desc,
  141. struct scatterlist *dst, struct scatterlist *src,
  142. unsigned int nbytes);
  143. unsigned int min_keysize;
  144. unsigned int max_keysize;
  145. unsigned int ivsize;
  146. };
  147. struct cipher_alg {
  148. unsigned int cia_min_keysize;
  149. unsigned int cia_max_keysize;
  150. int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  151. unsigned int keylen);
  152. void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  153. void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  154. };
  155. struct digest_alg {
  156. unsigned int dia_digestsize;
  157. void (*dia_init)(struct crypto_tfm *tfm);
  158. void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
  159. unsigned int len);
  160. void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
  161. int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  162. unsigned int keylen);
  163. };
  164. struct hash_alg {
  165. int (*init)(struct hash_desc *desc);
  166. int (*update)(struct hash_desc *desc, struct scatterlist *sg,
  167. unsigned int nbytes);
  168. int (*final)(struct hash_desc *desc, u8 *out);
  169. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  170. unsigned int nbytes, u8 *out);
  171. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  172. unsigned int keylen);
  173. unsigned int digestsize;
  174. };
  175. struct compress_alg {
  176. int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
  177. unsigned int slen, u8 *dst, unsigned int *dlen);
  178. int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
  179. unsigned int slen, u8 *dst, unsigned int *dlen);
  180. };
  181. #define cra_ablkcipher cra_u.ablkcipher
  182. #define cra_blkcipher cra_u.blkcipher
  183. #define cra_cipher cra_u.cipher
  184. #define cra_digest cra_u.digest
  185. #define cra_hash cra_u.hash
  186. #define cra_compress cra_u.compress
  187. struct crypto_alg {
  188. struct list_head cra_list;
  189. struct list_head cra_users;
  190. u32 cra_flags;
  191. unsigned int cra_blocksize;
  192. unsigned int cra_ctxsize;
  193. unsigned int cra_alignmask;
  194. int cra_priority;
  195. atomic_t cra_refcnt;
  196. char cra_name[CRYPTO_MAX_ALG_NAME];
  197. char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  198. const struct crypto_type *cra_type;
  199. union {
  200. struct ablkcipher_alg ablkcipher;
  201. struct blkcipher_alg blkcipher;
  202. struct cipher_alg cipher;
  203. struct digest_alg digest;
  204. struct hash_alg hash;
  205. struct compress_alg compress;
  206. } cra_u;
  207. int (*cra_init)(struct crypto_tfm *tfm);
  208. void (*cra_exit)(struct crypto_tfm *tfm);
  209. void (*cra_destroy)(struct crypto_alg *alg);
  210. struct module *cra_module;
  211. };
  212. /*
  213. * Algorithm registration interface.
  214. */
  215. int crypto_register_alg(struct crypto_alg *alg);
  216. int crypto_unregister_alg(struct crypto_alg *alg);
  217. /*
  218. * Algorithm query interface.
  219. */
  220. #ifdef CONFIG_CRYPTO
  221. int crypto_has_alg(const char *name, u32 type, u32 mask);
  222. #else
  223. static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
  224. {
  225. return 0;
  226. }
  227. #endif
  228. /*
  229. * Transforms: user-instantiated objects which encapsulate algorithms
  230. * and core processing logic. Managed via crypto_alloc_*() and
  231. * crypto_free_*(), as well as the various helpers below.
  232. */
  233. struct ablkcipher_tfm {
  234. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  235. unsigned int keylen);
  236. int (*encrypt)(struct ablkcipher_request *req);
  237. int (*decrypt)(struct ablkcipher_request *req);
  238. unsigned int ivsize;
  239. unsigned int reqsize;
  240. };
  241. struct blkcipher_tfm {
  242. void *iv;
  243. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  244. unsigned int keylen);
  245. int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  246. struct scatterlist *src, unsigned int nbytes);
  247. int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  248. struct scatterlist *src, unsigned int nbytes);
  249. };
  250. struct cipher_tfm {
  251. void *cit_iv;
  252. unsigned int cit_ivsize;
  253. u32 cit_mode;
  254. int (*cit_setkey)(struct crypto_tfm *tfm,
  255. const u8 *key, unsigned int keylen);
  256. int (*cit_encrypt)(struct crypto_tfm *tfm,
  257. struct scatterlist *dst,
  258. struct scatterlist *src,
  259. unsigned int nbytes);
  260. int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
  261. struct scatterlist *dst,
  262. struct scatterlist *src,
  263. unsigned int nbytes, u8 *iv);
  264. int (*cit_decrypt)(struct crypto_tfm *tfm,
  265. struct scatterlist *dst,
  266. struct scatterlist *src,
  267. unsigned int nbytes);
  268. int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
  269. struct scatterlist *dst,
  270. struct scatterlist *src,
  271. unsigned int nbytes, u8 *iv);
  272. void (*cit_xor_block)(u8 *dst, const u8 *src);
  273. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  274. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  275. };
  276. struct hash_tfm {
  277. int (*init)(struct hash_desc *desc);
  278. int (*update)(struct hash_desc *desc,
  279. struct scatterlist *sg, unsigned int nsg);
  280. int (*final)(struct hash_desc *desc, u8 *out);
  281. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  282. unsigned int nsg, u8 *out);
  283. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  284. unsigned int keylen);
  285. unsigned int digestsize;
  286. };
  287. struct compress_tfm {
  288. int (*cot_compress)(struct crypto_tfm *tfm,
  289. const u8 *src, unsigned int slen,
  290. u8 *dst, unsigned int *dlen);
  291. int (*cot_decompress)(struct crypto_tfm *tfm,
  292. const u8 *src, unsigned int slen,
  293. u8 *dst, unsigned int *dlen);
  294. };
  295. #define crt_ablkcipher crt_u.ablkcipher
  296. #define crt_blkcipher crt_u.blkcipher
  297. #define crt_cipher crt_u.cipher
  298. #define crt_hash crt_u.hash
  299. #define crt_compress crt_u.compress
  300. struct crypto_tfm {
  301. u32 crt_flags;
  302. union {
  303. struct ablkcipher_tfm ablkcipher;
  304. struct blkcipher_tfm blkcipher;
  305. struct cipher_tfm cipher;
  306. struct hash_tfm hash;
  307. struct compress_tfm compress;
  308. } crt_u;
  309. struct crypto_alg *__crt_alg;
  310. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  311. };
  312. struct crypto_ablkcipher {
  313. struct crypto_tfm base;
  314. };
  315. struct crypto_blkcipher {
  316. struct crypto_tfm base;
  317. };
  318. struct crypto_cipher {
  319. struct crypto_tfm base;
  320. };
  321. struct crypto_comp {
  322. struct crypto_tfm base;
  323. };
  324. struct crypto_hash {
  325. struct crypto_tfm base;
  326. };
  327. enum {
  328. CRYPTOA_UNSPEC,
  329. CRYPTOA_ALG,
  330. CRYPTOA_TYPE,
  331. __CRYPTOA_MAX,
  332. };
  333. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  334. struct crypto_attr_alg {
  335. char name[CRYPTO_MAX_ALG_NAME];
  336. };
  337. struct crypto_attr_type {
  338. u32 type;
  339. u32 mask;
  340. };
  341. /*
  342. * Transform user interface.
  343. */
  344. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  345. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  346. void crypto_free_tfm(struct crypto_tfm *tfm);
  347. /*
  348. * Transform helpers which query the underlying algorithm.
  349. */
  350. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  351. {
  352. return tfm->__crt_alg->cra_name;
  353. }
  354. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  355. {
  356. return tfm->__crt_alg->cra_driver_name;
  357. }
  358. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  359. {
  360. return tfm->__crt_alg->cra_priority;
  361. }
  362. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  363. {
  364. return module_name(tfm->__crt_alg->cra_module);
  365. }
  366. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  367. {
  368. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  369. }
  370. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  371. {
  372. return tfm->__crt_alg->cra_blocksize;
  373. }
  374. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  375. {
  376. return tfm->__crt_alg->cra_alignmask;
  377. }
  378. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  379. {
  380. return tfm->crt_flags;
  381. }
  382. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  383. {
  384. tfm->crt_flags |= flags;
  385. }
  386. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  387. {
  388. tfm->crt_flags &= ~flags;
  389. }
  390. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  391. {
  392. return tfm->__crt_ctx;
  393. }
  394. static inline unsigned int crypto_tfm_ctx_alignment(void)
  395. {
  396. struct crypto_tfm *tfm;
  397. return __alignof__(tfm->__crt_ctx);
  398. }
  399. /*
  400. * API wrappers.
  401. */
  402. static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
  403. struct crypto_tfm *tfm)
  404. {
  405. return (struct crypto_ablkcipher *)tfm;
  406. }
  407. static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
  408. const char *alg_name, u32 type, u32 mask)
  409. {
  410. type &= ~CRYPTO_ALG_TYPE_MASK;
  411. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  412. mask |= CRYPTO_ALG_TYPE_MASK;
  413. return __crypto_ablkcipher_cast(
  414. crypto_alloc_base(alg_name, type, mask));
  415. }
  416. static inline struct crypto_tfm *crypto_ablkcipher_tfm(
  417. struct crypto_ablkcipher *tfm)
  418. {
  419. return &tfm->base;
  420. }
  421. static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
  422. {
  423. crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
  424. }
  425. static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
  426. u32 mask)
  427. {
  428. type &= ~CRYPTO_ALG_TYPE_MASK;
  429. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  430. mask |= CRYPTO_ALG_TYPE_MASK;
  431. return crypto_has_alg(alg_name, type, mask);
  432. }
  433. static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
  434. struct crypto_ablkcipher *tfm)
  435. {
  436. return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
  437. }
  438. static inline unsigned int crypto_ablkcipher_ivsize(
  439. struct crypto_ablkcipher *tfm)
  440. {
  441. return crypto_ablkcipher_crt(tfm)->ivsize;
  442. }
  443. static inline unsigned int crypto_ablkcipher_blocksize(
  444. struct crypto_ablkcipher *tfm)
  445. {
  446. return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
  447. }
  448. static inline unsigned int crypto_ablkcipher_alignmask(
  449. struct crypto_ablkcipher *tfm)
  450. {
  451. return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
  452. }
  453. static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
  454. {
  455. return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
  456. }
  457. static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
  458. u32 flags)
  459. {
  460. crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
  461. }
  462. static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
  463. u32 flags)
  464. {
  465. crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
  466. }
  467. static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
  468. const u8 *key, unsigned int keylen)
  469. {
  470. return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen);
  471. }
  472. static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
  473. struct ablkcipher_request *req)
  474. {
  475. return __crypto_ablkcipher_cast(req->base.tfm);
  476. }
  477. static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
  478. {
  479. struct ablkcipher_tfm *crt =
  480. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  481. return crt->encrypt(req);
  482. }
  483. static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
  484. {
  485. struct ablkcipher_tfm *crt =
  486. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  487. return crt->decrypt(req);
  488. }
  489. static inline int crypto_ablkcipher_reqsize(struct crypto_ablkcipher *tfm)
  490. {
  491. return crypto_ablkcipher_crt(tfm)->reqsize;
  492. }
  493. static inline struct ablkcipher_request *ablkcipher_request_cast(
  494. struct crypto_async_request *req)
  495. {
  496. return container_of(req, struct ablkcipher_request, base);
  497. }
  498. static inline struct ablkcipher_request *ablkcipher_request_alloc(
  499. struct crypto_ablkcipher *tfm, gfp_t gfp)
  500. {
  501. struct ablkcipher_request *req;
  502. req = kmalloc(sizeof(struct ablkcipher_request) +
  503. crypto_ablkcipher_reqsize(tfm), gfp);
  504. if (likely(req))
  505. req->base.tfm = crypto_ablkcipher_tfm(tfm);
  506. return req;
  507. }
  508. static inline void ablkcipher_request_free(struct ablkcipher_request *req)
  509. {
  510. kfree(req);
  511. }
  512. static inline void ablkcipher_request_set_callback(
  513. struct ablkcipher_request *req,
  514. u32 flags, crypto_completion_t complete, void *data)
  515. {
  516. req->base.complete = complete;
  517. req->base.data = data;
  518. req->base.flags = flags;
  519. }
  520. static inline void ablkcipher_request_set_crypt(
  521. struct ablkcipher_request *req,
  522. struct scatterlist *src, struct scatterlist *dst,
  523. unsigned int nbytes, void *iv)
  524. {
  525. req->src = src;
  526. req->dst = dst;
  527. req->nbytes = nbytes;
  528. req->info = iv;
  529. }
  530. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  531. struct crypto_tfm *tfm)
  532. {
  533. return (struct crypto_blkcipher *)tfm;
  534. }
  535. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  536. struct crypto_tfm *tfm)
  537. {
  538. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  539. return __crypto_blkcipher_cast(tfm);
  540. }
  541. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  542. const char *alg_name, u32 type, u32 mask)
  543. {
  544. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  545. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  546. mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
  547. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  548. }
  549. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  550. struct crypto_blkcipher *tfm)
  551. {
  552. return &tfm->base;
  553. }
  554. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  555. {
  556. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  557. }
  558. static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
  559. {
  560. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  561. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  562. mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
  563. return crypto_has_alg(alg_name, type, mask);
  564. }
  565. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  566. {
  567. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  568. }
  569. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  570. struct crypto_blkcipher *tfm)
  571. {
  572. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  573. }
  574. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  575. struct crypto_blkcipher *tfm)
  576. {
  577. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  578. }
  579. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  580. {
  581. return crypto_blkcipher_alg(tfm)->ivsize;
  582. }
  583. static inline unsigned int crypto_blkcipher_blocksize(
  584. struct crypto_blkcipher *tfm)
  585. {
  586. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  587. }
  588. static inline unsigned int crypto_blkcipher_alignmask(
  589. struct crypto_blkcipher *tfm)
  590. {
  591. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  592. }
  593. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  594. {
  595. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  596. }
  597. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  598. u32 flags)
  599. {
  600. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  601. }
  602. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  603. u32 flags)
  604. {
  605. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  606. }
  607. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  608. const u8 *key, unsigned int keylen)
  609. {
  610. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  611. key, keylen);
  612. }
  613. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  614. struct scatterlist *dst,
  615. struct scatterlist *src,
  616. unsigned int nbytes)
  617. {
  618. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  619. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  620. }
  621. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  622. struct scatterlist *dst,
  623. struct scatterlist *src,
  624. unsigned int nbytes)
  625. {
  626. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  627. }
  628. static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
  629. struct scatterlist *dst,
  630. struct scatterlist *src,
  631. unsigned int nbytes)
  632. {
  633. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  634. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  635. }
  636. static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
  637. struct scatterlist *dst,
  638. struct scatterlist *src,
  639. unsigned int nbytes)
  640. {
  641. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  642. }
  643. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  644. const u8 *src, unsigned int len)
  645. {
  646. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  647. }
  648. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  649. u8 *dst, unsigned int len)
  650. {
  651. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  652. }
  653. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  654. {
  655. return (struct crypto_cipher *)tfm;
  656. }
  657. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  658. {
  659. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  660. return __crypto_cipher_cast(tfm);
  661. }
  662. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  663. u32 type, u32 mask)
  664. {
  665. type &= ~CRYPTO_ALG_TYPE_MASK;
  666. type |= CRYPTO_ALG_TYPE_CIPHER;
  667. mask |= CRYPTO_ALG_TYPE_MASK;
  668. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  669. }
  670. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  671. {
  672. return &tfm->base;
  673. }
  674. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  675. {
  676. crypto_free_tfm(crypto_cipher_tfm(tfm));
  677. }
  678. static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
  679. {
  680. type &= ~CRYPTO_ALG_TYPE_MASK;
  681. type |= CRYPTO_ALG_TYPE_CIPHER;
  682. mask |= CRYPTO_ALG_TYPE_MASK;
  683. return crypto_has_alg(alg_name, type, mask);
  684. }
  685. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  686. {
  687. return &crypto_cipher_tfm(tfm)->crt_cipher;
  688. }
  689. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  690. {
  691. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  692. }
  693. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  694. {
  695. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  696. }
  697. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  698. {
  699. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  700. }
  701. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  702. u32 flags)
  703. {
  704. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  705. }
  706. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  707. u32 flags)
  708. {
  709. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  710. }
  711. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  712. const u8 *key, unsigned int keylen)
  713. {
  714. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  715. key, keylen);
  716. }
  717. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  718. u8 *dst, const u8 *src)
  719. {
  720. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  721. dst, src);
  722. }
  723. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  724. u8 *dst, const u8 *src)
  725. {
  726. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  727. dst, src);
  728. }
  729. static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
  730. {
  731. return (struct crypto_hash *)tfm;
  732. }
  733. static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
  734. {
  735. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
  736. CRYPTO_ALG_TYPE_HASH_MASK);
  737. return __crypto_hash_cast(tfm);
  738. }
  739. static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
  740. u32 type, u32 mask)
  741. {
  742. type &= ~CRYPTO_ALG_TYPE_MASK;
  743. type |= CRYPTO_ALG_TYPE_HASH;
  744. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  745. return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
  746. }
  747. static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
  748. {
  749. return &tfm->base;
  750. }
  751. static inline void crypto_free_hash(struct crypto_hash *tfm)
  752. {
  753. crypto_free_tfm(crypto_hash_tfm(tfm));
  754. }
  755. static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
  756. {
  757. type &= ~CRYPTO_ALG_TYPE_MASK;
  758. type |= CRYPTO_ALG_TYPE_HASH;
  759. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  760. return crypto_has_alg(alg_name, type, mask);
  761. }
  762. static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
  763. {
  764. return &crypto_hash_tfm(tfm)->crt_hash;
  765. }
  766. static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
  767. {
  768. return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
  769. }
  770. static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
  771. {
  772. return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
  773. }
  774. static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
  775. {
  776. return crypto_hash_crt(tfm)->digestsize;
  777. }
  778. static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
  779. {
  780. return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
  781. }
  782. static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
  783. {
  784. crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
  785. }
  786. static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
  787. {
  788. crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
  789. }
  790. static inline int crypto_hash_init(struct hash_desc *desc)
  791. {
  792. return crypto_hash_crt(desc->tfm)->init(desc);
  793. }
  794. static inline int crypto_hash_update(struct hash_desc *desc,
  795. struct scatterlist *sg,
  796. unsigned int nbytes)
  797. {
  798. return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
  799. }
  800. static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
  801. {
  802. return crypto_hash_crt(desc->tfm)->final(desc, out);
  803. }
  804. static inline int crypto_hash_digest(struct hash_desc *desc,
  805. struct scatterlist *sg,
  806. unsigned int nbytes, u8 *out)
  807. {
  808. return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
  809. }
  810. static inline int crypto_hash_setkey(struct crypto_hash *hash,
  811. const u8 *key, unsigned int keylen)
  812. {
  813. return crypto_hash_crt(hash)->setkey(hash, key, keylen);
  814. }
  815. static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
  816. {
  817. return (struct crypto_comp *)tfm;
  818. }
  819. static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
  820. {
  821. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
  822. CRYPTO_ALG_TYPE_MASK);
  823. return __crypto_comp_cast(tfm);
  824. }
  825. static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
  826. u32 type, u32 mask)
  827. {
  828. type &= ~CRYPTO_ALG_TYPE_MASK;
  829. type |= CRYPTO_ALG_TYPE_COMPRESS;
  830. mask |= CRYPTO_ALG_TYPE_MASK;
  831. return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
  832. }
  833. static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
  834. {
  835. return &tfm->base;
  836. }
  837. static inline void crypto_free_comp(struct crypto_comp *tfm)
  838. {
  839. crypto_free_tfm(crypto_comp_tfm(tfm));
  840. }
  841. static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
  842. {
  843. type &= ~CRYPTO_ALG_TYPE_MASK;
  844. type |= CRYPTO_ALG_TYPE_COMPRESS;
  845. mask |= CRYPTO_ALG_TYPE_MASK;
  846. return crypto_has_alg(alg_name, type, mask);
  847. }
  848. static inline const char *crypto_comp_name(struct crypto_comp *tfm)
  849. {
  850. return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
  851. }
  852. static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
  853. {
  854. return &crypto_comp_tfm(tfm)->crt_compress;
  855. }
  856. static inline int crypto_comp_compress(struct crypto_comp *tfm,
  857. const u8 *src, unsigned int slen,
  858. u8 *dst, unsigned int *dlen)
  859. {
  860. return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
  861. src, slen, dst, dlen);
  862. }
  863. static inline int crypto_comp_decompress(struct crypto_comp *tfm,
  864. const u8 *src, unsigned int slen,
  865. u8 *dst, unsigned int *dlen)
  866. {
  867. return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
  868. src, slen, dst, dlen);
  869. }
  870. #endif /* _LINUX_CRYPTO_H */