crypto.h 34 KB

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