crypto.h 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. int (*cit_setkey)(struct crypto_tfm *tfm,
  252. const u8 *key, unsigned int keylen);
  253. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  254. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  255. };
  256. struct hash_tfm {
  257. int (*init)(struct hash_desc *desc);
  258. int (*update)(struct hash_desc *desc,
  259. struct scatterlist *sg, unsigned int nsg);
  260. int (*final)(struct hash_desc *desc, u8 *out);
  261. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  262. unsigned int nsg, u8 *out);
  263. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  264. unsigned int keylen);
  265. unsigned int digestsize;
  266. };
  267. struct compress_tfm {
  268. int (*cot_compress)(struct crypto_tfm *tfm,
  269. const u8 *src, unsigned int slen,
  270. u8 *dst, unsigned int *dlen);
  271. int (*cot_decompress)(struct crypto_tfm *tfm,
  272. const u8 *src, unsigned int slen,
  273. u8 *dst, unsigned int *dlen);
  274. };
  275. #define crt_ablkcipher crt_u.ablkcipher
  276. #define crt_blkcipher crt_u.blkcipher
  277. #define crt_cipher crt_u.cipher
  278. #define crt_hash crt_u.hash
  279. #define crt_compress crt_u.compress
  280. struct crypto_tfm {
  281. u32 crt_flags;
  282. union {
  283. struct ablkcipher_tfm ablkcipher;
  284. struct blkcipher_tfm blkcipher;
  285. struct cipher_tfm cipher;
  286. struct hash_tfm hash;
  287. struct compress_tfm compress;
  288. } crt_u;
  289. struct crypto_alg *__crt_alg;
  290. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  291. };
  292. struct crypto_ablkcipher {
  293. struct crypto_tfm base;
  294. };
  295. struct crypto_blkcipher {
  296. struct crypto_tfm base;
  297. };
  298. struct crypto_cipher {
  299. struct crypto_tfm base;
  300. };
  301. struct crypto_comp {
  302. struct crypto_tfm base;
  303. };
  304. struct crypto_hash {
  305. struct crypto_tfm base;
  306. };
  307. enum {
  308. CRYPTOA_UNSPEC,
  309. CRYPTOA_ALG,
  310. CRYPTOA_TYPE,
  311. __CRYPTOA_MAX,
  312. };
  313. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  314. struct crypto_attr_alg {
  315. char name[CRYPTO_MAX_ALG_NAME];
  316. };
  317. struct crypto_attr_type {
  318. u32 type;
  319. u32 mask;
  320. };
  321. /*
  322. * Transform user interface.
  323. */
  324. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  325. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  326. void crypto_free_tfm(struct crypto_tfm *tfm);
  327. /*
  328. * Transform helpers which query the underlying algorithm.
  329. */
  330. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  331. {
  332. return tfm->__crt_alg->cra_name;
  333. }
  334. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  335. {
  336. return tfm->__crt_alg->cra_driver_name;
  337. }
  338. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  339. {
  340. return tfm->__crt_alg->cra_priority;
  341. }
  342. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  343. {
  344. return module_name(tfm->__crt_alg->cra_module);
  345. }
  346. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  347. {
  348. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  349. }
  350. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  351. {
  352. return tfm->__crt_alg->cra_blocksize;
  353. }
  354. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  355. {
  356. return tfm->__crt_alg->cra_alignmask;
  357. }
  358. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  359. {
  360. return tfm->crt_flags;
  361. }
  362. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  363. {
  364. tfm->crt_flags |= flags;
  365. }
  366. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  367. {
  368. tfm->crt_flags &= ~flags;
  369. }
  370. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  371. {
  372. return tfm->__crt_ctx;
  373. }
  374. static inline unsigned int crypto_tfm_ctx_alignment(void)
  375. {
  376. struct crypto_tfm *tfm;
  377. return __alignof__(tfm->__crt_ctx);
  378. }
  379. /*
  380. * API wrappers.
  381. */
  382. static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
  383. struct crypto_tfm *tfm)
  384. {
  385. return (struct crypto_ablkcipher *)tfm;
  386. }
  387. static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher(
  388. const char *alg_name, u32 type, u32 mask)
  389. {
  390. type &= ~CRYPTO_ALG_TYPE_MASK;
  391. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  392. mask |= CRYPTO_ALG_TYPE_MASK;
  393. return __crypto_ablkcipher_cast(
  394. crypto_alloc_base(alg_name, type, mask));
  395. }
  396. static inline struct crypto_tfm *crypto_ablkcipher_tfm(
  397. struct crypto_ablkcipher *tfm)
  398. {
  399. return &tfm->base;
  400. }
  401. static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
  402. {
  403. crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
  404. }
  405. static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
  406. u32 mask)
  407. {
  408. type &= ~CRYPTO_ALG_TYPE_MASK;
  409. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  410. mask |= CRYPTO_ALG_TYPE_MASK;
  411. return crypto_has_alg(alg_name, type, mask);
  412. }
  413. static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
  414. struct crypto_ablkcipher *tfm)
  415. {
  416. return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
  417. }
  418. static inline unsigned int crypto_ablkcipher_ivsize(
  419. struct crypto_ablkcipher *tfm)
  420. {
  421. return crypto_ablkcipher_crt(tfm)->ivsize;
  422. }
  423. static inline unsigned int crypto_ablkcipher_blocksize(
  424. struct crypto_ablkcipher *tfm)
  425. {
  426. return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
  427. }
  428. static inline unsigned int crypto_ablkcipher_alignmask(
  429. struct crypto_ablkcipher *tfm)
  430. {
  431. return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
  432. }
  433. static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
  434. {
  435. return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
  436. }
  437. static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
  438. u32 flags)
  439. {
  440. crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
  441. }
  442. static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
  443. u32 flags)
  444. {
  445. crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
  446. }
  447. static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
  448. const u8 *key, unsigned int keylen)
  449. {
  450. return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen);
  451. }
  452. static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
  453. struct ablkcipher_request *req)
  454. {
  455. return __crypto_ablkcipher_cast(req->base.tfm);
  456. }
  457. static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
  458. {
  459. struct ablkcipher_tfm *crt =
  460. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  461. return crt->encrypt(req);
  462. }
  463. static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
  464. {
  465. struct ablkcipher_tfm *crt =
  466. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  467. return crt->decrypt(req);
  468. }
  469. static inline int crypto_ablkcipher_reqsize(struct crypto_ablkcipher *tfm)
  470. {
  471. return crypto_ablkcipher_crt(tfm)->reqsize;
  472. }
  473. static inline void ablkcipher_request_set_tfm(
  474. struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
  475. {
  476. req->base.tfm = crypto_ablkcipher_tfm(tfm);
  477. }
  478. static inline struct ablkcipher_request *ablkcipher_request_cast(
  479. struct crypto_async_request *req)
  480. {
  481. return container_of(req, struct ablkcipher_request, base);
  482. }
  483. static inline struct ablkcipher_request *ablkcipher_request_alloc(
  484. struct crypto_ablkcipher *tfm, gfp_t gfp)
  485. {
  486. struct ablkcipher_request *req;
  487. req = kmalloc(sizeof(struct ablkcipher_request) +
  488. crypto_ablkcipher_reqsize(tfm), gfp);
  489. if (likely(req))
  490. ablkcipher_request_set_tfm(req, tfm);
  491. return req;
  492. }
  493. static inline void ablkcipher_request_free(struct ablkcipher_request *req)
  494. {
  495. kfree(req);
  496. }
  497. static inline void ablkcipher_request_set_callback(
  498. struct ablkcipher_request *req,
  499. u32 flags, crypto_completion_t complete, void *data)
  500. {
  501. req->base.complete = complete;
  502. req->base.data = data;
  503. req->base.flags = flags;
  504. }
  505. static inline void ablkcipher_request_set_crypt(
  506. struct ablkcipher_request *req,
  507. struct scatterlist *src, struct scatterlist *dst,
  508. unsigned int nbytes, void *iv)
  509. {
  510. req->src = src;
  511. req->dst = dst;
  512. req->nbytes = nbytes;
  513. req->info = iv;
  514. }
  515. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  516. struct crypto_tfm *tfm)
  517. {
  518. return (struct crypto_blkcipher *)tfm;
  519. }
  520. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  521. struct crypto_tfm *tfm)
  522. {
  523. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  524. return __crypto_blkcipher_cast(tfm);
  525. }
  526. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  527. const char *alg_name, u32 type, u32 mask)
  528. {
  529. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  530. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  531. mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
  532. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  533. }
  534. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  535. struct crypto_blkcipher *tfm)
  536. {
  537. return &tfm->base;
  538. }
  539. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  540. {
  541. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  542. }
  543. static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
  544. {
  545. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  546. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  547. mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
  548. return crypto_has_alg(alg_name, type, mask);
  549. }
  550. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  551. {
  552. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  553. }
  554. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  555. struct crypto_blkcipher *tfm)
  556. {
  557. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  558. }
  559. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  560. struct crypto_blkcipher *tfm)
  561. {
  562. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  563. }
  564. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  565. {
  566. return crypto_blkcipher_alg(tfm)->ivsize;
  567. }
  568. static inline unsigned int crypto_blkcipher_blocksize(
  569. struct crypto_blkcipher *tfm)
  570. {
  571. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  572. }
  573. static inline unsigned int crypto_blkcipher_alignmask(
  574. struct crypto_blkcipher *tfm)
  575. {
  576. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  577. }
  578. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  579. {
  580. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  581. }
  582. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  583. u32 flags)
  584. {
  585. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  586. }
  587. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  588. u32 flags)
  589. {
  590. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  591. }
  592. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  593. const u8 *key, unsigned int keylen)
  594. {
  595. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  596. key, keylen);
  597. }
  598. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  599. struct scatterlist *dst,
  600. struct scatterlist *src,
  601. unsigned int nbytes)
  602. {
  603. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  604. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  605. }
  606. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  607. struct scatterlist *dst,
  608. struct scatterlist *src,
  609. unsigned int nbytes)
  610. {
  611. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  612. }
  613. static inline int crypto_blkcipher_decrypt(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)->decrypt(desc, dst, src, nbytes);
  620. }
  621. static inline int crypto_blkcipher_decrypt_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)->decrypt(desc, dst, src, nbytes);
  627. }
  628. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  629. const u8 *src, unsigned int len)
  630. {
  631. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  632. }
  633. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  634. u8 *dst, unsigned int len)
  635. {
  636. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  637. }
  638. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  639. {
  640. return (struct crypto_cipher *)tfm;
  641. }
  642. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  643. {
  644. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  645. return __crypto_cipher_cast(tfm);
  646. }
  647. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  648. u32 type, u32 mask)
  649. {
  650. type &= ~CRYPTO_ALG_TYPE_MASK;
  651. type |= CRYPTO_ALG_TYPE_CIPHER;
  652. mask |= CRYPTO_ALG_TYPE_MASK;
  653. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  654. }
  655. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  656. {
  657. return &tfm->base;
  658. }
  659. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  660. {
  661. crypto_free_tfm(crypto_cipher_tfm(tfm));
  662. }
  663. static inline int crypto_has_cipher(const char *alg_name, 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_has_alg(alg_name, type, mask);
  669. }
  670. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  671. {
  672. return &crypto_cipher_tfm(tfm)->crt_cipher;
  673. }
  674. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  675. {
  676. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  677. }
  678. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  679. {
  680. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  681. }
  682. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  683. {
  684. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  685. }
  686. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  687. u32 flags)
  688. {
  689. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  690. }
  691. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  692. u32 flags)
  693. {
  694. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  695. }
  696. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  697. const u8 *key, unsigned int keylen)
  698. {
  699. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  700. key, keylen);
  701. }
  702. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  703. u8 *dst, const u8 *src)
  704. {
  705. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  706. dst, src);
  707. }
  708. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  709. u8 *dst, const u8 *src)
  710. {
  711. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  712. dst, src);
  713. }
  714. static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
  715. {
  716. return (struct crypto_hash *)tfm;
  717. }
  718. static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
  719. {
  720. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
  721. CRYPTO_ALG_TYPE_HASH_MASK);
  722. return __crypto_hash_cast(tfm);
  723. }
  724. static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
  725. u32 type, u32 mask)
  726. {
  727. type &= ~CRYPTO_ALG_TYPE_MASK;
  728. type |= CRYPTO_ALG_TYPE_HASH;
  729. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  730. return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
  731. }
  732. static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
  733. {
  734. return &tfm->base;
  735. }
  736. static inline void crypto_free_hash(struct crypto_hash *tfm)
  737. {
  738. crypto_free_tfm(crypto_hash_tfm(tfm));
  739. }
  740. static inline int crypto_has_hash(const char *alg_name, 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_has_alg(alg_name, type, mask);
  746. }
  747. static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
  748. {
  749. return &crypto_hash_tfm(tfm)->crt_hash;
  750. }
  751. static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
  752. {
  753. return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
  754. }
  755. static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
  756. {
  757. return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
  758. }
  759. static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
  760. {
  761. return crypto_hash_crt(tfm)->digestsize;
  762. }
  763. static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
  764. {
  765. return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
  766. }
  767. static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
  768. {
  769. crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
  770. }
  771. static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
  772. {
  773. crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
  774. }
  775. static inline int crypto_hash_init(struct hash_desc *desc)
  776. {
  777. return crypto_hash_crt(desc->tfm)->init(desc);
  778. }
  779. static inline int crypto_hash_update(struct hash_desc *desc,
  780. struct scatterlist *sg,
  781. unsigned int nbytes)
  782. {
  783. return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
  784. }
  785. static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
  786. {
  787. return crypto_hash_crt(desc->tfm)->final(desc, out);
  788. }
  789. static inline int crypto_hash_digest(struct hash_desc *desc,
  790. struct scatterlist *sg,
  791. unsigned int nbytes, u8 *out)
  792. {
  793. return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
  794. }
  795. static inline int crypto_hash_setkey(struct crypto_hash *hash,
  796. const u8 *key, unsigned int keylen)
  797. {
  798. return crypto_hash_crt(hash)->setkey(hash, key, keylen);
  799. }
  800. static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
  801. {
  802. return (struct crypto_comp *)tfm;
  803. }
  804. static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
  805. {
  806. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
  807. CRYPTO_ALG_TYPE_MASK);
  808. return __crypto_comp_cast(tfm);
  809. }
  810. static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
  811. u32 type, u32 mask)
  812. {
  813. type &= ~CRYPTO_ALG_TYPE_MASK;
  814. type |= CRYPTO_ALG_TYPE_COMPRESS;
  815. mask |= CRYPTO_ALG_TYPE_MASK;
  816. return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
  817. }
  818. static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
  819. {
  820. return &tfm->base;
  821. }
  822. static inline void crypto_free_comp(struct crypto_comp *tfm)
  823. {
  824. crypto_free_tfm(crypto_comp_tfm(tfm));
  825. }
  826. static inline int crypto_has_comp(const char *alg_name, 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_has_alg(alg_name, type, mask);
  832. }
  833. static inline const char *crypto_comp_name(struct crypto_comp *tfm)
  834. {
  835. return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
  836. }
  837. static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
  838. {
  839. return &crypto_comp_tfm(tfm)->crt_compress;
  840. }
  841. static inline int crypto_comp_compress(struct crypto_comp *tfm,
  842. const u8 *src, unsigned int slen,
  843. u8 *dst, unsigned int *dlen)
  844. {
  845. return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
  846. src, slen, dst, dlen);
  847. }
  848. static inline int crypto_comp_decompress(struct crypto_comp *tfm,
  849. const u8 *src, unsigned int slen,
  850. u8 *dst, unsigned int *dlen)
  851. {
  852. return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
  853. src, slen, dst, dlen);
  854. }
  855. #endif /* _LINUX_CRYPTO_H */