blob: d78bbeba5a4d9bc0d3366ee1c7fe7b5e1678383f [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57#ifndef OPENSSL_HEADER_DH_H
58#define OPENSSL_HEADER_DH_H
59
60#include <openssl/base.h>
61
Adam Langley95c29f32014-06-20 12:00:00 -070062#include <openssl/ex_data.h>
Adam Langley683d7bd2015-04-13 11:04:14 -070063#include <openssl/thread.h>
Adam Langley95c29f32014-06-20 12:00:00 -070064
65#if defined(__cplusplus)
66extern "C" {
67#endif
68
69
David Benjamin4512b792017-08-18 19:21:50 -040070// DH contains functions for performing Diffie-Hellman key agreement in
71// multiplicative groups.
Adam Langley95c29f32014-06-20 12:00:00 -070072
73
David Benjamin4512b792017-08-18 19:21:50 -040074// Allocation and destruction.
Adam Langley95c29f32014-06-20 12:00:00 -070075
David Benjamin4512b792017-08-18 19:21:50 -040076// DH_new returns a new, empty DH object or NULL on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070077OPENSSL_EXPORT DH *DH_new(void);
Adam Langley95c29f32014-06-20 12:00:00 -070078
David Benjamin4512b792017-08-18 19:21:50 -040079// DH_free decrements the reference count of |dh| and frees it if the reference
80// count drops to zero.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070081OPENSSL_EXPORT void DH_free(DH *dh);
Adam Langley95c29f32014-06-20 12:00:00 -070082
David Benjamin4512b792017-08-18 19:21:50 -040083// DH_up_ref increments the reference count of |dh| and returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070084OPENSSL_EXPORT int DH_up_ref(DH *dh);
Adam Langley95c29f32014-06-20 12:00:00 -070085
86
David Benjamin4512b792017-08-18 19:21:50 -040087// Properties.
David Benjamin5a915032016-08-09 11:04:24 -040088
David Benjamin4512b792017-08-18 19:21:50 -040089// DH_get0_key sets |*out_pub_key| and |*out_priv_key|, if non-NULL, to |dh|'s
90// public and private key, respectively. If |dh| is a public key, the private
91// key will be set to NULL.
David Benjamin5a915032016-08-09 11:04:24 -040092OPENSSL_EXPORT void DH_get0_key(const DH *dh, const BIGNUM **out_pub_key,
93 const BIGNUM **out_priv_key);
94
David Benjamin4512b792017-08-18 19:21:50 -040095// DH_get0_pqg sets |*out_p|, |*out_q|, and |*out_g|, if non-NULL, to |dh|'s p,
96// q, and g parameters, respectively.
David Benjamin5a915032016-08-09 11:04:24 -040097OPENSSL_EXPORT void DH_get0_pqg(const DH *dh, const BIGNUM **out_p,
98 const BIGNUM **out_q, const BIGNUM **out_g);
99
100
David Benjamin4512b792017-08-18 19:21:50 -0400101// Standard parameters.
Adam Langley95c29f32014-06-20 12:00:00 -0700102
David Benjamin4512b792017-08-18 19:21:50 -0400103// BN_get_rfc3526_prime_1536 sets |*ret| to the 1536-bit MODP group from RFC
104// 3526 and returns |ret|. If |ret| is NULL then a fresh |BIGNUM| is allocated
105// and returned. It returns NULL on allocation failure.
David Benjamin96e1a252016-08-10 13:38:51 -0400106OPENSSL_EXPORT BIGNUM *BN_get_rfc3526_prime_1536(BIGNUM *ret);
107
Adam Langley95c29f32014-06-20 12:00:00 -0700108
David Benjamin4512b792017-08-18 19:21:50 -0400109// Parameter generation.
Adam Langley95c29f32014-06-20 12:00:00 -0700110
111#define DH_GENERATOR_2 2
112#define DH_GENERATOR_5 5
113
David Benjamin4512b792017-08-18 19:21:50 -0400114// DH_generate_parameters_ex generates a suitable Diffie-Hellman group with a
115// prime that is |prime_bits| long and stores it in |dh|. The generator of the
116// group will be |generator|, which should be |DH_GENERATOR_2| unless there's a
117// good reason to use a different value. The |cb| argument contains a callback
118// function that will be called during the generation. See the documentation in
119// |bn.h| about this. In addition to the callback invocations from |BN|, |cb|
120// will also be called with |event| equal to three when the generation is
121// complete.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700122OPENSSL_EXPORT int DH_generate_parameters_ex(DH *dh, int prime_bits,
123 int generator, BN_GENCB *cb);
Adam Langley95c29f32014-06-20 12:00:00 -0700124
125
David Benjamin4512b792017-08-18 19:21:50 -0400126// Diffie-Hellman operations.
Adam Langley95c29f32014-06-20 12:00:00 -0700127
David Benjamin4512b792017-08-18 19:21:50 -0400128// DH_generate_key generates a new, random, private key and stores it in
129// |dh|. It returns one on success and zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700130OPENSSL_EXPORT int DH_generate_key(DH *dh);
Adam Langley95c29f32014-06-20 12:00:00 -0700131
David Benjamin4512b792017-08-18 19:21:50 -0400132// DH_compute_key calculates the shared key between |dh| and |peers_key| and
133// writes it as a big-endian integer into |out|, which must have |DH_size|
134// bytes of space. It returns the number of bytes written, or a negative number
135// on error.
Adam Langleyded93582014-07-31 15:23:51 -0700136OPENSSL_EXPORT int DH_compute_key(uint8_t *out, const BIGNUM *peers_key,
137 DH *dh);
Adam Langley95c29f32014-06-20 12:00:00 -0700138
139
David Benjamin4512b792017-08-18 19:21:50 -0400140// Utility functions.
Adam Langley95c29f32014-06-20 12:00:00 -0700141
David Benjamin4512b792017-08-18 19:21:50 -0400142// DH_size returns the number of bytes in the DH group's prime.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700143OPENSSL_EXPORT int DH_size(const DH *dh);
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin4512b792017-08-18 19:21:50 -0400145// DH_num_bits returns the minimum number of bits needed to represent the
146// absolute value of the DH group's prime.
Adam Langleya7997f12015-05-14 17:38:50 -0700147OPENSSL_EXPORT unsigned DH_num_bits(const DH *dh);
148
Adam Langley95c29f32014-06-20 12:00:00 -0700149#define DH_CHECK_P_NOT_PRIME 0x01
150#define DH_CHECK_P_NOT_SAFE_PRIME 0x02
151#define DH_CHECK_UNABLE_TO_CHECK_GENERATOR 0x04
152#define DH_CHECK_NOT_SUITABLE_GENERATOR 0x08
153#define DH_CHECK_Q_NOT_PRIME 0x10
154#define DH_CHECK_INVALID_Q_VALUE 0x20
155#define DH_CHECK_INVALID_J_VALUE 0x40
156
David Benjamin4512b792017-08-18 19:21:50 -0400157// These are compatibility defines.
Adam Langley7e6c4c82015-04-02 13:09:19 -0700158#define DH_NOT_SUITABLE_GENERATOR DH_CHECK_NOT_SUITABLE_GENERATOR
159#define DH_UNABLE_TO_CHECK_GENERATOR DH_CHECK_UNABLE_TO_CHECK_GENERATOR
160
David Benjamin4512b792017-08-18 19:21:50 -0400161// DH_check checks the suitability of |dh| as a Diffie-Hellman group. and sets
162// |DH_CHECK_*| flags in |*out_flags| if it finds any errors. It returns one if
163// |*out_flags| was successfully set and zero on error.
164//
165// Note: these checks may be quite computationally expensive.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700166OPENSSL_EXPORT int DH_check(const DH *dh, int *out_flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjamin47ebec12016-01-28 14:57:35 -0500168#define DH_CHECK_PUBKEY_TOO_SMALL 0x1
169#define DH_CHECK_PUBKEY_TOO_LARGE 0x2
170#define DH_CHECK_PUBKEY_INVALID 0x4
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjamin4512b792017-08-18 19:21:50 -0400172// DH_check_pub_key checks the suitability of |pub_key| as a public key for the
173// DH group in |dh| and sets |DH_CHECK_PUBKEY_*| flags in |*out_flags| if it
174// finds any errors. It returns one if |*out_flags| was successfully set and
175// zero on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700176OPENSSL_EXPORT int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key,
177 int *out_flags);
Adam Langley95c29f32014-06-20 12:00:00 -0700178
David Benjamin4512b792017-08-18 19:21:50 -0400179// DHparams_dup allocates a fresh |DH| and copies the parameters from |dh| into
180// it. It returns the new |DH| or NULL on error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700181OPENSSL_EXPORT DH *DHparams_dup(const DH *dh);
Adam Langley95c29f32014-06-20 12:00:00 -0700182
183
David Benjamin4512b792017-08-18 19:21:50 -0400184// ASN.1 functions.
Adam Langley95c29f32014-06-20 12:00:00 -0700185
David Benjamin4512b792017-08-18 19:21:50 -0400186// DH_parse_parameters decodes a DER-encoded DHParameter structure (PKCS #3)
187// from |cbs| and advances |cbs|. It returns a newly-allocated |DH| or NULL on
188// error.
David Benjamin34733152016-05-07 17:40:02 -0400189OPENSSL_EXPORT DH *DH_parse_parameters(CBS *cbs);
Adam Langley95c29f32014-06-20 12:00:00 -0700190
David Benjamin4512b792017-08-18 19:21:50 -0400191// DH_marshal_parameters marshals |dh| as a DER-encoded DHParameter structure
192// (PKCS #3) and appends the result to |cbb|. It returns one on success and zero
193// on error.
David Benjamin34733152016-05-07 17:40:02 -0400194OPENSSL_EXPORT int DH_marshal_parameters(CBB *cbb, const DH *dh);
Adam Langley95c29f32014-06-20 12:00:00 -0700195
196
David Benjamin4512b792017-08-18 19:21:50 -0400197// ex_data functions.
198//
199// See |ex_data.h| for details.
Adam Langley95c29f32014-06-20 12:00:00 -0700200
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700201OPENSSL_EXPORT int DH_get_ex_new_index(long argl, void *argp,
David Benjamin8a589332015-12-04 23:14:35 -0500202 CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -0400203 CRYPTO_EX_dup *dup_unused,
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700204 CRYPTO_EX_free *free_func);
205OPENSSL_EXPORT int DH_set_ex_data(DH *d, int idx, void *arg);
206OPENSSL_EXPORT void *DH_get_ex_data(DH *d, int idx);
Adam Langley95c29f32014-06-20 12:00:00 -0700207
208
David Benjamin4512b792017-08-18 19:21:50 -0400209// Deprecated functions.
Piotr Sikora8b0fe8c2016-03-18 18:22:54 -0700210
David Benjamin4512b792017-08-18 19:21:50 -0400211// DH_generate_parameters behaves like |DH_generate_parameters_ex|, which is
212// what you should use instead. It returns NULL on error, or a newly-allocated
213// |DH| on success. This function is provided for compatibility only.
Piotr Sikora8b0fe8c2016-03-18 18:22:54 -0700214OPENSSL_EXPORT DH *DH_generate_parameters(int prime_len, int generator,
215 void (*callback)(int, int, void *),
216 void *cb_arg);
217
David Benjamin4512b792017-08-18 19:21:50 -0400218// d2i_DHparams parses an ASN.1, DER encoded Diffie-Hellman parameters structure
219// from |len| bytes at |*inp|. If |ret| is not NULL then, on exit, a pointer to
220// the result is in |*ret|. Note that, even if |*ret| is already non-NULL on
221// entry, it will not be written to. Rather, a fresh |DH| is allocated and the
222// previous one is freed.
223//
224// On successful exit, |*inp| is advanced past the DER structure. It
225// returns the result or NULL on error.
226//
227// Use |DH_parse_parameters| instead.
David Benjamin34733152016-05-07 17:40:02 -0400228OPENSSL_EXPORT DH *d2i_DHparams(DH **ret, const unsigned char **inp, long len);
229
David Benjamin4512b792017-08-18 19:21:50 -0400230// i2d_DHparams marshals |in| to an ASN.1, DER structure. If |outp| is not NULL
231// then the result is written to |*outp| and |*outp| is advanced just past the
232// output. It returns the number of bytes in the result, whether written or
233// not, or a negative value on error.
234//
235// Use |DH_marshal_parameters| instead.
David Benjamin34733152016-05-07 17:40:02 -0400236OPENSSL_EXPORT int i2d_DHparams(const DH *in, unsigned char **outp);
237
Piotr Sikora8b0fe8c2016-03-18 18:22:54 -0700238
Adam Langley95c29f32014-06-20 12:00:00 -0700239struct dh_st {
Adam Langley95c29f32014-06-20 12:00:00 -0700240 BIGNUM *p;
241 BIGNUM *g;
David Benjamin4512b792017-08-18 19:21:50 -0400242 BIGNUM *pub_key; // g^x mod p
243 BIGNUM *priv_key; // x
Adam Langley95c29f32014-06-20 12:00:00 -0700244
David Benjamin4512b792017-08-18 19:21:50 -0400245 // priv_length contains the length, in bits, of the private value. If zero,
246 // the private value will be the same length as |p|.
Adam Langley95c29f32014-06-20 12:00:00 -0700247 unsigned priv_length;
Adam Langley683d7bd2015-04-13 11:04:14 -0700248
249 CRYPTO_MUTEX method_mont_p_lock;
Adam Langley95c29f32014-06-20 12:00:00 -0700250 BN_MONT_CTX *method_mont_p;
251
David Benjamin4512b792017-08-18 19:21:50 -0400252 // Place holders if we want to do X9.42 DH
Adam Langley95c29f32014-06-20 12:00:00 -0700253 BIGNUM *q;
254 BIGNUM *j;
255 unsigned char *seed;
256 int seedlen;
257 BIGNUM *counter;
258
259 int flags;
Adam Langley0da323a2015-05-15 12:49:30 -0700260 CRYPTO_refcount_t references;
Adam Langley95c29f32014-06-20 12:00:00 -0700261 CRYPTO_EX_DATA ex_data;
262};
263
264
265#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400266} // extern C
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700267
268extern "C++" {
269
270namespace bssl {
271
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700272BORINGSSL_MAKE_DELETER(DH, DH_free)
273
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700274} // namespace bssl
275
David Benjamin4512b792017-08-18 19:21:50 -0400276} // extern C++
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700277
Adam Langley95c29f32014-06-20 12:00:00 -0700278#endif
279
David Benjamin689be0f2015-02-11 15:55:26 -0500280#define DH_R_BAD_GENERATOR 100
281#define DH_R_INVALID_PUBKEY 101
Adam Langley95c29f32014-06-20 12:00:00 -0700282#define DH_R_MODULUS_TOO_LARGE 102
283#define DH_R_NO_PRIVATE_VALUE 103
David Benjamin34733152016-05-07 17:40:02 -0400284#define DH_R_DECODE_ERROR 104
285#define DH_R_ENCODE_ERROR 105
Adam Langley95c29f32014-06-20 12:00:00 -0700286
David Benjamin4512b792017-08-18 19:21:50 -0400287#endif // OPENSSL_HEADER_DH_H