blob: 8d73f334e5143daf224afa0c9b3a72ad1a1cd794 [file] [log] [blame]
Adam Langley4fb0dc42015-11-13 13:09:47 -08001/* 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
15#ifndef OPENSSL_HEADER_CURVE25519_H
16#define OPENSSL_HEADER_CURVE25519_H
17
18#include <openssl/base.h>
19
20#if defined(__cplusplus)
21extern "C" {
22#endif
23
24
25/* Curve25519.
26 *
David Benjamin415564f2016-01-23 01:04:15 -050027 * Curve25519 is an elliptic curve. See https://tools.ietf.org/html/rfc7748. */
Adam Langley4fb0dc42015-11-13 13:09:47 -080028
29
30/* X25519.
31 *
David Benjamina82e8dd2016-04-14 09:48:39 -040032 * 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. */
Adam Langley4fb0dc42015-11-13 13:09:47 -080035
David Benjamin6f733792016-10-31 14:37:04 -040036#define X25519_PRIVATE_KEY_LEN 32
37#define X25519_PUBLIC_VALUE_LEN 32
38
Adam Langley4fb0dc42015-11-13 13:09:47 -080039/* X25519_keypair sets |out_public_value| and |out_private_key| to a freshly
40 * generated, public–private key pair. */
41OPENSSL_EXPORT void X25519_keypair(uint8_t out_public_value[32],
42 uint8_t out_private_key[32]);
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. */
50OPENSSL_EXPORT int X25519(uint8_t out_shared_key[32],
51 const uint8_t private_key[32],
52 const uint8_t peers_public_value[32]);
53
54/* X25519_public_from_private calculates a Diffie-Hellman public value from the
55 * given private key and writes it to |out_public_value|. */
56OPENSSL_EXPORT void X25519_public_from_private(uint8_t out_public_value[32],
57 const uint8_t private_key[32]);
58
59
60/* Ed25519.
61 *
62 * Ed25519 is a signature scheme using a twisted-Edwards curve that is
63 * birationally equivalent to curve25519. */
64
Matt Braithwaitec75c0ae2015-12-15 13:14:05 -080065#define ED25519_PRIVATE_KEY_LEN 64
66#define ED25519_PUBLIC_KEY_LEN 32
67#define ED25519_SIGNATURE_LEN 64
68
Adam Langley4fb0dc42015-11-13 13:09:47 -080069/* ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly
70 * generated, public–private key pair. */
71OPENSSL_EXPORT void ED25519_keypair(uint8_t out_public_key[32],
72 uint8_t out_private_key[64]);
73
74/* ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from
75 * |message| using |private_key|. It returns one on success or zero on
76 * error. */
77OPENSSL_EXPORT int ED25519_sign(uint8_t out_sig[64], const uint8_t *message,
78 size_t message_len,
79 const uint8_t private_key[64]);
80
81/* ED25519_verify returns one iff |signature| is a valid signature, by
82 * |public_key| of |message_len| bytes from |message|. It returns zero
83 * otherwise. */
84OPENSSL_EXPORT int ED25519_verify(const uint8_t *message, size_t message_len,
85 const uint8_t signature[64],
86 const uint8_t public_key[32]);
87
88
Arnar Birgissonf27459e2016-02-09 18:09:00 -080089/* SPAKE2.
90 *
91 * SPAKE2 is a password-authenticated key-exchange. It allows two parties,
92 * who share a low-entropy secret (i.e. password), to agree on a shared key.
93 * An attacker can only make one guess of the password per execution of the
94 * protocol.
95 *
96 * See https://tools.ietf.org/html/draft-irtf-cfrg-spake2-02. */
97
98/* spake2_role_t enumerates the different “roles” in SPAKE2. The protocol
99 * requires that the symmetry of the two parties be broken so one participant
100 * must be “Alice” and the other be “Bob”. */
101enum spake2_role_t {
102 spake2_role_alice,
103 spake2_role_bob,
104};
105
106/* SPAKE2_CTX_new creates a new |SPAKE2_CTX| (which can only be used for a
107 * single execution of the protocol). SPAKE2 requires the symmetry of the two
108 * parties to be broken which is indicated via |my_role| – each party must pass
109 * a different value for this argument.
110 *
111 * The |my_name| and |their_name| arguments allow optional, opaque names to be
112 * bound into the protocol. For example MAC addresses, hostnames, usernames
113 * etc. These values are not exposed and can avoid context-confusion attacks
114 * when a password is shared between several devices. */
115OPENSSL_EXPORT SPAKE2_CTX *SPAKE2_CTX_new(
116 enum spake2_role_t my_role,
117 const uint8_t *my_name, size_t my_name_len,
118 const uint8_t *their_name, size_t their_name_len);
119
120/* SPAKE2_CTX_free frees |ctx| and all the resources that it has allocated. */
121OPENSSL_EXPORT void SPAKE2_CTX_free(SPAKE2_CTX *ctx);
122
123/* SPAKE2_MAX_MSG_SIZE is the maximum size of a SPAKE2 message. */
124#define SPAKE2_MAX_MSG_SIZE 32
125
126/* SPAKE2_generate_msg generates a SPAKE2 message given |password|, writes
127 * it to |out| and sets |*out_len| to the number of bytes written.
128 *
129 * At most |max_out_len| bytes are written to |out| and, in order to ensure
130 * success, |max_out_len| should be at least |SPAKE2_MAX_MSG_SIZE| bytes.
131 *
132 * This function can only be called once for a given |SPAKE2_CTX|.
133 *
134 * It returns one on success and zero on error. */
135OPENSSL_EXPORT int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out,
136 size_t *out_len, size_t max_out_len,
137 const uint8_t *password,
138 size_t password_len);
139
140/* SPAKE2_MAX_KEY_SIZE is the maximum amount of key material that SPAKE2 will
141 * produce. */
142#define SPAKE2_MAX_KEY_SIZE 64
143
144/* SPAKE2_process_msg completes the SPAKE2 exchange given the peer's message in
145 * |their_msg|, writes at most |max_out_key_len| bytes to |out_key| and sets
146 * |*out_key_len| to the number of bytes written.
147 *
148 * The resulting keying material is suitable for:
149 * a) Using directly in a key-confirmation step: i.e. each side could
150 * transmit a hash of their role, a channel-binding value and the key
151 * material to prove to the other side that they know the shared key.
152 * b) Using as input keying material to HKDF to generate a variety of subkeys
153 * for encryption etc.
154 *
155 * If |max_out_key_key| is smaller than the amount of key material generated
156 * then the key is silently truncated. If you want to ensure that no truncation
157 * occurs then |max_out_key| should be at least |SPAKE2_MAX_KEY_SIZE|.
158 *
159 * You must call |SPAKE2_generate_msg| on a given |SPAKE2_CTX| before calling
160 * this function. On successful return, |ctx| is complete and calling
161 * |SPAKE2_CTX_free| is the only acceptable operation on it.
162 *
163 * Returns one on success or zero on error. */
164OPENSSL_EXPORT int SPAKE2_process_msg(SPAKE2_CTX *ctx, uint8_t *out_key,
165 size_t *out_key_len,
166 size_t max_out_key_len,
167 const uint8_t *their_msg,
168 size_t their_msg_len);
169
170
Adam Langley4fb0dc42015-11-13 13:09:47 -0800171#if defined(__cplusplus)
172} /* extern C */
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700173
174extern "C++" {
175
176namespace bssl {
177
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700178BORINGSSL_MAKE_DELETER(SPAKE2_CTX, SPAKE2_CTX_free)
179
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700180} // namespace bssl
181
182} /* extern C++ */
183
Adam Langley4fb0dc42015-11-13 13:09:47 -0800184#endif
185
186#endif /* OPENSSL_HEADER_CURVE25519_H */