ecb.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/internal/cipher.h>
  8. #include <crypto/internal/skcipher.h>
  9. #include <linux/err.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. static int crypto_ecb_crypt(struct crypto_cipher *cipher, const u8 *src,
  15. u8 *dst, unsigned nbytes, bool final,
  16. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  17. {
  18. const unsigned int bsize = crypto_cipher_blocksize(cipher);
  19. while (nbytes >= bsize) {
  20. fn(crypto_cipher_tfm(cipher), dst, src);
  21. src += bsize;
  22. dst += bsize;
  23. nbytes -= bsize;
  24. }
  25. return nbytes && final ? -EINVAL : nbytes;
  26. }
  27. static int crypto_ecb_encrypt2(struct crypto_lskcipher *tfm, const u8 *src,
  28. u8 *dst, unsigned len, u8 *iv, u32 flags)
  29. {
  30. struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm);
  31. struct crypto_cipher *cipher = *ctx;
  32. return crypto_ecb_crypt(cipher, src, dst, len,
  33. flags & CRYPTO_LSKCIPHER_FLAG_FINAL,
  34. crypto_cipher_alg(cipher)->cia_encrypt);
  35. }
  36. static int crypto_ecb_decrypt2(struct crypto_lskcipher *tfm, const u8 *src,
  37. u8 *dst, unsigned len, u8 *iv, u32 flags)
  38. {
  39. struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm);
  40. struct crypto_cipher *cipher = *ctx;
  41. return crypto_ecb_crypt(cipher, src, dst, len,
  42. flags & CRYPTO_LSKCIPHER_FLAG_FINAL,
  43. crypto_cipher_alg(cipher)->cia_decrypt);
  44. }
  45. static int lskcipher_setkey_simple2(struct crypto_lskcipher *tfm,
  46. const u8 *key, unsigned int keylen)
  47. {
  48. struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm);
  49. struct crypto_cipher *cipher = *ctx;
  50. crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
  51. crypto_cipher_set_flags(cipher, crypto_lskcipher_get_flags(tfm) &
  52. CRYPTO_TFM_REQ_MASK);
  53. return crypto_cipher_setkey(cipher, key, keylen);
  54. }
  55. static int lskcipher_init_tfm_simple2(struct crypto_lskcipher *tfm)
  56. {
  57. struct lskcipher_instance *inst = lskcipher_alg_instance(tfm);
  58. struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm);
  59. struct crypto_cipher_spawn *spawn;
  60. struct crypto_cipher *cipher;
  61. spawn = lskcipher_instance_ctx(inst);
  62. cipher = crypto_spawn_cipher(spawn);
  63. if (IS_ERR(cipher))
  64. return PTR_ERR(cipher);
  65. *ctx = cipher;
  66. return 0;
  67. }
  68. static void lskcipher_exit_tfm_simple2(struct crypto_lskcipher *tfm)
  69. {
  70. struct crypto_cipher **ctx = crypto_lskcipher_ctx(tfm);
  71. crypto_free_cipher(*ctx);
  72. }
  73. static void lskcipher_free_instance_simple2(struct lskcipher_instance *inst)
  74. {
  75. crypto_drop_cipher(lskcipher_instance_ctx(inst));
  76. kfree(inst);
  77. }
  78. static struct lskcipher_instance *lskcipher_alloc_instance_simple2(
  79. struct crypto_template *tmpl, struct rtattr **tb)
  80. {
  81. struct crypto_cipher_spawn *spawn;
  82. struct lskcipher_instance *inst;
  83. struct crypto_alg *cipher_alg;
  84. u32 mask;
  85. int err;
  86. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_LSKCIPHER, &mask);
  87. if (err)
  88. return ERR_PTR(err);
  89. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  90. if (!inst)
  91. return ERR_PTR(-ENOMEM);
  92. spawn = lskcipher_instance_ctx(inst);
  93. err = crypto_grab_cipher(spawn, lskcipher_crypto_instance(inst),
  94. crypto_attr_alg_name(tb[1]), 0, mask);
  95. if (err)
  96. goto err_free_inst;
  97. cipher_alg = crypto_spawn_cipher_alg(spawn);
  98. err = crypto_inst_setname(lskcipher_crypto_instance(inst), tmpl->name,
  99. cipher_alg);
  100. if (err)
  101. goto err_free_inst;
  102. inst->free = lskcipher_free_instance_simple2;
  103. /* Default algorithm properties, can be overridden */
  104. inst->alg.co.base.cra_blocksize = cipher_alg->cra_blocksize;
  105. inst->alg.co.base.cra_alignmask = cipher_alg->cra_alignmask;
  106. inst->alg.co.base.cra_priority = cipher_alg->cra_priority;
  107. inst->alg.co.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
  108. inst->alg.co.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
  109. inst->alg.co.ivsize = cipher_alg->cra_blocksize;
  110. /* Use struct crypto_cipher * by default, can be overridden */
  111. inst->alg.co.base.cra_ctxsize = sizeof(struct crypto_cipher *);
  112. inst->alg.setkey = lskcipher_setkey_simple2;
  113. inst->alg.init = lskcipher_init_tfm_simple2;
  114. inst->alg.exit = lskcipher_exit_tfm_simple2;
  115. return inst;
  116. err_free_inst:
  117. lskcipher_free_instance_simple2(inst);
  118. return ERR_PTR(err);
  119. }
  120. static int crypto_ecb_create2(struct crypto_template *tmpl, struct rtattr **tb)
  121. {
  122. struct lskcipher_instance *inst;
  123. int err;
  124. inst = lskcipher_alloc_instance_simple2(tmpl, tb);
  125. if (IS_ERR(inst))
  126. return PTR_ERR(inst);
  127. /* ECB mode doesn't take an IV */
  128. inst->alg.co.ivsize = 0;
  129. inst->alg.encrypt = crypto_ecb_encrypt2;
  130. inst->alg.decrypt = crypto_ecb_decrypt2;
  131. err = lskcipher_register_instance(tmpl, inst);
  132. if (err)
  133. inst->free(inst);
  134. return err;
  135. }
  136. static int crypto_ecb_create(struct crypto_template *tmpl, struct rtattr **tb)
  137. {
  138. struct crypto_lskcipher_spawn *spawn;
  139. struct lskcipher_alg *cipher_alg;
  140. struct lskcipher_instance *inst;
  141. int err;
  142. inst = lskcipher_alloc_instance_simple(tmpl, tb);
  143. if (IS_ERR(inst)) {
  144. err = crypto_ecb_create2(tmpl, tb);
  145. return err;
  146. }
  147. spawn = lskcipher_instance_ctx(inst);
  148. cipher_alg = crypto_lskcipher_spawn_alg(spawn);
  149. /* ECB mode doesn't take an IV */
  150. inst->alg.co.ivsize = 0;
  151. if (cipher_alg->co.ivsize)
  152. return -EINVAL;
  153. inst->alg.co.base.cra_ctxsize = cipher_alg->co.base.cra_ctxsize;
  154. inst->alg.setkey = cipher_alg->setkey;
  155. inst->alg.encrypt = cipher_alg->encrypt;
  156. inst->alg.decrypt = cipher_alg->decrypt;
  157. inst->alg.init = cipher_alg->init;
  158. inst->alg.exit = cipher_alg->exit;
  159. err = lskcipher_register_instance(tmpl, inst);
  160. if (err)
  161. inst->free(inst);
  162. return err;
  163. }
  164. static struct crypto_template crypto_ecb_tmpl = {
  165. .name = "ecb",
  166. .create = crypto_ecb_create,
  167. .module = THIS_MODULE,
  168. };
  169. static int __init crypto_ecb_module_init(void)
  170. {
  171. return crypto_register_template(&crypto_ecb_tmpl);
  172. }
  173. static void __exit crypto_ecb_module_exit(void)
  174. {
  175. crypto_unregister_template(&crypto_ecb_tmpl);
  176. }
  177. subsys_initcall(crypto_ecb_module_init);
  178. module_exit(crypto_ecb_module_exit);
  179. MODULE_LICENSE("GPL");
  180. MODULE_DESCRIPTION("ECB block cipher mode of operation");
  181. MODULE_ALIAS_CRYPTO("ecb");
  182. MODULE_IMPORT_NS(CRYPTO_INTERNAL);