cfb.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CFB: Cipher FeedBack mode
  4. *
  5. * Copyright (c) 2018 James.Bottomley@HansenPartnership.com
  6. *
  7. * CFB is a stream cipher mode which is layered on to a block
  8. * encryption scheme. It works very much like a one time pad where
  9. * the pad is generated initially from the encrypted IV and then
  10. * subsequently from the encrypted previous block of ciphertext. The
  11. * pad is XOR'd into the plain text to get the final ciphertext.
  12. *
  13. * The scheme of CFB is best described by wikipedia:
  14. *
  15. * https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CFB
  16. *
  17. * Note that since the pad for both encryption and decryption is
  18. * generated by an encryption operation, CFB never uses the block
  19. * decryption function.
  20. */
  21. #include <crypto/algapi.h>
  22. #include <crypto/internal/skcipher.h>
  23. #include <linux/err.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/string.h>
  29. #include <linux/types.h>
  30. struct crypto_cfb_ctx {
  31. struct crypto_cipher *child;
  32. };
  33. static unsigned int crypto_cfb_bsize(struct crypto_skcipher *tfm)
  34. {
  35. struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
  36. struct crypto_cipher *child = ctx->child;
  37. return crypto_cipher_blocksize(child);
  38. }
  39. static void crypto_cfb_encrypt_one(struct crypto_skcipher *tfm,
  40. const u8 *src, u8 *dst)
  41. {
  42. struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
  43. crypto_cipher_encrypt_one(ctx->child, dst, src);
  44. }
  45. /* final encrypt and decrypt is the same */
  46. static void crypto_cfb_final(struct skcipher_walk *walk,
  47. struct crypto_skcipher *tfm)
  48. {
  49. const unsigned long alignmask = crypto_skcipher_alignmask(tfm);
  50. u8 tmp[MAX_CIPHER_BLOCKSIZE + MAX_CIPHER_ALIGNMASK];
  51. u8 *stream = PTR_ALIGN(tmp + 0, alignmask + 1);
  52. u8 *src = walk->src.virt.addr;
  53. u8 *dst = walk->dst.virt.addr;
  54. u8 *iv = walk->iv;
  55. unsigned int nbytes = walk->nbytes;
  56. crypto_cfb_encrypt_one(tfm, iv, stream);
  57. crypto_xor_cpy(dst, stream, src, nbytes);
  58. }
  59. static int crypto_cfb_encrypt_segment(struct skcipher_walk *walk,
  60. struct crypto_skcipher *tfm)
  61. {
  62. const unsigned int bsize = crypto_cfb_bsize(tfm);
  63. unsigned int nbytes = walk->nbytes;
  64. u8 *src = walk->src.virt.addr;
  65. u8 *dst = walk->dst.virt.addr;
  66. u8 *iv = walk->iv;
  67. do {
  68. crypto_cfb_encrypt_one(tfm, iv, dst);
  69. crypto_xor(dst, src, bsize);
  70. memcpy(iv, dst, bsize);
  71. src += bsize;
  72. dst += bsize;
  73. } while ((nbytes -= bsize) >= bsize);
  74. return nbytes;
  75. }
  76. static int crypto_cfb_encrypt_inplace(struct skcipher_walk *walk,
  77. struct crypto_skcipher *tfm)
  78. {
  79. const unsigned int bsize = crypto_cfb_bsize(tfm);
  80. unsigned int nbytes = walk->nbytes;
  81. u8 *src = walk->src.virt.addr;
  82. u8 *iv = walk->iv;
  83. u8 tmp[MAX_CIPHER_BLOCKSIZE];
  84. do {
  85. crypto_cfb_encrypt_one(tfm, iv, tmp);
  86. crypto_xor(src, tmp, bsize);
  87. iv = src;
  88. src += bsize;
  89. } while ((nbytes -= bsize) >= bsize);
  90. memcpy(walk->iv, iv, bsize);
  91. return nbytes;
  92. }
  93. static int crypto_cfb_encrypt(struct skcipher_request *req)
  94. {
  95. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  96. struct skcipher_walk walk;
  97. unsigned int bsize = crypto_cfb_bsize(tfm);
  98. int err;
  99. err = skcipher_walk_virt(&walk, req, false);
  100. while (walk.nbytes >= bsize) {
  101. if (walk.src.virt.addr == walk.dst.virt.addr)
  102. err = crypto_cfb_encrypt_inplace(&walk, tfm);
  103. else
  104. err = crypto_cfb_encrypt_segment(&walk, tfm);
  105. err = skcipher_walk_done(&walk, err);
  106. }
  107. if (walk.nbytes) {
  108. crypto_cfb_final(&walk, tfm);
  109. err = skcipher_walk_done(&walk, 0);
  110. }
  111. return err;
  112. }
  113. static int crypto_cfb_decrypt_segment(struct skcipher_walk *walk,
  114. struct crypto_skcipher *tfm)
  115. {
  116. const unsigned int bsize = crypto_cfb_bsize(tfm);
  117. unsigned int nbytes = walk->nbytes;
  118. u8 *src = walk->src.virt.addr;
  119. u8 *dst = walk->dst.virt.addr;
  120. u8 *iv = walk->iv;
  121. do {
  122. crypto_cfb_encrypt_one(tfm, iv, dst);
  123. crypto_xor(dst, iv, bsize);
  124. iv = src;
  125. src += bsize;
  126. dst += bsize;
  127. } while ((nbytes -= bsize) >= bsize);
  128. memcpy(walk->iv, iv, bsize);
  129. return nbytes;
  130. }
  131. static int crypto_cfb_decrypt_inplace(struct skcipher_walk *walk,
  132. struct crypto_skcipher *tfm)
  133. {
  134. const unsigned int bsize = crypto_cfb_bsize(tfm);
  135. unsigned int nbytes = walk->nbytes;
  136. u8 *src = walk->src.virt.addr;
  137. u8 *iv = walk->iv;
  138. u8 tmp[MAX_CIPHER_BLOCKSIZE];
  139. do {
  140. crypto_cfb_encrypt_one(tfm, iv, tmp);
  141. memcpy(iv, src, bsize);
  142. crypto_xor(src, tmp, bsize);
  143. src += bsize;
  144. } while ((nbytes -= bsize) >= bsize);
  145. memcpy(walk->iv, iv, bsize);
  146. return nbytes;
  147. }
  148. static int crypto_cfb_decrypt_blocks(struct skcipher_walk *walk,
  149. struct crypto_skcipher *tfm)
  150. {
  151. if (walk->src.virt.addr == walk->dst.virt.addr)
  152. return crypto_cfb_decrypt_inplace(walk, tfm);
  153. else
  154. return crypto_cfb_decrypt_segment(walk, tfm);
  155. }
  156. static int crypto_cfb_setkey(struct crypto_skcipher *parent, const u8 *key,
  157. unsigned int keylen)
  158. {
  159. struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(parent);
  160. struct crypto_cipher *child = ctx->child;
  161. int err;
  162. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  163. crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  164. CRYPTO_TFM_REQ_MASK);
  165. err = crypto_cipher_setkey(child, key, keylen);
  166. crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
  167. CRYPTO_TFM_RES_MASK);
  168. return err;
  169. }
  170. static int crypto_cfb_decrypt(struct skcipher_request *req)
  171. {
  172. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  173. struct skcipher_walk walk;
  174. const unsigned int bsize = crypto_cfb_bsize(tfm);
  175. int err;
  176. err = skcipher_walk_virt(&walk, req, false);
  177. while (walk.nbytes >= bsize) {
  178. err = crypto_cfb_decrypt_blocks(&walk, tfm);
  179. err = skcipher_walk_done(&walk, err);
  180. }
  181. if (walk.nbytes) {
  182. crypto_cfb_final(&walk, tfm);
  183. err = skcipher_walk_done(&walk, 0);
  184. }
  185. return err;
  186. }
  187. static int crypto_cfb_init_tfm(struct crypto_skcipher *tfm)
  188. {
  189. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  190. struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
  191. struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
  192. struct crypto_cipher *cipher;
  193. cipher = crypto_spawn_cipher(spawn);
  194. if (IS_ERR(cipher))
  195. return PTR_ERR(cipher);
  196. ctx->child = cipher;
  197. return 0;
  198. }
  199. static void crypto_cfb_exit_tfm(struct crypto_skcipher *tfm)
  200. {
  201. struct crypto_cfb_ctx *ctx = crypto_skcipher_ctx(tfm);
  202. crypto_free_cipher(ctx->child);
  203. }
  204. static void crypto_cfb_free(struct skcipher_instance *inst)
  205. {
  206. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  207. kfree(inst);
  208. }
  209. static int crypto_cfb_create(struct crypto_template *tmpl, struct rtattr **tb)
  210. {
  211. struct skcipher_instance *inst;
  212. struct crypto_attr_type *algt;
  213. struct crypto_spawn *spawn;
  214. struct crypto_alg *alg;
  215. u32 mask;
  216. int err;
  217. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER);
  218. if (err)
  219. return err;
  220. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  221. if (!inst)
  222. return -ENOMEM;
  223. algt = crypto_get_attr_type(tb);
  224. err = PTR_ERR(algt);
  225. if (IS_ERR(algt))
  226. goto err_free_inst;
  227. mask = CRYPTO_ALG_TYPE_MASK |
  228. crypto_requires_off(algt->type, algt->mask,
  229. CRYPTO_ALG_NEED_FALLBACK);
  230. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask);
  231. err = PTR_ERR(alg);
  232. if (IS_ERR(alg))
  233. goto err_free_inst;
  234. spawn = skcipher_instance_ctx(inst);
  235. err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
  236. CRYPTO_ALG_TYPE_MASK);
  237. if (err)
  238. goto err_put_alg;
  239. err = crypto_inst_setname(skcipher_crypto_instance(inst), "cfb", alg);
  240. if (err)
  241. goto err_drop_spawn;
  242. inst->alg.base.cra_priority = alg->cra_priority;
  243. /* we're a stream cipher independend of the crypto cra_blocksize */
  244. inst->alg.base.cra_blocksize = 1;
  245. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  246. inst->alg.ivsize = alg->cra_blocksize;
  247. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  248. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  249. inst->alg.base.cra_ctxsize = sizeof(struct crypto_cfb_ctx);
  250. inst->alg.init = crypto_cfb_init_tfm;
  251. inst->alg.exit = crypto_cfb_exit_tfm;
  252. inst->alg.setkey = crypto_cfb_setkey;
  253. inst->alg.encrypt = crypto_cfb_encrypt;
  254. inst->alg.decrypt = crypto_cfb_decrypt;
  255. inst->free = crypto_cfb_free;
  256. err = skcipher_register_instance(tmpl, inst);
  257. if (err)
  258. goto err_drop_spawn;
  259. crypto_mod_put(alg);
  260. out:
  261. return err;
  262. err_drop_spawn:
  263. crypto_drop_spawn(spawn);
  264. err_put_alg:
  265. crypto_mod_put(alg);
  266. err_free_inst:
  267. kfree(inst);
  268. goto out;
  269. }
  270. static struct crypto_template crypto_cfb_tmpl = {
  271. .name = "cfb",
  272. .create = crypto_cfb_create,
  273. .module = THIS_MODULE,
  274. };
  275. static int __init crypto_cfb_module_init(void)
  276. {
  277. return crypto_register_template(&crypto_cfb_tmpl);
  278. }
  279. static void __exit crypto_cfb_module_exit(void)
  280. {
  281. crypto_unregister_template(&crypto_cfb_tmpl);
  282. }
  283. module_init(crypto_cfb_module_init);
  284. module_exit(crypto_cfb_module_exit);
  285. MODULE_LICENSE("GPL");
  286. MODULE_DESCRIPTION("CFB block cipher algorithm");
  287. MODULE_ALIAS_CRYPTO("cfb");