pcbc.c 7.7 KB

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