crypto.h 13 KB

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