cipher.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. 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 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)(void *, u8 *, const u8 *) = desc->crfn;
  155. u8 *iv = desc->info;
  156. unsigned int done = 0;
  157. do {
  158. xor(iv, src);
  159. fn(crypto_tfm_ctx(tfm), dst, iv);
  160. memcpy(iv, dst, bsize);
  161. src += bsize;
  162. dst += bsize;
  163. } while ((done += bsize) < nbytes);
  164. return done;
  165. }
  166. static unsigned int cbc_process_decrypt(const struct cipher_desc *desc,
  167. u8 *dst, const u8 *src,
  168. unsigned int nbytes)
  169. {
  170. struct crypto_tfm *tfm = desc->tfm;
  171. void (*xor)(u8 *, const u8 *) = tfm->crt_u.cipher.cit_xor_block;
  172. int bsize = crypto_tfm_alg_blocksize(tfm);
  173. u8 stack[src == dst ? bsize : 0];
  174. u8 *buf = stack;
  175. u8 **dst_p = src == dst ? &buf : &dst;
  176. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  177. u8 *iv = desc->info;
  178. unsigned int done = 0;
  179. do {
  180. u8 *tmp_dst = *dst_p;
  181. fn(crypto_tfm_ctx(tfm), tmp_dst, src);
  182. xor(tmp_dst, iv);
  183. memcpy(iv, src, bsize);
  184. if (tmp_dst != dst)
  185. memcpy(dst, tmp_dst, bsize);
  186. src += bsize;
  187. dst += bsize;
  188. } while ((done += bsize) < nbytes);
  189. return done;
  190. }
  191. static unsigned int ecb_process(const struct cipher_desc *desc, u8 *dst,
  192. const u8 *src, unsigned int nbytes)
  193. {
  194. struct crypto_tfm *tfm = desc->tfm;
  195. int bsize = crypto_tfm_alg_blocksize(tfm);
  196. void (*fn)(void *, u8 *, const u8 *) = desc->crfn;
  197. unsigned int done = 0;
  198. do {
  199. fn(crypto_tfm_ctx(tfm), dst, src);
  200. src += bsize;
  201. dst += bsize;
  202. } while ((done += bsize) < nbytes);
  203. return done;
  204. }
  205. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  206. {
  207. struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
  208. if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
  209. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  210. return -EINVAL;
  211. } else
  212. return cia->cia_setkey(crypto_tfm_ctx(tfm), key, keylen,
  213. &tfm->crt_flags);
  214. }
  215. static int ecb_encrypt(struct crypto_tfm *tfm,
  216. struct scatterlist *dst,
  217. struct scatterlist *src, unsigned int nbytes)
  218. {
  219. struct cipher_desc desc;
  220. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  221. desc.tfm = tfm;
  222. desc.crfn = cipher->cia_encrypt;
  223. desc.prfn = cipher->cia_encrypt_ecb ?: ecb_process;
  224. return crypt(&desc, dst, src, nbytes);
  225. }
  226. static int ecb_decrypt(struct crypto_tfm *tfm,
  227. struct scatterlist *dst,
  228. struct scatterlist *src,
  229. unsigned int nbytes)
  230. {
  231. struct cipher_desc desc;
  232. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  233. desc.tfm = tfm;
  234. desc.crfn = cipher->cia_decrypt;
  235. desc.prfn = cipher->cia_decrypt_ecb ?: ecb_process;
  236. return crypt(&desc, dst, src, nbytes);
  237. }
  238. static int cbc_encrypt(struct crypto_tfm *tfm,
  239. struct scatterlist *dst,
  240. struct scatterlist *src,
  241. unsigned int nbytes)
  242. {
  243. struct cipher_desc desc;
  244. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  245. desc.tfm = tfm;
  246. desc.crfn = cipher->cia_encrypt;
  247. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  248. desc.info = tfm->crt_cipher.cit_iv;
  249. return crypt(&desc, dst, src, nbytes);
  250. }
  251. static int cbc_encrypt_iv(struct crypto_tfm *tfm,
  252. struct scatterlist *dst,
  253. struct scatterlist *src,
  254. unsigned int nbytes, u8 *iv)
  255. {
  256. struct cipher_desc desc;
  257. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  258. desc.tfm = tfm;
  259. desc.crfn = cipher->cia_encrypt;
  260. desc.prfn = cipher->cia_encrypt_cbc ?: cbc_process_encrypt;
  261. desc.info = iv;
  262. return crypt_iv_unaligned(&desc, dst, src, nbytes);
  263. }
  264. static int cbc_decrypt(struct crypto_tfm *tfm,
  265. struct scatterlist *dst,
  266. struct scatterlist *src,
  267. unsigned int nbytes)
  268. {
  269. struct cipher_desc desc;
  270. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  271. desc.tfm = tfm;
  272. desc.crfn = cipher->cia_decrypt;
  273. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  274. desc.info = tfm->crt_cipher.cit_iv;
  275. return crypt(&desc, dst, src, nbytes);
  276. }
  277. static int cbc_decrypt_iv(struct crypto_tfm *tfm,
  278. struct scatterlist *dst,
  279. struct scatterlist *src,
  280. unsigned int nbytes, u8 *iv)
  281. {
  282. struct cipher_desc desc;
  283. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  284. desc.tfm = tfm;
  285. desc.crfn = cipher->cia_decrypt;
  286. desc.prfn = cipher->cia_decrypt_cbc ?: cbc_process_decrypt;
  287. desc.info = iv;
  288. return crypt_iv_unaligned(&desc, dst, src, nbytes);
  289. }
  290. static int nocrypt(struct crypto_tfm *tfm,
  291. struct scatterlist *dst,
  292. struct scatterlist *src,
  293. unsigned int nbytes)
  294. {
  295. return -ENOSYS;
  296. }
  297. static int nocrypt_iv(struct crypto_tfm *tfm,
  298. struct scatterlist *dst,
  299. struct scatterlist *src,
  300. unsigned int nbytes, u8 *iv)
  301. {
  302. return -ENOSYS;
  303. }
  304. int crypto_init_cipher_flags(struct crypto_tfm *tfm, u32 flags)
  305. {
  306. u32 mode = flags & CRYPTO_TFM_MODE_MASK;
  307. tfm->crt_cipher.cit_mode = mode ? mode : CRYPTO_TFM_MODE_ECB;
  308. if (flags & CRYPTO_TFM_REQ_WEAK_KEY)
  309. tfm->crt_flags = CRYPTO_TFM_REQ_WEAK_KEY;
  310. return 0;
  311. }
  312. int crypto_init_cipher_ops(struct crypto_tfm *tfm)
  313. {
  314. int ret = 0;
  315. struct cipher_tfm *ops = &tfm->crt_cipher;
  316. ops->cit_setkey = setkey;
  317. switch (tfm->crt_cipher.cit_mode) {
  318. case CRYPTO_TFM_MODE_ECB:
  319. ops->cit_encrypt = ecb_encrypt;
  320. ops->cit_decrypt = ecb_decrypt;
  321. break;
  322. case CRYPTO_TFM_MODE_CBC:
  323. ops->cit_encrypt = cbc_encrypt;
  324. ops->cit_decrypt = cbc_decrypt;
  325. ops->cit_encrypt_iv = cbc_encrypt_iv;
  326. ops->cit_decrypt_iv = cbc_decrypt_iv;
  327. break;
  328. case CRYPTO_TFM_MODE_CFB:
  329. ops->cit_encrypt = nocrypt;
  330. ops->cit_decrypt = nocrypt;
  331. ops->cit_encrypt_iv = nocrypt_iv;
  332. ops->cit_decrypt_iv = nocrypt_iv;
  333. break;
  334. case CRYPTO_TFM_MODE_CTR:
  335. ops->cit_encrypt = nocrypt;
  336. ops->cit_decrypt = nocrypt;
  337. ops->cit_encrypt_iv = nocrypt_iv;
  338. ops->cit_decrypt_iv = nocrypt_iv;
  339. break;
  340. default:
  341. BUG();
  342. }
  343. if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
  344. unsigned long align;
  345. unsigned long addr;
  346. switch (crypto_tfm_alg_blocksize(tfm)) {
  347. case 8:
  348. ops->cit_xor_block = xor_64;
  349. break;
  350. case 16:
  351. ops->cit_xor_block = xor_128;
  352. break;
  353. default:
  354. printk(KERN_WARNING "%s: block size %u not supported\n",
  355. crypto_tfm_alg_name(tfm),
  356. crypto_tfm_alg_blocksize(tfm));
  357. ret = -EINVAL;
  358. goto out;
  359. }
  360. ops->cit_ivsize = crypto_tfm_alg_blocksize(tfm);
  361. align = crypto_tfm_alg_alignmask(tfm) + 1;
  362. addr = (unsigned long)crypto_tfm_ctx(tfm);
  363. addr = ALIGN(addr, align);
  364. addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);
  365. ops->cit_iv = (void *)addr;
  366. }
  367. out:
  368. return ret;
  369. }
  370. void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
  371. {
  372. }