cbc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * CBC: Cipher Block Chaining mode
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/algapi.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/slab.h>
  19. struct crypto_cbc_ctx {
  20. struct crypto_cipher *child;
  21. void (*xor)(u8 *dst, const u8 *src, unsigned int bs);
  22. };
  23. static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key,
  24. unsigned int keylen)
  25. {
  26. struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(parent);
  27. struct crypto_cipher *child = ctx->child;
  28. int err;
  29. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  30. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  31. CRYPTO_TFM_REQ_MASK);
  32. err = crypto_cipher_setkey(child, key, keylen);
  33. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  34. CRYPTO_TFM_RES_MASK);
  35. return err;
  36. }
  37. static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
  38. struct blkcipher_walk *walk,
  39. struct crypto_cipher *tfm,
  40. void (*xor)(u8 *, const u8 *,
  41. unsigned int))
  42. {
  43. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  44. crypto_cipher_alg(tfm)->cia_encrypt;
  45. int bsize = crypto_cipher_blocksize(tfm);
  46. unsigned int nbytes = walk->nbytes;
  47. u8 *src = walk->src.virt.addr;
  48. u8 *dst = walk->dst.virt.addr;
  49. u8 *iv = walk->iv;
  50. do {
  51. xor(iv, src, bsize);
  52. fn(crypto_cipher_tfm(tfm), dst, iv);
  53. memcpy(iv, dst, bsize);
  54. src += bsize;
  55. dst += bsize;
  56. } while ((nbytes -= bsize) >= bsize);
  57. return nbytes;
  58. }
  59. static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
  60. struct blkcipher_walk *walk,
  61. struct crypto_cipher *tfm,
  62. void (*xor)(u8 *, const u8 *,
  63. unsigned int))
  64. {
  65. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  66. crypto_cipher_alg(tfm)->cia_encrypt;
  67. int bsize = crypto_cipher_blocksize(tfm);
  68. unsigned int nbytes = walk->nbytes;
  69. u8 *src = walk->src.virt.addr;
  70. u8 *iv = walk->iv;
  71. do {
  72. xor(src, iv, bsize);
  73. fn(crypto_cipher_tfm(tfm), src, src);
  74. iv = src;
  75. src += bsize;
  76. } while ((nbytes -= bsize) >= bsize);
  77. memcpy(walk->iv, iv, bsize);
  78. return nbytes;
  79. }
  80. static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
  81. struct scatterlist *dst, struct scatterlist *src,
  82. unsigned int nbytes)
  83. {
  84. struct blkcipher_walk walk;
  85. struct crypto_blkcipher *tfm = desc->tfm;
  86. struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
  87. struct crypto_cipher *child = ctx->child;
  88. void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
  89. int err;
  90. blkcipher_walk_init(&walk, dst, src, nbytes);
  91. err = blkcipher_walk_virt(desc, &walk);
  92. while ((nbytes = walk.nbytes)) {
  93. if (walk.src.virt.addr == walk.dst.virt.addr)
  94. nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child,
  95. xor);
  96. else
  97. nbytes = crypto_cbc_encrypt_segment(desc, &walk, child,
  98. xor);
  99. err = blkcipher_walk_done(desc, &walk, nbytes);
  100. }
  101. return err;
  102. }
  103. static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
  104. struct blkcipher_walk *walk,
  105. struct crypto_cipher *tfm,
  106. void (*xor)(u8 *, const u8 *,
  107. unsigned int))
  108. {
  109. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  110. crypto_cipher_alg(tfm)->cia_decrypt;
  111. int bsize = crypto_cipher_blocksize(tfm);
  112. unsigned int nbytes = walk->nbytes;
  113. u8 *src = walk->src.virt.addr;
  114. u8 *dst = walk->dst.virt.addr;
  115. u8 *iv = walk->iv;
  116. do {
  117. fn(crypto_cipher_tfm(tfm), dst, src);
  118. xor(dst, iv, bsize);
  119. iv = src;
  120. src += bsize;
  121. dst += bsize;
  122. } while ((nbytes -= bsize) >= bsize);
  123. memcpy(walk->iv, iv, bsize);
  124. return nbytes;
  125. }
  126. static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
  127. struct blkcipher_walk *walk,
  128. struct crypto_cipher *tfm,
  129. void (*xor)(u8 *, const u8 *,
  130. unsigned int))
  131. {
  132. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  133. crypto_cipher_alg(tfm)->cia_decrypt;
  134. int bsize = crypto_cipher_blocksize(tfm);
  135. unsigned long alignmask = crypto_cipher_alignmask(tfm);
  136. unsigned int nbytes = walk->nbytes;
  137. u8 *src = walk->src.virt.addr;
  138. u8 stack[bsize + alignmask];
  139. u8 *first_iv = (u8 *)ALIGN((unsigned long)stack, alignmask + 1);
  140. memcpy(first_iv, walk->iv, bsize);
  141. /* Start of the last block. */
  142. src += nbytes - nbytes % bsize - bsize;
  143. memcpy(walk->iv, src, bsize);
  144. for (;;) {
  145. fn(crypto_cipher_tfm(tfm), src, src);
  146. if ((nbytes -= bsize) < bsize)
  147. break;
  148. xor(src, src - bsize, bsize);
  149. src -= bsize;
  150. }
  151. xor(src, first_iv, bsize);
  152. return nbytes;
  153. }
  154. static int crypto_cbc_decrypt(struct blkcipher_desc *desc,
  155. struct scatterlist *dst, struct scatterlist *src,
  156. unsigned int nbytes)
  157. {
  158. struct blkcipher_walk walk;
  159. struct crypto_blkcipher *tfm = desc->tfm;
  160. struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
  161. struct crypto_cipher *child = ctx->child;
  162. void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
  163. int err;
  164. blkcipher_walk_init(&walk, dst, src, nbytes);
  165. err = blkcipher_walk_virt(desc, &walk);
  166. while ((nbytes = walk.nbytes)) {
  167. if (walk.src.virt.addr == walk.dst.virt.addr)
  168. nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child,
  169. xor);
  170. else
  171. nbytes = crypto_cbc_decrypt_segment(desc, &walk, child,
  172. xor);
  173. err = blkcipher_walk_done(desc, &walk, nbytes);
  174. }
  175. return err;
  176. }
  177. static void xor_byte(u8 *a, const u8 *b, unsigned int bs)
  178. {
  179. do {
  180. *a++ ^= *b++;
  181. } while (--bs);
  182. }
  183. static void xor_quad(u8 *dst, const u8 *src, unsigned int bs)
  184. {
  185. u32 *a = (u32 *)dst;
  186. u32 *b = (u32 *)src;
  187. do {
  188. *a++ ^= *b++;
  189. } while ((bs -= 4));
  190. }
  191. static void xor_64(u8 *a, const u8 *b, unsigned int bs)
  192. {
  193. ((u32 *)a)[0] ^= ((u32 *)b)[0];
  194. ((u32 *)a)[1] ^= ((u32 *)b)[1];
  195. }
  196. static void xor_128(u8 *a, const u8 *b, unsigned int bs)
  197. {
  198. ((u32 *)a)[0] ^= ((u32 *)b)[0];
  199. ((u32 *)a)[1] ^= ((u32 *)b)[1];
  200. ((u32 *)a)[2] ^= ((u32 *)b)[2];
  201. ((u32 *)a)[3] ^= ((u32 *)b)[3];
  202. }
  203. static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
  204. {
  205. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  206. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  207. struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  208. struct crypto_cipher *cipher;
  209. switch (crypto_tfm_alg_blocksize(tfm)) {
  210. case 8:
  211. ctx->xor = xor_64;
  212. break;
  213. case 16:
  214. ctx->xor = xor_128;
  215. break;
  216. default:
  217. if (crypto_tfm_alg_blocksize(tfm) % 4)
  218. ctx->xor = xor_byte;
  219. else
  220. ctx->xor = xor_quad;
  221. }
  222. cipher = crypto_spawn_cipher(spawn);
  223. if (IS_ERR(cipher))
  224. return PTR_ERR(cipher);
  225. ctx->child = cipher;
  226. return 0;
  227. }
  228. static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm)
  229. {
  230. struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  231. crypto_free_cipher(ctx->child);
  232. }
  233. static struct crypto_instance *crypto_cbc_alloc(void *param, unsigned int len)
  234. {
  235. struct crypto_instance *inst;
  236. struct crypto_alg *alg;
  237. alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
  238. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  239. if (IS_ERR(alg))
  240. return ERR_PTR(PTR_ERR(alg));
  241. inst = crypto_alloc_instance("cbc", alg);
  242. if (IS_ERR(inst))
  243. goto out_put_alg;
  244. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  245. inst->alg.cra_priority = alg->cra_priority;
  246. inst->alg.cra_blocksize = alg->cra_blocksize;
  247. inst->alg.cra_alignmask = alg->cra_alignmask;
  248. inst->alg.cra_type = &crypto_blkcipher_type;
  249. if (!(alg->cra_blocksize % 4))
  250. inst->alg.cra_alignmask |= 3;
  251. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  252. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  253. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  254. inst->alg.cra_ctxsize = sizeof(struct crypto_cbc_ctx);
  255. inst->alg.cra_init = crypto_cbc_init_tfm;
  256. inst->alg.cra_exit = crypto_cbc_exit_tfm;
  257. inst->alg.cra_blkcipher.setkey = crypto_cbc_setkey;
  258. inst->alg.cra_blkcipher.encrypt = crypto_cbc_encrypt;
  259. inst->alg.cra_blkcipher.decrypt = crypto_cbc_decrypt;
  260. out_put_alg:
  261. crypto_mod_put(alg);
  262. return inst;
  263. }
  264. static void crypto_cbc_free(struct crypto_instance *inst)
  265. {
  266. crypto_drop_spawn(crypto_instance_ctx(inst));
  267. kfree(inst);
  268. }
  269. static struct crypto_template crypto_cbc_tmpl = {
  270. .name = "cbc",
  271. .alloc = crypto_cbc_alloc,
  272. .free = crypto_cbc_free,
  273. .module = THIS_MODULE,
  274. };
  275. static int __init crypto_cbc_module_init(void)
  276. {
  277. return crypto_register_template(&crypto_cbc_tmpl);
  278. }
  279. static void __exit crypto_cbc_module_exit(void)
  280. {
  281. crypto_unregister_template(&crypto_cbc_tmpl);
  282. }
  283. module_init(crypto_cbc_module_init);
  284. module_exit(crypto_cbc_module_exit);
  285. MODULE_LICENSE("GPL");
  286. MODULE_DESCRIPTION("CBC block cipher algorithm");