dhm.h 12 KB

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