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