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