ofb.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OFB: Output FeedBack mode
  4. *
  5. * Copyright (C) 2018 ARM Limited or its affiliates.
  6. * All rights reserved.
  7. */
  8. #include <crypto/algapi.h>
  9. #include <crypto/internal/cipher.h>
  10. #include <crypto/internal/skcipher.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. static int crypto_ofb_crypt(struct skcipher_request *req)
  16. {
  17. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  18. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  19. const unsigned int bsize = crypto_cipher_blocksize(cipher);
  20. struct skcipher_walk walk;
  21. int err;
  22. err = skcipher_walk_virt(&walk, req, false);
  23. while (walk.nbytes >= bsize) {
  24. const u8 *src = walk.src.virt.addr;
  25. u8 *dst = walk.dst.virt.addr;
  26. u8 * const iv = walk.iv;
  27. unsigned int nbytes = walk.nbytes;
  28. do {
  29. crypto_cipher_encrypt_one(cipher, iv, iv);
  30. crypto_xor_cpy(dst, src, iv, bsize);
  31. dst += bsize;
  32. src += bsize;
  33. } while ((nbytes -= bsize) >= bsize);
  34. err = skcipher_walk_done(&walk, nbytes);
  35. }
  36. if (walk.nbytes) {
  37. crypto_cipher_encrypt_one(cipher, walk.iv, walk.iv);
  38. crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, walk.iv,
  39. walk.nbytes);
  40. err = skcipher_walk_done(&walk, 0);
  41. }
  42. return err;
  43. }
  44. static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
  45. {
  46. struct skcipher_instance *inst;
  47. struct crypto_alg *alg;
  48. int err;
  49. inst = skcipher_alloc_instance_simple(tmpl, tb);
  50. if (IS_ERR(inst))
  51. return PTR_ERR(inst);
  52. alg = skcipher_ialg_simple(inst);
  53. /* OFB mode is a stream cipher. */
  54. inst->alg.base.cra_blocksize = 1;
  55. /*
  56. * To simplify the implementation, configure the skcipher walk to only
  57. * give a partial block at the very end, never earlier.
  58. */
  59. inst->alg.chunksize = alg->cra_blocksize;
  60. inst->alg.encrypt = crypto_ofb_crypt;
  61. inst->alg.decrypt = crypto_ofb_crypt;
  62. err = skcipher_register_instance(tmpl, inst);
  63. if (err)
  64. inst->free(inst);
  65. return err;
  66. }
  67. static struct crypto_template crypto_ofb_tmpl = {
  68. .name = "ofb",
  69. .create = crypto_ofb_create,
  70. .module = THIS_MODULE,
  71. };
  72. static int __init crypto_ofb_module_init(void)
  73. {
  74. return crypto_register_template(&crypto_ofb_tmpl);
  75. }
  76. static void __exit crypto_ofb_module_exit(void)
  77. {
  78. crypto_unregister_template(&crypto_ofb_tmpl);
  79. }
  80. subsys_initcall(crypto_ofb_module_init);
  81. module_exit(crypto_ofb_module_exit);
  82. MODULE_LICENSE("GPL");
  83. MODULE_DESCRIPTION("OFB block cipher mode of operation");
  84. MODULE_ALIAS_CRYPTO("ofb");
  85. MODULE_IMPORT_NS(CRYPTO_INTERNAL);