crypto.h 33 KB

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