akcipher.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Public Key Encryption
  4. *
  5. * Copyright (c) 2015, Intel Corporation
  6. * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/crypto.h>
  15. #include <linux/compiler.h>
  16. #include <crypto/algapi.h>
  17. #include <linux/cryptouser.h>
  18. #include <net/netlink.h>
  19. #include <crypto/akcipher.h>
  20. #include <crypto/internal/akcipher.h>
  21. #include "internal.h"
  22. #ifdef CONFIG_NET
  23. static int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  24. {
  25. struct crypto_report_akcipher rakcipher;
  26. memset(&rakcipher, 0, sizeof(rakcipher));
  27. strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
  28. return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
  29. sizeof(rakcipher), &rakcipher);
  30. }
  31. #else
  32. static int crypto_akcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
  33. {
  34. return -ENOSYS;
  35. }
  36. #endif
  37. static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
  38. __maybe_unused;
  39. static void crypto_akcipher_show(struct seq_file *m, struct crypto_alg *alg)
  40. {
  41. seq_puts(m, "type : akcipher\n");
  42. }
  43. static void crypto_akcipher_exit_tfm(struct crypto_tfm *tfm)
  44. {
  45. struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
  46. struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
  47. alg->exit(akcipher);
  48. }
  49. static int crypto_akcipher_init_tfm(struct crypto_tfm *tfm)
  50. {
  51. struct crypto_akcipher *akcipher = __crypto_akcipher_tfm(tfm);
  52. struct akcipher_alg *alg = crypto_akcipher_alg(akcipher);
  53. if (alg->exit)
  54. akcipher->base.exit = crypto_akcipher_exit_tfm;
  55. if (alg->init)
  56. return alg->init(akcipher);
  57. return 0;
  58. }
  59. static void crypto_akcipher_free_instance(struct crypto_instance *inst)
  60. {
  61. struct akcipher_instance *akcipher = akcipher_instance(inst);
  62. akcipher->free(akcipher);
  63. }
  64. static const struct crypto_type crypto_akcipher_type = {
  65. .extsize = crypto_alg_extsize,
  66. .init_tfm = crypto_akcipher_init_tfm,
  67. .free = crypto_akcipher_free_instance,
  68. #ifdef CONFIG_PROC_FS
  69. .show = crypto_akcipher_show,
  70. #endif
  71. .report = crypto_akcipher_report,
  72. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  73. .maskset = CRYPTO_ALG_TYPE_MASK,
  74. .type = CRYPTO_ALG_TYPE_AKCIPHER,
  75. .tfmsize = offsetof(struct crypto_akcipher, base),
  76. };
  77. int crypto_grab_akcipher(struct crypto_akcipher_spawn *spawn,
  78. struct crypto_instance *inst,
  79. const char *name, u32 type, u32 mask)
  80. {
  81. spawn->base.frontend = &crypto_akcipher_type;
  82. return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
  83. }
  84. EXPORT_SYMBOL_GPL(crypto_grab_akcipher);
  85. struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
  86. u32 mask)
  87. {
  88. return crypto_alloc_tfm(alg_name, &crypto_akcipher_type, type, mask);
  89. }
  90. EXPORT_SYMBOL_GPL(crypto_alloc_akcipher);
  91. static void akcipher_prepare_alg(struct akcipher_alg *alg)
  92. {
  93. struct crypto_alg *base = &alg->base;
  94. base->cra_type = &crypto_akcipher_type;
  95. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  96. base->cra_flags |= CRYPTO_ALG_TYPE_AKCIPHER;
  97. }
  98. static int akcipher_default_op(struct akcipher_request *req)
  99. {
  100. return -ENOSYS;
  101. }
  102. int crypto_register_akcipher(struct akcipher_alg *alg)
  103. {
  104. struct crypto_alg *base = &alg->base;
  105. if (!alg->sign)
  106. alg->sign = akcipher_default_op;
  107. if (!alg->verify)
  108. alg->verify = akcipher_default_op;
  109. if (!alg->encrypt)
  110. alg->encrypt = akcipher_default_op;
  111. if (!alg->decrypt)
  112. alg->decrypt = akcipher_default_op;
  113. akcipher_prepare_alg(alg);
  114. return crypto_register_alg(base);
  115. }
  116. EXPORT_SYMBOL_GPL(crypto_register_akcipher);
  117. void crypto_unregister_akcipher(struct akcipher_alg *alg)
  118. {
  119. crypto_unregister_alg(&alg->base);
  120. }
  121. EXPORT_SYMBOL_GPL(crypto_unregister_akcipher);
  122. int akcipher_register_instance(struct crypto_template *tmpl,
  123. struct akcipher_instance *inst)
  124. {
  125. if (WARN_ON(!inst->free))
  126. return -EINVAL;
  127. akcipher_prepare_alg(&inst->alg);
  128. return crypto_register_instance(tmpl, akcipher_crypto_instance(inst));
  129. }
  130. EXPORT_SYMBOL_GPL(akcipher_register_instance);
  131. MODULE_LICENSE("GPL");
  132. MODULE_DESCRIPTION("Generic public key cipher type");