blob: f3ee14f6500afc9d7bfebaf97ddfdb5967daae5f [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)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_DHM_H
24#define MBEDTLS_DHM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker314052f2011-08-15 09:07:52 +000026#include "bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakkerf3b86c12011-01-27 15:24:17 +000028/*
29 * DHM Error codes
30 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#define MBEDTLS_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */
32#define MBEDTLS_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */
33#define MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */
34#define MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */
35#define MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */
36#define MBEDTLS_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
37#define MBEDTLS_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020038#define MBEDTLS_ERR_DHM_ALLOC_FAILED -0x3400 /**< Allocation of memory failed. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Hanno Becker0e6dc842017-09-27 11:48:02 +010041#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
42#if defined(MBEDTLS_DEPRECATED_WARNING)
43#define MBEDTLS_DEPRECATED __attribute__((deprecated))
44#else
45#define MBEDTLS_DEPRECATED
46#endif
47#endif
48
Paul Bakkerf3b86c12011-01-27 15:24:17 +000049/**
Paul Bakkerda7e3f22012-09-28 07:18:17 +000050 * RFC 3526 defines a number of standardized Diffie-Hellman groups
51 * for IKE.
Paul Bakker29b64762012-09-25 09:36:44 +000052 * RFC 5114 defines a number of standardized Diffie-Hellman groups
Paul Bakkerda7e3f22012-09-28 07:18:17 +000053 * that can be used.
54 *
55 * Some are included here for convenience.
Paul Bakker29b64762012-09-25 09:36:44 +000056 *
57 * Included are:
Paul Bakkerda7e3f22012-09-28 07:18:17 +000058 * RFC 3526 3. 2048-bit MODP Group
59 * RFC 3526 4. 3072-bit MODP Group
Manuel Pégourié-Gonnard78931032015-07-03 17:06:39 +020060 * RFC 3526 5. 4096-bit MODP Group
Paul Bakkerda7e3f22012-09-28 07:18:17 +000061 * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
Hanno Becker0e6dc842017-09-27 11:48:02 +010062 * The constants with suffix "_p" denote the chosen prime moduli, while
63 * the constants with suffix "_g" denote the chosen generator
64 * of the associated prime field.
65 *
66 * All constants are represented as null-terminated strings containing the
67 * hexadecimal presentation of the respective numbers.
68 *
69 * \warning The origin of the primes in RFC 5114 is not documented and
70 * their use therefore constitutes a security risk!
71 *
72 * \deprecated The primes from RFC 5114 are superseded by the primes
73 * from RFC 3526 and RFC 7919 and should no longer be used.
74 * They will be removed in the next major revision.
Paul Bakker29b64762012-09-25 09:36:44 +000075 */
Paul Bakkerda7e3f22012-09-28 07:18:17 +000076
Hanno Becker0e6dc842017-09-27 11:48:02 +010077const char *mbedtls_dhm_rfc3526_modp_2048_p;
78const char *mbedtls_dhm_rfc3526_modp_2048_g;
79const char *mbedtls_dhm_rfc3526_modp_3072_p;
80const char *mbedtls_dhm_rfc3526_modp_3072_g;
81const char *mbedtls_dhm_rfc3526_modp_4096_p;
82const char *mbedtls_dhm_rfc3526_modp_4096_g;
Paul Bakkerda7e3f22012-09-28 07:18:17 +000083
Paul Bakkerda7e3f22012-09-28 07:18:17 +000084
Hanno Becker0e6dc842017-09-27 11:48:02 +010085#if !defined(MBEDTLS_DEPRECATED_REMOVED)
86MBEDTLS_DEPRECATED const char *mbedtls_dhm_rfc5114_modp_2048_p;
87MBEDTLS_DEPRECATED const char *mbedtls_dhm_rfc5114_modp_2048_g;
88#endif
Paul Bakkerda7e3f22012-09-28 07:18:17 +000089
Hanno Becker0e6dc842017-09-27 11:48:02 +010090/**
91 * \deprecated These macros are superseded by direct access to the corresponding
92 * global variables and will be removed in the next major revision.
93 */
94#if !defined(MBEDTLS_DEPRECATED_REMOVED)
95#define MBEDTLS_DHM_RFC5114_MODP_2048_P mbedtls_dhm_rfc5114_modp_2048_p
96#define MBEDTLS_DHM_RFC5114_MODP_2048_G mbedtls_dhm_rfc5114_modp_2048_g
97#define MBEDTLS_DHM_RFC3526_MODP_2048_P mbedtls_dhm_rfc3526_modp_2048_p
98#define MBEDTLS_DHM_RFC3526_MODP_2048_G mbedtls_dhm_rfc3526_modp_2048_g
99#define MBEDTLS_DHM_RFC3526_MODP_3072_P mbedtls_dhm_rfc3526_modp_3072_p
100#define MBEDTLS_DHM_RFC3526_MODP_3072_G mbedtls_dhm_rfc3526_modp_3072_g
101#define MBEDTLS_DHM_RFC3526_MODP_4096_P mbedtls_dhm_rfc3526_modp_4096_p
102#define MBEDTLS_DHM_RFC3526_MODP_4096_G mbedtls_dhm_rfc3526_modp_4096_g
103#endif
Paul Bakker29b64762012-09-25 09:36:44 +0000104
Paul Bakker407a0da2013-06-27 14:29:21 +0200105#ifdef __cplusplus
106extern "C" {
107#endif
108
Paul Bakker29b64762012-09-25 09:36:44 +0000109/**
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000110 * \brief DHM context structure
111 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000112typedef struct
113{
Paul Bakker23986e52011-04-24 08:57:21 +0000114 size_t len; /*!< size(P) in chars */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_mpi P; /*!< prime modulus */
116 mbedtls_mpi G; /*!< generator */
117 mbedtls_mpi X; /*!< secret value */
118 mbedtls_mpi GX; /*!< self = G^X mod P */
119 mbedtls_mpi GY; /*!< peer = G^Y mod P */
120 mbedtls_mpi K; /*!< key = GY^X mod P */
121 mbedtls_mpi RP; /*!< cached R^2 mod P */
122 mbedtls_mpi Vi; /*!< blinding value */
123 mbedtls_mpi Vf; /*!< un-blinding value */
124 mbedtls_mpi pX; /*!< previous X */
Paul Bakker5121ce52009-01-03 21:22:43 +0000125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126mbedtls_dhm_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200129 * \brief Initialize DHM context
130 *
131 * \param ctx DHM context to be initialized
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133void mbedtls_dhm_init( mbedtls_dhm_context *ctx );
Paul Bakker8f870b02014-06-20 13:32:38 +0200134
135/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * \brief Parse the ServerKeyExchange parameters
137 *
138 * \param ctx DHM context
139 * \param p &(start of input buffer)
140 * \param end end of buffer
141 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000146 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148/**
149 * \brief Setup and write the ServerKeyExchange parameters
150 *
151 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +0000152 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 * \param output destination buffer
154 * \param olen number of chars written
155 * \param f_rng RNG function
156 * \param p_rng RNG parameter
157 *
158 * \note This function assumes that ctx->P and ctx->G
159 * have already been properly set (for example
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 * using mbedtls_mpi_read_string or mbedtls_mpi_read_binary).
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000165 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000166 int (*f_rng)(void *, unsigned char *, size_t),
167 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169/**
170 * \brief Import the peer's public value G^Y
171 *
172 * \param ctx DHM context
173 * \param input input buffer
174 * \param ilen size of buffer
175 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000179 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181/**
182 * \brief Create own private value X and export G^X
183 *
184 * \param ctx DHM context
Paul Bakker84bef1d2012-04-20 13:42:02 +0000185 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 * \param output destination buffer
Simon Butcher2917b9e2016-05-25 00:59:37 +0100187 * \param olen must be at least equal to the size of P, ctx->len
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 * \param f_rng RNG function
189 * \param p_rng RNG parameter
190 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000194 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000195 int (*f_rng)(void *, unsigned char *, size_t),
196 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198/**
199 * \brief Derive and export the shared secret (G^Y)^X mod P
200 *
201 * \param ctx DHM context
202 * \param output destination buffer
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100203 * \param output_size size of the destination buffer
204 * \param olen on exit, holds the actual number of bytes written
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200205 * \param f_rng RNG function, for blinding purposes
206 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200209 *
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200210 * \note If non-NULL, f_rng is used to blind the input as
211 * countermeasure against timing attacks. Blinding is
212 * automatically used if and only if our secret value X is
213 * re-used and costs nothing otherwise, so it is recommended
214 * to always pass a non-NULL f_rng argument.
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100217 unsigned char *output, size_t output_size, size_t *olen,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200218 int (*f_rng)(void *, unsigned char *, size_t),
219 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Paul Bakker9a736322012-11-14 12:39:52 +0000221/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200222 * \brief Free and clear the components of a DHM key
223 *
224 * \param ctx DHM context to free and clear
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226void mbedtls_dhm_free( mbedtls_dhm_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_ASN1_PARSE_C)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200229/** \ingroup x509_module */
230/**
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200231 * \brief Parse DHM parameters in PEM or DER format
Paul Bakker40ce79f2013-09-15 17:43:54 +0200232 *
233 * \param dhm DHM context to be initialized
234 * \param dhmin input buffer
235 * \param dhminlen size of the buffer
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200236 * (including the terminating null byte for PEM data)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200237 *
238 * \return 0 if successful, or a specific DHM or PEM error code
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
Paul Bakker40ce79f2013-09-15 17:43:54 +0200241 size_t dhminlen );
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_FS_IO)
Paul Bakker40ce79f2013-09-15 17:43:54 +0200244/** \ingroup x509_module */
245/**
246 * \brief Load and parse DHM parameters
247 *
248 * \param dhm DHM context to be initialized
249 * \param path filename to read the DHM Parameters from
250 *
251 * \return 0 if successful, or a specific DHM or PEM error code
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path );
254#endif /* MBEDTLS_FS_IO */
255#endif /* MBEDTLS_ASN1_PARSE_C */
Paul Bakker40ce79f2013-09-15 17:43:54 +0200256
Paul Bakker5121ce52009-01-03 21:22:43 +0000257/**
258 * \brief Checkup routine
259 *
260 * \return 0 if successful, or 1 if the test failed
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_dhm_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264#ifdef __cplusplus
265}
266#endif
267
Paul Bakker9af723c2014-05-01 13:03:14 +0200268#endif /* dhm.h */