ecb.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * ECB: Electronic CodeBook mode
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <crypto/algapi.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/slab.h>
  19. struct crypto_ecb_ctx {
  20. struct crypto_cipher *child;
  21. };
  22. static int crypto_ecb_setkey(struct crypto_tfm *parent, const u8 *key,
  23. unsigned int keylen)
  24. {
  25. struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(parent);
  26. struct crypto_cipher *child = ctx->child;
  27. int err;
  28. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  29. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  30. CRYPTO_TFM_REQ_MASK);
  31. err = crypto_cipher_setkey(child, key, keylen);
  32. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  33. CRYPTO_TFM_RES_MASK);
  34. return err;
  35. }
  36. static int crypto_ecb_crypt(struct blkcipher_desc *desc,
  37. struct blkcipher_walk *walk,
  38. struct crypto_cipher *tfm,
  39. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  40. {
  41. int bsize = crypto_cipher_blocksize(tfm);
  42. unsigned int nbytes;
  43. int err;
  44. err = blkcipher_walk_virt(desc, walk);
  45. while ((nbytes = walk->nbytes)) {
  46. u8 *wsrc = walk->src.virt.addr;
  47. u8 *wdst = walk->dst.virt.addr;
  48. do {
  49. fn(crypto_cipher_tfm(tfm), wdst, wsrc);
  50. wsrc += bsize;
  51. wdst += bsize;
  52. } while ((nbytes -= bsize) >= bsize);
  53. err = blkcipher_walk_done(desc, walk, nbytes);
  54. }
  55. return err;
  56. }
  57. static int crypto_ecb_encrypt(struct blkcipher_desc *desc,
  58. struct scatterlist *dst, struct scatterlist *src,
  59. unsigned int nbytes)
  60. {
  61. struct blkcipher_walk walk;
  62. struct crypto_blkcipher *tfm = desc->tfm;
  63. struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm);
  64. struct crypto_cipher *child = ctx->child;
  65. blkcipher_walk_init(&walk, dst, src, nbytes);
  66. return crypto_ecb_crypt(desc, &walk, child,
  67. crypto_cipher_alg(child)->cia_encrypt);
  68. }
  69. static int crypto_ecb_decrypt(struct blkcipher_desc *desc,
  70. struct scatterlist *dst, struct scatterlist *src,
  71. unsigned int nbytes)
  72. {
  73. struct blkcipher_walk walk;
  74. struct crypto_blkcipher *tfm = desc->tfm;
  75. struct crypto_ecb_ctx *ctx = crypto_blkcipher_ctx(tfm);
  76. struct crypto_cipher *child = ctx->child;
  77. blkcipher_walk_init(&walk, dst, src, nbytes);
  78. return crypto_ecb_crypt(desc, &walk, child,
  79. crypto_cipher_alg(child)->cia_decrypt);
  80. }
  81. static int crypto_ecb_init_tfm(struct crypto_tfm *tfm)
  82. {
  83. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  84. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  85. struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm);
  86. struct crypto_cipher *cipher;
  87. cipher = crypto_spawn_cipher(spawn);
  88. if (IS_ERR(cipher))
  89. return PTR_ERR(cipher);
  90. ctx->child = cipher;
  91. return 0;
  92. }
  93. static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm)
  94. {
  95. struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm);
  96. crypto_free_cipher(ctx->child);
  97. }
  98. static struct crypto_instance *crypto_ecb_alloc(struct rtattr **tb)
  99. {
  100. struct crypto_instance *inst;
  101. struct crypto_alg *alg;
  102. int err;
  103. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  104. if (err)
  105. return ERR_PTR(err);
  106. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  107. CRYPTO_ALG_TYPE_MASK);
  108. if (IS_ERR(alg))
  109. return ERR_CAST(alg);
  110. inst = crypto_alloc_instance("ecb", alg);
  111. if (IS_ERR(inst))
  112. goto out_put_alg;
  113. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  114. inst->alg.cra_priority = alg->cra_priority;
  115. inst->alg.cra_blocksize = alg->cra_blocksize;
  116. inst->alg.cra_alignmask = alg->cra_alignmask;
  117. inst->alg.cra_type = &crypto_blkcipher_type;
  118. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  119. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  120. inst->alg.cra_ctxsize = sizeof(struct crypto_ecb_ctx);
  121. inst->alg.cra_init = crypto_ecb_init_tfm;
  122. inst->alg.cra_exit = crypto_ecb_exit_tfm;
  123. inst->alg.cra_blkcipher.setkey = crypto_ecb_setkey;
  124. inst->alg.cra_blkcipher.encrypt = crypto_ecb_encrypt;
  125. inst->alg.cra_blkcipher.decrypt = crypto_ecb_decrypt;
  126. out_put_alg:
  127. crypto_mod_put(alg);
  128. return inst;
  129. }
  130. static void crypto_ecb_free(struct crypto_instance *inst)
  131. {
  132. crypto_drop_spawn(crypto_instance_ctx(inst));
  133. kfree(inst);
  134. }
  135. static struct crypto_template crypto_ecb_tmpl = {
  136. .name = "ecb",
  137. .alloc = crypto_ecb_alloc,
  138. .free = crypto_ecb_free,
  139. .module = THIS_MODULE,
  140. };
  141. static int __init crypto_ecb_module_init(void)
  142. {
  143. return crypto_register_template(&crypto_ecb_tmpl);
  144. }
  145. static void __exit crypto_ecb_module_exit(void)
  146. {
  147. crypto_unregister_template(&crypto_ecb_tmpl);
  148. }
  149. module_init(crypto_ecb_module_init);
  150. module_exit(crypto_ecb_module_exit);
  151. MODULE_LICENSE("GPL");
  152. MODULE_DESCRIPTION("ECB block cipher algorithm");