blob: a2eb5d5dd5943bd6560764a0abdfff7304f8ce40 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file dhm.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_DHM_H
26#define POLARSSL_DHM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker8e831ed2009-01-03 21:24:11 +000028#include "polarssl/bignum.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakkerb5bf1762009-07-19 20:28:35 +000030#define POLARSSL_ERR_DHM_BAD_INPUT_DATA 0x0480
31#define POLARSSL_ERR_DHM_READ_PARAMS_FAILED 0x0490
32#define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED 0x04A0
33#define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED 0x04B0
34#define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED 0x04C0
35#define POLARSSL_ERR_DHM_CALC_SECRET_FAILED 0x04D0
Paul Bakker5121ce52009-01-03 21:22:43 +000036
37typedef struct
38{
39 int len; /*!< size(P) in chars */
40 mpi P; /*!< prime modulus */
41 mpi G; /*!< generator */
42 mpi X; /*!< secret value */
43 mpi GX; /*!< self = G^X mod P */
44 mpi GY; /*!< peer = G^Y mod P */
45 mpi K; /*!< key = GY^X mod P */
46 mpi RP; /*!< cached R^2 mod P */
47}
48dhm_context;
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54/**
55 * \brief Parse the ServerKeyExchange parameters
56 *
57 * \param ctx DHM context
58 * \param p &(start of input buffer)
59 * \param end end of buffer
60 *
Paul Bakker40e46942009-01-03 21:51:57 +000061 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000062 */
63int dhm_read_params( dhm_context *ctx,
64 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000065 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +000066
67/**
68 * \brief Setup and write the ServerKeyExchange parameters
69 *
70 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +000071 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +000072 * \param output destination buffer
73 * \param olen number of chars written
74 * \param f_rng RNG function
75 * \param p_rng RNG parameter
76 *
77 * \note This function assumes that ctx->P and ctx->G
78 * have already been properly set (for example
79 * using mpi_read_string or mpi_read_binary).
80 *
Paul Bakker40e46942009-01-03 21:51:57 +000081 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000082 */
Paul Bakkerf55ec082010-07-18 09:22:04 +000083int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker5121ce52009-01-03 21:22:43 +000084 unsigned char *output, int *olen,
85 int (*f_rng)(void *), void *p_rng );
86
87/**
88 * \brief Import the peer's public value G^Y
89 *
90 * \param ctx DHM context
91 * \param input input buffer
92 * \param ilen size of buffer
93 *
Paul Bakker40e46942009-01-03 21:51:57 +000094 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
96int dhm_read_public( dhm_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +000097 const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief Create own private value X and export G^X
101 *
102 * \param ctx DHM context
103 * \param x_size private value size in bits
104 * \param output destination buffer
105 * \param olen must be equal to ctx->P.len
106 * \param f_rng RNG function
107 * \param p_rng RNG parameter
108 *
Paul Bakker40e46942009-01-03 21:51:57 +0000109 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 */
111int dhm_make_public( dhm_context *ctx, int s_size,
112 unsigned char *output, int olen,
113 int (*f_rng)(void *), void *p_rng );
114
115/**
116 * \brief Derive and export the shared secret (G^Y)^X mod P
117 *
118 * \param ctx DHM context
119 * \param output destination buffer
120 * \param olen number of chars written
121 *
Paul Bakker40e46942009-01-03 21:51:57 +0000122 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
124int dhm_calc_secret( dhm_context *ctx,
125 unsigned char *output, int *olen );
126
127/*
128 * \brief Free the components of a DHM key
129 */
130void dhm_free( dhm_context *ctx );
131
132/**
133 * \brief Checkup routine
134 *
135 * \return 0 if successful, or 1 if the test failed
136 */
137int dhm_self_test( int verbose );
138
139#ifdef __cplusplus
140}
141#endif
142
143#endif