pcbc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * PCBC: Propagating Cipher Block Chaining mode
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. *
  7. * Derived from cbc.c
  8. * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/algapi.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/compiler.h>
  24. struct crypto_pcbc_ctx {
  25. struct crypto_cipher *child;
  26. };
  27. static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
  28. unsigned int keylen)
  29. {
  30. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
  31. struct crypto_cipher *child = ctx->child;
  32. int err;
  33. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  34. crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  35. CRYPTO_TFM_REQ_MASK);
  36. err = crypto_cipher_setkey(child, key, keylen);
  37. crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
  38. CRYPTO_TFM_RES_MASK);
  39. return err;
  40. }
  41. static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
  42. struct skcipher_walk *walk,
  43. struct crypto_cipher *tfm)
  44. {
  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. crypto_xor(iv, src, bsize);
  52. crypto_cipher_encrypt_one(tfm, dst, iv);
  53. crypto_xor_cpy(iv, dst, src, bsize);
  54. src += bsize;
  55. dst += bsize;
  56. } while ((nbytes -= bsize) >= bsize);
  57. return nbytes;
  58. }
  59. static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
  60. struct skcipher_walk *walk,
  61. struct crypto_cipher *tfm)
  62. {
  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. u8 tmpbuf[MAX_CIPHER_BLOCKSIZE];
  68. do {
  69. memcpy(tmpbuf, src, bsize);
  70. crypto_xor(iv, src, bsize);
  71. crypto_cipher_encrypt_one(tfm, src, iv);
  72. crypto_xor_cpy(iv, tmpbuf, src, bsize);
  73. src += bsize;
  74. } while ((nbytes -= bsize) >= bsize);
  75. memcpy(walk->iv, iv, bsize);
  76. return nbytes;
  77. }
  78. static int crypto_pcbc_encrypt(struct skcipher_request *req)
  79. {
  80. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  81. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  82. struct crypto_cipher *child = ctx->child;
  83. struct skcipher_walk walk;
  84. unsigned int nbytes;
  85. int err;
  86. err = skcipher_walk_virt(&walk, req, false);
  87. while ((nbytes = walk.nbytes)) {
  88. if (walk.src.virt.addr == walk.dst.virt.addr)
  89. nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
  90. child);
  91. else
  92. nbytes = crypto_pcbc_encrypt_segment(req, &walk,
  93. child);
  94. err = skcipher_walk_done(&walk, nbytes);
  95. }
  96. return err;
  97. }
  98. static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
  99. struct skcipher_walk *walk,
  100. struct crypto_cipher *tfm)
  101. {
  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. crypto_cipher_decrypt_one(tfm, dst, src);
  109. crypto_xor(dst, iv, bsize);
  110. crypto_xor_cpy(iv, dst, src, bsize);
  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_pcbc_decrypt_inplace(struct skcipher_request *req,
  118. struct skcipher_walk *walk,
  119. struct crypto_cipher *tfm)
  120. {
  121. int bsize = crypto_cipher_blocksize(tfm);
  122. unsigned int nbytes = walk->nbytes;
  123. u8 *src = walk->src.virt.addr;
  124. u8 *iv = walk->iv;
  125. u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32));
  126. do {
  127. memcpy(tmpbuf, src, bsize);
  128. crypto_cipher_decrypt_one(tfm, src, src);
  129. crypto_xor(src, iv, bsize);
  130. crypto_xor_cpy(iv, src, tmpbuf, bsize);
  131. src += bsize;
  132. } while ((nbytes -= bsize) >= bsize);
  133. memcpy(walk->iv, iv, bsize);
  134. return nbytes;
  135. }
  136. static int crypto_pcbc_decrypt(struct skcipher_request *req)
  137. {
  138. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  139. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  140. struct crypto_cipher *child = ctx->child;
  141. struct skcipher_walk walk;
  142. unsigned int nbytes;
  143. int err;
  144. err = skcipher_walk_virt(&walk, req, false);
  145. while ((nbytes = walk.nbytes)) {
  146. if (walk.src.virt.addr == walk.dst.virt.addr)
  147. nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
  148. child);
  149. else
  150. nbytes = crypto_pcbc_decrypt_segment(req, &walk,
  151. child);
  152. err = skcipher_walk_done(&walk, nbytes);
  153. }
  154. return err;
  155. }
  156. static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
  157. {
  158. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  159. struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
  160. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  161. struct crypto_cipher *cipher;
  162. cipher = crypto_spawn_cipher(spawn);
  163. if (IS_ERR(cipher))
  164. return PTR_ERR(cipher);
  165. ctx->child = cipher;
  166. return 0;
  167. }
  168. static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
  169. {
  170. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  171. crypto_free_cipher(ctx->child);
  172. }
  173. static void crypto_pcbc_free(struct skcipher_instance *inst)
  174. {
  175. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  176. kfree(inst);
  177. }
  178. static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  179. {
  180. struct skcipher_instance *inst;
  181. struct crypto_attr_type *algt;
  182. struct crypto_spawn *spawn;
  183. struct crypto_alg *alg;
  184. int err;
  185. algt = crypto_get_attr_type(tb);
  186. if (IS_ERR(algt))
  187. return PTR_ERR(algt);
  188. if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
  189. ~CRYPTO_ALG_INTERNAL)
  190. return -EINVAL;
  191. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  192. if (!inst)
  193. return -ENOMEM;
  194. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
  195. (algt->type & CRYPTO_ALG_INTERNAL),
  196. CRYPTO_ALG_TYPE_MASK |
  197. (algt->mask & CRYPTO_ALG_INTERNAL));
  198. err = PTR_ERR(alg);
  199. if (IS_ERR(alg))
  200. goto err_free_inst;
  201. spawn = skcipher_instance_ctx(inst);
  202. err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
  203. CRYPTO_ALG_TYPE_MASK);
  204. if (err)
  205. goto err_put_alg;
  206. err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
  207. if (err)
  208. goto err_drop_spawn;
  209. inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
  210. inst->alg.base.cra_priority = alg->cra_priority;
  211. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  212. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  213. inst->alg.ivsize = alg->cra_blocksize;
  214. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  215. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  216. inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
  217. inst->alg.init = crypto_pcbc_init_tfm;
  218. inst->alg.exit = crypto_pcbc_exit_tfm;
  219. inst->alg.setkey = crypto_pcbc_setkey;
  220. inst->alg.encrypt = crypto_pcbc_encrypt;
  221. inst->alg.decrypt = crypto_pcbc_decrypt;
  222. inst->free = crypto_pcbc_free;
  223. err = skcipher_register_instance(tmpl, inst);
  224. if (err)
  225. goto err_drop_spawn;
  226. crypto_mod_put(alg);
  227. out:
  228. return err;
  229. err_drop_spawn:
  230. crypto_drop_spawn(spawn);
  231. err_put_alg:
  232. crypto_mod_put(alg);
  233. err_free_inst:
  234. kfree(inst);
  235. goto out;
  236. }
  237. static struct crypto_template crypto_pcbc_tmpl = {
  238. .name = "pcbc",
  239. .create = crypto_pcbc_create,
  240. .module = THIS_MODULE,
  241. };
  242. static int __init crypto_pcbc_module_init(void)
  243. {
  244. return crypto_register_template(&crypto_pcbc_tmpl);
  245. }
  246. static void __exit crypto_pcbc_module_exit(void)
  247. {
  248. crypto_unregister_template(&crypto_pcbc_tmpl);
  249. }
  250. module_init(crypto_pcbc_module_init);
  251. module_exit(crypto_pcbc_module_exit);
  252. MODULE_LICENSE("GPL");
  253. MODULE_DESCRIPTION("PCBC block cipher algorithm");
  254. MODULE_ALIAS_CRYPTO("pcbc");