kpp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Key-agreement Protocol Primitives (KPP)
  3. *
  4. * Copyright (c) 2016, Intel Corporation
  5. * Authors: Salvatore Benedetto <salvatore.benedetto@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 <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include <linux/crypto.h>
  20. #include <crypto/algapi.h>
  21. #include <linux/cryptouser.h>
  22. #include <linux/compiler.h>
  23. #include <net/netlink.h>
  24. #include <crypto/kpp.h>
  25. #include <crypto/internal/kpp.h>
  26. #include "internal.h"
  27. #ifdef CONFIG_NET
  28. static int crypto_kpp_report(struct sk_buff *skb, struct crypto_alg *alg)
  29. {
  30. struct crypto_report_kpp rkpp;
  31. strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
  32. if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
  33. sizeof(struct crypto_report_kpp), &rkpp))
  34. goto nla_put_failure;
  35. return 0;
  36. nla_put_failure:
  37. return -EMSGSIZE;
  38. }
  39. #else
  40. static int crypto_kpp_report(struct sk_buff *skb, struct crypto_alg *alg)
  41. {
  42. return -ENOSYS;
  43. }
  44. #endif
  45. static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg)
  46. __maybe_unused;
  47. static void crypto_kpp_show(struct seq_file *m, struct crypto_alg *alg)
  48. {
  49. seq_puts(m, "type : kpp\n");
  50. }
  51. static void crypto_kpp_exit_tfm(struct crypto_tfm *tfm)
  52. {
  53. struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
  54. struct kpp_alg *alg = crypto_kpp_alg(kpp);
  55. alg->exit(kpp);
  56. }
  57. static int crypto_kpp_init_tfm(struct crypto_tfm *tfm)
  58. {
  59. struct crypto_kpp *kpp = __crypto_kpp_tfm(tfm);
  60. struct kpp_alg *alg = crypto_kpp_alg(kpp);
  61. if (alg->exit)
  62. kpp->base.exit = crypto_kpp_exit_tfm;
  63. if (alg->init)
  64. return alg->init(kpp);
  65. return 0;
  66. }
  67. static const struct crypto_type crypto_kpp_type = {
  68. .extsize = crypto_alg_extsize,
  69. .init_tfm = crypto_kpp_init_tfm,
  70. #ifdef CONFIG_PROC_FS
  71. .show = crypto_kpp_show,
  72. #endif
  73. .report = crypto_kpp_report,
  74. .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  75. .maskset = CRYPTO_ALG_TYPE_MASK,
  76. .type = CRYPTO_ALG_TYPE_KPP,
  77. .tfmsize = offsetof(struct crypto_kpp, base),
  78. };
  79. struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask)
  80. {
  81. return crypto_alloc_tfm(alg_name, &crypto_kpp_type, type, mask);
  82. }
  83. EXPORT_SYMBOL_GPL(crypto_alloc_kpp);
  84. static void kpp_prepare_alg(struct kpp_alg *alg)
  85. {
  86. struct crypto_alg *base = &alg->base;
  87. base->cra_type = &crypto_kpp_type;
  88. base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
  89. base->cra_flags |= CRYPTO_ALG_TYPE_KPP;
  90. }
  91. int crypto_register_kpp(struct kpp_alg *alg)
  92. {
  93. struct crypto_alg *base = &alg->base;
  94. kpp_prepare_alg(alg);
  95. return crypto_register_alg(base);
  96. }
  97. EXPORT_SYMBOL_GPL(crypto_register_kpp);
  98. void crypto_unregister_kpp(struct kpp_alg *alg)
  99. {
  100. crypto_unregister_alg(&alg->base);
  101. }
  102. EXPORT_SYMBOL_GPL(crypto_unregister_kpp);
  103. MODULE_LICENSE("GPL");
  104. MODULE_DESCRIPTION("Key-agreement Protocol Primitives");