]>
Commit | Line | Data |
---|---|---|
ba702545 DSH |
1 | =pod |
2 | ||
3 | =head1 NAME | |
4 | ||
bb9ad09e | 5 | EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer, EVP_PKEY_derive - derive public key algorithm shared secret |
ba702545 DSH |
6 | |
7 | =head1 SYNOPSIS | |
8 | ||
9 | #include <openssl/evp.h> | |
10 | ||
11 | int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx); | |
12 | int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer); | |
13 | int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen); | |
14 | ||
15 | =head1 DESCRIPTION | |
16 | ||
17 | The EVP_PKEY_derive_init() function initializes a public key algorithm | |
18 | context using key B<pkey> for shared secret derivation. | |
19 | ||
20 | The EVP_PKEY_derive_set_peer() function sets the peer key: this will normally | |
21 | be a public key. | |
22 | ||
23 | The EVP_PKEY_derive() derives a shared secret using B<ctx>. | |
24 | If B<key> is B<NULL> then the maximum size of the output buffer is written to | |
25 | the B<keylen> parameter. If B<key> is not B<NULL> then before the call the | |
26 | B<keylen> parameter should contain the length of the B<key> buffer, if the call | |
27 | is successful the shared secret is written to B<key> and the amount of data | |
28 | written to B<keylen>. | |
29 | ||
30 | =head1 NOTES | |
31 | ||
32 | After the call to EVP_PKEY_derive_init() algorithm specific control | |
33 | operations can be performed to set any appropriate parameters for the | |
34 | operation. | |
35 | ||
36 | The function EVP_PKEY_derive() can be called more than once on the same | |
37 | context if several operations are performed using the same parameters. | |
38 | ||
39 | =head1 RETURN VALUES | |
40 | ||
41 | EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1 for success and 0 | |
42 | or a negative value for failure. In particular a return value of -2 | |
43 | indicates the operation is not supported by the public key algorithm. | |
44 | ||
40cb2be7 | 45 | =head1 EXAMPLES |
ba702545 DSH |
46 | |
47 | Derive shared secret (for example DH or EC keys): | |
48 | ||
49 | #include <openssl/evp.h> | |
50 | #include <openssl/rsa.h> | |
51 | ||
52 | EVP_PKEY_CTX *ctx; | |
9db6673e | 53 | ENGINE *eng; |
ba702545 DSH |
54 | unsigned char *skey; |
55 | size_t skeylen; | |
56 | EVP_PKEY *pkey, *peerkey; | |
9db6673e | 57 | /* NB: assumes pkey, eng, peerkey have been already set up */ |
ba702545 | 58 | |
9db6673e | 59 | ctx = EVP_PKEY_CTX_new(pkey, eng); |
ba702545 | 60 | if (!ctx) |
2947af32 | 61 | /* Error occurred */ |
ba702545 | 62 | if (EVP_PKEY_derive_init(ctx) <= 0) |
2947af32 | 63 | /* Error */ |
ba702545 | 64 | if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0) |
2947af32 | 65 | /* Error */ |
ba702545 DSH |
66 | |
67 | /* Determine buffer length */ | |
68 | if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0) | |
2947af32 | 69 | /* Error */ |
ba702545 DSH |
70 | |
71 | skey = OPENSSL_malloc(skeylen); | |
72 | ||
73 | if (!skey) | |
2947af32 | 74 | /* malloc failure */ |
1bc74519 | 75 | |
ba702545 | 76 | if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0) |
2947af32 | 77 | /* Error */ |
ba702545 DSH |
78 | |
79 | /* Shared secret is skey bytes written to buffer skey */ | |
80 | ||
81 | =head1 SEE ALSO | |
82 | ||
9b86974e RS |
83 | L<EVP_PKEY_CTX_new(3)>, |
84 | L<EVP_PKEY_encrypt(3)>, | |
85 | L<EVP_PKEY_decrypt(3)>, | |
86 | L<EVP_PKEY_sign(3)>, | |
87 | L<EVP_PKEY_verify(3)>, | |
88 | L<EVP_PKEY_verify_recover(3)>, | |
ba702545 DSH |
89 | |
90 | =head1 HISTORY | |
91 | ||
df75c2bf | 92 | These functions were added in OpenSSL 1.0.0. |
ba702545 | 93 | |
e2f92610 RS |
94 | =head1 COPYRIGHT |
95 | ||
1cb7eff4 | 96 | Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved. |
e2f92610 RS |
97 | |
98 | Licensed under the OpenSSL license (the "License"). You may not use | |
99 | this file except in compliance with the License. You can obtain a copy | |
100 | in the file LICENSE in the source distribution or at | |
101 | L<https://www.openssl.org/source/license.html>. | |
102 | ||
103 | =cut |