crypto.h 33 KB

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