dhm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * \file dhm.h
  3. *
  4. * \brief Diffie-Hellman-Merkle key exchange
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_DHM_H
  26. #define MBEDTLS_DHM_H
  27. #include "bignum.h"
  28. /*
  29. * DHM Error codes
  30. */
  31. #define MBEDTLS_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */
  32. #define MBEDTLS_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */
  33. #define MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */
  34. #define MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */
  35. #define MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */
  36. #define MBEDTLS_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */
  37. #define MBEDTLS_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */
  38. #define MBEDTLS_ERR_DHM_ALLOC_FAILED -0x3400 /**< Allocation of memory failed. */
  39. #define MBEDTLS_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file failed. */
  40. /**
  41. * RFC 3526 defines a number of standardized Diffie-Hellman groups
  42. * for IKE.
  43. * RFC 5114 defines a number of standardized Diffie-Hellman groups
  44. * that can be used.
  45. *
  46. * Some are included here for convenience.
  47. *
  48. * Included are:
  49. * RFC 3526 3. 2048-bit MODP Group
  50. * RFC 3526 4. 3072-bit MODP Group
  51. * RFC 3526 5. 4096-bit MODP Group
  52. * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup
  53. */
  54. #define MBEDTLS_DHM_RFC3526_MODP_2048_P \
  55. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  56. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  57. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  58. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  59. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  60. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  61. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  62. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  63. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  64. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  65. "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
  66. #define MBEDTLS_DHM_RFC3526_MODP_2048_G "02"
  67. #define MBEDTLS_DHM_RFC3526_MODP_3072_P \
  68. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  69. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  70. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  71. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  72. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  73. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  74. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  75. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  76. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  77. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  78. "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \
  79. "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \
  80. "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \
  81. "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \
  82. "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \
  83. "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
  84. #define MBEDTLS_DHM_RFC3526_MODP_3072_G "02"
  85. #define MBEDTLS_DHM_RFC3526_MODP_4096_P \
  86. "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
  87. "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
  88. "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
  89. "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
  90. "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
  91. "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
  92. "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
  93. "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \
  94. "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \
  95. "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \
  96. "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \
  97. "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \
  98. "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \
  99. "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \
  100. "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \
  101. "43DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D7" \
  102. "88719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA" \
  103. "2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6" \
  104. "287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED" \
  105. "1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA9" \
  106. "93B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" \
  107. "FFFFFFFFFFFFFFFF"
  108. #define MBEDTLS_DHM_RFC3526_MODP_4096_G "02"
  109. #define MBEDTLS_DHM_RFC5114_MODP_2048_P \
  110. "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \
  111. "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \
  112. "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \
  113. "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \
  114. "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \
  115. "B3BF8A317091883681286130BC8985DB1602E714415D9330" \
  116. "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \
  117. "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \
  118. "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \
  119. "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \
  120. "CF9DE5384E71B81C0AC4DFFE0C10E64F"
  121. #define MBEDTLS_DHM_RFC5114_MODP_2048_G \
  122. "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\
  123. "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\
  124. "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\
  125. "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\
  126. "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\
  127. "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\
  128. "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\
  129. "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\
  130. "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\
  131. "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\
  132. "81BC087F2A7065B384B890D3191F2BFA"
  133. #ifdef __cplusplus
  134. extern "C" {
  135. #endif
  136. /**
  137. * \brief DHM context structure
  138. */
  139. typedef struct
  140. {
  141. size_t len; /*!< size(P) in chars */
  142. mbedtls_mpi P; /*!< prime modulus */
  143. mbedtls_mpi G; /*!< generator */
  144. mbedtls_mpi X; /*!< secret value */
  145. mbedtls_mpi GX; /*!< self = G^X mod P */
  146. mbedtls_mpi GY; /*!< peer = G^Y mod P */
  147. mbedtls_mpi K; /*!< key = GY^X mod P */
  148. mbedtls_mpi RP; /*!< cached R^2 mod P */
  149. mbedtls_mpi Vi; /*!< blinding value */
  150. mbedtls_mpi Vf; /*!< un-blinding value */
  151. mbedtls_mpi pX; /*!< previous X */
  152. }
  153. mbedtls_dhm_context;
  154. /**
  155. * \brief Initialize DHM context
  156. *
  157. * \param ctx DHM context to be initialized
  158. */
  159. void mbedtls_dhm_init( mbedtls_dhm_context *ctx );
  160. /**
  161. * \brief Parse the ServerKeyExchange parameters
  162. *
  163. * \param ctx DHM context
  164. * \param p &(start of input buffer)
  165. * \param end end of buffer
  166. *
  167. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  168. */
  169. int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
  170. unsigned char **p,
  171. const unsigned char *end );
  172. /**
  173. * \brief Setup and write the ServerKeyExchange parameters
  174. *
  175. * \param ctx DHM context
  176. * \param x_size private value size in bytes
  177. * \param output destination buffer
  178. * \param olen number of chars written
  179. * \param f_rng RNG function
  180. * \param p_rng RNG parameter
  181. *
  182. * \note This function assumes that ctx->P and ctx->G
  183. * have already been properly set (for example
  184. * using mbedtls_mpi_read_string or mbedtls_mpi_read_binary).
  185. *
  186. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  187. */
  188. int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
  189. unsigned char *output, size_t *olen,
  190. int (*f_rng)(void *, unsigned char *, size_t),
  191. void *p_rng );
  192. /**
  193. * \brief Import the peer's public value G^Y
  194. *
  195. * \param ctx DHM context
  196. * \param input input buffer
  197. * \param ilen size of buffer
  198. *
  199. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  200. */
  201. int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
  202. const unsigned char *input, size_t ilen );
  203. /**
  204. * \brief Create own private value X and export G^X
  205. *
  206. * \param ctx DHM context
  207. * \param x_size private value size in bytes
  208. * \param output destination buffer
  209. * \param olen must be at least equal to the size of P, ctx->len
  210. * \param f_rng RNG function
  211. * \param p_rng RNG parameter
  212. *
  213. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  214. */
  215. int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
  216. unsigned char *output, size_t olen,
  217. int (*f_rng)(void *, unsigned char *, size_t),
  218. void *p_rng );
  219. /**
  220. * \brief Derive and export the shared secret (G^Y)^X mod P
  221. *
  222. * \param ctx DHM context
  223. * \param output destination buffer
  224. * \param output_size size of the destination buffer
  225. * \param olen on exit, holds the actual number of bytes written
  226. * \param f_rng RNG function, for blinding purposes
  227. * \param p_rng RNG parameter
  228. *
  229. * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code
  230. *
  231. * \note If non-NULL, f_rng is used to blind the input as
  232. * countermeasure against timing attacks. Blinding is
  233. * automatically used if and only if our secret value X is
  234. * re-used and costs nothing otherwise, so it is recommended
  235. * to always pass a non-NULL f_rng argument.
  236. */
  237. int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
  238. unsigned char *output, size_t output_size, size_t *olen,
  239. int (*f_rng)(void *, unsigned char *, size_t),
  240. void *p_rng );
  241. /**
  242. * \brief Free and clear the components of a DHM key
  243. *
  244. * \param ctx DHM context to free and clear
  245. */
  246. void mbedtls_dhm_free( mbedtls_dhm_context *ctx );
  247. #if defined(MBEDTLS_ASN1_PARSE_C)
  248. /** \ingroup x509_module */
  249. /**
  250. * \brief Parse DHM parameters in PEM or DER format
  251. *
  252. * \param dhm DHM context to be initialized
  253. * \param dhmin input buffer
  254. * \param dhminlen size of the buffer
  255. * (including the terminating null byte for PEM data)
  256. *
  257. * \return 0 if successful, or a specific DHM or PEM error code
  258. */
  259. int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
  260. size_t dhminlen );
  261. #if defined(MBEDTLS_FS_IO)
  262. /** \ingroup x509_module */
  263. /**
  264. * \brief Load and parse DHM parameters
  265. *
  266. * \param dhm DHM context to be initialized
  267. * \param path filename to read the DHM Parameters from
  268. *
  269. * \return 0 if successful, or a specific DHM or PEM error code
  270. */
  271. int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path );
  272. #endif /* MBEDTLS_FS_IO */
  273. #endif /* MBEDTLS_ASN1_PARSE_C */
  274. /**
  275. * \brief Checkup routine
  276. *
  277. * \return 0 if successful, or 1 if the test failed
  278. */
  279. int mbedtls_dhm_self_test( int verbose );
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #endif /* dhm.h */