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