Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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