Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file dhm.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | fc8c436 | 2010-03-21 17:37:16 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 21 | #ifndef POLARSSL_DHM_H |
| 22 | #define POLARSSL_DHM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame] | 24 | #include "polarssl/bignum.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Paul Bakker | b5bf176 | 2009-07-19 20:28:35 +0000 | [diff] [blame] | 26 | #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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
| 33 | typedef 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 | } |
| 44 | dhm_context; |
| 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 57 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | */ |
| 59 | int dhm_read_params( dhm_context *ctx, |
| 60 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 61 | const unsigned char *end ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | |
| 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 77 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | */ |
| 79 | int 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 90 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | */ |
| 92 | int dhm_read_public( dhm_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 93 | const unsigned char *input, int ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | |
| 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 105 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | */ |
| 107 | int 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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 118 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 | */ |
| 120 | int dhm_calc_secret( dhm_context *ctx, |
| 121 | unsigned char *output, int *olen ); |
| 122 | |
| 123 | /* |
| 124 | * \brief Free the components of a DHM key |
| 125 | */ |
| 126 | void dhm_free( dhm_context *ctx ); |
| 127 | |
| 128 | /** |
| 129 | * \brief Checkup routine |
| 130 | * |
| 131 | * \return 0 if successful, or 1 if the test failed |
| 132 | */ |
| 133 | int dhm_self_test( int verbose ); |
| 134 | |
| 135 | #ifdef __cplusplus |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | #endif |