crypto.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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_ABLKCIPHER 0x00000005
  35. #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
  36. #define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
  37. #define CRYPTO_ALG_TYPE_AEAD 0x00000009
  38. #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
  39. #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
  40. #define CRYPTO_ALG_LARVAL 0x00000010
  41. #define CRYPTO_ALG_DEAD 0x00000020
  42. #define CRYPTO_ALG_DYING 0x00000040
  43. #define CRYPTO_ALG_ASYNC 0x00000080
  44. /*
  45. * Set this bit if and only if the algorithm requires another algorithm of
  46. * the same type to handle corner cases.
  47. */
  48. #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
  49. /*
  50. * This bit is set for symmetric key ciphers that have already been wrapped
  51. * with a generic IV generator to prevent them from being wrapped again.
  52. */
  53. #define CRYPTO_ALG_GENIV 0x00000200
  54. /*
  55. * Transform masks and values (for crt_flags).
  56. */
  57. #define CRYPTO_TFM_REQ_MASK 0x000fff00
  58. #define CRYPTO_TFM_RES_MASK 0xfff00000
  59. #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
  60. #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
  61. #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
  62. #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
  63. #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
  64. #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
  65. #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
  66. #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
  67. /*
  68. * Miscellaneous stuff.
  69. */
  70. #define CRYPTO_MAX_ALG_NAME 64
  71. /*
  72. * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
  73. * declaration) is used to ensure that the crypto_tfm context structure is
  74. * aligned correctly for the given architecture so that there are no alignment
  75. * faults for C data types. In particular, this is required on platforms such
  76. * as arm where pointers are 32-bit aligned but there are data types such as
  77. * u64 which require 64-bit alignment.
  78. */
  79. #if defined(ARCH_KMALLOC_MINALIGN)
  80. #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
  81. #elif defined(ARCH_SLAB_MINALIGN)
  82. #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
  83. #endif
  84. #ifdef CRYPTO_MINALIGN
  85. #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
  86. #else
  87. #define CRYPTO_MINALIGN_ATTR
  88. #endif
  89. struct scatterlist;
  90. struct crypto_ablkcipher;
  91. struct crypto_async_request;
  92. struct crypto_aead;
  93. struct crypto_blkcipher;
  94. struct crypto_hash;
  95. struct crypto_tfm;
  96. struct crypto_type;
  97. struct aead_givcrypt_request;
  98. struct skcipher_givcrypt_request;
  99. typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
  100. struct crypto_async_request {
  101. struct list_head list;
  102. crypto_completion_t complete;
  103. void *data;
  104. struct crypto_tfm *tfm;
  105. u32 flags;
  106. };
  107. struct ablkcipher_request {
  108. struct crypto_async_request base;
  109. unsigned int nbytes;
  110. void *info;
  111. struct scatterlist *src;
  112. struct scatterlist *dst;
  113. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  114. };
  115. /**
  116. * struct aead_request - AEAD request
  117. * @base: Common attributes for async crypto requests
  118. * @assoclen: Length in bytes of associated data for authentication
  119. * @cryptlen: Length of data to be encrypted or decrypted
  120. * @iv: Initialisation vector
  121. * @assoc: Associated data
  122. * @src: Source data
  123. * @dst: Destination data
  124. * @__ctx: Start of private context data
  125. */
  126. struct aead_request {
  127. struct crypto_async_request base;
  128. unsigned int assoclen;
  129. unsigned int cryptlen;
  130. u8 *iv;
  131. struct scatterlist *assoc;
  132. struct scatterlist *src;
  133. struct scatterlist *dst;
  134. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  135. };
  136. struct blkcipher_desc {
  137. struct crypto_blkcipher *tfm;
  138. void *info;
  139. u32 flags;
  140. };
  141. struct cipher_desc {
  142. struct crypto_tfm *tfm;
  143. void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  144. unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
  145. const u8 *src, unsigned int nbytes);
  146. void *info;
  147. };
  148. struct hash_desc {
  149. struct crypto_hash *tfm;
  150. u32 flags;
  151. };
  152. /*
  153. * Algorithms: modular crypto algorithm implementations, managed
  154. * via crypto_register_alg() and crypto_unregister_alg().
  155. */
  156. struct ablkcipher_alg {
  157. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  158. unsigned int keylen);
  159. int (*encrypt)(struct ablkcipher_request *req);
  160. int (*decrypt)(struct ablkcipher_request *req);
  161. int (*givencrypt)(struct skcipher_givcrypt_request *req);
  162. int (*givdecrypt)(struct skcipher_givcrypt_request *req);
  163. const char *geniv;
  164. unsigned int min_keysize;
  165. unsigned int max_keysize;
  166. unsigned int ivsize;
  167. };
  168. struct aead_alg {
  169. int (*setkey)(struct crypto_aead *tfm, const u8 *key,
  170. unsigned int keylen);
  171. int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize);
  172. int (*encrypt)(struct aead_request *req);
  173. int (*decrypt)(struct aead_request *req);
  174. int (*givencrypt)(struct aead_givcrypt_request *req);
  175. int (*givdecrypt)(struct aead_givcrypt_request *req);
  176. const char *geniv;
  177. unsigned int ivsize;
  178. unsigned int maxauthsize;
  179. };
  180. struct blkcipher_alg {
  181. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  182. unsigned int keylen);
  183. int (*encrypt)(struct blkcipher_desc *desc,
  184. struct scatterlist *dst, struct scatterlist *src,
  185. unsigned int nbytes);
  186. int (*decrypt)(struct blkcipher_desc *desc,
  187. struct scatterlist *dst, struct scatterlist *src,
  188. unsigned int nbytes);
  189. const char *geniv;
  190. unsigned int min_keysize;
  191. unsigned int max_keysize;
  192. unsigned int ivsize;
  193. };
  194. struct cipher_alg {
  195. unsigned int cia_min_keysize;
  196. unsigned int cia_max_keysize;
  197. int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  198. unsigned int keylen);
  199. void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  200. void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  201. };
  202. struct digest_alg {
  203. unsigned int dia_digestsize;
  204. void (*dia_init)(struct crypto_tfm *tfm);
  205. void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
  206. unsigned int len);
  207. void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
  208. int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  209. unsigned int keylen);
  210. };
  211. struct hash_alg {
  212. int (*init)(struct hash_desc *desc);
  213. int (*update)(struct hash_desc *desc, struct scatterlist *sg,
  214. unsigned int nbytes);
  215. int (*final)(struct hash_desc *desc, u8 *out);
  216. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  217. unsigned int nbytes, u8 *out);
  218. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  219. unsigned int keylen);
  220. unsigned int digestsize;
  221. };
  222. struct compress_alg {
  223. int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
  224. unsigned int slen, u8 *dst, unsigned int *dlen);
  225. int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
  226. unsigned int slen, u8 *dst, unsigned int *dlen);
  227. };
  228. #define cra_ablkcipher cra_u.ablkcipher
  229. #define cra_aead cra_u.aead
  230. #define cra_blkcipher cra_u.blkcipher
  231. #define cra_cipher cra_u.cipher
  232. #define cra_digest cra_u.digest
  233. #define cra_hash cra_u.hash
  234. #define cra_compress cra_u.compress
  235. struct crypto_alg {
  236. struct list_head cra_list;
  237. struct list_head cra_users;
  238. u32 cra_flags;
  239. unsigned int cra_blocksize;
  240. unsigned int cra_ctxsize;
  241. unsigned int cra_alignmask;
  242. int cra_priority;
  243. atomic_t cra_refcnt;
  244. char cra_name[CRYPTO_MAX_ALG_NAME];
  245. char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  246. const struct crypto_type *cra_type;
  247. union {
  248. struct ablkcipher_alg ablkcipher;
  249. struct aead_alg aead;
  250. struct blkcipher_alg blkcipher;
  251. struct cipher_alg cipher;
  252. struct digest_alg digest;
  253. struct hash_alg hash;
  254. struct compress_alg compress;
  255. } cra_u;
  256. int (*cra_init)(struct crypto_tfm *tfm);
  257. void (*cra_exit)(struct crypto_tfm *tfm);
  258. void (*cra_destroy)(struct crypto_alg *alg);
  259. struct module *cra_module;
  260. };
  261. /*
  262. * Algorithm registration interface.
  263. */
  264. int crypto_register_alg(struct crypto_alg *alg);
  265. int crypto_unregister_alg(struct crypto_alg *alg);
  266. /*
  267. * Algorithm query interface.
  268. */
  269. #ifdef CONFIG_CRYPTO
  270. int crypto_has_alg(const char *name, u32 type, u32 mask);
  271. #else
  272. static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
  273. {
  274. return 0;
  275. }
  276. #endif
  277. /*
  278. * Transforms: user-instantiated objects which encapsulate algorithms
  279. * and core processing logic. Managed via crypto_alloc_*() and
  280. * crypto_free_*(), as well as the various helpers below.
  281. */
  282. struct ablkcipher_tfm {
  283. int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
  284. unsigned int keylen);
  285. int (*encrypt)(struct ablkcipher_request *req);
  286. int (*decrypt)(struct ablkcipher_request *req);
  287. int (*givencrypt)(struct skcipher_givcrypt_request *req);
  288. int (*givdecrypt)(struct skcipher_givcrypt_request *req);
  289. struct crypto_ablkcipher *base;
  290. unsigned int ivsize;
  291. unsigned int reqsize;
  292. };
  293. struct aead_tfm {
  294. int (*setkey)(struct crypto_aead *tfm, const u8 *key,
  295. unsigned int keylen);
  296. int (*encrypt)(struct aead_request *req);
  297. int (*decrypt)(struct aead_request *req);
  298. int (*givencrypt)(struct aead_givcrypt_request *req);
  299. int (*givdecrypt)(struct aead_givcrypt_request *req);
  300. struct crypto_aead *base;
  301. unsigned int ivsize;
  302. unsigned int authsize;
  303. unsigned int reqsize;
  304. };
  305. struct blkcipher_tfm {
  306. void *iv;
  307. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  308. unsigned int keylen);
  309. int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  310. struct scatterlist *src, unsigned int nbytes);
  311. int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  312. struct scatterlist *src, unsigned int nbytes);
  313. };
  314. struct cipher_tfm {
  315. int (*cit_setkey)(struct crypto_tfm *tfm,
  316. const u8 *key, unsigned int keylen);
  317. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  318. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  319. };
  320. struct hash_tfm {
  321. int (*init)(struct hash_desc *desc);
  322. int (*update)(struct hash_desc *desc,
  323. struct scatterlist *sg, unsigned int nsg);
  324. int (*final)(struct hash_desc *desc, u8 *out);
  325. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  326. unsigned int nsg, u8 *out);
  327. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  328. unsigned int keylen);
  329. unsigned int digestsize;
  330. };
  331. struct compress_tfm {
  332. int (*cot_compress)(struct crypto_tfm *tfm,
  333. const u8 *src, unsigned int slen,
  334. u8 *dst, unsigned int *dlen);
  335. int (*cot_decompress)(struct crypto_tfm *tfm,
  336. const u8 *src, unsigned int slen,
  337. u8 *dst, unsigned int *dlen);
  338. };
  339. #define crt_ablkcipher crt_u.ablkcipher
  340. #define crt_aead crt_u.aead
  341. #define crt_blkcipher crt_u.blkcipher
  342. #define crt_cipher crt_u.cipher
  343. #define crt_hash crt_u.hash
  344. #define crt_compress crt_u.compress
  345. struct crypto_tfm {
  346. u32 crt_flags;
  347. union {
  348. struct ablkcipher_tfm ablkcipher;
  349. struct aead_tfm aead;
  350. struct blkcipher_tfm blkcipher;
  351. struct cipher_tfm cipher;
  352. struct hash_tfm hash;
  353. struct compress_tfm compress;
  354. } crt_u;
  355. struct crypto_alg *__crt_alg;
  356. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  357. };
  358. struct crypto_ablkcipher {
  359. struct crypto_tfm base;
  360. };
  361. struct crypto_aead {
  362. struct crypto_tfm base;
  363. };
  364. struct crypto_blkcipher {
  365. struct crypto_tfm base;
  366. };
  367. struct crypto_cipher {
  368. struct crypto_tfm base;
  369. };
  370. struct crypto_comp {
  371. struct crypto_tfm base;
  372. };
  373. struct crypto_hash {
  374. struct crypto_tfm base;
  375. };
  376. enum {
  377. CRYPTOA_UNSPEC,
  378. CRYPTOA_ALG,
  379. CRYPTOA_TYPE,
  380. CRYPTOA_U32,
  381. __CRYPTOA_MAX,
  382. };
  383. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  384. /* Maximum number of (rtattr) parameters for each template. */
  385. #define CRYPTO_MAX_ATTRS 32
  386. struct crypto_attr_alg {
  387. char name[CRYPTO_MAX_ALG_NAME];
  388. };
  389. struct crypto_attr_type {
  390. u32 type;
  391. u32 mask;
  392. };
  393. struct crypto_attr_u32 {
  394. u32 num;
  395. };
  396. /*
  397. * Transform user interface.
  398. */
  399. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  400. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  401. void crypto_free_tfm(struct crypto_tfm *tfm);
  402. /*
  403. * Transform helpers which query the underlying algorithm.
  404. */
  405. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  406. {
  407. return tfm->__crt_alg->cra_name;
  408. }
  409. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  410. {
  411. return tfm->__crt_alg->cra_driver_name;
  412. }
  413. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  414. {
  415. return tfm->__crt_alg->cra_priority;
  416. }
  417. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  418. {
  419. return module_name(tfm->__crt_alg->cra_module);
  420. }
  421. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  422. {
  423. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  424. }
  425. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  426. {
  427. return tfm->__crt_alg->cra_blocksize;
  428. }
  429. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  430. {
  431. return tfm->__crt_alg->cra_alignmask;
  432. }
  433. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  434. {
  435. return tfm->crt_flags;
  436. }
  437. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  438. {
  439. tfm->crt_flags |= flags;
  440. }
  441. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  442. {
  443. tfm->crt_flags &= ~flags;
  444. }
  445. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  446. {
  447. return tfm->__crt_ctx;
  448. }
  449. static inline unsigned int crypto_tfm_ctx_alignment(void)
  450. {
  451. struct crypto_tfm *tfm;
  452. return __alignof__(tfm->__crt_ctx);
  453. }
  454. /*
  455. * API wrappers.
  456. */
  457. static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
  458. struct crypto_tfm *tfm)
  459. {
  460. return (struct crypto_ablkcipher *)tfm;
  461. }
  462. static inline u32 crypto_skcipher_type(u32 type)
  463. {
  464. type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  465. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  466. return type;
  467. }
  468. static inline u32 crypto_skcipher_mask(u32 mask)
  469. {
  470. mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
  471. mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
  472. return mask;
  473. }
  474. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  475. u32 type, u32 mask);
  476. static inline struct crypto_tfm *crypto_ablkcipher_tfm(
  477. struct crypto_ablkcipher *tfm)
  478. {
  479. return &tfm->base;
  480. }
  481. static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm)
  482. {
  483. crypto_free_tfm(crypto_ablkcipher_tfm(tfm));
  484. }
  485. static inline int crypto_has_ablkcipher(const char *alg_name, u32 type,
  486. u32 mask)
  487. {
  488. return crypto_has_alg(alg_name, crypto_skcipher_type(type),
  489. crypto_skcipher_mask(mask));
  490. }
  491. static inline struct ablkcipher_tfm *crypto_ablkcipher_crt(
  492. struct crypto_ablkcipher *tfm)
  493. {
  494. return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
  495. }
  496. static inline unsigned int crypto_ablkcipher_ivsize(
  497. struct crypto_ablkcipher *tfm)
  498. {
  499. return crypto_ablkcipher_crt(tfm)->ivsize;
  500. }
  501. static inline unsigned int crypto_ablkcipher_blocksize(
  502. struct crypto_ablkcipher *tfm)
  503. {
  504. return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
  505. }
  506. static inline unsigned int crypto_ablkcipher_alignmask(
  507. struct crypto_ablkcipher *tfm)
  508. {
  509. return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
  510. }
  511. static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm)
  512. {
  513. return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
  514. }
  515. static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm,
  516. u32 flags)
  517. {
  518. crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
  519. }
  520. static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
  521. u32 flags)
  522. {
  523. crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
  524. }
  525. static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
  526. const u8 *key, unsigned int keylen)
  527. {
  528. struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
  529. return crt->setkey(crt->base, key, keylen);
  530. }
  531. static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
  532. struct ablkcipher_request *req)
  533. {
  534. return __crypto_ablkcipher_cast(req->base.tfm);
  535. }
  536. static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
  537. {
  538. struct ablkcipher_tfm *crt =
  539. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  540. return crt->encrypt(req);
  541. }
  542. static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
  543. {
  544. struct ablkcipher_tfm *crt =
  545. crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
  546. return crt->decrypt(req);
  547. }
  548. static inline unsigned int crypto_ablkcipher_reqsize(
  549. struct crypto_ablkcipher *tfm)
  550. {
  551. return crypto_ablkcipher_crt(tfm)->reqsize;
  552. }
  553. static inline void ablkcipher_request_set_tfm(
  554. struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
  555. {
  556. req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
  557. }
  558. static inline struct ablkcipher_request *ablkcipher_request_cast(
  559. struct crypto_async_request *req)
  560. {
  561. return container_of(req, struct ablkcipher_request, base);
  562. }
  563. static inline struct ablkcipher_request *ablkcipher_request_alloc(
  564. struct crypto_ablkcipher *tfm, gfp_t gfp)
  565. {
  566. struct ablkcipher_request *req;
  567. req = kmalloc(sizeof(struct ablkcipher_request) +
  568. crypto_ablkcipher_reqsize(tfm), gfp);
  569. if (likely(req))
  570. ablkcipher_request_set_tfm(req, tfm);
  571. return req;
  572. }
  573. static inline void ablkcipher_request_free(struct ablkcipher_request *req)
  574. {
  575. kfree(req);
  576. }
  577. static inline void ablkcipher_request_set_callback(
  578. struct ablkcipher_request *req,
  579. u32 flags, crypto_completion_t complete, void *data)
  580. {
  581. req->base.complete = complete;
  582. req->base.data = data;
  583. req->base.flags = flags;
  584. }
  585. static inline void ablkcipher_request_set_crypt(
  586. struct ablkcipher_request *req,
  587. struct scatterlist *src, struct scatterlist *dst,
  588. unsigned int nbytes, void *iv)
  589. {
  590. req->src = src;
  591. req->dst = dst;
  592. req->nbytes = nbytes;
  593. req->info = iv;
  594. }
  595. static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm)
  596. {
  597. return (struct crypto_aead *)tfm;
  598. }
  599. static inline struct crypto_aead *crypto_alloc_aead(const char *alg_name,
  600. u32 type, u32 mask)
  601. {
  602. type &= ~CRYPTO_ALG_TYPE_MASK;
  603. type |= CRYPTO_ALG_TYPE_AEAD;
  604. mask |= CRYPTO_ALG_TYPE_MASK;
  605. return __crypto_aead_cast(crypto_alloc_base(alg_name, type, mask));
  606. }
  607. static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm)
  608. {
  609. return &tfm->base;
  610. }
  611. static inline void crypto_free_aead(struct crypto_aead *tfm)
  612. {
  613. crypto_free_tfm(crypto_aead_tfm(tfm));
  614. }
  615. static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm)
  616. {
  617. return &crypto_aead_tfm(tfm)->crt_aead;
  618. }
  619. static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm)
  620. {
  621. return crypto_aead_crt(tfm)->ivsize;
  622. }
  623. static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm)
  624. {
  625. return crypto_aead_crt(tfm)->authsize;
  626. }
  627. static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm)
  628. {
  629. return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
  630. }
  631. static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm)
  632. {
  633. return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
  634. }
  635. static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm)
  636. {
  637. return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
  638. }
  639. static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags)
  640. {
  641. crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
  642. }
  643. static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags)
  644. {
  645. crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
  646. }
  647. static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  648. unsigned int keylen)
  649. {
  650. struct aead_tfm *crt = crypto_aead_crt(tfm);
  651. return crt->setkey(crt->base, key, keylen);
  652. }
  653. int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
  654. static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)
  655. {
  656. return __crypto_aead_cast(req->base.tfm);
  657. }
  658. static inline int crypto_aead_encrypt(struct aead_request *req)
  659. {
  660. return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
  661. }
  662. static inline int crypto_aead_decrypt(struct aead_request *req)
  663. {
  664. return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
  665. }
  666. static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm)
  667. {
  668. return crypto_aead_crt(tfm)->reqsize;
  669. }
  670. static inline void aead_request_set_tfm(struct aead_request *req,
  671. struct crypto_aead *tfm)
  672. {
  673. req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base);
  674. }
  675. static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
  676. gfp_t gfp)
  677. {
  678. struct aead_request *req;
  679. req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
  680. if (likely(req))
  681. aead_request_set_tfm(req, tfm);
  682. return req;
  683. }
  684. static inline void aead_request_free(struct aead_request *req)
  685. {
  686. kfree(req);
  687. }
  688. static inline void aead_request_set_callback(struct aead_request *req,
  689. u32 flags,
  690. crypto_completion_t complete,
  691. void *data)
  692. {
  693. req->base.complete = complete;
  694. req->base.data = data;
  695. req->base.flags = flags;
  696. }
  697. static inline void aead_request_set_crypt(struct aead_request *req,
  698. struct scatterlist *src,
  699. struct scatterlist *dst,
  700. unsigned int cryptlen, u8 *iv)
  701. {
  702. req->src = src;
  703. req->dst = dst;
  704. req->cryptlen = cryptlen;
  705. req->iv = iv;
  706. }
  707. static inline void aead_request_set_assoc(struct aead_request *req,
  708. struct scatterlist *assoc,
  709. unsigned int assoclen)
  710. {
  711. req->assoc = assoc;
  712. req->assoclen = assoclen;
  713. }
  714. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  715. struct crypto_tfm *tfm)
  716. {
  717. return (struct crypto_blkcipher *)tfm;
  718. }
  719. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  720. struct crypto_tfm *tfm)
  721. {
  722. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  723. return __crypto_blkcipher_cast(tfm);
  724. }
  725. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  726. const char *alg_name, u32 type, u32 mask)
  727. {
  728. type &= ~CRYPTO_ALG_TYPE_MASK;
  729. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  730. mask |= CRYPTO_ALG_TYPE_MASK;
  731. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  732. }
  733. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  734. struct crypto_blkcipher *tfm)
  735. {
  736. return &tfm->base;
  737. }
  738. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  739. {
  740. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  741. }
  742. static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
  743. {
  744. type &= ~CRYPTO_ALG_TYPE_MASK;
  745. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  746. mask |= CRYPTO_ALG_TYPE_MASK;
  747. return crypto_has_alg(alg_name, type, mask);
  748. }
  749. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  750. {
  751. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  752. }
  753. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  754. struct crypto_blkcipher *tfm)
  755. {
  756. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  757. }
  758. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  759. struct crypto_blkcipher *tfm)
  760. {
  761. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  762. }
  763. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  764. {
  765. return crypto_blkcipher_alg(tfm)->ivsize;
  766. }
  767. static inline unsigned int crypto_blkcipher_blocksize(
  768. struct crypto_blkcipher *tfm)
  769. {
  770. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  771. }
  772. static inline unsigned int crypto_blkcipher_alignmask(
  773. struct crypto_blkcipher *tfm)
  774. {
  775. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  776. }
  777. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  778. {
  779. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  780. }
  781. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  782. u32 flags)
  783. {
  784. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  785. }
  786. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  787. u32 flags)
  788. {
  789. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  790. }
  791. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  792. const u8 *key, unsigned int keylen)
  793. {
  794. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  795. key, keylen);
  796. }
  797. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  798. struct scatterlist *dst,
  799. struct scatterlist *src,
  800. unsigned int nbytes)
  801. {
  802. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  803. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  804. }
  805. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  806. struct scatterlist *dst,
  807. struct scatterlist *src,
  808. unsigned int nbytes)
  809. {
  810. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  811. }
  812. static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
  813. struct scatterlist *dst,
  814. struct scatterlist *src,
  815. unsigned int nbytes)
  816. {
  817. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  818. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  819. }
  820. static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
  821. struct scatterlist *dst,
  822. struct scatterlist *src,
  823. unsigned int nbytes)
  824. {
  825. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  826. }
  827. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  828. const u8 *src, unsigned int len)
  829. {
  830. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  831. }
  832. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  833. u8 *dst, unsigned int len)
  834. {
  835. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  836. }
  837. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  838. {
  839. return (struct crypto_cipher *)tfm;
  840. }
  841. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  842. {
  843. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  844. return __crypto_cipher_cast(tfm);
  845. }
  846. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  847. u32 type, u32 mask)
  848. {
  849. type &= ~CRYPTO_ALG_TYPE_MASK;
  850. type |= CRYPTO_ALG_TYPE_CIPHER;
  851. mask |= CRYPTO_ALG_TYPE_MASK;
  852. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  853. }
  854. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  855. {
  856. return &tfm->base;
  857. }
  858. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  859. {
  860. crypto_free_tfm(crypto_cipher_tfm(tfm));
  861. }
  862. static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
  863. {
  864. type &= ~CRYPTO_ALG_TYPE_MASK;
  865. type |= CRYPTO_ALG_TYPE_CIPHER;
  866. mask |= CRYPTO_ALG_TYPE_MASK;
  867. return crypto_has_alg(alg_name, type, mask);
  868. }
  869. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  870. {
  871. return &crypto_cipher_tfm(tfm)->crt_cipher;
  872. }
  873. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  874. {
  875. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  876. }
  877. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  878. {
  879. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  880. }
  881. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  882. {
  883. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  884. }
  885. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  886. u32 flags)
  887. {
  888. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  889. }
  890. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  891. u32 flags)
  892. {
  893. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  894. }
  895. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  896. const u8 *key, unsigned int keylen)
  897. {
  898. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  899. key, keylen);
  900. }
  901. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  902. u8 *dst, const u8 *src)
  903. {
  904. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  905. dst, src);
  906. }
  907. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  908. u8 *dst, const u8 *src)
  909. {
  910. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  911. dst, src);
  912. }
  913. static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
  914. {
  915. return (struct crypto_hash *)tfm;
  916. }
  917. static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
  918. {
  919. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
  920. CRYPTO_ALG_TYPE_HASH_MASK);
  921. return __crypto_hash_cast(tfm);
  922. }
  923. static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
  924. u32 type, u32 mask)
  925. {
  926. type &= ~CRYPTO_ALG_TYPE_MASK;
  927. mask &= ~CRYPTO_ALG_TYPE_MASK;
  928. type |= CRYPTO_ALG_TYPE_HASH;
  929. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  930. return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
  931. }
  932. static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
  933. {
  934. return &tfm->base;
  935. }
  936. static inline void crypto_free_hash(struct crypto_hash *tfm)
  937. {
  938. crypto_free_tfm(crypto_hash_tfm(tfm));
  939. }
  940. static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
  941. {
  942. type &= ~CRYPTO_ALG_TYPE_MASK;
  943. mask &= ~CRYPTO_ALG_TYPE_MASK;
  944. type |= CRYPTO_ALG_TYPE_HASH;
  945. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  946. return crypto_has_alg(alg_name, type, mask);
  947. }
  948. static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
  949. {
  950. return &crypto_hash_tfm(tfm)->crt_hash;
  951. }
  952. static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
  953. {
  954. return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
  955. }
  956. static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
  957. {
  958. return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
  959. }
  960. static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
  961. {
  962. return crypto_hash_crt(tfm)->digestsize;
  963. }
  964. static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
  965. {
  966. return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
  967. }
  968. static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
  969. {
  970. crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
  971. }
  972. static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
  973. {
  974. crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
  975. }
  976. static inline int crypto_hash_init(struct hash_desc *desc)
  977. {
  978. return crypto_hash_crt(desc->tfm)->init(desc);
  979. }
  980. static inline int crypto_hash_update(struct hash_desc *desc,
  981. struct scatterlist *sg,
  982. unsigned int nbytes)
  983. {
  984. return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
  985. }
  986. static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
  987. {
  988. return crypto_hash_crt(desc->tfm)->final(desc, out);
  989. }
  990. static inline int crypto_hash_digest(struct hash_desc *desc,
  991. struct scatterlist *sg,
  992. unsigned int nbytes, u8 *out)
  993. {
  994. return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
  995. }
  996. static inline int crypto_hash_setkey(struct crypto_hash *hash,
  997. const u8 *key, unsigned int keylen)
  998. {
  999. return crypto_hash_crt(hash)->setkey(hash, key, keylen);
  1000. }
  1001. static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
  1002. {
  1003. return (struct crypto_comp *)tfm;
  1004. }
  1005. static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
  1006. {
  1007. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
  1008. CRYPTO_ALG_TYPE_MASK);
  1009. return __crypto_comp_cast(tfm);
  1010. }
  1011. static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
  1012. u32 type, u32 mask)
  1013. {
  1014. type &= ~CRYPTO_ALG_TYPE_MASK;
  1015. type |= CRYPTO_ALG_TYPE_COMPRESS;
  1016. mask |= CRYPTO_ALG_TYPE_MASK;
  1017. return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
  1018. }
  1019. static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
  1020. {
  1021. return &tfm->base;
  1022. }
  1023. static inline void crypto_free_comp(struct crypto_comp *tfm)
  1024. {
  1025. crypto_free_tfm(crypto_comp_tfm(tfm));
  1026. }
  1027. static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
  1028. {
  1029. type &= ~CRYPTO_ALG_TYPE_MASK;
  1030. type |= CRYPTO_ALG_TYPE_COMPRESS;
  1031. mask |= CRYPTO_ALG_TYPE_MASK;
  1032. return crypto_has_alg(alg_name, type, mask);
  1033. }
  1034. static inline const char *crypto_comp_name(struct crypto_comp *tfm)
  1035. {
  1036. return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
  1037. }
  1038. static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
  1039. {
  1040. return &crypto_comp_tfm(tfm)->crt_compress;
  1041. }
  1042. static inline int crypto_comp_compress(struct crypto_comp *tfm,
  1043. const u8 *src, unsigned int slen,
  1044. u8 *dst, unsigned int *dlen)
  1045. {
  1046. return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
  1047. src, slen, dst, dlen);
  1048. }
  1049. static inline int crypto_comp_decompress(struct crypto_comp *tfm,
  1050. const u8 *src, unsigned int slen,
  1051. u8 *dst, unsigned int *dlen)
  1052. {
  1053. return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
  1054. src, slen, dst, dlen);
  1055. }
  1056. #endif /* _LINUX_CRYPTO_H */