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 | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Diffie-Hellman-Merkle key exchange |
| 5 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 13 | * 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_DHM_H |
| 28 | #define POLARSSL_DHM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame] | 30 | #include "polarssl/bignum.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 32 | /* |
| 33 | * DHM Error codes |
| 34 | */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 35 | #define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */ |
| 36 | #define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */ |
| 37 | #define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */ |
| 38 | #define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */ |
| 39 | #define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Makeing of the public value failed. */ |
| 40 | #define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 42 | /** |
| 43 | * \brief DHM context structure |
| 44 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | typedef struct |
| 46 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 47 | size_t len; /*!< size(P) in chars */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | mpi P; /*!< prime modulus */ |
| 49 | mpi G; /*!< generator */ |
| 50 | mpi X; /*!< secret value */ |
| 51 | mpi GX; /*!< self = G^X mod P */ |
| 52 | mpi GY; /*!< peer = G^Y mod P */ |
| 53 | mpi K; /*!< key = GY^X mod P */ |
| 54 | mpi RP; /*!< cached R^2 mod P */ |
| 55 | } |
| 56 | dhm_context; |
| 57 | |
| 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
| 62 | /** |
| 63 | * \brief Parse the ServerKeyExchange parameters |
| 64 | * |
| 65 | * \param ctx DHM context |
| 66 | * \param p &(start of input buffer) |
| 67 | * \param end end of buffer |
| 68 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 69 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | */ |
| 71 | int dhm_read_params( dhm_context *ctx, |
| 72 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 73 | const unsigned char *end ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * \brief Setup and write the ServerKeyExchange parameters |
| 77 | * |
| 78 | * \param ctx DHM context |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 79 | * \param x_size private value size in bytes |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | * \param output destination buffer |
| 81 | * \param olen number of chars written |
| 82 | * \param f_rng RNG function |
| 83 | * \param p_rng RNG parameter |
| 84 | * |
| 85 | * \note This function assumes that ctx->P and ctx->G |
| 86 | * have already been properly set (for example |
| 87 | * using mpi_read_string or mpi_read_binary). |
| 88 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 89 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | */ |
Paul Bakker | f55ec08 | 2010-07-18 09:22:04 +0000 | [diff] [blame] | 91 | int dhm_make_params( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 92 | unsigned char *output, size_t *olen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | int (*f_rng)(void *), void *p_rng ); |
| 94 | |
| 95 | /** |
| 96 | * \brief Import the peer's public value G^Y |
| 97 | * |
| 98 | * \param ctx DHM context |
| 99 | * \param input input buffer |
| 100 | * \param ilen size of buffer |
| 101 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 102 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | */ |
| 104 | int dhm_read_public( dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 105 | const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * \brief Create own private value X and export G^X |
| 109 | * |
| 110 | * \param ctx DHM context |
| 111 | * \param x_size private value size in bits |
| 112 | * \param output destination buffer |
| 113 | * \param olen must be equal to ctx->P.len |
| 114 | * \param f_rng RNG function |
| 115 | * \param p_rng RNG parameter |
| 116 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 117 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | */ |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 119 | int dhm_make_public( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 120 | unsigned char *output, size_t olen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | int (*f_rng)(void *), void *p_rng ); |
| 122 | |
| 123 | /** |
| 124 | * \brief Derive and export the shared secret (G^Y)^X mod P |
| 125 | * |
| 126 | * \param ctx DHM context |
| 127 | * \param output destination buffer |
| 128 | * \param olen number of chars written |
| 129 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 130 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | */ |
| 132 | int dhm_calc_secret( dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 133 | unsigned char *output, size_t *olen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * \brief Free the components of a DHM key |
| 137 | */ |
| 138 | void dhm_free( dhm_context *ctx ); |
| 139 | |
| 140 | /** |
| 141 | * \brief Checkup routine |
| 142 | * |
| 143 | * \return 0 if successful, or 1 if the test failed |
| 144 | */ |
| 145 | int dhm_self_test( int verbose ); |
| 146 | |
| 147 | #ifdef __cplusplus |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | #endif |