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