pcbc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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/internal/skcipher.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/compiler.h>
  23. struct crypto_pcbc_ctx {
  24. struct crypto_cipher *child;
  25. };
  26. static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
  27. unsigned int keylen)
  28. {
  29. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
  30. struct crypto_cipher *child = ctx->child;
  31. int err;
  32. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  33. crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  34. CRYPTO_TFM_REQ_MASK);
  35. err = crypto_cipher_setkey(child, key, keylen);
  36. crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
  37. CRYPTO_TFM_RES_MASK);
  38. return err;
  39. }
  40. static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
  41. struct skcipher_walk *walk,
  42. struct crypto_cipher *tfm)
  43. {
  44. int bsize = crypto_cipher_blocksize(tfm);
  45. unsigned int nbytes = walk->nbytes;
  46. u8 *src = walk->src.virt.addr;
  47. u8 *dst = walk->dst.virt.addr;
  48. u8 * const iv = walk->iv;
  49. do {
  50. crypto_xor(iv, src, bsize);
  51. crypto_cipher_encrypt_one(tfm, dst, iv);
  52. crypto_xor_cpy(iv, dst, src, bsize);
  53. src += bsize;
  54. dst += bsize;
  55. } while ((nbytes -= bsize) >= bsize);
  56. return nbytes;
  57. }
  58. static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req,
  59. struct skcipher_walk *walk,
  60. struct crypto_cipher *tfm)
  61. {
  62. int bsize = crypto_cipher_blocksize(tfm);
  63. unsigned int nbytes = walk->nbytes;
  64. u8 *src = walk->src.virt.addr;
  65. u8 * const iv = walk->iv;
  66. u8 tmpbuf[bsize];
  67. do {
  68. memcpy(tmpbuf, src, bsize);
  69. crypto_xor(iv, src, bsize);
  70. crypto_cipher_encrypt_one(tfm, src, iv);
  71. crypto_xor_cpy(iv, tmpbuf, src, bsize);
  72. src += bsize;
  73. } while ((nbytes -= bsize) >= bsize);
  74. return nbytes;
  75. }
  76. static int crypto_pcbc_encrypt(struct skcipher_request *req)
  77. {
  78. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  79. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  80. struct crypto_cipher *child = ctx->child;
  81. struct skcipher_walk walk;
  82. unsigned int nbytes;
  83. int err;
  84. err = skcipher_walk_virt(&walk, req, false);
  85. while ((nbytes = walk.nbytes)) {
  86. if (walk.src.virt.addr == walk.dst.virt.addr)
  87. nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
  88. child);
  89. else
  90. nbytes = crypto_pcbc_encrypt_segment(req, &walk,
  91. child);
  92. err = skcipher_walk_done(&walk, nbytes);
  93. }
  94. return err;
  95. }
  96. static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
  97. struct skcipher_walk *walk,
  98. struct crypto_cipher *tfm)
  99. {
  100. int bsize = crypto_cipher_blocksize(tfm);
  101. unsigned int nbytes = walk->nbytes;
  102. u8 *src = walk->src.virt.addr;
  103. u8 *dst = walk->dst.virt.addr;
  104. u8 * const iv = walk->iv;
  105. do {
  106. crypto_cipher_decrypt_one(tfm, dst, src);
  107. crypto_xor(dst, iv, bsize);
  108. crypto_xor_cpy(iv, dst, src, bsize);
  109. src += bsize;
  110. dst += bsize;
  111. } while ((nbytes -= bsize) >= bsize);
  112. return nbytes;
  113. }
  114. static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
  115. struct skcipher_walk *walk,
  116. struct crypto_cipher *tfm)
  117. {
  118. int bsize = crypto_cipher_blocksize(tfm);
  119. unsigned int nbytes = walk->nbytes;
  120. u8 *src = walk->src.virt.addr;
  121. u8 * const iv = walk->iv;
  122. u8 tmpbuf[bsize] __aligned(__alignof__(u32));
  123. do {
  124. memcpy(tmpbuf, src, bsize);
  125. crypto_cipher_decrypt_one(tfm, src, src);
  126. crypto_xor(src, iv, bsize);
  127. crypto_xor_cpy(iv, src, tmpbuf, bsize);
  128. src += bsize;
  129. } while ((nbytes -= bsize) >= bsize);
  130. return nbytes;
  131. }
  132. static int crypto_pcbc_decrypt(struct skcipher_request *req)
  133. {
  134. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  135. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  136. struct crypto_cipher *child = ctx->child;
  137. struct skcipher_walk walk;
  138. unsigned int nbytes;
  139. int err;
  140. err = skcipher_walk_virt(&walk, req, false);
  141. while ((nbytes = walk.nbytes)) {
  142. if (walk.src.virt.addr == walk.dst.virt.addr)
  143. nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
  144. child);
  145. else
  146. nbytes = crypto_pcbc_decrypt_segment(req, &walk,
  147. child);
  148. err = skcipher_walk_done(&walk, nbytes);
  149. }
  150. return err;
  151. }
  152. static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
  153. {
  154. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  155. struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
  156. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  157. struct crypto_cipher *cipher;
  158. cipher = crypto_spawn_cipher(spawn);
  159. if (IS_ERR(cipher))
  160. return PTR_ERR(cipher);
  161. ctx->child = cipher;
  162. return 0;
  163. }
  164. static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
  165. {
  166. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  167. crypto_free_cipher(ctx->child);
  168. }
  169. static void crypto_pcbc_free(struct skcipher_instance *inst)
  170. {
  171. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  172. kfree(inst);
  173. }
  174. static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  175. {
  176. struct skcipher_instance *inst;
  177. struct crypto_attr_type *algt;
  178. struct crypto_spawn *spawn;
  179. struct crypto_alg *alg;
  180. int err;
  181. algt = crypto_get_attr_type(tb);
  182. if (IS_ERR(algt))
  183. return PTR_ERR(algt);
  184. if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
  185. ~CRYPTO_ALG_INTERNAL)
  186. return -EINVAL;
  187. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  188. if (!inst)
  189. return -ENOMEM;
  190. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
  191. (algt->type & CRYPTO_ALG_INTERNAL),
  192. CRYPTO_ALG_TYPE_MASK |
  193. (algt->mask & CRYPTO_ALG_INTERNAL));
  194. err = PTR_ERR(alg);
  195. if (IS_ERR(alg))
  196. goto err_free_inst;
  197. spawn = skcipher_instance_ctx(inst);
  198. err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
  199. CRYPTO_ALG_TYPE_MASK);
  200. crypto_mod_put(alg);
  201. if (err)
  202. goto err_free_inst;
  203. err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
  204. if (err)
  205. goto err_drop_spawn;
  206. inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
  207. inst->alg.base.cra_priority = alg->cra_priority;
  208. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  209. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  210. inst->alg.ivsize = alg->cra_blocksize;
  211. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  212. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  213. inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
  214. inst->alg.init = crypto_pcbc_init_tfm;
  215. inst->alg.exit = crypto_pcbc_exit_tfm;
  216. inst->alg.setkey = crypto_pcbc_setkey;
  217. inst->alg.encrypt = crypto_pcbc_encrypt;
  218. inst->alg.decrypt = crypto_pcbc_decrypt;
  219. inst->free = crypto_pcbc_free;
  220. err = skcipher_register_instance(tmpl, inst);
  221. if (err)
  222. goto err_drop_spawn;
  223. out:
  224. return err;
  225. err_drop_spawn:
  226. crypto_drop_spawn(spawn);
  227. err_free_inst:
  228. kfree(inst);
  229. goto out;
  230. }
  231. static struct crypto_template crypto_pcbc_tmpl = {
  232. .name = "pcbc",
  233. .create = crypto_pcbc_create,
  234. .module = THIS_MODULE,
  235. };
  236. static int __init crypto_pcbc_module_init(void)
  237. {
  238. return crypto_register_template(&crypto_pcbc_tmpl);
  239. }
  240. static void __exit crypto_pcbc_module_exit(void)
  241. {
  242. crypto_unregister_template(&crypto_pcbc_tmpl);
  243. }
  244. module_init(crypto_pcbc_module_init);
  245. module_exit(crypto_pcbc_module_exit);
  246. MODULE_LICENSE("GPL");
  247. MODULE_DESCRIPTION("PCBC block cipher algorithm");
  248. MODULE_ALIAS_CRYPTO("pcbc");