crypto.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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_has_alg(const char *name, u32 type, u32 mask);
  210. #else
  211. static inline int crypto_alg_available(const char *name, u32 flags)
  212. {
  213. return 0;
  214. }
  215. static inline int crypto_has_alg(const char *name, u32 type, u32 mask)
  216. {
  217. return 0;
  218. }
  219. #endif
  220. /*
  221. * Transforms: user-instantiated objects which encapsulate algorithms
  222. * and core processing logic. Managed via crypto_alloc_*() and
  223. * crypto_free_*(), as well as the various helpers below.
  224. */
  225. struct blkcipher_tfm {
  226. void *iv;
  227. int (*setkey)(struct crypto_tfm *tfm, const u8 *key,
  228. unsigned int keylen);
  229. int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  230. struct scatterlist *src, unsigned int nbytes);
  231. int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst,
  232. struct scatterlist *src, unsigned int nbytes);
  233. };
  234. struct cipher_tfm {
  235. void *cit_iv;
  236. unsigned int cit_ivsize;
  237. u32 cit_mode;
  238. int (*cit_setkey)(struct crypto_tfm *tfm,
  239. const u8 *key, unsigned int keylen);
  240. int (*cit_encrypt)(struct crypto_tfm *tfm,
  241. struct scatterlist *dst,
  242. struct scatterlist *src,
  243. unsigned int nbytes);
  244. int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
  245. struct scatterlist *dst,
  246. struct scatterlist *src,
  247. unsigned int nbytes, u8 *iv);
  248. int (*cit_decrypt)(struct crypto_tfm *tfm,
  249. struct scatterlist *dst,
  250. struct scatterlist *src,
  251. unsigned int nbytes);
  252. int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
  253. struct scatterlist *dst,
  254. struct scatterlist *src,
  255. unsigned int nbytes, u8 *iv);
  256. void (*cit_xor_block)(u8 *dst, const u8 *src);
  257. void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  258. void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  259. };
  260. struct hash_tfm {
  261. int (*init)(struct hash_desc *desc);
  262. int (*update)(struct hash_desc *desc,
  263. struct scatterlist *sg, unsigned int nsg);
  264. int (*final)(struct hash_desc *desc, u8 *out);
  265. int (*digest)(struct hash_desc *desc, struct scatterlist *sg,
  266. unsigned int nsg, u8 *out);
  267. int (*setkey)(struct crypto_hash *tfm, const u8 *key,
  268. unsigned int keylen);
  269. unsigned int digestsize;
  270. };
  271. struct compress_tfm {
  272. int (*cot_compress)(struct crypto_tfm *tfm,
  273. const u8 *src, unsigned int slen,
  274. u8 *dst, unsigned int *dlen);
  275. int (*cot_decompress)(struct crypto_tfm *tfm,
  276. const u8 *src, unsigned int slen,
  277. u8 *dst, unsigned int *dlen);
  278. };
  279. #define crt_blkcipher crt_u.blkcipher
  280. #define crt_cipher crt_u.cipher
  281. #define crt_hash crt_u.hash
  282. #define crt_compress crt_u.compress
  283. struct crypto_tfm {
  284. u32 crt_flags;
  285. union {
  286. struct blkcipher_tfm blkcipher;
  287. struct cipher_tfm cipher;
  288. struct hash_tfm hash;
  289. struct compress_tfm compress;
  290. } crt_u;
  291. struct crypto_alg *__crt_alg;
  292. void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
  293. };
  294. #define crypto_cipher crypto_tfm
  295. #define crypto_comp crypto_tfm
  296. struct crypto_blkcipher {
  297. struct crypto_tfm base;
  298. };
  299. struct crypto_hash {
  300. struct crypto_tfm base;
  301. };
  302. enum {
  303. CRYPTOA_UNSPEC,
  304. CRYPTOA_ALG,
  305. };
  306. struct crypto_attr_alg {
  307. char name[CRYPTO_MAX_ALG_NAME];
  308. };
  309. /*
  310. * Transform user interface.
  311. */
  312. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  313. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
  314. void crypto_free_tfm(struct crypto_tfm *tfm);
  315. /*
  316. * Transform helpers which query the underlying algorithm.
  317. */
  318. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  319. {
  320. return tfm->__crt_alg->cra_name;
  321. }
  322. static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
  323. {
  324. return tfm->__crt_alg->cra_driver_name;
  325. }
  326. static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
  327. {
  328. return tfm->__crt_alg->cra_priority;
  329. }
  330. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  331. {
  332. return module_name(tfm->__crt_alg->cra_module);
  333. }
  334. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  335. {
  336. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  337. }
  338. static unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
  339. __deprecated;
  340. static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
  341. {
  342. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  343. return tfm->__crt_alg->cra_cipher.cia_min_keysize;
  344. }
  345. static unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
  346. __deprecated;
  347. static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
  348. {
  349. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  350. return tfm->__crt_alg->cra_cipher.cia_max_keysize;
  351. }
  352. static unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm) __deprecated;
  353. static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
  354. {
  355. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  356. return tfm->crt_cipher.cit_ivsize;
  357. }
  358. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  359. {
  360. return tfm->__crt_alg->cra_blocksize;
  361. }
  362. static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
  363. {
  364. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  365. return tfm->__crt_alg->cra_digest.dia_digestsize;
  366. }
  367. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  368. {
  369. return tfm->__crt_alg->cra_alignmask;
  370. }
  371. static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
  372. {
  373. return tfm->crt_flags;
  374. }
  375. static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
  376. {
  377. tfm->crt_flags |= flags;
  378. }
  379. static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
  380. {
  381. tfm->crt_flags &= ~flags;
  382. }
  383. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  384. {
  385. return tfm->__crt_ctx;
  386. }
  387. static inline unsigned int crypto_tfm_ctx_alignment(void)
  388. {
  389. struct crypto_tfm *tfm;
  390. return __alignof__(tfm->__crt_ctx);
  391. }
  392. /*
  393. * API wrappers.
  394. */
  395. static inline struct crypto_blkcipher *__crypto_blkcipher_cast(
  396. struct crypto_tfm *tfm)
  397. {
  398. return (struct crypto_blkcipher *)tfm;
  399. }
  400. static inline struct crypto_blkcipher *crypto_blkcipher_cast(
  401. struct crypto_tfm *tfm)
  402. {
  403. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER);
  404. return __crypto_blkcipher_cast(tfm);
  405. }
  406. static inline struct crypto_blkcipher *crypto_alloc_blkcipher(
  407. const char *alg_name, u32 type, u32 mask)
  408. {
  409. type &= ~CRYPTO_ALG_TYPE_MASK;
  410. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  411. mask |= CRYPTO_ALG_TYPE_MASK;
  412. return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask));
  413. }
  414. static inline struct crypto_tfm *crypto_blkcipher_tfm(
  415. struct crypto_blkcipher *tfm)
  416. {
  417. return &tfm->base;
  418. }
  419. static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm)
  420. {
  421. crypto_free_tfm(crypto_blkcipher_tfm(tfm));
  422. }
  423. static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask)
  424. {
  425. type &= ~CRYPTO_ALG_TYPE_MASK;
  426. type |= CRYPTO_ALG_TYPE_BLKCIPHER;
  427. mask |= CRYPTO_ALG_TYPE_MASK;
  428. return crypto_has_alg(alg_name, type, mask);
  429. }
  430. static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm)
  431. {
  432. return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
  433. }
  434. static inline struct blkcipher_tfm *crypto_blkcipher_crt(
  435. struct crypto_blkcipher *tfm)
  436. {
  437. return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
  438. }
  439. static inline struct blkcipher_alg *crypto_blkcipher_alg(
  440. struct crypto_blkcipher *tfm)
  441. {
  442. return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
  443. }
  444. static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm)
  445. {
  446. return crypto_blkcipher_alg(tfm)->ivsize;
  447. }
  448. static inline unsigned int crypto_blkcipher_blocksize(
  449. struct crypto_blkcipher *tfm)
  450. {
  451. return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
  452. }
  453. static inline unsigned int crypto_blkcipher_alignmask(
  454. struct crypto_blkcipher *tfm)
  455. {
  456. return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
  457. }
  458. static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm)
  459. {
  460. return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
  461. }
  462. static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm,
  463. u32 flags)
  464. {
  465. crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
  466. }
  467. static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm,
  468. u32 flags)
  469. {
  470. crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
  471. }
  472. static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm,
  473. const u8 *key, unsigned int keylen)
  474. {
  475. return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
  476. key, keylen);
  477. }
  478. static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc,
  479. struct scatterlist *dst,
  480. struct scatterlist *src,
  481. unsigned int nbytes)
  482. {
  483. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  484. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  485. }
  486. static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc,
  487. struct scatterlist *dst,
  488. struct scatterlist *src,
  489. unsigned int nbytes)
  490. {
  491. return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes);
  492. }
  493. static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc,
  494. struct scatterlist *dst,
  495. struct scatterlist *src,
  496. unsigned int nbytes)
  497. {
  498. desc->info = crypto_blkcipher_crt(desc->tfm)->iv;
  499. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  500. }
  501. static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc,
  502. struct scatterlist *dst,
  503. struct scatterlist *src,
  504. unsigned int nbytes)
  505. {
  506. return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes);
  507. }
  508. static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm,
  509. const u8 *src, unsigned int len)
  510. {
  511. memcpy(crypto_blkcipher_crt(tfm)->iv, src, len);
  512. }
  513. static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm,
  514. u8 *dst, unsigned int len)
  515. {
  516. memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len);
  517. }
  518. static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm)
  519. {
  520. return (struct crypto_cipher *)tfm;
  521. }
  522. static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm)
  523. {
  524. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  525. return __crypto_cipher_cast(tfm);
  526. }
  527. static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name,
  528. u32 type, u32 mask)
  529. {
  530. type &= ~CRYPTO_ALG_TYPE_MASK;
  531. type |= CRYPTO_ALG_TYPE_CIPHER;
  532. mask |= CRYPTO_ALG_TYPE_MASK;
  533. return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask));
  534. }
  535. static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm)
  536. {
  537. return tfm;
  538. }
  539. static inline void crypto_free_cipher(struct crypto_cipher *tfm)
  540. {
  541. crypto_free_tfm(crypto_cipher_tfm(tfm));
  542. }
  543. static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask)
  544. {
  545. type &= ~CRYPTO_ALG_TYPE_MASK;
  546. type |= CRYPTO_ALG_TYPE_CIPHER;
  547. mask |= CRYPTO_ALG_TYPE_MASK;
  548. return crypto_has_alg(alg_name, type, mask);
  549. }
  550. static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm)
  551. {
  552. return &crypto_cipher_tfm(tfm)->crt_cipher;
  553. }
  554. static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm)
  555. {
  556. return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
  557. }
  558. static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm)
  559. {
  560. return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
  561. }
  562. static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm)
  563. {
  564. return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
  565. }
  566. static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm,
  567. u32 flags)
  568. {
  569. crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
  570. }
  571. static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm,
  572. u32 flags)
  573. {
  574. crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
  575. }
  576. static inline int crypto_cipher_setkey(struct crypto_cipher *tfm,
  577. const u8 *key, unsigned int keylen)
  578. {
  579. return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
  580. key, keylen);
  581. }
  582. static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm,
  583. u8 *dst, const u8 *src)
  584. {
  585. crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
  586. dst, src);
  587. }
  588. static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm,
  589. u8 *dst, const u8 *src)
  590. {
  591. crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
  592. dst, src);
  593. }
  594. static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm)
  595. {
  596. return (struct crypto_hash *)tfm;
  597. }
  598. static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm)
  599. {
  600. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) &
  601. CRYPTO_ALG_TYPE_HASH_MASK);
  602. return __crypto_hash_cast(tfm);
  603. }
  604. static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name,
  605. u32 type, u32 mask)
  606. {
  607. type &= ~CRYPTO_ALG_TYPE_MASK;
  608. type |= CRYPTO_ALG_TYPE_HASH;
  609. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  610. return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask));
  611. }
  612. static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm)
  613. {
  614. return &tfm->base;
  615. }
  616. static inline void crypto_free_hash(struct crypto_hash *tfm)
  617. {
  618. crypto_free_tfm(crypto_hash_tfm(tfm));
  619. }
  620. static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask)
  621. {
  622. type &= ~CRYPTO_ALG_TYPE_MASK;
  623. type |= CRYPTO_ALG_TYPE_HASH;
  624. mask |= CRYPTO_ALG_TYPE_HASH_MASK;
  625. return crypto_has_alg(alg_name, type, mask);
  626. }
  627. static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm)
  628. {
  629. return &crypto_hash_tfm(tfm)->crt_hash;
  630. }
  631. static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm)
  632. {
  633. return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
  634. }
  635. static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm)
  636. {
  637. return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
  638. }
  639. static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm)
  640. {
  641. return crypto_hash_crt(tfm)->digestsize;
  642. }
  643. static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm)
  644. {
  645. return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
  646. }
  647. static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags)
  648. {
  649. crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
  650. }
  651. static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags)
  652. {
  653. crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
  654. }
  655. static inline int crypto_hash_init(struct hash_desc *desc)
  656. {
  657. return crypto_hash_crt(desc->tfm)->init(desc);
  658. }
  659. static inline int crypto_hash_update(struct hash_desc *desc,
  660. struct scatterlist *sg,
  661. unsigned int nbytes)
  662. {
  663. return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes);
  664. }
  665. static inline int crypto_hash_final(struct hash_desc *desc, u8 *out)
  666. {
  667. return crypto_hash_crt(desc->tfm)->final(desc, out);
  668. }
  669. static inline int crypto_hash_digest(struct hash_desc *desc,
  670. struct scatterlist *sg,
  671. unsigned int nbytes, u8 *out)
  672. {
  673. return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out);
  674. }
  675. static inline int crypto_hash_setkey(struct crypto_hash *hash,
  676. const u8 *key, unsigned int keylen)
  677. {
  678. return crypto_hash_crt(hash)->setkey(hash, key, keylen);
  679. }
  680. static int crypto_cipher_encrypt(struct crypto_tfm *tfm,
  681. struct scatterlist *dst,
  682. struct scatterlist *src,
  683. unsigned int nbytes) __deprecated;
  684. static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
  685. struct scatterlist *dst,
  686. struct scatterlist *src,
  687. unsigned int nbytes)
  688. {
  689. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  690. return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
  691. }
  692. static int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
  693. struct scatterlist *dst,
  694. struct scatterlist *src,
  695. unsigned int nbytes, u8 *iv) __deprecated;
  696. static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
  697. struct scatterlist *dst,
  698. struct scatterlist *src,
  699. unsigned int nbytes, u8 *iv)
  700. {
  701. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  702. return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
  703. }
  704. static int crypto_cipher_decrypt(struct crypto_tfm *tfm,
  705. struct scatterlist *dst,
  706. struct scatterlist *src,
  707. unsigned int nbytes) __deprecated;
  708. static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
  709. struct scatterlist *dst,
  710. struct scatterlist *src,
  711. unsigned int nbytes)
  712. {
  713. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  714. return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
  715. }
  716. static int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
  717. struct scatterlist *dst,
  718. struct scatterlist *src,
  719. unsigned int nbytes, u8 *iv) __deprecated;
  720. static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
  721. struct scatterlist *dst,
  722. struct scatterlist *src,
  723. unsigned int nbytes, u8 *iv)
  724. {
  725. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  726. return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
  727. }
  728. static void crypto_cipher_set_iv(struct crypto_tfm *tfm,
  729. const u8 *src, unsigned int len) __deprecated;
  730. static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
  731. const u8 *src, unsigned int len)
  732. {
  733. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  734. memcpy(tfm->crt_cipher.cit_iv, src, len);
  735. }
  736. static void crypto_cipher_get_iv(struct crypto_tfm *tfm,
  737. u8 *dst, unsigned int len) __deprecated;
  738. static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
  739. u8 *dst, unsigned int len)
  740. {
  741. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  742. memcpy(dst, tfm->crt_cipher.cit_iv, len);
  743. }
  744. static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
  745. {
  746. return (struct crypto_comp *)tfm;
  747. }
  748. static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm)
  749. {
  750. BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) &
  751. CRYPTO_ALG_TYPE_MASK);
  752. return __crypto_comp_cast(tfm);
  753. }
  754. static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
  755. u32 type, u32 mask)
  756. {
  757. type &= ~CRYPTO_ALG_TYPE_MASK;
  758. type |= CRYPTO_ALG_TYPE_COMPRESS;
  759. mask |= CRYPTO_ALG_TYPE_MASK;
  760. return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
  761. }
  762. static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
  763. {
  764. return tfm;
  765. }
  766. static inline void crypto_free_comp(struct crypto_comp *tfm)
  767. {
  768. crypto_free_tfm(crypto_comp_tfm(tfm));
  769. }
  770. static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
  771. {
  772. type &= ~CRYPTO_ALG_TYPE_MASK;
  773. type |= CRYPTO_ALG_TYPE_COMPRESS;
  774. mask |= CRYPTO_ALG_TYPE_MASK;
  775. return crypto_has_alg(alg_name, type, mask);
  776. }
  777. static inline const char *crypto_comp_name(struct crypto_comp *tfm)
  778. {
  779. return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
  780. }
  781. static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm)
  782. {
  783. return &crypto_comp_tfm(tfm)->crt_compress;
  784. }
  785. static inline int crypto_comp_compress(struct crypto_comp *tfm,
  786. const u8 *src, unsigned int slen,
  787. u8 *dst, unsigned int *dlen)
  788. {
  789. return crypto_comp_crt(tfm)->cot_compress(tfm, src, slen, dst, dlen);
  790. }
  791. static inline int crypto_comp_decompress(struct crypto_comp *tfm,
  792. const u8 *src, unsigned int slen,
  793. u8 *dst, unsigned int *dlen)
  794. {
  795. return crypto_comp_crt(tfm)->cot_decompress(tfm, src, slen, dst, dlen);
  796. }
  797. #endif /* _LINUX_CRYPTO_H */