crypto.h 32 KB

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