cfb.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. iv = dst;
  71. src += bsize;
  72. dst += bsize;
  73. } while ((nbytes -= bsize) >= bsize);
  74. memcpy(walk->iv, iv, bsize);
  75. return nbytes;
  76. }
  77. static int crypto_cfb_encrypt_inplace(struct skcipher_walk *walk,
  78. struct crypto_skcipher *tfm)
  79. {
  80. const unsigned int bsize = crypto_cfb_bsize(tfm);
  81. unsigned int nbytes = walk->nbytes;
  82. u8 *src = walk->src.virt.addr;
  83. u8 *iv = walk->iv;
  84. u8 tmp[MAX_CIPHER_BLOCKSIZE];
  85. do {
  86. crypto_cfb_encrypt_one(tfm, iv, tmp);
  87. crypto_xor(src, tmp, bsize);
  88. iv = src;
  89. src += bsize;
  90. } while ((nbytes -= bsize) >= bsize);
  91. memcpy(walk->iv, iv, bsize);
  92. return nbytes;
  93. }
  94. static int crypto_cfb_encrypt(struct skcipher_request *req)
  95. {
  96. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  97. struct skcipher_walk walk;
  98. unsigned int bsize = crypto_cfb_bsize(tfm);
  99. int err;
  100. err = skcipher_walk_virt(&walk, req, false);
  101. while (walk.nbytes >= bsize) {
  102. if (walk.src.virt.addr == walk.dst.virt.addr)
  103. err = crypto_cfb_encrypt_inplace(&walk, tfm);
  104. else
  105. err = crypto_cfb_encrypt_segment(&walk, tfm);
  106. err = skcipher_walk_done(&walk, err);
  107. }
  108. if (walk.nbytes) {
  109. crypto_cfb_final(&walk, tfm);
  110. err = skcipher_walk_done(&walk, 0);
  111. }
  112. return err;
  113. }
  114. static int crypto_cfb_decrypt_segment(struct skcipher_walk *walk,
  115. struct crypto_skcipher *tfm)
  116. {
  117. const unsigned int bsize = crypto_cfb_bsize(tfm);
  118. unsigned int nbytes = walk->nbytes;
  119. u8 *src = walk->src.virt.addr;
  120. u8 *dst = walk->dst.virt.addr;
  121. u8 *iv = walk->iv;
  122. do {
  123. crypto_cfb_encrypt_one(tfm, iv, dst);
  124. crypto_xor(dst, src, bsize);
  125. iv = src;
  126. src += bsize;
  127. dst += bsize;
  128. } while ((nbytes -= bsize) >= bsize);
  129. memcpy(walk->iv, iv, bsize);
  130. return nbytes;
  131. }
  132. static int crypto_cfb_decrypt_inplace(struct skcipher_walk *walk,
  133. struct crypto_skcipher *tfm)
  134. {
  135. const unsigned int bsize = crypto_cfb_bsize(tfm);
  136. unsigned int nbytes = walk->nbytes;
  137. u8 *src = walk->src.virt.addr;
  138. u8 * const iv = walk->iv;
  139. u8 tmp[MAX_CIPHER_BLOCKSIZE];
  140. do {
  141. crypto_cfb_encrypt_one(tfm, iv, tmp);
  142. memcpy(iv, src, bsize);
  143. crypto_xor(src, tmp, bsize);
  144. src += bsize;
  145. } while ((nbytes -= bsize) >= 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. /*
  247. * To simplify the implementation, configure the skcipher walk to only
  248. * give a partial block at the very end, never earlier.
  249. */
  250. inst->alg.chunksize = alg->cra_blocksize;
  251. inst->alg.ivsize = alg->cra_blocksize;
  252. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  253. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  254. inst->alg.base.cra_ctxsize = sizeof(struct crypto_cfb_ctx);
  255. inst->alg.init = crypto_cfb_init_tfm;
  256. inst->alg.exit = crypto_cfb_exit_tfm;
  257. inst->alg.setkey = crypto_cfb_setkey;
  258. inst->alg.encrypt = crypto_cfb_encrypt;
  259. inst->alg.decrypt = crypto_cfb_decrypt;
  260. inst->free = crypto_cfb_free;
  261. err = skcipher_register_instance(tmpl, inst);
  262. if (err)
  263. goto err_drop_spawn;
  264. crypto_mod_put(alg);
  265. out:
  266. return err;
  267. err_drop_spawn:
  268. crypto_drop_spawn(spawn);
  269. err_put_alg:
  270. crypto_mod_put(alg);
  271. err_free_inst:
  272. kfree(inst);
  273. goto out;
  274. }
  275. static struct crypto_template crypto_cfb_tmpl = {
  276. .name = "cfb",
  277. .create = crypto_cfb_create,
  278. .module = THIS_MODULE,
  279. };
  280. static int __init crypto_cfb_module_init(void)
  281. {
  282. return crypto_register_template(&crypto_cfb_tmpl);
  283. }
  284. static void __exit crypto_cfb_module_exit(void)
  285. {
  286. crypto_unregister_template(&crypto_cfb_tmpl);
  287. }
  288. module_init(crypto_cfb_module_init);
  289. module_exit(crypto_cfb_module_exit);
  290. MODULE_LICENSE("GPL");
  291. MODULE_DESCRIPTION("CFB block cipher algorithm");
  292. MODULE_ALIAS_CRYPTO("cfb");