crypto.h 34 KB

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