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