crypto.h 33 KB

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