pcbc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. void (*xor)(u8 *dst, const u8 *src, unsigned int bs);
  26. };
  27. static int crypto_pcbc_setkey(struct crypto_tfm *parent, const u8 *key,
  28. unsigned int keylen)
  29. {
  30. struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(parent);
  31. struct crypto_cipher *child = ctx->child;
  32. int err;
  33. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  34. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  35. CRYPTO_TFM_REQ_MASK);
  36. err = crypto_cipher_setkey(child, key, keylen);
  37. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  38. CRYPTO_TFM_RES_MASK);
  39. return err;
  40. }
  41. static int crypto_pcbc_encrypt_segment(struct blkcipher_desc *desc,
  42. struct blkcipher_walk *walk,
  43. struct crypto_cipher *tfm,
  44. void (*xor)(u8 *, const u8 *,
  45. unsigned int))
  46. {
  47. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  48. crypto_cipher_alg(tfm)->cia_encrypt;
  49. int bsize = crypto_cipher_blocksize(tfm);
  50. unsigned int nbytes = walk->nbytes;
  51. u8 *src = walk->src.virt.addr;
  52. u8 *dst = walk->dst.virt.addr;
  53. u8 *iv = walk->iv;
  54. do {
  55. xor(iv, src, bsize);
  56. fn(crypto_cipher_tfm(tfm), dst, iv);
  57. memcpy(iv, dst, bsize);
  58. xor(iv, src, bsize);
  59. src += bsize;
  60. dst += bsize;
  61. } while ((nbytes -= bsize) >= bsize);
  62. return nbytes;
  63. }
  64. static int crypto_pcbc_encrypt_inplace(struct blkcipher_desc *desc,
  65. struct blkcipher_walk *walk,
  66. struct crypto_cipher *tfm,
  67. void (*xor)(u8 *, const u8 *,
  68. unsigned int))
  69. {
  70. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  71. crypto_cipher_alg(tfm)->cia_encrypt;
  72. int bsize = crypto_cipher_blocksize(tfm);
  73. unsigned int nbytes = walk->nbytes;
  74. u8 *src = walk->src.virt.addr;
  75. u8 *iv = walk->iv;
  76. u8 tmpbuf[bsize];
  77. do {
  78. memcpy(tmpbuf, src, bsize);
  79. xor(iv, tmpbuf, bsize);
  80. fn(crypto_cipher_tfm(tfm), src, iv);
  81. memcpy(iv, src, bsize);
  82. xor(iv, tmpbuf, bsize);
  83. src += bsize;
  84. } while ((nbytes -= bsize) >= bsize);
  85. memcpy(walk->iv, iv, bsize);
  86. return nbytes;
  87. }
  88. static int crypto_pcbc_encrypt(struct blkcipher_desc *desc,
  89. struct scatterlist *dst, struct scatterlist *src,
  90. unsigned int nbytes)
  91. {
  92. struct blkcipher_walk walk;
  93. struct crypto_blkcipher *tfm = desc->tfm;
  94. struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
  95. struct crypto_cipher *child = ctx->child;
  96. void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
  97. int err;
  98. blkcipher_walk_init(&walk, dst, src, nbytes);
  99. err = blkcipher_walk_virt(desc, &walk);
  100. while ((nbytes = walk.nbytes)) {
  101. if (walk.src.virt.addr == walk.dst.virt.addr)
  102. nbytes = crypto_pcbc_encrypt_inplace(desc, &walk, child,
  103. xor);
  104. else
  105. nbytes = crypto_pcbc_encrypt_segment(desc, &walk, child,
  106. xor);
  107. err = blkcipher_walk_done(desc, &walk, nbytes);
  108. }
  109. return err;
  110. }
  111. static int crypto_pcbc_decrypt_segment(struct blkcipher_desc *desc,
  112. struct blkcipher_walk *walk,
  113. struct crypto_cipher *tfm,
  114. void (*xor)(u8 *, const u8 *,
  115. unsigned int))
  116. {
  117. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  118. crypto_cipher_alg(tfm)->cia_decrypt;
  119. int bsize = crypto_cipher_blocksize(tfm);
  120. unsigned int nbytes = walk->nbytes;
  121. u8 *src = walk->src.virt.addr;
  122. u8 *dst = walk->dst.virt.addr;
  123. u8 *iv = walk->iv;
  124. do {
  125. fn(crypto_cipher_tfm(tfm), dst, src);
  126. xor(dst, iv, bsize);
  127. memcpy(iv, src, bsize);
  128. xor(iv, dst, bsize);
  129. src += bsize;
  130. dst += bsize;
  131. } while ((nbytes -= bsize) >= bsize);
  132. memcpy(walk->iv, iv, bsize);
  133. return nbytes;
  134. }
  135. static int crypto_pcbc_decrypt_inplace(struct blkcipher_desc *desc,
  136. struct blkcipher_walk *walk,
  137. struct crypto_cipher *tfm,
  138. void (*xor)(u8 *, const u8 *,
  139. unsigned int))
  140. {
  141. void (*fn)(struct crypto_tfm *, u8 *, const u8 *) =
  142. crypto_cipher_alg(tfm)->cia_decrypt;
  143. int bsize = crypto_cipher_blocksize(tfm);
  144. unsigned int nbytes = walk->nbytes;
  145. u8 *src = walk->src.virt.addr;
  146. u8 *iv = walk->iv;
  147. u8 tmpbuf[bsize];
  148. do {
  149. memcpy(tmpbuf, src, bsize);
  150. fn(crypto_cipher_tfm(tfm), src, src);
  151. xor(src, iv, bsize);
  152. memcpy(iv, tmpbuf, bsize);
  153. xor(iv, src, bsize);
  154. src += bsize;
  155. } while ((nbytes -= bsize) >= bsize);
  156. memcpy(walk->iv, iv, bsize);
  157. return nbytes;
  158. }
  159. static int crypto_pcbc_decrypt(struct blkcipher_desc *desc,
  160. struct scatterlist *dst, struct scatterlist *src,
  161. unsigned int nbytes)
  162. {
  163. struct blkcipher_walk walk;
  164. struct crypto_blkcipher *tfm = desc->tfm;
  165. struct crypto_pcbc_ctx *ctx = crypto_blkcipher_ctx(tfm);
  166. struct crypto_cipher *child = ctx->child;
  167. void (*xor)(u8 *, const u8 *, unsigned int bs) = ctx->xor;
  168. int err;
  169. blkcipher_walk_init(&walk, dst, src, nbytes);
  170. err = blkcipher_walk_virt(desc, &walk);
  171. while ((nbytes = walk.nbytes)) {
  172. if (walk.src.virt.addr == walk.dst.virt.addr)
  173. nbytes = crypto_pcbc_decrypt_inplace(desc, &walk, child,
  174. xor);
  175. else
  176. nbytes = crypto_pcbc_decrypt_segment(desc, &walk, child,
  177. xor);
  178. err = blkcipher_walk_done(desc, &walk, nbytes);
  179. }
  180. return err;
  181. }
  182. static void xor_byte(u8 *a, const u8 *b, unsigned int bs)
  183. {
  184. do {
  185. *a++ ^= *b++;
  186. } while (--bs);
  187. }
  188. static void xor_quad(u8 *dst, const u8 *src, unsigned int bs)
  189. {
  190. u32 *a = (u32 *)dst;
  191. u32 *b = (u32 *)src;
  192. do {
  193. *a++ ^= *b++;
  194. } while ((bs -= 4));
  195. }
  196. static void xor_64(u8 *a, const u8 *b, unsigned int bs)
  197. {
  198. ((u32 *)a)[0] ^= ((u32 *)b)[0];
  199. ((u32 *)a)[1] ^= ((u32 *)b)[1];
  200. }
  201. static void xor_128(u8 *a, const u8 *b, unsigned int bs)
  202. {
  203. ((u32 *)a)[0] ^= ((u32 *)b)[0];
  204. ((u32 *)a)[1] ^= ((u32 *)b)[1];
  205. ((u32 *)a)[2] ^= ((u32 *)b)[2];
  206. ((u32 *)a)[3] ^= ((u32 *)b)[3];
  207. }
  208. static int crypto_pcbc_init_tfm(struct crypto_tfm *tfm)
  209. {
  210. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  211. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  212. struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
  213. struct crypto_cipher *cipher;
  214. switch (crypto_tfm_alg_blocksize(tfm)) {
  215. case 8:
  216. ctx->xor = xor_64;
  217. break;
  218. case 16:
  219. ctx->xor = xor_128;
  220. break;
  221. default:
  222. if (crypto_tfm_alg_blocksize(tfm) % 4)
  223. ctx->xor = xor_byte;
  224. else
  225. ctx->xor = xor_quad;
  226. }
  227. cipher = crypto_spawn_cipher(spawn);
  228. if (IS_ERR(cipher))
  229. return PTR_ERR(cipher);
  230. ctx->child = cipher;
  231. return 0;
  232. }
  233. static void crypto_pcbc_exit_tfm(struct crypto_tfm *tfm)
  234. {
  235. struct crypto_pcbc_ctx *ctx = crypto_tfm_ctx(tfm);
  236. crypto_free_cipher(ctx->child);
  237. }
  238. static struct crypto_instance *crypto_pcbc_alloc(void *param, unsigned int len)
  239. {
  240. struct crypto_instance *inst;
  241. struct crypto_alg *alg;
  242. alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
  243. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  244. if (IS_ERR(alg))
  245. return ERR_PTR(PTR_ERR(alg));
  246. inst = crypto_alloc_instance("pcbc", alg);
  247. if (IS_ERR(inst))
  248. goto out_put_alg;
  249. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  250. inst->alg.cra_priority = alg->cra_priority;
  251. inst->alg.cra_blocksize = alg->cra_blocksize;
  252. inst->alg.cra_alignmask = alg->cra_alignmask;
  253. inst->alg.cra_type = &crypto_blkcipher_type;
  254. if (!(alg->cra_blocksize % 4))
  255. inst->alg.cra_alignmask |= 3;
  256. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  257. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  258. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  259. inst->alg.cra_ctxsize = sizeof(struct crypto_pcbc_ctx);
  260. inst->alg.cra_init = crypto_pcbc_init_tfm;
  261. inst->alg.cra_exit = crypto_pcbc_exit_tfm;
  262. inst->alg.cra_blkcipher.setkey = crypto_pcbc_setkey;
  263. inst->alg.cra_blkcipher.encrypt = crypto_pcbc_encrypt;
  264. inst->alg.cra_blkcipher.decrypt = crypto_pcbc_decrypt;
  265. out_put_alg:
  266. crypto_mod_put(alg);
  267. return inst;
  268. }
  269. static void crypto_pcbc_free(struct crypto_instance *inst)
  270. {
  271. crypto_drop_spawn(crypto_instance_ctx(inst));
  272. kfree(inst);
  273. }
  274. static struct crypto_template crypto_pcbc_tmpl = {
  275. .name = "pcbc",
  276. .alloc = crypto_pcbc_alloc,
  277. .free = crypto_pcbc_free,
  278. .module = THIS_MODULE,
  279. };
  280. static int __init crypto_pcbc_module_init(void)
  281. {
  282. return crypto_register_template(&crypto_pcbc_tmpl);
  283. }
  284. static void __exit crypto_pcbc_module_exit(void)
  285. {
  286. crypto_unregister_template(&crypto_pcbc_tmpl);
  287. }
  288. module_init(crypto_pcbc_module_init);
  289. module_exit(crypto_pcbc_module_exit);
  290. MODULE_LICENSE("GPL");
  291. MODULE_DESCRIPTION("PCBC block cipher algorithm");