1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <stdio.h>
#include <string.h>
#include <openssl/core_names.h>
#include <openssl/evp.h>
/*
* This is a demonstration of key exchange using X25519.
*
* The variables beginning `peer1_` / `peer2_` are data which would normally be
* accessible to that peer.
*
* Ordinarily you would use random keys, which are demonstrated
* below when use_kat=0. A known answer test is demonstrated
* when use_kat=1.
*/
/* A property query used for selecting the X25519 implementation. */
static const char *propq = NULL;
static const unsigned char peer1_privk_data[32] = {
0x80, 0x5b, 0x30, 0x20, 0x25, 0x4a, 0x70, 0x2c,
0xad, 0xa9, 0x8d, 0x7d, 0x47, 0xf8, 0x1b, 0x20,
0x89, 0xd2, 0xf9, 0x14, 0xac, 0x92, 0x27, 0xf2,
0x10, 0x7e, 0xdb, 0x21, 0xbd, 0x73, 0x73, 0x5d
};
static const unsigned char peer2_privk_data[32] = {
0xf8, 0x84, 0x19, 0x69, 0x79, 0x13, 0x0d, 0xbd,
0xb1, 0x76, 0xd7, 0x0e, 0x7e, 0x0f, 0xb6, 0xf4,
0x8c, 0x4a, 0x8c, 0x5f, 0xd8, 0x15, 0x09, 0x0a,
0x71, 0x78, 0x74, 0x92, 0x0f, 0x85, 0xc8, 0x43
};
static const unsigned char expected_result[32] = {
0x19, 0x71, 0x26, 0x12, 0x74, 0xb5, 0xb1, 0xce,
0x77, 0xd0, 0x79, 0x24, 0xb6, 0x0a, 0x5c, 0x72,
0x0c, 0xa6, 0x56, 0xc0, 0x11, 0xeb, 0x43, 0x11,
0x94, 0x3b, 0x01, 0x45, 0xca, 0x19, 0xfe, 0x09
};
typedef struct peer_data_st {
const char *name; /* name of peer */
EVP_PKEY *privk; /* privk generated for peer */
unsigned char pubk_data[32]; /* generated pubk to send to other peer */
unsigned char *secret; /* allocated shared secret buffer */
size_t secret_len;
} PEER_DATA;
/*
* Prepare for X25519 key exchange. The public key to be sent to the remote peer
* is put in pubk_data, which should be a 32-byte buffer. Returns 1 on success.
*/
static int keyexch_x25519_before(
OSSL_LIB_CTX *libctx,
const unsigned char *kat_privk_data,
PEER_DATA *local_peer)
{
int ret = 0;
size_t pubk_data_len = 0;
/* Generate or load X25519 key for the peer */
if (kat_privk_data != NULL)
local_peer->privk =
EVP_PKEY_new_raw_private_key_ex(libctx, "X25519", propq,
kat_privk_data,
sizeof(peer1_privk_data));
else
local_peer->privk = EVP_PKEY_Q_keygen(libctx, propq, "X25519");
if (local_peer->privk == NULL) {
fprintf(stderr, "Could not load or generate private key\n");
goto end;
}
/* Get public key corresponding to the private key */
if (EVP_PKEY_get_octet_string_param(local_peer->privk,
OSSL_PKEY_PARAM_PUB_KEY,
local_peer->pubk_data,
sizeof(local_peer->pubk_data),
&pubk_data_len) == 0) {
fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");
goto end;
}
/* X25519 public keys are always 32 bytes */
if (pubk_data_len != 32) {
fprintf(stderr, "EVP_PKEY_get_octet_string_param() "
"yielded wrong length\n");
goto end;
}
ret = 1;
end:
if (ret == 0) {
EVP_PKEY_free(local_peer->privk);
local_peer->privk = NULL;
}
return ret;
}
/*
* Complete X25519 key exchange. remote_peer_pubk_data should be the 32 byte
* public key value received from the remote peer. On success, returns 1 and the
* secret is pointed to by *secret. The caller must free it.
*/
static int keyexch_x25519_after(
OSSL_LIB_CTX *libctx,
int use_kat,
PEER_DATA *local_peer,
const unsigned char *remote_peer_pubk_data)
{
int ret = 0;
EVP_PKEY *remote_peer_pubk = NULL;
EVP_PKEY_CTX *ctx = NULL;
local_peer->secret = NULL;
/* Load public key for remote peer. */
remote_peer_pubk =
EVP_PKEY_new_raw_public_key_ex(libctx, "X25519", propq,
remote_peer_pubk_data, 32);
if (remote_peer_pubk == NULL) {
fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");
goto end;
}
/* Create key exchange context. */
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, local_peer->privk, propq);
if (ctx == NULL) {
fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");
goto end;
}
/* Initialize derivation process. */
if (EVP_PKEY_derive_init(ctx) == 0) {
fprintf(stderr, "EVP_PKEY_derive_init() failed\n");
goto end;
}
/* Configure each peer with the other peer's public key. */
if (EVP_PKEY_derive_set_peer(ctx, remote_peer_pubk) == 0) {
fprintf(stderr, "EVP_PKEY_derive_set_peer() failed\n");
goto end;
}
/* Determine the secret length. */
if (EVP_PKEY_derive(ctx, NULL, &local_peer->secret_len) == 0) {
fprintf(stderr, "EVP_PKEY_derive() failed\n");
goto end;
}
/*
* We are using X25519, so the secret generated will always be 32 bytes.
* However for exposition, the code below demonstrates a generic
* implementation for arbitrary lengths.
*/
if (local_peer->secret_len != 32) { /* unreachable */
fprintf(stderr, "Secret is always 32 bytes for X25519\n");
goto end;
}
/* Allocate memory for shared secrets. */
local_peer->secret = OPENSSL_malloc(local_peer->secret_len);
if (local_peer->secret == NULL) {
fprintf(stderr, "Could not allocate memory for secret\n");
goto end;
}
/* Derive the shared secret. */
if (EVP_PKEY_derive(ctx, local_peer->secret,
&local_peer->secret_len) == 0) {
fprintf(stderr, "EVP_PKEY_derive() failed\n");
goto end;
}
printf("Shared secret (%s):\n", local_peer->name);
BIO_dump_indent_fp(stdout, local_peer->secret, local_peer->secret_len, 2);
putchar('\n');
ret = 1;
end:
EVP_PKEY_CTX_free(ctx);
EVP_PKEY_free(remote_peer_pubk);
if (ret == 0) {
OPENSSL_clear_free(local_peer->secret, local_peer->secret_len);
local_peer->secret = NULL;
}
return ret;
}
static int keyexch_x25519(int use_kat)
{
int ret = 0;
OSSL_LIB_CTX *libctx = NULL;
PEER_DATA peer1 = {"peer 1"}, peer2 = {"peer 2"};
/*
* Each peer generates its private key and sends its public key
* to the other peer. The private key is stored locally for
* later use.
*/
if (keyexch_x25519_before(libctx, use_kat ? peer1_privk_data : NULL,
&peer1) == 0)
return 0;
if (keyexch_x25519_before(libctx, use_kat ? peer2_privk_data : NULL,
&peer2) == 0)
return 0;
/*
* Each peer uses the other peer's public key to perform key exchange.
* After this succeeds, each peer has the same secret in its
* PEER_DATA.
*/
if (keyexch_x25519_after(libctx, use_kat, &peer1, peer2.pubk_data) == 0)
return 0;
if (keyexch_x25519_after(libctx, use_kat, &peer2, peer1.pubk_data) == 0)
return 0;
/*
* Here we demonstrate the secrets are equal for exposition purposes.
*
* Although in practice you will generally not need to compare secrets
* produced through key exchange, if you do compare cryptographic secrets,
* always do so using a constant-time function such as CRYPTO_memcmp, never
* using memcmp(3).
*/
if (CRYPTO_memcmp(peer1.secret, peer2.secret, peer1.secret_len) != 0) {
fprintf(stderr, "Negotiated secrets do not match\n");
goto end;
}
/* If we are doing the KAT, the secret should equal our reference result. */
if (use_kat && CRYPTO_memcmp(peer1.secret, expected_result,
peer1.secret_len) != 0) {
fprintf(stderr, "Did not get expected result\n");
goto end;
}
ret = 1;
end:
/* The secrets are sensitive, so ensure they are erased before freeing. */
OPENSSL_clear_free(peer1.secret, peer1.secret_len);
OPENSSL_clear_free(peer2.secret, peer2.secret_len);
EVP_PKEY_free(peer1.privk);
EVP_PKEY_free(peer2.privk);
OSSL_LIB_CTX_free(libctx);
return ret;
}
int main(int argc, char **argv)
{
/* Test X25519 key exchange with known result. */
printf("Key exchange using known answer (deterministic):
|