curve25519.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_CURVE25519_H
  15. #define OPENSSL_HEADER_CURVE25519_H
  16. #include <openssl/base.h>
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. /* Curve25519.
  21. *
  22. * Curve25519 is an elliptic curve. See https://tools.ietf.org/html/rfc7748. */
  23. /* X25519.
  24. *
  25. * X25519 is the Diffie-Hellman primitive built from curve25519. It is
  26. * sometimes referred to as “curve25519”, but “X25519” is a more precise name.
  27. * See http://cr.yp.to/ecdh.html and https://tools.ietf.org/html/rfc7748. */
  28. #define X25519_PRIVATE_KEY_LEN 32
  29. #define X25519_PUBLIC_VALUE_LEN 32
  30. #define X25519_SHARED_KEY_LEN 32
  31. /* X25519_keypair sets |out_public_value| and |out_private_key| to a freshly
  32. * generated, public–private key pair. */
  33. OPENSSL_EXPORT void X25519_keypair(uint8_t out_public_value[32],
  34. uint8_t out_private_key[32]);
  35. /* X25519 writes a shared key to |out_shared_key| that is calculated from the
  36. * given private key and the peer's public value. It returns one on success and
  37. * zero on error.
  38. *
  39. * Don't use the shared key directly, rather use a KDF and also include the two
  40. * public values as inputs. */
  41. OPENSSL_EXPORT int X25519(uint8_t out_shared_key[32],
  42. const uint8_t private_key[32],
  43. const uint8_t peers_public_value[32]);
  44. /* X25519_public_from_private calculates a Diffie-Hellman public value from the
  45. * given private key and writes it to |out_public_value|. */
  46. OPENSSL_EXPORT void X25519_public_from_private(uint8_t out_public_value[32],
  47. const uint8_t private_key[32]);
  48. /* Ed25519.
  49. *
  50. * Ed25519 is a signature scheme using a twisted-Edwards curve that is
  51. * birationally equivalent to curve25519. */
  52. #define ED25519_PRIVATE_KEY_LEN 64
  53. #define ED25519_PUBLIC_KEY_LEN 32
  54. #define ED25519_SIGNATURE_LEN 64
  55. /* ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly
  56. * generated, public–private key pair. */
  57. OPENSSL_EXPORT void ED25519_keypair(uint8_t out_public_key[32],
  58. uint8_t out_private_key[64]);
  59. /* ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from
  60. * |message| using |private_key|. It returns one on success or zero on
  61. * error. */
  62. OPENSSL_EXPORT int ED25519_sign(uint8_t out_sig[64], const uint8_t *message,
  63. size_t message_len,
  64. const uint8_t private_key[64]);
  65. /* ED25519_verify returns one iff |signature| is a valid signature, by
  66. * |public_key| of |message_len| bytes from |message|. It returns zero
  67. * otherwise. */
  68. OPENSSL_EXPORT int ED25519_verify(const uint8_t *message, size_t message_len,
  69. const uint8_t signature[64],
  70. const uint8_t public_key[32]);
  71. /* ED25519_keypair_from_seed calculates a public and private key from an
  72. * Ed25519 “seed”. Seed values are not exposed by this API (although they
  73. * happen to be the first 32 bytes of a private key) so this function is for
  74. * interoperating with systems that may store just a seed instead of a full
  75. * private key. */
  76. OPENSSL_EXPORT void ED25519_keypair_from_seed(uint8_t out_public_key[32],
  77. uint8_t out_private_key[64],
  78. const uint8_t seed[32]);
  79. /* SPAKE2.
  80. *
  81. * SPAKE2 is a password-authenticated key-exchange. It allows two parties,
  82. * who share a low-entropy secret (i.e. password), to agree on a shared key.
  83. * An attacker can only make one guess of the password per execution of the
  84. * protocol.
  85. *
  86. * See https://tools.ietf.org/html/draft-irtf-cfrg-spake2-02. */
  87. /* spake2_role_t enumerates the different “roles” in SPAKE2. The protocol
  88. * requires that the symmetry of the two parties be broken so one participant
  89. * must be “Alice” and the other be “Bob”. */
  90. enum spake2_role_t {
  91. spake2_role_alice,
  92. spake2_role_bob,
  93. };
  94. /* SPAKE2_CTX_new creates a new |SPAKE2_CTX| (which can only be used for a
  95. * single execution of the protocol). SPAKE2 requires the symmetry of the two
  96. * parties to be broken which is indicated via |my_role| – each party must pass
  97. * a different value for this argument.
  98. *
  99. * The |my_name| and |their_name| arguments allow optional, opaque names to be
  100. * bound into the protocol. For example MAC addresses, hostnames, usernames
  101. * etc. These values are not exposed and can avoid context-confusion attacks
  102. * when a password is shared between several devices. */
  103. OPENSSL_EXPORT SPAKE2_CTX *SPAKE2_CTX_new(
  104. enum spake2_role_t my_role,
  105. const uint8_t *my_name, size_t my_name_len,
  106. const uint8_t *their_name, size_t their_name_len);
  107. /* SPAKE2_CTX_free frees |ctx| and all the resources that it has allocated. */
  108. OPENSSL_EXPORT void SPAKE2_CTX_free(SPAKE2_CTX *ctx);
  109. /* SPAKE2_MAX_MSG_SIZE is the maximum size of a SPAKE2 message. */
  110. #define SPAKE2_MAX_MSG_SIZE 32
  111. /* SPAKE2_generate_msg generates a SPAKE2 message given |password|, writes
  112. * it to |out| and sets |*out_len| to the number of bytes written.
  113. *
  114. * At most |max_out_len| bytes are written to |out| and, in order to ensure
  115. * success, |max_out_len| should be at least |SPAKE2_MAX_MSG_SIZE| bytes.
  116. *
  117. * This function can only be called once for a given |SPAKE2_CTX|.
  118. *
  119. * It returns one on success and zero on error. */
  120. OPENSSL_EXPORT int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out,
  121. size_t *out_len, size_t max_out_len,
  122. const uint8_t *password,
  123. size_t password_len);
  124. /* SPAKE2_MAX_KEY_SIZE is the maximum amount of key material that SPAKE2 will
  125. * produce. */
  126. #define SPAKE2_MAX_KEY_SIZE 64
  127. /* SPAKE2_process_msg completes the SPAKE2 exchange given the peer's message in
  128. * |their_msg|, writes at most |max_out_key_len| bytes to |out_key| and sets
  129. * |*out_key_len| to the number of bytes written.
  130. *
  131. * The resulting keying material is suitable for:
  132. * a) Using directly in a key-confirmation step: i.e. each side could
  133. * transmit a hash of their role, a channel-binding value and the key
  134. * material to prove to the other side that they know the shared key.
  135. * b) Using as input keying material to HKDF to generate a variety of subkeys
  136. * for encryption etc.
  137. *
  138. * If |max_out_key_key| is smaller than the amount of key material generated
  139. * then the key is silently truncated. If you want to ensure that no truncation
  140. * occurs then |max_out_key| should be at least |SPAKE2_MAX_KEY_SIZE|.
  141. *
  142. * You must call |SPAKE2_generate_msg| on a given |SPAKE2_CTX| before calling
  143. * this function. On successful return, |ctx| is complete and calling
  144. * |SPAKE2_CTX_free| is the only acceptable operation on it.
  145. *
  146. * Returns one on success or zero on error. */
  147. OPENSSL_EXPORT int SPAKE2_process_msg(SPAKE2_CTX *ctx, uint8_t *out_key,
  148. size_t *out_key_len,
  149. size_t max_out_key_len,
  150. const uint8_t *their_msg,
  151. size_t their_msg_len);
  152. #if defined(__cplusplus)
  153. } /* extern C */
  154. extern "C++" {
  155. namespace bssl {
  156. BORINGSSL_MAKE_DELETER(SPAKE2_CTX, SPAKE2_CTX_free)
  157. } // namespace bssl
  158. } /* extern C++ */
  159. #endif
  160. #endif /* OPENSSL_HEADER_CURVE25519_H */