ecb.c 4.8 KB

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