blob: ed39f8db42c46c0bcd7f17f681f6986df093dd2b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file dhm.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Diffie-Hellman-Merkle key exchange
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Hanno Beckera2f6b722017-09-28 10:33:29 +010022 *
23 * \warning The security of the DHM key exchange relies on the proper choice
24 * of prime modulus - optimally, it should be a safe prime. The usage
25 * of non-safe primes both decreases the difficulty of the underlying
26 * discrete logarithm problem and can lead to small subgroup attacks
27 * leaking private exponent bits when invalid public keys are used
28 * and not detected. This is especially relevant if the same DHM parameters
29 * are reused for multiple key exchanges as in static DHM, while the
30 * criticality of small-subgroup attacks is lower for ephemeral DHM.
31 *
32 * For performance reasons, the code does neither perform primality
33 * nor safe primality tests, nor the expensive checks for invalid
34 * subgroups.
35 *
36 * The possibility for the use of custom, non-safe primes in DHM
37 * is a deficiency in the TLS protocol that has been adressed only
38 * recently through the addition of the named group extension from
39 * RFC 7919, which however is not yet implemented in Mbed TLS.
40 *
41 * If possible, we recommend to use elliptic curve based key
42 * exchanges instead of DHM-based ones, because the former only
43 * accepts standardized groups.
44 *
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#ifndef MBEDTLS_DHM_H
47#define MBEDTLS_DHM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Paul Bakker314052f2011-08-15 09:07:52 +000049#include "bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakkerf3b86c12011-01-27 15:24:17 +000051/*
52 * DHM Error codes
53 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define MBEDTLS_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */
55#define MBEDTLS_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */
56#define MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */
57#define MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */
58#define MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */
59#define MBEDTLS_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
60#define MBEDTLS_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020061#define MBEDTLS_ERR_DHM_ALLOC_FAILED -0x3400 /**< Allocation of memory failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define MBEDTLS_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Hanno Becker0e6dc842017-09-27 11:48:02 +010064#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
65#if defined(MBEDTLS_DEPRECATED_WARNING)
66#define MBEDTLS_DEPRECATED __attribute__((deprecated))
67#else
68#define MBEDTLS_DEPRECATED
69#endif
70#endif
71
Paul Bakkerf3b86c12011-01-27 15:24:17 +000072/**
Hanno Beckerb2bad802017-09-27 11:49:31 +010073 * RFC 3526, RFC 5114 and RFC 7919 standardize a number of
74 * Diffie-Hellman groups, some of which are included here
75 * for use within the SSL/TLS module and the user's convenience
76 * when configuring the Diffie-Hellman parameters by hand
77 * through \c mbedtls_ssl_conf_dh_param.
Paul Bakker29b64762012-09-25 09:36:44 +000078 *
79 * Included are:
Hanno Beckerb2bad802017-09-27 11:49:31 +010080 * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
Paul Bakkerda7e3f22012-09-28 07:18:17 +000081 * RFC 3526 3. 2048-bit MODP Group
82 * RFC 3526 4. 3072-bit MODP Group
Manuel Pégourié-Gonnard78931032015-07-03 17:06:39 +020083 * RFC 3526 5. 4096-bit MODP Group
Hanno Beckerb2bad802017-09-27 11:49:31 +010084 * RFC 7919 A.1 ffdhe2048
85 * RFC 7919 A.2 ffdhe3072
86 * RFC 7919 A.3 ffdhe4096
87 * RFC 7919 A.4 ffdhe6144
88 * RFC 7919 A.5 ffdhe8192
89 *
Hanno Becker0e6dc842017-09-27 11:48:02 +010090 * The constants with suffix "_p" denote the chosen prime moduli, while
91 * the constants with suffix "_g" denote the chosen generator
92 * of the associated prime field.
93 *
94 * All constants are represented as null-terminated strings containing the
95 * hexadecimal presentation of the respective numbers.
96 *
97 * \warning The origin of the primes in RFC 5114 is not documented and
98 * their use therefore constitutes a security risk!
99 *
100 * \deprecated The primes from RFC 5114 are superseded by the primes
101 * from RFC 3526 and RFC 7919 and should no longer be used.
102 * They will be removed in the next major revision.
Hanno Beckerb1d4d1f2017-09-27 12:42:59 +0100103 *
104 * The primes from RFC 3526 and RFC 7919 have been generating by the following
105 * trust-worthy procedure:
106 * - Fix N in { 2048, 3072, 4096, 6144, 8192 } and consider the N-bit number
107 * the first and last 64 bits are all 1, and the remaining N - 128 bits of
108 * which are 0x7ff...ff.
109 * - Add the smallest multiple of the first N - 129 bits of the binary expansion
110 * of pi (for RFC 5236) or e (for RFC 7919) to this intermediate bit-string
111 * such that the resulting integer is a safe-prime.
112 * - The result is the respective RFC 3526 / 7919 prime, and the corresponding
113 * generator is always chosen to be 2 (which is a square for these prime,
114 * hence the corresponding subgroup has order (p-1)/2 and avoids leaking a
115 * bit in the private exponent).
116 *
117 * The above description can be validated using the
118 * the program programs/util/rfc_3526_7919_verify.
Paul Bakker29b64762012-09-25 09:36:44 +0000119 */
Paul Bakkerda7e3f22012-09-28 07:18:17 +0000120
Hanno Becker4c72b002017-09-27 16:06:22 +0100121const char * const mbedtls_dhm_rfc3526_modp_2048_p;
122const char * const mbedtls_dhm_rfc3526_modp_2048_g;
123const char * const mbedtls_dhm_rfc3526_modp_3072_p;
124const char * const mbedtls_dhm_rfc3526_modp_3072_g;
125const char * const mbedtls_dhm_rfc3526_modp_4096_p;
126const char * const mbedtls_dhm_rfc3526_modp_4096_g;
Paul Bakkerda7e3f22012-09-28 07:18:17 +0000127
Hanno Becker4c72b002017-09-27 16:06:22 +0100128const char * const mbedtls_dhm_rfc7919_ffdhe2048_p;
129const char * const mbedtls_dhm_rfc7919_ffdhe2048_g;
130const char * const mbedtls_dhm_rfc7919_ffdhe3072_p;
131const char * const mbedtls_dhm_rfc7919_ffdhe3072_g;
132const char * const mbedtls_dhm_rfc7919_ffdhe4096_p;
133const char * const mbedtls_dhm_rfc7919_ffdhe4096_g;
134const char * const mbedtls_dhm_rfc7919_ffdhe6144_p;
135const char * const mbedtls_dhm_rfc7919_ffdhe6144_g;
136const char * const mbedtls_dhm_rfc7919_ffdhe8192_p;
137const char * const mbedtls_dhm_rfc7919_ffdhe8192_g;
Paul Bakkerda7e3f22012-09-28 07:18:17 +0000138
Hanno Becker0e6dc842017-09-27 11:48:02 +0100139#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Hanno Becker4c72b002017-09-27 16:06:22 +0100140MBEDTLS_DEPRECATED const char * const mbedtls_dhm_rfc5114_modp_2048_p;
141MBEDTLS_DEPRECATED const char * const mbedtls_dhm_rfc5114_modp_2048_g;
Hanno Becker0e6dc842017-09-27 11:48:02 +0100142#endif
Paul Bakkerda7e3f22012-09-28 07:18:17 +0000143
Hanno Becker0e6dc842017-09-27 11:48:02 +0100144/**
145 * \deprecated These macros are superseded by direct access to the corresponding
146 * global variables and will be removed in the next major revision.
147 */
148#if !defined(MBEDTLS_DEPRECATED_REMOVED)
149#define MBEDTLS_DHM_RFC5114_MODP_2048_P mbedtls_dhm_rfc5114_modp_2048_p
150#define MBEDTLS_DHM_RFC5114_MODP_2048_G mbedtls_dhm_rfc5114_modp_2048_g
151#define MBEDTLS_DHM_RFC3526_MODP_2048_P mbedtls_dhm_rfc3526_modp_2048_p
152#define MBEDTLS_DHM_RFC3526_MODP_2048_G mbedtls_dhm_rfc3526_modp_2048_g
153#define MBEDTLS_DHM_RFC3526_MODP_3072_P mbedtls_dhm_rfc3526_modp_3072_p
154#define MBEDTLS_DHM_RFC3526_MODP_3072_G mbedtls_dhm_rfc3526_modp_3072_g
155#define MBEDTLS_DHM_RFC3526_MODP_4096_P mbedtls_dhm_rfc3526_modp_4096_p
156#define MBEDTLS_DHM_RFC3526_MODP_4096_G mbedtls_dhm_rfc3526_modp_4096_g
157#endif
Paul Bakker29b64762012-09-25 09:36:44 +0000158
Paul Bakker407a0da2013-06-27 14:29:21 +0200159#ifdef __cplusplus
160extern "C" {
161#endif
162
Paul Bakker29b64762012-09-25 09:36:44 +0000163/**
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000164 * \brief DHM context structure
165 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166typedef struct
167{
Paul Bakker23986e52011-04-24 08:57:21 +0000168 size_t len; /*!< size(P) in chars */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_mpi P; /*!< prime modulus */
170 mbedtls_mpi G; /*!< generator */
171 mbedtls_mpi X; /*!< secret value */
172 mbedtls_mpi GX; /*!< self = G^X mod P */
173 mbedtls_mpi GY; /*!< peer = G^Y mod P */
174 mbedtls_mpi K; /*!< key = GY^X mod P */
175 mbedtls_mpi RP; /*!< cached R^2 mod P */
176 mbedtls_mpi Vi; /*!< blinding value */
177 mbedtls_mpi Vf; /*!< un-blinding value */
178 mbedtls_mpi pX; /*!< previous X */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180mbedtls_dhm_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Paul Bakker5121ce52009-01-03 21:22:43 +0000182/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200183 * \brief Initialize DHM context
184 *
185 * \param ctx DHM context to be initialized
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187void mbedtls_dhm_init( mbedtls_dhm_context *ctx );
Paul Bakker8f870b02014-06-20 13:32:38 +0200188
189/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 * \brief Parse the ServerKeyExchange parameters
191 *
192 * \param ctx DHM context
Hanno Beckerf240ea02017-10-02 15:09:14 +0100193 * \param p On input, *p must be the start of the input buffer.
194 * On output, *p is updated to point to the end of the data
195 * that has been read. On success, this is the first byte
196 * past the end of the ServerKeyExchange parameters.
197 * On error, this is the point at which an error has been
198 * detected, which is usually not useful except to debug
199 * failures.
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 * \param end end of buffer
201 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000206 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208/**
209 * \brief Setup and write the ServerKeyExchange parameters
210 *
211 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +0000212 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 * \param output destination buffer
214 * \param olen number of chars written
215 * \param f_rng RNG function
216 * \param p_rng RNG parameter
217 *
Hanno Beckere7643242017-09-28 10:33:11 +0100218 * \note The destination buffer must be large enough to hold
Hanno Becker70da2c52017-10-02 15:02:59 +0100219 * the reduced binary presentation of the modulus, the generator
220 * and the public key, each wrapped with a 2-byte length field.
221 * It is the responsibility of the caller to ensure that enough
222 * space is available. Refer to \c mbedtls_mpi_size to computing
223 * the byte-size of an MPI.
Hanno Beckere7643242017-09-28 10:33:11 +0100224 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 * \note This function assumes that ctx->P and ctx->G
226 * have already been properly set (for example
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 * using mbedtls_mpi_read_string or mbedtls_mpi_read_binary).
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000232 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000233 int (*f_rng)(void *, unsigned char *, size_t),
234 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236/**
237 * \brief Import the peer's public value G^Y
238 *
239 * \param ctx DHM context
240 * \param input input buffer
241 * \param ilen size of buffer
242 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000246 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248/**
249 * \brief Create own private value X and export G^X
250 *
251 * \param ctx DHM context
Paul Bakker84bef1d2012-04-20 13:42:02 +0000252 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 * \param output destination buffer
Hanno Beckere7643242017-09-28 10:33:11 +0100254 * \param olen size of the destination buffer;
255 * must be at least equal to the size of P, ctx->len
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 * \param f_rng RNG function
257 * \param p_rng RNG parameter
258 *
Hanno Beckere7643242017-09-28 10:33:11 +0100259 * \note The destination buffer will always be fully written
260 * so as to contain a big-endian presentation of G^X mod P.
261 * If it is larger than ctx->len, it will accordingly be
262 * padded with zero-bytes in the beginning.
263 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000267 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000268 int (*f_rng)(void *, unsigned char *, size_t),
269 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270
271/**
272 * \brief Derive and export the shared secret (G^Y)^X mod P
273 *
274 * \param ctx DHM context
275 * \param output destination buffer
Hanno Beckere7643242017-09-28 10:33:11 +0100276 * \param output_size size of the destination buffer, must be at
277 * at least the size of ctx->len
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100278 * \param olen on exit, holds the actual number of bytes written
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200279 * \param f_rng RNG function, for blinding purposes
280 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200283 *
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200284 * \note If non-NULL, f_rng is used to blind the input as
285 * countermeasure against timing attacks. Blinding is
286 * automatically used if and only if our secret value X is
287 * re-used and costs nothing otherwise, so it is recommended
288 * to always pass a non-NULL f_rng argument.
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100291 unsigned char *output, size_t output_size, size_t *olen,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200292 int (*f_rng)(void *, unsigned char *, size_t),
293 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
Paul Bakker9a736322012-11-14 12:39:52 +0000295/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200296 * \brief Free and clear the components of a DHM key
297 *
298 * \param ctx DHM context to free and clear
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300void mbedtls_dhm_free( mbedtls_dhm_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_ASN1_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200303/** \ingroup x509_module */
304/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200305 * \brief Parse DHM parameters in PEM or DER format
Paul Bakker40ce79f2013-09-15 17:43:54 +0200306 *
307 * \param dhm DHM context to be initialized
308 * \param dhmin input buffer
309 * \param dhminlen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200310 * (including the terminating null byte for PEM data)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200311 *
312 * \return 0 if successful, or a specific DHM or PEM error code
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
Paul Bakker40ce79f2013-09-15 17:43:54 +0200315 size_t dhminlen );
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_FS_IO)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200318/** \ingroup x509_module */
319/**
320 * \brief Load and parse DHM parameters
321 *
322 * \param dhm DHM context to be initialized
323 * \param path filename to read the DHM Parameters from
324 *
325 * \return 0 if successful, or a specific DHM or PEM error code
326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path );
328#endif /* MBEDTLS_FS_IO */
329#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200330
Paul Bakker5121ce52009-01-03 21:22:43 +0000331/**
332 * \brief Checkup routine
333 *
334 * \return 0 if successful, or 1 if the test failed
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336int mbedtls_dhm_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338#ifdef __cplusplus
339}
340#endif
341
Paul Bakker9af723c2014-05-01 13:03:14 +0200342#endif /* dhm.h */