fpu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * FPU: Wrapper for blkcipher touching fpu
  3. *
  4. * Copyright (c) Intel Corp.
  5. * Author: Huang Ying <ying.huang@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. */
  13. #include <crypto/internal/skcipher.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <asm/fpu/api.h>
  20. struct crypto_fpu_ctx {
  21. struct crypto_skcipher *child;
  22. };
  23. static int crypto_fpu_setkey(struct crypto_skcipher *parent, const u8 *key,
  24. unsigned int keylen)
  25. {
  26. struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(parent);
  27. struct crypto_skcipher *child = ctx->child;
  28. int err;
  29. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  30. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  31. CRYPTO_TFM_REQ_MASK);
  32. err = crypto_skcipher_setkey(child, key, keylen);
  33. crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
  34. CRYPTO_TFM_RES_MASK);
  35. return err;
  36. }
  37. static int crypto_fpu_encrypt(struct skcipher_request *req)
  38. {
  39. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  40. struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
  41. struct crypto_skcipher *child = ctx->child;
  42. SKCIPHER_REQUEST_ON_STACK(subreq, child);
  43. int err;
  44. skcipher_request_set_tfm(subreq, child);
  45. skcipher_request_set_callback(subreq, 0, NULL, NULL);
  46. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  47. req->iv);
  48. kernel_fpu_begin();
  49. err = crypto_skcipher_encrypt(subreq);
  50. kernel_fpu_end();
  51. skcipher_request_zero(subreq);
  52. return err;
  53. }
  54. static int crypto_fpu_decrypt(struct skcipher_request *req)
  55. {
  56. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  57. struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
  58. struct crypto_skcipher *child = ctx->child;
  59. SKCIPHER_REQUEST_ON_STACK(subreq, child);
  60. int err;
  61. skcipher_request_set_tfm(subreq, child);
  62. skcipher_request_set_callback(subreq, 0, NULL, NULL);
  63. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  64. req->iv);
  65. kernel_fpu_begin();
  66. err = crypto_skcipher_decrypt(subreq);
  67. kernel_fpu_end();
  68. skcipher_request_zero(subreq);
  69. return err;
  70. }
  71. static int crypto_fpu_init_tfm(struct crypto_skcipher *tfm)
  72. {
  73. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  74. struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
  75. struct crypto_skcipher_spawn *spawn;
  76. struct crypto_skcipher *cipher;
  77. spawn = skcipher_instance_ctx(inst);
  78. cipher = crypto_spawn_skcipher(spawn);
  79. if (IS_ERR(cipher))
  80. return PTR_ERR(cipher);
  81. ctx->child = cipher;
  82. return 0;
  83. }
  84. static void crypto_fpu_exit_tfm(struct crypto_skcipher *tfm)
  85. {
  86. struct crypto_fpu_ctx *ctx = crypto_skcipher_ctx(tfm);
  87. crypto_free_skcipher(ctx->child);
  88. }
  89. static void crypto_fpu_free(struct skcipher_instance *inst)
  90. {
  91. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  92. kfree(inst);
  93. }
  94. static int crypto_fpu_create(struct crypto_template *tmpl, struct rtattr **tb)
  95. {
  96. struct crypto_skcipher_spawn *spawn;
  97. struct skcipher_instance *inst;
  98. struct crypto_attr_type *algt;
  99. struct skcipher_alg *alg;
  100. const char *cipher_name;
  101. int err;
  102. algt = crypto_get_attr_type(tb);
  103. if (IS_ERR(algt))
  104. return PTR_ERR(algt);
  105. if ((algt->type ^ (CRYPTO_ALG_INTERNAL | CRYPTO_ALG_TYPE_SKCIPHER)) &
  106. algt->mask)
  107. return -EINVAL;
  108. if (!(algt->mask & CRYPTO_ALG_INTERNAL))
  109. return -EINVAL;
  110. cipher_name = crypto_attr_alg_name(tb[1]);
  111. if (IS_ERR(cipher_name))
  112. return PTR_ERR(cipher_name);
  113. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  114. if (!inst)
  115. return -ENOMEM;
  116. spawn = skcipher_instance_ctx(inst);
  117. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  118. err = crypto_grab_skcipher(spawn, cipher_name, CRYPTO_ALG_INTERNAL,
  119. CRYPTO_ALG_INTERNAL | CRYPTO_ALG_ASYNC);
  120. if (err)
  121. goto out_free_inst;
  122. alg = crypto_skcipher_spawn_alg(spawn);
  123. err = crypto_inst_setname(skcipher_crypto_instance(inst), "fpu",
  124. &alg->base);
  125. if (err)
  126. goto out_drop_skcipher;
  127. inst->alg.base.cra_flags = CRYPTO_ALG_INTERNAL;
  128. inst->alg.base.cra_priority = alg->base.cra_priority;
  129. inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
  130. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  131. inst->alg.ivsize = crypto_skcipher_alg_ivsize(alg);
  132. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
  133. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
  134. inst->alg.base.cra_ctxsize = sizeof(struct crypto_fpu_ctx);
  135. inst->alg.init = crypto_fpu_init_tfm;
  136. inst->alg.exit = crypto_fpu_exit_tfm;
  137. inst->alg.setkey = crypto_fpu_setkey;
  138. inst->alg.encrypt = crypto_fpu_encrypt;
  139. inst->alg.decrypt = crypto_fpu_decrypt;
  140. inst->free = crypto_fpu_free;
  141. err = skcipher_register_instance(tmpl, inst);
  142. if (err)
  143. goto out_drop_skcipher;
  144. out:
  145. return err;
  146. out_drop_skcipher:
  147. crypto_drop_skcipher(spawn);
  148. out_free_inst:
  149. kfree(inst);
  150. goto out;
  151. }
  152. static struct crypto_template crypto_fpu_tmpl = {
  153. .name = "fpu",
  154. .create = crypto_fpu_create,
  155. .module = THIS_MODULE,
  156. };
  157. int __init crypto_fpu_init(void)
  158. {
  159. return crypto_register_template(&crypto_fpu_tmpl);
  160. }
  161. void crypto_fpu_exit(void)
  162. {
  163. crypto_unregister_template(&crypto_fpu_tmpl);
  164. }
  165. MODULE_ALIAS_CRYPTO("fpu");