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.
 
 
 
 
 
 

196 lines
7.9 KiB

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