blob: ad0795fd79c29fa08ce279871637bb2fd6832093 [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 Bakkerb5bf1762009-07-19 20:28:35 +000032#define POLARSSL_ERR_DHM_BAD_INPUT_DATA 0x0480
33#define POLARSSL_ERR_DHM_READ_PARAMS_FAILED 0x0490
34#define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED 0x04A0
35#define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED 0x04B0
36#define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED 0x04C0
37#define POLARSSL_ERR_DHM_CALC_SECRET_FAILED 0x04D0
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39typedef struct
40{
41 int len; /*!< size(P) in chars */
42 mpi P; /*!< prime modulus */
43 mpi G; /*!< generator */
44 mpi X; /*!< secret value */
45 mpi GX; /*!< self = G^X mod P */
46 mpi GY; /*!< peer = G^Y mod P */
47 mpi K; /*!< key = GY^X mod P */
48 mpi RP; /*!< cached R^2 mod P */
49}
50dhm_context;
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/**
57 * \brief Parse the ServerKeyExchange parameters
58 *
59 * \param ctx DHM context
60 * \param p &(start of input buffer)
61 * \param end end of buffer
62 *
Paul Bakker40e46942009-01-03 21:51:57 +000063 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000064 */
65int dhm_read_params( dhm_context *ctx,
66 unsigned char **p,
Paul Bakkerff60ee62010-03-16 21:09:09 +000067 const unsigned char *end );
Paul Bakker5121ce52009-01-03 21:22:43 +000068
69/**
70 * \brief Setup and write the ServerKeyExchange parameters
71 *
72 * \param ctx DHM context
Paul Bakkerff7fe672010-07-18 09:45:05 +000073 * \param x_size private value size in bytes
Paul Bakker5121ce52009-01-03 21:22:43 +000074 * \param output destination buffer
75 * \param olen number of chars written
76 * \param f_rng RNG function
77 * \param p_rng RNG parameter
78 *
79 * \note This function assumes that ctx->P and ctx->G
80 * have already been properly set (for example
81 * using mpi_read_string or mpi_read_binary).
82 *
Paul Bakker40e46942009-01-03 21:51:57 +000083 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000084 */
Paul Bakkerf55ec082010-07-18 09:22:04 +000085int dhm_make_params( dhm_context *ctx, int x_size,
Paul Bakker5121ce52009-01-03 21:22:43 +000086 unsigned char *output, int *olen,
87 int (*f_rng)(void *), void *p_rng );
88
89/**
90 * \brief Import the peer's public value G^Y
91 *
92 * \param ctx DHM context
93 * \param input input buffer
94 * \param ilen size of buffer
95 *
Paul Bakker40e46942009-01-03 21:51:57 +000096 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
98int dhm_read_public( dhm_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +000099 const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/**
102 * \brief Create own private value X and export G^X
103 *
104 * \param ctx DHM context
105 * \param x_size private value size in bits
106 * \param output destination buffer
107 * \param olen must be equal to ctx->P.len
108 * \param f_rng RNG function
109 * \param p_rng RNG parameter
110 *
Paul Bakker40e46942009-01-03 21:51:57 +0000111 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 */
Paul Bakker37ca75d2011-01-06 12:28:03 +0000113int dhm_make_public( dhm_context *ctx, int x_size,
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 unsigned char *output, int olen,
115 int (*f_rng)(void *), void *p_rng );
116
117/**
118 * \brief Derive and export the shared secret (G^Y)^X mod P
119 *
120 * \param ctx DHM context
121 * \param output destination buffer
122 * \param olen number of chars written
123 *
Paul Bakker40e46942009-01-03 21:51:57 +0000124 * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
126int dhm_calc_secret( dhm_context *ctx,
127 unsigned char *output, int *olen );
128
129/*
130 * \brief Free the components of a DHM key
131 */
132void dhm_free( dhm_context *ctx );
133
134/**
135 * \brief Checkup routine
136 *
137 * \return 0 if successful, or 1 if the test failed
138 */
139int dhm_self_test( int verbose );
140
141#ifdef __cplusplus
142}
143#endif
144
145#endif