blob: 8d01915ff517431b46fb3bcadc005bfda507b344 [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_DHM_H
25#define POLARSSL_DHM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker314052f2011-08-15 09:07:52 +000027#include "bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakkerf3b86c12011-01-27 15:24:17 +000029/*
30 * DHM Error codes
31 */
Paul Bakker9d781402011-05-09 16:17:09 +000032#define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */
33#define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */
34#define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */
35#define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */
Paul Bakkerc9b3e1e2012-04-26 18:59:23 +000036#define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */
Paul Bakker9d781402011-05-09 16:17:09 +000037#define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
Paul Bakker40ce79f2013-09-15 17:43:54 +020038#define POLARSSL_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */
39#define POLARSSL_ERR_DHM_MALLOC_FAILED -0x3400 /**< Allocation of memory failed. */
40#define POLARSSL_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakkerf3b86c12011-01-27 15:24:17 +000042/**
Paul Bakker911c5dc2014-05-28 11:48:00 +020043 * RFC 2409 defines a number of standardized Diffie-Hellman groups
44 * that can be used.
Paul Bakkerda7e3f22012-09-28 07:18:17 +000045 * RFC 3526 defines a number of standardized Diffie-Hellman groups
46 * for IKE.
Paul Bakker29b64762012-09-25 09:36:44 +000047 * RFC 5114 defines a number of standardized Diffie-Hellman groups
Paul Bakkerda7e3f22012-09-28 07:18:17 +000048 * that can be used.
49 *
50 * Some are included here for convenience.
Paul Bakker29b64762012-09-25 09:36:44 +000051 *
52 * Included are:
Paul Bakker911c5dc2014-05-28 11:48:00 +020053 * RFC 2409 6.2. 1024-bit MODP Group (Second Oakley Group)
Paul Bakkerda7e3f22012-09-28 07:18:17 +000054 * RFC 3526 3. 2048-bit MODP Group
55 * RFC 3526 4. 3072-bit MODP Group
56 * RFC 5114 2.1. 1024-bit MODP Group with 160-bit Prime Order Subgroup
57 * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
Paul Bakker29b64762012-09-25 09:36:44 +000058 */
Paul Bakker911c5dc2014-05-28 11:48:00 +020059#define POLARSSL_DHM_RFC2409_MODP_1024_P \
60 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
61 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
62 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
63 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
64 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \
65 "FFFFFFFFFFFFFFFF"
66
67#define POLARSSL_DHM_RFC2409_MODP_1024_G "02"
68
Paul Bakkerda7e3f22012-09-28 07:18:17 +000069#define POLARSSL_DHM_RFC3526_MODP_2048_P \
70 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
71 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
72 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
73 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
74 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
75 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
76 "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
77 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
78 "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
79 "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
80 "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
81
82#define POLARSSL_DHM_RFC3526_MODP_2048_G "02"
83
84#define POLARSSL_DHM_RFC3526_MODP_3072_P \
85 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
86 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
87 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
88 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
89 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
90 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
91 "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
92 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
93 "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
94 "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
95 "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \
96 "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \
97 "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \
98 "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \
99 "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \
100 "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
101
102#define POLARSSL_DHM_RFC3526_MODP_3072_G "02"
103
Paul Bakker29b64762012-09-25 09:36:44 +0000104#define POLARSSL_DHM_RFC5114_MODP_1024_P \
105 "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6" \
106 "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0" \
107 "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70" \
108 "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0" \
109 "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708" \
Paul Bakkera864f2e2012-09-26 08:29:20 +0000110 "DF1FB2BC2E4A4371"
Paul Bakker29b64762012-09-25 09:36:44 +0000111
112#define POLARSSL_DHM_RFC5114_MODP_1024_G \
113 "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F" \
114 "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213" \
115 "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1" \
116 "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A" \
117 "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24" \
Paul Bakkera864f2e2012-09-26 08:29:20 +0000118 "855E6EEB22B3B2E5"
Paul Bakker29b64762012-09-25 09:36:44 +0000119
120#define POLARSSL_DHM_RFC5114_MODP_2048_P \
121 "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \
122 "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \
123 "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \
124 "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \
125 "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \
126 "B3BF8A317091883681286130BC8985DB1602E714415D9330" \
127 "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \
128 "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \
129 "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \
130 "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \
Paul Bakkera864f2e2012-09-26 08:29:20 +0000131 "CF9DE5384E71B81C0AC4DFFE0C10E64F"
Paul Bakker29b64762012-09-25 09:36:44 +0000132
133#define POLARSSL_DHM_RFC5114_MODP_2048_G \
134 "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\
135 "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\
136 "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\
137 "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\
138 "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\
139 "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\
140 "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\
141 "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\
142 "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\
143 "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\
Paul Bakkera864f2e2012-09-26 08:29:20 +0000144 "81BC087F2A7065B384B890D3191F2BFA"
Paul Bakker29b64762012-09-25 09:36:44 +0000145
Paul Bakker407a0da2013-06-27 14:29:21 +0200146#ifdef __cplusplus
147extern "C" {
148#endif
149
Paul Bakker29b64762012-09-25 09:36:44 +0000150/**
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000151 * \brief DHM context structure
152 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000153typedef struct
154{
Paul Bakker23986e52011-04-24 08:57:21 +0000155 size_t len; /*!< size(P) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 mpi P; /*!< prime modulus */
157 mpi G; /*!< generator */
158 mpi X; /*!< secret value */
159 mpi GX; /*!< self = G^X mod P */
160 mpi GY; /*!< peer = G^Y mod P */
161 mpi K; /*!< key = GY^X mod P */
162 mpi RP; /*!< cached R^2 mod P */
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200163 mpi Vi; /*!< blinding value */
164 mpi Vf; /*!< un-blinding value */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200165 mpi pX; /*!< previous X */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166}
167dhm_context;
168
Paul Bakker5121ce52009-01-03 21:22:43 +0000169/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200170 * \brief Initialize DHM context
171 *
172 * \param ctx DHM context to be initialized
173 */
174void dhm_init( dhm_context *ctx );
175
176/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 * \brief Parse the ServerKeyExchange parameters
178 *
179 * \param ctx DHM context
180 * \param p &(start of input buffer)
181 * \param end end of buffer
182 *
Paul Bakker40e46942009-01-03 21:51:57 +0000183 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
185int dhm_read_params( dhm_context *ctx,
186 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000187 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189/**
190 * \brief Setup and write the ServerKeyExchange parameters
191 *
192 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +0000193 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 * \param output destination buffer
195 * \param olen number of chars written
196 * \param f_rng RNG function
197 * \param p_rng RNG parameter
198 *
199 * \note This function assumes that ctx->P and ctx->G
200 * have already been properly set (for example
201 * using mpi_read_string or mpi_read_binary).
202 *
Paul Bakker40e46942009-01-03 21:51:57 +0000203 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 */
Paul Bakkerf55ec082010-07-18 09:22:04 +0000205int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000206 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000207 int (*f_rng)(void *, unsigned char *, size_t),
208 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210/**
211 * \brief Import the peer's public value G^Y
212 *
213 * \param ctx DHM context
214 * \param input input buffer
215 * \param ilen size of buffer
216 *
Paul Bakker40e46942009-01-03 21:51:57 +0000217 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 */
219int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000220 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222/**
223 * \brief Create own private value X and export G^X
224 *
225 * \param ctx DHM context
Paul Bakker84bef1d2012-04-20 13:42:02 +0000226 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 * \param output destination buffer
228 * \param olen must be equal to ctx->P.len
229 * \param f_rng RNG function
230 * \param p_rng RNG parameter
231 *
Paul Bakker40e46942009-01-03 21:51:57 +0000232 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000234int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000235 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000236 int (*f_rng)(void *, unsigned char *, size_t),
237 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238
239/**
240 * \brief Derive and export the shared secret (G^Y)^X mod P
241 *
242 * \param ctx DHM context
243 * \param output destination buffer
Manuel Pégourié-Gonnard5af0e5b2014-06-25 13:03:50 +0200244 * \param olen on entry, must hold the size of the destination buffer
245 * on exit, holds the actual number of bytes written
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200246 * \param f_rng RNG function, for blinding purposes
247 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 *
Paul Bakker40e46942009-01-03 21:51:57 +0000249 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200250 *
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200251 * \note If non-NULL, f_rng is used to blind the input as
252 * countermeasure against timing attacks. Blinding is
253 * automatically used if and only if our secret value X is
254 * re-used and costs nothing otherwise, so it is recommended
255 * to always pass a non-NULL f_rng argument.
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 */
257int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200258 unsigned char *output, size_t *olen,
259 int (*f_rng)(void *, unsigned char *, size_t),
260 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
Paul Bakker9a736322012-11-14 12:39:52 +0000262/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200263 * \brief Free and clear the components of a DHM key
264 *
265 * \param ctx DHM context to free and clear
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 */
267void dhm_free( dhm_context *ctx );
268
Paul Bakker40ce79f2013-09-15 17:43:54 +0200269#if defined(POLARSSL_ASN1_PARSE_C)
270/** \ingroup x509_module */
271/**
272 * \brief Parse DHM parameters
273 *
274 * \param dhm DHM context to be initialized
275 * \param dhmin input buffer
276 * \param dhminlen size of the buffer
277 *
278 * \return 0 if successful, or a specific DHM or PEM error code
279 */
280int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
281 size_t dhminlen );
282
283#if defined(POLARSSL_FS_IO)
284/** \ingroup x509_module */
285/**
286 * \brief Load and parse DHM parameters
287 *
288 * \param dhm DHM context to be initialized
289 * \param path filename to read the DHM Parameters from
290 *
291 * \return 0 if successful, or a specific DHM or PEM error code
292 */
293int dhm_parse_dhmfile( dhm_context *dhm, const char *path );
294#endif /* POLARSSL_FS_IO */
295#endif /* POLARSSL_ASN1_PARSE_C */
296
Paul Bakker5121ce52009-01-03 21:22:43 +0000297/**
298 * \brief Checkup routine
299 *
300 * \return 0 if successful, or 1 if the test failed
301 */
302int dhm_self_test( int verbose );
303
304#ifdef __cplusplus
305}
306#endif
307
Paul Bakker9af723c2014-05-01 13:03:14 +0200308#endif /* dhm.h */