crypto.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/types.h>
  22. #include <linux/list.h>
  23. #include <linux/string.h>
  24. #include <asm/page.h>
  25. /*
  26. * Algorithm masks and types.
  27. */
  28. #define CRYPTO_ALG_TYPE_MASK 0x000000ff
  29. #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
  30. #define CRYPTO_ALG_TYPE_DIGEST 0x00000002
  31. #define CRYPTO_ALG_TYPE_COMPRESS 0x00000004
  32. /*
  33. * Transform masks and values (for crt_flags).
  34. */
  35. #define CRYPTO_TFM_MODE_MASK 0x000000ff
  36. #define CRYPTO_TFM_REQ_MASK 0x000fff00
  37. #define CRYPTO_TFM_RES_MASK 0xfff00000
  38. #define CRYPTO_TFM_MODE_ECB 0x00000001
  39. #define CRYPTO_TFM_MODE_CBC 0x00000002
  40. #define CRYPTO_TFM_MODE_CFB 0x00000004
  41. #define CRYPTO_TFM_MODE_CTR 0x00000008
  42. #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
  43. #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
  44. #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
  45. #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
  46. #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
  47. #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
  48. #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
  49. /*
  50. * Miscellaneous stuff.
  51. */
  52. #define CRYPTO_UNSPEC 0
  53. #define CRYPTO_MAX_ALG_NAME 64
  54. #define CRYPTO_DIR_ENCRYPT 1
  55. #define CRYPTO_DIR_DECRYPT 0
  56. struct scatterlist;
  57. struct crypto_tfm;
  58. struct cipher_desc {
  59. struct crypto_tfm *tfm;
  60. void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  61. unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst,
  62. const u8 *src, unsigned int nbytes);
  63. void *info;
  64. };
  65. /*
  66. * Algorithms: modular crypto algorithm implementations, managed
  67. * via crypto_register_alg() and crypto_unregister_alg().
  68. */
  69. struct cipher_alg {
  70. unsigned int cia_min_keysize;
  71. unsigned int cia_max_keysize;
  72. int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  73. unsigned int keylen, u32 *flags);
  74. void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  75. void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
  76. unsigned int (*cia_encrypt_ecb)(const struct cipher_desc *desc,
  77. u8 *dst, const u8 *src,
  78. unsigned int nbytes);
  79. unsigned int (*cia_decrypt_ecb)(const struct cipher_desc *desc,
  80. u8 *dst, const u8 *src,
  81. unsigned int nbytes);
  82. unsigned int (*cia_encrypt_cbc)(const struct cipher_desc *desc,
  83. u8 *dst, const u8 *src,
  84. unsigned int nbytes);
  85. unsigned int (*cia_decrypt_cbc)(const struct cipher_desc *desc,
  86. u8 *dst, const u8 *src,
  87. unsigned int nbytes);
  88. };
  89. struct digest_alg {
  90. unsigned int dia_digestsize;
  91. void (*dia_init)(struct crypto_tfm *tfm);
  92. void (*dia_update)(struct crypto_tfm *tfm, const u8 *data,
  93. unsigned int len);
  94. void (*dia_final)(struct crypto_tfm *tfm, u8 *out);
  95. int (*dia_setkey)(struct crypto_tfm *tfm, const u8 *key,
  96. unsigned int keylen, u32 *flags);
  97. };
  98. struct compress_alg {
  99. int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
  100. unsigned int slen, u8 *dst, unsigned int *dlen);
  101. int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
  102. unsigned int slen, u8 *dst, unsigned int *dlen);
  103. };
  104. #define cra_cipher cra_u.cipher
  105. #define cra_digest cra_u.digest
  106. #define cra_compress cra_u.compress
  107. struct crypto_alg {
  108. struct list_head cra_list;
  109. u32 cra_flags;
  110. unsigned int cra_blocksize;
  111. unsigned int cra_ctxsize;
  112. unsigned int cra_alignmask;
  113. int cra_priority;
  114. char cra_name[CRYPTO_MAX_ALG_NAME];
  115. char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  116. union {
  117. struct cipher_alg cipher;
  118. struct digest_alg digest;
  119. struct compress_alg compress;
  120. } cra_u;
  121. int (*cra_init)(struct crypto_tfm *tfm);
  122. void (*cra_exit)(struct crypto_tfm *tfm);
  123. struct module *cra_module;
  124. };
  125. /*
  126. * Algorithm registration interface.
  127. */
  128. int crypto_register_alg(struct crypto_alg *alg);
  129. int crypto_unregister_alg(struct crypto_alg *alg);
  130. /*
  131. * Algorithm query interface.
  132. */
  133. #ifdef CONFIG_CRYPTO
  134. int crypto_alg_available(const char *name, u32 flags);
  135. #else
  136. static inline int crypto_alg_available(const char *name, u32 flags)
  137. {
  138. return 0;
  139. }
  140. #endif
  141. /*
  142. * Transforms: user-instantiated objects which encapsulate algorithms
  143. * and core processing logic. Managed via crypto_alloc_tfm() and
  144. * crypto_free_tfm(), as well as the various helpers below.
  145. */
  146. struct cipher_tfm {
  147. void *cit_iv;
  148. unsigned int cit_ivsize;
  149. u32 cit_mode;
  150. int (*cit_setkey)(struct crypto_tfm *tfm,
  151. const u8 *key, unsigned int keylen);
  152. int (*cit_encrypt)(struct crypto_tfm *tfm,
  153. struct scatterlist *dst,
  154. struct scatterlist *src,
  155. unsigned int nbytes);
  156. int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
  157. struct scatterlist *dst,
  158. struct scatterlist *src,
  159. unsigned int nbytes, u8 *iv);
  160. int (*cit_decrypt)(struct crypto_tfm *tfm,
  161. struct scatterlist *dst,
  162. struct scatterlist *src,
  163. unsigned int nbytes);
  164. int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
  165. struct scatterlist *dst,
  166. struct scatterlist *src,
  167. unsigned int nbytes, u8 *iv);
  168. void (*cit_xor_block)(u8 *dst, const u8 *src);
  169. };
  170. struct digest_tfm {
  171. void (*dit_init)(struct crypto_tfm *tfm);
  172. void (*dit_update)(struct crypto_tfm *tfm,
  173. struct scatterlist *sg, unsigned int nsg);
  174. void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
  175. void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
  176. unsigned int nsg, u8 *out);
  177. int (*dit_setkey)(struct crypto_tfm *tfm,
  178. const u8 *key, unsigned int keylen);
  179. #ifdef CONFIG_CRYPTO_HMAC
  180. void *dit_hmac_block;
  181. #endif
  182. };
  183. struct compress_tfm {
  184. int (*cot_compress)(struct crypto_tfm *tfm,
  185. const u8 *src, unsigned int slen,
  186. u8 *dst, unsigned int *dlen);
  187. int (*cot_decompress)(struct crypto_tfm *tfm,
  188. const u8 *src, unsigned int slen,
  189. u8 *dst, unsigned int *dlen);
  190. };
  191. #define crt_cipher crt_u.cipher
  192. #define crt_digest crt_u.digest
  193. #define crt_compress crt_u.compress
  194. struct crypto_tfm {
  195. u32 crt_flags;
  196. union {
  197. struct cipher_tfm cipher;
  198. struct digest_tfm digest;
  199. struct compress_tfm compress;
  200. } crt_u;
  201. struct crypto_alg *__crt_alg;
  202. char __crt_ctx[] __attribute__ ((__aligned__));
  203. };
  204. /*
  205. * Transform user interface.
  206. */
  207. /*
  208. * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
  209. * If that fails and the kernel supports dynamically loadable modules, it
  210. * will then attempt to load a module of the same name or alias. A refcount
  211. * is grabbed on the algorithm which is then associated with the new transform.
  212. *
  213. * crypto_free_tfm() frees up the transform and any associated resources,
  214. * then drops the refcount on the associated algorithm.
  215. */
  216. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  217. void crypto_free_tfm(struct crypto_tfm *tfm);
  218. /*
  219. * Transform helpers which query the underlying algorithm.
  220. */
  221. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  222. {
  223. return tfm->__crt_alg->cra_name;
  224. }
  225. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  226. {
  227. return module_name(tfm->__crt_alg->cra_module);
  228. }
  229. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  230. {
  231. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  232. }
  233. static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
  234. {
  235. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  236. return tfm->__crt_alg->cra_cipher.cia_min_keysize;
  237. }
  238. static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
  239. {
  240. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  241. return tfm->__crt_alg->cra_cipher.cia_max_keysize;
  242. }
  243. static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
  244. {
  245. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  246. return tfm->crt_cipher.cit_ivsize;
  247. }
  248. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  249. {
  250. return tfm->__crt_alg->cra_blocksize;
  251. }
  252. static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
  253. {
  254. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  255. return tfm->__crt_alg->cra_digest.dia_digestsize;
  256. }
  257. static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
  258. {
  259. return tfm->__crt_alg->cra_alignmask;
  260. }
  261. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  262. {
  263. return tfm->__crt_ctx;
  264. }
  265. static inline unsigned int crypto_tfm_ctx_alignment(void)
  266. {
  267. struct crypto_tfm *tfm;
  268. return __alignof__(tfm->__crt_ctx);
  269. }
  270. /*
  271. * API wrappers.
  272. */
  273. static inline void crypto_digest_init(struct crypto_tfm *tfm)
  274. {
  275. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  276. tfm->crt_digest.dit_init(tfm);
  277. }
  278. static inline void crypto_digest_update(struct crypto_tfm *tfm,
  279. struct scatterlist *sg,
  280. unsigned int nsg)
  281. {
  282. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  283. tfm->crt_digest.dit_update(tfm, sg, nsg);
  284. }
  285. static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
  286. {
  287. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  288. tfm->crt_digest.dit_final(tfm, out);
  289. }
  290. static inline void crypto_digest_digest(struct crypto_tfm *tfm,
  291. struct scatterlist *sg,
  292. unsigned int nsg, u8 *out)
  293. {
  294. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  295. tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
  296. }
  297. static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
  298. const u8 *key, unsigned int keylen)
  299. {
  300. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  301. if (tfm->crt_digest.dit_setkey == NULL)
  302. return -ENOSYS;
  303. return tfm->crt_digest.dit_setkey(tfm, key, keylen);
  304. }
  305. static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
  306. const u8 *key, unsigned int keylen)
  307. {
  308. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  309. return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
  310. }
  311. static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
  312. struct scatterlist *dst,
  313. struct scatterlist *src,
  314. unsigned int nbytes)
  315. {
  316. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  317. return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
  318. }
  319. static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
  320. struct scatterlist *dst,
  321. struct scatterlist *src,
  322. unsigned int nbytes, u8 *iv)
  323. {
  324. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  325. BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
  326. return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
  327. }
  328. static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
  329. struct scatterlist *dst,
  330. struct scatterlist *src,
  331. unsigned int nbytes)
  332. {
  333. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  334. return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
  335. }
  336. static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
  337. struct scatterlist *dst,
  338. struct scatterlist *src,
  339. unsigned int nbytes, u8 *iv)
  340. {
  341. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  342. BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
  343. return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
  344. }
  345. static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
  346. const u8 *src, unsigned int len)
  347. {
  348. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  349. memcpy(tfm->crt_cipher.cit_iv, src, len);
  350. }
  351. static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
  352. u8 *dst, unsigned int len)
  353. {
  354. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  355. memcpy(dst, tfm->crt_cipher.cit_iv, len);
  356. }
  357. static inline int crypto_comp_compress(struct crypto_tfm *tfm,
  358. const u8 *src, unsigned int slen,
  359. u8 *dst, unsigned int *dlen)
  360. {
  361. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
  362. return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
  363. }
  364. static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
  365. const u8 *src, unsigned int slen,
  366. u8 *dst, unsigned int *dlen)
  367. {
  368. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
  369. return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
  370. }
  371. /*
  372. * HMAC support.
  373. */
  374. #ifdef CONFIG_CRYPTO_HMAC
  375. void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
  376. void crypto_hmac_update(struct crypto_tfm *tfm,
  377. struct scatterlist *sg, unsigned int nsg);
  378. void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
  379. unsigned int *keylen, u8 *out);
  380. void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
  381. struct scatterlist *sg, unsigned int nsg, u8 *out);
  382. #endif /* CONFIG_CRYPTO_HMAC */
  383. #endif /* _LINUX_CRYPTO_H */