cbc.c 7.5 KB

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