ofb.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * OFB: Output FeedBack mode
  4. *
  5. * Copyright (C) 2018 ARM Limited or its affiliates.
  6. * All rights reserved.
  7. *
  8. * Based loosely on public domain code gleaned from libtomcrypt
  9. * (https://github.com/libtom/libtomcrypt).
  10. */
  11. #include <crypto/algapi.h>
  12. #include <crypto/internal/skcipher.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_ofb_ctx {
  20. struct crypto_cipher *child;
  21. int cnt;
  22. };
  23. static int crypto_ofb_setkey(struct crypto_skcipher *parent, const u8 *key,
  24. unsigned int keylen)
  25. {
  26. struct crypto_ofb_ctx *ctx = crypto_skcipher_ctx(parent);
  27. struct crypto_cipher *child = ctx->child;
  28. int err;
  29. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  30. crypto_cipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  31. CRYPTO_TFM_REQ_MASK);
  32. err = crypto_cipher_setkey(child, key, keylen);
  33. crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(child) &
  34. CRYPTO_TFM_RES_MASK);
  35. return err;
  36. }
  37. static int crypto_ofb_encrypt_segment(struct crypto_ofb_ctx *ctx,
  38. struct skcipher_walk *walk,
  39. struct crypto_cipher *tfm)
  40. {
  41. int bsize = crypto_cipher_blocksize(tfm);
  42. int nbytes = walk->nbytes;
  43. u8 *src = walk->src.virt.addr;
  44. u8 *dst = walk->dst.virt.addr;
  45. u8 *iv = walk->iv;
  46. do {
  47. if (ctx->cnt == bsize) {
  48. if (nbytes < bsize)
  49. break;
  50. crypto_cipher_encrypt_one(tfm, iv, iv);
  51. ctx->cnt = 0;
  52. }
  53. *dst = *src ^ iv[ctx->cnt];
  54. src++;
  55. dst++;
  56. ctx->cnt++;
  57. } while (--nbytes);
  58. return nbytes;
  59. }
  60. static int crypto_ofb_encrypt(struct skcipher_request *req)
  61. {
  62. struct skcipher_walk walk;
  63. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  64. unsigned int bsize;
  65. struct crypto_ofb_ctx *ctx = crypto_skcipher_ctx(tfm);
  66. struct crypto_cipher *child = ctx->child;
  67. int ret = 0;
  68. bsize = crypto_cipher_blocksize(child);
  69. ctx->cnt = bsize;
  70. ret = skcipher_walk_virt(&walk, req, false);
  71. while (walk.nbytes) {
  72. ret = crypto_ofb_encrypt_segment(ctx, &walk, child);
  73. ret = skcipher_walk_done(&walk, ret);
  74. }
  75. return ret;
  76. }
  77. /* OFB encrypt and decrypt are identical */
  78. static int crypto_ofb_decrypt(struct skcipher_request *req)
  79. {
  80. return crypto_ofb_encrypt(req);
  81. }
  82. static int crypto_ofb_init_tfm(struct crypto_skcipher *tfm)
  83. {
  84. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  85. struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
  86. struct crypto_ofb_ctx *ctx = crypto_skcipher_ctx(tfm);
  87. struct crypto_cipher *cipher;
  88. cipher = crypto_spawn_cipher(spawn);
  89. if (IS_ERR(cipher))
  90. return PTR_ERR(cipher);
  91. ctx->child = cipher;
  92. return 0;
  93. }
  94. static void crypto_ofb_exit_tfm(struct crypto_skcipher *tfm)
  95. {
  96. struct crypto_ofb_ctx *ctx = crypto_skcipher_ctx(tfm);
  97. crypto_free_cipher(ctx->child);
  98. }
  99. static void crypto_ofb_free(struct skcipher_instance *inst)
  100. {
  101. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  102. kfree(inst);
  103. }
  104. static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
  105. {
  106. struct skcipher_instance *inst;
  107. struct crypto_attr_type *algt;
  108. struct crypto_spawn *spawn;
  109. struct crypto_alg *alg;
  110. u32 mask;
  111. int err;
  112. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER);
  113. if (err)
  114. return err;
  115. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  116. if (!inst)
  117. return -ENOMEM;
  118. algt = crypto_get_attr_type(tb);
  119. err = PTR_ERR(algt);
  120. if (IS_ERR(algt))
  121. goto err_free_inst;
  122. mask = CRYPTO_ALG_TYPE_MASK |
  123. crypto_requires_off(algt->type, algt->mask,
  124. CRYPTO_ALG_NEED_FALLBACK);
  125. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask);
  126. err = PTR_ERR(alg);
  127. if (IS_ERR(alg))
  128. goto err_free_inst;
  129. spawn = skcipher_instance_ctx(inst);
  130. err = crypto_init_spawn(spawn, alg, skcipher_crypto_instance(inst),
  131. CRYPTO_ALG_TYPE_MASK);
  132. crypto_mod_put(alg);
  133. if (err)
  134. goto err_free_inst;
  135. err = crypto_inst_setname(skcipher_crypto_instance(inst), "ofb", alg);
  136. if (err)
  137. goto err_drop_spawn;
  138. inst->alg.base.cra_priority = alg->cra_priority;
  139. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  140. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  141. /* We access the data as u32s when xoring. */
  142. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  143. inst->alg.ivsize = alg->cra_blocksize;
  144. inst->alg.min_keysize = alg->cra_cipher.cia_min_keysize;
  145. inst->alg.max_keysize = alg->cra_cipher.cia_max_keysize;
  146. inst->alg.base.cra_ctxsize = sizeof(struct crypto_ofb_ctx);
  147. inst->alg.init = crypto_ofb_init_tfm;
  148. inst->alg.exit = crypto_ofb_exit_tfm;
  149. inst->alg.setkey = crypto_ofb_setkey;
  150. inst->alg.encrypt = crypto_ofb_encrypt;
  151. inst->alg.decrypt = crypto_ofb_decrypt;
  152. inst->free = crypto_ofb_free;
  153. err = skcipher_register_instance(tmpl, inst);
  154. if (err)
  155. goto err_drop_spawn;
  156. out:
  157. return err;
  158. err_drop_spawn:
  159. crypto_drop_spawn(spawn);
  160. err_free_inst:
  161. kfree(inst);
  162. goto out;
  163. }
  164. static struct crypto_template crypto_ofb_tmpl = {
  165. .name = "ofb",
  166. .create = crypto_ofb_create,
  167. .module = THIS_MODULE,
  168. };
  169. static int __init crypto_ofb_module_init(void)
  170. {
  171. return crypto_register_template(&crypto_ofb_tmpl);
  172. }
  173. static void __exit crypto_ofb_module_exit(void)
  174. {
  175. crypto_unregister_template(&crypto_ofb_tmpl);
  176. }
  177. module_init(crypto_ofb_module_init);
  178. module_exit(crypto_ofb_module_exit);
  179. MODULE_LICENSE("GPL");
  180. MODULE_DESCRIPTION("OFB block cipher algorithm");
  181. MODULE_ALIAS_CRYPTO("ofb");