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