blob: 064472f3589ab96405f9b5b67149384037e1e616 [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 *
Paul Bakker407a0da2013-06-27 14:29:21 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_DHM_H
28#define POLARSSL_DHM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker314052f2011-08-15 09:07:52 +000030#include "bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkerf3b86c12011-01-27 15:24:17 +000032/*
33 * DHM Error codes
34 */
Paul Bakker9d781402011-05-09 16:17:09 +000035#define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */
36#define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */
37#define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */
38#define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */
Paul Bakkerc9b3e1e2012-04-26 18:59:23 +000039#define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */
Paul Bakker9d781402011-05-09 16:17:09 +000040#define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
Paul Bakker40ce79f2013-09-15 17:43:54 +020041#define POLARSSL_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */
42#define POLARSSL_ERR_DHM_MALLOC_FAILED -0x3400 /**< Allocation of memory failed. */
43#define POLARSSL_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Paul Bakkerf3b86c12011-01-27 15:24:17 +000045/**
Paul Bakker911c5dc2014-05-28 11:48:00 +020046 * RFC 2409 defines a number of standardized Diffie-Hellman groups
47 * that can be used.
Paul Bakkerda7e3f22012-09-28 07:18:17 +000048 * RFC 3526 defines a number of standardized Diffie-Hellman groups
49 * for IKE.
Paul Bakker29b64762012-09-25 09:36:44 +000050 * RFC 5114 defines a number of standardized Diffie-Hellman groups
Paul Bakkerda7e3f22012-09-28 07:18:17 +000051 * that can be used.
52 *
53 * Some are included here for convenience.
Paul Bakker29b64762012-09-25 09:36:44 +000054 *
55 * Included are:
Paul Bakker911c5dc2014-05-28 11:48:00 +020056 * RFC 2409 6.2. 1024-bit MODP Group (Second Oakley Group)
Paul Bakkerda7e3f22012-09-28 07:18:17 +000057 * RFC 3526 3. 2048-bit MODP Group
58 * RFC 3526 4. 3072-bit MODP Group
59 * RFC 5114 2.1. 1024-bit MODP Group with 160-bit Prime Order Subgroup
60 * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
Paul Bakker29b64762012-09-25 09:36:44 +000061 */
Paul Bakker911c5dc2014-05-28 11:48:00 +020062#define POLARSSL_DHM_RFC2409_MODP_1024_P \
63 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
64 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
65 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
66 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
67 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \
68 "FFFFFFFFFFFFFFFF"
69
70#define POLARSSL_DHM_RFC2409_MODP_1024_G "02"
71
Paul Bakkerda7e3f22012-09-28 07:18:17 +000072#define POLARSSL_DHM_RFC3526_MODP_2048_P \
73 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
74 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
75 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
76 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
77 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
78 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
79 "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
80 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
81 "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
82 "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
83 "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
84
85#define POLARSSL_DHM_RFC3526_MODP_2048_G "02"
86
87#define POLARSSL_DHM_RFC3526_MODP_3072_P \
88 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
89 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
90 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
91 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
92 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
93 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
94 "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
95 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
96 "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
97 "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
98 "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \
99 "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \
100 "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \
101 "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \
102 "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \
103 "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
104
105#define POLARSSL_DHM_RFC3526_MODP_3072_G "02"
106
Paul Bakker29b64762012-09-25 09:36:44 +0000107#define POLARSSL_DHM_RFC5114_MODP_1024_P \
108 "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6" \
109 "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0" \
110 "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70" \
111 "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0" \
112 "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708" \
Paul Bakkera864f2e2012-09-26 08:29:20 +0000113 "DF1FB2BC2E4A4371"
Paul Bakker29b64762012-09-25 09:36:44 +0000114
115#define POLARSSL_DHM_RFC5114_MODP_1024_G \
116 "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F" \
117 "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213" \
118 "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1" \
119 "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A" \
120 "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24" \
Paul Bakkera864f2e2012-09-26 08:29:20 +0000121 "855E6EEB22B3B2E5"
Paul Bakker29b64762012-09-25 09:36:44 +0000122
123#define POLARSSL_DHM_RFC5114_MODP_2048_P \
124 "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \
125 "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \
126 "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \
127 "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \
128 "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \
129 "B3BF8A317091883681286130BC8985DB1602E714415D9330" \
130 "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \
131 "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \
132 "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \
133 "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \
Paul Bakkera864f2e2012-09-26 08:29:20 +0000134 "CF9DE5384E71B81C0AC4DFFE0C10E64F"
Paul Bakker29b64762012-09-25 09:36:44 +0000135
136#define POLARSSL_DHM_RFC5114_MODP_2048_G \
137 "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\
138 "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\
139 "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\
140 "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\
141 "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\
142 "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\
143 "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\
144 "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\
145 "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\
146 "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\
Paul Bakkera864f2e2012-09-26 08:29:20 +0000147 "81BC087F2A7065B384B890D3191F2BFA"
Paul Bakker29b64762012-09-25 09:36:44 +0000148
Paul Bakker407a0da2013-06-27 14:29:21 +0200149#ifdef __cplusplus
150extern "C" {
151#endif
152
Paul Bakker29b64762012-09-25 09:36:44 +0000153/**
Paul Bakkerf3b86c12011-01-27 15:24:17 +0000154 * \brief DHM context structure
155 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000156typedef struct
157{
Paul Bakker23986e52011-04-24 08:57:21 +0000158 size_t len; /*!< size(P) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 mpi P; /*!< prime modulus */
160 mpi G; /*!< generator */
161 mpi X; /*!< secret value */
162 mpi GX; /*!< self = G^X mod P */
163 mpi GY; /*!< peer = G^Y mod P */
164 mpi K; /*!< key = GY^X mod P */
165 mpi RP; /*!< cached R^2 mod P */
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200166 mpi Vi; /*!< blinding value */
167 mpi Vf; /*!< un-blinding value */
Paul Bakkerd61cc3b2013-10-11 09:38:49 +0200168 mpi pX; /*!< previous X */
Paul Bakker5121ce52009-01-03 21:22:43 +0000169}
170dhm_context;
171
Paul Bakker5121ce52009-01-03 21:22:43 +0000172/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200173 * \brief Initialize DHM context
174 *
175 * \param ctx DHM context to be initialized
176 */
177void dhm_init( dhm_context *ctx );
178
179/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 * \brief Parse the ServerKeyExchange parameters
181 *
182 * \param ctx DHM context
183 * \param p &(start of input buffer)
184 * \param end end of buffer
185 *
Paul Bakker40e46942009-01-03 21:51:57 +0000186 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 */
188int dhm_read_params( dhm_context *ctx,
189 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000190 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192/**
193 * \brief Setup and write the ServerKeyExchange parameters
194 *
195 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +0000196 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 * \param output destination buffer
198 * \param olen number of chars written
199 * \param f_rng RNG function
200 * \param p_rng RNG parameter
201 *
202 * \note This function assumes that ctx->P and ctx->G
203 * have already been properly set (for example
204 * using mpi_read_string or mpi_read_binary).
205 *
Paul Bakker40e46942009-01-03 21:51:57 +0000206 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Paul Bakkerf55ec082010-07-18 09:22:04 +0000208int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000209 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000210 int (*f_rng)(void *, unsigned char *, size_t),
211 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213/**
214 * \brief Import the peer's public value G^Y
215 *
216 * \param ctx DHM context
217 * \param input input buffer
218 * \param ilen size of buffer
219 *
Paul Bakker40e46942009-01-03 21:51:57 +0000220 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 */
222int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000223 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
225/**
226 * \brief Create own private value X and export G^X
227 *
228 * \param ctx DHM context
Paul Bakker84bef1d2012-04-20 13:42:02 +0000229 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 * \param output destination buffer
231 * \param olen must be equal to ctx->P.len
232 * \param f_rng RNG function
233 * \param p_rng RNG parameter
234 *
Paul Bakker40e46942009-01-03 21:51:57 +0000235 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000237int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000238 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000239 int (*f_rng)(void *, unsigned char *, size_t),
240 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
242/**
243 * \brief Derive and export the shared secret (G^Y)^X mod P
244 *
245 * \param ctx DHM context
246 * \param output destination buffer
Manuel Pégourié-Gonnard5af0e5b2014-06-25 13:03:50 +0200247 * \param olen on entry, must hold the size of the destination buffer
248 * on exit, holds the actual number of bytes written
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200249 * \param f_rng RNG function, for blinding purposes
250 * \param p_rng RNG parameter
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 *
Paul Bakker40e46942009-01-03 21:51:57 +0000252 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Manuel Pégourié-Gonnard143b5022013-09-04 16:29:59 +0200253 *
Manuel Pégourié-Gonnard15d5de12013-09-17 11:34:11 +0200254 * \note If non-NULL, f_rng is used to blind the input as
255 * countermeasure against timing attacks. Blinding is
256 * automatically used if and only if our secret value X is
257 * re-used and costs nothing otherwise, so it is recommended
258 * to always pass a non-NULL f_rng argument.
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 */
260int dhm_calc_secret( dhm_context *ctx,
Manuel Pégourié-Gonnard2d627642013-09-04 14:22:07 +0200261 unsigned char *output, size_t *olen,
262 int (*f_rng)(void *, unsigned char *, size_t),
263 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
Paul Bakker9a736322012-11-14 12:39:52 +0000265/**
Paul Bakker8f870b02014-06-20 13:32:38 +0200266 * \brief Free and clear the components of a DHM key
267 *
268 * \param ctx DHM context to free and clear
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
270void dhm_free( dhm_context *ctx );
271
Paul Bakker40ce79f2013-09-15 17:43:54 +0200272#if defined(POLARSSL_ASN1_PARSE_C)
273/** \ingroup x509_module */
274/**
275 * \brief Parse DHM parameters
276 *
277 * \param dhm DHM context to be initialized
278 * \param dhmin input buffer
279 * \param dhminlen size of the buffer
280 *
281 * \return 0 if successful, or a specific DHM or PEM error code
282 */
283int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
284 size_t dhminlen );
285
286#if defined(POLARSSL_FS_IO)
287/** \ingroup x509_module */
288/**
289 * \brief Load and parse DHM parameters
290 *
291 * \param dhm DHM context to be initialized
292 * \param path filename to read the DHM Parameters from
293 *
294 * \return 0 if successful, or a specific DHM or PEM error code
295 */
296int dhm_parse_dhmfile( dhm_context *dhm, const char *path );
297#endif /* POLARSSL_FS_IO */
298#endif /* POLARSSL_ASN1_PARSE_C */
299
Paul Bakker5121ce52009-01-03 21:22:43 +0000300/**
301 * \brief Checkup routine
302 *
303 * \return 0 if successful, or 1 if the test failed
304 */
305int dhm_self_test( int verbose );
306
307#ifdef __cplusplus
308}
309#endif
310
Paul Bakker9af723c2014-05-01 13:03:14 +0200311#endif /* dhm.h */