crypto.h 34 KB

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