crypto.h 32 KB

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