Path: blob/main/crypto/openssl/demos/keyexch/x25519.c
108657 views
/*1* Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <stdio.h>10#include <string.h>11#include <openssl/core_names.h>12#include <openssl/evp.h>1314/*15* This is a demonstration of key exchange using X25519.16*17* The variables beginning `peer1_` / `peer2_` are data which would normally be18* accessible to that peer.19*20* Ordinarily you would use random keys, which are demonstrated21* below when use_kat=0. A known answer test is demonstrated22* when use_kat=1.23*/2425/* A property query used for selecting the X25519 implementation. */26static const char *propq = NULL;2728static const unsigned char peer1_privk_data[32] = {290x80, 0x5b, 0x30, 0x20, 0x25, 0x4a, 0x70, 0x2c,300xad, 0xa9, 0x8d, 0x7d, 0x47, 0xf8, 0x1b, 0x20,310x89, 0xd2, 0xf9, 0x14, 0xac, 0x92, 0x27, 0xf2,320x10, 0x7e, 0xdb, 0x21, 0xbd, 0x73, 0x73, 0x5d33};3435static const unsigned char peer2_privk_data[32] = {360xf8, 0x84, 0x19, 0x69, 0x79, 0x13, 0x0d, 0xbd,370xb1, 0x76, 0xd7, 0x0e, 0x7e, 0x0f, 0xb6, 0xf4,380x8c, 0x4a, 0x8c, 0x5f, 0xd8, 0x15, 0x09, 0x0a,390x71, 0x78, 0x74, 0x92, 0x0f, 0x85, 0xc8, 0x4340};4142static const unsigned char expected_result[32] = {430x19, 0x71, 0x26, 0x12, 0x74, 0xb5, 0xb1, 0xce,440x77, 0xd0, 0x79, 0x24, 0xb6, 0x0a, 0x5c, 0x72,450x0c, 0xa6, 0x56, 0xc0, 0x11, 0xeb, 0x43, 0x11,460x94, 0x3b, 0x01, 0x45, 0xca, 0x19, 0xfe, 0x0947};4849typedef struct peer_data_st {50const char *name; /* name of peer */51EVP_PKEY *privk; /* privk generated for peer */52unsigned char pubk_data[32]; /* generated pubk to send to other peer */5354unsigned char *secret; /* allocated shared secret buffer */55size_t secret_len;56} PEER_DATA;5758/*59* Prepare for X25519 key exchange. The public key to be sent to the remote peer60* is put in pubk_data, which should be a 32-byte buffer. Returns 1 on success.61*/62static int keyexch_x25519_before(63OSSL_LIB_CTX *libctx,64const unsigned char *kat_privk_data,65PEER_DATA *local_peer)66{67int ret = 0;68size_t pubk_data_len = 0;6970/* Generate or load X25519 key for the peer */71if (kat_privk_data != NULL)72local_peer->privk = EVP_PKEY_new_raw_private_key_ex(libctx, "X25519", propq,73kat_privk_data,74sizeof(peer1_privk_data));75else76local_peer->privk = EVP_PKEY_Q_keygen(libctx, propq, "X25519");7778if (local_peer->privk == NULL) {79fprintf(stderr, "Could not load or generate private key\n");80goto end;81}8283/* Get public key corresponding to the private key */84if (EVP_PKEY_get_octet_string_param(local_peer->privk,85OSSL_PKEY_PARAM_PUB_KEY,86local_peer->pubk_data,87sizeof(local_peer->pubk_data),88&pubk_data_len)89== 0) {90fprintf(stderr, "EVP_PKEY_get_octet_string_param() failed\n");91goto end;92}9394/* X25519 public keys are always 32 bytes */95if (pubk_data_len != 32) {96fprintf(stderr, "EVP_PKEY_get_octet_string_param() "97"yielded wrong length\n");98goto end;99}100101ret = 1;102end:103if (ret == 0) {104EVP_PKEY_free(local_peer->privk);105local_peer->privk = NULL;106}107108return ret;109}110111/*112* Complete X25519 key exchange. remote_peer_pubk_data should be the 32 byte113* public key value received from the remote peer. On success, returns 1 and the114* secret is pointed to by *secret. The caller must free it.115*/116static int keyexch_x25519_after(117OSSL_LIB_CTX *libctx,118int use_kat,119PEER_DATA *local_peer,120const unsigned char *remote_peer_pubk_data)121{122int ret = 0;123EVP_PKEY *remote_peer_pubk = NULL;124EVP_PKEY_CTX *ctx = NULL;125126local_peer->secret = NULL;127128/* Load public key for remote peer. */129remote_peer_pubk = EVP_PKEY_new_raw_public_key_ex(libctx, "X25519", propq,130remote_peer_pubk_data, 32);131if (remote_peer_pubk == NULL) {132fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");133goto end;134}135136/* Create key exchange context. */137ctx = EVP_PKEY_CTX_new_from_pkey(libctx, local_peer->privk, propq);138if (ctx == NULL) {139fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");140goto end;141}142143/* Initialize derivation process. */144if (EVP_PKEY_derive_init(ctx) == 0) {145fprintf(stderr, "EVP_PKEY_derive_init() failed\n");146goto end;147}148149/* Configure each peer with the other peer's public key. */150if (EVP_PKEY_derive_set_peer(ctx, remote_peer_pubk) == 0) {151fprintf(stderr, "EVP_PKEY_derive_set_peer() failed\n");152goto end;153}154155/* Determine the secret length. */156if (EVP_PKEY_derive(ctx, NULL, &local_peer->secret_len) == 0) {157fprintf(stderr, "EVP_PKEY_derive() failed\n");158goto end;159}160161/*162* We are using X25519, so the secret generated will always be 32 bytes.163* However for exposition, the code below demonstrates a generic164* implementation for arbitrary lengths.165*/166if (local_peer->secret_len != 32) { /* unreachable */167fprintf(stderr, "Secret is always 32 bytes for X25519\n");168goto end;169}170171/* Allocate memory for shared secrets. */172local_peer->secret = OPENSSL_malloc(local_peer->secret_len);173if (local_peer->secret == NULL) {174fprintf(stderr, "Could not allocate memory for secret\n");175goto end;176}177178/* Derive the shared secret. */179if (EVP_PKEY_derive(ctx, local_peer->secret,180&local_peer->secret_len)181== 0) {182fprintf(stderr, "EVP_PKEY_derive() failed\n");183goto end;184}185186printf("Shared secret (%s):\n", local_peer->name);187BIO_dump_indent_fp(stdout, local_peer->secret, local_peer->secret_len, 2);188putchar('\n');189190ret = 1;191end:192EVP_PKEY_CTX_free(ctx);193EVP_PKEY_free(remote_peer_pubk);194if (ret == 0) {195OPENSSL_clear_free(local_peer->secret, local_peer->secret_len);196local_peer->secret = NULL;197}198199return ret;200}201202static int keyexch_x25519(int use_kat)203{204int ret = 0;205OSSL_LIB_CTX *libctx = NULL;206PEER_DATA peer1 = { "peer 1" }, peer2 = { "peer 2" };207208/*209* Each peer generates its private key and sends its public key210* to the other peer. The private key is stored locally for211* later use.212*/213if (keyexch_x25519_before(libctx, use_kat ? peer1_privk_data : NULL,214&peer1)215== 0)216return 0;217218if (keyexch_x25519_before(libctx, use_kat ? peer2_privk_data : NULL,219&peer2)220== 0)221return 0;222223/*224* Each peer uses the other peer's public key to perform key exchange.225* After this succeeds, each peer has the same secret in its226* PEER_DATA.227*/228if (keyexch_x25519_after(libctx, use_kat, &peer1, peer2.pubk_data) == 0)229return 0;230231if (keyexch_x25519_after(libctx, use_kat, &peer2, peer1.pubk_data) == 0)232return 0;233234/*235* Here we demonstrate the secrets are equal for exposition purposes.236*237* Although in practice you will generally not need to compare secrets238* produced through key exchange, if you do compare cryptographic secrets,239* always do so using a constant-time function such as CRYPTO_memcmp, never240* using memcmp(3).241*/242if (CRYPTO_memcmp(peer1.secret, peer2.secret, peer1.secret_len) != 0) {243fprintf(stderr, "Negotiated secrets do not match\n");244goto end;245}246247/* If we are doing the KAT, the secret should equal our reference result. */248if (use_kat && CRYPTO_memcmp(peer1.secret, expected_result, peer1.secret_len) != 0) {249fprintf(stderr, "Did not get expected result\n");250goto end;251}252253ret = 1;254end:255/* The secrets are sensitive, so ensure they are erased before freeing. */256OPENSSL_clear_free(peer1.secret, peer1.secret_len);257OPENSSL_clear_free(peer2.secret, peer2.secret_len);258259EVP_PKEY_free(peer1.privk);260EVP_PKEY_free(peer2.privk);261OSSL_LIB_CTX_free(libctx);262return ret;263}264265int main(int argc, char **argv)266{267/* Test X25519 key exchange with known result. */268printf("Key exchange using known answer (deterministic):\n");269if (keyexch_x25519(1) == 0)270return EXIT_FAILURE;271272/* Test X25519 key exchange with random keys. */273printf("Key exchange using random keys:\n");274if (keyexch_x25519(0) == 0)275return EXIT_FAILURE;276277return EXIT_SUCCESS;278}279280281