crypto.h 34 KB

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