ecb.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. tfm = crypto_spawn_tfm(spawn);
  87. if (IS_ERR(tfm))
  88. return PTR_ERR(tfm);
  89. ctx->child = crypto_cipher_cast(tfm);
  90. return 0;
  91. }
  92. static void crypto_ecb_exit_tfm(struct crypto_tfm *tfm)
  93. {
  94. struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(tfm);
  95. crypto_free_cipher(ctx->child);
  96. }
  97. static struct crypto_instance *crypto_ecb_alloc(void *param, unsigned int len)
  98. {
  99. struct crypto_instance *inst;
  100. struct crypto_alg *alg;
  101. alg = crypto_get_attr_alg(param, len, CRYPTO_ALG_TYPE_CIPHER,
  102. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  103. if (IS_ERR(alg))
  104. return ERR_PTR(PTR_ERR(alg));
  105. inst = crypto_alloc_instance("ecb", alg);
  106. if (IS_ERR(inst))
  107. goto out_put_alg;
  108. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  109. inst->alg.cra_priority = alg->cra_priority;
  110. inst->alg.cra_blocksize = alg->cra_blocksize;
  111. inst->alg.cra_alignmask = alg->cra_alignmask;
  112. inst->alg.cra_type = &crypto_blkcipher_type;
  113. inst->alg.cra_blkcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
  114. inst->alg.cra_blkcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
  115. inst->alg.cra_ctxsize = sizeof(struct crypto_ecb_ctx);
  116. inst->alg.cra_init = crypto_ecb_init_tfm;
  117. inst->alg.cra_exit = crypto_ecb_exit_tfm;
  118. inst->alg.cra_blkcipher.setkey = crypto_ecb_setkey;
  119. inst->alg.cra_blkcipher.encrypt = crypto_ecb_encrypt;
  120. inst->alg.cra_blkcipher.decrypt = crypto_ecb_decrypt;
  121. out_put_alg:
  122. crypto_mod_put(alg);
  123. return inst;
  124. }
  125. static void crypto_ecb_free(struct crypto_instance *inst)
  126. {
  127. crypto_drop_spawn(crypto_instance_ctx(inst));
  128. kfree(inst);
  129. }
  130. static struct crypto_template crypto_ecb_tmpl = {
  131. .name = "ecb",
  132. .alloc = crypto_ecb_alloc,
  133. .free = crypto_ecb_free,
  134. .module = THIS_MODULE,
  135. };
  136. static int __init crypto_ecb_module_init(void)
  137. {
  138. return crypto_register_template(&crypto_ecb_tmpl);
  139. }
  140. static void __exit crypto_ecb_module_exit(void)
  141. {
  142. crypto_unregister_template(&crypto_ecb_tmpl);
  143. }
  144. module_init(crypto_ecb_module_init);
  145. module_exit(crypto_ecb_module_exit);
  146. MODULE_LICENSE("GPL");
  147. MODULE_DESCRIPTION("ECB block cipher algorithm");