| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CURVE25519_H |
| 5 |
#define OPENSSL_HEADER_CURVE25519_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
|
| 14 |
// Curve25519. |
| 15 |
// |
| 16 |
// Curve25519 is an elliptic curve. See https://tools.ietf.org/html/rfc7748. |
| 17 |
|
| 18 |
|
| 19 |
// X25519. |
| 20 |
// |
| 21 |
// X25519 is the Diffie-Hellman primitive built from curve25519. It is |
| 22 |
// sometimes referred to as “curve25519”, but “X25519” is a more precise name. |
| 23 |
// See http://cr.yp.to/ecdh.html and https://tools.ietf.org/html/rfc7748. |
| 24 |
|
| 25 |
#define X25519_PRIVATE_KEY_LEN 32 |
| 26 |
#define X25519_PUBLIC_VALUE_LEN 32 |
| 27 |
#define X25519_SHARED_KEY_LEN 32 |
| 28 |
|
| 29 |
// X25519_keypair sets |out_public_value| and |out_private_key| to a freshly |
| 30 |
// generated, public–private key pair. |
| 31 |
OPENSSL_EXPORT void X25519_keypair( |
| 32 |
uint8_t out_public_value[X25519_PUBLIC_VALUE_LEN], |
| 33 |
uint8_t out_private_key[X25519_PRIVATE_KEY_LEN]); |
| 34 |
|
| 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[X25519_SHARED_KEY_LEN], |
| 42 |
const uint8_t private_key[X25519_PRIVATE_KEY_LEN], |
| 43 |
const uint8_t peer_public_value[X25519_PUBLIC_VALUE_LEN]); |
| 44 |
|
| 45 |
// X25519_public_from_private calculates a Diffie-Hellman public value from the |
| 46 |
// given private key and writes it to |out_public_value|. |
| 47 |
OPENSSL_EXPORT void X25519_public_from_private( |
| 48 |
uint8_t out_public_value[X25519_PUBLIC_VALUE_LEN], |
| 49 |
const uint8_t private_key[X25519_PRIVATE_KEY_LEN]); |
| 50 |
|
| 51 |
|
| 52 |
// Ed25519. |
| 53 |
// |
| 54 |
// Ed25519 is a signature scheme using a twisted-Edwards curve that is |
| 55 |
// birationally equivalent to curve25519. |
| 56 |
// |
| 57 |
// Note that, unlike RFC 8032's formulation, our private key representation |
| 58 |
// includes a public key suffix to make multiple key signing operations with the |
| 59 |
// same key more efficient. The RFC 8032 private key is referred to in this |
| 60 |
// implementation as the "seed" and is the first 32 bytes of our private key. |
| 61 |
|
| 62 |
#define ED25519_PRIVATE_KEY_LEN 64 |
| 63 |
#define ED25519_PRIVATE_KEY_SEED_LEN 32 |
| 64 |
#define ED25519_PUBLIC_KEY_LEN 32 |
| 65 |
#define ED25519_SIGNATURE_LEN 64 |
| 66 |
#define ED25519_SEED_LEN 32 |
| 67 |
|
| 68 |
// ED25519_keypair sets |out_public_key| and |out_private_key| to a freshly |
| 69 |
// generated, public–private key pair. |
| 70 |
OPENSSL_EXPORT void ED25519_keypair(uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN], |
| 71 |
uint8_t out_private_key[ED25519_PRIVATE_KEY_LEN]); |
| 72 |
|
| 73 |
// ED25519_sign sets |out_sig| to be a signature of |message_len| bytes from |
| 74 |
// |message| using |private_key|. It returns one on success or zero on |
| 75 |
// allocation failure. |
| 76 |
OPENSSL_EXPORT int ED25519_sign(uint8_t out_sig[ED25519_SIGNATURE_LEN], |
| 77 |
const uint8_t *message, size_t message_len, |
| 78 |
const uint8_t private_key[ED25519_PRIVATE_KEY_LEN]); |
| 79 |
|
| 80 |
// ED25519_verify returns one iff |signature| is a valid signature, by |
| 81 |
// |public_key| of |message_len| bytes from |message|. It returns zero |
| 82 |
// otherwise. |
| 83 |
OPENSSL_EXPORT int ED25519_verify(const uint8_t *message, size_t message_len, |
| 84 |
const uint8_t signature[ED25519_SIGNATURE_LEN], |
| 85 |
const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]); |
| 86 |
|
| 87 |
// ED25519ctx_sign sets |out_sig| to be a Ed25519ctx (RFC 8032) pure signature |
| 88 |
// of |message_len| bytes from |message| using |private_key|, and the provided |
| 89 |
// |context_len| bytes for |context|. |context_len| must be greater than zero, |
| 90 |
// but no more than 255. It returns one on success or zero on failure. |
| 91 |
OPENSSL_EXPORT int ED25519ctx_sign( |
| 92 |
uint8_t out_sig[ED25519_SIGNATURE_LEN], |
| 93 |
const uint8_t *message, size_t message_len, |
| 94 |
const uint8_t private_key[ED25519_PRIVATE_KEY_LEN], |
| 95 |
const uint8_t *context, size_t context_len); |
| 96 |
|
| 97 |
// ED25519ctx_verify returns one iff |signature| is a valid Ed25519ctx pure |
| 98 |
// signature, by |public_key| of |message_len| bytes from |message|, and |
| 99 |
// |context_len| bytes from |context|. |context_len| must be greater than zero, |
| 100 |
// but no more than 255. It returns zero otherwise. |
| 101 |
OPENSSL_EXPORT int ED25519ctx_verify(const uint8_t *message, size_t message_len, |
| 102 |
const uint8_t signature[ED25519_SIGNATURE_LEN], |
| 103 |
const uint8_t public_key[ED25519_PUBLIC_KEY_LEN], |
| 104 |
const uint8_t *context, size_t context_len); |
| 105 |
|
| 106 |
// ED25519ph_sign sets |out_sig| to be a Ed25519ph (RFC 8032) / HashEdDSA |
| 107 |
// signature of |message_len| bytes from |message| using |private_key|, and the |
| 108 |
// provided |context_len| bytes for |context|. |context_len| may be zero, but no |
| 109 |
// more than 255. It returns one on success or zero on failure. |
| 110 |
OPENSSL_EXPORT int ED25519ph_sign( |
| 111 |
uint8_t out_sig[ED25519_SIGNATURE_LEN], |
| 112 |
const uint8_t *message, size_t message_len, |
| 113 |
const uint8_t private_key[ED25519_PRIVATE_KEY_LEN], |
| 114 |
const uint8_t *context, size_t context_len); |
| 115 |
|
| 116 |
// ED25519ph_verify returns one iff |signature| is a valid Ed25519ph (RFC 8032) |
| 117 |
// / HashEdDSA signature, by |public_key| of |message_len| bytes from |message|, |
| 118 |
// and |context_len| bytes from |context|. |context_len| may be zero, but no |
| 119 |
// more than 255. It returns zero otherwise. |
| 120 |
OPENSSL_EXPORT int ED25519ph_verify(const uint8_t *message, size_t message_len, |
| 121 |
const uint8_t signature[ED25519_SIGNATURE_LEN], |
| 122 |
const uint8_t public_key[ED25519_PUBLIC_KEY_LEN], |
| 123 |
const uint8_t *context, size_t context_len); |
| 124 |
|
| 125 |
// ED25519ph_sign_digest sets |out_sig| to be a Ed25519ph (RFC 8032) / HashEdDSA |
| 126 |
// signature of a pre-computed SHA-512 message digest |digest| using |
| 127 |
// |private_key|, and the provided |context_len| bytes for |context|. |
| 128 |
// |context_len| may be zero, but no more than 255. |
| 129 |
// It returns one on success or zero on failure. |
| 130 |
OPENSSL_EXPORT int ED25519ph_sign_digest( |
| 131 |
uint8_t out_sig[ED25519_SIGNATURE_LEN], |
| 132 |
const uint8_t digest[64], |
| 133 |
const uint8_t private_key[ED25519_PRIVATE_KEY_LEN], |
| 134 |
const uint8_t *context, size_t context_len); |
| 135 |
|
| 136 |
// ED25519ph_verify_digest returns one iff |signature| is a valid Ed25519ph (RFC |
| 137 |
// 8032) / HashEdDSA signature, by |public_key| of a pre-computed SHA-512 |
| 138 |
// message digest |digest|, and |context_len| bytes from |context|. |
| 139 |
// |context_len| may be zero, but no more than 255. |
| 140 |
// It returns zero otherwise. |
| 141 |
OPENSSL_EXPORT int ED25519ph_verify_digest(const uint8_t digest[64], |
| 142 |
const uint8_t signature[ED25519_SIGNATURE_LEN], |
| 143 |
const uint8_t public_key[ED25519_PUBLIC_KEY_LEN], |
| 144 |
const uint8_t *context, size_t context_len); |
| 145 |
|
| 146 |
// ED25519_keypair_from_seed calculates a public and private key from an |
| 147 |
// Ed25519 “seed”. Seed values are not exposed by this API (although they |
| 148 |
// happen to be the first 32 bytes of a private key) so this function is for |
| 149 |
// interoperating with systems that may store just a seed instead of a full |
| 150 |
// private key. |
| 151 |
OPENSSL_EXPORT void ED25519_keypair_from_seed(uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN], |
| 152 |
uint8_t out_private_key[ED25519_PRIVATE_KEY_LEN], |
| 153 |
const uint8_t seed[ED25519_SEED_LEN]); |
| 154 |
|
| 155 |
|
| 156 |
// SPAKE2. |
| 157 |
// |
| 158 |
// SPAKE2 is a password-authenticated key-exchange. It allows two parties, |
| 159 |
// who share a low-entropy secret (i.e. password), to agree on a shared key. |
| 160 |
// An attacker can only make one guess of the password per execution of the |
| 161 |
// protocol. |
| 162 |
// |
| 163 |
// See https://tools.ietf.org/html/draft-irtf-cfrg-spake2-02. |
| 164 |
|
| 165 |
// spake2_role_t enumerates the different “roles” in SPAKE2. The protocol |
| 166 |
// requires that the symmetry of the two parties be broken so one participant |
| 167 |
// must be “Alice” and the other be “Bob”. |
| 168 |
enum spake2_role_t { |
| 169 |
spake2_role_alice, |
| 170 |
spake2_role_bob |
| 171 |
}; |
| 172 |
|
| 173 |
// SPAKE2_CTX_new creates a new |SPAKE2_CTX| (which can only be used for a |
| 174 |
// single execution of the protocol). SPAKE2 requires the symmetry of the two |
| 175 |
// parties to be broken which is indicated via |my_role| – each party must pass |
| 176 |
// a different value for this argument. |
| 177 |
// |
| 178 |
// The |my_name| and |their_name| arguments allow optional, opaque names to be |
| 179 |
// bound into the protocol. For example MAC addresses, hostnames, usernames |
| 180 |
// etc. These values are not exposed and can avoid context-confusion attacks |
| 181 |
// when a password is shared between several devices. |
| 182 |
OPENSSL_EXPORT SPAKE2_CTX *SPAKE2_CTX_new( |
| 183 |
enum spake2_role_t my_role, |
| 184 |
const uint8_t *my_name, size_t my_name_len, |
| 185 |
const uint8_t *their_name, size_t their_name_len); |
| 186 |
|
| 187 |
// SPAKE2_CTX_free frees |ctx| and all the resources that it has allocated. |
| 188 |
OPENSSL_EXPORT void SPAKE2_CTX_free(SPAKE2_CTX *ctx); |
| 189 |
|
| 190 |
// SPAKE2_MAX_MSG_SIZE is the maximum size of a SPAKE2 message. |
| 191 |
#define SPAKE2_MAX_MSG_SIZE 32 |
| 192 |
|
| 193 |
// SPAKE2_generate_msg generates a SPAKE2 message given |password|, writes |
| 194 |
// it to |out| and sets |*out_len| to the number of bytes written. |
| 195 |
// |
| 196 |
// At most |max_out_len| bytes are written to |out| and, in order to ensure |
| 197 |
// success, |max_out_len| should be at least |SPAKE2_MAX_MSG_SIZE| bytes. |
| 198 |
// |
| 199 |
// This function can only be called once for a given |SPAKE2_CTX|. |
| 200 |
// |
| 201 |
// It returns one on success and zero on error. |
| 202 |
OPENSSL_EXPORT int SPAKE2_generate_msg(SPAKE2_CTX *ctx, uint8_t *out, |
| 203 |
size_t *out_len, size_t max_out_len, |
| 204 |
const uint8_t *password, |
| 205 |
size_t password_len); |
| 206 |
|
| 207 |
// SPAKE2_MAX_KEY_SIZE is the maximum amount of key material that SPAKE2 will |
| 208 |
// produce. |
| 209 |
#define SPAKE2_MAX_KEY_SIZE 64 |
| 210 |
|
| 211 |
// SPAKE2_process_msg completes the SPAKE2 exchange given the peer's message in |
| 212 |
// |their_msg|, writes at most |max_out_key_len| bytes to |out_key| and sets |
| 213 |
// |*out_key_len| to the number of bytes written. |
| 214 |
// |
| 215 |
// The resulting keying material is suitable for: |
| 216 |
// - Using directly in a key-confirmation step: i.e. each side could |
| 217 |
// transmit a hash of their role, a channel-binding value and the key |
| 218 |
// material to prove to the other side that they know the shared key. |
| 219 |
// - Using as input keying material to HKDF to generate a variety of subkeys |
| 220 |
// for encryption etc. |
| 221 |
// |
| 222 |
// If |max_out_key_key| is smaller than the amount of key material generated |
| 223 |
// then the key is silently truncated. If you want to ensure that no truncation |
| 224 |
// occurs then |max_out_key| should be at least |SPAKE2_MAX_KEY_SIZE|. |
| 225 |
// |
| 226 |
// You must call |SPAKE2_generate_msg| on a given |SPAKE2_CTX| before calling |
| 227 |
// this function. On successful return, |ctx| is complete and calling |
| 228 |
// |SPAKE2_CTX_free| is the only acceptable operation on it. |
| 229 |
// |
| 230 |
// Returns one on success or zero on error. |
| 231 |
OPENSSL_EXPORT int SPAKE2_process_msg(SPAKE2_CTX *ctx, uint8_t *out_key, |
| 232 |
size_t *out_key_len, |
| 233 |
size_t max_out_key_len, |
| 234 |
const uint8_t *their_msg, |
| 235 |
size_t their_msg_len); |
| 236 |
|
| 237 |
|
| 238 |
#if defined(__cplusplus) |
| 239 |
} // extern C |
| 240 |
|
| 241 |
extern "C++" { |
| 242 |
|
| 243 |
BSSL_NAMESPACE_BEGIN |
| 244 |
|
| 245 |
BORINGSSL_MAKE_DELETER(SPAKE2_CTX, SPAKE2_CTX_free) |
| 246 |
|
| 247 |
BSSL_NAMESPACE_END |
| 248 |
|
| 249 |
} // extern C++ |
| 250 |
|
| 251 |
#endif |
| 252 |
|
| 253 |
#endif // OPENSSL_HEADER_CURVE25519_H |