blob: 0c8dd55ed8fc0bccc1463260a299edc4011c59b1 [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/**
43 * \brief DHM context structure
44 */
Paul Bakker5121ce52009-01-03 21:22:43 +000045typedef struct
46{
Paul Bakker23986e52011-04-24 08:57:21 +000047 size_t len; /*!< size(P) in chars */
Paul Bakker5121ce52009-01-03 21:22:43 +000048 mpi P; /*!< prime modulus */
49 mpi G; /*!< generator */
50 mpi X; /*!< secret value */
51 mpi GX; /*!< self = G^X mod P */
52 mpi GY; /*!< peer = G^Y mod P */
53 mpi K; /*!< key = GY^X mod P */
54 mpi RP; /*!< cached R^2 mod P */
55}
56dhm_context;
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/**
63 * \brief Parse the ServerKeyExchange parameters
64 *
65 * \param ctx DHM context
66 * \param p &(start of input buffer)
67 * \param end end of buffer
68 *
Paul Bakker40e46942009-01-03 21:51:57 +000069 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000070 */
71int dhm_read_params( dhm_context *ctx,
72 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000073 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75/**
76 * \brief Setup and write the ServerKeyExchange parameters
77 *
78 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +000079 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +000080 * \param output destination buffer
81 * \param olen number of chars written
82 * \param f_rng RNG function
83 * \param p_rng RNG parameter
84 *
85 * \note This function assumes that ctx->P and ctx->G
86 * have already been properly set (for example
87 * using mpi_read_string or mpi_read_binary).
88 *
Paul Bakker40e46942009-01-03 21:51:57 +000089 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Paul Bakkerf55ec082010-07-18 09:22:04 +000091int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +000092 unsigned char *output, size_t *olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +000093 int (*f_rng)(void *, unsigned char *, size_t),
94 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96/**
97 * \brief Import the peer's public value G^Y
98 *
99 * \param ctx DHM context
100 * \param input input buffer
101 * \param ilen size of buffer
102 *
Paul Bakker40e46942009-01-03 21:51:57 +0000103 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
105int dhm_read_public( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000106 const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/**
109 * \brief Create own private value X and export G^X
110 *
111 * \param ctx DHM context
Paul Bakker84bef1d2012-04-20 13:42:02 +0000112 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 * \param output destination buffer
114 * \param olen must be equal to ctx->P.len
115 * \param f_rng RNG function
116 * \param p_rng RNG parameter
117 *
Paul Bakker40e46942009-01-03 21:51:57 +0000118 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000120int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker23986e52011-04-24 08:57:21 +0000121 unsigned char *output, size_t olen,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000122 int (*f_rng)(void *, unsigned char *, size_t),
123 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
126 * \brief Derive and export the shared secret (G^Y)^X mod P
127 *
128 * \param ctx DHM context
129 * \param output destination buffer
130 * \param olen number of chars written
131 *
Paul Bakker40e46942009-01-03 21:51:57 +0000132 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 */
134int dhm_calc_secret( dhm_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000135 unsigned char *output, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137/*
138 * \brief Free the components of a DHM key
139 */
140void dhm_free( dhm_context *ctx );
141
142/**
143 * \brief Checkup routine
144 *
145 * \return 0 if successful, or 1 if the test failed
146 */
147int dhm_self_test( int verbose );
148
149#ifdef __cplusplus
150}
151#endif
152
153#endif