crypto.h 34 KB

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