pcbc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 *iv = walk->iv;
  49. do {
  50. crypto_xor(iv, src, bsize);
  51. crypto_cipher_encrypt_one(tfm, dst, iv);
  52. memcpy(iv, dst, bsize);
  53. crypto_xor(iv, 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[bsize];
  68. do {
  69. memcpy(tmpbuf, src, bsize);
  70. crypto_xor(iv, src, bsize);
  71. crypto_cipher_encrypt_one(tfm, src, iv);
  72. memcpy(iv, tmpbuf, bsize);
  73. crypto_xor(iv, src, bsize);
  74. src += bsize;
  75. } while ((nbytes -= bsize) >= bsize);
  76. memcpy(walk->iv, iv, bsize);
  77. return nbytes;
  78. }
  79. static int crypto_pcbc_encrypt(struct skcipher_request *req)
  80. {
  81. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  82. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  83. struct crypto_cipher *child = ctx->child;
  84. struct skcipher_walk walk;
  85. unsigned int nbytes;
  86. int err;
  87. err = skcipher_walk_virt(&walk, req, false);
  88. while ((nbytes = walk.nbytes)) {
  89. if (walk.src.virt.addr == walk.dst.virt.addr)
  90. nbytes = crypto_pcbc_encrypt_inplace(req, &walk,
  91. child);
  92. else
  93. nbytes = crypto_pcbc_encrypt_segment(req, &walk,
  94. child);
  95. err = skcipher_walk_done(&walk, nbytes);
  96. }
  97. return err;
  98. }
  99. static int crypto_pcbc_decrypt_segment(struct skcipher_request *req,
  100. struct skcipher_walk *walk,
  101. struct crypto_cipher *tfm)
  102. {
  103. int bsize = crypto_cipher_blocksize(tfm);
  104. unsigned int nbytes = walk->nbytes;
  105. u8 *src = walk->src.virt.addr;
  106. u8 *dst = walk->dst.virt.addr;
  107. u8 *iv = walk->iv;
  108. do {
  109. crypto_cipher_decrypt_one(tfm, dst, src);
  110. crypto_xor(dst, iv, bsize);
  111. memcpy(iv, src, bsize);
  112. crypto_xor(iv, dst, bsize);
  113. src += bsize;
  114. dst += bsize;
  115. } while ((nbytes -= bsize) >= bsize);
  116. memcpy(walk->iv, iv, bsize);
  117. return nbytes;
  118. }
  119. static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
  120. struct skcipher_walk *walk,
  121. struct crypto_cipher *tfm)
  122. {
  123. int bsize = crypto_cipher_blocksize(tfm);
  124. unsigned int nbytes = walk->nbytes;
  125. u8 *src = walk->src.virt.addr;
  126. u8 *iv = walk->iv;
  127. u8 tmpbuf[bsize] __aligned(__alignof__(u32));
  128. do {
  129. memcpy(tmpbuf, src, bsize);
  130. crypto_cipher_decrypt_one(tfm, src, src);
  131. crypto_xor(src, iv, bsize);
  132. memcpy(iv, tmpbuf, bsize);
  133. crypto_xor(iv, src, bsize);
  134. src += bsize;
  135. } while ((nbytes -= bsize) >= bsize);
  136. memcpy(walk->iv, iv, bsize);
  137. return nbytes;
  138. }
  139. static int crypto_pcbc_decrypt(struct skcipher_request *req)
  140. {
  141. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  142. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  143. struct crypto_cipher *child = ctx->child;
  144. struct skcipher_walk walk;
  145. unsigned int nbytes;
  146. int err;
  147. err = skcipher_walk_virt(&walk, req, false);
  148. while ((nbytes = walk.nbytes)) {
  149. if (walk.src.virt.addr == walk.dst.virt.addr)
  150. nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
  151. child);
  152. else
  153. nbytes = crypto_pcbc_decrypt_segment(req, &walk,
  154. child);
  155. err = skcipher_walk_done(&walk, nbytes);
  156. }
  157. return err;
  158. }
  159. static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
  160. {
  161. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  162. struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
  163. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  164. struct crypto_cipher *cipher;
  165. cipher = crypto_spawn_cipher(spawn);
  166. if (IS_ERR(cipher))
  167. return PTR_ERR(cipher);
  168. ctx->child = cipher;
  169. return 0;
  170. }
  171. static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
  172. {
  173. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  174. crypto_free_cipher(ctx->child);
  175. }
  176. static void crypto_pcbc_free(struct skcipher_instance *inst)
  177. {
  178. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  179. kfree(inst);
  180. }
  181. static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  182. {
  183. struct skcipher_instance *inst;
  184. struct crypto_attr_type *algt;
  185. struct crypto_spawn *spawn;
  186. struct crypto_alg *alg;
  187. int err;
  188. algt = crypto_get_attr_type(tb);
  189. if (IS_ERR(algt))
  190. return PTR_ERR(algt);
  191. if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
  192. ~CRYPTO_ALG_INTERNAL)
  193. return -EINVAL;
  194. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  195. if (!inst)
  196. return -ENOMEM;
  197. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
  198. (algt->type & CRYPTO_ALG_INTERNAL),
  199. CRYPTO_ALG_TYPE_MASK |
  200. (algt->mask & CRYPTO_ALG_INTERNAL));
  201. err = PTR_ERR(alg);
  202. if (IS_ERR(alg))
  203. goto err_free_inst;
  204. spawn = skcipher_instance_ctx(inst);
  205. err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
  206. CRYPTO_ALG_TYPE_MASK);
  207. crypto_mod_put(alg);
  208. if (err)
  209. goto err_free_inst;
  210. err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
  211. if (err)
  212. goto err_drop_spawn;
  213. inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
  214. inst->alg.base.cra_priority = alg->cra_priority;
  215. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  216. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  217. /* We access the data as u32s when xoring. */
  218. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  219. inst->alg.ivsize = alg->cra_blocksize;
  220. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  221. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  222. inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
  223. inst->alg.init = crypto_pcbc_init_tfm;
  224. inst->alg.exit = crypto_pcbc_exit_tfm;
  225. inst->alg.setkey = crypto_pcbc_setkey;
  226. inst->alg.encrypt = crypto_pcbc_encrypt;
  227. inst->alg.decrypt = crypto_pcbc_decrypt;
  228. inst->free = crypto_pcbc_free;
  229. err = skcipher_register_instance(tmpl, inst);
  230. if (err)
  231. goto err_drop_spawn;
  232. out:
  233. return err;
  234. err_drop_spawn:
  235. crypto_drop_spawn(spawn);
  236. err_free_inst:
  237. kfree(inst);
  238. goto out;
  239. }
  240. static struct crypto_template crypto_pcbc_tmpl = {
  241. .name = "pcbc",
  242. .create = crypto_pcbc_create,
  243. .module = THIS_MODULE,
  244. };
  245. static int __init crypto_pcbc_module_init(void)
  246. {
  247. return crypto_register_template(&crypto_pcbc_tmpl);
  248. }
  249. static void __exit crypto_pcbc_module_exit(void)
  250. {
  251. crypto_unregister_template(&crypto_pcbc_tmpl);
  252. }
  253. module_init(crypto_pcbc_module_init);
  254. module_exit(crypto_pcbc_module_exit);
  255. MODULE_LICENSE("GPL");
  256. MODULE_DESCRIPTION("PCBC block cipher algorithm");
  257. MODULE_ALIAS_CRYPTO("pcbc");