crypto.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. *
  7. * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
  8. * and Nettle, by Niels Möller.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #ifndef _LINUX_CRYPTO_H
  17. #define _LINUX_CRYPTO_H
  18. #include <linux/config.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_RES_WEAK_KEY 0x00100000
  44. #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
  45. #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
  46. #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
  47. #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
  48. /*
  49. * Miscellaneous stuff.
  50. */
  51. #define CRYPTO_UNSPEC 0
  52. #define CRYPTO_MAX_ALG_NAME 64
  53. #define CRYPTO_DIR_ENCRYPT 1
  54. #define CRYPTO_DIR_DECRYPT 0
  55. struct scatterlist;
  56. /*
  57. * Algorithms: modular crypto algorithm implementations, managed
  58. * via crypto_register_alg() and crypto_unregister_alg().
  59. */
  60. struct cipher_alg {
  61. unsigned int cia_min_keysize;
  62. unsigned int cia_max_keysize;
  63. int (*cia_setkey)(void *ctx, const u8 *key,
  64. unsigned int keylen, u32 *flags);
  65. void (*cia_encrypt)(void *ctx, u8 *dst, const u8 *src);
  66. void (*cia_decrypt)(void *ctx, u8 *dst, const u8 *src);
  67. };
  68. struct digest_alg {
  69. unsigned int dia_digestsize;
  70. void (*dia_init)(void *ctx);
  71. void (*dia_update)(void *ctx, const u8 *data, unsigned int len);
  72. void (*dia_final)(void *ctx, u8 *out);
  73. int (*dia_setkey)(void *ctx, const u8 *key,
  74. unsigned int keylen, u32 *flags);
  75. };
  76. struct compress_alg {
  77. int (*coa_init)(void *ctx);
  78. void (*coa_exit)(void *ctx);
  79. int (*coa_compress)(void *ctx, const u8 *src, unsigned int slen,
  80. u8 *dst, unsigned int *dlen);
  81. int (*coa_decompress)(void *ctx, const u8 *src, unsigned int slen,
  82. u8 *dst, unsigned int *dlen);
  83. };
  84. #define cra_cipher cra_u.cipher
  85. #define cra_digest cra_u.digest
  86. #define cra_compress cra_u.compress
  87. struct crypto_alg {
  88. struct list_head cra_list;
  89. u32 cra_flags;
  90. unsigned int cra_blocksize;
  91. unsigned int cra_ctxsize;
  92. const char cra_name[CRYPTO_MAX_ALG_NAME];
  93. union {
  94. struct cipher_alg cipher;
  95. struct digest_alg digest;
  96. struct compress_alg compress;
  97. } cra_u;
  98. struct module *cra_module;
  99. };
  100. /*
  101. * Algorithm registration interface.
  102. */
  103. int crypto_register_alg(struct crypto_alg *alg);
  104. int crypto_unregister_alg(struct crypto_alg *alg);
  105. /*
  106. * Algorithm query interface.
  107. */
  108. #ifdef CONFIG_CRYPTO
  109. int crypto_alg_available(const char *name, u32 flags);
  110. #else
  111. static inline int crypto_alg_available(const char *name, u32 flags)
  112. {
  113. return 0;
  114. }
  115. #endif
  116. /*
  117. * Transforms: user-instantiated objects which encapsulate algorithms
  118. * and core processing logic. Managed via crypto_alloc_tfm() and
  119. * crypto_free_tfm(), as well as the various helpers below.
  120. */
  121. struct crypto_tfm;
  122. struct cipher_tfm {
  123. void *cit_iv;
  124. unsigned int cit_ivsize;
  125. u32 cit_mode;
  126. int (*cit_setkey)(struct crypto_tfm *tfm,
  127. const u8 *key, unsigned int keylen);
  128. int (*cit_encrypt)(struct crypto_tfm *tfm,
  129. struct scatterlist *dst,
  130. struct scatterlist *src,
  131. unsigned int nbytes);
  132. int (*cit_encrypt_iv)(struct crypto_tfm *tfm,
  133. struct scatterlist *dst,
  134. struct scatterlist *src,
  135. unsigned int nbytes, u8 *iv);
  136. int (*cit_decrypt)(struct crypto_tfm *tfm,
  137. struct scatterlist *dst,
  138. struct scatterlist *src,
  139. unsigned int nbytes);
  140. int (*cit_decrypt_iv)(struct crypto_tfm *tfm,
  141. struct scatterlist *dst,
  142. struct scatterlist *src,
  143. unsigned int nbytes, u8 *iv);
  144. void (*cit_xor_block)(u8 *dst, const u8 *src);
  145. };
  146. struct digest_tfm {
  147. void (*dit_init)(struct crypto_tfm *tfm);
  148. void (*dit_update)(struct crypto_tfm *tfm,
  149. struct scatterlist *sg, unsigned int nsg);
  150. void (*dit_final)(struct crypto_tfm *tfm, u8 *out);
  151. void (*dit_digest)(struct crypto_tfm *tfm, struct scatterlist *sg,
  152. unsigned int nsg, u8 *out);
  153. int (*dit_setkey)(struct crypto_tfm *tfm,
  154. const u8 *key, unsigned int keylen);
  155. #ifdef CONFIG_CRYPTO_HMAC
  156. void *dit_hmac_block;
  157. #endif
  158. };
  159. struct compress_tfm {
  160. int (*cot_compress)(struct crypto_tfm *tfm,
  161. const u8 *src, unsigned int slen,
  162. u8 *dst, unsigned int *dlen);
  163. int (*cot_decompress)(struct crypto_tfm *tfm,
  164. const u8 *src, unsigned int slen,
  165. u8 *dst, unsigned int *dlen);
  166. };
  167. #define crt_cipher crt_u.cipher
  168. #define crt_digest crt_u.digest
  169. #define crt_compress crt_u.compress
  170. struct crypto_tfm {
  171. u32 crt_flags;
  172. union {
  173. struct cipher_tfm cipher;
  174. struct digest_tfm digest;
  175. struct compress_tfm compress;
  176. } crt_u;
  177. struct crypto_alg *__crt_alg;
  178. };
  179. /*
  180. * Transform user interface.
  181. */
  182. /*
  183. * crypto_alloc_tfm() will first attempt to locate an already loaded algorithm.
  184. * If that fails and the kernel supports dynamically loadable modules, it
  185. * will then attempt to load a module of the same name or alias. A refcount
  186. * is grabbed on the algorithm which is then associated with the new transform.
  187. *
  188. * crypto_free_tfm() frees up the transform and any associated resources,
  189. * then drops the refcount on the associated algorithm.
  190. */
  191. struct crypto_tfm *crypto_alloc_tfm(const char *alg_name, u32 tfm_flags);
  192. void crypto_free_tfm(struct crypto_tfm *tfm);
  193. /*
  194. * Transform helpers which query the underlying algorithm.
  195. */
  196. static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
  197. {
  198. return tfm->__crt_alg->cra_name;
  199. }
  200. static inline const char *crypto_tfm_alg_modname(struct crypto_tfm *tfm)
  201. {
  202. return module_name(tfm->__crt_alg->cra_module);
  203. }
  204. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  205. {
  206. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  207. }
  208. static inline unsigned int crypto_tfm_alg_min_keysize(struct crypto_tfm *tfm)
  209. {
  210. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  211. return tfm->__crt_alg->cra_cipher.cia_min_keysize;
  212. }
  213. static inline unsigned int crypto_tfm_alg_max_keysize(struct crypto_tfm *tfm)
  214. {
  215. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  216. return tfm->__crt_alg->cra_cipher.cia_max_keysize;
  217. }
  218. static inline unsigned int crypto_tfm_alg_ivsize(struct crypto_tfm *tfm)
  219. {
  220. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  221. return tfm->crt_cipher.cit_ivsize;
  222. }
  223. static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
  224. {
  225. return tfm->__crt_alg->cra_blocksize;
  226. }
  227. static inline unsigned int crypto_tfm_alg_digestsize(struct crypto_tfm *tfm)
  228. {
  229. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  230. return tfm->__crt_alg->cra_digest.dia_digestsize;
  231. }
  232. /*
  233. * API wrappers.
  234. */
  235. static inline void crypto_digest_init(struct crypto_tfm *tfm)
  236. {
  237. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  238. tfm->crt_digest.dit_init(tfm);
  239. }
  240. static inline void crypto_digest_update(struct crypto_tfm *tfm,
  241. struct scatterlist *sg,
  242. unsigned int nsg)
  243. {
  244. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  245. tfm->crt_digest.dit_update(tfm, sg, nsg);
  246. }
  247. static inline void crypto_digest_final(struct crypto_tfm *tfm, u8 *out)
  248. {
  249. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  250. tfm->crt_digest.dit_final(tfm, out);
  251. }
  252. static inline void crypto_digest_digest(struct crypto_tfm *tfm,
  253. struct scatterlist *sg,
  254. unsigned int nsg, u8 *out)
  255. {
  256. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  257. tfm->crt_digest.dit_digest(tfm, sg, nsg, out);
  258. }
  259. static inline int crypto_digest_setkey(struct crypto_tfm *tfm,
  260. const u8 *key, unsigned int keylen)
  261. {
  262. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_DIGEST);
  263. if (tfm->crt_digest.dit_setkey == NULL)
  264. return -ENOSYS;
  265. return tfm->crt_digest.dit_setkey(tfm, key, keylen);
  266. }
  267. static inline int crypto_cipher_setkey(struct crypto_tfm *tfm,
  268. const u8 *key, unsigned int keylen)
  269. {
  270. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  271. return tfm->crt_cipher.cit_setkey(tfm, key, keylen);
  272. }
  273. static inline int crypto_cipher_encrypt(struct crypto_tfm *tfm,
  274. struct scatterlist *dst,
  275. struct scatterlist *src,
  276. unsigned int nbytes)
  277. {
  278. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  279. return tfm->crt_cipher.cit_encrypt(tfm, dst, src, nbytes);
  280. }
  281. static inline int crypto_cipher_encrypt_iv(struct crypto_tfm *tfm,
  282. struct scatterlist *dst,
  283. struct scatterlist *src,
  284. unsigned int nbytes, u8 *iv)
  285. {
  286. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  287. BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
  288. return tfm->crt_cipher.cit_encrypt_iv(tfm, dst, src, nbytes, iv);
  289. }
  290. static inline int crypto_cipher_decrypt(struct crypto_tfm *tfm,
  291. struct scatterlist *dst,
  292. struct scatterlist *src,
  293. unsigned int nbytes)
  294. {
  295. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  296. return tfm->crt_cipher.cit_decrypt(tfm, dst, src, nbytes);
  297. }
  298. static inline int crypto_cipher_decrypt_iv(struct crypto_tfm *tfm,
  299. struct scatterlist *dst,
  300. struct scatterlist *src,
  301. unsigned int nbytes, u8 *iv)
  302. {
  303. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  304. BUG_ON(tfm->crt_cipher.cit_mode == CRYPTO_TFM_MODE_ECB);
  305. return tfm->crt_cipher.cit_decrypt_iv(tfm, dst, src, nbytes, iv);
  306. }
  307. static inline void crypto_cipher_set_iv(struct crypto_tfm *tfm,
  308. const u8 *src, unsigned int len)
  309. {
  310. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  311. memcpy(tfm->crt_cipher.cit_iv, src, len);
  312. }
  313. static inline void crypto_cipher_get_iv(struct crypto_tfm *tfm,
  314. u8 *dst, unsigned int len)
  315. {
  316. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER);
  317. memcpy(dst, tfm->crt_cipher.cit_iv, len);
  318. }
  319. static inline int crypto_comp_compress(struct crypto_tfm *tfm,
  320. const u8 *src, unsigned int slen,
  321. u8 *dst, unsigned int *dlen)
  322. {
  323. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
  324. return tfm->crt_compress.cot_compress(tfm, src, slen, dst, dlen);
  325. }
  326. static inline int crypto_comp_decompress(struct crypto_tfm *tfm,
  327. const u8 *src, unsigned int slen,
  328. u8 *dst, unsigned int *dlen)
  329. {
  330. BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_COMPRESS);
  331. return tfm->crt_compress.cot_decompress(tfm, src, slen, dst, dlen);
  332. }
  333. /*
  334. * HMAC support.
  335. */
  336. #ifdef CONFIG_CRYPTO_HMAC
  337. void crypto_hmac_init(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen);
  338. void crypto_hmac_update(struct crypto_tfm *tfm,
  339. struct scatterlist *sg, unsigned int nsg);
  340. void crypto_hmac_final(struct crypto_tfm *tfm, u8 *key,
  341. unsigned int *keylen, u8 *out);
  342. void crypto_hmac(struct crypto_tfm *tfm, u8 *key, unsigned int *keylen,
  343. struct scatterlist *sg, unsigned int nsg, u8 *out);
  344. #endif /* CONFIG_CRYPTO_HMAC */
  345. #endif /* _LINUX_CRYPTO_H */