| 1 | /** |
|---|
| 2 | * \file dhm.h |
|---|
| 3 | * |
|---|
| 4 | * \brief Diffie-Hellman-Merkle key exchange |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
|---|
| 7 | * |
|---|
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
|---|
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
|---|
| 10 | * |
|---|
| 11 | * All rights reserved. |
|---|
| 12 | * |
|---|
| 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. |
|---|
| 26 | */ |
|---|
| 27 | #ifndef POLARSSL_DHM_H |
|---|
| 28 | #define POLARSSL_DHM_H |
|---|
| 29 | |
|---|
| 30 | #include "bignum.h" |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | * DHM Error codes |
|---|
| 34 | */ |
|---|
| 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 /**< Making of the public value failed. */ |
|---|
| 40 | #define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */ |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * \brief DHM context structure |
|---|
| 44 | */ |
|---|
| 45 | typedef struct |
|---|
| 46 | { |
|---|
| 47 | size_t len; /*!< size(P) in chars */ |
|---|
| 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 | * |
|---|
| 69 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
|---|
| 70 | */ |
|---|
| 71 | int dhm_read_params( dhm_context *ctx, |
|---|
| 72 | unsigned char **p, |
|---|
| 73 | const unsigned char *end ); |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * \brief Setup and write the ServerKeyExchange parameters |
|---|
| 77 | * |
|---|
| 78 | * \param ctx DHM context |
|---|
| 79 | * \param x_size private value size in bytes |
|---|
| 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 | * |
|---|
| 89 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
|---|
| 90 | */ |
|---|
| 91 | int dhm_make_params( dhm_context *ctx, int x_size, |
|---|
| 92 | unsigned char *output, size_t *olen, |
|---|
| 93 | int (*f_rng)(void *, unsigned char *, size_t), |
|---|
| 94 | void *p_rng ); |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * \brief Import the peer's public value G^Y |
|---|
| 98 | * |
|---|
| 99 | * \param ctx DHM context |
|---|
| 100 | * \param input input buffer |
|---|
| 101 | * \param ilen size of buffer |
|---|
| 102 | * |
|---|
| 103 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
|---|
| 104 | */ |
|---|
| 105 | int dhm_read_public( dhm_context *ctx, |
|---|
| 106 | const unsigned char *input, size_t ilen ); |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | * \brief Create own private value X and export G^X |
|---|
| 110 | * |
|---|
| 111 | * \param ctx DHM context |
|---|
| 112 | * \param x_size private value size in bytes |
|---|
| 113 | * \param output destination buffer |
|---|
| 114 | * \param olen must be equal to ctx->P.len |
|---|
| 115 | * \param f_rng RNG function |
|---|
| 116 | * \param p_rng RNG parameter |
|---|
| 117 | * |
|---|
| 118 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
|---|
| 119 | */ |
|---|
| 120 | int dhm_make_public( dhm_context *ctx, int x_size, |
|---|
| 121 | unsigned char *output, size_t olen, |
|---|
| 122 | int (*f_rng)(void *, unsigned char *, size_t), |
|---|
| 123 | void *p_rng ); |
|---|
| 124 | |
|---|
| 125 | /** |
|---|
| 126 | * \brief Derive and export the shared secret (G^Y)^X mod P |
|---|
| 127 | * |
|---|
| 128 | * \param ctx DHM context |
|---|
| 129 | * \param output destination buffer |
|---|
| 130 | * \param olen number of chars written |
|---|
| 131 | * |
|---|
| 132 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
|---|
| 133 | */ |
|---|
| 134 | int dhm_calc_secret( dhm_context *ctx, |
|---|
| 135 | unsigned char *output, size_t *olen ); |
|---|
| 136 | |
|---|
| 137 | /* |
|---|
| 138 | * \brief Free the components of a DHM key |
|---|
| 139 | */ |
|---|
| 140 | void dhm_free( dhm_context *ctx ); |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * \brief Checkup routine |
|---|
| 144 | * |
|---|
| 145 | * \return 0 if successful, or 1 if the test failed |
|---|
| 146 | */ |
|---|
| 147 | int dhm_self_test( int verbose ); |
|---|
| 148 | |
|---|
| 149 | #ifdef __cplusplus |
|---|
| 150 | } |
|---|
| 151 | #endif |
|---|
| 152 | |
|---|
| 153 | #endif |
|---|