cipher.c 8.5 KB

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