crypto.h 33 KB

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