crypto.h 32 KB

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