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