Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file dhm.h |
| 3 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 4 | #ifndef POLARSSL_DHM_H |
| 5 | #define POLARSSL_DHM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | |
Paul Bakker | 8e831ed | 2009-01-03 21:24:11 +0000 | [diff] [blame] | 7 | #include "polarssl/bignum.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 9 | #define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x0480 |
| 10 | #define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x0490 |
| 11 | #define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x04A0 |
| 12 | #define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x04B0 |
| 13 | #define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x04C0 |
| 14 | #define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x04D0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 15 | |
| 16 | typedef struct |
| 17 | { |
| 18 | int len; /*!< size(P) in chars */ |
| 19 | mpi P; /*!< prime modulus */ |
| 20 | mpi G; /*!< generator */ |
| 21 | mpi X; /*!< secret value */ |
| 22 | mpi GX; /*!< self = G^X mod P */ |
| 23 | mpi GY; /*!< peer = G^Y mod P */ |
| 24 | mpi K; /*!< key = GY^X mod P */ |
| 25 | mpi RP; /*!< cached R^2 mod P */ |
| 26 | } |
| 27 | dhm_context; |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | /** |
| 34 | * \brief Parse the ServerKeyExchange parameters |
| 35 | * |
| 36 | * \param ctx DHM context |
| 37 | * \param p &(start of input buffer) |
| 38 | * \param end end of buffer |
| 39 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 40 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | */ |
| 42 | int dhm_read_params( dhm_context *ctx, |
| 43 | unsigned char **p, |
| 44 | unsigned char *end ); |
| 45 | |
| 46 | /** |
| 47 | * \brief Setup and write the ServerKeyExchange parameters |
| 48 | * |
| 49 | * \param ctx DHM context |
| 50 | * \param x_size private value size in bits |
| 51 | * \param output destination buffer |
| 52 | * \param olen number of chars written |
| 53 | * \param f_rng RNG function |
| 54 | * \param p_rng RNG parameter |
| 55 | * |
| 56 | * \note This function assumes that ctx->P and ctx->G |
| 57 | * have already been properly set (for example |
| 58 | * using mpi_read_string or mpi_read_binary). |
| 59 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 60 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | */ |
| 62 | int dhm_make_params( dhm_context *ctx, int s_size, |
| 63 | unsigned char *output, int *olen, |
| 64 | int (*f_rng)(void *), void *p_rng ); |
| 65 | |
| 66 | /** |
| 67 | * \brief Import the peer's public value G^Y |
| 68 | * |
| 69 | * \param ctx DHM context |
| 70 | * \param input input buffer |
| 71 | * \param ilen size of buffer |
| 72 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 73 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | */ |
| 75 | int dhm_read_public( dhm_context *ctx, |
| 76 | unsigned char *input, int ilen ); |
| 77 | |
| 78 | /** |
| 79 | * \brief Create own private value X and export G^X |
| 80 | * |
| 81 | * \param ctx DHM context |
| 82 | * \param x_size private value size in bits |
| 83 | * \param output destination buffer |
| 84 | * \param olen must be equal to ctx->P.len |
| 85 | * \param f_rng RNG function |
| 86 | * \param p_rng RNG parameter |
| 87 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 88 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | */ |
| 90 | int dhm_make_public( dhm_context *ctx, int s_size, |
| 91 | unsigned char *output, int olen, |
| 92 | int (*f_rng)(void *), void *p_rng ); |
| 93 | |
| 94 | /** |
| 95 | * \brief Derive and export the shared secret (G^Y)^X mod P |
| 96 | * |
| 97 | * \param ctx DHM context |
| 98 | * \param output destination buffer |
| 99 | * \param olen number of chars written |
| 100 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame^] | 101 | * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | */ |
| 103 | int dhm_calc_secret( dhm_context *ctx, |
| 104 | unsigned char *output, int *olen ); |
| 105 | |
| 106 | /* |
| 107 | * \brief Free the components of a DHM key |
| 108 | */ |
| 109 | void dhm_free( dhm_context *ctx ); |
| 110 | |
| 111 | /** |
| 112 | * \brief Checkup routine |
| 113 | * |
| 114 | * \return 0 if successful, or 1 if the test failed |
| 115 | */ |
| 116 | int dhm_self_test( int verbose ); |
| 117 | |
| 118 | #ifdef __cplusplus |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | #endif |