crypto.h 25 KB

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