crypto.h 27 KB

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