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