cipher.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Cipher operations.
  5. *
  6. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  7. * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <linux/compiler.h>
  16. #include <linux/kernel.h>
  17. #include <linux/crypto.h>
  18. #include <linux/errno.h>
  19. #include <linux/mm.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <asm/scatterlist.h>
  23. #include "internal.h"
  24. #include "scatterwalk.h"
  25. static inline void xor_64(u8 *a, const u8 *b)
  26. {
  27. ((u32 *)a)[0] ^= ((u32 *)b)[0];
  28. ((u32 *)a)[1] ^= ((u32 *)b)[1];
  29. }
  30. static inline void xor_128(u8 *a, const u8 *b)
  31. {
  32. ((u32 *)a)[0] ^= ((u32 *)b)[0];
  33. ((u32 *)a)[1] ^= ((u32 *)b)[1];
  34. ((u32 *)a)[2] ^= ((u32 *)b)[2];
  35. ((u32 *)a)[3] ^= ((u32 *)b)[3];
  36. }
  37. static unsigned int crypt_slow(const struct cipher_desc *desc,
  38. struct scatter_walk *in,
  39. struct scatter_walk *out, unsigned int bsize)
  40. {
  41. u8 src[bsize];
  42. u8 dst[bsize];
  43. unsigned int n;
  44. n = scatterwalk_copychunks(src, in, bsize, 0);
  45. scatterwalk_advance(in, n);
  46. desc->prfn(desc, dst, src, bsize);
  47. n = scatterwalk_copychunks(dst, out, bsize, 1);
  48. scatterwalk_advance(out, n);
  49. return bsize;
  50. }
  51. static inline unsigned int crypt_fast(const struct cipher_desc *desc,
  52. struct scatter_walk *in,
  53. struct scatter_walk *out,
  54. unsigned int nbytes)
  55. {
  56. u8 *src, *dst;
  57. src = in->data;
  58. dst = scatterwalk_samebuf(in, out) ? src : out->data;
  59. nbytes = desc->prfn(desc, dst, src, nbytes);
  60. scatterwalk_advance(in, nbytes);
  61. scatterwalk_advance(out, nbytes);
  62. return nbytes;
  63. }
  64. /*
  65. * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  66. * multiple page boundaries by using temporary blocks. In user context,
  67. * the kernel is given a chance to schedule us once per page.
  68. */
  69. static int crypt(const struct cipher_desc *desc,
  70. struct scatterlist *dst,
  71. struct scatterlist *src,
  72. unsigned int nbytes)
  73. {
  74. struct scatter_walk walk_in, walk_out;
  75. struct crypto_tfm *tfm = desc->tfm;
  76. const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
  77. if (!nbytes)
  78. return 0;
  79. if (nbytes % bsize) {
  80. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  81. return -EINVAL;
  82. }
  83. scatterwalk_start(&walk_in, src);
  84. scatterwalk_start(&walk_out, dst);
  85. for(;;) {
  86. unsigned int n;
  87. scatterwalk_map(&walk_in, 0);
  88. scatterwalk_map(&walk_out, 1);
  89. n = scatterwalk_clamp(&walk_in, nbytes);
  90. n = scatterwalk_clamp(&walk_out, n);
  91. if (likely(n >= bsize))
  92. n = crypt_fast(desc, &walk_in, &walk_out, n);
  93. else
  94. n = crypt_slow(desc, &walk_in, &walk_out, bsize);
  95. nbytes -= n;
  96. scatterwalk_done(&walk_in, 0, nbytes);
  97. scatterwalk_done(&walk_out, 1, nbytes);
  98. if (!nbytes)
  99. return 0;
  100. crypto_yield(tfm);
  101. }
  102. }
  103. static unsigned int cbc_process_encrypt(const struct cipher_desc *desc,
  104. u8 *dst, const u8 *src,
  105. unsigned int nbytes)
  106. {
  107. struct crypto_tfm *tfm = desc->tfm;
  108. void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
  109. int bsize = crypto_tfm_alg_blocksize(tfm);
  110. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  111. u8 *iv = desc->info;
  112. unsigned int done = 0;
  113. do {
  114. xor(iv, src);
  115. fn(crypto_tfm_ctx(tfm), dst, iv);
  116. memcpy(iv, dst, bsize);
  117. src += bsize;
  118. dst += bsize;
  119. } while ((done += bsize) < nbytes);
  120. return done;
  121. }
  122. static unsigned int cbc_process_decrypt(const struct cipher_desc *desc,
  123. u8 *dst, const u8 *src,
  124. unsigned int nbytes)
  125. {
  126. struct crypto_tfm *tfm = desc->tfm;
  127. void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
  128. int bsize = crypto_tfm_alg_blocksize(tfm);
  129. u8 stack[src == dst ? bsize : 0];
  130. u8 *buf = stack;
  131. u8 **dst_p = src == dst ? &buf : &dst;
  132. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  133. u8 *iv = desc->info;
  134. unsigned int done = 0;
  135. do {
  136. u8 *tmp_dst = *dst_p;
  137. fn(crypto_tfm_ctx(tfm), tmp_dst, src);
  138. xor(tmp_dst, iv);
  139. memcpy(iv, src, bsize);
  140. if (tmp_dst != dst)
  141. memcpy(dst, tmp_dst, bsize);
  142. src += bsize;
  143. dst += bsize;
  144. } while ((done += bsize) < nbytes);
  145. return done;
  146. }
  147. static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst,
  148. const u8 *src, unsigned int nbytes)
  149. {
  150. struct crypto_tfm *tfm = desc->tfm;
  151. int bsize = crypto_tfm_alg_blocksize(tfm);
  152. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  153. unsigned int done = 0;
  154. do {
  155. fn(crypto_tfm_ctx(tfm), dst, src);
  156. src += bsize;
  157. dst += bsize;
  158. } while ((done += bsize) < nbytes);
  159. return done;
  160. }
  161. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  162. {
  163. struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
  164. if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
  165. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  166. return -EINVAL;
  167. } else
  168. return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
  169. &tfm->crt_flags);
  170. }
  171. static int ecb_encrypt(struct crypto_tfm *tfm,
  172. struct scatterlist *dst,
  173. struct scatterlist *src, unsigned int nbytes)
  174. {
  175. struct cipher_desc desc;
  176. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  177. desc.tfm = tfm;
  178. desc.crfn = cipher->cia_encrypt;
  179. desc.prfn = cipher->cia_encrypt_ecb ?: ecb_process;
  180. return crypt(&desc, dst, src, nbytes);
  181. }
  182. static int ecb_decrypt(struct crypto_tfm *tfm,
  183. struct scatterlist *dst,
  184. struct scatterlist *src,
  185. unsigned int nbytes)
  186. {
  187. struct cipher_desc desc;
  188. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  189. desc.tfm = tfm;
  190. desc.crfn = cipher->cia_decrypt;
  191. desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process;
  192. return crypt(&desc, dst, src, nbytes);
  193. }
  194. static int cbc_encrypt(struct crypto_tfm *tfm,
  195. struct scatterlist *dst,
  196. struct scatterlist *src,
  197. unsigned int nbytes)
  198. {
  199. struct cipher_desc desc;
  200. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  201. desc.tfm = tfm;
  202. desc.crfn = cipher->cia_encrypt;
  203. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  204. desc.info = tfm->crt_cipher.cit_iv;
  205. return crypt(&desc, dst, src, nbytes);
  206. }
  207. static int cbc_encrypt_iv(struct crypto_tfm *tfm,
  208. struct scatterlist *dst,
  209. struct scatterlist *src,
  210. unsigned int nbytes, u8 *iv)
  211. {
  212. struct cipher_desc desc;
  213. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  214. desc.tfm = tfm;
  215. desc.crfn = cipher->cia_encrypt;
  216. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  217. desc.info = iv;
  218. return crypt(&desc, dst, src, nbytes);
  219. }
  220. static int cbc_decrypt(struct crypto_tfm *tfm,
  221. struct scatterlist *dst,
  222. struct scatterlist *src,
  223. unsigned int nbytes)
  224. {
  225. struct cipher_desc desc;
  226. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  227. desc.tfm = tfm;
  228. desc.crfn = cipher->cia_decrypt;
  229. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  230. desc.info = tfm->crt_cipher.cit_iv;
  231. return crypt(&desc, dst, src, nbytes);
  232. }
  233. static int cbc_decrypt_iv(struct crypto_tfm *tfm,
  234. struct scatterlist *dst,
  235. struct scatterlist *src,
  236. unsigned int nbytes, u8 *iv)
  237. {
  238. struct cipher_desc desc;
  239. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  240. desc.tfm = tfm;
  241. desc.crfn = cipher->cia_decrypt;
  242. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  243. desc.info = iv;
  244. return crypt(&desc, dst, src, nbytes);
  245. }
  246. static int nocrypt(struct crypto_tfm *tfm,
  247. struct scatterlist *dst,
  248. struct scatterlist *src,
  249. unsigned int nbytes)
  250. {
  251. return -ENOSYS;
  252. }
  253. static int nocrypt_iv(struct crypto_tfm *tfm,
  254. struct scatterlist *dst,
  255. struct scatterlist *src,
  256. unsigned int nbytes, u8 *iv)
  257. {
  258. return -ENOSYS;
  259. }
  260. int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
  261. {
  262. u32 mode = flags & CRYPTO_TFM_MODE_MASK;
  263. tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
  264. if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
  265. tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
  266. return 0;
  267. }
  268. int crypto_init_cipher_ops(struct crypto_tfm *tfm)
  269. {
  270. int ret = 0;
  271. struct cipher_tfm *ops = &tfm->crt_cipher;
  272. ops->cit_setkey = setkey;
  273. switch (tfm->crt_cipher.cit_mode) {
  274. case CRYPTO_TFM_MODE_ECB:
  275. ops->cit_encrypt = ecb_encrypt;
  276. ops->cit_decrypt = ecb_decrypt;
  277. break;
  278. case CRYPTO_TFM_MODE_CBC:
  279. ops->cit_encrypt = cbc_encrypt;
  280. ops->cit_decrypt = cbc_decrypt;
  281. ops->cit_encrypt_iv = cbc_encrypt_iv;
  282. ops->cit_decrypt_iv = cbc_decrypt_iv;
  283. break;
  284. case CRYPTO_TFM_MODE_CFB:
  285. ops->cit_encrypt = nocrypt;
  286. ops->cit_decrypt = nocrypt;
  287. ops->cit_encrypt_iv = nocrypt_iv;
  288. ops->cit_decrypt_iv = nocrypt_iv;
  289. break;
  290. case CRYPTO_TFM_MODE_CTR:
  291. ops->cit_encrypt = nocrypt;
  292. ops->cit_decrypt = nocrypt;
  293. ops->cit_encrypt_iv = nocrypt_iv;
  294. ops->cit_decrypt_iv = nocrypt_iv;
  295. break;
  296. default:
  297. BUG();
  298. }
  299. if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
  300. switch (crypto_tfm_alg_blocksize(tfm)) {
  301. case 8:
  302. ops->cit_xor_block = xor_64;
  303. break;
  304. case 16:
  305. ops->cit_xor_block = xor_128;
  306. break;
  307. default:
  308. printk(KERN_WARNING "%s: block size %u not supported\n",
  309. crypto_tfm_alg_name(tfm),
  310. crypto_tfm_alg_blocksize(tfm));
  311. ret = -EINVAL;
  312. goto out;
  313. }
  314. ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
  315. ops->cit_iv = kmalloc(ops->cit_ivsize, GFP_KERNEL);
  316. if (ops->cit_iv == NULL)
  317. ret = -ENOMEM;
  318. }
  319. out:
  320. return ret;
  321. }
  322. void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
  323. {
  324. kfree(tfm->crt_cipher.cit_iv);
  325. }