curve25519.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* $OpenBSD: curve25519.h,v 1.7 2022/11/13 14:05:04 tb Exp $ */
  2. /*
  3. * Copyright (c) 2015, Google Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  12. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  14. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #ifndef HEADER_CURVE25519_H
  18. #define HEADER_CURVE25519_H
  19. #include <stdint.h>
  20. #include <openssl/opensslconf.h>
  21. #if defined(__cplusplus)
  22. extern "C" {
  23. #endif
  24. /*
  25. * Curve25519.
  26. *
  27. * Curve25519 is an elliptic curve. See https://tools.ietf.org/html/rfc7748.
  28. */
  29. /*
  30. * X25519.
  31. *
  32. * X25519 is the Diffie-Hellman primitive built from curve25519. It is
  33. * sometimes referred to as curve25519, but X25519 is a more precise name.
  34. * See http://cr.yp.to/ecdh.html and https://tools.ietf.org/html/rfc7748.
  35. */
  36. #define X25519_KEY_LENGTH 32
  37. /*
  38. * X25519_keypair sets |out_public_value| and |out_private_key| to a freshly
  39. * generated, public/private key pair.
  40. */
  41. void X25519_keypair(uint8_t out_public_value[X25519_KEY_LENGTH],
  42. uint8_t out_private_key[X25519_KEY_LENGTH]);
  43. /*
  44. * X25519 writes a shared key to |out_shared_key| that is calculated from the
  45. * given private key and the peer's public value. It returns one on success and
  46. * zero on error.
  47. *
  48. * Don't use the shared key directly, rather use a KDF and also include the two
  49. * public values as inputs.
  50. */
  51. int X25519(uint8_t out_shared_key[X25519_KEY_LENGTH],
  52. const uint8_t private_key[X25519_KEY_LENGTH],
  53. const uint8_t peers_public_value[X25519_KEY_LENGTH]);
  54. /*
  55. * ED25519
  56. *
  57. * Ed25519 is a signature scheme using a twisted Edwards curve that is
  58. * birationally equivalent to curve25519.
  59. */
  60. #define ED25519_PRIVATE_KEY_LENGTH 32
  61. #define ED25519_PUBLIC_KEY_LENGTH 32
  62. #define ED25519_SIGNATURE_LENGTH 64
  63. /*
  64. * ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly
  65. * generated, public/private key pair.
  66. */
  67. void ED25519_keypair(uint8_t out_public_key[ED25519_PUBLIC_KEY_LENGTH],
  68. uint8_t out_private_key[ED25519_PRIVATE_KEY_LENGTH]);
  69. /*
  70. * ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from
  71. * |message| using |public_key| and |private_key|. It returns one on success
  72. * or zero on allocation failure.
  73. */
  74. int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
  75. const uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH],
  76. const uint8_t private_key_seed[ED25519_PRIVATE_KEY_LENGTH]);
  77. /*
  78. * ED25519_verify returns one iff |signature| is a valid signature by
  79. * |public_key| of |message_len| bytes from |message|. It returns zero
  80. * otherwise.
  81. */
  82. int ED25519_verify(const uint8_t *message, size_t message_len,
  83. const uint8_t signature[ED25519_SIGNATURE_LENGTH],
  84. const uint8_t public_key[ED25519_PUBLIC_KEY_LENGTH]);
  85. #if defined(__cplusplus)
  86. } /* extern C */
  87. #endif
  88. #endif /* HEADER_CURVE25519_H */