cipher.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 long 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. scatterwalk_copychunks(src, in, bsize, 0);
  46. desc->prfn(desc, dst, src, bsize);
  47. scatterwalk_copychunks(dst, out, bsize, 1);
  48. return bsize;
  49. }
  50. static inline unsigned int crypt_fast(const struct cipher_desc *desc,
  51. struct scatter_walk *in,
  52. struct scatter_walk *out,
  53. unsigned int nbytes, u8 *tmp)
  54. {
  55. u8 *src, *dst;
  56. u8 *real_src, *real_dst;
  57. real_src = scatterwalk_map(in, 0);
  58. real_dst = scatterwalk_map(out, 1);
  59. src = real_src;
  60. dst = scatterwalk_samebuf(in, out) ? src : real_dst;
  61. if (tmp) {
  62. memcpy(tmp, src, nbytes);
  63. src = tmp;
  64. dst = tmp;
  65. }
  66. nbytes = desc->prfn(desc, dst, src, nbytes);
  67. if (tmp)
  68. memcpy(real_dst, tmp, nbytes);
  69. scatterwalk_unmap(real_src, 0);
  70. scatterwalk_unmap(real_dst, 1);
  71. scatterwalk_advance(in, nbytes);
  72. scatterwalk_advance(out, nbytes);
  73. return nbytes;
  74. }
  75. /*
  76. * Generic encrypt/decrypt wrapper for ciphers, handles operations across
  77. * multiple page boundaries by using temporary blocks. In user context,
  78. * the kernel is given a chance to schedule us once per page.
  79. */
  80. static int crypt(const struct cipher_desc *desc,
  81. struct scatterlist *dst,
  82. struct scatterlist *src,
  83. unsigned int nbytes)
  84. {
  85. struct scatter_walk walk_in, walk_out;
  86. struct crypto_tfm *tfm = desc->tfm;
  87. const unsigned int bsize = crypto_tfm_alg_blocksize(tfm);
  88. unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
  89. unsigned long buffer = 0;
  90. if (!nbytes)
  91. return 0;
  92. if (nbytes % bsize) {
  93. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  94. return -EINVAL;
  95. }
  96. scatterwalk_start(&walk_in, src);
  97. scatterwalk_start(&walk_out, dst);
  98. for(;;) {
  99. unsigned int n = nbytes;
  100. u8 *tmp = NULL;
  101. if (!scatterwalk_aligned(&walk_in, alignmask) ||
  102. !scatterwalk_aligned(&walk_out, alignmask)) {
  103. if (!buffer) {
  104. buffer = __get_free_page(GFP_ATOMIC);
  105. if (!buffer)
  106. n = 0;
  107. }
  108. tmp = (u8 *)buffer;
  109. }
  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->crt_flags);
  122. }
  123. if (buffer)
  124. free_page(buffer);
  125. return 0;
  126. }
  127. static int crypt_iv_unaligned(struct cipher_desc *desc,
  128. struct scatterlist *dst,
  129. struct scatterlist *src,
  130. unsigned int nbytes)
  131. {
  132. struct crypto_tfm *tfm = desc->tfm;
  133. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  134. u8 *iv = desc->info;
  135. if (unlikely(((unsigned long)iv & alignmask))) {
  136. unsigned int ivsize = tfm->crt_cipher.cit_ivsize;
  137. u8 buffer[ivsize + alignmask];
  138. u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  139. int err;
  140. desc->info = memcpy(tmp, iv, ivsize);
  141. err = crypt(desc, dst, src, nbytes);
  142. memcpy(iv, tmp, ivsize);
  143. return err;
  144. }
  145. return crypt(desc, dst, src, nbytes);
  146. }
  147. static unsigned int cbc_process_encrypt(const struct cipher_desc *desc,
  148. u8 *dst, const u8 *src,
  149. unsigned int nbytes)
  150. {
  151. struct crypto_tfm *tfm = desc->tfm;
  152. void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
  153. int bsize = crypto_tfm_alg_blocksize(tfm);
  154. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = desc->crfn;
  155. u8 *iv = desc->info;
  156. unsigned int done = 0;
  157. nbytes -= bsize;
  158. do {
  159. xor(iv, src);
  160. fn(tfm, dst, iv);
  161. memcpy(iv, dst, bsize);
  162. src += bsize;
  163. dst += bsize;
  164. } while ((done += bsize) <= nbytes);
  165. return done;
  166. }
  167. static unsigned int cbc_process_decrypt(const struct cipher_desc *desc,
  168. u8 *dst, const u8 *src,
  169. unsigned int nbytes)
  170. {
  171. struct crypto_tfm *tfm = desc->tfm;
  172. void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
  173. int bsize = crypto_tfm_alg_blocksize(tfm);
  174. unsigned long alignmask = crypto_tfm_alg_alignmask(desc->tfm);
  175. u8 stack[src == dst ? bsize + alignmask : 0];
  176. u8 *buf = (u8 *)ALIGN((unsigned long)stack, alignmask + 1);
  177. u8 **dst_p = src == dst ? &buf : &dst;
  178. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = desc->crfn;
  179. u8 *iv = desc->info;
  180. unsigned int done = 0;
  181. nbytes -= bsize;
  182. do {
  183. u8 *tmp_dst = *dst_p;
  184. fn(tfm, tmp_dst, src);
  185. xor(tmp_dst, iv);
  186. memcpy(iv, src, bsize);
  187. if (tmp_dst != dst)
  188. memcpy(dst, tmp_dst, bsize);
  189. src += bsize;
  190. dst += bsize;
  191. } while ((done += bsize) <= nbytes);
  192. return done;
  193. }
  194. static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst,
  195. const u8 *src, unsigned int nbytes)
  196. {
  197. struct crypto_tfm *tfm = desc->tfm;
  198. int bsize = crypto_tfm_alg_blocksize(tfm);
  199. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) = desc->crfn;
  200. unsigned int done = 0;
  201. nbytes -= bsize;
  202. do {
  203. fn(tfm, dst, src);
  204. src += bsize;
  205. dst += bsize;
  206. } while ((done += bsize) <= nbytes);
  207. return done;
  208. }
  209. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  210. {
  211. struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
  212. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  213. if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
  214. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  215. return -EINVAL;
  216. } else
  217. return cia->cia_setkey(tfm, key, keylen);
  218. }
  219. static int ecb_encrypt(struct crypto_tfm *tfm,
  220. struct scatterlist *dst,
  221. struct scatterlist *src, 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_ecb ?: ecb_process;
  228. return crypt(&desc, dst, src, nbytes);
  229. }
  230. static int ecb_decrypt(struct crypto_tfm *tfm,
  231. struct scatterlist *dst,
  232. struct scatterlist *src,
  233. unsigned int nbytes)
  234. {
  235. struct cipher_desc desc;
  236. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  237. desc.tfm = tfm;
  238. desc.crfn = cipher->cia_decrypt;
  239. desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process;
  240. return crypt(&desc, dst, src, nbytes);
  241. }
  242. static int cbc_encrypt(struct crypto_tfm *tfm,
  243. struct scatterlist *dst,
  244. struct scatterlist *src,
  245. unsigned int nbytes)
  246. {
  247. struct cipher_desc desc;
  248. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  249. desc.tfm = tfm;
  250. desc.crfn = cipher->cia_encrypt;
  251. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  252. desc.info = tfm->crt_cipher.cit_iv;
  253. return crypt(&desc, dst, src, nbytes);
  254. }
  255. static int cbc_encrypt_iv(struct crypto_tfm *tfm,
  256. struct scatterlist *dst,
  257. struct scatterlist *src,
  258. unsigned int nbytes, u8 *iv)
  259. {
  260. struct cipher_desc desc;
  261. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  262. desc.tfm = tfm;
  263. desc.crfn = cipher->cia_encrypt;
  264. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  265. desc.info = iv;
  266. return crypt_iv_unaligned(&desc, dst, src, nbytes);
  267. }
  268. static int cbc_decrypt(struct crypto_tfm *tfm,
  269. struct scatterlist *dst,
  270. struct scatterlist *src,
  271. unsigned int nbytes)
  272. {
  273. struct cipher_desc desc;
  274. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  275. desc.tfm = tfm;
  276. desc.crfn = cipher->cia_decrypt;
  277. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  278. desc.info = tfm->crt_cipher.cit_iv;
  279. return crypt(&desc, dst, src, nbytes);
  280. }
  281. static int cbc_decrypt_iv(struct crypto_tfm *tfm,
  282. struct scatterlist *dst,
  283. struct scatterlist *src,
  284. unsigned int nbytes, u8 *iv)
  285. {
  286. struct cipher_desc desc;
  287. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  288. desc.tfm = tfm;
  289. desc.crfn = cipher->cia_decrypt;
  290. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  291. desc.info = iv;
  292. return crypt_iv_unaligned(&desc, dst, src, nbytes);
  293. }
  294. static int nocrypt(struct crypto_tfm *tfm,
  295. struct scatterlist *dst,
  296. struct scatterlist *src,
  297. unsigned int nbytes)
  298. {
  299. return -ENOSYS;
  300. }
  301. static int nocrypt_iv(struct crypto_tfm *tfm,
  302. struct scatterlist *dst,
  303. struct scatterlist *src,
  304. unsigned int nbytes, u8 *iv)
  305. {
  306. return -ENOSYS;
  307. }
  308. int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
  309. {
  310. u32 mode = flags & CRYPTO_TFM_MODE_MASK;
  311. tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
  312. return 0;
  313. }
  314. static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
  315. const u8 *),
  316. struct crypto_tfm *tfm,
  317. u8 *dst, const u8 *src)
  318. {
  319. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  320. unsigned int size = crypto_tfm_alg_blocksize(tfm);
  321. u8 buffer[size + alignmask];
  322. u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  323. memcpy(tmp, src, size);
  324. fn(tfm, tmp, tmp);
  325. memcpy(dst, tmp, size);
  326. }
  327. static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
  328. u8 *dst, const u8 *src)
  329. {
  330. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  331. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  332. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  333. cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
  334. return;
  335. }
  336. cipher->cia_encrypt(tfm, dst, src);
  337. }
  338. static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
  339. u8 *dst, const u8 *src)
  340. {
  341. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  342. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  343. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  344. cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
  345. return;
  346. }
  347. cipher->cia_decrypt(tfm, dst, src);
  348. }
  349. int crypto_init_cipher_ops(struct crypto_tfm *tfm)
  350. {
  351. int ret = 0;
  352. struct cipher_tfm *ops = &tfm->crt_cipher;
  353. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  354. ops->cit_setkey = setkey;
  355. ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  356. cipher_encrypt_unaligned : cipher->cia_encrypt;
  357. ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  358. cipher_decrypt_unaligned : cipher->cia_decrypt;
  359. switch (tfm->crt_cipher.cit_mode) {
  360. case CRYPTO_TFM_MODE_ECB:
  361. ops->cit_encrypt = ecb_encrypt;
  362. ops->cit_decrypt = ecb_decrypt;
  363. ops->cit_encrypt_iv = nocrypt_iv;
  364. ops->cit_decrypt_iv = nocrypt_iv;
  365. break;
  366. case CRYPTO_TFM_MODE_CBC:
  367. ops->cit_encrypt = cbc_encrypt;
  368. ops->cit_decrypt = cbc_decrypt;
  369. ops->cit_encrypt_iv = cbc_encrypt_iv;
  370. ops->cit_decrypt_iv = cbc_decrypt_iv;
  371. break;
  372. case CRYPTO_TFM_MODE_CFB:
  373. ops->cit_encrypt = nocrypt;
  374. ops->cit_decrypt = nocrypt;
  375. ops->cit_encrypt_iv = nocrypt_iv;
  376. ops->cit_decrypt_iv = nocrypt_iv;
  377. break;
  378. case CRYPTO_TFM_MODE_CTR:
  379. ops->cit_encrypt = nocrypt;
  380. ops->cit_decrypt = nocrypt;
  381. ops->cit_encrypt_iv = nocrypt_iv;
  382. ops->cit_decrypt_iv = nocrypt_iv;
  383. break;
  384. default:
  385. BUG();
  386. }
  387. if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
  388. unsigned long align;
  389. unsigned long addr;
  390. switch (crypto_tfm_alg_blocksize(tfm)) {
  391. case 8:
  392. ops->cit_xor_block = xor_64;
  393. break;
  394. case 16:
  395. ops->cit_xor_block = xor_128;
  396. break;
  397. default:
  398. printk(KERN_WARNING "%s: block size %u not supported\n",
  399. crypto_tfm_alg_name(tfm),
  400. crypto_tfm_alg_blocksize(tfm));
  401. ret = -EINVAL;
  402. goto out;
  403. }
  404. ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
  405. align = crypto_tfm_alg_alignmask(tfm) + 1;
  406. addr = (unsigned long)crypto_tfm_ctx(tfm);
  407. addr = ALIGN(addr, align);
  408. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  409. ops->cit_iv = (void *)addr;
  410. }
  411. out:
  412. return ret;
  413. }
  414. void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
  415. {
  416. }