pcbc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. struct crypto_pcbc_ctx {
  23. struct crypto_cipher *child;
  24. };
  25. static int crypto_pcbc_setkey(struct crypto_skcipher *parent, const u8 *key,
  26. unsigned int keylen)
  27. {
  28. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(parent);
  29. struct crypto_cipher *child = ctx->child;
  30. int err;
  31. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  32. crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  33. CRYPTO_TFM_REQ_MASK);
  34. err = crypto_cipher_setkey(child, key, keylen);
  35. crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
  36. CRYPTO_TFM_RES_MASK);
  37. return err;
  38. }
  39. static int crypto_pcbc_encrypt_segment(struct skcipher_request *req,
  40. struct skcipher_walk *walk,
  41. struct crypto_cipher *tfm)
  42. {
  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. crypto_cipher_encrypt_one(tfm, dst, iv);
  51. memcpy(iv, dst, bsize);
  52. crypto_xor(iv, 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 *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. memcpy(iv, tmpbuf, bsize);
  72. crypto_xor(iv, 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. memcpy(iv, src, bsize);
  111. crypto_xor(iv, dst, bsize);
  112. src += bsize;
  113. dst += bsize;
  114. } while ((nbytes -= bsize) >= bsize);
  115. memcpy(walk->iv, iv, bsize);
  116. return nbytes;
  117. }
  118. static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req,
  119. struct skcipher_walk *walk,
  120. struct crypto_cipher *tfm)
  121. {
  122. int bsize = crypto_cipher_blocksize(tfm);
  123. unsigned int nbytes = walk->nbytes;
  124. u8 *src = walk->src.virt.addr;
  125. u8 *iv = walk->iv;
  126. u8 tmpbuf[bsize] __attribute__ ((aligned(__alignof__(u32))));
  127. do {
  128. memcpy(tmpbuf, src, bsize);
  129. crypto_cipher_decrypt_one(tfm, src, src);
  130. crypto_xor(src, iv, bsize);
  131. memcpy(iv, tmpbuf, bsize);
  132. crypto_xor(iv, src, bsize);
  133. src += bsize;
  134. } while ((nbytes -= bsize) >= bsize);
  135. memcpy(walk->iv, iv, bsize);
  136. return nbytes;
  137. }
  138. static int crypto_pcbc_decrypt(struct skcipher_request *req)
  139. {
  140. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  141. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  142. struct crypto_cipher *child = ctx->child;
  143. struct skcipher_walk walk;
  144. unsigned int nbytes;
  145. int err;
  146. err = skcipher_walk_virt(&walk, req, false);
  147. while ((nbytes = walk.nbytes)) {
  148. if (walk.src.virt.addr == walk.dst.virt.addr)
  149. nbytes = crypto_pcbc_decrypt_inplace(req, &walk,
  150. child);
  151. else
  152. nbytes = crypto_pcbc_decrypt_segment(req, &walk,
  153. child);
  154. err = skcipher_walk_done(&walk, nbytes);
  155. }
  156. return err;
  157. }
  158. static int crypto_pcbc_init_tfm(struct crypto_skcipher *tfm)
  159. {
  160. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  161. struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
  162. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  163. struct crypto_cipher *cipher;
  164. cipher = crypto_spawn_cipher(spawn);
  165. if (IS_ERR(cipher))
  166. return PTR_ERR(cipher);
  167. ctx->child = cipher;
  168. return 0;
  169. }
  170. static void crypto_pcbc_exit_tfm(struct crypto_skcipher *tfm)
  171. {
  172. struct crypto_pcbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  173. crypto_free_cipher(ctx->child);
  174. }
  175. static void crypto_pcbc_free(struct skcipher_instance *inst)
  176. {
  177. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  178. kfree(inst);
  179. }
  180. static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  181. {
  182. struct skcipher_instance *inst;
  183. struct crypto_attr_type *algt;
  184. struct crypto_spawn *spawn;
  185. struct crypto_alg *alg;
  186. int err;
  187. algt = crypto_get_attr_type(tb);
  188. if (IS_ERR(algt))
  189. return PTR_ERR(algt);
  190. if (((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask) &
  191. ~CRYPTO_ALG_INTERNAL)
  192. return -EINVAL;
  193. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  194. if (!inst)
  195. return -ENOMEM;
  196. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER |
  197. (algt->type & CRYPTO_ALG_INTERNAL),
  198. CRYPTO_ALG_TYPE_MASK |
  199. (algt->mask & CRYPTO_ALG_INTERNAL));
  200. err = PTR_ERR(alg);
  201. if (IS_ERR(alg))
  202. goto err_free_inst;
  203. spawn = skcipher_instance_ctx(inst);
  204. err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
  205. CRYPTO_ALG_TYPE_MASK);
  206. crypto_mod_put(alg);
  207. if (err)
  208. goto err_free_inst;
  209. err = crypto_inst_setname(skcipher_crypto_instance(inst), "pcbc", alg);
  210. if (err)
  211. goto err_drop_spawn;
  212. inst->alg.base.cra_flags = alg->cra_flags & CRYPTO_ALG_INTERNAL;
  213. inst->alg.base.cra_priority = alg->cra_priority;
  214. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  215. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  216. /* We access the data as u32s when xoring. */
  217. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  218. inst->alg.ivsize = alg->cra_blocksize;
  219. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  220. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  221. inst->alg.base.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
  222. inst->alg.init = crypto_pcbc_init_tfm;
  223. inst->alg.exit = crypto_pcbc_exit_tfm;
  224. inst->alg.setkey = crypto_pcbc_setkey;
  225. inst->alg.encrypt = crypto_pcbc_encrypt;
  226. inst->alg.decrypt = crypto_pcbc_decrypt;
  227. inst->free = crypto_pcbc_free;
  228. err = skcipher_register_instance(tmpl, inst);
  229. if (err)
  230. goto err_drop_spawn;
  231. out:
  232. return err;
  233. err_drop_spawn:
  234. crypto_drop_spawn(spawn);
  235. err_free_inst:
  236. kfree(inst);
  237. goto out;
  238. }
  239. static struct crypto_template crypto_pcbc_tmpl = {
  240. .name = "pcbc",
  241. .create = crypto_pcbc_create,
  242. .module = THIS_MODULE,
  243. };
  244. static int __init crypto_pcbc_module_init(void)
  245. {
  246. return crypto_register_template(&crypto_pcbc_tmpl);
  247. }
  248. static void __exit crypto_pcbc_module_exit(void)
  249. {
  250. crypto_unregister_template(&crypto_pcbc_tmpl);
  251. }
  252. module_init(crypto_pcbc_module_init);
  253. module_exit(crypto_pcbc_module_exit);
  254. MODULE_LICENSE("GPL");
  255. MODULE_DESCRIPTION("PCBC block cipher algorithm");
  256. MODULE_ALIAS_CRYPTO("pcbc");