xref: /freebsd/crypto/openssl/doc/man3/DH_generate_key.pod (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1=pod
2
3=head1 NAME
4
5DH_generate_key, DH_compute_key, DH_compute_key_padded - perform
6Diffie-Hellman key exchange
7
8=head1 SYNOPSIS
9
10 #include <openssl/dh.h>
11
12 int DH_generate_key(DH *dh);
13
14 int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
15
16 int DH_compute_key_padded(unsigned char *key, const BIGNUM *pub_key, DH *dh);
17
18=head1 DESCRIPTION
19
20DH_generate_key() performs the first step of a Diffie-Hellman key
21exchange by generating private and public DH values. By calling
22DH_compute_key() or DH_compute_key_padded(), these are combined with
23the other party's public value to compute the shared key.
24
25DH_generate_key() expects B<dh> to contain the shared parameters
26B<dh-E<gt>p> and B<dh-E<gt>g>. It generates a random private DH value
27unless B<dh-E<gt>priv_key> is already set, and computes the
28corresponding public value B<dh-E<gt>pub_key>, which can then be
29published.
30
31DH_compute_key() computes the shared secret from the private DH value
32in B<dh> and the other party's public value in B<pub_key> and stores
33it in B<key>. B<key> must point to B<DH_size(dh)> bytes of memory.
34The padding style is RFC 5246 (8.1.2) that strips leading zero bytes.
35It is not constant time due to the leading zero bytes being stripped.
36The return value should be considered public.
37
38DH_compute_key_padded() is similar but stores a fixed number of bytes.
39The padding style is NIST SP 800-56A (C.1) that retains leading zero bytes.
40It is constant time due to the leading zero bytes being retained.
41The return value should be considered public.
42
43=head1 RETURN VALUES
44
45DH_generate_key() returns 1 on success, 0 otherwise.
46
47DH_compute_key() returns the size of the shared secret on success, -1
48on error.
49
50DH_compute_key_padded() returns B<DH_size(dh)> on success, -1 on error.
51
52The error codes can be obtained by L<ERR_get_error(3)>.
53
54=head1 SEE ALSO
55
56L<DH_new(3)>, L<ERR_get_error(3)>, L<RAND_bytes(3)>, L<DH_size(3)>
57
58=head1 HISTORY
59
60DH_compute_key_padded() was added in OpenSSL 1.0.2.
61
62=head1 COPYRIGHT
63
64Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
65
66Licensed under the OpenSSL license (the "License").  You may not use
67this file except in compliance with the License.  You can obtain a copy
68in the file LICENSE in the source distribution or at
69L<https://www.openssl.org/source/license.html>.
70
71=cut
72