ecb.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ECB: Electronic CodeBook mode
  4. *
  5. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #include <crypto/algapi.h>
  8. #include <crypto/internal/cipher.h>
  9. #include <crypto/internal/skcipher.h>
  10. #include <linux/err.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. static int crypto_ecb_crypt(struct skcipher_request *req,
  15. struct crypto_cipher *cipher,
  16. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  17. {
  18. const unsigned int bsize = crypto_cipher_blocksize(cipher);
  19. struct skcipher_walk walk;
  20. unsigned int nbytes;
  21. int err;
  22. err = skcipher_walk_virt(&walk, req, false);
  23. while ((nbytes = walk.nbytes) != 0) {
  24. const u8 *src = walk.src.virt.addr;
  25. u8 *dst = walk.dst.virt.addr;
  26. do {
  27. fn(crypto_cipher_tfm(cipher), dst, src);
  28. src += bsize;
  29. dst += bsize;
  30. } while ((nbytes -= bsize) >= bsize);
  31. err = skcipher_walk_done(&walk, nbytes);
  32. }
  33. return err;
  34. }
  35. static int crypto_ecb_encrypt(struct skcipher_request *req)
  36. {
  37. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  38. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  39. return crypto_ecb_crypt(req, cipher,
  40. crypto_cipher_alg(cipher)->cia_encrypt);
  41. }
  42. static int crypto_ecb_decrypt(struct skcipher_request *req)
  43. {
  44. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  45. struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
  46. return crypto_ecb_crypt(req, cipher,
  47. crypto_cipher_alg(cipher)->cia_decrypt);
  48. }
  49. static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
  50. {
  51. struct skcipher_instance *inst;
  52. int err;
  53. inst = skcipher_alloc_instance_simple(tmpl, tb);
  54. if (IS_ERR(inst))
  55. return PTR_ERR(inst);
  56. inst->alg.ivsize = 0; /* ECB mode doesn't take an IV */
  57. inst->alg.encrypt = crypto_ecb_encrypt;
  58. inst->alg.decrypt = crypto_ecb_decrypt;
  59. err = skcipher_register_instance(tmpl, inst);
  60. if (err)
  61. inst->free(inst);
  62. return err;
  63. }
  64. static struct crypto_template crypto_ecb_tmpl = {
  65. .name = "ecb",
  66. .create = crypto_ecb_create,
  67. .module = THIS_MODULE,
  68. };
  69. static int __init crypto_ecb_module_init(void)
  70. {
  71. return crypto_register_template(&crypto_ecb_tmpl);
  72. }
  73. static void __exit crypto_ecb_module_exit(void)
  74. {
  75. crypto_unregister_template(&crypto_ecb_tmpl);
  76. }
  77. subsys_initcall(crypto_ecb_module_init);
  78. module_exit(crypto_ecb_module_exit);
  79. MODULE_LICENSE("GPL");
  80. MODULE_DESCRIPTION("ECB block cipher mode of operation");
  81. MODULE_ALIAS_CRYPTO("ecb");