crypto.h 34 KB

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