blob: 4206b8806ec647e84481091359641073d452e608 [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 Bakker8e831ed2009-01-03 21:24:11 +000030#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakkerf3b86c12011-01-27 15:24:17 +000032/*
33 * DHM Error codes
34 */
Paul Bakkerb5bf1762009-07-19 20:28:35 +000035#define POLARSSL_ERR_DHM_BAD_INPUT_DATA 0x0480
36#define POLARSSL_ERR_DHM_READ_PARAMS_FAILED 0x0490
37#define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED 0x04A0
38#define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED 0x04B0
39#define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED 0x04C0
40#define POLARSSL_ERR_DHM_CALC_SECRET_FAILED 0x04D0
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{
47 int len; /*!< size(P) in chars */
48 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 Bakker5121ce52009-01-03 21:22:43 +000092 unsigned char *output, int *olen,
93 int (*f_rng)(void *), void *p_rng );
94
95/**
96 * \brief Import the peer's public value G^Y
97 *
98 * \param ctx DHM context
99 * \param input input buffer
100 * \param ilen size of buffer
101 *
Paul Bakker40e46942009-01-03 21:51:57 +0000102 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 */
104int dhm_read_public( dhm_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000105 const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107/**
108 * \brief Create own private value X and export G^X
109 *
110 * \param ctx DHM context
111 * \param x_size private value size in bits
112 * \param output destination buffer
113 * \param olen must be equal to ctx->P.len
114 * \param f_rng RNG function
115 * \param p_rng RNG parameter
116 *
Paul Bakker40e46942009-01-03 21:51:57 +0000117 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000119int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 unsigned char *output, int olen,
121 int (*f_rng)(void *), void *p_rng );
122
123/**
124 * \brief Derive and export the shared secret (G^Y)^X mod P
125 *
126 * \param ctx DHM context
127 * \param output destination buffer
128 * \param olen number of chars written
129 *
Paul Bakker40e46942009-01-03 21:51:57 +0000130 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 */
132int dhm_calc_secret( dhm_context *ctx,
133 unsigned char *output, int *olen );
134
135/*
136 * \brief Free the components of a DHM key
137 */
138void dhm_free( dhm_context *ctx );
139
140/**
141 * \brief Checkup routine
142 *
143 * \return 0 if successful, or 1 if the test failed
144 */
145int dhm_self_test( int verbose );
146
147#ifdef __cplusplus
148}
149#endif
150
151#endif