| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Diffie-Hellman-Merkle key exchange | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | */ | 
|  | 7 | /* | 
| Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 8 | *  The following sources were referenced in the design of this implementation | 
|  | 9 | *  of the Diffie-Hellman-Merkle algorithm: | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 10 | * | 
| Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 11 | *  [1] Handbook of Applied Cryptography - 1997, Chapter 12 | 
|  | 12 | *      Menezes, van Oorschot and Vanstone | 
|  | 13 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 14 | */ | 
|  | 15 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 16 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 17 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | #if defined(MBEDTLS_DHM_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 19 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 20 | #include "mbedtls/dhm.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 21 | #include "mbedtls/platform_util.h" | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 22 | #include "mbedtls/error.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 24 | #include <string.h> | 
|  | 25 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_PEM_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 27 | #include "mbedtls/pem.h" | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 28 | #endif | 
|  | 29 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_ASN1_PARSE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/asn1.h" | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 32 | #endif | 
|  | 33 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/platform.h" | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 35 |  | 
| Reuven Levin | 1f35ca9 | 2017-12-07 10:09:32 +0000 | [diff] [blame] | 36 | #if !defined(MBEDTLS_DHM_ALT) | 
| Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 37 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 38 | #define DHM_VALIDATE_RET(cond)    \ | 
|  | 39 | MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_DHM_BAD_INPUT_DATA) | 
|  | 40 | #define DHM_VALIDATE(cond)        \ | 
|  | 41 | MBEDTLS_INTERNAL_VALIDATE(cond) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [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 | * helper to validate the mbedtls_mpi size and import it | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 46 | static int dhm_read_bignum(mbedtls_mpi *X, | 
|  | 47 | unsigned char **p, | 
|  | 48 | const unsigned char *end) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | { | 
|  | 50 | int ret, n; | 
|  | 51 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | if (end - *p < 2) { | 
|  | 53 | return MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
|  | 54 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 56 | n = ((*p)[0] << 8) | (*p)[1]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | (*p) += 2; | 
|  | 58 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 59 | if ((int) (end - *p) < n) { | 
|  | 60 | return MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
|  | 61 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 63 | if ((ret = mbedtls_mpi_read_binary(X, *p, n)) != 0) { | 
|  | 64 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_READ_PARAMS_FAILED, ret); | 
|  | 65 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 |  | 
|  | 67 | (*p) += n; | 
|  | 68 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 69 | return 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
|  | 72 | /* | 
| Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 73 | * Verify sanity of parameter with regards to P | 
| Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 74 | * | 
| Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 75 | * Parameter should be: 2 <= public_param <= P - 2 | 
| Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 76 | * | 
| Janos Follath | aa325d7 | 2017-09-20 15:33:24 +0100 | [diff] [blame] | 77 | * This means that we need to return an error if | 
| Janos Follath | 1ad1c6d | 2017-09-21 09:02:11 +0100 | [diff] [blame] | 78 | *              public_param < 2 or public_param > P-2 | 
| Janos Follath | aa325d7 | 2017-09-20 15:33:24 +0100 | [diff] [blame] | 79 | * | 
| Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 80 | * For more information on the attack, see: | 
|  | 81 | *  http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf | 
|  | 82 | *  http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643 | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 83 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | static int dhm_check_range(const mbedtls_mpi *param, const mbedtls_mpi *P) | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 85 | { | 
| Gilles Peskine | 58df4c9 | 2021-03-31 22:56:43 +0200 | [diff] [blame] | 86 | mbedtls_mpi U; | 
| Janos Follath | aa325d7 | 2017-09-20 15:33:24 +0100 | [diff] [blame] | 87 | int ret = 0; | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 88 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | mbedtls_mpi_init(&U); | 
| Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 90 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&U, P, 2)); | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 92 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 93 | if (mbedtls_mpi_cmp_int(param, 2) < 0 || | 
|  | 94 | mbedtls_mpi_cmp_mpi(param, &U) > 0) { | 
| Janos Follath | aa325d7 | 2017-09-20 15:33:24 +0100 | [diff] [blame] | 95 | ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 98 | cleanup: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | mbedtls_mpi_free(&U); | 
|  | 100 | return ret; | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 103 | void mbedtls_dhm_init(mbedtls_dhm_context *ctx) | 
| Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 104 | { | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | DHM_VALIDATE(ctx != NULL); | 
|  | 106 | memset(ctx, 0, sizeof(mbedtls_dhm_context)); | 
| Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 109 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | * Parse the ServerKeyExchange parameters | 
|  | 111 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | int mbedtls_dhm_read_params(mbedtls_dhm_context *ctx, | 
|  | 113 | unsigned char **p, | 
|  | 114 | const unsigned char *end) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 116 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 117 | DHM_VALIDATE_RET(ctx != NULL); | 
|  | 118 | DHM_VALIDATE_RET(p != NULL && *p != NULL); | 
|  | 119 | DHM_VALIDATE_RET(end != NULL); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | if ((ret = dhm_read_bignum(&ctx->P,  p, end)) != 0 || | 
|  | 122 | (ret = dhm_read_bignum(&ctx->G,  p, end)) != 0 || | 
|  | 123 | (ret = dhm_read_bignum(&ctx->GY, p, end)) != 0) { | 
|  | 124 | return ret; | 
|  | 125 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | if ((ret = dhm_check_range(&ctx->GY, &ctx->P)) != 0) { | 
|  | 128 | return ret; | 
|  | 129 | } | 
| Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 130 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 131 | ctx->len = mbedtls_mpi_size(&ctx->P); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | return 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | } | 
|  | 135 |  | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 136 | /* | 
| Gilles Peskine | 16e3668 | 2021-03-31 23:04:50 +0200 | [diff] [blame] | 137 | * Pick a random R in the range [2, M-2] for blinding or key generation. | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 138 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | static int dhm_random_below(mbedtls_mpi *R, const mbedtls_mpi *M, | 
|  | 140 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 141 | { | 
| Gilles Peskine | 16e3668 | 2021-03-31 23:04:50 +0200 | [diff] [blame] | 142 | int ret; | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 143 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | MBEDTLS_MPI_CHK(mbedtls_mpi_random(R, 3, M, f_rng, p_rng)); | 
|  | 145 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(R, R, 1)); | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 146 |  | 
|  | 147 | cleanup: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | return ret; | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | static int dhm_make_common(mbedtls_dhm_context *ctx, int x_size, | 
|  | 152 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 153 | void *p_rng) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | { | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 155 | int ret = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | if (mbedtls_mpi_cmp_int(&ctx->P, 0) == 0) { | 
|  | 158 | return MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
| Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 159 | } | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | if (x_size < 0) { | 
|  | 161 | return MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | if ((unsigned) x_size < mbedtls_mpi_size(&ctx->P)) { | 
|  | 165 | MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&ctx->X, x_size, f_rng, p_rng)); | 
|  | 166 | } else { | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 167 | /* Generate X as large as possible ( <= P - 2 ) */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 168 | ret = dhm_random_below(&ctx->X, &ctx->P, f_rng, p_rng); | 
|  | 169 | if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { | 
|  | 170 | return MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED; | 
|  | 171 | } | 
|  | 172 | if (ret != 0) { | 
|  | 173 | return ret; | 
|  | 174 | } | 
| Gilles Peskine | 87fdb1f | 2021-03-31 22:48:14 +0200 | [diff] [blame] | 175 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 |  | 
| Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 177 | /* | 
|  | 178 | * Calculate GX = G^X mod P | 
|  | 179 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->GX, &ctx->G, &ctx->X, | 
|  | 181 | &ctx->P, &ctx->RP)); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | if ((ret = dhm_check_range(&ctx->GX, &ctx->P)) != 0) { | 
|  | 184 | return ret; | 
|  | 185 | } | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 186 |  | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 187 | cleanup: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 188 | return ret; | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
|  | 191 | /* | 
|  | 192 | * Setup and write the ServerKeyExchange parameters | 
|  | 193 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | int mbedtls_dhm_make_params(mbedtls_dhm_context *ctx, int x_size, | 
|  | 195 | unsigned char *output, size_t *olen, | 
|  | 196 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 197 | void *p_rng) | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 198 | { | 
|  | 199 | int ret; | 
|  | 200 | size_t n1, n2, n3; | 
|  | 201 | unsigned char *p; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | DHM_VALIDATE_RET(ctx != NULL); | 
|  | 203 | DHM_VALIDATE_RET(output != NULL); | 
|  | 204 | DHM_VALIDATE_RET(olen != NULL); | 
|  | 205 | DHM_VALIDATE_RET(f_rng != NULL); | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 206 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | ret = dhm_make_common(ctx, x_size, f_rng, p_rng); | 
|  | 208 | if (ret != 0) { | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 209 | goto cleanup; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | } | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 211 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | /* | 
| Gilles Peskine | 104eb82 | 2021-04-13 22:10:24 +0200 | [diff] [blame] | 213 | * Export P, G, GX. RFC 5246 §4.4 states that "leading zero octets are | 
|  | 214 | * not required". We omit leading zeros for compactness. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | #define DHM_MPI_EXPORT(X, n)                                          \ | 
| Hanno Becker | e71ad12 | 2017-09-28 10:32:25 +0100 | [diff] [blame] | 217 | do {                                                                \ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary((X),               \ | 
|  | 219 | p + 2,               \ | 
|  | 220 | (n)));           \ | 
|  | 221 | *p++ = MBEDTLS_BYTE_1(n);                                     \ | 
|  | 222 | *p++ = MBEDTLS_BYTE_0(n);                                     \ | 
|  | 223 | p += (n);                                                     \ | 
|  | 224 | } while (0) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | n1 = mbedtls_mpi_size(&ctx->P); | 
|  | 227 | n2 = mbedtls_mpi_size(&ctx->G); | 
|  | 228 | n3 = mbedtls_mpi_size(&ctx->GX); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 229 |  | 
|  | 230 | p = output; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | DHM_MPI_EXPORT(&ctx->P, n1); | 
|  | 232 | DHM_MPI_EXPORT(&ctx->G, n2); | 
|  | 233 | DHM_MPI_EXPORT(&ctx->GX, n3); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 |  | 
| Hanno Becker | e71ad12 | 2017-09-28 10:32:25 +0100 | [diff] [blame] | 235 | *olen = p - output; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 236 |  | 
|  | 237 | ctx->len = n1; | 
|  | 238 |  | 
|  | 239 | cleanup: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | if (ret != 0 && ret > -128) { | 
|  | 241 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED, ret); | 
|  | 242 | } | 
|  | 243 | return ret; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
|  | 246 | /* | 
| Hanno Becker | 8880e75 | 2017-10-04 13:15:08 +0100 | [diff] [blame] | 247 | * Set prime modulus and generator | 
|  | 248 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | int mbedtls_dhm_set_group(mbedtls_dhm_context *ctx, | 
|  | 250 | const mbedtls_mpi *P, | 
|  | 251 | const mbedtls_mpi *G) | 
| Hanno Becker | 8880e75 | 2017-10-04 13:15:08 +0100 | [diff] [blame] | 252 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 253 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | DHM_VALIDATE_RET(ctx != NULL); | 
|  | 255 | DHM_VALIDATE_RET(P != NULL); | 
|  | 256 | DHM_VALIDATE_RET(G != NULL); | 
| Hanno Becker | 8880e75 | 2017-10-04 13:15:08 +0100 | [diff] [blame] | 257 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | if ((ret = mbedtls_mpi_copy(&ctx->P, P)) != 0 || | 
|  | 259 | (ret = mbedtls_mpi_copy(&ctx->G, G)) != 0) { | 
|  | 260 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_SET_GROUP_FAILED, ret); | 
| Hanno Becker | 8880e75 | 2017-10-04 13:15:08 +0100 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 263 | ctx->len = mbedtls_mpi_size(&ctx->P); | 
|  | 264 | return 0; | 
| Hanno Becker | 8880e75 | 2017-10-04 13:15:08 +0100 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
|  | 267 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | * Import the peer's public value G^Y | 
|  | 269 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | int mbedtls_dhm_read_public(mbedtls_dhm_context *ctx, | 
|  | 271 | const unsigned char *input, size_t ilen) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 272 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 273 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 274 | DHM_VALIDATE_RET(ctx != NULL); | 
|  | 275 | DHM_VALIDATE_RET(input != NULL); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 276 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | if (ilen < 1 || ilen > ctx->len) { | 
|  | 278 | return MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
|  | 279 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 280 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 281 | if ((ret = mbedtls_mpi_read_binary(&ctx->GY, input, ilen)) != 0) { | 
|  | 282 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED, ret); | 
|  | 283 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 285 | return 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 286 | } | 
|  | 287 |  | 
|  | 288 | /* | 
|  | 289 | * Create own private value X and export G^X | 
|  | 290 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | int mbedtls_dhm_make_public(mbedtls_dhm_context *ctx, int x_size, | 
|  | 292 | unsigned char *output, size_t olen, | 
|  | 293 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 294 | void *p_rng) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 295 | { | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 296 | int ret; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | DHM_VALIDATE_RET(ctx != NULL); | 
|  | 298 | DHM_VALIDATE_RET(output != NULL); | 
|  | 299 | DHM_VALIDATE_RET(f_rng != NULL); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | if (olen < 1 || olen > ctx->len) { | 
|  | 302 | return MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
|  | 303 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 304 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 305 | ret = dhm_make_common(ctx, x_size, f_rng, p_rng); | 
|  | 306 | if (ret == MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED) { | 
|  | 307 | return MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED; | 
|  | 308 | } | 
|  | 309 | if (ret != 0) { | 
| Gilles Peskine | 0853bb2 | 2021-03-31 22:35:13 +0200 | [diff] [blame] | 310 | goto cleanup; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | } | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 312 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 313 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->GX, output, olen)); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 314 |  | 
|  | 315 | cleanup: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 316 | if (ret != 0 && ret > -128) { | 
|  | 317 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED, ret); | 
|  | 318 | } | 
|  | 319 | return ret; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| Manuel Pégourié-Gonnard | 9f58c4b | 2020-06-25 12:34:58 +0200 | [diff] [blame] | 322 |  | 
|  | 323 | /* | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 324 | * Use the blinding method and optimisation suggested in section 10 of: | 
|  | 325 | *  KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA, | 
| Manuel Pégourié-Gonnard | 998930a | 2015-04-03 13:48:06 +0200 | [diff] [blame] | 326 | *  DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 327 | *  Berlin Heidelberg, 1996. p. 104-113. | 
|  | 328 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 329 | static int dhm_update_blinding(mbedtls_dhm_context *ctx, | 
|  | 330 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 331 | { | 
| Manuel Pégourié-Gonnard | 9f58c4b | 2020-06-25 12:34:58 +0200 | [diff] [blame] | 332 | int ret; | 
| Manuel Pégourié-Gonnard | af72167 | 2020-06-25 12:47:22 +0200 | [diff] [blame] | 333 | mbedtls_mpi R; | 
|  | 334 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | mbedtls_mpi_init(&R); | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 336 |  | 
|  | 337 | /* | 
| Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 338 | * Don't use any blinding the first time a particular X is used, | 
|  | 339 | * but remember it to use blinding next time. | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 340 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | if (mbedtls_mpi_cmp_mpi(&ctx->X, &ctx->pX) != 0) { | 
|  | 342 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&ctx->pX, &ctx->X)); | 
|  | 343 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->Vi, 1)); | 
|  | 344 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&ctx->Vf, 1)); | 
| Manuel Pégourié-Gonnard | ed8a02b | 2013-09-04 16:39:03 +0200 | [diff] [blame] | 345 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 346 | return 0; | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
|  | 349 | /* | 
| Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 350 | * Ok, we need blinding. Can we re-use existing values? | 
|  | 351 | * If yes, just update them by squaring them. | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 352 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 353 | if (mbedtls_mpi_cmp_int(&ctx->Vi, 1) != 0) { | 
|  | 354 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi)); | 
|  | 355 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->P)); | 
| Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 356 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf)); | 
|  | 358 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P)); | 
| Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 359 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | return 0; | 
| Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
|  | 363 | /* | 
|  | 364 | * We need to generate blinding values from scratch | 
|  | 365 | */ | 
|  | 366 |  | 
| Gilles Peskine | b4e815f | 2021-03-31 22:50:57 +0200 | [diff] [blame] | 367 | /* Vi = random( 2, P-2 ) */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | MBEDTLS_MPI_CHK(dhm_random_below(&ctx->Vi, &ctx->P, f_rng, p_rng)); | 
| Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 369 |  | 
| Manuel Pégourié-Gonnard | af72167 | 2020-06-25 12:47:22 +0200 | [diff] [blame] | 370 | /* Vf = Vi^-X mod P | 
|  | 371 | * First compute Vi^-1 = R * (R Vi)^-1, (avoiding leaks from inv_mod), | 
|  | 372 | * then elevate to the Xth power. */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 373 | MBEDTLS_MPI_CHK(dhm_random_below(&R, &ctx->P, f_rng, p_rng)); | 
|  | 374 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vi, &R)); | 
|  | 375 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P)); | 
|  | 376 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&ctx->Vf, &ctx->Vf, &ctx->P)); | 
|  | 377 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &R)); | 
|  | 378 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->P)); | 
| Manuel Pégourié-Gonnard | 15d5de1 | 2013-09-17 11:34:11 +0200 | [diff] [blame] | 379 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP)); | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 381 |  | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 382 | cleanup: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | mbedtls_mpi_free(&R); | 
| Manuel Pégourié-Gonnard | af72167 | 2020-06-25 12:47:22 +0200 | [diff] [blame] | 384 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | return ret; | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 386 | } | 
|  | 387 |  | 
|  | 388 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 389 | * Derive and export the shared secret (G^Y)^X mod P | 
|  | 390 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | int mbedtls_dhm_calc_secret(mbedtls_dhm_context *ctx, | 
|  | 392 | unsigned char *output, size_t output_size, size_t *olen, | 
|  | 393 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 394 | void *p_rng) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 395 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 396 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 397 | mbedtls_mpi GYb; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | DHM_VALIDATE_RET(ctx != NULL); | 
|  | 399 | DHM_VALIDATE_RET(output != NULL); | 
|  | 400 | DHM_VALIDATE_RET(olen != NULL); | 
| Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 401 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | if (output_size < ctx->len) { | 
|  | 403 | return MBEDTLS_ERR_DHM_BAD_INPUT_DATA; | 
|  | 404 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 405 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | if ((ret = dhm_check_range(&ctx->GY, &ctx->P)) != 0) { | 
|  | 407 | return ret; | 
|  | 408 | } | 
| Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 409 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | mbedtls_mpi_init(&GYb); | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 411 |  | 
|  | 412 | /* Blind peer's value */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 413 | if (f_rng != NULL) { | 
|  | 414 | MBEDTLS_MPI_CHK(dhm_update_blinding(ctx, f_rng, p_rng)); | 
|  | 415 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&GYb, &ctx->GY, &ctx->Vi)); | 
|  | 416 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&GYb, &GYb, &ctx->P)); | 
|  | 417 | } else { | 
|  | 418 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&GYb, &ctx->GY)); | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 419 | } | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 420 |  | 
|  | 421 | /* Do modular exponentiation */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 422 | MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->K, &GYb, &ctx->X, | 
|  | 423 | &ctx->P, &ctx->RP)); | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 424 |  | 
|  | 425 | /* Unblind secret value */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 426 | if (f_rng != NULL) { | 
|  | 427 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->K, &ctx->K, &ctx->Vf)); | 
|  | 428 | MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->K, &ctx->K, &ctx->P)); | 
| Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 429 | } | 
|  | 430 |  | 
| Gilles Peskine | 104eb82 | 2021-04-13 22:10:24 +0200 | [diff] [blame] | 431 | /* Output the secret without any leading zero byte. This is mandatory | 
|  | 432 | * for TLS per RFC 5246 §8.1.2. */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | *olen = mbedtls_mpi_size(&ctx->K); | 
|  | 434 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&ctx->K, output, *olen)); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 435 |  | 
|  | 436 | cleanup: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 437 | mbedtls_mpi_free(&GYb); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 438 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 439 | if (ret != 0) { | 
|  | 440 | return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_CALC_SECRET_FAILED, ret); | 
|  | 441 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 442 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 443 | return 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 444 | } | 
|  | 445 |  | 
|  | 446 | /* | 
|  | 447 | * Free the components of a DHM key | 
|  | 448 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 449 | void mbedtls_dhm_free(mbedtls_dhm_context *ctx) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 450 | { | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 451 | if (ctx == NULL) { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 452 | return; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 453 | } | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 454 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 455 | mbedtls_mpi_free(&ctx->pX); | 
|  | 456 | mbedtls_mpi_free(&ctx->Vf); | 
|  | 457 | mbedtls_mpi_free(&ctx->Vi); | 
|  | 458 | mbedtls_mpi_free(&ctx->RP); | 
|  | 459 | mbedtls_mpi_free(&ctx->K); | 
|  | 460 | mbedtls_mpi_free(&ctx->GY); | 
|  | 461 | mbedtls_mpi_free(&ctx->GX); | 
|  | 462 | mbedtls_mpi_free(&ctx->X); | 
|  | 463 | mbedtls_mpi_free(&ctx->G); | 
|  | 464 | mbedtls_mpi_free(&ctx->P); | 
| Manuel Pégourié-Gonnard | b72b4ed | 2013-09-13 13:55:26 +0200 | [diff] [blame] | 465 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 466 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_dhm_context)); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 467 | } | 
|  | 468 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 469 | #if defined(MBEDTLS_ASN1_PARSE_C) | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 470 | /* | 
|  | 471 | * Parse DHM parameters | 
|  | 472 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 473 | int mbedtls_dhm_parse_dhm(mbedtls_dhm_context *dhm, const unsigned char *dhmin, | 
|  | 474 | size_t dhminlen) | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 475 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 476 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 477 | size_t len; | 
|  | 478 | unsigned char *p, *end; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | #if defined(MBEDTLS_PEM_PARSE_C) | 
|  | 480 | mbedtls_pem_context pem; | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 481 | #endif /* MBEDTLS_PEM_PARSE_C */ | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 482 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 483 | DHM_VALIDATE_RET(dhm != NULL); | 
|  | 484 | DHM_VALIDATE_RET(dhmin != NULL); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 485 |  | 
|  | 486 | #if defined(MBEDTLS_PEM_PARSE_C) | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 487 | mbedtls_pem_init(&pem); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 488 |  | 
| Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 489 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 490 | if (dhminlen == 0 || dhmin[dhminlen - 1] != '\0') { | 
| Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 491 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 492 | } else { | 
|  | 493 | ret = mbedtls_pem_read_buffer(&pem, | 
|  | 494 | "-----BEGIN DH PARAMETERS-----", | 
|  | 495 | "-----END DH PARAMETERS-----", | 
|  | 496 | dhmin, NULL, 0, &dhminlen); | 
|  | 497 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 498 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 499 | if (ret == 0) { | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 500 | /* | 
|  | 501 | * Was PEM encoded | 
|  | 502 | */ | 
|  | 503 | dhminlen = pem.buflen; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 504 | } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) { | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 505 | goto exit; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 506 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 507 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | p = (ret == 0) ? pem.buf : (unsigned char *) dhmin; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 509 | #else | 
|  | 510 | p = (unsigned char *) dhmin; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | #endif /* MBEDTLS_PEM_PARSE_C */ | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 512 | end = p + dhminlen; | 
|  | 513 |  | 
|  | 514 | /* | 
|  | 515 | *  DHParams ::= SEQUENCE { | 
| Daniel Kahn Gillmor | 2ed8173 | 2015-04-03 13:09:24 -0400 | [diff] [blame] | 516 | *      prime              INTEGER,  -- P | 
|  | 517 | *      generator          INTEGER,  -- g | 
|  | 518 | *      privateValueLength INTEGER OPTIONAL | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 519 | *  } | 
|  | 520 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 521 | if ((ret = mbedtls_asn1_get_tag(&p, end, &len, | 
|  | 522 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { | 
|  | 523 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT, ret); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 524 | goto exit; | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | end = p + len; | 
|  | 528 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 529 | if ((ret = mbedtls_asn1_get_mpi(&p, end, &dhm->P)) != 0 || | 
|  | 530 | (ret = mbedtls_asn1_get_mpi(&p, end, &dhm->G)) != 0) { | 
|  | 531 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT, ret); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 532 | goto exit; | 
|  | 533 | } | 
|  | 534 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 535 | if (p != end) { | 
| Manuel Pégourié-Gonnard | de9b363 | 2015-04-17 20:06:31 +0200 | [diff] [blame] | 536 | /* This might be the optional privateValueLength. | 
|  | 537 | * If so, we can cleanly discard it */ | 
|  | 538 | mbedtls_mpi rec; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 539 | mbedtls_mpi_init(&rec); | 
|  | 540 | ret = mbedtls_asn1_get_mpi(&p, end, &rec); | 
|  | 541 | mbedtls_mpi_free(&rec); | 
|  | 542 | if (ret != 0) { | 
|  | 543 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT, ret); | 
| Daniel Kahn Gillmor | 2ed8173 | 2015-04-03 13:09:24 -0400 | [diff] [blame] | 544 | goto exit; | 
|  | 545 | } | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 546 | if (p != end) { | 
|  | 547 | ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_DHM_INVALID_FORMAT, | 
|  | 548 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH); | 
| Daniel Kahn Gillmor | 2ed8173 | 2015-04-03 13:09:24 -0400 | [diff] [blame] | 549 | goto exit; | 
|  | 550 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 551 | } | 
|  | 552 |  | 
|  | 553 | ret = 0; | 
|  | 554 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 555 | dhm->len = mbedtls_mpi_size(&dhm->P); | 
| Manuel Pégourié-Gonnard | 3fec220 | 2014-03-29 16:42:38 +0100 | [diff] [blame] | 556 |  | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 557 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 558 | #if defined(MBEDTLS_PEM_PARSE_C) | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 559 | mbedtls_pem_free(&pem); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 560 | #endif | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 561 | if (ret != 0) { | 
|  | 562 | mbedtls_dhm_free(dhm); | 
|  | 563 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 564 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 565 | return ret; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 566 | } | 
|  | 567 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | #if defined(MBEDTLS_FS_IO) | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 569 | /* | 
|  | 570 | * Load all data from a file into a given buffer. | 
| Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 571 | * | 
|  | 572 | * The file is expected to contain either PEM or DER encoded data. | 
|  | 573 | * A terminating null byte is always appended. It is included in the announced | 
|  | 574 | * length only if the data looks like it is PEM encoded. | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 575 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 576 | static int load_file(const char *path, unsigned char **buf, size_t *n) | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 577 | { | 
|  | 578 | FILE *f; | 
|  | 579 | long size; | 
|  | 580 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 581 | if ((f = fopen(path, "rb")) == NULL) { | 
|  | 582 | return MBEDTLS_ERR_DHM_FILE_IO_ERROR; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 583 | } | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 |  | 
|  | 585 | fseek(f, 0, SEEK_END); | 
|  | 586 | if ((size = ftell(f)) == -1) { | 
|  | 587 | fclose(f); | 
|  | 588 | return MBEDTLS_ERR_DHM_FILE_IO_ERROR; | 
|  | 589 | } | 
|  | 590 | fseek(f, 0, SEEK_SET); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 591 |  | 
|  | 592 | *n = (size_t) size; | 
|  | 593 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 594 | if (*n + 1 == 0 || | 
|  | 595 | (*buf = mbedtls_calloc(1, *n + 1)) == NULL) { | 
|  | 596 | fclose(f); | 
|  | 597 | return MBEDTLS_ERR_DHM_ALLOC_FAILED; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 598 | } | 
|  | 599 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 600 | if (fread(*buf, 1, *n, f) != *n) { | 
|  | 601 | fclose(f); | 
| Andres Amaya Garcia | bdbca7b | 2017-06-23 16:23:21 +0100 | [diff] [blame] | 602 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 603 | mbedtls_platform_zeroize(*buf, *n + 1); | 
|  | 604 | mbedtls_free(*buf); | 
| Andres Amaya Garcia | bdbca7b | 2017-06-23 16:23:21 +0100 | [diff] [blame] | 605 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 606 | return MBEDTLS_ERR_DHM_FILE_IO_ERROR; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 607 | } | 
|  | 608 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 609 | fclose(f); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 610 |  | 
|  | 611 | (*buf)[*n] = '\0'; | 
|  | 612 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 613 | if (strstr((const char *) *buf, "-----BEGIN ") != NULL) { | 
| Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 614 | ++*n; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 615 | } | 
| Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 616 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 617 | return 0; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 618 | } | 
|  | 619 |  | 
|  | 620 | /* | 
|  | 621 | * Load and parse DHM parameters | 
|  | 622 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 623 | int mbedtls_dhm_parse_dhmfile(mbedtls_dhm_context *dhm, const char *path) | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 624 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 625 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 626 | size_t n; | 
|  | 627 | unsigned char *buf; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 628 | DHM_VALIDATE_RET(dhm != NULL); | 
|  | 629 | DHM_VALIDATE_RET(path != NULL); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 630 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 631 | if ((ret = load_file(path, &buf, &n)) != 0) { | 
|  | 632 | return ret; | 
|  | 633 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 634 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 635 | ret = mbedtls_dhm_parse_dhm(dhm, buf, n); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 636 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 637 | mbedtls_platform_zeroize(buf, n); | 
|  | 638 | mbedtls_free(buf); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 639 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 640 | return ret; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 641 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 642 | #endif /* MBEDTLS_FS_IO */ | 
|  | 643 | #endif /* MBEDTLS_ASN1_PARSE_C */ | 
| nirekh01 | 49762fa | 2017-12-25 06:46:48 +0000 | [diff] [blame] | 644 | #endif /* MBEDTLS_DHM_ALT */ | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 645 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 646 | #if defined(MBEDTLS_SELF_TEST) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 647 |  | 
| Hanno Becker | 8b0f9e6 | 2019-05-31 17:28:59 +0100 | [diff] [blame] | 648 | #if defined(MBEDTLS_PEM_PARSE_C) | 
| Manuel Pégourié-Gonnard | 53585ee | 2015-06-25 08:52:25 +0200 | [diff] [blame] | 649 | static const char mbedtls_test_dhm_params[] = | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 650 | "-----BEGIN DH PARAMETERS-----\r\n" | 
|  | 651 | "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n" | 
|  | 652 | "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n" | 
|  | 653 | "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n" | 
|  | 654 | "-----END DH PARAMETERS-----\r\n"; | 
| Hanno Becker | 8b0f9e6 | 2019-05-31 17:28:59 +0100 | [diff] [blame] | 655 | #else /* MBEDTLS_PEM_PARSE_C */ | 
|  | 656 | static const char mbedtls_test_dhm_params[] = { | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 657 | 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0x9e, 0x35, 0xf4, 0x30, 0x44, | 
|  | 658 | 0x3a, 0x09, 0x90, 0x4f, 0x3a, 0x39, 0xa9, 0x79, 0x79, 0x7d, 0x07, 0x0d, | 
|  | 659 | 0xf5, 0x33, 0x78, 0xe7, 0x9c, 0x24, 0x38, 0xbe, 0xf4, 0xe7, 0x61, 0xf3, | 
|  | 660 | 0xc7, 0x14, 0x55, 0x33, 0x28, 0x58, 0x9b, 0x04, 0x1c, 0x80, 0x9b, 0xe1, | 
|  | 661 | 0xd6, 0xc6, 0xb5, 0xf1, 0xfc, 0x9f, 0x47, 0xd3, 0xa2, 0x54, 0x43, 0x18, | 
|  | 662 | 0x82, 0x53, 0xa9, 0x92, 0xa5, 0x68, 0x18, 0xb3, 0x7b, 0xa9, 0xde, 0x5a, | 
|  | 663 | 0x40, 0xd3, 0x62, 0xe5, 0x6e, 0xff, 0x0b, 0xe5, 0x41, 0x74, 0x74, 0xc1, | 
|  | 664 | 0x25, 0xc1, 0x99, 0x27, 0x2c, 0x8f, 0xe4, 0x1d, 0xea, 0x73, 0x3d, 0xf6, | 
|  | 665 | 0xf6, 0x62, 0xc9, 0x2a, 0xe7, 0x65, 0x56, 0xe7, 0x55, 0xd1, 0x0c, 0x64, | 
|  | 666 | 0xe6, 0xa5, 0x09, 0x68, 0xf6, 0x7f, 0xc6, 0xea, 0x73, 0xd0, 0xdc, 0xa8, | 
|  | 667 | 0x56, 0x9b, 0xe2, 0xba, 0x20, 0x4e, 0x23, 0x58, 0x0d, 0x8b, 0xca, 0x2f, | 
|  | 668 | 0x49, 0x75, 0xb3, 0x02, 0x01, 0x02 | 
|  | 669 | }; | 
| Hanno Becker | 8b0f9e6 | 2019-05-31 17:28:59 +0100 | [diff] [blame] | 670 | #endif /* MBEDTLS_PEM_PARSE_C */ | 
| Manuel Pégourié-Gonnard | 53585ee | 2015-06-25 08:52:25 +0200 | [diff] [blame] | 671 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 672 | static const size_t mbedtls_test_dhm_params_len = sizeof(mbedtls_test_dhm_params); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 673 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 674 | /* | 
|  | 675 | * Checkup routine | 
|  | 676 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 677 | int mbedtls_dhm_self_test(int verbose) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 678 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 679 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | mbedtls_dhm_context dhm; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 681 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 682 | mbedtls_dhm_init(&dhm); | 
| Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 683 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 684 | if (verbose != 0) { | 
|  | 685 | mbedtls_printf("  DHM parameter load: "); | 
|  | 686 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 687 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 688 | if ((ret = mbedtls_dhm_parse_dhm(&dhm, | 
|  | 689 | (const unsigned char *) mbedtls_test_dhm_params, | 
|  | 690 | mbedtls_test_dhm_params_len)) != 0) { | 
|  | 691 | if (verbose != 0) { | 
|  | 692 | mbedtls_printf("failed\n"); | 
|  | 693 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 694 |  | 
| Manuel Pégourié-Gonnard | b196fc2 | 2014-07-09 16:53:29 +0200 | [diff] [blame] | 695 | ret = 1; | 
| Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 696 | goto exit; | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 697 | } | 
|  | 698 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 699 | if (verbose != 0) { | 
|  | 700 | mbedtls_printf("passed\n\n"); | 
|  | 701 | } | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 702 |  | 
| Paul Bakker | 8f870b0 | 2014-06-20 13:32:38 +0200 | [diff] [blame] | 703 | exit: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 704 | mbedtls_dhm_free(&dhm); | 
| Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 705 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 706 | return ret; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 707 | } | 
|  | 708 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | #endif /* MBEDTLS_SELF_TEST */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 710 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | #endif /* MBEDTLS_DHM_C */ |