Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /* |
Manuel Pégourié-Gonnard | 32b04c1 | 2013-12-02 15:49:09 +0100 | [diff] [blame] | 2 | * Elliptic curves over GF(p): generic functions |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * References: |
| 22 | * |
Xiaokang Qian | 0e5a27b | 2023-04-21 09:58:07 +0000 | [diff] [blame] | 23 | * SEC1 https://www.secg.org/sec1-v2.pdf |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 24 | * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 25 | * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 26 | * RFC 4492 for the related TLS structures and constants |
Xiaokang Qian | 0e5a27b | 2023-04-21 09:58:07 +0000 | [diff] [blame] | 27 | * - https://www.rfc-editor.org/rfc/rfc4492 |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 28 | * RFC 7748 for the Curve448 and Curve25519 curve definitions |
Xiaokang Qian | 0e5a27b | 2023-04-21 09:58:07 +0000 | [diff] [blame] | 29 | * - https://www.rfc-editor.org/rfc/rfc7748 |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 30 | * |
Xiaokang Qian | 0e5a27b | 2023-04-21 09:58:07 +0000 | [diff] [blame] | 31 | * [Curve25519] https://cr.yp.to/ecdh/curve25519-20060209.pdf |
Manuel Pégourié-Gonnard | fe0af40 | 2013-12-04 18:14:55 +0100 | [diff] [blame] | 32 | * |
Manuel Pégourié-Gonnard | 998930a | 2015-04-03 13:48:06 +0200 | [diff] [blame] | 33 | * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 34 | * for elliptic curve cryptosystems. In : Cryptographic Hardware and |
| 35 | * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302. |
| 36 | * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25> |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 37 | * |
Manuel Pégourié-Gonnard | 998930a | 2015-04-03 13:48:06 +0200 | [diff] [blame] | 38 | * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 39 | * render ECC resistant against Side Channel Attacks. IACR Cryptology |
| 40 | * ePrint Archive, 2004, vol. 2004, p. 342. |
| 41 | * <http://eprint.iacr.org/2004/342.pdf> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 42 | */ |
| 43 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 44 | #include "common.h" |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 45 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 46 | /** |
| 47 | * \brief Function level alternative implementation. |
| 48 | * |
| 49 | * The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to |
| 50 | * replace certain functions in this module. The alternative implementations are |
| 51 | * typically hardware accelerators and need to activate the hardware before the |
| 52 | * computation starts and deactivate it after it finishes. The |
| 53 | * mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve |
| 54 | * this purpose. |
| 55 | * |
| 56 | * To preserve the correct functionality the following conditions must hold: |
| 57 | * |
| 58 | * - The alternative implementation must be activated by |
| 59 | * mbedtls_internal_ecp_init() before any of the replaceable functions is |
| 60 | * called. |
| 61 | * - mbedtls_internal_ecp_free() must \b only be called when the alternative |
| 62 | * implementation is activated. |
| 63 | * - mbedtls_internal_ecp_init() must \b not be called when the alternative |
| 64 | * implementation is activated. |
| 65 | * - Public functions must not return while the alternative implementation is |
| 66 | * activated. |
| 67 | * - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and |
| 68 | * before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) ) |
| 69 | * \endcode ensures that the alternative implementation supports the current |
| 70 | * group. |
| 71 | */ |
| 72 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
| 73 | #endif |
| 74 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 76 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 77 | #include "mbedtls/ecp.h" |
Janos Follath | 430d337 | 2016-11-03 14:25:37 +0000 | [diff] [blame] | 78 | #include "mbedtls/threading.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 79 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 80 | #include "mbedtls/error.h" |
Janos Follath | bc96a79 | 2021-06-24 14:48:38 +0100 | [diff] [blame] | 81 | #include "mbedtls/bn_mul.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 82 | |
Gilles Peskine | 80ba850 | 2021-04-03 20:36:37 +0200 | [diff] [blame] | 83 | #include "ecp_invasive.h" |
| 84 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 85 | #include <string.h> |
| 86 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 87 | #if !defined(MBEDTLS_ECP_ALT) |
| 88 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 89 | /* Parameter validation macros based on platform_util.h */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | #define ECP_VALIDATE_RET(cond) \ |
| 91 | MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA) |
| 92 | #define ECP_VALIDATE(cond) \ |
| 93 | MBEDTLS_INTERNAL_VALIDATE(cond) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 94 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 95 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 96 | |
Janos Follath | 47d28f0 | 2016-11-01 13:22:05 +0000 | [diff] [blame] | 97 | #include "mbedtls/ecp_internal.h" |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 98 | |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 99 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 100 | #if defined(MBEDTLS_HMAC_DRBG_C) |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 101 | #include "mbedtls/hmac_drbg.h" |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 102 | #elif defined(MBEDTLS_CTR_DRBG_C) |
| 103 | #include "mbedtls/ctr_drbg.h" |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 104 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | #error \ |
| 106 | "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 107 | #endif |
| 108 | #endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */ |
| 109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 111 | /* |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 112 | * Counts of point addition and doubling, and field multiplications. |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 113 | * Used to test resistance of point multiplication to simple timing attacks. |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 114 | */ |
Manuel Pégourié-Gonnard | 43863ee | 2013-12-01 16:51:27 +0100 | [diff] [blame] | 115 | static unsigned long add_count, dbl_count, mul_count; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 116 | #endif |
| 117 | |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 118 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
| 119 | /* |
| 120 | * Currently ecp_mul() takes a RNG function as an argument, used for |
Manuel Pégourié-Gonnard | 25705e6 | 2020-06-10 09:18:25 +0200 | [diff] [blame] | 121 | * side-channel protection, but it can be NULL. The initial reasoning was |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 122 | * that people will pass non-NULL RNG when they care about side-channels, but |
| 123 | * unfortunately we have some APIs that call ecp_mul() with a NULL RNG, with |
| 124 | * no opportunity for the user to do anything about it. |
| 125 | * |
| 126 | * The obvious strategies for addressing that include: |
| 127 | * - change those APIs so that they take RNG arguments; |
| 128 | * - require a global RNG to be available to all crypto modules. |
| 129 | * |
| 130 | * Unfortunately those would break compatibility. So what we do instead is |
| 131 | * have our own internal DRBG instance, seeded from the secret scalar. |
| 132 | * |
| 133 | * The following is a light-weight abstraction layer for doing that with |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 134 | * HMAC_DRBG (first choice) or CTR_DRBG. |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 135 | */ |
| 136 | |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 137 | #if defined(MBEDTLS_HMAC_DRBG_C) |
| 138 | |
| 139 | /* DRBG context type */ |
| 140 | typedef mbedtls_hmac_drbg_context ecp_drbg_context; |
| 141 | |
| 142 | /* DRBG context init */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 143 | static inline void ecp_drbg_init(ecp_drbg_context *ctx) |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 144 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | mbedtls_hmac_drbg_init(ctx); |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /* DRBG context free */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 149 | static inline void ecp_drbg_free(ecp_drbg_context *ctx) |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 150 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | mbedtls_hmac_drbg_free(ctx); |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* DRBG function */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 155 | static inline int ecp_drbg_random(void *p_rng, |
| 156 | unsigned char *output, size_t output_len) |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 157 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 158 | return mbedtls_hmac_drbg_random(p_rng, output, output_len); |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* DRBG context seeding */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | static int ecp_drbg_seed(ecp_drbg_context *ctx, |
| 163 | const mbedtls_mpi *secret, size_t secret_len) |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 164 | { |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 165 | int ret; |
| 166 | unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES]; |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 167 | /* The list starts with strong hashes */ |
Paul Elliott | fc820d9 | 2023-01-13 16:29:30 +0000 | [diff] [blame] | 168 | const mbedtls_md_type_t md_type = |
| 169 | (const mbedtls_md_type_t) (mbedtls_md_list()[0]); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | if (secret_len > MBEDTLS_ECP_MAX_BYTES) { |
Manuel Pégourié-Gonnard | 1215c54 | 2020-06-19 11:59:49 +0200 | [diff] [blame] | 173 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 174 | goto cleanup; |
| 175 | } |
| 176 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(secret, |
| 178 | secret_bytes, secret_len)); |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | ret = mbedtls_hmac_drbg_seed_buf(ctx, md_info, secret_bytes, secret_len); |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 181 | |
| 182 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | mbedtls_platform_zeroize(secret_bytes, secret_len); |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | return ret; |
Manuel Pégourié-Gonnard | d53ef2f | 2020-06-04 12:32:14 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | #elif defined(MBEDTLS_CTR_DRBG_C) |
| 189 | |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 190 | /* DRBG context type */ |
| 191 | typedef mbedtls_ctr_drbg_context ecp_drbg_context; |
| 192 | |
| 193 | /* DRBG context init */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | static inline void ecp_drbg_init(ecp_drbg_context *ctx) |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 195 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | mbedtls_ctr_drbg_init(ctx); |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* DRBG context free */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | static inline void ecp_drbg_free(ecp_drbg_context *ctx) |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 201 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | mbedtls_ctr_drbg_free(ctx); |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* DRBG function */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 206 | static inline int ecp_drbg_random(void *p_rng, |
| 207 | unsigned char *output, size_t output_len) |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 208 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | return mbedtls_ctr_drbg_random(p_rng, output, output_len); |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Since CTR_DRBG doesn't have a seed_buf() function the way HMAC_DRBG does, |
| 214 | * we need to pass an entropy function when seeding. So we use a dummy |
| 215 | * function for that, and pass the actual entropy as customisation string. |
| 216 | * (During seeding of CTR_DRBG the entropy input and customisation string are |
| 217 | * concatenated before being used to update the secret state.) |
| 218 | */ |
| 219 | static int ecp_ctr_drbg_null_entropy(void *ctx, unsigned char *out, size_t len) |
| 220 | { |
| 221 | (void) ctx; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | memset(out, 0, len); |
| 223 | return 0; |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* DRBG context seeding */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 227 | static int ecp_drbg_seed(ecp_drbg_context *ctx, |
| 228 | const mbedtls_mpi *secret, size_t secret_len) |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 229 | { |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 230 | int ret; |
| 231 | unsigned char secret_bytes[MBEDTLS_ECP_MAX_BYTES]; |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | if (secret_len > MBEDTLS_ECP_MAX_BYTES) { |
Manuel Pégourié-Gonnard | 1215c54 | 2020-06-19 11:59:49 +0200 | [diff] [blame] | 234 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
| 235 | goto cleanup; |
| 236 | } |
| 237 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(secret, |
| 239 | secret_bytes, secret_len)); |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | ret = mbedtls_ctr_drbg_seed(ctx, ecp_ctr_drbg_null_entropy, NULL, |
| 242 | secret_bytes, secret_len); |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 243 | |
| 244 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 245 | mbedtls_platform_zeroize(secret_bytes, secret_len); |
Manuel Pégourié-Gonnard | 4539a45 | 2020-06-18 12:27:56 +0200 | [diff] [blame] | 246 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | return ret; |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 250 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | #error \ |
| 252 | "Invalid configuration detected. Include check_config.h to ensure that the configuration is valid." |
Manuel Pégourié-Gonnard | c52a43c | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 253 | #endif /* DRBG modules */ |
| 254 | #endif /* MBEDTLS_ECP_NO_INTERNAL_RNG */ |
| 255 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 256 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 257 | /* |
| 258 | * Maximum number of "basic operations" to be done in a row. |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 259 | * |
| 260 | * Default value 0 means that ECC operations will not yield. |
| 261 | * Note that regardless of the value of ecp_max_ops, always at |
| 262 | * least one step is performed before yielding. |
| 263 | * |
| 264 | * Setting ecp_max_ops=1 can be suitable for testing purposes |
| 265 | * as it will interrupt computation at all possible points. |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 266 | */ |
| 267 | static unsigned ecp_max_ops = 0; |
| 268 | |
| 269 | /* |
| 270 | * Set ecp_max_ops |
| 271 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | void mbedtls_ecp_set_max_ops(unsigned max_ops) |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 273 | { |
| 274 | ecp_max_ops = max_ops; |
| 275 | } |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 276 | |
| 277 | /* |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 278 | * Check if restart is enabled |
| 279 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 280 | int mbedtls_ecp_restart_is_enabled(void) |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 281 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | return ecp_max_ops != 0; |
Manuel Pégourié-Gonnard | a0c5bcc | 2017-04-21 11:33:57 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /* |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 286 | * Restart sub-context for ecp_mul_comb() |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 287 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 288 | struct mbedtls_ecp_restart_mul { |
Manuel Pégourié-Gonnard | 8962ddb | 2017-03-14 12:11:21 +0100 | [diff] [blame] | 289 | mbedtls_ecp_point R; /* current intermediate result */ |
Manuel Pégourié-Gonnard | c5d844b | 2017-03-15 13:06:28 +0100 | [diff] [blame] | 290 | size_t i; /* current index in various loops, 0 outside */ |
Manuel Pégourié-Gonnard | c9c0aa6 | 2017-03-16 14:53:26 +0100 | [diff] [blame] | 291 | mbedtls_ecp_point *T; /* table for precomputed points */ |
| 292 | unsigned char T_size; /* number of points in table T */ |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 293 | enum { /* what were we doing last time we returned? */ |
| 294 | ecp_rsm_init = 0, /* nothing so far, dummy initial state */ |
| 295 | ecp_rsm_pre_dbl, /* precompute 2^n multiples */ |
Manuel Pégourié-Gonnard | 45fd016 | 2017-03-22 08:24:42 +0100 | [diff] [blame] | 296 | ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */ |
| 297 | ecp_rsm_pre_add, /* precompute remaining points by adding */ |
| 298 | ecp_rsm_pre_norm_add, /* normalize all precomputed points */ |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 299 | ecp_rsm_comb_core, /* ecp_mul_comb_core() */ |
Manuel Pégourié-Gonnard | 45fd016 | 2017-03-22 08:24:42 +0100 | [diff] [blame] | 300 | ecp_rsm_final_norm, /* do the final normalization */ |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 301 | } state; |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 302 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
| 303 | ecp_drbg_context drbg_ctx; |
| 304 | unsigned char drbg_seeded; |
| 305 | #endif |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 306 | }; |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 307 | |
| 308 | /* |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 309 | * Init restart_mul sub-context |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 310 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 311 | static void ecp_restart_rsm_init(mbedtls_ecp_restart_mul_ctx *ctx) |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 312 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 313 | mbedtls_ecp_point_init(&ctx->R); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 314 | ctx->i = 0; |
| 315 | ctx->T = NULL; |
| 316 | ctx->T_size = 0; |
| 317 | ctx->state = ecp_rsm_init; |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 318 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 319 | ecp_drbg_init(&ctx->drbg_ctx); |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 320 | ctx->drbg_seeded = 0; |
| 321 | #endif |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 325 | * Free the components of a restart_mul sub-context |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 326 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 327 | static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx) |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 328 | { |
Manuel Pégourié-Gonnard | c9c0aa6 | 2017-03-16 14:53:26 +0100 | [diff] [blame] | 329 | unsigned char i; |
| 330 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 331 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 332 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 333 | } |
Manuel Pégourié-Gonnard | 78d564a | 2017-03-14 11:48:38 +0100 | [diff] [blame] | 334 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | mbedtls_ecp_point_free(&ctx->R); |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | if (ctx->T != NULL) { |
| 338 | for (i = 0; i < ctx->T_size; i++) { |
| 339 | mbedtls_ecp_point_free(ctx->T + i); |
| 340 | } |
| 341 | mbedtls_free(ctx->T); |
Manuel Pégourié-Gonnard | c9c0aa6 | 2017-03-16 14:53:26 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 344 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 345 | ecp_drbg_free(&ctx->drbg_ctx); |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 346 | #endif |
| 347 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 348 | ecp_restart_rsm_init(ctx); |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 349 | } |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 350 | |
| 351 | /* |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 352 | * Restart context for ecp_muladd() |
| 353 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 354 | struct mbedtls_ecp_restart_muladd { |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 355 | mbedtls_ecp_point mP; /* mP value */ |
| 356 | mbedtls_ecp_point R; /* R intermediate result */ |
| 357 | enum { /* what should we do next? */ |
| 358 | ecp_rsma_mul1 = 0, /* first multiplication */ |
| 359 | ecp_rsma_mul2, /* second multiplication */ |
| 360 | ecp_rsma_add, /* addition */ |
| 361 | ecp_rsma_norm, /* normalization */ |
| 362 | } state; |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | /* |
| 366 | * Init restart_muladd sub-context |
| 367 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | static void ecp_restart_ma_init(mbedtls_ecp_restart_muladd_ctx *ctx) |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 369 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 370 | mbedtls_ecp_point_init(&ctx->mP); |
| 371 | mbedtls_ecp_point_init(&ctx->R); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 372 | ctx->state = ecp_rsma_mul1; |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Free the components of a restart_muladd sub-context |
| 377 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 378 | static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx) |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 379 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 381 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 382 | } |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 383 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 384 | mbedtls_ecp_point_free(&ctx->mP); |
| 385 | mbedtls_ecp_point_free(&ctx->R); |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 386 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 387 | ecp_restart_ma_init(ctx); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 391 | * Initialize a restart context |
| 392 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 394 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 395 | ECP_VALIDATE(ctx != NULL); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 396 | ctx->ops_done = 0; |
| 397 | ctx->depth = 0; |
| 398 | ctx->rsm = NULL; |
| 399 | ctx->ma = NULL; |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Free the components of a restart context |
| 404 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 405 | void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx) |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 406 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 407 | if (ctx == NULL) { |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 408 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 409 | } |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 410 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | ecp_restart_rsm_free(ctx->rsm); |
| 412 | mbedtls_free(ctx->rsm); |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 413 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | ecp_restart_ma_free(ctx->ma); |
| 415 | mbedtls_free(ctx->ma); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 416 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 417 | mbedtls_ecp_restart_init(ctx); |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 421 | * Check if we can do the next step |
| 422 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 423 | int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp, |
| 424 | mbedtls_ecp_restart_ctx *rs_ctx, |
| 425 | unsigned ops) |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 426 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 427 | ECP_VALIDATE_RET(grp != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 429 | if (rs_ctx != NULL && ecp_max_ops != 0) { |
Manuel Pégourié-Gonnard | e685449 | 2017-03-20 14:35:19 +0100 | [diff] [blame] | 430 | /* scale depending on curve size: the chosen reference is 256-bit, |
| 431 | * and multiplication is quadratic. Round to the closest integer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 432 | if (grp->pbits >= 512) { |
Manuel Pégourié-Gonnard | e685449 | 2017-03-20 14:35:19 +0100 | [diff] [blame] | 433 | ops *= 4; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 434 | } else if (grp->pbits >= 384) { |
Manuel Pégourié-Gonnard | e685449 | 2017-03-20 14:35:19 +0100 | [diff] [blame] | 435 | ops *= 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | } |
Manuel Pégourié-Gonnard | e685449 | 2017-03-20 14:35:19 +0100 | [diff] [blame] | 437 | |
Hanno Becker | b10c660 | 2018-10-26 13:50:13 +0100 | [diff] [blame] | 438 | /* Avoid infinite loops: always allow first step. |
| 439 | * Because of that, however, it's not generally true |
| 440 | * that ops_done <= ecp_max_ops, so the check |
| 441 | * ops_done > ecp_max_ops below is mandatory. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | if ((rs_ctx->ops_done != 0) && |
| 443 | (rs_ctx->ops_done > ecp_max_ops || |
| 444 | ops > ecp_max_ops - rs_ctx->ops_done)) { |
| 445 | return MBEDTLS_ERR_ECP_IN_PROGRESS; |
Hanno Becker | b10c660 | 2018-10-26 13:50:13 +0100 | [diff] [blame] | 446 | } |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 447 | |
Manuel Pégourié-Gonnard | e685449 | 2017-03-20 14:35:19 +0100 | [diff] [blame] | 448 | /* update running count */ |
Manuel Pégourié-Gonnard | 646393b | 2017-04-20 10:03:45 +0200 | [diff] [blame] | 449 | rs_ctx->ops_done += ops; |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 450 | } |
| 451 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | return 0; |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Manuel Pégourié-Gonnard | db4a8eb | 2017-08-23 18:18:22 +0200 | [diff] [blame] | 455 | /* Call this when entering a function that needs its own sub-context */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 456 | #define ECP_RS_ENTER(SUB) do { \ |
| 457 | /* reset ops count for this call if top-level */ \ |
| 458 | if (rs_ctx != NULL && rs_ctx->depth++ == 0) \ |
Manuel Pégourié-Gonnard | a58e011 | 2018-10-16 10:42:47 +0200 | [diff] [blame] | 459 | rs_ctx->ops_done = 0; \ |
| 460 | \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | /* set up our own sub-context if needed */ \ |
| 462 | if (mbedtls_ecp_restart_is_enabled() && \ |
| 463 | rs_ctx != NULL && rs_ctx->SUB == NULL) \ |
| 464 | { \ |
| 465 | rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \ |
| 466 | if (rs_ctx->SUB == NULL) \ |
| 467 | return MBEDTLS_ERR_ECP_ALLOC_FAILED; \ |
| 468 | \ |
| 469 | ecp_restart_## SUB ##_init(rs_ctx->SUB); \ |
| 470 | } \ |
| 471 | } while (0) |
Manuel Pégourié-Gonnard | db4a8eb | 2017-08-23 18:18:22 +0200 | [diff] [blame] | 472 | |
| 473 | /* Call this when leaving a function that needs its own sub-context */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 474 | #define ECP_RS_LEAVE(SUB) do { \ |
| 475 | /* clear our sub-context when not in progress (done or error) */ \ |
| 476 | if (rs_ctx != NULL && rs_ctx->SUB != NULL && \ |
| 477 | ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \ |
| 478 | { \ |
| 479 | ecp_restart_## SUB ##_free(rs_ctx->SUB); \ |
| 480 | mbedtls_free(rs_ctx->SUB); \ |
| 481 | rs_ctx->SUB = NULL; \ |
| 482 | } \ |
Manuel Pégourié-Gonnard | a58e011 | 2018-10-16 10:42:47 +0200 | [diff] [blame] | 483 | \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 484 | if (rs_ctx != NULL) \ |
Manuel Pégourié-Gonnard | a58e011 | 2018-10-16 10:42:47 +0200 | [diff] [blame] | 485 | rs_ctx->depth--; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 486 | } while (0) |
Manuel Pégourié-Gonnard | db4a8eb | 2017-08-23 18:18:22 +0200 | [diff] [blame] | 487 | |
| 488 | #else /* MBEDTLS_ECP_RESTARTABLE */ |
| 489 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 490 | #define ECP_RS_ENTER(sub) (void) rs_ctx; |
| 491 | #define ECP_RS_LEAVE(sub) (void) rs_ctx; |
Manuel Pégourié-Gonnard | db4a8eb | 2017-08-23 18:18:22 +0200 | [diff] [blame] | 492 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 493 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 054433c | 2017-03-22 11:18:33 +0100 | [diff] [blame] | 494 | |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 495 | /* |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 496 | * List of supported curves: |
| 497 | * - internal ID |
Christoph M. Wintersteiger | cb31073 | 2019-02-15 15:50:38 +0000 | [diff] [blame] | 498 | * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7) |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 499 | * - size in bits |
Manuel Pégourié-Gonnard | 8195c1a | 2013-10-07 19:40:41 +0200 | [diff] [blame] | 500 | * - readable name |
Gergely Budai | e40c469 | 2014-01-22 11:22:20 +0100 | [diff] [blame] | 501 | * |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 502 | * Curves are listed in order: largest curves first, and for a given size, |
| 503 | * fastest curves first. This provides the default order for the SSL module. |
Manuel Pégourié-Gonnard | 88db5da | 2015-06-15 14:34:59 +0200 | [diff] [blame] | 504 | * |
Gilles Peskine | 1174db5 | 2020-02-26 19:52:06 +0100 | [diff] [blame] | 505 | * Reminder: update profiles in x509_crt.c when adding a new curves! |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 506 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | static const mbedtls_ecp_curve_info ecp_supported_curves[] = |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 508 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 510 | { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 511 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
| 513 | { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" }, |
Gergely Budai | e40c469 | 2014-01-22 11:22:20 +0100 | [diff] [blame] | 514 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 516 | { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 517 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
| 519 | { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" }, |
Gergely Budai | e40c469 | 2014-01-22 11:22:20 +0100 | [diff] [blame] | 520 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 521 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 522 | { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 523 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
| 525 | { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" }, |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 526 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
| 528 | { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" }, |
Gergely Budai | e40c469 | 2014-01-22 11:22:20 +0100 | [diff] [blame] | 529 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
| 531 | { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 532 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 533 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
| 534 | { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" }, |
Manuel Pégourié-Gonnard | 9bcff39 | 2014-01-10 18:26:48 +0100 | [diff] [blame] | 535 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
| 537 | { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" }, |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 538 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
| 540 | { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" }, |
Manuel Pégourié-Gonnard | 9bcff39 | 2014-01-10 18:26:48 +0100 | [diff] [blame] | 541 | #endif |
Gilles Peskine | 360e2c4 | 2020-07-24 02:03:20 +0200 | [diff] [blame] | 542 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Christoph M. Wintersteiger | 86e36c4 | 2018-12-06 17:27:31 +0000 | [diff] [blame] | 543 | { MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" }, |
Christoph M. Wintersteiger | c9f737b | 2018-10-25 13:03:05 +0100 | [diff] [blame] | 544 | #endif |
Gilles Peskine | 360e2c4 | 2020-07-24 02:03:20 +0200 | [diff] [blame] | 545 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 546 | { MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" }, |
| 547 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 548 | { MBEDTLS_ECP_DP_NONE, 0, 0, NULL }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 549 | }; |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 551 | #define ECP_NB_CURVES sizeof(ecp_supported_curves) / \ |
| 552 | sizeof(ecp_supported_curves[0]) |
Manuel Pégourié-Gonnard | ba782bb | 2014-07-08 13:31:34 +0200 | [diff] [blame] | 553 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES]; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 555 | |
| 556 | /* |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 557 | * List of supported curves and associated info |
| 558 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 559 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void) |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 560 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 561 | return ecp_supported_curves; |
Manuel Pégourié-Gonnard | da179e4 | 2013-09-18 15:31:24 +0200 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 565 | * List of supported curves, group ID only |
| 566 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 567 | const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void) |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 568 | { |
| 569 | static int init_done = 0; |
| 570 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 571 | if (!init_done) { |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 572 | size_t i = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | const mbedtls_ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 574 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 575 | for (curve_info = mbedtls_ecp_curve_list(); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 576 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 577 | curve_info++) { |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 578 | ecp_supported_grp_id[i++] = curve_info->grp_id; |
| 579 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE; |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 581 | |
| 582 | init_done = 1; |
| 583 | } |
| 584 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 585 | return ecp_supported_grp_id; |
Manuel Pégourié-Gonnard | ac71941 | 2014-02-04 14:48:50 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Get the curve info for the internal identifier |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 590 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 591 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id) |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 592 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | const mbedtls_ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 594 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 595 | for (curve_info = mbedtls_ecp_curve_list(); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 597 | curve_info++) { |
| 598 | if (curve_info->grp_id == grp_id) { |
| 599 | return curve_info; |
| 600 | } |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 601 | } |
| 602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 603 | return NULL; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* |
| 607 | * Get the curve info from the TLS identifier |
| 608 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 609 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id) |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 610 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | const mbedtls_ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 613 | for (curve_info = mbedtls_ecp_curve_list(); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 615 | curve_info++) { |
| 616 | if (curve_info->tls_id == tls_id) { |
| 617 | return curve_info; |
| 618 | } |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 621 | return NULL; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 625 | * Get the curve info from the name |
| 626 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 627 | const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name) |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | const mbedtls_ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 630 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 631 | if (name == NULL) { |
| 632 | return NULL; |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 635 | for (curve_info = mbedtls_ecp_curve_list(); |
| 636 | curve_info->grp_id != MBEDTLS_ECP_DP_NONE; |
| 637 | curve_info++) { |
| 638 | if (strcmp(curve_info->name, name) == 0) { |
| 639 | return curve_info; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | return NULL; |
Manuel Pégourié-Gonnard | 0267e3d | 2013-11-30 15:10:14 +0100 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 647 | * Get the type of a curve |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 648 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 649 | mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp) |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 650 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 651 | if (grp->G.X.p == NULL) { |
| 652 | return MBEDTLS_ECP_TYPE_NONE; |
| 653 | } |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 654 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 655 | if (grp->G.Y.p == NULL) { |
| 656 | return MBEDTLS_ECP_TYPE_MONTGOMERY; |
| 657 | } else { |
| 658 | return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS; |
| 659 | } |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 663 | * Initialize (the components of) a point |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 664 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 665 | void mbedtls_ecp_point_init(mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 666 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 667 | ECP_VALIDATE(pt != NULL); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 668 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 669 | mbedtls_mpi_init(&pt->X); |
| 670 | mbedtls_mpi_init(&pt->Y); |
| 671 | mbedtls_mpi_init(&pt->Z); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Initialize (the components of) a group |
| 676 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 677 | void mbedtls_ecp_group_init(mbedtls_ecp_group *grp) |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 678 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 679 | ECP_VALIDATE(grp != NULL); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 680 | |
Manuel Pégourié-Gonnard | 95e2eca | 2018-06-20 10:29:47 +0200 | [diff] [blame] | 681 | grp->id = MBEDTLS_ECP_DP_NONE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 682 | mbedtls_mpi_init(&grp->P); |
| 683 | mbedtls_mpi_init(&grp->A); |
| 684 | mbedtls_mpi_init(&grp->B); |
| 685 | mbedtls_ecp_point_init(&grp->G); |
| 686 | mbedtls_mpi_init(&grp->N); |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 687 | grp->pbits = 0; |
| 688 | grp->nbits = 0; |
| 689 | grp->h = 0; |
| 690 | grp->modp = NULL; |
| 691 | grp->t_pre = NULL; |
| 692 | grp->t_post = NULL; |
| 693 | grp->t_data = NULL; |
| 694 | grp->T = NULL; |
| 695 | grp->T_size = 0; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 699 | * Initialize (the components of) a key pair |
| 700 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 701 | void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key) |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 702 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 703 | ECP_VALIDATE(key != NULL); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 704 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 705 | mbedtls_ecp_group_init(&key->grp); |
| 706 | mbedtls_mpi_init(&key->d); |
| 707 | mbedtls_ecp_point_init(&key->Q); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | /* |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 711 | * Unallocate (the components of) a point |
| 712 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 713 | void mbedtls_ecp_point_free(mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 714 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 715 | if (pt == NULL) { |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 716 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 717 | } |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 719 | mbedtls_mpi_free(&(pt->X)); |
| 720 | mbedtls_mpi_free(&(pt->Y)); |
| 721 | mbedtls_mpi_free(&(pt->Z)); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Unallocate (the components of) a group |
| 726 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 727 | void mbedtls_ecp_group_free(mbedtls_ecp_group *grp) |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 728 | { |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 729 | size_t i; |
| 730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 731 | if (grp == NULL) { |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 732 | return; |
Manuel Pégourié-Gonnard | 1f82b04 | 2013-12-06 12:51:50 +0100 | [diff] [blame] | 733 | } |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 734 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | if (grp->h != 1) { |
| 736 | mbedtls_mpi_free(&grp->P); |
| 737 | mbedtls_mpi_free(&grp->A); |
| 738 | mbedtls_mpi_free(&grp->B); |
| 739 | mbedtls_ecp_point_free(&grp->G); |
| 740 | mbedtls_mpi_free(&grp->N); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 741 | } |
| 742 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 743 | if (grp->T != NULL) { |
| 744 | for (i = 0; i < grp->T_size; i++) { |
| 745 | mbedtls_ecp_point_free(&grp->T[i]); |
| 746 | } |
| 747 | mbedtls_free(grp->T); |
| 748 | } |
| 749 | |
| 750 | mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group)); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 751 | } |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 752 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 753 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 754 | * Unallocate (the components of) a key pair |
| 755 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 756 | void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key) |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 757 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 758 | if (key == NULL) { |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 759 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 760 | } |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 761 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 762 | mbedtls_ecp_group_free(&key->grp); |
| 763 | mbedtls_mpi_free(&key->d); |
| 764 | mbedtls_ecp_point_free(&key->Q); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 768 | * Copy the contents of a point |
| 769 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 770 | int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q) |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 771 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 772 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 773 | ECP_VALIDATE_RET(P != NULL); |
| 774 | ECP_VALIDATE_RET(Q != NULL); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 775 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 776 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X)); |
| 777 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y)); |
| 778 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z)); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 779 | |
| 780 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 781 | return ret; |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Copy the contents of a group object |
| 786 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 787 | int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src) |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 788 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 789 | ECP_VALIDATE_RET(dst != NULL); |
| 790 | ECP_VALIDATE_RET(src != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 791 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 792 | return mbedtls_ecp_group_load(dst, src->id); |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 796 | * Set point to zero |
| 797 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 798 | int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 799 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 800 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 801 | ECP_VALIDATE_RET(pt != NULL); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 802 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 803 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1)); |
| 804 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1)); |
| 805 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0)); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 806 | |
| 807 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 808 | return ret; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /* |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 812 | * Tell if a point is zero |
| 813 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 814 | int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 815 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 816 | ECP_VALIDATE_RET(pt != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 817 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 818 | return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0; |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | /* |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 822 | * Compare two points lazily |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 823 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 824 | int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P, |
| 825 | const mbedtls_ecp_point *Q) |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 826 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 827 | ECP_VALIDATE_RET(P != NULL); |
| 828 | ECP_VALIDATE_RET(Q != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 829 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 830 | if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 && |
| 831 | mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 && |
| 832 | mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) { |
| 833 | return 0; |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 836 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 6029a85 | 2015-08-11 15:44:41 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /* |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 840 | * Import a non-zero point from ASCII strings |
| 841 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 842 | int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix, |
| 843 | const char *x, const char *y) |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 844 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 845 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 846 | ECP_VALIDATE_RET(P != NULL); |
| 847 | ECP_VALIDATE_RET(x != NULL); |
| 848 | ECP_VALIDATE_RET(y != NULL); |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 849 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 850 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x)); |
| 851 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y)); |
| 852 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1)); |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 853 | |
| 854 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 855 | return ret; |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | /* |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 859 | * Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 860 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 861 | int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp, |
| 862 | const mbedtls_ecp_point *P, |
| 863 | int format, size_t *olen, |
| 864 | unsigned char *buf, size_t buflen) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 865 | { |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 866 | int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 867 | size_t plen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 868 | ECP_VALIDATE_RET(grp != NULL); |
| 869 | ECP_VALIDATE_RET(P != NULL); |
| 870 | ECP_VALIDATE_RET(olen != NULL); |
| 871 | ECP_VALIDATE_RET(buf != NULL); |
| 872 | ECP_VALIDATE_RET(format == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 873 | format == MBEDTLS_ECP_PF_COMPRESSED); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 875 | plen = mbedtls_mpi_size(&grp->P); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 876 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 877 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 5997005 | 2019-02-28 13:12:06 +0100 | [diff] [blame] | 878 | (void) format; /* Montgomery curves always use the same point format */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 879 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 880 | *olen = plen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 881 | if (buflen < *olen) { |
| 882 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 883 | } |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 885 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen)); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 886 | } |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 887 | #endif |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 888 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 889 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 890 | /* |
| 891 | * Common case: P == 0 |
| 892 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 893 | if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) { |
| 894 | if (buflen < 1) { |
| 895 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 896 | } |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 897 | |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 898 | buf[0] = 0x00; |
| 899 | *olen = 1; |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 900 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 901 | return 0; |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 904 | if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) { |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 905 | *olen = 2 * plen + 1; |
| 906 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 907 | if (buflen < *olen) { |
| 908 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 909 | } |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 910 | |
| 911 | buf[0] = 0x04; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 912 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen)); |
| 913 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen)); |
| 914 | } else if (format == MBEDTLS_ECP_PF_COMPRESSED) { |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 915 | *olen = plen + 1; |
| 916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 917 | if (buflen < *olen) { |
| 918 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 919 | } |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 920 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 921 | buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0); |
| 922 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen)); |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 923 | } |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 924 | } |
Janos Follath | 7caf8e4 | 2019-02-20 12:00:22 +0000 | [diff] [blame] | 925 | #endif |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 926 | |
| 927 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 928 | return ret; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 932 | * Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748) |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 933 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 934 | int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp, |
| 935 | mbedtls_ecp_point *pt, |
| 936 | const unsigned char *buf, size_t ilen) |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 937 | { |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 938 | int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 939 | size_t plen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 940 | ECP_VALIDATE_RET(grp != NULL); |
| 941 | ECP_VALIDATE_RET(pt != NULL); |
| 942 | ECP_VALIDATE_RET(buf != NULL); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 943 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 944 | if (ilen < 1) { |
| 945 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 946 | } |
Manuel Pégourié-Gonnard | 67dbe1e | 2014-07-08 13:09:24 +0200 | [diff] [blame] | 947 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 948 | plen = mbedtls_mpi_size(&grp->P); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 949 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 950 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 951 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 952 | if (plen != ilen) { |
| 953 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 954 | } |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 955 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 956 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen)); |
| 957 | mbedtls_mpi_free(&pt->Y); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 958 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 959 | if (grp->id == MBEDTLS_ECP_DP_CURVE25519) { |
Janos Follath | ffbd7e8 | 2019-02-25 11:35:20 +0000 | [diff] [blame] | 960 | /* Set most significant bit to 0 as prescribed in RFC7748 §5 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 961 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0)); |
| 962 | } |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 963 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 964 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1)); |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 965 | } |
| 966 | #endif |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 967 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 968 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
| 969 | if (buf[0] == 0x00) { |
| 970 | if (ilen == 1) { |
| 971 | return mbedtls_ecp_set_zero(pt); |
| 972 | } else { |
| 973 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 974 | } |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 977 | if (buf[0] != 0x04) { |
| 978 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 979 | } |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 980 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 981 | if (ilen != 2 * plen + 1) { |
| 982 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 983 | } |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 984 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 985 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen)); |
| 986 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->Y, |
| 987 | buf + 1 + plen, plen)); |
| 988 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1)); |
Janos Follath | 59b813c | 2019-02-13 10:44:06 +0000 | [diff] [blame] | 989 | } |
| 990 | #endif |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 991 | |
| 992 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 993 | return ret; |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 997 | * Import a point from a TLS ECPoint record (RFC 4492) |
| 998 | * struct { |
| 999 | * opaque point <1..2^8-1>; |
| 1000 | * } ECPoint; |
| 1001 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1002 | int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp, |
| 1003 | mbedtls_ecp_point *pt, |
| 1004 | const unsigned char **buf, size_t buf_len) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1005 | { |
| 1006 | unsigned char data_len; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 1007 | const unsigned char *buf_start; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1008 | ECP_VALIDATE_RET(grp != NULL); |
| 1009 | ECP_VALIDATE_RET(pt != NULL); |
| 1010 | ECP_VALIDATE_RET(buf != NULL); |
| 1011 | ECP_VALIDATE_RET(*buf != NULL); |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1012 | |
| 1013 | /* |
Manuel Pégourié-Gonnard | 67dbe1e | 2014-07-08 13:09:24 +0200 | [diff] [blame] | 1014 | * We must have at least two bytes (1 for length, at least one for data) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1015 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1016 | if (buf_len < 2) { |
| 1017 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1018 | } |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1019 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 1020 | data_len = *(*buf)++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1021 | if (data_len < 1 || data_len > buf_len - 1) { |
| 1022 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1023 | } |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1024 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 1025 | /* |
| 1026 | * Save buffer start for read_binary and update buf |
| 1027 | */ |
| 1028 | buf_start = *buf; |
| 1029 | *buf += data_len; |
| 1030 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1031 | return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len); |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * Export a point as a TLS ECPoint record (RFC 4492) |
| 1036 | * struct { |
| 1037 | * opaque point <1..2^8-1>; |
| 1038 | * } ECPoint; |
| 1039 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1040 | int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, |
| 1041 | int format, size_t *olen, |
| 1042 | unsigned char *buf, size_t blen) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1043 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1044 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1045 | ECP_VALIDATE_RET(grp != NULL); |
| 1046 | ECP_VALIDATE_RET(pt != NULL); |
| 1047 | ECP_VALIDATE_RET(olen != NULL); |
| 1048 | ECP_VALIDATE_RET(buf != NULL); |
| 1049 | ECP_VALIDATE_RET(format == MBEDTLS_ECP_PF_UNCOMPRESSED || |
| 1050 | format == MBEDTLS_ECP_PF_COMPRESSED); |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 1051 | |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1052 | /* |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 1053 | * buffer length must be at least one, for our length byte |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1054 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1055 | if (blen < 1) { |
| 1056 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1057 | } |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1058 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1059 | if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format, |
| 1060 | olen, buf + 1, blen - 1)) != 0) { |
| 1061 | return ret; |
| 1062 | } |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * write length to the first byte and update total length |
| 1066 | */ |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1067 | buf[0] = (unsigned char) *olen; |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 1068 | ++*olen; |
| 1069 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1070 | return 0; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /* |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 1074 | * Set a group from an ECParameters record (RFC 4492) |
| 1075 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1076 | int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp, |
| 1077 | const unsigned char **buf, size_t len) |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1078 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1079 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1080 | mbedtls_ecp_group_id grp_id; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1081 | ECP_VALIDATE_RET(grp != NULL); |
| 1082 | ECP_VALIDATE_RET(buf != NULL); |
| 1083 | ECP_VALIDATE_RET(*buf != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1084 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1085 | if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) { |
| 1086 | return ret; |
| 1087 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1089 | return mbedtls_ecp_group_load(grp, grp_id); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Read a group id from an ECParameters record (RFC 4492) and convert it to |
| 1094 | * mbedtls_ecp_group_id. |
| 1095 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1096 | int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp, |
| 1097 | const unsigned char **buf, size_t len) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 1098 | { |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 1099 | uint16_t tls_id; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1100 | const mbedtls_ecp_curve_info *curve_info; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1101 | ECP_VALIDATE_RET(grp != NULL); |
| 1102 | ECP_VALIDATE_RET(buf != NULL); |
| 1103 | ECP_VALIDATE_RET(*buf != NULL); |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 1104 | |
| 1105 | /* |
| 1106 | * We expect at least three bytes (see below) |
| 1107 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1108 | if (len < 3) { |
| 1109 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1110 | } |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 1111 | |
| 1112 | /* |
| 1113 | * First byte is curve_type; only named_curve is handled |
| 1114 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1115 | if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) { |
| 1116 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1117 | } |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 1118 | |
| 1119 | /* |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 1120 | * Next two bytes are the namedcurve value |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 1121 | */ |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 1122 | tls_id = *(*buf)++; |
| 1123 | tls_id <<= 8; |
| 1124 | tls_id |= *(*buf)++; |
| 1125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1126 | if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) { |
| 1127 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 1128 | } |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 1129 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1130 | *grp = curve_info->grp_id; |
| 1131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1132 | return 0; |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * Write the ECParameters record corresponding to a group (RFC 4492) |
| 1137 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1138 | int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen, |
| 1139 | unsigned char *buf, size_t blen) |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 1140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | const mbedtls_ecp_curve_info *curve_info; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1142 | ECP_VALIDATE_RET(grp != NULL); |
| 1143 | ECP_VALIDATE_RET(buf != NULL); |
| 1144 | ECP_VALIDATE_RET(olen != NULL); |
Manuel Pégourié-Gonnard | f24b4a7 | 2013-09-23 18:14:50 +0200 | [diff] [blame] | 1145 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1146 | if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) { |
| 1147 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1148 | } |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 1149 | |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 1150 | /* |
| 1151 | * We are going to write 3 bytes (see below) |
| 1152 | */ |
| 1153 | *olen = 3; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1154 | if (blen < *olen) { |
| 1155 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
| 1156 | } |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 1157 | |
| 1158 | /* |
| 1159 | * First byte is curve_type, always named_curve |
| 1160 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1161 | *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE; |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 1162 | |
| 1163 | /* |
| 1164 | * Next two bytes are the namedcurve value |
| 1165 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1166 | MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0); |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 1167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1168 | return 0; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 1169 | } |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 1170 | |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 1171 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi. |
| 1173 | * See the documentation of struct mbedtls_ecp_group. |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 1174 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf. |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 1176 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1177 | static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 1178 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1179 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 1180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1181 | if (grp->modp == NULL) { |
| 1182 | return mbedtls_mpi_mod_mpi(N, N, &grp->P); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 1183 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 1184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1185 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
| 1186 | if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) || |
| 1187 | mbedtls_mpi_bitlen(N) > 2 * grp->pbits) { |
| 1188 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1189 | } |
| 1190 | |
| 1191 | MBEDTLS_MPI_CHK(grp->modp(N)); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 1192 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 1193 | /* N->s < 0 is a much faster test, which fails only if N is 0 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1194 | while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) { |
| 1195 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P)); |
| 1196 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 1197 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1198 | while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) { |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 1199 | /* we known P, N and the result are positive */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1200 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P)); |
| 1201 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 1202 | |
Manuel Pégourié-Gonnard | cae6f3e | 2013-10-23 20:19:57 +0200 | [diff] [blame] | 1203 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1204 | return ret; |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 1205 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 1206 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 1207 | /* |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1208 | * Fast mod-p functions expect their argument to be in the 0..p^2 range. |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 1209 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1210 | * In order to guarantee that, we need to ensure that operands of |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 1212 | * bring the result back to this range. |
| 1213 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1214 | * The following macros are shortcuts for doing that. |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 1215 | */ |
| 1216 | |
| 1217 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1218 | * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1219 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1220 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 9181481 | 2013-11-21 20:23:55 +0100 | [diff] [blame] | 1221 | #define INC_MUL_COUNT mul_count++; |
| 1222 | #else |
| 1223 | #define INC_MUL_COUNT |
| 1224 | #endif |
| 1225 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1226 | #define MOD_MUL(N) \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 1227 | do \ |
| 1228 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1229 | MBEDTLS_MPI_CHK(ecp_modp(&(N), grp)); \ |
Hanno Becker | 1eeca41 | 2018-10-15 12:01:35 +0100 | [diff] [blame] | 1230 | INC_MUL_COUNT \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1231 | } while (0) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1233 | static inline int mbedtls_mpi_mul_mod(const mbedtls_ecp_group *grp, |
| 1234 | mbedtls_mpi *X, |
| 1235 | const mbedtls_mpi *A, |
| 1236 | const mbedtls_mpi *B) |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1237 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1238 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1239 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(X, A, B)); |
| 1240 | MOD_MUL(*X); |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1241 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1242 | return ret; |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1245 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi |
Manuel Pégourié-Gonnard | c9e387c | 2013-10-17 17:15:35 +0200 | [diff] [blame] | 1247 | * N->s < 0 is a very fast test, which fails only if N is 0 |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1248 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1249 | #define MOD_SUB(N) \ |
| 1250 | while ((N).s < 0 && mbedtls_mpi_cmp_int(&(N), 0) != 0) \ |
| 1251 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&(N), &(N), &grp->P)) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1253 | #if (defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \ |
| 1254 | !(defined(MBEDTLS_ECP_NO_FALLBACK) && \ |
| 1255 | defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \ |
| 1256 | defined(MBEDTLS_ECP_ADD_MIXED_ALT))) || \ |
| 1257 | (defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) && \ |
| 1258 | !(defined(MBEDTLS_ECP_NO_FALLBACK) && \ |
| 1259 | defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT))) |
| 1260 | static inline int mbedtls_mpi_sub_mod(const mbedtls_ecp_group *grp, |
| 1261 | mbedtls_mpi *X, |
| 1262 | const mbedtls_mpi *A, |
| 1263 | const mbedtls_mpi *B) |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1264 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1265 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1266 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, A, B)); |
| 1267 | MOD_SUB(*X); |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1268 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1269 | return ret; |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1270 | } |
Steven Cooreman | e538896 | 2021-03-01 14:04:53 +0100 | [diff] [blame] | 1271 | #endif /* All functions referencing mbedtls_mpi_sub_mod() are alt-implemented without fallback */ |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1272 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1273 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1274 | * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int. |
Manuel Pégourié-Gonnard | c9e387c | 2013-10-17 17:15:35 +0200 | [diff] [blame] | 1275 | * We known P, N and the result are positive, so sub_abs is correct, and |
| 1276 | * a bit faster. |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1277 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1278 | #define MOD_ADD(N) \ |
| 1279 | while (mbedtls_mpi_cmp_mpi(&(N), &grp->P) >= 0) \ |
| 1280 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&(N), &(N), &grp->P)) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1282 | static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp, |
| 1283 | mbedtls_mpi *X, |
| 1284 | const mbedtls_mpi *A, |
| 1285 | const mbedtls_mpi *B) |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1286 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1287 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1288 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B)); |
| 1289 | MOD_ADD(*X); |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1290 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1291 | return ret; |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1292 | } |
| 1293 | |
Steven Cooreman | e538896 | 2021-03-01 14:04:53 +0100 | [diff] [blame] | 1294 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1295 | !(defined(MBEDTLS_ECP_NO_FALLBACK) && \ |
| 1296 | defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) && \ |
| 1297 | defined(MBEDTLS_ECP_ADD_MIXED_ALT)) |
| 1298 | static inline int mbedtls_mpi_shift_l_mod(const mbedtls_ecp_group *grp, |
| 1299 | mbedtls_mpi *X, |
| 1300 | size_t count) |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1301 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1302 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1303 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, count)); |
| 1304 | MOD_ADD(*X); |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1305 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1306 | return ret; |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1307 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1308 | #endif \ |
| 1309 | /* All functions referencing mbedtls_mpi_shift_l_mod() are alt-implemented without fallback */ |
Gilles Peskine | 3b3b34f | 2019-07-18 21:08:27 +0200 | [diff] [blame] | 1310 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 1311 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1312 | /* |
| 1313 | * For curves in short Weierstrass form, we do all the internal operations in |
| 1314 | * Jacobian coordinates. |
| 1315 | * |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1316 | * For multiplication, we'll use a comb method with countermeasures against |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 1317 | * SPA, hence timing attacks. |
| 1318 | */ |
| 1319 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1320 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1321 | * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1) |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1322 | * Cost: 1N := 1I + 3M + 1S |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1323 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1324 | static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1325 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1326 | if (mbedtls_mpi_cmp_int(&pt->Z, 0) == 0) { |
| 1327 | return 0; |
| 1328 | } |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1329 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1330 | #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1331 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 1332 | return mbedtls_internal_ecp_normalize_jac(grp, pt); |
| 1333 | } |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 1334 | #endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */ |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1335 | |
| 1336 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1337 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1338 | #else |
| 1339 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1340 | mbedtls_mpi Zi, ZZi; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1341 | mbedtls_mpi_init(&Zi); mbedtls_mpi_init(&ZZi); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1342 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1343 | /* |
| 1344 | * X = X / Z^2 mod p |
| 1345 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1346 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&Zi, &pt->Z, &grp->P)); |
| 1347 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ZZi, &Zi, &Zi)); |
| 1348 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->X, &pt->X, &ZZi)); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1349 | |
| 1350 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1351 | * Y = Y / Z^3 mod p |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1352 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1353 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &ZZi)); |
| 1354 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &Zi)); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1355 | |
| 1356 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1357 | * Z = 1 |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1358 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1359 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1)); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1360 | |
| 1361 | cleanup: |
| 1362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1363 | mbedtls_mpi_free(&Zi); mbedtls_mpi_free(&ZZi); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1365 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1366 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */ |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /* |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1370 | * Normalize jacobian coordinates of an array of (pointers to) points, |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1371 | * using Montgomery's trick to perform only one inversion mod P. |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1372 | * (See for example Cohen's "A Course in Computational Algebraic Number |
| 1373 | * Theory", Algorithm 10.3.4.) |
| 1374 | * |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1375 | * Warning: fails (returning an error) if one of the points is zero! |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 1376 | * This should never happen, see choice of w in ecp_mul_comb(). |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1377 | * |
| 1378 | * Cost: 1N(t) := 1I + (6t - 3)M + 1S |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1379 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1380 | static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp, |
| 1381 | mbedtls_ecp_point *T[], size_t T_size) |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1382 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1383 | if (T_size < 2) { |
| 1384 | return ecp_normalize_jac(grp, *T); |
| 1385 | } |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1386 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1387 | #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1388 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 1389 | return mbedtls_internal_ecp_normalize_jac_many(grp, T, T_size); |
| 1390 | } |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1391 | #endif |
| 1392 | |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1393 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1394 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1395 | #else |
| 1396 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1397 | size_t i; |
| 1398 | mbedtls_mpi *c, u, Zi, ZZi; |
| 1399 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1400 | if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) { |
| 1401 | return MBEDTLS_ERR_ECP_ALLOC_FAILED; |
| 1402 | } |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1403 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1404 | for (i = 0; i < T_size; i++) { |
| 1405 | mbedtls_mpi_init(&c[i]); |
| 1406 | } |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 1407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1408 | mbedtls_mpi_init(&u); mbedtls_mpi_init(&Zi); mbedtls_mpi_init(&ZZi); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1409 | |
| 1410 | /* |
| 1411 | * c[i] = Z_0 * ... * Z_i |
| 1412 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1413 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&c[0], &T[0]->Z)); |
| 1414 | for (i = 1; i < T_size; i++) { |
| 1415 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &c[i], &c[i-1], &T[i]->Z)); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | * u = 1 / (Z_0 * ... * Z_n) mod P |
| 1420 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1421 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&u, &c[T_size-1], &grp->P)); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1423 | for (i = T_size - 1;; i--) { |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1424 | /* |
| 1425 | * Zi = 1 / Z_i mod p |
| 1426 | * u = 1 / (Z_0 * ... * Z_i) mod P |
| 1427 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1428 | if (i == 0) { |
| 1429 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&Zi, &u)); |
| 1430 | } else { |
| 1431 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &Zi, &u, &c[i-1])); |
| 1432 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &u, &u, &T[i]->Z)); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * proceed as in normalize() |
| 1437 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1438 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ZZi, &Zi, &Zi)); |
| 1439 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->X, &T[i]->X, &ZZi)); |
| 1440 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->Y, &T[i]->Y, &ZZi)); |
| 1441 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T[i]->Y, &T[i]->Y, &Zi)); |
Manuel Pégourié-Gonnard | 1f789b8 | 2013-12-30 17:31:56 +0100 | [diff] [blame] | 1442 | |
| 1443 | /* |
| 1444 | * Post-precessing: reclaim some memory by shrinking coordinates |
| 1445 | * - not storing Z (always 1) |
| 1446 | * - shrinking other coordinates, but still keeping the same number of |
| 1447 | * limbs as P, as otherwise it will too likely be regrown too fast. |
| 1448 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1449 | MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n)); |
| 1450 | MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n)); |
| 1451 | mbedtls_mpi_free(&T[i]->Z); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1452 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1453 | if (i == 0) { |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1454 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1455 | } |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | cleanup: |
| 1459 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1460 | mbedtls_mpi_free(&u); mbedtls_mpi_free(&Zi); mbedtls_mpi_free(&ZZi); |
| 1461 | for (i = 0; i < T_size; i++) { |
| 1462 | mbedtls_mpi_free(&c[i]); |
| 1463 | } |
| 1464 | mbedtls_free(c); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1466 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1467 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) */ |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1468 | } |
| 1469 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1470 | /* |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1471 | * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak. |
| 1472 | * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid |
| 1473 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1474 | static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp, |
| 1475 | mbedtls_ecp_point *Q, |
| 1476 | unsigned char inv) |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1477 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1478 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1479 | unsigned char nonzero; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | mbedtls_mpi mQY; |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1481 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1482 | mbedtls_mpi_init(&mQY); |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1483 | |
| 1484 | /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1485 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mQY, &grp->P, &Q->Y)); |
| 1486 | nonzero = mbedtls_mpi_cmp_int(&Q->Y, 0) != 0; |
| 1487 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&Q->Y, &mQY, inv & nonzero)); |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1488 | |
| 1489 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1490 | mbedtls_mpi_free(&mQY); |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1491 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1492 | return ret; |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /* |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 1496 | * Point doubling R = 2 P, Jacobian coordinates |
Manuel Pégourié-Gonnard | 0ace4b3 | 2013-10-10 12:44:27 +0200 | [diff] [blame] | 1497 | * |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1498 | * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 . |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1499 | * |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1500 | * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR |
| 1501 | * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring. |
| 1502 | * |
| 1503 | * Standard optimizations are applied when curve parameter A is one of { 0, -3 }. |
| 1504 | * |
| 1505 | * Cost: 1D := 3M + 4S (A == 0) |
| 1506 | * 4M + 4S (A == -3) |
| 1507 | * 3M + 6S + 1a otherwise |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1508 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1509 | static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 1510 | const mbedtls_ecp_point *P) |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1511 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1512 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 0cd6f98 | 2013-10-10 15:55:39 +0200 | [diff] [blame] | 1513 | dbl_count++; |
| 1514 | #endif |
| 1515 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1516 | #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1517 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 1518 | return mbedtls_internal_ecp_double_jac(grp, R, P); |
| 1519 | } |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 1520 | #endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1521 | |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1522 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1523 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1524 | #else |
| 1525 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1526 | mbedtls_mpi M, S, T, U; |
| 1527 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1528 | mbedtls_mpi_init(&M); mbedtls_mpi_init(&S); mbedtls_mpi_init(&T); mbedtls_mpi_init(&U); |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 1529 | |
| 1530 | /* Special case for A = -3 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1531 | if (grp->A.p == NULL) { |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1532 | /* M = 3(X + Z^2)(X - Z^2) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1533 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->Z, &P->Z)); |
| 1534 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &T, &P->X, &S)); |
| 1535 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &U, &P->X, &S)); |
| 1536 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &T, &U)); |
| 1537 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&M, &S, 3)); MOD_ADD(M); |
| 1538 | } else { |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1539 | /* M = 3.X^2 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1540 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->X, &P->X)); |
| 1541 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(&M, &S, 3)); MOD_ADD(M); |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1542 | |
| 1543 | /* Optimize away for "koblitz" curves with A = 0 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1544 | if (mbedtls_mpi_cmp_int(&grp->A, 0) != 0) { |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1545 | /* M += A.Z^4 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1546 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->Z, &P->Z)); |
| 1547 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &S, &S)); |
| 1548 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &T, &grp->A)); |
| 1549 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &M, &M, &S)); |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1550 | } |
Peter Vaskovic | a676acf | 2014-08-06 00:48:39 +0200 | [diff] [blame] | 1551 | } |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 1552 | |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1553 | /* S = 4.X.Y^2 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1554 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &P->Y, &P->Y)); |
| 1555 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &T, 1)); |
| 1556 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &P->X, &T)); |
| 1557 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &S, 1)); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1558 | |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1559 | /* U = 8.Y^4 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1560 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &U, &T, &T)); |
| 1561 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &U, 1)); |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1562 | |
| 1563 | /* T = M^2 - 2.S */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1564 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T, &M, &M)); |
| 1565 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T, &T, &S)); |
| 1566 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T, &T, &S)); |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1567 | |
| 1568 | /* S = M(S - T) - U */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1569 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S, &S, &T)); |
| 1570 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S, &S, &M)); |
| 1571 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S, &S, &U)); |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1572 | |
| 1573 | /* U = 2.Y.Z */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1574 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &U, &P->Y, &P->Z)); |
| 1575 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &U, 1)); |
Peter Dettman | ce661b2 | 2015-02-07 14:43:51 +0700 | [diff] [blame] | 1576 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1577 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->X, &T)); |
| 1578 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Y, &S)); |
| 1579 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Z, &U)); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1580 | |
| 1581 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1582 | mbedtls_mpi_free(&M); mbedtls_mpi_free(&S); mbedtls_mpi_free(&T); mbedtls_mpi_free(&U); |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1584 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1585 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) */ |
Manuel Pégourié-Gonnard | 1c4aa24 | 2013-10-09 16:09:46 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /* |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1589 | * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22) |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1590 | * |
| 1591 | * The coordinates of Q must be normalized (= affine), |
| 1592 | * but those of P don't need to. R is not normalized. |
| 1593 | * |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1594 | * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q. |
Manuel Pégourié-Gonnard | 7a949d3 | 2013-12-05 10:26:01 +0100 | [diff] [blame] | 1595 | * None of these cases can happen as intermediate step in ecp_mul_comb(): |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1596 | * - at each step, P, Q and R are multiples of the base point, the factor |
| 1597 | * being less than its order, so none of them is zero; |
| 1598 | * - Q is an odd multiple of the base point, P an even multiple, |
| 1599 | * due to the choice of precomputed points in the modified comb method. |
| 1600 | * So branches for these cases do not leak secret information. |
| 1601 | * |
Manuel Pégourié-Gonnard | 72c172a | 2013-12-30 16:04:55 +0100 | [diff] [blame] | 1602 | * We accept Q->Z being unset (saving memory in tables) as meaning 1. |
| 1603 | * |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1604 | * Cost: 1A := 8M + 3S |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1605 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1606 | static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 1607 | const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1608 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1609 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1610 | add_count++; |
| 1611 | #endif |
| 1612 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1613 | #if defined(MBEDTLS_ECP_ADD_MIXED_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1614 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 1615 | return mbedtls_internal_ecp_add_mixed(grp, R, P, Q); |
| 1616 | } |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 1617 | #endif /* MBEDTLS_ECP_ADD_MIXED_ALT */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1618 | |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1619 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_ADD_MIXED_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1620 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1621 | #else |
| 1622 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1623 | mbedtls_mpi T1, T2, T3, T4, X, Y, Z; |
| 1624 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1625 | /* |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1626 | * Trivial cases: P == 0 or Q == 0 (case 1) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1627 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1628 | if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) { |
| 1629 | return mbedtls_ecp_copy(R, Q); |
| 1630 | } |
Manuel Pégourié-Gonnard | 469a209 | 2013-11-21 18:20:43 +0100 | [diff] [blame] | 1631 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1632 | if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 0) == 0) { |
| 1633 | return mbedtls_ecp_copy(R, P); |
| 1634 | } |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1635 | |
| 1636 | /* |
| 1637 | * Make sure Q coordinates are normalized |
| 1638 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1639 | if (Q->Z.p != NULL && mbedtls_mpi_cmp_int(&Q->Z, 1) != 0) { |
| 1640 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 1641 | } |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1643 | mbedtls_mpi_init(&T1); mbedtls_mpi_init(&T2); mbedtls_mpi_init(&T3); mbedtls_mpi_init(&T4); |
| 1644 | mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y); mbedtls_mpi_init(&Z); |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 1645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1646 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T1, &P->Z, &P->Z)); |
| 1647 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T2, &T1, &P->Z)); |
| 1648 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T1, &T1, &Q->X)); |
| 1649 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T2, &T2, &Q->Y)); |
| 1650 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T1, &T1, &P->X)); |
| 1651 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T2, &T2, &P->Y)); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1652 | |
Manuel Pégourié-Gonnard | aade42f | 2013-11-21 19:19:54 +0100 | [diff] [blame] | 1653 | /* Special cases (2) and (3) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1654 | if (mbedtls_mpi_cmp_int(&T1, 0) == 0) { |
| 1655 | if (mbedtls_mpi_cmp_int(&T2, 0) == 0) { |
| 1656 | ret = ecp_double_jac(grp, R, P); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1657 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1658 | } else { |
| 1659 | ret = mbedtls_ecp_set_zero(R); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1660 | goto cleanup; |
| 1661 | } |
| 1662 | } |
| 1663 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1664 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &Z, &P->Z, &T1)); |
| 1665 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T1, &T1)); |
| 1666 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T4, &T3, &T1)); |
| 1667 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T3, &P->X)); |
| 1668 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&T1, &T3)); |
| 1669 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, &T1, 1)); |
| 1670 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &X, &T2, &T2)); |
| 1671 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &X, &X, &T1)); |
| 1672 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &X, &X, &T4)); |
| 1673 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &T3, &T3, &X)); |
| 1674 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T3, &T3, &T2)); |
| 1675 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &T4, &T4, &P->Y)); |
| 1676 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &Y, &T3, &T4)); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1678 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->X, &X)); |
| 1679 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Y, &Y)); |
| 1680 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&R->Z, &Z)); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1681 | |
| 1682 | cleanup: |
| 1683 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1684 | mbedtls_mpi_free(&T1); mbedtls_mpi_free(&T2); mbedtls_mpi_free(&T3); mbedtls_mpi_free(&T4); |
| 1685 | mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y); mbedtls_mpi_free(&Z); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1686 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1687 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1688 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */ |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | /* |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1692 | * Randomize jacobian coordinates: |
| 1693 | * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l |
Manuel Pégourié-Gonnard | 3c0b4ea | 2013-12-02 19:44:41 +0100 | [diff] [blame] | 1694 | * This is sort of the reverse operation of ecp_normalize_jac(). |
Manuel Pégourié-Gonnard | 44aab79 | 2013-11-21 10:53:59 +0100 | [diff] [blame] | 1695 | * |
| 1696 | * This countermeasure was first suggested in [2]. |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1697 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1698 | static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, |
| 1699 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1700 | { |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1701 | #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1702 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 1703 | return mbedtls_internal_ecp_randomize_jac(grp, pt, f_rng, p_rng); |
| 1704 | } |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 1705 | #endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1706 | |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1707 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1708 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1709 | #else |
| 1710 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 1711 | mbedtls_mpi l, ll; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1712 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1713 | mbedtls_mpi_init(&l); mbedtls_mpi_init(&ll); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1714 | |
| 1715 | /* Generate l such that 1 < l < p */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1716 | MBEDTLS_MPI_CHK(mbedtls_mpi_random(&l, 2, &grp->P, f_rng, p_rng)); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1717 | |
| 1718 | /* Z = l * Z */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1719 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Z, &pt->Z, &l)); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1720 | |
| 1721 | /* X = l^2 * X */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1722 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ll, &l, &l)); |
| 1723 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->X, &pt->X, &ll)); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1724 | |
| 1725 | /* Y = l^3 * Y */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1726 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &ll, &ll, &l)); |
| 1727 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &pt->Y, &pt->Y, &ll)); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1728 | |
| 1729 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1730 | mbedtls_mpi_free(&l); mbedtls_mpi_free(&ll); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1731 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1732 | if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { |
Gilles Peskine | 6466d34 | 2021-03-29 22:28:50 +0200 | [diff] [blame] | 1733 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1734 | } |
| 1735 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 1736 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) */ |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /* |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1740 | * Check and define parameters used by the comb method (see below for details) |
| 1741 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1742 | #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7 |
| 1743 | #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds" |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1744 | #endif |
| 1745 | |
| 1746 | /* d = ceil( n / w ) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1747 | #define COMB_MAX_D (MBEDTLS_ECP_MAX_BITS + 1) / 2 |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1748 | |
| 1749 | /* number of precomputed points */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1750 | #define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1)) |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1751 | |
| 1752 | /* |
| 1753 | * Compute the representation of m that will be used with our comb method. |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1754 | * |
| 1755 | * The basic comb method is described in GECC 3.44 for example. We use a |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1756 | * modified version that provides resistance to SPA by avoiding zero |
| 1757 | * digits in the representation as in [3]. We modify the method further by |
| 1758 | * requiring that all K_i be odd, which has the small cost that our |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1759 | * representation uses one more K_i, due to carries, but saves on the size of |
| 1760 | * the precomputed table. |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1761 | * |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1762 | * Summary of the comb method and its modifications: |
| 1763 | * |
| 1764 | * - The goal is to compute m*P for some w*d-bit integer m. |
| 1765 | * |
| 1766 | * - The basic comb method splits m into the w-bit integers |
| 1767 | * x[0] .. x[d-1] where x[i] consists of the bits in m whose |
| 1768 | * index has residue i modulo d, and computes m * P as |
| 1769 | * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where |
| 1770 | * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P. |
| 1771 | * |
| 1772 | * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by |
| 1773 | * .. + 2^{i-1} S[x[i-1]] - 2^i S[x[i]] + 2^{i+1} S[x[i]] + 2^{i+2} S[x[i+2]] .., |
| 1774 | * thereby successively converting it into a form where all summands |
| 1775 | * are nonzero, at the cost of negative summands. This is the basic idea of [3]. |
| 1776 | * |
| 1777 | * - More generally, even if x[i+1] != 0, we can first transform the sum as |
| 1778 | * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] .., |
| 1779 | * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]]. |
| 1780 | * Performing and iterating this procedure for those x[i] that are even |
| 1781 | * (keeping track of carry), we can transform the original sum into one of the form |
| 1782 | * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]] |
| 1783 | * with all x'[i] odd. It is therefore only necessary to know S at odd indices, |
| 1784 | * which is why we are only computing half of it in the first place in |
| 1785 | * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb. |
| 1786 | * |
| 1787 | * - For the sake of compactness, only the seven low-order bits of x[i] |
| 1788 | * are used to represent its absolute value (K_i in the paper), and the msb |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1789 | * of x[i] encodes the sign (s_i in the paper): it is set if and only if |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1790 | * if s_i == -1; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1791 | * |
| 1792 | * Calling conventions: |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1793 | * - x is an array of size d + 1 |
Manuel Pégourié-Gonnard | c30200e | 2013-11-20 18:39:55 +0100 | [diff] [blame] | 1794 | * - w is the size, ie number of teeth, of the comb, and must be between |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE) |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1796 | * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d |
| 1797 | * (the result will be incorrect if these assumptions are not satisfied) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1798 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1799 | static void ecp_comb_recode_core(unsigned char x[], size_t d, |
| 1800 | unsigned char w, const mbedtls_mpi *m) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1801 | { |
| 1802 | size_t i, j; |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1803 | unsigned char c, cc, adjust; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1805 | memset(x, 0, d+1); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1806 | |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1807 | /* First get the classical comb values (except for x_d = 0) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1808 | for (i = 0; i < d; i++) { |
| 1809 | for (j = 0; j < w; j++) { |
| 1810 | x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j; |
| 1811 | } |
| 1812 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1813 | |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1814 | /* Now make sure x_1 .. x_d are odd */ |
| 1815 | c = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1816 | for (i = 1; i <= d; i++) { |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1817 | /* Add carry and update it */ |
| 1818 | cc = x[i] & c; |
| 1819 | x[i] = x[i] ^ c; |
| 1820 | c = cc; |
| 1821 | |
Manuel Pégourié-Gonnard | edc1a1f | 2013-11-21 09:50:00 +0100 | [diff] [blame] | 1822 | /* Adjust if needed, avoiding branches */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1823 | adjust = 1 - (x[i] & 0x01); |
| 1824 | c |= x[i] & (x[i-1] * adjust); |
| 1825 | x[i] = x[i] ^ (x[i-1] * adjust); |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1826 | x[i-1] |= adjust << 7; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | /* |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1831 | * Precompute points for the adapted comb method |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1832 | * |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1833 | * Assumption: T must be able to hold 2^{w - 1} elements. |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1834 | * |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1835 | * Operation: If i = i_{w-1} ... i_1 is the binary representation of i, |
| 1836 | * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P. |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 1837 | * |
| 1838 | * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1) |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1839 | * |
| 1840 | * Note: Even comb values (those where P would be omitted from the |
| 1841 | * sum defining T[i] above) are not needed in our adaption |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1842 | * the comb method. See ecp_comb_recode_core(). |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1843 | * |
| 1844 | * This function currently works in four steps: |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1845 | * (1) [dbl] Computation of intermediate T[i] for 2-power values of i |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1846 | * (2) [norm_dbl] Normalization of coordinates of these T[i] |
| 1847 | * (3) [add] Computation of all T[i] |
| 1848 | * (4) [norm_add] Normalization of all T[i] |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 1849 | * |
| 1850 | * Step 1 can be interrupted but not the others; together with the final |
| 1851 | * coordinate normalization they are the largest steps done at once, depending |
| 1852 | * on the window size. Here are operation counts for P-256: |
| 1853 | * |
| 1854 | * step (2) (3) (4) |
| 1855 | * w = 5 142 165 208 |
| 1856 | * w = 4 136 77 160 |
| 1857 | * w = 3 130 33 136 |
| 1858 | * w = 2 124 11 124 |
| 1859 | * |
| 1860 | * So if ECC operations are blocking for too long even with a low max_ops |
| 1861 | * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order |
| 1862 | * to minimize maximum blocking time. |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1863 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1864 | static int ecp_precompute_comb(const mbedtls_ecp_group *grp, |
| 1865 | mbedtls_ecp_point T[], const mbedtls_ecp_point *P, |
| 1866 | unsigned char w, size_t d, |
| 1867 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1868 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1869 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | fc3e0be | 2017-03-20 09:29:31 +0100 | [diff] [blame] | 1870 | unsigned char i; |
Manuel Pégourié-Gonnard | 213541a | 2017-03-20 12:50:41 +0100 | [diff] [blame] | 1871 | size_t j = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1872 | const unsigned char T_size = 1U << (w - 1); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1873 | mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1]; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1874 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 1875 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1876 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
| 1877 | if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1878 | goto dbl; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1879 | } |
| 1880 | if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) { |
Manuel Pégourié-Gonnard | e2d7cb3 | 2017-03-20 10:24:17 +0100 | [diff] [blame] | 1881 | goto norm_dbl; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1882 | } |
| 1883 | if (rs_ctx->rsm->state == ecp_rsm_pre_add) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1884 | goto add; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1885 | } |
| 1886 | if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1887 | goto norm_add; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1888 | } |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 1889 | } |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1890 | #else |
| 1891 | (void) rs_ctx; |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 1892 | #endif |
| 1893 | |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1894 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1895 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1896 | rs_ctx->rsm->state = ecp_rsm_pre_dbl; |
| 1897 | |
| 1898 | /* initial state for the loop */ |
| 1899 | rs_ctx->rsm->i = 0; |
| 1900 | } |
| 1901 | |
| 1902 | dbl: |
| 1903 | #endif |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1904 | /* |
| 1905 | * Set T[0] = P and |
| 1906 | * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value) |
| 1907 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1908 | MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&T[0], P)); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1909 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 1910 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1911 | if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 1912 | j = rs_ctx->rsm->i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1913 | } else |
Manuel Pégourié-Gonnard | 213541a | 2017-03-20 12:50:41 +0100 | [diff] [blame] | 1914 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1915 | j = 0; |
Manuel Pégourié-Gonnard | 213541a | 2017-03-20 12:50:41 +0100 | [diff] [blame] | 1916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1917 | for (; j < d * (w - 1); j++) { |
| 1918 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL); |
Manuel Pégourié-Gonnard | 213541a | 2017-03-20 12:50:41 +0100 | [diff] [blame] | 1919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1920 | i = 1U << (j / d); |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1921 | cur = T + i; |
Manuel Pégourié-Gonnard | ae55707 | 2017-03-20 12:21:24 +0100 | [diff] [blame] | 1922 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1923 | if (j % d == 0) { |
| 1924 | MBEDTLS_MPI_CHK(mbedtls_ecp_copy(cur, T + (i >> 1))); |
| 1925 | } |
Manuel Pégourié-Gonnard | ae55707 | 2017-03-20 12:21:24 +0100 | [diff] [blame] | 1926 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1927 | MBEDTLS_MPI_CHK(ecp_double_jac(grp, cur, cur)); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 1930 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1931 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1932 | rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1933 | } |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1934 | |
Manuel Pégourié-Gonnard | e2d7cb3 | 2017-03-20 10:24:17 +0100 | [diff] [blame] | 1935 | norm_dbl: |
| 1936 | #endif |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1937 | /* |
| 1938 | * Normalize current elements in T. As T has holes, |
| 1939 | * use an auxiliary array of pointers to elements in T. |
| 1940 | */ |
Manuel Pégourié-Gonnard | fc3e0be | 2017-03-20 09:29:31 +0100 | [diff] [blame] | 1941 | j = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1942 | for (i = 1; i < T_size; i <<= 1) { |
Manuel Pégourié-Gonnard | fc3e0be | 2017-03-20 09:29:31 +0100 | [diff] [blame] | 1943 | TT[j++] = T + i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1944 | } |
Manuel Pégourié-Gonnard | fc3e0be | 2017-03-20 09:29:31 +0100 | [diff] [blame] | 1945 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1946 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2); |
Manuel Pégourié-Gonnard | e2d7cb3 | 2017-03-20 10:24:17 +0100 | [diff] [blame] | 1947 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1948 | MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j)); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1949 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 1950 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1951 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1952 | rs_ctx->rsm->state = ecp_rsm_pre_add; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1953 | } |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1954 | |
Manuel Pégourié-Gonnard | e2d7cb3 | 2017-03-20 10:24:17 +0100 | [diff] [blame] | 1955 | add: |
| 1956 | #endif |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1957 | /* |
| 1958 | * Compute the remaining ones using the minimal number of additions |
| 1959 | * Be careful to update T[2^l] only after using it! |
| 1960 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1961 | MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD); |
Manuel Pégourié-Gonnard | e2d7cb3 | 2017-03-20 10:24:17 +0100 | [diff] [blame] | 1962 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1963 | for (i = 1; i < T_size; i <<= 1) { |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 1964 | j = i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1965 | while (j--) { |
| 1966 | MBEDTLS_MPI_CHK(ecp_add_mixed(grp, &T[i + j], &T[j], &T[i])); |
| 1967 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 1970 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1971 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1972 | rs_ctx->rsm->state = ecp_rsm_pre_norm_add; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1973 | } |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 1974 | |
Manuel Pégourié-Gonnard | e2d7cb3 | 2017-03-20 10:24:17 +0100 | [diff] [blame] | 1975 | norm_add: |
| 1976 | #endif |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1977 | /* |
Manuel Pégourié-Gonnard | a966fde | 2018-10-23 10:41:11 +0200 | [diff] [blame] | 1978 | * Normalize final elements in T. Even though there are no holes now, we |
| 1979 | * still need the auxiliary array for homogeneity with the previous |
| 1980 | * call. Also, skip T[0] which is already normalised, being a copy of P. |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 1981 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1982 | for (j = 0; j + 1 < T_size; j++) { |
Manuel Pégourié-Gonnard | fc3e0be | 2017-03-20 09:29:31 +0100 | [diff] [blame] | 1983 | TT[j] = T + j + 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1984 | } |
Manuel Pégourié-Gonnard | fc3e0be | 2017-03-20 09:29:31 +0100 | [diff] [blame] | 1985 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1986 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2); |
Manuel Pégourié-Gonnard | e2d7cb3 | 2017-03-20 10:24:17 +0100 | [diff] [blame] | 1987 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1988 | MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j)); |
Manuel Pégourié-Gonnard | e282012 | 2013-11-21 10:08:50 +0100 | [diff] [blame] | 1989 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 1990 | cleanup: |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 1991 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1992 | if (rs_ctx != NULL && rs_ctx->rsm != NULL && |
| 1993 | ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
| 1994 | if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) { |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 1995 | rs_ctx->rsm->i = j; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1996 | } |
Manuel Pégourié-Gonnard | 213541a | 2017-03-20 12:50:41 +0100 | [diff] [blame] | 1997 | } |
| 1998 | #endif |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 1999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2000 | return ret; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | /* |
Manuel Pégourié-Gonnard | 101a39f | 2013-11-20 14:47:19 +0100 | [diff] [blame] | 2004 | * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ] |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 2005 | * |
| 2006 | * See ecp_comb_recode_core() for background |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2007 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2008 | static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2009 | const mbedtls_ecp_point T[], unsigned char T_size, |
| 2010 | unsigned char i) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2011 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2012 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 2013 | unsigned char ii, j; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2014 | |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 2015 | /* Ignore the "sign" bit and scale down */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2016 | ii = (i & 0x7Fu) >> 1; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2017 | |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 2018 | /* Read the whole table to thwart cache-based timing attacks */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2019 | for (j = 0; j < T_size; j++) { |
| 2020 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&R->X, &T[j].X, j == ii)); |
| 2021 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&R->Y, &T[j].Y, j == ii)); |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 2022 | } |
| 2023 | |
Manuel Pégourié-Gonnard | 01fca5e | 2013-11-21 17:47:12 +0100 | [diff] [blame] | 2024 | /* Safely invert result if i is "negative" */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2025 | MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, R, i >> 7)); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2026 | |
| 2027 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2028 | return ret; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * Core multiplication algorithm for the (modified) comb method. |
| 2033 | * This part is actually common with the basic comb method (GECC 3.44) |
Manuel Pégourié-Gonnard | 04a0225 | 2013-11-20 22:57:38 +0100 | [diff] [blame] | 2034 | * |
| 2035 | * Cost: d A + d D + 1 R |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2036 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2037 | static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2038 | const mbedtls_ecp_point T[], unsigned char T_size, |
| 2039 | const unsigned char x[], size_t d, |
| 2040 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2041 | void *p_rng, |
| 2042 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2043 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2044 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2045 | mbedtls_ecp_point Txi; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2046 | size_t i; |
| 2047 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2048 | mbedtls_ecp_point_init(&Txi); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2049 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2050 | #if !defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 2051 | (void) rs_ctx; |
| 2052 | #endif |
| 2053 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2054 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2055 | if (rs_ctx != NULL && rs_ctx->rsm != NULL && |
| 2056 | rs_ctx->rsm->state != ecp_rsm_comb_core) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2057 | rs_ctx->rsm->i = 0; |
| 2058 | rs_ctx->rsm->state = ecp_rsm_comb_core; |
| 2059 | } |
| 2060 | |
| 2061 | /* new 'if' instead of nested for the sake of the 'else' branch */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2062 | if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) { |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 2063 | /* restore current index (R already pointing to rs_ctx->rsm->R) */ |
| 2064 | i = rs_ctx->rsm->i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2065 | } else |
Manuel Pégourié-Gonnard | c5d844b | 2017-03-15 13:06:28 +0100 | [diff] [blame] | 2066 | #endif |
| 2067 | { |
Paul Elliott | fc820d9 | 2023-01-13 16:29:30 +0000 | [diff] [blame] | 2068 | int have_rng = 1; |
| 2069 | |
Manuel Pégourié-Gonnard | c5d844b | 2017-03-15 13:06:28 +0100 | [diff] [blame] | 2070 | /* Start with a non-zero point and randomize its coordinates */ |
| 2071 | i = d; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2072 | MBEDTLS_MPI_CHK(ecp_select_comb(grp, R, T, T_size, x[i])); |
| 2073 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->Z, 1)); |
David Horstmann | ef661c5 | 2022-10-26 17:55:53 +0100 | [diff] [blame] | 2074 | |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2075 | #if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2076 | if (f_rng == NULL) { |
David Horstmann | ef661c5 | 2022-10-26 17:55:53 +0100 | [diff] [blame] | 2077 | have_rng = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2078 | } |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2079 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2080 | if (have_rng) { |
| 2081 | MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, R, f_rng, p_rng)); |
| 2082 | } |
Manuel Pégourié-Gonnard | c5d844b | 2017-03-15 13:06:28 +0100 | [diff] [blame] | 2083 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2084 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2085 | while (i != 0) { |
| 2086 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD); |
Manuel Pégourié-Gonnard | 90f31b7 | 2018-10-16 10:45:24 +0200 | [diff] [blame] | 2087 | --i; |
| 2088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2089 | MBEDTLS_MPI_CHK(ecp_double_jac(grp, R, R)); |
| 2090 | MBEDTLS_MPI_CHK(ecp_select_comb(grp, &Txi, T, T_size, x[i])); |
| 2091 | MBEDTLS_MPI_CHK(ecp_add_mixed(grp, R, R, &Txi)); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | cleanup: |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2095 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2096 | mbedtls_ecp_point_free(&Txi); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2097 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2098 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2099 | if (rs_ctx != NULL && rs_ctx->rsm != NULL && |
| 2100 | ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
Manuel Pégourié-Gonnard | 90f31b7 | 2018-10-16 10:45:24 +0200 | [diff] [blame] | 2101 | rs_ctx->rsm->i = i; |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2102 | /* no need to save R, already pointing to rs_ctx->rsm->R */ |
Manuel Pégourié-Gonnard | c5d844b | 2017-03-15 13:06:28 +0100 | [diff] [blame] | 2103 | } |
| 2104 | #endif |
| 2105 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2106 | return ret; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | /* |
Manuel Pégourié-Gonnard | 62738e9 | 2017-03-14 10:00:21 +0100 | [diff] [blame] | 2110 | * Recode the scalar to get constant-time comb multiplication |
| 2111 | * |
| 2112 | * As the actual scalar recoding needs an odd scalar as a starting point, |
| 2113 | * this wrapper ensures that by replacing m by N - m if necessary, and |
| 2114 | * informs the caller that the result of multiplication will be negated. |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 2115 | * |
Manuel Pégourié-Gonnard | fd87e35 | 2017-08-24 14:21:05 +0200 | [diff] [blame] | 2116 | * This works because we only support large prime order for Short Weierstrass |
| 2117 | * curves, so N is always odd hence either m or N - m is. |
| 2118 | * |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 2119 | * See ecp_comb_recode_core() for background. |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2120 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2121 | static int ecp_comb_recode_scalar(const mbedtls_ecp_group *grp, |
| 2122 | const mbedtls_mpi *m, |
| 2123 | unsigned char k[COMB_MAX_D + 1], |
| 2124 | size_t d, |
| 2125 | unsigned char w, |
| 2126 | unsigned char *parity_trick) |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2127 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2128 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 62738e9 | 2017-03-14 10:00:21 +0100 | [diff] [blame] | 2129 | mbedtls_mpi M, mm; |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2130 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2131 | mbedtls_mpi_init(&M); |
| 2132 | mbedtls_mpi_init(&mm); |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2133 | |
Manuel Pégourié-Gonnard | fd87e35 | 2017-08-24 14:21:05 +0200 | [diff] [blame] | 2134 | /* N is always odd (see above), just make extra sure */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2135 | if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) { |
| 2136 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
| 2137 | } |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2138 | |
Manuel Pégourié-Gonnard | 62738e9 | 2017-03-14 10:00:21 +0100 | [diff] [blame] | 2139 | /* do we need the parity trick? */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2140 | *parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0); |
Manuel Pégourié-Gonnard | 62738e9 | 2017-03-14 10:00:21 +0100 | [diff] [blame] | 2141 | |
| 2142 | /* execute parity fix in constant time */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2143 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m)); |
| 2144 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m)); |
| 2145 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick)); |
Manuel Pégourié-Gonnard | 62738e9 | 2017-03-14 10:00:21 +0100 | [diff] [blame] | 2146 | |
| 2147 | /* actual scalar recoding */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2148 | ecp_comb_recode_core(k, d, w, &M); |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2149 | |
| 2150 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2151 | mbedtls_mpi_free(&mm); |
| 2152 | mbedtls_mpi_free(&M); |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2153 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2154 | return ret; |
Manuel Pégourié-Gonnard | ec5606a | 2017-03-09 12:46:45 +0100 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | /* |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2158 | * Perform comb multiplication (for short Weierstrass curves) |
| 2159 | * once the auxiliary table has been pre-computed. |
Manuel Pégourié-Gonnard | 62738e9 | 2017-03-14 10:00:21 +0100 | [diff] [blame] | 2160 | * |
| 2161 | * Scalar recoding may use a parity trick that makes us compute -m * P, |
| 2162 | * if that is the case we'll need to recover m * P at the end. |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2163 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2164 | static int ecp_mul_comb_after_precomp(const mbedtls_ecp_group *grp, |
| 2165 | mbedtls_ecp_point *R, |
| 2166 | const mbedtls_mpi *m, |
| 2167 | const mbedtls_ecp_point *T, |
| 2168 | unsigned char T_size, |
| 2169 | unsigned char w, |
| 2170 | size_t d, |
| 2171 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2172 | void *p_rng, |
| 2173 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2174 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2175 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 62738e9 | 2017-03-14 10:00:21 +0100 | [diff] [blame] | 2176 | unsigned char parity_trick; |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2177 | unsigned char k[COMB_MAX_D + 1]; |
Manuel Pégourié-Gonnard | 8962ddb | 2017-03-14 12:11:21 +0100 | [diff] [blame] | 2178 | mbedtls_ecp_point *RR = R; |
Paul Elliott | fc820d9 | 2023-01-13 16:29:30 +0000 | [diff] [blame] | 2179 | int have_rng = 1; |
Manuel Pégourié-Gonnard | 8962ddb | 2017-03-14 12:11:21 +0100 | [diff] [blame] | 2180 | |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2181 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2182 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2183 | RR = &rs_ctx->rsm->R; |
| 2184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2185 | if (rs_ctx->rsm->state == ecp_rsm_final_norm) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2186 | goto final_norm; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2187 | } |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2188 | } |
Manuel Pégourié-Gonnard | 8962ddb | 2017-03-14 12:11:21 +0100 | [diff] [blame] | 2189 | #endif |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2191 | MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w, |
| 2192 | &parity_trick)); |
| 2193 | MBEDTLS_MPI_CHK(ecp_mul_comb_core(grp, RR, T, T_size, k, d, |
| 2194 | f_rng, p_rng, rs_ctx)); |
| 2195 | MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, RR, parity_trick)); |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2196 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2197 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2198 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2199 | rs_ctx->rsm->state = ecp_rsm_final_norm; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2200 | } |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 2201 | |
Manuel Pégourié-Gonnard | 4ed1dab | 2017-08-24 11:02:04 +0200 | [diff] [blame] | 2202 | final_norm: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2203 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV); |
Manuel Pégourié-Gonnard | 2fad7ae | 2017-03-14 13:13:13 +0100 | [diff] [blame] | 2204 | #endif |
Manuel Pégourié-Gonnard | a4aa89b | 2020-03-25 12:41:29 +0100 | [diff] [blame] | 2205 | /* |
| 2206 | * Knowledge of the jacobian coordinates may leak the last few bits of the |
| 2207 | * scalar [1], and since our MPI implementation isn't constant-flow, |
| 2208 | * inversion (used for coordinate normalization) may leak the full value |
| 2209 | * of its input via side-channels [2]. |
| 2210 | * |
| 2211 | * [1] https://eprint.iacr.org/2003/191 |
| 2212 | * [2] https://eprint.iacr.org/2020/055 |
| 2213 | * |
| 2214 | * Avoid the leak by randomizing coordinates before we normalize them. |
| 2215 | */ |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2216 | #if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2217 | if (f_rng == NULL) { |
David Horstmann | ef661c5 | 2022-10-26 17:55:53 +0100 | [diff] [blame] | 2218 | have_rng = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2219 | } |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2220 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2221 | if (have_rng) { |
| 2222 | MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, RR, f_rng, p_rng)); |
| 2223 | } |
Manuel Pégourié-Gonnard | a4aa89b | 2020-03-25 12:41:29 +0100 | [diff] [blame] | 2224 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2225 | MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, RR)); |
Manuel Pégourié-Gonnard | 8962ddb | 2017-03-14 12:11:21 +0100 | [diff] [blame] | 2226 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2227 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2228 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
| 2229 | MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, RR)); |
| 2230 | } |
Manuel Pégourié-Gonnard | 8962ddb | 2017-03-14 12:11:21 +0100 | [diff] [blame] | 2231 | #endif |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2232 | |
| 2233 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2234 | return ret; |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2235 | } |
| 2236 | |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2237 | /* |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2238 | * Pick window size based on curve size and whether we optimize for base point |
| 2239 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2240 | static unsigned char ecp_pick_window_size(const mbedtls_ecp_group *grp, |
| 2241 | unsigned char p_eq_g) |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2242 | { |
| 2243 | unsigned char w; |
| 2244 | |
| 2245 | /* |
| 2246 | * Minimize the number of multiplications, that is minimize |
| 2247 | * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w ) |
| 2248 | * (see costs of the various parts, with 1S = 1M) |
| 2249 | */ |
| 2250 | w = grp->nbits >= 384 ? 5 : 4; |
| 2251 | |
| 2252 | /* |
| 2253 | * If P == G, pre-compute a bit more, since this may be re-used later. |
| 2254 | * Just adding one avoids upping the cost of the first mul too much, |
| 2255 | * and the memory cost too. |
| 2256 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2257 | if (p_eq_g) { |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2258 | w++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2259 | } |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2260 | |
| 2261 | /* |
| 2262 | * Make sure w is within bounds. |
| 2263 | * (The last test is useful only for very small curves in the test suite.) |
| 2264 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2265 | #if (MBEDTLS_ECP_WINDOW_SIZE < 6) |
| 2266 | if (w > MBEDTLS_ECP_WINDOW_SIZE) { |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2267 | w = MBEDTLS_ECP_WINDOW_SIZE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2268 | } |
k-stachowiak | 653a4a2 | 2019-07-03 14:31:09 +0200 | [diff] [blame] | 2269 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2270 | if (w >= grp->nbits) { |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2271 | w = 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2272 | } |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2273 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2274 | return w; |
Manuel Pégourié-Gonnard | 4b2336d | 2017-03-09 13:23:50 +0100 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | /* |
Manuel Pégourié-Gonnard | 07bf6f5 | 2017-03-16 17:21:38 +0100 | [diff] [blame] | 2278 | * Multiplication using the comb method - for curves in short Weierstrass form |
| 2279 | * |
| 2280 | * This function is mainly responsible for administrative work: |
| 2281 | * - managing the restart context if enabled |
Manuel Pégourié-Gonnard | 11556e2 | 2017-08-24 13:41:19 +0200 | [diff] [blame] | 2282 | * - managing the table of precomputed points (passed between the below two |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2283 | * functions): allocation, computation, ownership transfer, freeing. |
Manuel Pégourié-Gonnard | 07bf6f5 | 2017-03-16 17:21:38 +0100 | [diff] [blame] | 2284 | * |
| 2285 | * It delegates the actual arithmetic work to: |
| 2286 | * ecp_precompute_comb() and ecp_mul_comb_with_precomp() |
| 2287 | * |
| 2288 | * See comments on ecp_comb_recode_core() regarding the computation strategy. |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2289 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2290 | static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2291 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 2292 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2293 | void *p_rng, |
| 2294 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2295 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2296 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 11556e2 | 2017-08-24 13:41:19 +0200 | [diff] [blame] | 2297 | unsigned char w, p_eq_g, i; |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 2298 | size_t d; |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2299 | unsigned char T_size = 0, T_ok = 0; |
| 2300 | mbedtls_ecp_point *T = NULL; |
| 2301 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
| 2302 | ecp_drbg_context drbg_ctx; |
| 2303 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2304 | ecp_drbg_init(&drbg_ctx); |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2305 | #endif |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2307 | ECP_RS_ENTER(rsm); |
Manuel Pégourié-Gonnard | 510d5ca | 2017-03-08 11:41:47 +0100 | [diff] [blame] | 2308 | |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2309 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2310 | if (f_rng == NULL) { |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2311 | /* Adjust pointers */ |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2312 | f_rng = &ecp_drbg_random; |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2313 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2314 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2315 | p_rng = &rs_ctx->rsm->drbg_ctx; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2316 | } else |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2317 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2318 | p_rng = &drbg_ctx; |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2319 | |
| 2320 | /* Initialize internal DRBG if necessary */ |
| 2321 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2322 | if (rs_ctx == NULL || rs_ctx->rsm == NULL || |
| 2323 | rs_ctx->rsm->drbg_seeded == 0) |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2324 | #endif |
| 2325 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2326 | const size_t m_len = (grp->nbits + 7) / 8; |
| 2327 | MBEDTLS_MPI_CHK(ecp_drbg_seed(p_rng, m, m_len)); |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2328 | } |
| 2329 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2330 | if (rs_ctx != NULL && rs_ctx->rsm != NULL) { |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2331 | rs_ctx->rsm->drbg_seeded = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2332 | } |
Manuel Pégourié-Gonnard | 53fb66d | 2020-06-04 09:43:14 +0200 | [diff] [blame] | 2333 | #endif |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2334 | } |
| 2335 | #endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ |
| 2336 | |
Manuel Pégourié-Gonnard | 22be635 | 2017-03-09 13:02:35 +0100 | [diff] [blame] | 2337 | /* Is P the base point ? */ |
| 2338 | #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1 |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2339 | p_eq_g = (mbedtls_mpi_cmp_mpi(&P->Y, &grp->G.Y) == 0 && |
| 2340 | mbedtls_mpi_cmp_mpi(&P->X, &grp->G.X) == 0); |
Manuel Pégourié-Gonnard | 196d133 | 2017-08-28 13:14:27 +0200 | [diff] [blame] | 2341 | #else |
| 2342 | p_eq_g = 0; |
Manuel Pégourié-Gonnard | 22be635 | 2017-03-09 13:02:35 +0100 | [diff] [blame] | 2343 | #endif |
| 2344 | |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2345 | /* Pick window size and deduce related sizes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2346 | w = ecp_pick_window_size(grp, p_eq_g); |
| 2347 | T_size = 1U << (w - 1); |
| 2348 | d = (grp->nbits + w - 1) / w; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2349 | |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 2350 | /* Pre-computed table: do we have it already for the base point? */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2351 | if (p_eq_g && grp->T != NULL) { |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 2352 | /* second pointer to the same table, will be deleted on exit */ |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 2353 | T = grp->T; |
| 2354 | T_ok = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2355 | } else |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2356 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 2357 | /* Pre-computed table: do we have one in progress? complete? */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2358 | if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) { |
Manuel Pégourié-Gonnard | 45fd016 | 2017-03-22 08:24:42 +0100 | [diff] [blame] | 2359 | /* transfer ownership of T from rsm to local function */ |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 2360 | T = rs_ctx->rsm->T; |
| 2361 | rs_ctx->rsm->T = NULL; |
| 2362 | rs_ctx->rsm->T_size = 0; |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 2363 | |
Manuel Pégourié-Gonnard | b25cb60 | 2018-10-16 11:48:09 +0200 | [diff] [blame] | 2364 | /* This effectively jumps to the call to mul_comb_after_precomp() */ |
Manuel Pégourié-Gonnard | 11556e2 | 2017-08-24 13:41:19 +0200 | [diff] [blame] | 2365 | T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2366 | } else |
Manuel Pégourié-Gonnard | c9c0aa6 | 2017-03-16 14:53:26 +0100 | [diff] [blame] | 2367 | #endif |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 2368 | /* Allocate table if we didn't have any */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2369 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2370 | T = mbedtls_calloc(T_size, sizeof(mbedtls_ecp_point)); |
| 2371 | if (T == NULL) { |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2372 | ret = MBEDTLS_ERR_ECP_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2373 | goto cleanup; |
| 2374 | } |
Manuel Pégourié-Gonnard | 5bd38b1 | 2017-08-23 16:55:59 +0200 | [diff] [blame] | 2375 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2376 | for (i = 0; i < T_size; i++) { |
| 2377 | mbedtls_ecp_point_init(&T[i]); |
| 2378 | } |
Manuel Pégourié-Gonnard | 11556e2 | 2017-08-24 13:41:19 +0200 | [diff] [blame] | 2379 | |
| 2380 | T_ok = 0; |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 2381 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2382 | |
Manuel Pégourié-Gonnard | 085b1df | 2017-03-16 16:56:04 +0100 | [diff] [blame] | 2383 | /* Compute table (or finish computing it) if not done already */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2384 | if (!T_ok) { |
| 2385 | MBEDTLS_MPI_CHK(ecp_precompute_comb(grp, T, P, w, d, rs_ctx)); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2386 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2387 | if (p_eq_g) { |
Manuel Pégourié-Gonnard | 7037e22 | 2017-08-23 14:30:36 +0200 | [diff] [blame] | 2388 | /* almost transfer ownership of T to the group, but keep a copy of |
Manuel Pégourié-Gonnard | ee68cff | 2018-10-15 15:27:49 +0200 | [diff] [blame] | 2389 | * the pointer to use for calling the next function more easily */ |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2390 | grp->T = T; |
Manuel Pégourié-Gonnard | 92cceb2 | 2017-08-23 16:27:29 +0200 | [diff] [blame] | 2391 | grp->T_size = T_size; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2392 | } |
| 2393 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2394 | |
Manuel Pégourié-Gonnard | 391f441 | 2017-03-13 12:26:21 +0100 | [diff] [blame] | 2395 | /* Actual comb multiplication using precomputed points */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2396 | MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m, |
| 2397 | T, T_size, w, d, |
| 2398 | f_rng, p_rng, rs_ctx)); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2399 | |
| 2400 | cleanup: |
| 2401 | |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2402 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2403 | ecp_drbg_free(&drbg_ctx); |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2404 | #endif |
| 2405 | |
Manuel Pégourié-Gonnard | 07bf6f5 | 2017-03-16 17:21:38 +0100 | [diff] [blame] | 2406 | /* does T belong to the group? */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2407 | if (T == grp->T) { |
Manuel Pégourié-Gonnard | 07bf6f5 | 2017-03-16 17:21:38 +0100 | [diff] [blame] | 2408 | T = NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2409 | } |
Manuel Pégourié-Gonnard | 07bf6f5 | 2017-03-16 17:21:38 +0100 | [diff] [blame] | 2410 | |
| 2411 | /* does T belong to the restart context? */ |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2412 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2413 | if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) { |
Manuel Pégourié-Gonnard | 45fd016 | 2017-03-22 08:24:42 +0100 | [diff] [blame] | 2414 | /* transfer ownership of T from local function to rsm */ |
Manuel Pégourié-Gonnard | 92cceb2 | 2017-08-23 16:27:29 +0200 | [diff] [blame] | 2415 | rs_ctx->rsm->T_size = T_size; |
Manuel Pégourié-Gonnard | 3cade22 | 2017-04-20 09:31:00 +0200 | [diff] [blame] | 2416 | rs_ctx->rsm->T = T; |
Manuel Pégourié-Gonnard | c9c0aa6 | 2017-03-16 14:53:26 +0100 | [diff] [blame] | 2417 | T = NULL; |
| 2418 | } |
| 2419 | #endif |
| 2420 | |
Manuel Pégourié-Gonnard | 07bf6f5 | 2017-03-16 17:21:38 +0100 | [diff] [blame] | 2421 | /* did T belong to us? then let's destroy it! */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2422 | if (T != NULL) { |
| 2423 | for (i = 0; i < T_size; i++) { |
| 2424 | mbedtls_ecp_point_free(&T[i]); |
| 2425 | } |
| 2426 | mbedtls_free(T); |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2427 | } |
| 2428 | |
Manuel Pégourié-Gonnard | 07bf6f5 | 2017-03-16 17:21:38 +0100 | [diff] [blame] | 2429 | /* prevent caller from using invalid value */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2430 | int should_free_R = (ret != 0); |
David Horstmann | b95ee00 | 2022-10-06 19:11:04 +0100 | [diff] [blame] | 2431 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
| 2432 | /* don't free R while in progress in case R == P */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2433 | if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) { |
David Horstmann | e9af9e3 | 2022-10-25 10:32:08 +0100 | [diff] [blame] | 2434 | should_free_R = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2435 | } |
David Horstmann | b95ee00 | 2022-10-06 19:11:04 +0100 | [diff] [blame] | 2436 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2437 | if (should_free_R) { |
| 2438 | mbedtls_ecp_point_free(R); |
| 2439 | } |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2440 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2441 | ECP_RS_LEAVE(rsm); |
Manuel Pégourié-Gonnard | 77af79a | 2017-03-14 10:58:00 +0100 | [diff] [blame] | 2442 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2443 | return ret; |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2444 | } |
| 2445 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2446 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 2447 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2448 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 2449 | /* |
| 2450 | * For Montgomery curves, we do all the internal arithmetic in projective |
| 2451 | * coordinates. Import/export of points uses only the x coordinates, which is |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2452 | * internally represented as X / Z. |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 2453 | * |
| 2454 | * For scalar multiplication, we'll use a Montgomery ladder. |
| 2455 | */ |
| 2456 | |
Manuel Pégourié-Gonnard | d1c1ba9 | 2013-11-16 15:50:12 +0100 | [diff] [blame] | 2457 | /* |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2458 | * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1 |
| 2459 | * Cost: 1M + 1I |
| 2460 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2461 | static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P) |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2462 | { |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2463 | #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2464 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 2465 | return mbedtls_internal_ecp_normalize_mxz(grp, P); |
| 2466 | } |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 2467 | #endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2468 | |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2469 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2470 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2471 | #else |
| 2472 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2473 | MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&P->Z, &P->Z, &grp->P)); |
| 2474 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->X, &P->X, &P->Z)); |
| 2475 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1)); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2476 | |
| 2477 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2478 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2479 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) */ |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | /* |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2483 | * Randomize projective x/z coordinates: |
| 2484 | * (X, Z) -> (l X, l Z) for random l |
| 2485 | * This is sort of the reverse operation of ecp_normalize_mxz(). |
| 2486 | * |
| 2487 | * This countermeasure was first suggested in [2]. |
| 2488 | * Cost: 2M |
| 2489 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2490 | static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, |
| 2491 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2492 | { |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2493 | #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2494 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 2495 | return mbedtls_internal_ecp_randomize_mxz(grp, P, f_rng, p_rng); |
| 2496 | } |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 2497 | #endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2498 | |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2499 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2500 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2501 | #else |
| 2502 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2503 | mbedtls_mpi l; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2504 | mbedtls_mpi_init(&l); |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2505 | |
| 2506 | /* Generate l such that 1 < l < p */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2507 | MBEDTLS_MPI_CHK(mbedtls_mpi_random(&l, 2, &grp->P, f_rng, p_rng)); |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2508 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2509 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->X, &P->X, &l)); |
| 2510 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &P->Z, &P->Z, &l)); |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2511 | |
| 2512 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2513 | mbedtls_mpi_free(&l); |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2514 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2515 | if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { |
Gilles Peskine | 6466d34 | 2021-03-29 22:28:50 +0200 | [diff] [blame] | 2516 | ret = MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2517 | } |
| 2518 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2519 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) */ |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /* |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2523 | * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q), |
| 2524 | * for Montgomery curves in x/z coordinates. |
| 2525 | * |
| 2526 | * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3 |
| 2527 | * with |
| 2528 | * d = X1 |
| 2529 | * P = (X2, Z2) |
| 2530 | * Q = (X3, Z3) |
| 2531 | * R = (X4, Z4) |
| 2532 | * S = (X5, Z5) |
| 2533 | * and eliminating temporary variables tO, ..., t4. |
| 2534 | * |
| 2535 | * Cost: 5M + 4S |
| 2536 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2537 | static int ecp_double_add_mxz(const mbedtls_ecp_group *grp, |
| 2538 | mbedtls_ecp_point *R, mbedtls_ecp_point *S, |
| 2539 | const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q, |
| 2540 | const mbedtls_mpi *d) |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2541 | { |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2542 | #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2543 | if (mbedtls_internal_ecp_grp_capable(grp)) { |
| 2544 | return mbedtls_internal_ecp_double_add_mxz(grp, R, S, P, Q, d); |
| 2545 | } |
Janos Follath | 372697b | 2016-10-28 16:53:11 +0100 | [diff] [blame] | 2546 | #endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */ |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2547 | |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2548 | #if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2549 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2550 | #else |
| 2551 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 2552 | mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB; |
| 2553 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2554 | mbedtls_mpi_init(&A); mbedtls_mpi_init(&AA); mbedtls_mpi_init(&B); |
| 2555 | mbedtls_mpi_init(&BB); mbedtls_mpi_init(&E); mbedtls_mpi_init(&C); |
| 2556 | mbedtls_mpi_init(&D); mbedtls_mpi_init(&DA); mbedtls_mpi_init(&CB); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2558 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &A, &P->X, &P->Z)); |
| 2559 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &AA, &A, &A)); |
| 2560 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &B, &P->X, &P->Z)); |
| 2561 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &BB, &B, &B)); |
| 2562 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &E, &AA, &BB)); |
| 2563 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &C, &Q->X, &Q->Z)); |
| 2564 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &D, &Q->X, &Q->Z)); |
| 2565 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &DA, &D, &A)); |
| 2566 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &CB, &C, &B)); |
| 2567 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &S->X, &DA, &CB)); |
| 2568 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->X, &S->X, &S->X)); |
| 2569 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, &S->Z, &DA, &CB)); |
| 2570 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->Z, &S->Z, &S->Z)); |
| 2571 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &S->Z, d, &S->Z)); |
| 2572 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->X, &AA, &BB)); |
| 2573 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->Z, &grp->A, &E)); |
| 2574 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &R->Z, &BB, &R->Z)); |
| 2575 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &R->Z, &E, &R->Z)); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2576 | |
| 2577 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2578 | mbedtls_mpi_free(&A); mbedtls_mpi_free(&AA); mbedtls_mpi_free(&B); |
| 2579 | mbedtls_mpi_free(&BB); mbedtls_mpi_free(&E); mbedtls_mpi_free(&C); |
| 2580 | mbedtls_mpi_free(&D); mbedtls_mpi_free(&DA); mbedtls_mpi_free(&CB); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2581 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2582 | return ret; |
Steven Cooreman | 7eb2aa0 | 2021-01-22 09:43:59 +0100 | [diff] [blame] | 2583 | #endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) */ |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | /* |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 2587 | * Multiplication with Montgomery ladder in x/z coordinates, |
| 2588 | * for curves in Montgomery form |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2589 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2590 | static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2591 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 2592 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2593 | void *p_rng) |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2594 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2595 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Agathiyan Bragadeesh | 5521b4c | 2023-07-31 16:15:56 +0100 | [diff] [blame^] | 2596 | int have_rng = 1; |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2597 | size_t i; |
Manuel Pégourié-Gonnard | b6f45a6 | 2013-12-04 21:54:36 +0100 | [diff] [blame] | 2598 | unsigned char b; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2599 | mbedtls_ecp_point RP; |
| 2600 | mbedtls_mpi PX; |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2601 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
| 2602 | ecp_drbg_context drbg_ctx; |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2603 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2604 | ecp_drbg_init(&drbg_ctx); |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2605 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2606 | mbedtls_ecp_point_init(&RP); mbedtls_mpi_init(&PX); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2607 | |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2608 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2609 | if (f_rng == NULL) { |
| 2610 | const size_t m_len = (grp->nbits + 7) / 8; |
| 2611 | MBEDTLS_MPI_CHK(ecp_drbg_seed(&drbg_ctx, m, m_len)); |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2612 | f_rng = &ecp_drbg_random; |
| 2613 | p_rng = &drbg_ctx; |
| 2614 | } |
| 2615 | #endif /* !MBEDTLS_ECP_NO_INTERNAL_RNG */ |
| 2616 | |
Manuel Pégourié-Gonnard | 3afa07f | 2013-12-03 13:28:21 +0100 | [diff] [blame] | 2617 | /* Save PX and read from P before writing to R, in case P == R */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2618 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&PX, &P->X)); |
| 2619 | MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&RP, P)); |
Manuel Pégourié-Gonnard | 357ff65 | 2013-12-04 18:39:17 +0100 | [diff] [blame] | 2620 | |
| 2621 | /* Set R to zero in modified x/z coordinates */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2622 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->X, 1)); |
| 2623 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&R->Z, 0)); |
| 2624 | mbedtls_mpi_free(&R->Y); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2625 | |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2626 | /* RP.X might be slightly larger than P, so reduce it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2627 | MOD_ADD(RP.X); |
Manuel Pégourié-Gonnard | 93f41db | 2013-12-05 10:48:42 +0100 | [diff] [blame] | 2628 | |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2629 | #if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Agathiyan Bragadeesh | 5521b4c | 2023-07-31 16:15:56 +0100 | [diff] [blame^] | 2630 | /* Derandomize coordinates of the starting point */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2631 | if (f_rng == NULL) { |
David Horstmann | ef661c5 | 2022-10-26 17:55:53 +0100 | [diff] [blame] | 2632 | have_rng = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2633 | } |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2634 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2635 | if (have_rng) { |
| 2636 | MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, &RP, f_rng, p_rng)); |
| 2637 | } |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2638 | |
Manuel Pégourié-Gonnard | b6f45a6 | 2013-12-04 21:54:36 +0100 | [diff] [blame] | 2639 | /* Loop invariant: R = result so far, RP = R + P */ |
Aurelien Jarno | edc110d | 2022-05-15 13:24:05 +0200 | [diff] [blame] | 2640 | i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2641 | while (i-- > 0) { |
| 2642 | b = mbedtls_mpi_get_bit(m, i); |
Manuel Pégourié-Gonnard | b6f45a6 | 2013-12-04 21:54:36 +0100 | [diff] [blame] | 2643 | /* |
| 2644 | * if (b) R = 2R + P else R = 2R, |
| 2645 | * which is: |
| 2646 | * if (b) double_add( RP, R, RP, R ) |
| 2647 | * else double_add( R, RP, R, RP ) |
| 2648 | * but using safe conditional swaps to avoid leaks |
| 2649 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2650 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->X, &RP.X, b)); |
| 2651 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->Z, &RP.Z, b)); |
| 2652 | MBEDTLS_MPI_CHK(ecp_double_add_mxz(grp, R, &RP, R, &RP, &PX)); |
| 2653 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->X, &RP.X, b)); |
| 2654 | MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap(&R->Z, &RP.Z, b)); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
Manuel Pégourié-Gonnard | a4aa89b | 2020-03-25 12:41:29 +0100 | [diff] [blame] | 2657 | /* |
| 2658 | * Knowledge of the projective coordinates may leak the last few bits of the |
| 2659 | * scalar [1], and since our MPI implementation isn't constant-flow, |
| 2660 | * inversion (used for coordinate normalization) may leak the full value |
| 2661 | * of its input via side-channels [2]. |
| 2662 | * |
| 2663 | * [1] https://eprint.iacr.org/2003/191 |
| 2664 | * [2] https://eprint.iacr.org/2020/055 |
| 2665 | * |
| 2666 | * Avoid the leak by randomizing coordinates before we normalize them. |
| 2667 | */ |
David Horstmann | ef661c5 | 2022-10-26 17:55:53 +0100 | [diff] [blame] | 2668 | have_rng = 1; |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2669 | #if defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2670 | if (f_rng == NULL) { |
David Horstmann | ef661c5 | 2022-10-26 17:55:53 +0100 | [diff] [blame] | 2671 | have_rng = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2672 | } |
Manuel Pégourié-Gonnard | 22b1de3 | 2020-06-04 10:43:29 +0200 | [diff] [blame] | 2673 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2674 | if (have_rng) { |
| 2675 | MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, R, f_rng, p_rng)); |
| 2676 | } |
Manuel Pégourié-Gonnard | a4aa89b | 2020-03-25 12:41:29 +0100 | [diff] [blame] | 2677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2678 | MBEDTLS_MPI_CHK(ecp_normalize_mxz(grp, R)); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2679 | |
| 2680 | cleanup: |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2681 | #if !defined(MBEDTLS_ECP_NO_INTERNAL_RNG) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2682 | ecp_drbg_free(&drbg_ctx); |
Manuel Pégourié-Gonnard | f2a9fcf | 2020-06-03 12:11:56 +0200 | [diff] [blame] | 2683 | #endif |
| 2684 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2685 | mbedtls_ecp_point_free(&RP); mbedtls_mpi_free(&PX); |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2686 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2687 | return ret; |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2688 | } |
| 2689 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2690 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
Manuel Pégourié-Gonnard | 7c94d8b | 2013-12-04 23:15:46 +0100 | [diff] [blame] | 2691 | |
Manuel Pégourié-Gonnard | d9ea82e7 | 2013-12-03 12:02:28 +0100 | [diff] [blame] | 2692 | /* |
Manuel Pégourié-Gonnard | 884569c | 2017-04-20 10:10:59 +0200 | [diff] [blame] | 2693 | * Restartable multiplication R = m * P |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 2694 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2695 | int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2696 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 2697 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 2698 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 2699 | { |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2700 | int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 2701 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
| 2702 | char is_grp_capable = 0; |
| 2703 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2704 | ECP_VALIDATE_RET(grp != NULL); |
| 2705 | ECP_VALIDATE_RET(R != NULL); |
| 2706 | ECP_VALIDATE_RET(m != NULL); |
| 2707 | ECP_VALIDATE_RET(P != NULL); |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 2708 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2709 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2710 | /* reset ops count for this call if top-level */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2711 | if (rs_ctx != NULL && rs_ctx->depth++ == 0) { |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2712 | rs_ctx->ops_done = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2713 | } |
Gilles Peskine | 5997005 | 2019-02-28 13:12:06 +0100 | [diff] [blame] | 2714 | #else |
| 2715 | (void) rs_ctx; |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2716 | #endif |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 2717 | |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 2718 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2719 | if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) { |
| 2720 | MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp)); |
| 2721 | } |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 2722 | #endif /* MBEDTLS_ECP_INTERNAL_ALT */ |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2723 | |
David Horstmann | b95ee00 | 2022-10-06 19:11:04 +0100 | [diff] [blame] | 2724 | int restarting = 0; |
Manuel Pégourié-Gonnard | 95aedfe | 2017-08-24 13:47:04 +0200 | [diff] [blame] | 2725 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2726 | restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL); |
Manuel Pégourié-Gonnard | a08cd1a | 2017-04-20 11:29:43 +0200 | [diff] [blame] | 2727 | #endif |
David Horstmann | b95ee00 | 2022-10-06 19:11:04 +0100 | [diff] [blame] | 2728 | /* skip argument check when restarting */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2729 | if (!restarting) { |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 2730 | /* check_privkey is free */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2731 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_CHK); |
Manuel Pégourié-Gonnard | 5314f23 | 2017-04-21 12:36:59 +0200 | [diff] [blame] | 2732 | |
Manuel Pégourié-Gonnard | a08cd1a | 2017-04-20 11:29:43 +0200 | [diff] [blame] | 2733 | /* Common sanity checks */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2734 | MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m)); |
| 2735 | MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P)); |
Manuel Pégourié-Gonnard | a08cd1a | 2017-04-20 11:29:43 +0200 | [diff] [blame] | 2736 | } |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2737 | |
| 2738 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2739 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2740 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 2741 | MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng)); |
| 2742 | } |
Janos Follath | 430d337 | 2016-11-03 14:25:37 +0000 | [diff] [blame] | 2743 | #endif |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2744 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2745 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
| 2746 | MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx)); |
| 2747 | } |
Janos Follath | 430d337 | 2016-11-03 14:25:37 +0000 | [diff] [blame] | 2748 | #endif |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2749 | |
Janos Follath | 6c8ccd5 | 2016-11-29 15:37:09 +0000 | [diff] [blame] | 2750 | cleanup: |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 2751 | |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2752 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2753 | if (is_grp_capable) { |
| 2754 | mbedtls_internal_ecp_free(grp); |
| 2755 | } |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 2756 | #endif /* MBEDTLS_ECP_INTERNAL_ALT */ |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2757 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2758 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2759 | if (rs_ctx != NULL) { |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2760 | rs_ctx->depth--; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2761 | } |
Manuel Pégourié-Gonnard | 3a25612 | 2017-04-20 11:20:26 +0200 | [diff] [blame] | 2762 | #endif |
| 2763 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2764 | return ret; |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 2765 | } |
| 2766 | |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 2767 | /* |
Manuel Pégourié-Gonnard | 884569c | 2017-04-20 10:10:59 +0200 | [diff] [blame] | 2768 | * Multiplication R = m * P |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 2769 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2770 | int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2771 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 2772 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 2773 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2774 | ECP_VALIDATE_RET(grp != NULL); |
| 2775 | ECP_VALIDATE_RET(R != NULL); |
| 2776 | ECP_VALIDATE_RET(m != NULL); |
| 2777 | ECP_VALIDATE_RET(P != NULL); |
| 2778 | return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL); |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 2779 | } |
Manuel Pégourié-Gonnard | b739a71 | 2017-04-19 10:11:56 +0200 | [diff] [blame] | 2780 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2781 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Manuel Pégourié-Gonnard | a0179b8 | 2013-12-04 11:49:20 +0100 | [diff] [blame] | 2782 | /* |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 2783 | * Check that an affine point is valid as a public key, |
| 2784 | * short weierstrass curves (SEC1 3.2.3.1) |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2785 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2786 | static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2787 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2788 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2789 | mbedtls_mpi YY, RHS; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2790 | |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 2791 | /* pt coordinates must be normalized for our checks */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2792 | if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 || |
| 2793 | mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 || |
| 2794 | mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 || |
| 2795 | mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) { |
| 2796 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 2797 | } |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2798 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2799 | mbedtls_mpi_init(&YY); mbedtls_mpi_init(&RHS); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2800 | |
| 2801 | /* |
| 2802 | * YY = Y^2 |
Manuel Pégourié-Gonnard | cd7458a | 2013-10-08 13:11:30 +0200 | [diff] [blame] | 2803 | * RHS = X (X^2 + A) + B = X^3 + A X + B |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2804 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2805 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &YY, &pt->Y, &pt->Y)); |
| 2806 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &RHS, &pt->X, &pt->X)); |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 2807 | |
| 2808 | /* Special case for A = -3 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2809 | if (grp->A.p == NULL) { |
| 2810 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&RHS, &RHS, 3)); MOD_SUB(RHS); |
| 2811 | } else { |
| 2812 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &RHS, &RHS, &grp->A)); |
Manuel Pégourié-Gonnard | 73cc01d | 2013-12-06 12:41:30 +0100 | [diff] [blame] | 2813 | } |
| 2814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2815 | MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, &RHS, &RHS, &pt->X)); |
| 2816 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, &RHS, &RHS, &grp->B)); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2817 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2818 | if (mbedtls_mpi_cmp_mpi(&YY, &RHS) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2819 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2820 | } |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2821 | |
| 2822 | cleanup: |
| 2823 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2824 | mbedtls_mpi_free(&YY); mbedtls_mpi_free(&RHS); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2825 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2826 | return ret; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 2827 | } |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2828 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 2829 | |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 2830 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2831 | /* |
TRodziewicz | 9edff74 | 2021-03-04 17:59:39 +0100 | [diff] [blame] | 2832 | * R = m * P with shortcuts for m == 0, m == 1 and m == -1 |
Manuel Pégourié-Gonnard | de9f953 | 2015-10-23 15:50:37 +0200 | [diff] [blame] | 2833 | * NOT constant-time - ONLY for short Weierstrass! |
| 2834 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2835 | static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp, |
| 2836 | mbedtls_ecp_point *R, |
| 2837 | const mbedtls_mpi *m, |
| 2838 | const mbedtls_ecp_point *P, |
| 2839 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | de9f953 | 2015-10-23 15:50:37 +0200 | [diff] [blame] | 2840 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2841 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | de9f953 | 2015-10-23 15:50:37 +0200 | [diff] [blame] | 2842 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2843 | if (mbedtls_mpi_cmp_int(m, 0) == 0) { |
| 2844 | MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P)); |
| 2845 | MBEDTLS_MPI_CHK(mbedtls_ecp_set_zero(R)); |
| 2846 | } else if (mbedtls_mpi_cmp_int(m, 1) == 0) { |
| 2847 | MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P)); |
| 2848 | MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P)); |
| 2849 | } else if (mbedtls_mpi_cmp_int(m, -1) == 0) { |
| 2850 | MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P)); |
| 2851 | MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P)); |
| 2852 | if (mbedtls_mpi_cmp_int(&R->Y, 0) != 0) { |
| 2853 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&R->Y, &grp->P, &R->Y)); |
| 2854 | } |
| 2855 | } else { |
| 2856 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, R, m, P, |
| 2857 | NULL, NULL, rs_ctx)); |
Manuel Pégourié-Gonnard | de9f953 | 2015-10-23 15:50:37 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2861 | return ret; |
Manuel Pégourié-Gonnard | de9f953 | 2015-10-23 15:50:37 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | /* |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 2865 | * Restartable linear combination |
Manuel Pégourié-Gonnard | de9f953 | 2015-10-23 15:50:37 +0200 | [diff] [blame] | 2866 | * NOT constant-time |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2867 | */ |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 2868 | int mbedtls_ecp_muladd_restartable( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2869 | mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2870 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 2871 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q, |
| 2872 | mbedtls_ecp_restart_ctx *rs_ctx) |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2873 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 2874 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2875 | mbedtls_ecp_point mP; |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2876 | mbedtls_ecp_point *pmP = &mP; |
| 2877 | mbedtls_ecp_point *pR = R; |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 2878 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
| 2879 | char is_grp_capable = 0; |
| 2880 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2881 | ECP_VALIDATE_RET(grp != NULL); |
| 2882 | ECP_VALIDATE_RET(R != NULL); |
| 2883 | ECP_VALIDATE_RET(m != NULL); |
| 2884 | ECP_VALIDATE_RET(P != NULL); |
| 2885 | ECP_VALIDATE_RET(n != NULL); |
| 2886 | ECP_VALIDATE_RET(Q != NULL); |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2887 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2888 | if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
| 2889 | return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 2890 | } |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2891 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2892 | mbedtls_ecp_point_init(&mP); |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2893 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2894 | ECP_RS_ENTER(ma); |
Manuel Pégourié-Gonnard | db4a8eb | 2017-08-23 18:18:22 +0200 | [diff] [blame] | 2895 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2896 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2897 | if (rs_ctx != NULL && rs_ctx->ma != NULL) { |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2898 | /* redirect intermediate results to restart context */ |
| 2899 | pmP = &rs_ctx->ma->mP; |
| 2900 | pR = &rs_ctx->ma->R; |
| 2901 | |
| 2902 | /* jump to next operation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2903 | if (rs_ctx->ma->state == ecp_rsma_mul2) { |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2904 | goto mul2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2905 | } |
| 2906 | if (rs_ctx->ma->state == ecp_rsma_add) { |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2907 | goto add; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2908 | } |
| 2909 | if (rs_ctx->ma->state == ecp_rsma_norm) { |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2910 | goto norm; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2911 | } |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2912 | } |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2913 | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 2914 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2915 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx)); |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2916 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2917 | if (rs_ctx != NULL && rs_ctx->ma != NULL) { |
Manuel Pégourié-Gonnard | c9efa00 | 2017-08-24 10:25:06 +0200 | [diff] [blame] | 2918 | rs_ctx->ma->state = ecp_rsma_mul2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2919 | } |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2920 | |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2921 | mul2: |
| 2922 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2923 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pR, n, Q, rs_ctx)); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2924 | |
| 2925 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2926 | if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) { |
| 2927 | MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp)); |
| 2928 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2929 | #endif /* MBEDTLS_ECP_INTERNAL_ALT */ |
| 2930 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2931 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2932 | if (rs_ctx != NULL && rs_ctx->ma != NULL) { |
Manuel Pégourié-Gonnard | c9efa00 | 2017-08-24 10:25:06 +0200 | [diff] [blame] | 2933 | rs_ctx->ma->state = ecp_rsma_add; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2934 | } |
Manuel Pégourié-Gonnard | 1a7c5ef | 2015-08-13 10:19:09 +0200 | [diff] [blame] | 2935 | |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2936 | add: |
| 2937 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2938 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_ADD); |
| 2939 | MBEDTLS_MPI_CHK(ecp_add_mixed(grp, pR, pmP, pR)); |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2940 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2941 | if (rs_ctx != NULL && rs_ctx->ma != NULL) { |
Manuel Pégourié-Gonnard | c9efa00 | 2017-08-24 10:25:06 +0200 | [diff] [blame] | 2942 | rs_ctx->ma->state = ecp_rsma_norm; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2943 | } |
Janos Follath | 430d337 | 2016-11-03 14:25:37 +0000 | [diff] [blame] | 2944 | |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2945 | norm: |
| 2946 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2947 | MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV); |
| 2948 | MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, pR)); |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2949 | |
Manuel Pégourié-Gonnard | 4b9c51e | 2017-04-20 15:50:26 +0200 | [diff] [blame] | 2950 | #if defined(MBEDTLS_ECP_RESTARTABLE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2951 | if (rs_ctx != NULL && rs_ctx->ma != NULL) { |
| 2952 | MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, pR)); |
| 2953 | } |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2954 | #endif |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2955 | |
| 2956 | cleanup: |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 2957 | #if defined(MBEDTLS_ECP_INTERNAL_ALT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2958 | if (is_grp_capable) { |
| 2959 | mbedtls_internal_ecp_free(grp); |
| 2960 | } |
Janos Follath | c44ab97 | 2016-11-18 16:38:23 +0000 | [diff] [blame] | 2961 | #endif /* MBEDTLS_ECP_INTERNAL_ALT */ |
Manuel Pégourié-Gonnard | 1631d63 | 2017-04-20 14:48:56 +0200 | [diff] [blame] | 2962 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2963 | mbedtls_ecp_point_free(&mP); |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2964 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2965 | ECP_RS_LEAVE(ma); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 2966 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2967 | return ret; |
Manuel Pégourié-Gonnard | 56cc88a | 2015-05-11 18:40:45 +0200 | [diff] [blame] | 2968 | } |
| 2969 | |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 2970 | /* |
| 2971 | * Linear combination |
| 2972 | * NOT constant-time |
| 2973 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2974 | int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R, |
| 2975 | const mbedtls_mpi *m, const mbedtls_ecp_point *P, |
| 2976 | const mbedtls_mpi *n, const mbedtls_ecp_point *Q) |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 2977 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2978 | ECP_VALIDATE_RET(grp != NULL); |
| 2979 | ECP_VALIDATE_RET(R != NULL); |
| 2980 | ECP_VALIDATE_RET(m != NULL); |
| 2981 | ECP_VALIDATE_RET(P != NULL); |
| 2982 | ECP_VALIDATE_RET(n != NULL); |
| 2983 | ECP_VALIDATE_RET(Q != NULL); |
| 2984 | return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL); |
Manuel Pégourié-Gonnard | 54dd652 | 2017-04-20 13:36:18 +0200 | [diff] [blame] | 2985 | } |
Gilles Peskine | 9b99a89 | 2018-09-14 18:32:19 +0200 | [diff] [blame] | 2986 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 2987 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 2988 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 2989 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2990 | #define ECP_MPI_INIT(s, n, p) { s, (n), (mbedtls_mpi_uint *) (p) } |
Manuel Pégourié-Gonnard | 520f0a0 | 2021-06-23 12:53:18 +0200 | [diff] [blame] | 2991 | #define ECP_MPI_INIT_ARRAY(x) \ |
| 2992 | ECP_MPI_INIT(1, sizeof(x) / sizeof(mbedtls_mpi_uint), x) |
| 2993 | /* |
| 2994 | * Constants for the two points other than 0, 1, -1 (mod p) in |
| 2995 | * https://cr.yp.to/ecdh.html#validate |
| 2996 | * See ecp_check_pubkey_x25519(). |
| 2997 | */ |
| 2998 | static const mbedtls_mpi_uint x25519_bad_point_1[] = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2999 | MBEDTLS_BYTES_TO_T_UINT_8(0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae), |
| 3000 | MBEDTLS_BYTES_TO_T_UINT_8(0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a), |
| 3001 | MBEDTLS_BYTES_TO_T_UINT_8(0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd), |
| 3002 | MBEDTLS_BYTES_TO_T_UINT_8(0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00), |
Manuel Pégourié-Gonnard | 520f0a0 | 2021-06-23 12:53:18 +0200 | [diff] [blame] | 3003 | }; |
| 3004 | static const mbedtls_mpi_uint x25519_bad_point_2[] = { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3005 | MBEDTLS_BYTES_TO_T_UINT_8(0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24), |
| 3006 | MBEDTLS_BYTES_TO_T_UINT_8(0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b), |
| 3007 | MBEDTLS_BYTES_TO_T_UINT_8(0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86), |
| 3008 | MBEDTLS_BYTES_TO_T_UINT_8(0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57), |
Manuel Pégourié-Gonnard | 520f0a0 | 2021-06-23 12:53:18 +0200 | [diff] [blame] | 3009 | }; |
| 3010 | static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3011 | x25519_bad_point_1); |
Manuel Pégourié-Gonnard | 520f0a0 | 2021-06-23 12:53:18 +0200 | [diff] [blame] | 3012 | static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3013 | x25519_bad_point_2); |
Janos Follath | 7d4ebdd | 2021-06-24 15:34:59 +0100 | [diff] [blame] | 3014 | #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */ |
Manuel Pégourié-Gonnard | 10b8e5a | 2021-06-23 12:25:48 +0200 | [diff] [blame] | 3015 | |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3016 | /* |
| 3017 | * Check that the input point is not one of the low-order points. |
| 3018 | * This is recommended by the "May the Fourth" paper: |
| 3019 | * https://eprint.iacr.org/2017/806.pdf |
| 3020 | * Those points are never sent by an honest peer. |
| 3021 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3022 | static int ecp_check_bad_points_mx(const mbedtls_mpi *X, const mbedtls_mpi *P, |
| 3023 | const mbedtls_ecp_group_id grp_id) |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3024 | { |
| 3025 | int ret; |
Manuel Pégourié-Gonnard | 10b8e5a | 2021-06-23 12:25:48 +0200 | [diff] [blame] | 3026 | mbedtls_mpi XmP; |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3027 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3028 | mbedtls_mpi_init(&XmP); |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3029 | |
| 3030 | /* Reduce X mod P so that we only need to check values less than P. |
| 3031 | * We know X < 2^256 so we can proceed by subtraction. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3032 | MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&XmP, X)); |
| 3033 | while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) { |
| 3034 | MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&XmP, &XmP, P)); |
| 3035 | } |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3036 | |
Janos Follath | 7d4ebdd | 2021-06-24 15:34:59 +0100 | [diff] [blame] | 3037 | /* Check against the known bad values that are less than P. For Curve448 |
| 3038 | * these are 0, 1 and -1. For Curve25519 we check the values less than P |
| 3039 | * from the following list: https://cr.yp.to/ecdh.html#validate */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3040 | if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) { /* takes care of 0 and 1 */ |
Janos Follath | b4c676e | 2021-06-24 14:24:13 +0100 | [diff] [blame] | 3041 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3042 | goto cleanup; |
| 3043 | } |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3044 | |
Janos Follath | 7d4ebdd | 2021-06-24 15:34:59 +0100 | [diff] [blame] | 3045 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3046 | if (grp_id == MBEDTLS_ECP_DP_CURVE25519) { |
| 3047 | if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) { |
Janos Follath | 7d4ebdd | 2021-06-24 15:34:59 +0100 | [diff] [blame] | 3048 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3049 | goto cleanup; |
| 3050 | } |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3051 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3052 | if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) { |
Janos Follath | 7d4ebdd | 2021-06-24 15:34:59 +0100 | [diff] [blame] | 3053 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3054 | goto cleanup; |
| 3055 | } |
Janos Follath | b4c676e | 2021-06-24 14:24:13 +0100 | [diff] [blame] | 3056 | } |
Janos Follath | 2667fb7 | 2021-06-25 15:29:56 +0100 | [diff] [blame] | 3057 | #else |
| 3058 | (void) grp_id; |
Janos Follath | 7d4ebdd | 2021-06-24 15:34:59 +0100 | [diff] [blame] | 3059 | #endif |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3060 | |
Manuel Pégourié-Gonnard | 10b8e5a | 2021-06-23 12:25:48 +0200 | [diff] [blame] | 3061 | /* Final check: check if XmP + 1 is P (final because it changes XmP!) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3062 | MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&XmP, &XmP, 1)); |
| 3063 | if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) { |
Janos Follath | b4c676e | 2021-06-24 14:24:13 +0100 | [diff] [blame] | 3064 | ret = MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3065 | goto cleanup; |
| 3066 | } |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3067 | |
| 3068 | ret = 0; |
| 3069 | |
| 3070 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3071 | mbedtls_mpi_free(&XmP); |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3072 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3073 | return ret; |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3074 | } |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3075 | |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3076 | /* |
| 3077 | * Check validity of a public key for Montgomery curves with x-only schemes |
| 3078 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3079 | static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3080 | { |
Manuel Pégourié-Gonnard | 0789433 | 2015-06-23 00:18:41 +0200 | [diff] [blame] | 3081 | /* [Curve25519 p. 5] Just check X is the correct number of bytes */ |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 3082 | /* Allow any public value, if it's too big then we'll just reduce it mod p |
| 3083 | * (RFC 7748 sec. 5 para. 3). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3084 | if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) { |
| 3085 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3086 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3087 | |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3088 | /* Implicit in all standards (as they don't consider negative numbers): |
| 3089 | * X must be non-negative. This is normally ensured by the way it's |
| 3090 | * encoded for transmission, but let's be extra sure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3091 | if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) { |
| 3092 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3093 | } |
Manuel Pégourié-Gonnard | f2268d1 | 2021-06-23 10:14:58 +0200 | [diff] [blame] | 3094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3095 | return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id); |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3096 | } |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3097 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3098 | |
| 3099 | /* |
| 3100 | * Check that a point is valid as a public key |
| 3101 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3102 | int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp, |
| 3103 | const mbedtls_ecp_point *pt) |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3104 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3105 | ECP_VALIDATE_RET(grp != NULL); |
| 3106 | ECP_VALIDATE_RET(pt != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3107 | |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3108 | /* Must use affine coordinates */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3109 | if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) { |
| 3110 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3111 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3112 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3113 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3114 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 3115 | return ecp_check_pubkey_mx(grp, pt); |
| 3116 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3117 | #endif |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3118 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3119 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
| 3120 | return ecp_check_pubkey_sw(grp, pt); |
| 3121 | } |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3122 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3123 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | d962273 | 2013-12-05 10:06:06 +0100 | [diff] [blame] | 3124 | } |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 3125 | |
| 3126 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3127 | * Check that an mbedtls_mpi is valid as a private key |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 3128 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3129 | int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp, |
| 3130 | const mbedtls_mpi *d) |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 3131 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3132 | ECP_VALIDATE_RET(grp != NULL); |
| 3133 | ECP_VALIDATE_RET(d != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3134 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3135 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3136 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 3137 | /* see RFC 7748 sec. 5 para. 5 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3138 | if (mbedtls_mpi_get_bit(d, 0) != 0 || |
| 3139 | mbedtls_mpi_get_bit(d, 1) != 0 || |
| 3140 | mbedtls_mpi_bitlen(d) - 1 != grp->nbits) { /* mbedtls_mpi_bitlen is one-based! */ |
| 3141 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3142 | } |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 3143 | |
| 3144 | /* see [Curve25519] page 5 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3145 | if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) { |
| 3146 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3147 | } |
Nicholas Wilson | 08f3ef1 | 2015-11-10 13:10:01 +0000 | [diff] [blame] | 3148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3149 | return 0; |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 3150 | } |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3151 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
| 3152 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3153 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 3154 | /* see SEC1 3.2 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3155 | if (mbedtls_mpi_cmp_int(d, 1) < 0 || |
| 3156 | mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) { |
| 3157 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
| 3158 | } else { |
| 3159 | return 0; |
| 3160 | } |
Manuel Pégourié-Gonnard | 312d2e8 | 2013-12-04 11:08:01 +0100 | [diff] [blame] | 3161 | } |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3162 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 3163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3164 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
Gilles Peskine | de33213 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 3167 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
| 3168 | MBEDTLS_STATIC_TESTABLE |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3169 | int mbedtls_ecp_gen_privkey_mx(size_t high_bit, |
| 3170 | mbedtls_mpi *d, |
| 3171 | int (*f_rng)(void *, unsigned char *, size_t), |
| 3172 | void *p_rng) |
Gilles Peskine | de33213 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 3173 | { |
| 3174 | int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Gilles Peskine | eadf31d | 2021-03-24 12:46:46 +0100 | [diff] [blame] | 3175 | size_t n_random_bytes = high_bit / 8 + 1; |
Gilles Peskine | de33213 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 3176 | |
| 3177 | /* [Curve25519] page 5 */ |
Gilles Peskine | eadf31d | 2021-03-24 12:46:46 +0100 | [diff] [blame] | 3178 | /* Generate a (high_bit+1)-bit random number by generating just enough |
| 3179 | * random bytes, then shifting out extra bits from the top (necessary |
| 3180 | * when (high_bit+1) is not a multiple of 8). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3181 | MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(d, n_random_bytes, |
| 3182 | f_rng, p_rng)); |
| 3183 | MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1)); |
Gilles Peskine | de33213 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 3184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3185 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, high_bit, 1)); |
Gilles Peskine | de33213 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 3186 | |
| 3187 | /* Make sure the last two bits are unset for Curve448, three bits for |
| 3188 | Curve25519 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3189 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 0, 0)); |
| 3190 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 1, 0)); |
| 3191 | if (high_bit == 254) { |
| 3192 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 2, 0)); |
Gilles Peskine | de33213 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3196 | return ret; |
Gilles Peskine | de33213 | 2021-03-23 22:31:31 +0100 | [diff] [blame] | 3197 | } |
| 3198 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
| 3199 | |
Gilles Peskine | aeab0fb | 2021-03-29 22:28:21 +0200 | [diff] [blame] | 3200 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
| 3201 | static int mbedtls_ecp_gen_privkey_sw( |
| 3202 | const mbedtls_mpi *N, mbedtls_mpi *d, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3203 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Gilles Peskine | aeab0fb | 2021-03-29 22:28:21 +0200 | [diff] [blame] | 3204 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3205 | int ret = mbedtls_mpi_random(d, 1, N, f_rng, p_rng); |
| 3206 | switch (ret) { |
Gilles Peskine | aeab0fb | 2021-03-29 22:28:21 +0200 | [diff] [blame] | 3207 | case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3208 | return MBEDTLS_ERR_ECP_RANDOM_FAILED; |
Gilles Peskine | aeab0fb | 2021-03-29 22:28:21 +0200 | [diff] [blame] | 3209 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3210 | return ret; |
Gilles Peskine | aeab0fb | 2021-03-29 22:28:21 +0200 | [diff] [blame] | 3211 | } |
| 3212 | } |
| 3213 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
| 3214 | |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 3215 | /* |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 3216 | * Generate a private key |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 3217 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3218 | int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp, |
| 3219 | mbedtls_mpi *d, |
| 3220 | int (*f_rng)(void *, unsigned char *, size_t), |
| 3221 | void *p_rng) |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 3222 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3223 | ECP_VALIDATE_RET(grp != NULL); |
| 3224 | ECP_VALIDATE_RET(d != NULL); |
| 3225 | ECP_VALIDATE_RET(f_rng != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3226 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3227 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3228 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 3229 | return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng); |
| 3230 | } |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3231 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 3232 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3233 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3234 | if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
| 3235 | return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng); |
| 3236 | } |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3237 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 3238 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3239 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 3240 | } |
Manuel Pégourié-Gonnard | c957399 | 2014-01-03 12:54:00 +0100 | [diff] [blame] | 3241 | |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 3242 | /* |
| 3243 | * Generate a keypair with configurable base point |
| 3244 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3245 | int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp, |
| 3246 | const mbedtls_ecp_point *G, |
| 3247 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 3248 | int (*f_rng)(void *, unsigned char *, size_t), |
| 3249 | void *p_rng) |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 3250 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3251 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3252 | ECP_VALIDATE_RET(grp != NULL); |
| 3253 | ECP_VALIDATE_RET(d != NULL); |
| 3254 | ECP_VALIDATE_RET(G != NULL); |
| 3255 | ECP_VALIDATE_RET(Q != NULL); |
| 3256 | ECP_VALIDATE_RET(f_rng != NULL); |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 3257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3258 | MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng)); |
| 3259 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, Q, d, G, f_rng, p_rng)); |
Manuel Pégourié-Gonnard | a7937f9 | 2017-04-20 15:37:46 +0200 | [diff] [blame] | 3260 | |
| 3261 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3262 | return ret; |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | /* |
| 3266 | * Generate key pair, wrapper for conventional base point |
| 3267 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3268 | int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, |
| 3269 | mbedtls_mpi *d, mbedtls_ecp_point *Q, |
| 3270 | int (*f_rng)(void *, unsigned char *, size_t), |
| 3271 | void *p_rng) |
Manuel Pégourié-Gonnard | d9a3f47 | 2015-08-11 14:31:03 +0200 | [diff] [blame] | 3272 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3273 | ECP_VALIDATE_RET(grp != NULL); |
| 3274 | ECP_VALIDATE_RET(d != NULL); |
| 3275 | ECP_VALIDATE_RET(Q != NULL); |
| 3276 | ECP_VALIDATE_RET(f_rng != NULL); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 3277 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3278 | return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 3279 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 3280 | |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 3281 | /* |
| 3282 | * Generate a keypair, prettier wrapper |
| 3283 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3284 | int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
| 3285 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 3286 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3287 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3288 | ECP_VALIDATE_RET(key != NULL); |
| 3289 | ECP_VALIDATE_RET(f_rng != NULL); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 3290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3291 | if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { |
| 3292 | return ret; |
| 3293 | } |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 3294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3295 | return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng); |
Manuel Pégourié-Gonnard | 104ee1d | 2013-11-30 14:13:16 +0100 | [diff] [blame] | 3296 | } |
| 3297 | |
Janos Follath | 7780096 | 2019-02-25 16:32:08 +0000 | [diff] [blame] | 3298 | #define ECP_CURVE25519_KEY_SIZE 32 |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3299 | /* |
| 3300 | * Read a private key. |
| 3301 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3302 | int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, |
| 3303 | const unsigned char *buf, size_t buflen) |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3304 | { |
| 3305 | int ret = 0; |
| 3306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3307 | ECP_VALIDATE_RET(key != NULL); |
| 3308 | ECP_VALIDATE_RET(buf != NULL); |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3310 | if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) { |
| 3311 | return ret; |
| 3312 | } |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3313 | |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3314 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
| 3315 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3316 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3317 | if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3318 | /* |
| 3319 | * If it is Curve25519 curve then mask the key as mandated by RFC7748 |
| 3320 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3321 | if (grp_id == MBEDTLS_ECP_DP_CURVE25519) { |
| 3322 | if (buflen != ECP_CURVE25519_KEY_SIZE) { |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3323 | return MBEDTLS_ERR_ECP_INVALID_KEY; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3324 | } |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3326 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen)); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3327 | |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3328 | /* Set the three least significant bits to 0 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3329 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0)); |
| 3330 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0)); |
| 3331 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0)); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3332 | |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3333 | /* Set the most significant bit to 0 */ |
| 3334 | MBEDTLS_MPI_CHK( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3335 | mbedtls_mpi_set_bit(&key->d, |
| 3336 | ECP_CURVE25519_KEY_SIZE * 8 - 1, 0) |
| 3337 | ); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3338 | |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3339 | /* Set the second most significant bit to 1 */ |
| 3340 | MBEDTLS_MPI_CHK( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3341 | mbedtls_mpi_set_bit(&key->d, |
| 3342 | ECP_CURVE25519_KEY_SIZE * 8 - 2, 1) |
| 3343 | ); |
| 3344 | } else { |
Janos Follath | 28eb06d | 2019-02-26 10:53:34 +0000 | [diff] [blame] | 3345 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3346 | } |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | #endif |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3350 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3351 | if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
| 3352 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen)); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3354 | MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d)); |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | #endif |
| 3358 | cleanup: |
| 3359 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3360 | if (ret != 0) { |
| 3361 | mbedtls_mpi_free(&key->d); |
| 3362 | } |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3363 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3364 | return ret; |
Janos Follath | 171a7ef | 2019-02-15 16:17:45 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3367 | /* |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3368 | * Write a private key. |
| 3369 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3370 | int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key, |
| 3371 | unsigned char *buf, size_t buflen) |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3372 | { |
Steven Cooreman | 0024df6 | 2020-07-13 10:59:40 +0200 | [diff] [blame] | 3373 | int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3375 | ECP_VALIDATE_RET(key != NULL); |
| 3376 | ECP_VALIDATE_RET(buf != NULL); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3377 | |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3378 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3379 | if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) { |
| 3380 | if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) { |
| 3381 | if (buflen < ECP_CURVE25519_KEY_SIZE) { |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3382 | return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3383 | } |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3384 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3385 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen)); |
| 3386 | } else { |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3387 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3388 | } |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | #endif |
Gilles Peskine | e8c04fe | 2018-09-14 17:44:21 +0200 | [diff] [blame] | 3392 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3393 | if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) { |
| 3394 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen)); |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | #endif |
| 3398 | cleanup: |
| 3399 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3400 | return ret; |
Steven Cooreman | de8593f | 2020-06-09 19:55:26 +0200 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | |
| 3404 | /* |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3405 | * Check a public-private key pair |
| 3406 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3407 | int mbedtls_ecp_check_pub_priv(const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv) |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3408 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3409 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3410 | mbedtls_ecp_point Q; |
| 3411 | mbedtls_ecp_group grp; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3412 | ECP_VALIDATE_RET(pub != NULL); |
| 3413 | ECP_VALIDATE_RET(prv != NULL); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3414 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3415 | if (pub->grp.id == MBEDTLS_ECP_DP_NONE || |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3416 | pub->grp.id != prv->grp.id || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3417 | mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) || |
| 3418 | mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) || |
| 3419 | mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) { |
| 3420 | return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3421 | } |
| 3422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3423 | mbedtls_ecp_point_init(&Q); |
| 3424 | mbedtls_ecp_group_init(&grp); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3425 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3426 | /* mbedtls_ecp_mul() needs a non-const group... */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3427 | mbedtls_ecp_group_copy(&grp, &prv->grp); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3428 | |
| 3429 | /* Also checks d is valid */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3430 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, NULL, NULL)); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3431 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3432 | if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) || |
| 3433 | mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) || |
| 3434 | mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3435 | ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3436 | goto cleanup; |
| 3437 | } |
| 3438 | |
| 3439 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3440 | mbedtls_ecp_point_free(&Q); |
| 3441 | mbedtls_ecp_group_free(&grp); |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3442 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3443 | return ret; |
Manuel Pégourié-Gonnard | 30668d6 | 2014-11-06 15:25:32 +0100 | [diff] [blame] | 3444 | } |
| 3445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3446 | #if defined(MBEDTLS_SELF_TEST) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 3447 | |
Gilles Peskine | 6d9c8d7 | 2020-07-22 01:26:25 +0200 | [diff] [blame] | 3448 | /* Adjust the exponent to be a valid private point for the specified curve. |
| 3449 | * This is sometimes necessary because we use a single set of exponents |
| 3450 | * for all curves but the validity of values depends on the curve. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3451 | static int self_test_adjust_exponent(const mbedtls_ecp_group *grp, |
| 3452 | mbedtls_mpi *m) |
Gilles Peskine | a088c81 | 2018-09-17 18:31:15 +0200 | [diff] [blame] | 3453 | { |
| 3454 | int ret = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3455 | switch (grp->id) { |
| 3456 | /* If Curve25519 is available, then that's what we use for the |
| 3457 | * Montgomery test, so we don't need the adjustment code. */ |
| 3458 | #if !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Gilles Peskine | a088c81 | 2018-09-17 18:31:15 +0200 | [diff] [blame] | 3459 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 3460 | case MBEDTLS_ECP_DP_CURVE448: |
| 3461 | /* Move highest bit from 254 to N-1. Setting bit N-1 is |
| 3462 | * necessary to enforce the highest-bit-set constraint. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3463 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0)); |
| 3464 | MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1)); |
Gilles Peskine | a088c81 | 2018-09-17 18:31:15 +0200 | [diff] [blame] | 3465 | /* Copy second-highest bit from 253 to N-2. This is not |
| 3466 | * necessary but improves the test variety a bit. */ |
| 3467 | MBEDTLS_MPI_CHK( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3468 | mbedtls_mpi_set_bit(m, grp->nbits - 1, |
| 3469 | mbedtls_mpi_get_bit(m, 253))); |
Gilles Peskine | a088c81 | 2018-09-17 18:31:15 +0200 | [diff] [blame] | 3470 | break; |
| 3471 | #endif |
| 3472 | #endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */ |
| 3473 | default: |
| 3474 | /* Non-Montgomery curves and Curve25519 need no adjustment. */ |
| 3475 | (void) grp; |
| 3476 | (void) m; |
| 3477 | goto cleanup; |
| 3478 | } |
| 3479 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3480 | return ret; |
Gilles Peskine | a088c81 | 2018-09-17 18:31:15 +0200 | [diff] [blame] | 3481 | } |
| 3482 | |
Gilles Peskine | 6d9c8d7 | 2020-07-22 01:26:25 +0200 | [diff] [blame] | 3483 | /* Calculate R = m.P for each m in exponents. Check that the number of |
| 3484 | * basic operations doesn't depend on the value of m. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3485 | static int self_test_point(int verbose, |
| 3486 | mbedtls_ecp_group *grp, |
| 3487 | mbedtls_ecp_point *R, |
| 3488 | mbedtls_mpi *m, |
| 3489 | const mbedtls_ecp_point *P, |
| 3490 | const char *const *exponents, |
| 3491 | size_t n_exponents) |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3492 | { |
| 3493 | int ret = 0; |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3494 | size_t i = 0; |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3495 | unsigned long add_c_prev, dbl_c_prev, mul_c_prev; |
| 3496 | add_count = 0; |
| 3497 | dbl_count = 0; |
| 3498 | mul_count = 0; |
Gilles Peskine | a088c81 | 2018-09-17 18:31:15 +0200 | [diff] [blame] | 3499 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3500 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0])); |
| 3501 | MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m)); |
| 3502 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, NULL, NULL)); |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3503 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3504 | for (i = 1; i < n_exponents; i++) { |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3505 | add_c_prev = add_count; |
| 3506 | dbl_c_prev = dbl_count; |
| 3507 | mul_c_prev = mul_count; |
| 3508 | add_count = 0; |
| 3509 | dbl_count = 0; |
| 3510 | mul_count = 0; |
| 3511 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3512 | MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i])); |
| 3513 | MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m)); |
| 3514 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, NULL, NULL)); |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3515 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3516 | if (add_count != add_c_prev || |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3517 | dbl_count != dbl_c_prev || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3518 | mul_count != mul_c_prev) { |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3519 | ret = 1; |
| 3520 | break; |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | cleanup: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3525 | if (verbose != 0) { |
| 3526 | if (ret != 0) { |
| 3527 | mbedtls_printf("failed (%u)\n", (unsigned int) i); |
| 3528 | } else { |
| 3529 | mbedtls_printf("passed\n"); |
| 3530 | } |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3531 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3532 | return ret; |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3533 | } |
| 3534 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 3535 | /* |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 3536 | * Checkup routine |
| 3537 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3538 | int mbedtls_ecp_self_test(int verbose) |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 3539 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 3540 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3541 | mbedtls_ecp_group grp; |
| 3542 | mbedtls_ecp_point R, P; |
| 3543 | mbedtls_mpi m; |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3544 | |
| 3545 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Gilles Peskine | d9767a5 | 2018-09-14 19:29:47 +0200 | [diff] [blame] | 3546 | /* Exponents especially adapted for secp192k1, which has the lowest |
| 3547 | * order n of all supported curves (secp192r1 is in a slightly larger |
| 3548 | * field but the order of its base point is slightly smaller). */ |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3549 | const char *sw_exponents[] = |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3550 | { |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 3551 | "000000000000000000000000000000000000000000000001", /* one */ |
Gilles Peskine | d9767a5 | 2018-09-14 19:29:47 +0200 | [diff] [blame] | 3552 | "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */ |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 3553 | "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */ |
Manuel Pégourié-Gonnard | ff27b7c | 2013-11-21 09:28:03 +0100 | [diff] [blame] | 3554 | "400000000000000000000000000000000000000000000000", /* one and zeros */ |
| 3555 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */ |
| 3556 | "555555555555555555555555555555555555555555555555", /* 101010... */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3557 | }; |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3558 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
| 3559 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
| 3560 | const char *m_exponents[] = |
| 3561 | { |
Gilles Peskine | 6d9c8d7 | 2020-07-22 01:26:25 +0200 | [diff] [blame] | 3562 | /* Valid private values for Curve25519. In a build with Curve448 |
| 3563 | * but not Curve25519, they will be adjusted in |
| 3564 | * self_test_adjust_exponent(). */ |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3565 | "4000000000000000000000000000000000000000000000000000000000000000", |
| 3566 | "5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30", |
| 3567 | "5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8", |
| 3568 | "41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460", |
| 3569 | "5555555555555555555555555555555555555555555555555555555555555550", |
| 3570 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8", |
| 3571 | }; |
| 3572 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3573 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3574 | mbedtls_ecp_group_init(&grp); |
| 3575 | mbedtls_ecp_point_init(&R); |
| 3576 | mbedtls_ecp_point_init(&P); |
| 3577 | mbedtls_mpi_init(&m); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3578 | |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3579 | #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 3580 | /* Use secp192r1 if available, or any available curve */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3581 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3582 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP192R1)); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 3583 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3584 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id)); |
Manuel Pégourié-Gonnard | b8012fc | 2013-10-10 15:40:49 +0200 | [diff] [blame] | 3585 | #endif |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3586 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3587 | if (verbose != 0) { |
| 3588 | mbedtls_printf(" ECP SW test #1 (constant op_count, base point G): "); |
| 3589 | } |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 3590 | /* Do a dummy multiplication first to trigger precomputation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3591 | MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2)); |
| 3592 | MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, NULL, NULL)); |
| 3593 | ret = self_test_point(verbose, |
| 3594 | &grp, &R, &m, &grp.G, |
| 3595 | sw_exponents, |
| 3596 | sizeof(sw_exponents) / sizeof(sw_exponents[0])); |
| 3597 | if (ret != 0) { |
Gilles Peskine | c95696f | 2018-09-17 15:59:01 +0200 | [diff] [blame] | 3598 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3599 | } |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3600 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3601 | if (verbose != 0) { |
| 3602 | mbedtls_printf(" ECP SW test #2 (constant op_count, other point): "); |
| 3603 | } |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 3604 | /* We computed P = 2G last time, use it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3605 | ret = self_test_point(verbose, |
| 3606 | &grp, &R, &m, &P, |
| 3607 | sw_exponents, |
| 3608 | sizeof(sw_exponents) / sizeof(sw_exponents[0])); |
| 3609 | if (ret != 0) { |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3610 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3611 | } |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3613 | mbedtls_ecp_group_free(&grp); |
| 3614 | mbedtls_ecp_point_free(&R); |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3615 | #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ |
| 3616 | |
| 3617 | #if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3618 | if (verbose != 0) { |
| 3619 | mbedtls_printf(" ECP Montgomery test (constant op_count): "); |
| 3620 | } |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3621 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3622 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE25519)); |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3623 | #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3624 | MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE448)); |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3625 | #else |
| 3626 | #error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test" |
| 3627 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3628 | ret = self_test_point(verbose, |
| 3629 | &grp, &R, &m, &grp.G, |
| 3630 | m_exponents, |
| 3631 | sizeof(m_exponents) / sizeof(m_exponents[0])); |
| 3632 | if (ret != 0) { |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3633 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3634 | } |
Gilles Peskine | 2466679 | 2018-09-17 18:29:49 +0200 | [diff] [blame] | 3635 | #endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame] | 3636 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3637 | cleanup: |
| 3638 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3639 | if (ret < 0 && verbose != 0) { |
| 3640 | mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret); |
| 3641 | } |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3643 | mbedtls_ecp_group_free(&grp); |
| 3644 | mbedtls_ecp_point_free(&R); |
| 3645 | mbedtls_ecp_point_free(&P); |
| 3646 | mbedtls_mpi_free(&m); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3647 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3648 | if (verbose != 0) { |
| 3649 | mbedtls_printf("\n"); |
| 3650 | } |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 3651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3652 | return ret; |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 3653 | } |
| 3654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3655 | #endif /* MBEDTLS_SELF_TEST */ |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 3656 | |
Janos Follath | b069753 | 2016-08-18 12:38:46 +0100 | [diff] [blame] | 3657 | #endif /* !MBEDTLS_ECP_ALT */ |
| 3658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3659 | #endif /* MBEDTLS_ECP_C */ |