crypto.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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_DIGEST 0x00000002
  32. #define CRYPTO_ALG_TYPE_HASH 0x00000003
  33. #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
  34. #define CRYPTO_ALG_TYPE_COMPRESS 0x00000005
  35. #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
  36. #define CRYPTO_ALG_LARVAL 0x00000010
  37. #define CRYPTO_ALG_DEAD 0x00000020
  38. #define CRYPTO_ALG_DYING 0x00000040
  39. #define CRYPTO_ALG_ASYNC 0x00000080
  40. /*
  41. * Set this bit if and only if the algorithm requires another algorithm of
  42. * the same type to handle corner cases.
  43. */
  44. #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
  45. /*
  46. * Transform masks and values (for crt_flags).
  47. */
  48. #define CRYPTO_TFM_REQ_MASK 0x000fff00
  49. #define CRYPTO_TFM_RES_MASK 0xfff00000
  50. #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
  51. #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
  52. #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
  53. #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
  54. #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
  55. #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
  56. #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
  57. /*
  58. * Miscellaneous stuff.
  59. */
  60. #define CRYPTO_MAX_ALG_NAME 64
  61. /*
  62. * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
  63. * declaration) is used to ensure that the crypto_tfm context structure is
  64. * aligned correctly for the given architecture so that there are no alignment
  65. * faults for C data types. In particular, this is required on platforms such
  66. * as arm where pointers are 32-bit aligned but there are data types such as
  67. * u64 which require 64-bit alignment.
  68. */
  69. #if defined(ARCH_KMALLOC_MINALIGN)
  70. #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
  71. #elif defined(ARCH_SLAB_MINALIGN)
  72. #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
  73. #endif
  74. #ifdef CRYPTO_MINALIGN
  75. #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
  76. #else
  77. #define CRYPTO_MINALIGN_ATTR
  78. #endif
  79. struct scatterlist;
  80. struct crypto_blkcipher;
  81. struct crypto_hash;
  82. struct crypto_tfm;
  83. struct crypto_type;
  84. struct blkcipher_desc {
  85. struct crypto_blkcipher *tfm;
  86. void *info;
  87. u32 flags;
  88. };
  89. struct cipher_desc {
  90. struct crypto_tfm *tfm;
  91. void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  92. unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
  93. const u8 *src, unsigned int nbytes);
  94. void *info;
  95. };
  96. struct hash_desc {
  97. struct crypto_hash *tfm;
  98. u32 flags;
  99. };
  100. /*
  101. * Algorithms: modular crypto algorithm implementations, managed
  102. * via crypto_register_alg() and crypto_unregister_alg().
  103. */
  104. struct blkcipher_alg {
  105. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  106. unsigned int keylen);
  107. int (*encrypt)(struct blkcipher_desc *desc,
  108. struct scatterlist *dst, struct scatterlist *src,
  109. unsigned int nbytes);
  110. int (*decrypt)(struct blkcipher_desc *desc,
  111. struct scatterlist *dst, struct scatterlist *src,
  112. unsigned int nbytes);
  113. unsigned int min_keysize;
  114. unsigned int max_keysize;
  115. unsigned int ivsize;
  116. };
  117. struct cipher_alg {
  118. unsigned int cia_min_keysize;
  119. unsigned int cia_max_keysize;
  120. int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  121. unsigned int keylen);
  122. void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  123. void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  124. };
  125. struct digest_alg {
  126. unsigned int dia_digestsize;
  127. void (*dia_init)(struct crypto_tfm *tfm);
  128. void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
  129. unsigned int len);
  130. void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
  131. int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  132. unsigned int keylen);
  133. };
  134. struct hash_alg {
  135. int (*init)(struct hash_desc *desc);
  136. int (*update)(struct hash_desc *desc, struct scatterlist *sg,
  137. unsigned int nbytes);
  138. int (*final)(struct hash_desc *desc, u8 *out);
  139. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  140. unsigned int nbytes, u8 *out);
  141. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  142. unsigned int keylen);
  143. unsigned int digestsize;
  144. };
  145. struct compress_alg {
  146. int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
  147. unsigned int slen, u8 *dst, unsigned int *dlen);
  148. int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
  149. unsigned int slen, u8 *dst, unsigned int *dlen);
  150. };
  151. #define cra_blkcipher cra_u.blkcipher
  152. #define cra_cipher cra_u.cipher
  153. #define cra_digest cra_u.digest
  154. #define cra_hash cra_u.hash
  155. #define cra_compress cra_u.compress
  156. struct crypto_alg {
  157. struct list_head cra_list;
  158. struct list_head cra_users;
  159. u32 cra_flags;
  160. unsigned int cra_blocksize;
  161. unsigned int cra_ctxsize;
  162. unsigned int cra_alignmask;
  163. int cra_priority;
  164. atomic_t cra_refcnt;
  165. char cra_name[CRYPTO_MAX_ALG_NAME];
  166. char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  167. const struct crypto_type *cra_type;
  168. union {
  169. struct blkcipher_alg blkcipher;
  170. struct cipher_alg cipher;
  171. struct digest_alg digest;
  172. struct hash_alg hash;
  173. struct compress_alg compress;
  174. } cra_u;
  175. int (*cra_init)(struct crypto_tfm *tfm);
  176. void (*cra_exit)(struct crypto_tfm *tfm);
  177. void (*cra_destroy)(struct crypto_alg *alg);
  178. struct module *cra_module;
  179. };
  180. /*
  181. * Algorithm registration interface.
  182. */
  183. int crypto_register_alg(struct crypto_alg *alg);
  184. int crypto_unregister_alg(struct crypto_alg *alg);
  185. /*
  186. * Algorithm query interface.
  187. */
  188. #ifdef CONFIG_CRYPTO
  189. int crypto_has_alg(const char *name, u32 type, u32 mask);
  190. #else
  191. static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
  192. {
  193. return 0;
  194. }
  195. #endif
  196. /*
  197. * Transforms: user-instantiated objects which encapsulate algorithms
  198. * and core processing logic. Managed via crypto_alloc_*() and
  199. * crypto_free_*(), as well as the various helpers below.
  200. */
  201. struct blkcipher_tfm {
  202. void *iv;
  203. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  204. unsigned int keylen);
  205. int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  206. struct scatterlist *src, unsigned int nbytes);
  207. int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  208. struct scatterlist *src, unsigned int nbytes);
  209. };
  210. struct cipher_tfm {
  211. void *cit_iv;
  212. unsigned int cit_ivsize;
  213. u32 cit_mode;
  214. int (*cit_setkey)(struct crypto_tfm *tfm,
  215. const u8 *key, unsigned int keylen);
  216. int (*cit_encrypt)(struct crypto_tfm *tfm,
  217. struct scatterlist *dst,
  218. struct scatterlist *src,
  219. unsigned int nbytes);
  220. int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
  221. struct scatterlist *dst,
  222. struct scatterlist *src,
  223. unsigned int nbytes, u8 *iv);
  224. int (*cit_decrypt)(struct crypto_tfm *tfm,
  225. struct scatterlist *dst,
  226. struct scatterlist *src,
  227. unsigned int nbytes);
  228. int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
  229. struct scatterlist *dst,
  230. struct scatterlist *src,
  231. unsigned int nbytes, u8 *iv);
  232. void (*cit_xor_block)(u8 *dst, const u8 *src);
  233. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  234. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  235. };
  236. struct hash_tfm {
  237. int (*init)(struct hash_desc *desc);
  238. int (*update)(struct hash_desc *desc,
  239. struct scatterlist *sg, unsigned int nsg);
  240. int (*final)(struct hash_desc *desc, u8 *out);
  241. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  242. unsigned int nsg, u8 *out);
  243. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  244. unsigned int keylen);
  245. unsigned int digestsize;
  246. };
  247. struct compress_tfm {
  248. int (*cot_compress)(struct crypto_tfm *tfm,
  249. const u8 *src, unsigned int slen,
  250. u8 *dst, unsigned int *dlen);
  251. int (*cot_decompress)(struct crypto_tfm *tfm,
  252. const u8 *src, unsigned int slen,
  253. u8 *dst, unsigned int *dlen);
  254. };
  255. #define crt_blkcipher crt_u.blkcipher
  256. #define crt_cipher crt_u.cipher
  257. #define crt_hash crt_u.hash
  258. #define crt_compress crt_u.compress
  259. struct crypto_tfm {
  260. u32 crt_flags;
  261. union {
  262. struct blkcipher_tfm blkcipher;
  263. struct cipher_tfm cipher;
  264. struct hash_tfm hash;
  265. struct compress_tfm compress;
  266. } crt_u;
  267. struct crypto_alg *__crt_alg;
  268. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  269. };
  270. struct crypto_blkcipher {
  271. struct crypto_tfm base;
  272. };
  273. struct crypto_cipher {
  274. struct crypto_tfm base;
  275. };
  276. struct crypto_comp {
  277. struct crypto_tfm base;
  278. };
  279. struct crypto_hash {
  280. struct crypto_tfm base;
  281. };
  282. enum {
  283. CRYPTOA_UNSPEC,
  284. CRYPTOA_ALG,
  285. };
  286. struct crypto_attr_alg {
  287. char name[CRYPTO_MAX_ALG_NAME];
  288. };
  289. /*
  290. * Transform user interface.
  291. */
  292. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  293. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  294. void crypto_free_tfm(struct crypto_tfm *tfm);
  295. /*
  296. * Transform helpers which query the underlying algorithm.
  297. */
  298. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  299. {
  300. return tfm->__crt_alg->cra_name;
  301. }
  302. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  303. {
  304. return tfm->__crt_alg->cra_driver_name;
  305. }
  306. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  307. {
  308. return tfm->__crt_alg->cra_priority;
  309. }
  310. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  311. {
  312. return module_name(tfm->__crt_alg->cra_module);
  313. }
  314. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  315. {
  316. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  317. }
  318. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  319. {
  320. return tfm->__crt_alg->cra_blocksize;
  321. }
  322. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  323. {
  324. return tfm->__crt_alg->cra_alignmask;
  325. }
  326. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  327. {
  328. return tfm->crt_flags;
  329. }
  330. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  331. {
  332. tfm->crt_flags |= flags;
  333. }
  334. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  335. {
  336. tfm->crt_flags &= ~flags;
  337. }
  338. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  339. {
  340. return tfm->__crt_ctx;
  341. }
  342. static inline unsigned int crypto_tfm_ctx_alignment(void)
  343. {
  344. struct crypto_tfm *tfm;
  345. return __alignof__(tfm->__crt_ctx);
  346. }
  347. /*
  348. * API wrappers.
  349. */
  350. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  351. struct crypto_tfm *tfm)
  352. {
  353. return (struct crypto_blkcipher *)tfm;
  354. }
  355. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  356. struct crypto_tfm *tfm)
  357. {
  358. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  359. return __crypto_blkcipher_cast(tfm);
  360. }
  361. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  362. const char *alg_name, u32 type, u32 mask)
  363. {
  364. type &= ~CRYPTO_ALG_TYPE_MASK;
  365. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  366. mask |= CRYPTO_ALG_TYPE_MASK;
  367. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  368. }
  369. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  370. struct crypto_blkcipher *tfm)
  371. {
  372. return &tfm->base;
  373. }
  374. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  375. {
  376. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  377. }
  378. static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
  379. {
  380. type &= ~CRYPTO_ALG_TYPE_MASK;
  381. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  382. mask |= CRYPTO_ALG_TYPE_MASK;
  383. return crypto_has_alg(alg_name, type, mask);
  384. }
  385. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  386. {
  387. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  388. }
  389. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  390. struct crypto_blkcipher *tfm)
  391. {
  392. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  393. }
  394. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  395. struct crypto_blkcipher *tfm)
  396. {
  397. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  398. }
  399. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  400. {
  401. return crypto_blkcipher_alg(tfm)->ivsize;
  402. }
  403. static inline unsigned int crypto_blkcipher_blocksize(
  404. struct crypto_blkcipher *tfm)
  405. {
  406. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  407. }
  408. static inline unsigned int crypto_blkcipher_alignmask(
  409. struct crypto_blkcipher *tfm)
  410. {
  411. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  412. }
  413. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  414. {
  415. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  416. }
  417. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  418. u32 flags)
  419. {
  420. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  421. }
  422. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  423. u32 flags)
  424. {
  425. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  426. }
  427. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  428. const u8 *key, unsigned int keylen)
  429. {
  430. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  431. key, keylen);
  432. }
  433. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  434. struct scatterlist *dst,
  435. struct scatterlist *src,
  436. unsigned int nbytes)
  437. {
  438. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  439. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  440. }
  441. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  442. struct scatterlist *dst,
  443. struct scatterlist *src,
  444. unsigned int nbytes)
  445. {
  446. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  447. }
  448. static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
  449. struct scatterlist *dst,
  450. struct scatterlist *src,
  451. unsigned int nbytes)
  452. {
  453. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  454. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  455. }
  456. static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
  457. struct scatterlist *dst,
  458. struct scatterlist *src,
  459. unsigned int nbytes)
  460. {
  461. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  462. }
  463. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  464. const u8 *src, unsigned int len)
  465. {
  466. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  467. }
  468. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  469. u8 *dst, unsigned int len)
  470. {
  471. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  472. }
  473. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  474. {
  475. return (struct crypto_cipher *)tfm;
  476. }
  477. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  478. {
  479. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  480. return __crypto_cipher_cast(tfm);
  481. }
  482. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  483. u32 type, u32 mask)
  484. {
  485. type &= ~CRYPTO_ALG_TYPE_MASK;
  486. type |= CRYPTO_ALG_TYPE_CIPHER;
  487. mask |= CRYPTO_ALG_TYPE_MASK;
  488. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  489. }
  490. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  491. {
  492. return &tfm->base;
  493. }
  494. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  495. {
  496. crypto_free_tfm(crypto_cipher_tfm(tfm));
  497. }
  498. static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
  499. {
  500. type &= ~CRYPTO_ALG_TYPE_MASK;
  501. type |= CRYPTO_ALG_TYPE_CIPHER;
  502. mask |= CRYPTO_ALG_TYPE_MASK;
  503. return crypto_has_alg(alg_name, type, mask);
  504. }
  505. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  506. {
  507. return &crypto_cipher_tfm(tfm)->crt_cipher;
  508. }
  509. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  510. {
  511. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  512. }
  513. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  514. {
  515. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  516. }
  517. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  518. {
  519. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  520. }
  521. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  522. u32 flags)
  523. {
  524. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  525. }
  526. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  527. u32 flags)
  528. {
  529. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  530. }
  531. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  532. const u8 *key, unsigned int keylen)
  533. {
  534. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  535. key, keylen);
  536. }
  537. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  538. u8 *dst, const u8 *src)
  539. {
  540. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  541. dst, src);
  542. }
  543. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  544. u8 *dst, const u8 *src)
  545. {
  546. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  547. dst, src);
  548. }
  549. static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
  550. {
  551. return (struct crypto_hash *)tfm;
  552. }
  553. static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
  554. {
  555. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
  556. CRYPTO_ALG_TYPE_HASH_MASK);
  557. return __crypto_hash_cast(tfm);
  558. }
  559. static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
  560. u32 type, u32 mask)
  561. {
  562. type &= ~CRYPTO_ALG_TYPE_MASK;
  563. type |= CRYPTO_ALG_TYPE_HASH;
  564. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  565. return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
  566. }
  567. static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
  568. {
  569. return &tfm->base;
  570. }
  571. static inline void crypto_free_hash(struct crypto_hash *tfm)
  572. {
  573. crypto_free_tfm(crypto_hash_tfm(tfm));
  574. }
  575. static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
  576. {
  577. type &= ~CRYPTO_ALG_TYPE_MASK;
  578. type |= CRYPTO_ALG_TYPE_HASH;
  579. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  580. return crypto_has_alg(alg_name, type, mask);
  581. }
  582. static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
  583. {
  584. return &crypto_hash_tfm(tfm)->crt_hash;
  585. }
  586. static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
  587. {
  588. return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
  589. }
  590. static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
  591. {
  592. return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
  593. }
  594. static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
  595. {
  596. return crypto_hash_crt(tfm)->digestsize;
  597. }
  598. static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
  599. {
  600. return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
  601. }
  602. static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
  603. {
  604. crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
  605. }
  606. static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
  607. {
  608. crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
  609. }
  610. static inline int crypto_hash_init(struct hash_desc *desc)
  611. {
  612. return crypto_hash_crt(desc->tfm)->init(desc);
  613. }
  614. static inline int crypto_hash_update(struct hash_desc *desc,
  615. struct scatterlist *sg,
  616. unsigned int nbytes)
  617. {
  618. return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
  619. }
  620. static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
  621. {
  622. return crypto_hash_crt(desc->tfm)->final(desc, out);
  623. }
  624. static inline int crypto_hash_digest(struct hash_desc *desc,
  625. struct scatterlist *sg,
  626. unsigned int nbytes, u8 *out)
  627. {
  628. return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
  629. }
  630. static inline int crypto_hash_setkey(struct crypto_hash *hash,
  631. const u8 *key, unsigned int keylen)
  632. {
  633. return crypto_hash_crt(hash)->setkey(hash, key, keylen);
  634. }
  635. static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
  636. {
  637. return (struct crypto_comp *)tfm;
  638. }
  639. static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
  640. {
  641. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
  642. CRYPTO_ALG_TYPE_MASK);
  643. return __crypto_comp_cast(tfm);
  644. }
  645. static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
  646. u32 type, u32 mask)
  647. {
  648. type &= ~CRYPTO_ALG_TYPE_MASK;
  649. type |= CRYPTO_ALG_TYPE_COMPRESS;
  650. mask |= CRYPTO_ALG_TYPE_MASK;
  651. return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
  652. }
  653. static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
  654. {
  655. return &tfm->base;
  656. }
  657. static inline void crypto_free_comp(struct crypto_comp *tfm)
  658. {
  659. crypto_free_tfm(crypto_comp_tfm(tfm));
  660. }
  661. static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
  662. {
  663. type &= ~CRYPTO_ALG_TYPE_MASK;
  664. type |= CRYPTO_ALG_TYPE_COMPRESS;
  665. mask |= CRYPTO_ALG_TYPE_MASK;
  666. return crypto_has_alg(alg_name, type, mask);
  667. }
  668. static inline const char *crypto_comp_name(struct crypto_comp *tfm)
  669. {
  670. return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
  671. }
  672. static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
  673. {
  674. return &crypto_comp_tfm(tfm)->crt_compress;
  675. }
  676. static inline int crypto_comp_compress(struct crypto_comp *tfm,
  677. const u8 *src, unsigned int slen,
  678. u8 *dst, unsigned int *dlen)
  679. {
  680. return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
  681. src, slen, dst, dlen);
  682. }
  683. static inline int crypto_comp_decompress(struct crypto_comp *tfm,
  684. const u8 *src, unsigned int slen,
  685. u8 *dst, unsigned int *dlen)
  686. {
  687. return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
  688. src, slen, dst, dlen);
  689. }
  690. #endif /* _LINUX_CRYPTO_H */