curve25519-glue.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. *
  5. * Based on public domain code from Daniel J. Bernstein and Peter Schwabe. This
  6. * began from SUPERCOP's curve25519/neon2/scalarmult.s, but has subsequently been
  7. * manually reworked for use in kernel space.
  8. */
  9. #include <asm/hwcap.h>
  10. #include <asm/neon.h>
  11. #include <asm/simd.h>
  12. #include <crypto/internal/kpp.h>
  13. #include <crypto/internal/simd.h>
  14. #include <linux/types.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/jump_label.h>
  18. #include <linux/scatterlist.h>
  19. #include <crypto/curve25519.h>
  20. asmlinkage void curve25519_neon(u8 mypublic[CURVE25519_KEY_SIZE],
  21. const u8 secret[CURVE25519_KEY_SIZE],
  22. const u8 basepoint[CURVE25519_KEY_SIZE]);
  23. static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon);
  24. void curve25519_arch(u8 out[CURVE25519_KEY_SIZE],
  25. const u8 scalar[CURVE25519_KEY_SIZE],
  26. const u8 point[CURVE25519_KEY_SIZE])
  27. {
  28. if (static_branch_likely(&have_neon) && crypto_simd_usable()) {
  29. kernel_neon_begin();
  30. curve25519_neon(out, scalar, point);
  31. kernel_neon_end();
  32. } else {
  33. curve25519_generic(out, scalar, point);
  34. }
  35. }
  36. EXPORT_SYMBOL(curve25519_arch);
  37. void curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
  38. const u8 secret[CURVE25519_KEY_SIZE])
  39. {
  40. return curve25519_arch(pub, secret, curve25519_base_point);
  41. }
  42. EXPORT_SYMBOL(curve25519_base_arch);
  43. static int curve25519_set_secret(struct crypto_kpp *tfm, const void *buf,
  44. unsigned int len)
  45. {
  46. u8 *secret = kpp_tfm_ctx(tfm);
  47. if (!len)
  48. curve25519_generate_secret(secret);
  49. else if (len == CURVE25519_KEY_SIZE &&
  50. crypto_memneq(buf, curve25519_null_point, CURVE25519_KEY_SIZE))
  51. memcpy(secret, buf, CURVE25519_KEY_SIZE);
  52. else
  53. return -EINVAL;
  54. return 0;
  55. }
  56. static int curve25519_compute_value(struct kpp_request *req)
  57. {
  58. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  59. const u8 *secret = kpp_tfm_ctx(tfm);
  60. u8 public_key[CURVE25519_KEY_SIZE];
  61. u8 buf[CURVE25519_KEY_SIZE];
  62. int copied, nbytes;
  63. u8 const *bp;
  64. if (req->src) {
  65. copied = sg_copy_to_buffer(req->src,
  66. sg_nents_for_len(req->src,
  67. CURVE25519_KEY_SIZE),
  68. public_key, CURVE25519_KEY_SIZE);
  69. if (copied != CURVE25519_KEY_SIZE)
  70. return -EINVAL;
  71. bp = public_key;
  72. } else {
  73. bp = curve25519_base_point;
  74. }
  75. curve25519_arch(buf, secret, bp);
  76. /* might want less than we've got */
  77. nbytes = min_t(size_t, CURVE25519_KEY_SIZE, req->dst_len);
  78. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
  79. nbytes),
  80. buf, nbytes);
  81. if (copied != nbytes)
  82. return -EINVAL;
  83. return 0;
  84. }
  85. static unsigned int curve25519_max_size(struct crypto_kpp *tfm)
  86. {
  87. return CURVE25519_KEY_SIZE;
  88. }
  89. static struct kpp_alg curve25519_alg = {
  90. .base.cra_name = "curve25519",
  91. .base.cra_driver_name = "curve25519-neon",
  92. .base.cra_priority = 200,
  93. .base.cra_module = THIS_MODULE,
  94. .base.cra_ctxsize = CURVE25519_KEY_SIZE,
  95. .set_secret = curve25519_set_secret,
  96. .generate_public_key = curve25519_compute_value,
  97. .compute_shared_secret = curve25519_compute_value,
  98. .max_size = curve25519_max_size,
  99. };
  100. static int __init mod_init(void)
  101. {
  102. if (elf_hwcap & HWCAP_NEON) {
  103. static_branch_enable(&have_neon);
  104. return IS_REACHABLE(CONFIG_CRYPTO_KPP) ?
  105. crypto_register_kpp(&curve25519_alg) : 0;
  106. }
  107. return 0;
  108. }
  109. static void __exit mod_exit(void)
  110. {
  111. if (IS_REACHABLE(CONFIG_CRYPTO_KPP) && elf_hwcap & HWCAP_NEON)
  112. crypto_unregister_kpp(&curve25519_alg);
  113. }
  114. module_init(mod_init);
  115. module_exit(mod_exit);
  116. MODULE_ALIAS_CRYPTO("curve25519");
  117. MODULE_ALIAS_CRYPTO("curve25519-neon");
  118. MODULE_LICENSE("GPL v2");