1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1.1.4 christos EVP_PKEY_derive_init, EVP_PKEY_derive_init_ex, 6 1.1.1.4 christos EVP_PKEY_derive_set_peer_ex, EVP_PKEY_derive_set_peer, EVP_PKEY_derive 7 1.1.1.4 christos - derive public key algorithm shared secret 8 1.1 christos 9 1.1 christos =head1 SYNOPSIS 10 1.1 christos 11 1.1 christos #include <openssl/evp.h> 12 1.1 christos 13 1.1 christos int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); 14 1.1.1.4 christos int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]); 15 1.1.1.4 christos int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer, 16 1.1.1.4 christos int validate_peer); 17 1.1 christos int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); 18 1.1 christos int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); 19 1.1 christos 20 1.1 christos =head1 DESCRIPTION 21 1.1 christos 22 1.1.1.4 christos EVP_PKEY_derive_init() initializes a public key algorithm context I<ctx> for 23 1.1.1.4 christos shared secret derivation using the algorithm given when the context was created 24 1.1.1.4 christos using L<EVP_PKEY_CTX_new(3)> or variants thereof. The algorithm is used to 25 1.1.1.4 christos fetch a B<EVP_KEYEXCH> method implicitly, see L<provider(7)/Implicit fetch> for 26 1.1.1.4 christos more information about implicit fetches. 27 1.1.1.4 christos 28 1.1.1.4 christos EVP_PKEY_derive_init_ex() is the same as EVP_PKEY_derive_init() but additionally 29 1.1.1.4 christos sets the passed parameters I<params> on the context before returning. 30 1.1.1.4 christos 31 1.1.1.4 christos EVP_PKEY_derive_set_peer_ex() sets the peer key: this will normally 32 1.1.1.4 christos be a public key. The I<validate_peer> will validate the public key if this value 33 1.1.1.4 christos is non zero. 34 1.1.1.4 christos 35 1.1.1.5 christos EVP_PKEY_derive_set_peer() is similar to EVP_PKEY_derive_set_peer_ex() with 36 1.1.1.4 christos I<validate_peer> set to 1. 37 1.1.1.4 christos 38 1.1.1.4 christos EVP_PKEY_derive() derives a shared secret using I<ctx>. 39 1.1.1.4 christos If I<key> is NULL then the maximum size of the output buffer is written to the 40 1.1.1.4 christos I<keylen> parameter. If I<key> is not NULL then before the call the I<keylen> 41 1.1.1.4 christos parameter should contain the length of the I<key> buffer, if the call is 42 1.1.1.4 christos successful the shared secret is written to I<key> and the amount of data 43 1.1.1.4 christos written to I<keylen>. 44 1.1 christos 45 1.1 christos =head1 NOTES 46 1.1 christos 47 1.1.1.4 christos After the call to EVP_PKEY_derive_init(), algorithm 48 1.1.1.4 christos specific control operations can be performed to set any appropriate parameters 49 1.1.1.4 christos for the operation. 50 1.1 christos 51 1.1 christos The function EVP_PKEY_derive() can be called more than once on the same 52 1.1 christos context if several operations are performed using the same parameters. 53 1.1 christos 54 1.1 christos =head1 RETURN VALUES 55 1.1 christos 56 1.1.1.4 christos EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1 57 1.1.1.4 christos for success and 0 or a negative value for failure. 58 1.1.1.4 christos In particular a return value of -2 indicates the operation is not supported by 59 1.1.1.4 christos the public key algorithm. 60 1.1 christos 61 1.1.1.3 christos =head1 EXAMPLES 62 1.1 christos 63 1.1 christos Derive shared secret (for example DH or EC keys): 64 1.1 christos 65 1.1 christos #include <openssl/evp.h> 66 1.1 christos #include <openssl/rsa.h> 67 1.1 christos 68 1.1 christos EVP_PKEY_CTX *ctx; 69 1.1 christos ENGINE *eng; 70 1.1 christos unsigned char *skey; 71 1.1 christos size_t skeylen; 72 1.1 christos EVP_PKEY *pkey, *peerkey; 73 1.1 christos /* NB: assumes pkey, eng, peerkey have been already set up */ 74 1.1 christos 75 1.1 christos ctx = EVP_PKEY_CTX_new(pkey, eng); 76 1.1 christos if (!ctx) 77 1.1 christos /* Error occurred */ 78 1.1 christos if (EVP_PKEY_derive_init(ctx) <= 0) 79 1.1 christos /* Error */ 80 1.1 christos if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0) 81 1.1 christos /* Error */ 82 1.1 christos 83 1.1 christos /* Determine buffer length */ 84 1.1 christos if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0) 85 1.1 christos /* Error */ 86 1.1 christos 87 1.1 christos skey = OPENSSL_malloc(skeylen); 88 1.1 christos 89 1.1 christos if (!skey) 90 1.1 christos /* malloc failure */ 91 1.1 christos 92 1.1 christos if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0) 93 1.1 christos /* Error */ 94 1.1 christos 95 1.1 christos /* Shared secret is skey bytes written to buffer skey */ 96 1.1 christos 97 1.1 christos =head1 SEE ALSO 98 1.1 christos 99 1.1 christos L<EVP_PKEY_CTX_new(3)>, 100 1.1 christos L<EVP_PKEY_encrypt(3)>, 101 1.1 christos L<EVP_PKEY_decrypt(3)>, 102 1.1 christos L<EVP_PKEY_sign(3)>, 103 1.1 christos L<EVP_PKEY_verify(3)>, 104 1.1 christos L<EVP_PKEY_verify_recover(3)>, 105 1.1.1.4 christos L<EVP_KEYEXCH_fetch(3)> 106 1.1 christos 107 1.1 christos =head1 HISTORY 108 1.1 christos 109 1.1.1.4 christos The EVP_PKEY_derive_init(), EVP_PKEY_derive_set_peer() and EVP_PKEY_derive() 110 1.1.1.4 christos functions were originally added in OpenSSL 1.0.0. 111 1.1.1.4 christos 112 1.1.1.4 christos The EVP_PKEY_derive_init_ex() and EVP_PKEY_derive_set_peer_ex() functions were 113 1.1.1.4 christos added in OpenSSL 3.0. 114 1.1 christos 115 1.1 christos =head1 COPYRIGHT 116 1.1 christos 117 1.1.1.5 christos Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved. 118 1.1 christos 119 1.1.1.4 christos Licensed under the Apache License 2.0 (the "License"). You may not use 120 1.1 christos this file except in compliance with the License. You can obtain a copy 121 1.1 christos in the file LICENSE in the source distribution or at 122 1.1 christos L<https://www.openssl.org/source/license.html>. 123 1.1 christos 124 1.1 christos =cut 125