You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 peer_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. //
  53. // Note that, unlike RFC 8032's formulation, our private key representation
  54. // includes a public key suffix to make multiple key signing operations with the
  55. // same key more efficient. The RFC 8032 private key is referred to in this
  56. // implementation as the "seed" and is the first 32 bytes of our private key.
  57. #define ED25519_PRIVATE_KEY_LEN 64
  58. #define ED25519_PUBLIC_KEY_LEN 32
  59. #define ED25519_SIGNATURE_LEN 64
  60. // ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly
  61. // generated, public–private key pair.
  62. OPENSSL_EXPORT void ED25519_keypair(uint8_t out_public_key[32],
  63. uint8_t out_private_key[64]);
  64. // ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from
  65. // |message| using |private_key|. It returns one on success or zero on
  66. // allocation failure.
  67. OPENSSL_EXPORT int ED25519_sign(uint8_t out_sig[64], const uint8_t *message,
  68. size_t message_len,
  69. const uint8_t private_key[64]);
  70. // ED25519_verify returns one iff |signature| is a valid signature, by
  71. // |public_key| of |message_len| bytes from |message|. It returns zero
  72. // otherwise.
  73. OPENSSL_EXPORT int ED25519_verify(const uint8_t *message, size_t message_len,
  74. const uint8_t signature[64],
  75. const uint8_t public_key[32]);
  76. // ED25519_keypair_from_seed calculates a public and private key from an
  77. // Ed25519 “seed”. Seed values are not exposed by this API (although they
  78. // happen to be the first 32 bytes of a private key) so this function is for
  79. // interoperating with systems that may store just a seed instead of a full
  80. // private key.
  81. OPENSSL_EXPORT void ED25519_keypair_from_seed(uint8_t out_public_key[32],
  82. uint8_t out_private_key[64],
  83. const uint8_t seed[32]);
  84. // SPAKE2.
  85. //
  86. // SPAKE2 is a password-authenticated key-exchange. It allows two parties,
  87. // who share a low-entropy secret (i.e. password), to agree on a shared key.
  88. // An attacker can only make one guess of the password per execution of the
  89. // protocol.
  90. //
  91. // See https://tools.ietf.org/html/draft-irtf-cfrg-spake2-02.
  92. // spake2_role_t enumerates the different “roles” in SPAKE2. The protocol
  93. // requires that the symmetry of the two parties be broken so one participant
  94. // must be “Alice” and the other be “Bob”.
  95. enum spake2_role_t {
  96. spake2_role_alice,
  97. spake2_role_bob,
  98. };
  99. // SPAKE2_CTX_new creates a new |SPAKE2_CTX| (which can only be used for a
  100. // single execution of the protocol). SPAKE2 requires the symmetry of the two
  101. // parties to be broken which is indicated via |my_role| – each party must pass
  102. // a different value for this argument.
  103. //
  104. // The |my_name| and |their_name| arguments allow optional, opaque names to be
  105. // bound into the protocol. For example MAC addresses, hostnames, usernames
  106. // etc. These values are not exposed and can avoid context-confusion attacks
  107. // when a password is shared between several devices.
  108. OPENSSL_EXPORT SPAKE2_CTX *SPAKE2_CTX_new(
  109. enum spake2_role_t my_role,
  110. const uint8_t *my_name, size_t my_name_len,
  111. const uint8_t *their_name, size_t their_name_len);
  112. // SPAKE2_CTX_free frees |ctx| and all the resources that it has allocated.
  113. OPENSSL_EXPORT void SPAKE2_CTX_free(SPAKE2_CTX *ctx);
  114. // SPAKE2_MAX_MSG_SIZE is the maximum size of a SPAKE2 message.
  115. #define SPAKE2_MAX_MSG_SIZE 32
  116. // SPAKE2_generate_msg generates a SPAKE2 message given |password|, writes
  117. // it to |out| and sets |*out_len| to the number of bytes written.
  118. //
  119. // At most |max_out_len| bytes are written to |out| and, in order to ensure
  120. // success, |max_out_len| should be at least |SPAKE2_MAX_MSG_SIZE| bytes.
  121. //
  122. // This function can only be called once for a given |SPAKE2_CTX|.
  123. //
  124. // It returns one on success and zero on error.
  125. OPENSSL_EXPORT int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out,
  126. size_t *out_len, size_t max_out_len,
  127. const uint8_t *password,
  128. size_t password_len);
  129. // SPAKE2_MAX_KEY_SIZE is the maximum amount of key material that SPAKE2 will
  130. // produce.
  131. #define SPAKE2_MAX_KEY_SIZE 64
  132. // SPAKE2_process_msg completes the SPAKE2 exchange given the peer's message in
  133. // |their_msg|, writes at most |max_out_key_len| bytes to |out_key| and sets
  134. // |*out_key_len| to the number of bytes written.
  135. //
  136. // The resulting keying material is suitable for:
  137. // a) Using directly in a key-confirmation step: i.e. each side could
  138. // transmit a hash of their role, a channel-binding value and the key
  139. // material to prove to the other side that they know the shared key.
  140. // b) Using as input keying material to HKDF to generate a variety of subkeys
  141. // for encryption etc.
  142. //
  143. // If |max_out_key_key| is smaller than the amount of key material generated
  144. // then the key is silently truncated. If you want to ensure that no truncation
  145. // occurs then |max_out_key| should be at least |SPAKE2_MAX_KEY_SIZE|.
  146. //
  147. // You must call |SPAKE2_generate_msg| on a given |SPAKE2_CTX| before calling
  148. // this function. On successful return, |ctx| is complete and calling
  149. // |SPAKE2_CTX_free| is the only acceptable operation on it.
  150. //
  151. // Returns one on success or zero on error.
  152. OPENSSL_EXPORT int SPAKE2_process_msg(SPAKE2_CTX *ctx, uint8_t *out_key,
  153. size_t *out_key_len,
  154. size_t max_out_key_len,
  155. const uint8_t *their_msg,
  156. size_t their_msg_len);
  157. #if defined(__cplusplus)
  158. } // extern C
  159. extern "C++" {
  160. BSSL_NAMESPACE_BEGIN
  161. BORINGSSL_MAKE_DELETER(SPAKE2_CTX, SPAKE2_CTX_free)
  162. BSSL_NAMESPACE_END
  163. } // extern C++
  164. #endif
  165. #endif // OPENSSL_HEADER_CURVE25519_H