blob: ca59c9a66d16cf17661554da85f863160598e2c4 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file dhm.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
6 * Copyright (C) 2009 Paul Bakker
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000021 */
Paul Bakker40e46942009-01-03 21:51:57 +000022#ifndef POLARSSL_DHM_H
23#define POLARSSL_DHM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker8e831ed2009-01-03 21:24:11 +000025#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker40e46942009-01-03 21:51:57 +000027#define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x0480
28#define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x0490
29#define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x04A0
30#define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x04B0
31#define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x04C0
32#define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x04D0
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34typedef struct
35{
36 int len; /*!< size(P) in chars */
37 mpi P; /*!< prime modulus */
38 mpi G; /*!< generator */
39 mpi X; /*!< secret value */
40 mpi GX; /*!< self = G^X mod P */
41 mpi GY; /*!< peer = G^Y mod P */
42 mpi K; /*!< key = GY^X mod P */
43 mpi RP; /*!< cached R^2 mod P */
44}
45dhm_context;
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51/**
52 * \brief Parse the ServerKeyExchange parameters
53 *
54 * \param ctx DHM context
55 * \param p &(start of input buffer)
56 * \param end end of buffer
57 *
Paul Bakker40e46942009-01-03 21:51:57 +000058 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000059 */
60int dhm_read_params( dhm_context *ctx,
61 unsigned char **p,
62 unsigned char *end );
63
64/**
65 * \brief Setup and write the ServerKeyExchange parameters
66 *
67 * \param ctx DHM context
68 * \param x_size private value size in bits
69 * \param output destination buffer
70 * \param olen number of chars written
71 * \param f_rng RNG function
72 * \param p_rng RNG parameter
73 *
74 * \note This function assumes that ctx->P and ctx->G
75 * have already been properly set (for example
76 * using mpi_read_string or mpi_read_binary).
77 *
Paul Bakker40e46942009-01-03 21:51:57 +000078 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
80int dhm_make_params( dhm_context *ctx, int s_size,
81 unsigned char *output, int *olen,
82 int (*f_rng)(void *), void *p_rng );
83
84/**
85 * \brief Import the peer's public value G^Y
86 *
87 * \param ctx DHM context
88 * \param input input buffer
89 * \param ilen size of buffer
90 *
Paul Bakker40e46942009-01-03 21:51:57 +000091 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000092 */
93int dhm_read_public( dhm_context *ctx,
94 unsigned char *input, int ilen );
95
96/**
97 * \brief Create own private value X and export G^X
98 *
99 * \param ctx DHM context
100 * \param x_size private value size in bits
101 * \param output destination buffer
102 * \param olen must be equal to ctx->P.len
103 * \param f_rng RNG function
104 * \param p_rng RNG parameter
105 *
Paul Bakker40e46942009-01-03 21:51:57 +0000106 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 */
108int dhm_make_public( dhm_context *ctx, int s_size,
109 unsigned char *output, int olen,
110 int (*f_rng)(void *), void *p_rng );
111
112/**
113 * \brief Derive and export the shared secret (G^Y)^X mod P
114 *
115 * \param ctx DHM context
116 * \param output destination buffer
117 * \param olen number of chars written
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_calc_secret( dhm_context *ctx,
122 unsigned char *output, int *olen );
123
124/*
125 * \brief Free the components of a DHM key
126 */
127void dhm_free( dhm_context *ctx );
128
129/**
130 * \brief Checkup routine
131 *
132 * \return 0 if successful, or 1 if the test failed
133 */
134int dhm_self_test( int verbose );
135
136#ifdef __cplusplus
137}
138#endif
139
140#endif