blob: 726273d01f99c45d13771cce331ba6393223de85 [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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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 Bakker5121ce52009-01-03 21:22:43 +000041
Paul Bakkerf3b86c12011-01-27 15:24:17 +000042/**
Paul Bakker29b64762012-09-25 09:36:44 +000043 * RFC 5114 defines a number of standardized Diffie-Hellman groups
44 * that can be used. Some are included here for convenience.
45 *
46 * Included are:
47 * 2.1. 1024-bit MODP Group with 160-bit Prime Order Subgroup
48 * 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
49 */
50#define POLARSSL_DHM_RFC5114_MODP_1024_P \
51 "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6" \
52 "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0" \
53 "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70" \
54 "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0" \
55 "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708" \
56 "DF1FB2BC2E4A4371";
57
58#define POLARSSL_DHM_RFC5114_MODP_1024_G \
59 "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F" \
60 "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213" \
61 "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1" \
62 "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A" \
63 "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24" \
64 "855E6EEB22B3B2E5";
65
66#define POLARSSL_DHM_RFC5114_MODP_2048_P \
67 "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \
68 "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \
69 "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \
70 "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \
71 "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \
72 "B3BF8A317091883681286130BC8985DB1602E714415D9330" \
73 "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \
74 "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \
75 "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \
76 "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \
77 "CF9DE5384E71B81C0AC4DFFE0C10E64F";
78
79#define POLARSSL_DHM_RFC5114_MODP_2048_G \
80 "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\
81 "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\
82 "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\
83 "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\
84 "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\
85 "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\
86 "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\
87 "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\
88 "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\
89 "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\
90 "81BC087F2A7065B384B890D3191F2BFA";
91
92/**
Paul Bakkerf3b86c12011-01-27 15:24:17 +000093 * \brief DHM context structure
94 */
Paul Bakker5121ce52009-01-03 21:22:43 +000095typedef struct
96{
Paul Bakker23986e52011-04-24 08:57:21 +000097 size_t len; /*!< size(P) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000098 mpi P; /*!< prime modulus */
99 mpi G; /*!< generator */
100 mpi X; /*!< secret value */
101 mpi GX; /*!< self = G^X mod P */
102 mpi GY; /*!< peer = G^Y mod P */
103 mpi K; /*!< key = GY^X mod P */
104 mpi RP; /*!< cached R^2 mod P */
105}
106dhm_context;
107
108#ifdef __cplusplus
109extern "C" {
110#endif
111
112/**
113 * \brief Parse the ServerKeyExchange parameters
114 *
115 * \param ctx DHM context
116 * \param p &(start of input buffer)
117 * \param end end of buffer
118 *
Paul Bakker40e46942009-01-03 21:51:57 +0000119 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
121int dhm_read_params( dhm_context *ctx,
122 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000123 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
126 * \brief Setup and write the ServerKeyExchange parameters
127 *
128 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +0000129 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 * \param output destination buffer
131 * \param olen number of chars written
132 * \param f_rng RNG function
133 * \param p_rng RNG parameter
134 *
135 * \note This function assumes that ctx->P and ctx->G
136 * have already been properly set (for example
137 * using mpi_read_string or mpi_read_binary).
138 *
Paul Bakker40e46942009-01-03 21:51:57 +0000139 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Paul Bakkerf55ec082010-07-18 09:22:04 +0000141int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000142 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000143 int (*f_rng)(void *, unsigned char *, size_t),
144 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief Import the peer's public value G^Y
148 *
149 * \param ctx DHM context
150 * \param input input buffer
151 * \param ilen size of buffer
152 *
Paul Bakker40e46942009-01-03 21:51:57 +0000153 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
155int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000156 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
159 * \brief Create own private value X and export G^X
160 *
161 * \param ctx DHM context
Paul Bakker84bef1d2012-04-20 13:42:02 +0000162 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 * \param output destination buffer
164 * \param olen must be equal to ctx->P.len
165 * \param f_rng RNG function
166 * \param p_rng RNG parameter
167 *
Paul Bakker40e46942009-01-03 21:51:57 +0000168 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000170int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000171 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000172 int (*f_rng)(void *, unsigned char *, size_t),
173 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
176 * \brief Derive and export the shared secret (G^Y)^X mod P
177 *
178 * \param ctx DHM context
179 * \param output destination buffer
180 * \param olen number of chars written
181 *
Paul Bakker40e46942009-01-03 21:51:57 +0000182 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
184int dhm_calc_secret( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000185 unsigned char *output, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187/*
188 * \brief Free the components of a DHM key
189 */
190void dhm_free( dhm_context *ctx );
191
192/**
193 * \brief Checkup routine
194 *
195 * \return 0 if successful, or 1 if the test failed
196 */
197int dhm_self_test( int verbose );
198
199#ifdef __cplusplus
200}
201#endif
202
203#endif