crypto.h 27 KB

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