PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/include/polarssl/dhm.h

https://github.com/leg0/polarssl
C Header | 244 lines | 114 code | 21 blank | 109 comment | 0 complexity | c2d615a06f3399ad8bc6215050608ebc MD5 | raw file
Possible License(s): GPL-2.0
  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 "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 /**< Making of the public value failed. */
  38. #define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
  39. /**
  40. * RFC 3526 defines a number of standardized Diffie-Hellman groups
  41. * for IKE.
  42. * RFC 5114 defines a number of standardized Diffie-Hellman groups
  43. * that can be used.
  44. *
  45. * Some are included here for convenience.
  46. *
  47. * Included are:
  48. * RFC 3526 3. 2048-bit MODP Group
  49. * RFC 3526 4. 3072-bit MODP Group
  50. * RFC 5114 2.1. 1024-bit MODP Group with 160-bit Prime Order Subgroup
  51. * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
  52. */
  53. #define POLARSSL_DHM_RFC3526_MODP_2048_P \
  54. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  55. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  56. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  57. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  58. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  59. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  60. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  61. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  62. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  63. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  64. "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
  65. #define POLARSSL_DHM_RFC3526_MODP_2048_G "02"
  66. #define POLARSSL_DHM_RFC3526_MODP_3072_P \
  67. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  68. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  69. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  70. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  71. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  72. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  73. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  74. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  75. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  76. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  77. "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \
  78. "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \
  79. "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \
  80. "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \
  81. "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \
  82. "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
  83. #define POLARSSL_DHM_RFC3526_MODP_3072_G "02"
  84. #define POLARSSL_DHM_RFC5114_MODP_1024_P \
  85. "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6" \
  86. "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0" \
  87. "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70" \
  88. "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0" \
  89. "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708" \
  90. "DF1FB2BC2E4A4371"
  91. #define POLARSSL_DHM_RFC5114_MODP_1024_G \
  92. "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F" \
  93. "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213" \
  94. "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1" \
  95. "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A" \
  96. "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24" \
  97. "855E6EEB22B3B2E5"
  98. #define POLARSSL_DHM_RFC5114_MODP_2048_P \
  99. "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \
  100. "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \
  101. "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \
  102. "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \
  103. "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \
  104. "B3BF8A317091883681286130BC8985DB1602E714415D9330" \
  105. "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \
  106. "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \
  107. "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \
  108. "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \
  109. "CF9DE5384E71B81C0AC4DFFE0C10E64F"
  110. #define POLARSSL_DHM_RFC5114_MODP_2048_G \
  111. "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\
  112. "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\
  113. "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\
  114. "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\
  115. "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\
  116. "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\
  117. "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\
  118. "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\
  119. "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\
  120. "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\
  121. "81BC087F2A7065B384B890D3191F2BFA"
  122. /**
  123. * \brief DHM context structure
  124. */
  125. typedef struct
  126. {
  127. size_t len; /*!< size(P) in chars */
  128. mpi P; /*!< prime modulus */
  129. mpi G; /*!< generator */
  130. mpi X; /*!< secret value */
  131. mpi GX; /*!< self = G^X mod P */
  132. mpi GY; /*!< peer = G^Y mod P */
  133. mpi K; /*!< key = GY^X mod P */
  134. mpi RP; /*!< cached R^2 mod P */
  135. }
  136. dhm_context;
  137. #ifdef __cplusplus
  138. extern "C" {
  139. #endif
  140. /**
  141. * \brief Parse the ServerKeyExchange parameters
  142. *
  143. * \param ctx DHM context
  144. * \param p &(start of input buffer)
  145. * \param end end of buffer
  146. *
  147. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  148. */
  149. int dhm_read_params( dhm_context *ctx,
  150. unsigned char **p,
  151. const unsigned char *end );
  152. /**
  153. * \brief Setup and write the ServerKeyExchange parameters
  154. *
  155. * \param ctx DHM context
  156. * \param x_size private value size in bytes
  157. * \param output destination buffer
  158. * \param olen number of chars written
  159. * \param f_rng RNG function
  160. * \param p_rng RNG parameter
  161. *
  162. * \note This function assumes that ctx->P and ctx->G
  163. * have already been properly set (for example
  164. * using mpi_read_string or mpi_read_binary).
  165. *
  166. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  167. */
  168. int dhm_make_params( dhm_context *ctx, int x_size,
  169. unsigned char *output, size_t *olen,
  170. int (*f_rng)(void *, unsigned char *, size_t),
  171. void *p_rng );
  172. /**
  173. * \brief Import the peer's public value G^Y
  174. *
  175. * \param ctx DHM context
  176. * \param input input buffer
  177. * \param ilen size of buffer
  178. *
  179. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  180. */
  181. int dhm_read_public( dhm_context *ctx,
  182. const unsigned char *input, size_t ilen );
  183. /**
  184. * \brief Create own private value X and export G^X
  185. *
  186. * \param ctx DHM context
  187. * \param x_size private value size in bytes
  188. * \param output destination buffer
  189. * \param olen must be equal to ctx->P.len
  190. * \param f_rng RNG function
  191. * \param p_rng RNG parameter
  192. *
  193. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  194. */
  195. int dhm_make_public( dhm_context *ctx, int x_size,
  196. unsigned char *output, size_t olen,
  197. int (*f_rng)(void *, unsigned char *, size_t),
  198. void *p_rng );
  199. /**
  200. * \brief Derive and export the shared secret (G^Y)^X mod P
  201. *
  202. * \param ctx DHM context
  203. * \param output destination buffer
  204. * \param olen number of chars written
  205. *
  206. * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code
  207. */
  208. int dhm_calc_secret( dhm_context *ctx,
  209. unsigned char *output, size_t *olen );
  210. /**
  211. * \brief Free the components of a DHM key
  212. */
  213. void dhm_free( dhm_context *ctx );
  214. /**
  215. * \brief Checkup routine
  216. *
  217. * \return 0 if successful, or 1 if the test failed
  218. */
  219. int dhm_self_test( int verbose );
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223. #endif