Path: blob/main/crypto/openssl/demos/keyexch/x25519.c
34878 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 =73EVP_PKEY_new_raw_private_key_ex(libctx, "X25519", propq,74kat_privk_data,75sizeof(peer1_privk_data));76else77local_peer->privk = EVP_PKEY_Q_keygen(libctx, propq, "X25519");7879if (local_peer->privk == NULL) {80fprintf(stderr, "Could not load or generate private key\n");81goto end;82}8384/* Get public key corresponding to the private key */85if (EVP_PKEY_get_octet_string_param(local_peer->privk,86OSSL_PKEY_PARAM_PUB_KEY,87local_peer->pubk_data,88sizeof(local_peer->pubk_data),89&pubk_data_len) == 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 =130EVP_PKEY_new_raw_public_key_ex(libctx, "X25519", propq,131remote_peer_pubk_data, 32);132if (remote_peer_pubk == NULL) {133fprintf(stderr, "EVP_PKEY_new_raw_public_key_ex() failed\n");134goto end;135}136137/* Create key exchange context. */138ctx = EVP_PKEY_CTX_new_from_pkey(libctx, local_peer->privk, propq);139if (ctx == NULL) {140fprintf(stderr, "EVP_PKEY_CTX_new_from_pkey() failed\n");141goto end;142}143144/* Initialize derivation process. */145if (EVP_PKEY_derive_init(ctx) == 0) {146fprintf(stderr, "EVP_PKEY_derive_init() failed\n");147goto end;148}149150/* Configure each peer with the other peer's public key. */151if (EVP_PKEY_derive_set_peer(ctx, remote_peer_pubk) == 0) {152fprintf(stderr, "EVP_PKEY_derive_set_peer() failed\n");153goto end;154}155156/* Determine the secret length. */157if (EVP_PKEY_derive(ctx, NULL, &local_peer->secret_len) == 0) {158fprintf(stderr, "EVP_PKEY_derive() failed\n");159goto end;160}161162/*163* We are using X25519, so the secret generated will always be 32 bytes.164* However for exposition, the code below demonstrates a generic165* implementation for arbitrary lengths.166*/167if (local_peer->secret_len != 32) { /* unreachable */168fprintf(stderr, "Secret is always 32 bytes for X25519\n");169goto end;170}171172/* Allocate memory for shared secrets. */173local_peer->secret = OPENSSL_malloc(local_peer->secret_len);174if (local_peer->secret == NULL) {175fprintf(stderr, "Could not allocate memory for secret\n");176goto end;177}178179/* Derive the shared secret. */180if (EVP_PKEY_derive(ctx, local_peer->secret,181&local_peer->secret_len) == 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) == 0)215return 0;216217if (keyexch_x25519_before(libctx, use_kat ? peer2_privk_data : NULL,218&peer2) == 0)219return 0;220221/*222* Each peer uses the other peer's public key to perform key exchange.223* After this succeeds, each peer has the same secret in its224* PEER_DATA.225*/226if (keyexch_x25519_after(libctx, use_kat, &peer1, peer2.pubk_data) == 0)227return 0;228229if (keyexch_x25519_after(libctx, use_kat, &peer2, peer1.pubk_data) == 0)230return 0;231232/*233* Here we demonstrate the secrets are equal for exposition purposes.234*235* Although in practice you will generally not need to compare secrets236* produced through key exchange, if you do compare cryptographic secrets,237* always do so using a constant-time function such as CRYPTO_memcmp, never238* using memcmp(3).239*/240if (CRYPTO_memcmp(peer1.secret, peer2.secret, peer1.secret_len) != 0) {241fprintf(stderr, "Negotiated secrets do not match\n");242goto end;243}244245/* If we are doing the KAT, the secret should equal our reference result. */246if (use_kat && CRYPTO_memcmp(peer1.secret, expected_result,247peer1.secret_len) != 0) {248fprintf(stderr, "Did not get expected result\n");249goto end;250}251252ret = 1;253end:254/* The secrets are sensitive, so ensure they are erased before freeing. */255OPENSSL_clear_free(peer1.secret, peer1.secret_len);256OPENSSL_clear_free(peer2.secret, peer2.secret_len);257258EVP_PKEY_free(peer1.privk);259EVP_PKEY_free(peer2.privk);260OSSL_LIB_CTX_free(libctx);261return ret;262}263264int main(int argc, char **argv)265{266/* Test X25519 key exchange with known result. */267printf("Key exchange using known answer (deterministic):\n");268if (keyexch_x25519(1) == 0)269return EXIT_FAILURE;270271/* Test X25519 key exchange with random keys. */272printf("Key exchange using random keys:\n");273if (keyexch_x25519(0) == 0)274return EXIT_FAILURE;275276return EXIT_SUCCESS;277}278279280