crypto.h 36 KB

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