cbc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * CBC: Cipher Block Chaining mode
  3. *
  4. * Copyright (c) 2006-2016 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 <crypto/cbc.h>
  14. #include <crypto/internal/skcipher.h>
  15. #include <linux/err.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/log2.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. struct crypto_cbc_ctx {
  22. struct crypto_cipher *child;
  23. };
  24. static int crypto_cbc_setkey(struct crypto_skcipher *parent, const u8 *key,
  25. unsigned int keylen)
  26. {
  27. struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(parent);
  28. struct crypto_cipher *child = ctx->child;
  29. int err;
  30. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  31. crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  32. CRYPTO_TFM_REQ_MASK);
  33. err = crypto_cipher_setkey(child, key, keylen);
  34. crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
  35. CRYPTO_TFM_RES_MASK);
  36. return err;
  37. }
  38. static inline void crypto_cbc_encrypt_one(struct crypto_skcipher *tfm,
  39. const u8 *src, u8 *dst)
  40. {
  41. struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  42. crypto_cipher_encrypt_one(ctx->child, dst, src);
  43. }
  44. static int crypto_cbc_encrypt(struct skcipher_request *req)
  45. {
  46. return crypto_cbc_encrypt_walk(req, crypto_cbc_encrypt_one);
  47. }
  48. static inline void crypto_cbc_decrypt_one(struct crypto_skcipher *tfm,
  49. const u8 *src, u8 *dst)
  50. {
  51. struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  52. crypto_cipher_decrypt_one(ctx->child, dst, src);
  53. }
  54. static int crypto_cbc_decrypt(struct skcipher_request *req)
  55. {
  56. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  57. struct skcipher_walk walk;
  58. int err;
  59. err = skcipher_walk_virt(&walk, req, false);
  60. while (walk.nbytes) {
  61. err = crypto_cbc_decrypt_blocks(&walk, tfm,
  62. crypto_cbc_decrypt_one);
  63. err = skcipher_walk_done(&walk, err);
  64. }
  65. return err;
  66. }
  67. static int crypto_cbc_init_tfm(struct crypto_skcipher *tfm)
  68. {
  69. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  70. struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
  71. struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  72. struct crypto_cipher *cipher;
  73. cipher = crypto_spawn_cipher(spawn);
  74. if (IS_ERR(cipher))
  75. return PTR_ERR(cipher);
  76. ctx->child = cipher;
  77. return 0;
  78. }
  79. static void crypto_cbc_exit_tfm(struct crypto_skcipher *tfm)
  80. {
  81. struct crypto_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
  82. crypto_free_cipher(ctx->child);
  83. }
  84. static void crypto_cbc_free(struct skcipher_instance *inst)
  85. {
  86. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  87. kfree(inst);
  88. }
  89. static int crypto_cbc_create(struct crypto_template *tmpl, struct rtattr **tb)
  90. {
  91. struct skcipher_instance *inst;
  92. struct crypto_attr_type *algt;
  93. struct crypto_spawn *spawn;
  94. struct crypto_alg *alg;
  95. u32 mask;
  96. int err;
  97. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER);
  98. if (err)
  99. return err;
  100. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  101. if (!inst)
  102. return -ENOMEM;
  103. algt = crypto_get_attr_type(tb);
  104. err = PTR_ERR(algt);
  105. if (IS_ERR(algt))
  106. goto err_free_inst;
  107. mask = CRYPTO_ALG_TYPE_MASK |
  108. crypto_requires_off(algt->type, algt->mask,
  109. CRYPTO_ALG_NEED_FALLBACK);
  110. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask);
  111. err = PTR_ERR(alg);
  112. if (IS_ERR(alg))
  113. goto err_free_inst;
  114. spawn = skcipher_instance_ctx(inst);
  115. err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
  116. CRYPTO_ALG_TYPE_MASK);
  117. if (err)
  118. goto err_put_alg;
  119. err = crypto_inst_setname(skcipher_crypto_instance(inst), "cbc", alg);
  120. if (err)
  121. goto err_drop_spawn;
  122. err = -EINVAL;
  123. if (!is_power_of_2(alg->cra_blocksize))
  124. goto err_drop_spawn;
  125. inst->alg.base.cra_priority = alg->cra_priority;
  126. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  127. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  128. inst->alg.ivsize = alg->cra_blocksize;
  129. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  130. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  131. inst->alg.base.cra_ctxsize = sizeof(struct crypto_cbc_ctx);
  132. inst->alg.init = crypto_cbc_init_tfm;
  133. inst->alg.exit = crypto_cbc_exit_tfm;
  134. inst->alg.setkey = crypto_cbc_setkey;
  135. inst->alg.encrypt = crypto_cbc_encrypt;
  136. inst->alg.decrypt = crypto_cbc_decrypt;
  137. inst->free = crypto_cbc_free;
  138. err = skcipher_register_instance(tmpl, inst);
  139. if (err)
  140. goto err_drop_spawn;
  141. crypto_mod_put(alg);
  142. out:
  143. return err;
  144. err_drop_spawn:
  145. crypto_drop_spawn(spawn);
  146. err_put_alg:
  147. crypto_mod_put(alg);
  148. err_free_inst:
  149. kfree(inst);
  150. goto out;
  151. }
  152. static struct crypto_template crypto_cbc_tmpl = {
  153. .name = "cbc",
  154. .create = crypto_cbc_create,
  155. .module = THIS_MODULE,
  156. };
  157. static int __init crypto_cbc_module_init(void)
  158. {
  159. return crypto_register_template(&crypto_cbc_tmpl);
  160. }
  161. static void __exit crypto_cbc_module_exit(void)
  162. {
  163. crypto_unregister_template(&crypto_cbc_tmpl);
  164. }
  165. module_init(crypto_cbc_module_init);
  166. module_exit(crypto_cbc_module_exit);
  167. MODULE_LICENSE("GPL");
  168. MODULE_DESCRIPTION("CBC block cipher algorithm");
  169. MODULE_ALIAS_CRYPTO("cbc");