pcbc.c 7.6 KB

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