cipher.c 14 KB

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