cbc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. };
  22. static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key,
  23. unsigned int keylen)
  24. {
  25. struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(parent);
  26. struct crypto_cipher *child = ctx->child;
  27. int err;
  28. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  29. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  30. CRYPTO_TFM_REQ_MASK);
  31. err = crypto_cipher_setkey(child, key, keylen);
  32. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  33. CRYPTO_TFM_RES_MASK);
  34. return err;
  35. }
  36. static int crypto_cbc_encrypt_segment(struct blkcipher_desc *desc,
  37. struct blkcipher_walk *walk,
  38. struct crypto_cipher *tfm)
  39. {
  40. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  41. crypto_cipher_alg(tfm)->cia_encrypt;
  42. int bsize = crypto_cipher_blocksize(tfm);
  43. unsigned int nbytes = walk->nbytes;
  44. u8 *src = walk->src.virt.addr;
  45. u8 *dst = walk->dst.virt.addr;
  46. u8 *iv = walk->iv;
  47. do {
  48. crypto_xor(iv, src, bsize);
  49. fn(crypto_cipher_tfm(tfm), dst, iv);
  50. memcpy(iv, dst, bsize);
  51. src += bsize;
  52. dst += bsize;
  53. } while ((nbytes -= bsize) >= bsize);
  54. return nbytes;
  55. }
  56. static int crypto_cbc_encrypt_inplace(struct blkcipher_desc *desc,
  57. struct blkcipher_walk *walk,
  58. struct crypto_cipher *tfm)
  59. {
  60. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  61. crypto_cipher_alg(tfm)->cia_encrypt;
  62. int bsize = crypto_cipher_blocksize(tfm);
  63. unsigned int nbytes = walk->nbytes;
  64. u8 *src = walk->src.virt.addr;
  65. u8 *iv = walk->iv;
  66. do {
  67. crypto_xor(src, iv, bsize);
  68. fn(crypto_cipher_tfm(tfm), src, src);
  69. iv = src;
  70. src += bsize;
  71. } while ((nbytes -= bsize) >= bsize);
  72. memcpy(walk->iv, iv, bsize);
  73. return nbytes;
  74. }
  75. static int crypto_cbc_encrypt(struct blkcipher_desc *desc,
  76. struct scatterlist *dst, struct scatterlist *src,
  77. unsigned int nbytes)
  78. {
  79. struct blkcipher_walk walk;
  80. struct crypto_blkcipher *tfm = desc->tfm;
  81. struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
  82. struct crypto_cipher *child = ctx->child;
  83. int err;
  84. blkcipher_walk_init(&walk, dst, src, nbytes);
  85. err = blkcipher_walk_virt(desc, &walk);
  86. while ((nbytes = walk.nbytes)) {
  87. if (walk.src.virt.addr == walk.dst.virt.addr)
  88. nbytes = crypto_cbc_encrypt_inplace(desc, &walk, child);
  89. else
  90. nbytes = crypto_cbc_encrypt_segment(desc, &walk, child);
  91. err = blkcipher_walk_done(desc, &walk, nbytes);
  92. }
  93. return err;
  94. }
  95. static int crypto_cbc_decrypt_segment(struct blkcipher_desc *desc,
  96. struct blkcipher_walk *walk,
  97. struct crypto_cipher *tfm)
  98. {
  99. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  100. crypto_cipher_alg(tfm)->cia_decrypt;
  101. int bsize = crypto_cipher_blocksize(tfm);
  102. unsigned int nbytes = walk->nbytes;
  103. u8 *src = walk->src.virt.addr;
  104. u8 *dst = walk->dst.virt.addr;
  105. u8 *iv = walk->iv;
  106. do {
  107. fn(crypto_cipher_tfm(tfm), dst, src);
  108. crypto_xor(dst, iv, bsize);
  109. iv = src;
  110. src += bsize;
  111. dst += bsize;
  112. } while ((nbytes -= bsize) >= bsize);
  113. memcpy(walk->iv, iv, bsize);
  114. return nbytes;
  115. }
  116. static int crypto_cbc_decrypt_inplace(struct blkcipher_desc *desc,
  117. struct blkcipher_walk *walk,
  118. struct crypto_cipher *tfm)
  119. {
  120. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  121. crypto_cipher_alg(tfm)->cia_decrypt;
  122. int bsize = crypto_cipher_blocksize(tfm);
  123. unsigned long alignmask = crypto_cipher_alignmask(tfm);
  124. unsigned int nbytes = walk->nbytes;
  125. u8 *src = walk->src.virt.addr;
  126. u8 stack[bsize + alignmask];
  127. u8 *first_iv = (u8 *)ALIGN((unsigned long)stack, alignmask + 1);
  128. memcpy(first_iv, walk->iv, bsize);
  129. /* Start of the last block. */
  130. src += nbytes - nbytes % bsize - bsize;
  131. memcpy(walk->iv, src, bsize);
  132. for (;;) {
  133. fn(crypto_cipher_tfm(tfm), src, src);
  134. if ((nbytes -= bsize) < bsize)
  135. break;
  136. crypto_xor(src, src - bsize, bsize);
  137. src -= bsize;
  138. }
  139. crypto_xor(src, first_iv, bsize);
  140. return nbytes;
  141. }
  142. static int crypto_cbc_decrypt(struct blkcipher_desc *desc,
  143. struct scatterlist *dst, struct scatterlist *src,
  144. unsigned int nbytes)
  145. {
  146. struct blkcipher_walk walk;
  147. struct crypto_blkcipher *tfm = desc->tfm;
  148. struct crypto_cbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
  149. struct crypto_cipher *child = ctx->child;
  150. int err;
  151. blkcipher_walk_init(&walk, dst, src, nbytes);
  152. err = blkcipher_walk_virt(desc, &walk);
  153. while ((nbytes = walk.nbytes)) {
  154. if (walk.src.virt.addr == walk.dst.virt.addr)
  155. nbytes = crypto_cbc_decrypt_inplace(desc, &walk, child);
  156. else
  157. nbytes = crypto_cbc_decrypt_segment(desc, &walk, child);
  158. err = blkcipher_walk_done(desc, &walk, nbytes);
  159. }
  160. return err;
  161. }
  162. static int crypto_cbc_init_tfm(struct crypto_tfm *tfm)
  163. {
  164. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  165. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  166. struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  167. struct crypto_cipher *cipher;
  168. cipher = crypto_spawn_cipher(spawn);
  169. if (IS_ERR(cipher))
  170. return PTR_ERR(cipher);
  171. ctx->child = cipher;
  172. return 0;
  173. }
  174. static void crypto_cbc_exit_tfm(struct crypto_tfm *tfm)
  175. {
  176. struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(tfm);
  177. crypto_free_cipher(ctx->child);
  178. }
  179. static struct crypto_instance *crypto_cbc_alloc(struct rtattr **tb)
  180. {
  181. struct crypto_instance *inst;
  182. struct crypto_alg *alg;
  183. int err;
  184. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  185. if (err)
  186. return ERR_PTR(err);
  187. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  188. CRYPTO_ALG_TYPE_MASK);
  189. if (IS_ERR(alg))
  190. return ERR_PTR(PTR_ERR(alg));
  191. inst = crypto_alloc_instance("cbc", alg);
  192. if (IS_ERR(inst))
  193. goto out_put_alg;
  194. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  195. inst->alg.cra_priority = alg->cra_priority;
  196. inst->alg.cra_blocksize = alg->cra_blocksize;
  197. inst->alg.cra_alignmask = alg->cra_alignmask;
  198. inst->alg.cra_type = &crypto_blkcipher_type;
  199. /* We access the data as u32s when xoring. */
  200. inst->alg.cra_alignmask |= __alignof__(u32) - 1;
  201. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  202. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  203. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  204. inst->alg.cra_ctxsize = sizeof(struct crypto_cbc_ctx);
  205. inst->alg.cra_init = crypto_cbc_init_tfm;
  206. inst->alg.cra_exit = crypto_cbc_exit_tfm;
  207. inst->alg.cra_blkcipher.setkey = crypto_cbc_setkey;
  208. inst->alg.cra_blkcipher.encrypt = crypto_cbc_encrypt;
  209. inst->alg.cra_blkcipher.decrypt = crypto_cbc_decrypt;
  210. out_put_alg:
  211. crypto_mod_put(alg);
  212. return inst;
  213. }
  214. static void crypto_cbc_free(struct crypto_instance *inst)
  215. {
  216. crypto_drop_spawn(crypto_instance_ctx(inst));
  217. kfree(inst);
  218. }
  219. static struct crypto_template crypto_cbc_tmpl = {
  220. .name = "cbc",
  221. .alloc = crypto_cbc_alloc,
  222. .free = crypto_cbc_free,
  223. .module = THIS_MODULE,
  224. };
  225. static int __init crypto_cbc_module_init(void)
  226. {
  227. return crypto_register_template(&crypto_cbc_tmpl);
  228. }
  229. static void __exit crypto_cbc_module_exit(void)
  230. {
  231. crypto_unregister_template(&crypto_cbc_tmpl);
  232. }
  233. module_init(crypto_cbc_module_init);
  234. module_exit(crypto_cbc_module_exit);
  235. MODULE_LICENSE("GPL");
  236. MODULE_DESCRIPTION("CBC block cipher algorithm");