Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file dhm.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Diffie-Hellman-Merkle key exchange |
| 5 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 20 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
Hanno Becker | a2f6b72 | 2017-09-28 10:33:29 +0100 | [diff] [blame] | 22 | * |
| 23 | * \warning The security of the DHM key exchange relies on the proper choice |
| 24 | * of prime modulus - optimally, it should be a safe prime. The usage |
| 25 | * of non-safe primes both decreases the difficulty of the underlying |
| 26 | * discrete logarithm problem and can lead to small subgroup attacks |
| 27 | * leaking private exponent bits when invalid public keys are used |
Hanno Becker | f9734b3 | 2017-10-03 12:09:22 +0100 | [diff] [blame^] | 28 | * and not detected. This is especially relevant if the same DHM |
| 29 | * parameters are reused for multiple key exchanges as in static DHM, |
| 30 | * while the criticality of small-subgroup attacks is lower for |
| 31 | * ephemeral DHM. |
Hanno Becker | a2f6b72 | 2017-09-28 10:33:29 +0100 | [diff] [blame] | 32 | * |
| 33 | * For performance reasons, the code does neither perform primality |
| 34 | * nor safe primality tests, nor the expensive checks for invalid |
Hanno Becker | f9734b3 | 2017-10-03 12:09:22 +0100 | [diff] [blame^] | 35 | * subgroups. Moreover, even if these were performed, non-standardized |
| 36 | * primes cannot be trusted because of the possibility of backdoors |
| 37 | * that can't be effectively checked for. |
Hanno Becker | a2f6b72 | 2017-09-28 10:33:29 +0100 | [diff] [blame] | 38 | * |
Hanno Becker | f9734b3 | 2017-10-03 12:09:22 +0100 | [diff] [blame^] | 39 | * We therefore consider DHE a security risk. If possible, it is |
| 40 | * recommended users should consider preferring other methods of |
| 41 | * key exchange. |
Hanno Becker | a2f6b72 | 2017-09-28 10:33:29 +0100 | [diff] [blame] | 42 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | #ifndef MBEDTLS_DHM_H |
| 45 | #define MBEDTLS_DHM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | |
Paul Bakker | 314052f | 2011-08-15 09:07:52 +0000 | [diff] [blame] | 47 | #include "bignum.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 49 | /* |
| 50 | * DHM Error codes |
| 51 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #define MBEDTLS_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */ |
| 53 | #define MBEDTLS_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */ |
| 54 | #define MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */ |
| 55 | #define MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */ |
| 56 | #define MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */ |
| 57 | #define MBEDTLS_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */ |
| 58 | #define MBEDTLS_ERR_DHM_INVALID_FORMAT -0x3380 /**< The ASN.1 data is not formatted correctly. */ |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 59 | #define MBEDTLS_ERR_DHM_ALLOC_FAILED -0x3400 /**< Allocation of memory failed. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #define MBEDTLS_ERR_DHM_FILE_IO_ERROR -0x3480 /**< Read/write of file failed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | |
Hanno Becker | 0e6dc84 | 2017-09-27 11:48:02 +0100 | [diff] [blame] | 62 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 63 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 64 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 65 | #else |
| 66 | #define MBEDTLS_DEPRECATED |
| 67 | #endif |
| 68 | #endif |
| 69 | |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 70 | /** |
Hanno Becker | b2bad80 | 2017-09-27 11:49:31 +0100 | [diff] [blame] | 71 | * RFC 3526, RFC 5114 and RFC 7919 standardize a number of |
| 72 | * Diffie-Hellman groups, some of which are included here |
| 73 | * for use within the SSL/TLS module and the user's convenience |
| 74 | * when configuring the Diffie-Hellman parameters by hand |
| 75 | * through \c mbedtls_ssl_conf_dh_param. |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 76 | * |
| 77 | * Included are: |
Hanno Becker | b2bad80 | 2017-09-27 11:49:31 +0100 | [diff] [blame] | 78 | * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 79 | * RFC 3526 3. 2048-bit MODP Group |
| 80 | * RFC 3526 4. 3072-bit MODP Group |
Manuel Pégourié-Gonnard | 7893103 | 2015-07-03 17:06:39 +0200 | [diff] [blame] | 81 | * RFC 3526 5. 4096-bit MODP Group |
Hanno Becker | b2bad80 | 2017-09-27 11:49:31 +0100 | [diff] [blame] | 82 | * RFC 7919 A.1 ffdhe2048 |
| 83 | * RFC 7919 A.2 ffdhe3072 |
| 84 | * RFC 7919 A.3 ffdhe4096 |
| 85 | * RFC 7919 A.4 ffdhe6144 |
| 86 | * RFC 7919 A.5 ffdhe8192 |
| 87 | * |
Hanno Becker | 0e6dc84 | 2017-09-27 11:48:02 +0100 | [diff] [blame] | 88 | * The constants with suffix "_p" denote the chosen prime moduli, while |
| 89 | * the constants with suffix "_g" denote the chosen generator |
| 90 | * of the associated prime field. |
| 91 | * |
| 92 | * All constants are represented as null-terminated strings containing the |
| 93 | * hexadecimal presentation of the respective numbers. |
| 94 | * |
| 95 | * \warning The origin of the primes in RFC 5114 is not documented and |
| 96 | * their use therefore constitutes a security risk! |
| 97 | * |
| 98 | * \deprecated The primes from RFC 5114 are superseded by the primes |
| 99 | * from RFC 3526 and RFC 7919 and should no longer be used. |
| 100 | * They will be removed in the next major revision. |
Hanno Becker | b1d4d1f | 2017-09-27 12:42:59 +0100 | [diff] [blame] | 101 | * |
| 102 | * The primes from RFC 3526 and RFC 7919 have been generating by the following |
| 103 | * trust-worthy procedure: |
| 104 | * - Fix N in { 2048, 3072, 4096, 6144, 8192 } and consider the N-bit number |
| 105 | * the first and last 64 bits are all 1, and the remaining N - 128 bits of |
| 106 | * which are 0x7ff...ff. |
| 107 | * - Add the smallest multiple of the first N - 129 bits of the binary expansion |
| 108 | * of pi (for RFC 5236) or e (for RFC 7919) to this intermediate bit-string |
| 109 | * such that the resulting integer is a safe-prime. |
| 110 | * - The result is the respective RFC 3526 / 7919 prime, and the corresponding |
| 111 | * generator is always chosen to be 2 (which is a square for these prime, |
| 112 | * hence the corresponding subgroup has order (p-1)/2 and avoids leaking a |
| 113 | * bit in the private exponent). |
| 114 | * |
| 115 | * The above description can be validated using the |
| 116 | * the program programs/util/rfc_3526_7919_verify. |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 117 | */ |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 118 | |
Hanno Becker | 4c72b00 | 2017-09-27 16:06:22 +0100 | [diff] [blame] | 119 | const char * const mbedtls_dhm_rfc3526_modp_2048_p; |
| 120 | const char * const mbedtls_dhm_rfc3526_modp_2048_g; |
| 121 | const char * const mbedtls_dhm_rfc3526_modp_3072_p; |
| 122 | const char * const mbedtls_dhm_rfc3526_modp_3072_g; |
| 123 | const char * const mbedtls_dhm_rfc3526_modp_4096_p; |
| 124 | const char * const mbedtls_dhm_rfc3526_modp_4096_g; |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 125 | |
Hanno Becker | 4c72b00 | 2017-09-27 16:06:22 +0100 | [diff] [blame] | 126 | const char * const mbedtls_dhm_rfc7919_ffdhe2048_p; |
| 127 | const char * const mbedtls_dhm_rfc7919_ffdhe2048_g; |
| 128 | const char * const mbedtls_dhm_rfc7919_ffdhe3072_p; |
| 129 | const char * const mbedtls_dhm_rfc7919_ffdhe3072_g; |
| 130 | const char * const mbedtls_dhm_rfc7919_ffdhe4096_p; |
| 131 | const char * const mbedtls_dhm_rfc7919_ffdhe4096_g; |
| 132 | const char * const mbedtls_dhm_rfc7919_ffdhe6144_p; |
| 133 | const char * const mbedtls_dhm_rfc7919_ffdhe6144_g; |
| 134 | const char * const mbedtls_dhm_rfc7919_ffdhe8192_p; |
| 135 | const char * const mbedtls_dhm_rfc7919_ffdhe8192_g; |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 136 | |
Hanno Becker | 0e6dc84 | 2017-09-27 11:48:02 +0100 | [diff] [blame] | 137 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Hanno Becker | 4c72b00 | 2017-09-27 16:06:22 +0100 | [diff] [blame] | 138 | MBEDTLS_DEPRECATED const char * const mbedtls_dhm_rfc5114_modp_2048_p; |
| 139 | MBEDTLS_DEPRECATED const char * const mbedtls_dhm_rfc5114_modp_2048_g; |
Hanno Becker | 0e6dc84 | 2017-09-27 11:48:02 +0100 | [diff] [blame] | 140 | #endif |
Paul Bakker | da7e3f2 | 2012-09-28 07:18:17 +0000 | [diff] [blame] | 141 | |
Hanno Becker | 0e6dc84 | 2017-09-27 11:48:02 +0100 | [diff] [blame] | 142 | /** |
| 143 | * \deprecated These macros are superseded by direct access to the corresponding |
| 144 | * global variables and will be removed in the next major revision. |
| 145 | */ |
| 146 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 147 | #define MBEDTLS_DHM_RFC5114_MODP_2048_P mbedtls_dhm_rfc5114_modp_2048_p |
| 148 | #define MBEDTLS_DHM_RFC5114_MODP_2048_G mbedtls_dhm_rfc5114_modp_2048_g |
| 149 | #define MBEDTLS_DHM_RFC3526_MODP_2048_P mbedtls_dhm_rfc3526_modp_2048_p |
| 150 | #define MBEDTLS_DHM_RFC3526_MODP_2048_G mbedtls_dhm_rfc3526_modp_2048_g |
| 151 | #define MBEDTLS_DHM_RFC3526_MODP_3072_P mbedtls_dhm_rfc3526_modp_3072_p |
| 152 | #define MBEDTLS_DHM_RFC3526_MODP_3072_G mbedtls_dhm_rfc3526_modp_3072_g |
| 153 | #define MBEDTLS_DHM_RFC3526_MODP_4096_P mbedtls_dhm_rfc3526_modp_4096_p |
| 154 | #define MBEDTLS_DHM_RFC3526_MODP_4096_G mbedtls_dhm_rfc3526_modp_4096_g |
| 155 | #endif |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 156 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 157 | #ifdef __cplusplus |
| 158 | extern "C" { |
| 159 | #endif |
| 160 | |
Paul Bakker | 29b6476 | 2012-09-25 09:36:44 +0000 | [diff] [blame] | 161 | /** |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 162 | * \brief DHM context structure |
| 163 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | typedef struct |
| 165 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 166 | size_t len; /*!< size(P) in chars */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | mbedtls_mpi P; /*!< prime modulus */ |
| 168 | mbedtls_mpi G; /*!< generator */ |
| 169 | mbedtls_mpi X; /*!< secret value */ |
| 170 | mbedtls_mpi GX; /*!< self = G^X mod P */ |
| 171 | mbedtls_mpi GY; /*!< peer = G^Y mod P */ |
| 172 | mbedtls_mpi K; /*!< key = GY^X mod P */ |
| 173 | mbedtls_mpi RP; /*!< cached R^2 mod P */ |
| 174 | mbedtls_mpi Vi; /*!< blinding value */ |
| 175 | mbedtls_mpi Vf; /*!< un-blinding value */ |
| 176 | mbedtls_mpi pX; /*!< previous X */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | mbedtls_dhm_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 180 | /** |
Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 181 | * \brief Initialize DHM context |
| 182 | * |
| 183 | * \param ctx DHM context to be initialized |
| 184 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | void mbedtls_dhm_init( mbedtls_dhm_context *ctx ); |
Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 186 | |
| 187 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | * \brief Parse the ServerKeyExchange parameters |
| 189 | * |
| 190 | * \param ctx DHM context |
Hanno Becker | f240ea0 | 2017-10-02 15:09:14 +0100 | [diff] [blame] | 191 | * \param p On input, *p must be the start of the input buffer. |
| 192 | * On output, *p is updated to point to the end of the data |
| 193 | * that has been read. On success, this is the first byte |
| 194 | * past the end of the ServerKeyExchange parameters. |
| 195 | * On error, this is the point at which an error has been |
| 196 | * detected, which is usually not useful except to debug |
| 197 | * failures. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | * \param end end of buffer |
| 199 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 204 | const unsigned char *end ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * \brief Setup and write the ServerKeyExchange parameters |
| 208 | * |
| 209 | * \param ctx DHM context |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 210 | * \param x_size private value size in bytes |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | * \param output destination buffer |
| 212 | * \param olen number of chars written |
| 213 | * \param f_rng RNG function |
| 214 | * \param p_rng RNG parameter |
| 215 | * |
Hanno Becker | e764324 | 2017-09-28 10:33:11 +0100 | [diff] [blame] | 216 | * \note The destination buffer must be large enough to hold |
Hanno Becker | 70da2c5 | 2017-10-02 15:02:59 +0100 | [diff] [blame] | 217 | * the reduced binary presentation of the modulus, the generator |
| 218 | * and the public key, each wrapped with a 2-byte length field. |
| 219 | * It is the responsibility of the caller to ensure that enough |
| 220 | * space is available. Refer to \c mbedtls_mpi_size to computing |
| 221 | * the byte-size of an MPI. |
Hanno Becker | e764324 | 2017-09-28 10:33:11 +0100 | [diff] [blame] | 222 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 223 | * \note This function assumes that ctx->P and ctx->G |
| 224 | * have already been properly set (for example |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | * using mbedtls_mpi_read_string or mbedtls_mpi_read_binary). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 228 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 230 | unsigned char *output, size_t *olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 231 | int (*f_rng)(void *, unsigned char *, size_t), |
| 232 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * \brief Import the peer's public value G^Y |
| 236 | * |
| 237 | * \param ctx DHM context |
| 238 | * \param input input buffer |
| 239 | * \param ilen size of buffer |
| 240 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 244 | const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * \brief Create own private value X and export G^X |
| 248 | * |
| 249 | * \param ctx DHM context |
Paul Bakker | 84bef1d | 2012-04-20 13:42:02 +0000 | [diff] [blame] | 250 | * \param x_size private value size in bytes |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | * \param output destination buffer |
Hanno Becker | e764324 | 2017-09-28 10:33:11 +0100 | [diff] [blame] | 252 | * \param olen size of the destination buffer; |
| 253 | * must be at least equal to the size of P, ctx->len |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | * \param f_rng RNG function |
| 255 | * \param p_rng RNG parameter |
| 256 | * |
Hanno Becker | e764324 | 2017-09-28 10:33:11 +0100 | [diff] [blame] | 257 | * \note The destination buffer will always be fully written |
| 258 | * so as to contain a big-endian presentation of G^X mod P. |
| 259 | * If it is larger than ctx->len, it will accordingly be |
| 260 | * padded with zero-bytes in the beginning. |
| 261 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 263 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 265 | unsigned char *output, size_t olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 266 | int (*f_rng)(void *, unsigned char *, size_t), |
| 267 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * \brief Derive and export the shared secret (G^Y)^X mod P |
| 271 | * |
| 272 | * \param ctx DHM context |
| 273 | * \param output destination buffer |
Hanno Becker | e764324 | 2017-09-28 10:33:11 +0100 | [diff] [blame] | 274 | * \param output_size size of the destination buffer, must be at |
| 275 | * at least the size of ctx->len |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 276 | * \param olen on exit, holds the actual number of bytes written |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 277 | * \param f_rng RNG function, for blinding purposes |
| 278 | * \param p_rng RNG parameter |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | * \return 0 if successful, or an MBEDTLS_ERR_DHM_XXX error code |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 281 | * |
Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 282 | * \note If non-NULL, f_rng is used to blind the input as |
| 283 | * countermeasure against timing attacks. Blinding is |
| 284 | * automatically used if and only if our secret value X is |
| 285 | * re-used and costs nothing otherwise, so it is recommended |
| 286 | * to always pass a non-NULL f_rng argument. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 287 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx, |
Manuel Pégourié-Gonnard | 3335205 | 2015-06-02 16:17:08 +0100 | [diff] [blame] | 289 | unsigned char *output, size_t output_size, size_t *olen, |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 290 | int (*f_rng)(void *, unsigned char *, size_t), |
| 291 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 293 | /** |
Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 294 | * \brief Free and clear the components of a DHM key |
| 295 | * |
| 296 | * \param ctx DHM context to free and clear |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | void mbedtls_dhm_free( mbedtls_dhm_context *ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | #if defined(MBEDTLS_ASN1_PARSE_C) |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 301 | /** \ingroup x509_module */ |
| 302 | /** |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 303 | * \brief Parse DHM parameters in PEM or DER format |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 304 | * |
| 305 | * \param dhm DHM context to be initialized |
| 306 | * \param dhmin input buffer |
| 307 | * \param dhminlen size of the buffer |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 308 | * (including the terminating null byte for PEM data) |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 309 | * |
| 310 | * \return 0 if successful, or a specific DHM or PEM error code |
| 311 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin, |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 313 | size_t dhminlen ); |
| 314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 316 | /** \ingroup x509_module */ |
| 317 | /** |
| 318 | * \brief Load and parse DHM parameters |
| 319 | * |
| 320 | * \param dhm DHM context to be initialized |
| 321 | * \param path filename to read the DHM Parameters from |
| 322 | * |
| 323 | * \return 0 if successful, or a specific DHM or PEM error code |
| 324 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path ); |
| 326 | #endif /* MBEDTLS_FS_IO */ |
| 327 | #endif /* MBEDTLS_ASN1_PARSE_C */ |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 328 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 329 | /** |
| 330 | * \brief Checkup routine |
| 331 | * |
| 332 | * \return 0 if successful, or 1 if the test failed |
| 333 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 334 | int mbedtls_dhm_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 335 | |
| 336 | #ifdef __cplusplus |
| 337 | } |
| 338 | #endif |
| 339 | |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 340 | #endif /* dhm.h */ |