dhm.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #include "polarssl/bignum.h"
  30. /*
  31. * DHM Error codes
  32. */
  33. #define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */
  34. #define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */
  35. #define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */
  36. #define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */
  37. #define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Makeing of the public value failed. */
  38. #define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
  39. /**
  40. * \brief DHM context structure
  41. */
  42. typedef struct
  43. {
  44. size_t len; /*!< size(P) in chars */
  45. mpi P; /*!< prime modulus */
  46. mpi G; /*!< generator */
  47. mpi X; /*!< secret value */
  48. mpi GX; /*!< self = G^X mod P */
  49. mpi GY; /*!< peer = G^Y mod P */
  50. mpi K; /*!< key = GY^X mod P */
  51. mpi RP; /*!< cached R^2 mod P */
  52. }
  53. dhm_context;
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * \brief Parse the ServerKeyExchange parameters
  59. *
  60. * \param ctx DHM context
  61. * \param p &(start of input buffer)
  62. * \param end end of buffer
  63. *
  64. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  65. */
  66. int dhm_read_params( dhm_context *ctx,
  67. unsigned char **p,
  68. const unsigned char *end );
  69. /**
  70. * \brief Setup and write the ServerKeyExchange parameters
  71. *
  72. * \param ctx DHM context
  73. * \param x_size private value size in bytes
  74. * \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. *
  83. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  84. */
  85. int dhm_make_params( dhm_context *ctx, int x_size,
  86. unsigned char *output, size_t *olen,
  87. int (*f_rng)(void *), void *p_rng );
  88. /**
  89. * \brief Import the peer's public value G^Y
  90. *
  91. * \param ctx DHM context
  92. * \param input input buffer
  93. * \param ilen size of buffer
  94. *
  95. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  96. */
  97. int dhm_read_public( dhm_context *ctx,
  98. const unsigned char *input, size_t ilen );
  99. /**
  100. * \brief Create own private value X and export G^X
  101. *
  102. * \param ctx DHM context
  103. * \param x_size private value size in bits
  104. * \param output destination buffer
  105. * \param olen must be equal to ctx->P.len
  106. * \param f_rng RNG function
  107. * \param p_rng RNG parameter
  108. *
  109. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  110. */
  111. int dhm_make_public( dhm_context *ctx, int x_size,
  112. unsigned char *output, size_t olen,
  113. int (*f_rng)(void *), void *p_rng );
  114. /**
  115. * \brief Derive and export the shared secret (G^Y)^X mod P
  116. *
  117. * \param ctx DHM context
  118. * \param output destination buffer
  119. * \param olen number of chars written
  120. *
  121. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  122. */
  123. int dhm_calc_secret( dhm_context *ctx,
  124. unsigned char *output, size_t *olen );
  125. /*
  126. * \brief Free the components of a DHM key
  127. */
  128. void dhm_free( dhm_context *ctx );
  129. /**
  130. * \brief Checkup routine
  131. *
  132. * \return 0 if successful, or 1 if the test failed
  133. */
  134. int dhm_self_test( int verbose );
  135. #ifdef __cplusplus
  136. }
  137. #endif
  138. #endif