crypto.h 32 KB

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