cipher.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. unsigned int alignmask = crypto_tfm_alg_alignmask(desc->tfm);
  42. u8 buffer[bsize * 2 + alignmask];
  43. u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  44. u8 *dst = src + bsize;
  45. unsigned int n;
  46. n = scatterwalk_copychunks(src, in, bsize, 0);
  47. scatterwalk_advance(in, n);
  48. desc->prfn(desc, dst, src, bsize);
  49. n = scatterwalk_copychunks(dst, out, bsize, 1);
  50. scatterwalk_advance(out, n);
  51. return bsize;
  52. }
  53. static inline unsigned int crypt_fast(const struct cipher_desc *desc,
  54. struct scatter_walk *in,
  55. struct scatter_walk *out,
  56. unsigned int nbytes, u8 *tmp)
  57. {
  58. u8 *src, *dst;
  59. src = in->data;
  60. dst = scatterwalk_samebuf(in, out) ? src : out->data;
  61. if (tmp) {
  62. memcpy(tmp, in->data, nbytes);
  63. src = tmp;
  64. dst = tmp;
  65. }
  66. nbytes = desc->prfn(desc, dst, src, nbytes);
  67. if (tmp)
  68. memcpy(out->data, tmp, nbytes);
  69. scatterwalk_advance(in, nbytes);
  70. scatterwalk_advance(out, nbytes);
  71. return nbytes;
  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 page.
  77. */
  78. static int crypt(const struct cipher_desc *desc,
  79. struct scatterlist *dst,
  80. struct scatterlist *src,
  81. unsigned int nbytes)
  82. {
  83. struct scatter_walk walk_in, walk_out;
  84. struct crypto_tfm *tfm = desc->tfm;
  85. const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
  86. unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
  87. unsigned long buffer = 0;
  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. unsigned int n = nbytes;
  98. u8 *tmp = NULL;
  99. if (!scatterwalk_aligned(&walk_in, alignmask) ||
  100. !scatterwalk_aligned(&walk_out, alignmask)) {
  101. if (!buffer) {
  102. buffer = __get_free_page(GFP_ATOMIC);
  103. if (!buffer)
  104. n = 0;
  105. }
  106. tmp = (u8 *)buffer;
  107. }
  108. scatterwalk_map(&walk_in, 0);
  109. scatterwalk_map(&walk_out, 1);
  110. n = scatterwalk_clamp(&walk_in, n);
  111. n = scatterwalk_clamp(&walk_out, n);
  112. if (likely(n >= bsize))
  113. n = crypt_fast(desc, &walk_in, &walk_out, n, tmp);
  114. else
  115. n = crypt_slow(desc, &walk_in, &walk_out, bsize);
  116. nbytes -= n;
  117. scatterwalk_done(&walk_in, 0, nbytes);
  118. scatterwalk_done(&walk_out, 1, nbytes);
  119. if (!nbytes)
  120. break;
  121. crypto_yield(tfm);
  122. }
  123. if (buffer)
  124. free_page(buffer);
  125. return 0;
  126. }
  127. static unsigned int cbc_process_encrypt(const struct cipher_desc *desc,
  128. u8 *dst, const u8 *src,
  129. unsigned int nbytes)
  130. {
  131. struct crypto_tfm *tfm = desc->tfm;
  132. void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
  133. int bsize = crypto_tfm_alg_blocksize(tfm);
  134. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  135. u8 *iv = desc->info;
  136. unsigned int done = 0;
  137. do {
  138. xor(iv, src);
  139. fn(crypto_tfm_ctx(tfm), dst, iv);
  140. memcpy(iv, dst, bsize);
  141. src += bsize;
  142. dst += bsize;
  143. } while ((done += bsize) < nbytes);
  144. return done;
  145. }
  146. static unsigned int cbc_process_decrypt(const struct cipher_desc *desc,
  147. u8 *dst, const u8 *src,
  148. unsigned int nbytes)
  149. {
  150. struct crypto_tfm *tfm = desc->tfm;
  151. void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
  152. int bsize = crypto_tfm_alg_blocksize(tfm);
  153. u8 stack[src == dst ? bsize : 0];
  154. u8 *buf = stack;
  155. u8 **dst_p = src == dst ? &buf : &dst;
  156. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  157. u8 *iv = desc->info;
  158. unsigned int done = 0;
  159. do {
  160. u8 *tmp_dst = *dst_p;
  161. fn(crypto_tfm_ctx(tfm), tmp_dst, src);
  162. xor(tmp_dst, iv);
  163. memcpy(iv, src, bsize);
  164. if (tmp_dst != dst)
  165. memcpy(dst, tmp_dst, bsize);
  166. src += bsize;
  167. dst += bsize;
  168. } while ((done += bsize) < nbytes);
  169. return done;
  170. }
  171. static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst,
  172. const u8 *src, unsigned int nbytes)
  173. {
  174. struct crypto_tfm *tfm = desc->tfm;
  175. int bsize = crypto_tfm_alg_blocksize(tfm);
  176. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  177. unsigned int done = 0;
  178. do {
  179. fn(crypto_tfm_ctx(tfm), dst, src);
  180. src += bsize;
  181. dst += bsize;
  182. } while ((done += bsize) < nbytes);
  183. return done;
  184. }
  185. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  186. {
  187. struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
  188. if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
  189. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  190. return -EINVAL;
  191. } else
  192. return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
  193. &tfm->crt_flags);
  194. }
  195. static int ecb_encrypt(struct crypto_tfm *tfm,
  196. struct scatterlist *dst,
  197. struct scatterlist *src, 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_ecb ?: ecb_process;
  204. return crypt(&desc, dst, src, nbytes);
  205. }
  206. static int ecb_decrypt(struct crypto_tfm *tfm,
  207. struct scatterlist *dst,
  208. struct scatterlist *src,
  209. unsigned int nbytes)
  210. {
  211. struct cipher_desc desc;
  212. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  213. desc.tfm = tfm;
  214. desc.crfn = cipher->cia_decrypt;
  215. desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process;
  216. return crypt(&desc, dst, src, nbytes);
  217. }
  218. static int cbc_encrypt(struct crypto_tfm *tfm,
  219. struct scatterlist *dst,
  220. struct scatterlist *src,
  221. unsigned int nbytes)
  222. {
  223. struct cipher_desc desc;
  224. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  225. desc.tfm = tfm;
  226. desc.crfn = cipher->cia_encrypt;
  227. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  228. desc.info = tfm->crt_cipher.cit_iv;
  229. return crypt(&desc, dst, src, nbytes);
  230. }
  231. static int cbc_encrypt_iv(struct crypto_tfm *tfm,
  232. struct scatterlist *dst,
  233. struct scatterlist *src,
  234. unsigned int nbytes, u8 *iv)
  235. {
  236. struct cipher_desc desc;
  237. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  238. desc.tfm = tfm;
  239. desc.crfn = cipher->cia_encrypt;
  240. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  241. desc.info = iv;
  242. return crypt(&desc, dst, src, nbytes);
  243. }
  244. static int cbc_decrypt(struct crypto_tfm *tfm,
  245. struct scatterlist *dst,
  246. struct scatterlist *src,
  247. unsigned int nbytes)
  248. {
  249. struct cipher_desc desc;
  250. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  251. desc.tfm = tfm;
  252. desc.crfn = cipher->cia_decrypt;
  253. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  254. desc.info = tfm->crt_cipher.cit_iv;
  255. return crypt(&desc, dst, src, nbytes);
  256. }
  257. static int cbc_decrypt_iv(struct crypto_tfm *tfm,
  258. struct scatterlist *dst,
  259. struct scatterlist *src,
  260. unsigned int nbytes, u8 *iv)
  261. {
  262. struct cipher_desc desc;
  263. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  264. desc.tfm = tfm;
  265. desc.crfn = cipher->cia_decrypt;
  266. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  267. desc.info = iv;
  268. return crypt(&desc, dst, src, nbytes);
  269. }
  270. static int nocrypt(struct crypto_tfm *tfm,
  271. struct scatterlist *dst,
  272. struct scatterlist *src,
  273. unsigned int nbytes)
  274. {
  275. return -ENOSYS;
  276. }
  277. static int nocrypt_iv(struct crypto_tfm *tfm,
  278. struct scatterlist *dst,
  279. struct scatterlist *src,
  280. unsigned int nbytes, u8 *iv)
  281. {
  282. return -ENOSYS;
  283. }
  284. int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
  285. {
  286. u32 mode = flags & CRYPTO_TFM_MODE_MASK;
  287. tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
  288. if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
  289. tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
  290. return 0;
  291. }
  292. int crypto_init_cipher_ops(struct crypto_tfm *tfm)
  293. {
  294. int ret = 0;
  295. struct cipher_tfm *ops = &tfm->crt_cipher;
  296. ops->cit_setkey = setkey;
  297. switch (tfm->crt_cipher.cit_mode) {
  298. case CRYPTO_TFM_MODE_ECB:
  299. ops->cit_encrypt = ecb_encrypt;
  300. ops->cit_decrypt = ecb_decrypt;
  301. break;
  302. case CRYPTO_TFM_MODE_CBC:
  303. ops->cit_encrypt = cbc_encrypt;
  304. ops->cit_decrypt = cbc_decrypt;
  305. ops->cit_encrypt_iv = cbc_encrypt_iv;
  306. ops->cit_decrypt_iv = cbc_decrypt_iv;
  307. break;
  308. case CRYPTO_TFM_MODE_CFB:
  309. ops->cit_encrypt = nocrypt;
  310. ops->cit_decrypt = nocrypt;
  311. ops->cit_encrypt_iv = nocrypt_iv;
  312. ops->cit_decrypt_iv = nocrypt_iv;
  313. break;
  314. case CRYPTO_TFM_MODE_CTR:
  315. ops->cit_encrypt = nocrypt;
  316. ops->cit_decrypt = nocrypt;
  317. ops->cit_encrypt_iv = nocrypt_iv;
  318. ops->cit_decrypt_iv = nocrypt_iv;
  319. break;
  320. default:
  321. BUG();
  322. }
  323. if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
  324. unsigned int align;
  325. unsigned long addr;
  326. switch (crypto_tfm_alg_blocksize(tfm)) {
  327. case 8:
  328. ops->cit_xor_block = xor_64;
  329. break;
  330. case 16:
  331. ops->cit_xor_block = xor_128;
  332. break;
  333. default:
  334. printk(KERN_WARNING "%s: block size %u not supported\n",
  335. crypto_tfm_alg_name(tfm),
  336. crypto_tfm_alg_blocksize(tfm));
  337. ret = -EINVAL;
  338. goto out;
  339. }
  340. ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
  341. align = crypto_tfm_alg_alignmask(tfm) + 1;
  342. addr = (unsigned long)crypto_tfm_ctx(tfm);
  343. addr = ALIGN(addr, align);
  344. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  345. ops->cit_iv = (void *)addr;
  346. }
  347. out:
  348. return ret;
  349. }
  350. void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
  351. {
  352. }