crypto.h 32 KB

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