blob: 365372a448838355e12f2df63e5f3ed8a098a0c5 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010020 */
21
22/*
23 * References:
24 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010025 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010026 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010027 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010028 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020029 *
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +020030 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010031 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020032 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
34 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
35 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010036 *
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +020037 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010038 * render ECC resistant against Side Channel Attacks. IACR Cryptology
39 * ePrint Archive, 2004, vol. 2004, p. 342.
40 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010041 */
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020045#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020047#endif
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010050
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/ecp.h"
Janos Follath430d3372016-11-03 14:25:37 +000052#include "mbedtls/threading.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020053
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <string.h>
55
Janos Follathb0697532016-08-18 12:38:46 +010056#if !defined(MBEDTLS_ECP_ALT)
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020060#else
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <stdlib.h>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000062#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_printf printf
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020064#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020066#endif
67
Janos Follath47d28f02016-11-01 13:22:05 +000068#include "mbedtls/ecp_internal.h"
Janos Follathb0697532016-08-18 12:38:46 +010069
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010070#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
71 !defined(inline) && !defined(__cplusplus)
Paul Bakker6a6087e2013-10-28 18:53:08 +010072#define inline __inline
Manuel Pégourié-Gonnard20af64d2015-07-07 18:33:39 +020073#endif
Paul Bakker6a6087e2013-10-28 18:53:08 +010074
Paul Bakker34617722014-06-13 17:20:13 +020075/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020077 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
78}
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010081/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010082 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020083 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010084 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010085static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010086#endif
87
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +020088#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +010089/*
90 * Maximum number of "basic operations" to be done in a row.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +020091 *
92 * Default value 0 means that ECC operations will not yield.
93 * Note that regardless of the value of ecp_max_ops, always at
94 * least one step is performed before yielding.
95 *
96 * Setting ecp_max_ops=1 can be suitable for testing purposes
97 * as it will interrupt computation at all possible points.
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +010098 */
99static unsigned ecp_max_ops = 0;
100
101/*
102 * Set ecp_max_ops
103 */
104void mbedtls_ecp_set_max_ops( unsigned max_ops )
105{
106 ecp_max_ops = max_ops;
107}
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100108
109/*
Manuel Pégourié-Gonnarda0c5bcc2017-04-21 11:33:57 +0200110 * Check if restart is enabled
111 */
112int mbedtls_ecp_restart_enabled( void )
113{
114 return( ecp_max_ops != 0 );
115}
116
117/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200118 * Restart sub-context for ecp_mul_comb()
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100119 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200120struct mbedtls_ecp_restart_mul
121{
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100122 mbedtls_ecp_point R; /* current intermediate result */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +0100123 size_t i; /* current index in various loops, 0 outside */
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100124 mbedtls_ecp_point *T; /* table for precomputed points */
125 unsigned char T_size; /* number of points in table T */
126 enum { /* what's the next step ? */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100127 ecp_rsm_init = 0, /* just getting started */
128 ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
129 ecp_rsm_pre_add, /* precompute remaining points by adding */
130 ecp_rsm_pre_norm_add, /* normalize all precomputed points */
131 ecp_rsm_T_done, /* call ecp_mul_comb_after_precomp() */
132 ecp_rsm_final_norm, /* do the final normalization */
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100133 } state;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100134};
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100135
136/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200137 * Init restart_mul sub-context
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +0100138 */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100139static void ecp_restart_mul_init( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100140{
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100141 memset( ctx, 0, sizeof( mbedtls_ecp_restart_mul_ctx ) );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100142}
143
144/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200145 * Free the components of a restart_mul sub-context
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100146 */
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100147static void ecp_restart_mul_free( mbedtls_ecp_restart_mul_ctx *ctx )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100148{
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100149 unsigned char i;
150
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100151 if( ctx == NULL )
152 return;
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +0100153
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +0100154 mbedtls_ecp_point_free( &ctx->R );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100155
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +0200156 if( ctx->T != NULL )
157 {
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +0100158 for( i = 0; i < ctx->T_size; i++ )
159 mbedtls_ecp_point_free( ctx->T + i );
160 mbedtls_free( ctx->T );
161 }
162
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +0100163 memset( ctx, 0, sizeof( mbedtls_ecp_restart_mul_ctx ) );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +0100164}
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100165
166/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200167 * Restart context for ecp_muladd()
168 */
169struct mbedtls_ecp_restart_muladd
170{
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200171 mbedtls_ecp_point mP; /* mP value */
172 mbedtls_ecp_point R; /* R intermediate result */
173 enum { /* what should we do next? */
174 ecp_rsma_mul1 = 0, /* first multiplication */
175 ecp_rsma_mul2, /* second multiplication */
176 ecp_rsma_add, /* addition */
177 ecp_rsma_norm, /* normalization */
178 } state;
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200179};
180
181/*
182 * Init restart_muladd sub-context
183 */
184static void ecp_restart_muladd_init( mbedtls_ecp_restart_muladd_ctx *ctx )
185{
186 memset( ctx, 0, sizeof( *ctx ) );
187}
188
189/*
190 * Free the components of a restart_muladd sub-context
191 */
192static void ecp_restart_muladd_free( mbedtls_ecp_restart_muladd_ctx *ctx )
193{
194 if( ctx == NULL )
195 return;
196
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200197 mbedtls_ecp_point_free( &ctx->mP );
198 mbedtls_ecp_point_free( &ctx->R );
199
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +0200200 memset( ctx, 0, sizeof( *ctx ) );
201}
202
203/*
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200204 * Initialize a restart context
205 */
206void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx )
207{
208 memset( ctx, 0, sizeof( *ctx ) );
209}
210
211/*
212 * Free the components of a restart context
213 */
214void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx )
215{
216 if( ctx == NULL )
217 return;
218
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200219 ctx->ops_done = 0;
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +0200220 ctx->depth = 0;
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200221
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200222 ecp_restart_mul_free( ctx->rsm );
223 mbedtls_free( ctx->rsm );
224 ctx->rsm = NULL;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +0200225
226 ecp_restart_muladd_free( ctx->ma );
227 mbedtls_free( ctx->ma );
228 ctx->ma = NULL;
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +0200229}
230
231/*
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100232 * Check if we can do the next step
233 */
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +0200234int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
235 mbedtls_ecp_restart_ctx *rs_ctx,
236 unsigned ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100237{
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200238 if( rs_ctx != NULL && ecp_max_ops != 0 )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100239 {
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100240 /* scale depending on curve size: the chosen reference is 256-bit,
241 * and multiplication is quadratic. Round to the closest integer. */
242 if( grp->pbits >= 512 )
243 ops *= 4;
244 else if( grp->pbits >= 384 )
245 ops *= 2;
246
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100247 /* avoid infinite loops: always allow first step */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200248 if( rs_ctx->ops_done != 0 && rs_ctx->ops_done + ops > ecp_max_ops )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100249 return( MBEDTLS_ERR_ECP_IN_PROGRESS );
250
Manuel Pégourié-Gonnarde6854492017-03-20 14:35:19 +0100251 /* update running count */
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +0200252 rs_ctx->ops_done += ops;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +0100253 }
254
255 return( 0 );
256}
257
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +0200258#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard054433c2017-03-22 11:18:33 +0100259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
261 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
262 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
263 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
264 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
265 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
266 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
267 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
268 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
269 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
270 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200271#define ECP_SHORTWEIERSTRASS
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100272#endif
273
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +0200274#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200275#define ECP_MONTGOMERY
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100276#endif
277
278/*
279 * Curve types: internal for now, might be exposed later
280 */
281typedef enum
282{
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200283 ECP_TYPE_NONE = 0,
284 ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
285 ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100286} ecp_curve_type;
287
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100288/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200289 * List of supported curves:
290 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200291 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200292 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200293 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100294 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100295 * Curves are listed in order: largest curves first, and for a given size,
296 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200297 *
298 * Reminder: update profiles in x509_crt.c when adding a new curves!
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300static const mbedtls_ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
303 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
306 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
309 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
312 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
315 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200316#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
318 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100319#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
321 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Gergely Budaie40c4692014-01-22 11:22:20 +0100322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
324 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
327 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100328#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
330 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100331#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
333 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200336};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100337
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200338#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
339 sizeof( ecp_supported_curves[0] )
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200342
343/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200344 * List of supported curves and associated info
345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200347{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200348 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200349}
350
351/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100352 * List of supported curves, group ID only
353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100355{
356 static int init_done = 0;
357
358 if( ! init_done )
359 {
360 size_t i = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 for( curve_info = mbedtls_ecp_curve_list();
364 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100365 curve_info++ )
366 {
367 ecp_supported_grp_id[i++] = curve_info->grp_id;
368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100370
371 init_done = 1;
372 }
373
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200374 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100375}
376
377/*
378 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200381{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 for( curve_info = mbedtls_ecp_curve_list();
385 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200386 curve_info++ )
387 {
388 if( curve_info->grp_id == grp_id )
389 return( curve_info );
390 }
391
392 return( NULL );
393}
394
395/*
396 * Get the curve info from the TLS identifier
397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 for( curve_info = mbedtls_ecp_curve_list();
403 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200404 curve_info++ )
405 {
406 if( curve_info->tls_id == tls_id )
407 return( curve_info );
408 }
409
410 return( NULL );
411}
412
413/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100414 * Get the curve info from the name
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 for( curve_info = mbedtls_ecp_curve_list();
421 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100422 curve_info++ )
423 {
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200424 if( strcmp( curve_info->name, name ) == 0 )
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100425 return( curve_info );
426 }
427
428 return( NULL );
429}
430
431/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100432 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100435{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100436 if( grp->G.X.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200437 return( ECP_TYPE_NONE );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100438
439 if( grp->G.Y.p == NULL )
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200440 return( ECP_TYPE_MONTGOMERY );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100441 else
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200442 return( ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100443}
444
445/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100446 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100449{
450 if( pt == NULL )
451 return;
452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_mpi_init( &pt->X );
454 mbedtls_mpi_init( &pt->Y );
455 mbedtls_mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100456}
457
458/*
459 * Initialize (the components of) a group
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100462{
463 if( grp == NULL )
464 return;
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 memset( grp, 0, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100467}
468
469/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200470 * Initialize (the components of) a key pair
471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200473{
Paul Bakker66d5d072014-06-17 16:39:18 +0200474 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200475 return;
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_ecp_group_init( &key->grp );
478 mbedtls_mpi_init( &key->d );
479 mbedtls_ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200480}
481
482/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100483 * Unallocate (the components of) a point
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100486{
487 if( pt == NULL )
488 return;
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_mpi_free( &( pt->X ) );
491 mbedtls_mpi_free( &( pt->Y ) );
492 mbedtls_mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100493}
494
495/*
496 * Unallocate (the components of) a group
497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100499{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200500 size_t i;
501
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100502 if( grp == NULL )
503 return;
504
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100505 if( grp->h != 1 )
506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_mpi_free( &grp->P );
508 mbedtls_mpi_free( &grp->A );
509 mbedtls_mpi_free( &grp->B );
510 mbedtls_ecp_point_free( &grp->G );
511 mbedtls_mpi_free( &grp->N );
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100512 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200513
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200514 if( grp->T != NULL )
515 {
516 for( i = 0; i < grp->T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_ecp_point_free( &grp->T[i] );
518 mbedtls_free( grp->T );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200519 }
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 mbedtls_zeroize( grp, sizeof( mbedtls_ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100522}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100523
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100524/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200525 * Unallocate (the components of) a key pair
526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200528{
Paul Bakker66d5d072014-06-17 16:39:18 +0200529 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200530 return;
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_ecp_group_free( &key->grp );
533 mbedtls_mpi_free( &key->d );
534 mbedtls_ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200535}
536
537/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200538 * Copy the contents of a point
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200541{
542 int ret;
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
545 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
546 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200547
548cleanup:
549 return( ret );
550}
551
552/*
553 * Copy the contents of a group object
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200556{
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200557 return mbedtls_ecp_group_load( dst, src->id );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200558}
559
560/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100561 * Set point to zero
562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100564{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100565 int ret;
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
568 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
569 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100570
571cleanup:
572 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100573}
574
575/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100576 * Tell if a point is zero
577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100581}
582
583/*
Manuel Pégourié-Gonnard6029a852015-08-11 15:44:41 +0200584 * Compare two points lazyly
585 */
586int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
587 const mbedtls_ecp_point *Q )
588{
589 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
590 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
591 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
592 {
593 return( 0 );
594 }
595
596 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
597}
598
599/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100600 * Import a non-zero point from ASCII strings
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100603 const char *x, const char *y )
604{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100605 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
608 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
609 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100610
611cleanup:
612 return( ret );
613}
614
615/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100616 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100619 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100620 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100621{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200622 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100623 size_t plen;
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 if( format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
626 format != MBEDTLS_ECP_PF_COMPRESSED )
627 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100628
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100629 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100630 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100633 {
634 if( buflen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100636
637 buf[0] = 0x00;
638 *olen = 1;
639
640 return( 0 );
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100646 {
647 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100648
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100649 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100651
652 buf[0] = 0x04;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
654 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100655 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100657 {
658 *olen = plen + 1;
659
660 if( buflen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
664 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100665 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100666
667cleanup:
668 return( ret );
669}
670
671/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100672 * Import a point from unsigned binary data (SEC1 2.3.4)
673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100675 const unsigned char *buf, size_t ilen )
676{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100677 int ret;
678 size_t plen;
679
Paul Bakker82788fb2014-10-20 13:59:19 +0200680 if( ilen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200682
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100683 if( buf[0] == 0x00 )
684 {
685 if( ilen == 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 return( mbedtls_ecp_set_zero( pt ) );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100687 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100689 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 plen = mbedtls_mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100692
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100693 if( buf[0] != 0x04 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100695
696 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
700 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
701 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100702
703cleanup:
704 return( ret );
705}
706
707/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100708 * Import a point from a TLS ECPoint record (RFC 4492)
709 * struct {
710 * opaque point <1..2^8-1>;
711 * } ECPoint;
712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100714 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100715{
716 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100717 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100718
719 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200720 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100721 */
722 if( buf_len < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100724
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100725 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100726 if( data_len < 1 || data_len > buf_len - 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100728
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100729 /*
730 * Save buffer start for read_binary and update buf
731 */
732 buf_start = *buf;
733 *buf += data_len;
734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100736}
737
738/*
739 * Export a point as a TLS ECPoint record (RFC 4492)
740 * struct {
741 * opaque point <1..2^8-1>;
742 * } ECPoint;
743 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100745 int format, size_t *olen,
746 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100747{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100748 int ret;
749
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100750 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100751 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100752 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100753 if( blen < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100757 olen, buf + 1, blen - 1) ) != 0 )
758 return( ret );
759
760 /*
761 * write length to the first byte and update total length
762 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200763 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100764 ++*olen;
765
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200766 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100767}
768
769/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100770 * Set a group from an ECParameters record (RFC 4492)
771 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100773{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200774 uint16_t tls_id;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100776
777 /*
778 * We expect at least three bytes (see below)
779 */
780 if( len < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100782
783 /*
784 * First byte is curve_type; only named_curve is handled
785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
787 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100788
789 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100790 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100791 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200792 tls_id = *(*buf)++;
793 tls_id <<= 8;
794 tls_id |= *(*buf)++;
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
797 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200798
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200799 return mbedtls_ecp_group_load( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100800}
801
802/*
803 * Write the ECParameters record corresponding to a group (RFC 4492)
804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100806 unsigned char *buf, size_t blen )
807{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
811 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200812
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100813 /*
814 * We are going to write 3 bytes (see below)
815 */
816 *olen = 3;
817 if( blen < *olen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100819
820 /*
821 * First byte is curve_type, always named_curve
822 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100824
825 /*
826 * Next two bytes are the namedcurve value
827 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200828 buf[0] = curve_info->tls_id >> 8;
829 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100830
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200831 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100832}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100833
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200834/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
836 * See the documentation of struct mbedtls_ecp_group.
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200837 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200841{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200842 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200843
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200844 if( grp->modp == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200846
847 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200849 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200852 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 MBEDTLS_MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200855
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200856 /* N->s < 0 is a much faster test, which fails only if N is 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
858 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200861 /* we known P, N and the result are positive */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200863
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200864cleanup:
865 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200866}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200867
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100868/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100869 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100870 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100871 * In order to guarantee that, we need to ensure that operands of
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100873 * bring the result back to this range.
874 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100875 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100876 */
877
878/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100882#define INC_MUL_COUNT mul_count++;
883#else
884#define INC_MUL_COUNT
885#endif
886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100888 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100889
890/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200892 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100893 */
894#define MOD_SUB( N ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
896 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100897
898/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200900 * We known P, N and the result are positive, so sub_abs is correct, and
901 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100902 */
903#define MOD_ADD( N ) \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
905 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100906
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +0200907#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100908/*
909 * For curves in short Weierstrass form, we do all the internal operations in
910 * Jacobian coordinates.
911 *
912 * For multiplication, we'll use a comb method with coutermeasueres against
913 * SPA, hence timing attacks.
914 */
915
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100916/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100917 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100918 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100919 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100921{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100922 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100926 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100927
Janos Follathb0697532016-08-18 12:38:46 +0100928#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +0200929 if( mbedtls_internal_ecp_grp_capable( grp ) )
930 return( mbedtls_internal_ecp_normalize_jac( grp, pt ) );
Janos Follath372697b2016-10-28 16:53:11 +0100931#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +0200932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100934
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100935 /*
936 * X = X / Z^2 mod p
937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
939 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
940 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100941
942 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100943 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
946 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100947
948 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100949 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100952
953cleanup:
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100956
957 return( ret );
958}
959
960/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100961 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100962 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100963 * (See for example Cohen's "A Course in Computational Algebraic Number
964 * Theory", Algorithm 10.3.4.)
965 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200966 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100967 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100968 *
969 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100970 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +0200972 mbedtls_ecp_point *T[], size_t T_size )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100973{
974 int ret;
975 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_mpi *c, u, Zi, ZZi;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100977
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +0200978 if( T_size < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100979 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100980
Janos Follathb0697532016-08-18 12:38:46 +0100981#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +0200982 if( mbedtls_internal_ecp_grp_capable( grp ) )
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +0200983 return( mbedtls_internal_ecp_normalize_jac_many( grp, T, T_size ) );
Janos Follathb0697532016-08-18 12:38:46 +0100984#endif
985
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +0200986 if( ( c = mbedtls_calloc( T_size, sizeof( mbedtls_mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200987 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100990
991 /*
992 * c[i] = Z_0 * ... * Z_i
993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +0200995 for( i = 1; i < T_size; i++ )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100998 MOD_MUL( c[i] );
999 }
1000
1001 /*
1002 * u = 1 / (Z_0 * ... * Z_n) mod P
1003 */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001004 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[T_size-1], &grp->P ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001005
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001006 for( i = T_size - 1; ; i-- )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001007 {
1008 /*
1009 * Zi = 1 / Z_i mod p
1010 * u = 1 / (Z_0 * ... * Z_i) mod P
1011 */
1012 if( i == 0 ) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001014 }
1015 else
1016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
1018 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001019 }
1020
1021 /*
1022 * proceed as in normalize()
1023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
1025 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
1026 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
1027 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +01001028
1029 /*
1030 * Post-precessing: reclaim some memory by shrinking coordinates
1031 * - not storing Z (always 1)
1032 * - shrinking other coordinates, but still keeping the same number of
1033 * limbs as P, as otherwise it will too likely be regrown too fast.
1034 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
1036 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
1037 mbedtls_mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001038
1039 if( i == 0 )
1040 break;
1041 }
1042
1043cleanup:
1044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001046 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_mpi_free( &c[i] );
1048 mbedtls_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001049
1050 return( ret );
1051}
1052
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +01001053/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001054 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
1055 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
1056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
1058 mbedtls_ecp_point *Q,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001059 unsigned char inv )
1060{
1061 int ret;
1062 unsigned char nonzero;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_mpi mQY;
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_mpi_init( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001066
1067 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
1069 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
1070 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001071
1072cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 mbedtls_mpi_free( &mQY );
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001074
1075 return( ret );
1076}
1077
1078/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001079 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +02001080 *
Peter Dettmance661b22015-02-07 14:43:51 +07001081 * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001082 *
Peter Dettmance661b22015-02-07 14:43:51 +07001083 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
1084 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
1085 *
1086 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
1087 *
1088 * Cost: 1D := 3M + 4S (A == 0)
1089 * 4M + 4S (A == -3)
1090 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1093 const mbedtls_ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001094{
1095 int ret;
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001096 mbedtls_mpi M, S, T, U;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001099 dbl_count++;
1100#endif
1101
Janos Follathb0697532016-08-18 12:38:46 +01001102#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001103 if( mbedtls_internal_ecp_grp_capable( grp ) )
1104 return( mbedtls_internal_ecp_double_jac( grp, R, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001105#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001106
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001107 mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001108
1109 /* Special case for A = -3 */
1110 if( grp->A.p == NULL )
1111 {
Peter Dettmance661b22015-02-07 14:43:51 +07001112 /* M = 3(X + Z^2)(X - Z^2) */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001113 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1114 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
1115 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
1116 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
1117 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001118 }
1119 else
Peter Vaskovica676acf2014-08-06 00:48:39 +02001120 {
Peter Dettmance661b22015-02-07 14:43:51 +07001121 /* M = 3.X^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001122 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
1123 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001124
1125 /* Optimize away for "koblitz" curves with A = 0 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001126 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
Peter Dettmance661b22015-02-07 14:43:51 +07001127 {
1128 /* M += A.Z^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001129 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
1130 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
1131 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
1132 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
Peter Dettmance661b22015-02-07 14:43:51 +07001133 }
Peter Vaskovica676acf2014-08-06 00:48:39 +02001134 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001135
Peter Dettmance661b22015-02-07 14:43:51 +07001136 /* S = 4.X.Y^2 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001137 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
1138 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
1139 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
1140 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001141
Peter Dettmance661b22015-02-07 14:43:51 +07001142 /* U = 8.Y^4 */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001143 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
1144 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001145
1146 /* T = M^2 - 2.S */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001147 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
1148 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
1149 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
Peter Dettmance661b22015-02-07 14:43:51 +07001150
1151 /* S = M(S - T) - U */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001152 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
1153 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
1154 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
Peter Dettmance661b22015-02-07 14:43:51 +07001155
1156 /* U = 2.Y.Z */
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001157 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
1158 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
Peter Dettmance661b22015-02-07 14:43:51 +07001159
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001160 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
1161 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
1162 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001163
1164cleanup:
Manuel Pégourié-Gonnard2088ba62015-05-12 10:36:26 +02001165 mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +02001166
1167 return( ret );
1168}
1169
1170/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001171 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001172 *
1173 * The coordinates of Q must be normalized (= affine),
1174 * but those of P don't need to. R is not normalized.
1175 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001176 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001177 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001178 * - at each step, P, Q and R are multiples of the base point, the factor
1179 * being less than its order, so none of them is zero;
1180 * - Q is an odd multiple of the base point, P an even multiple,
1181 * due to the choice of precomputed points in the modified comb method.
1182 * So branches for these cases do not leak secret information.
1183 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001184 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1185 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001186 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1189 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001190{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001191 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001195 add_count++;
1196#endif
1197
Janos Follathb0697532016-08-18 12:38:46 +01001198#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001199 if( mbedtls_internal_ecp_grp_capable( grp ) )
1200 return( mbedtls_internal_ecp_add_mixed( grp, R, P, Q ) );
Janos Follath372697b2016-10-28 16:53:11 +01001201#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001202
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001203 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001204 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
1207 return( mbedtls_ecp_copy( R, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
1210 return( mbedtls_ecp_copy( R, P ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001211
1212 /*
1213 * Make sure Q coordinates are normalized
1214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
1216 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
1219 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1223 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1224 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1225 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1226 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001227
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001228 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001232 {
1233 ret = ecp_double_jac( grp, R, P );
1234 goto cleanup;
1235 }
1236 else
1237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 ret = mbedtls_ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001239 goto cleanup;
1240 }
1241 }
1242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1244 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1245 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1246 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1247 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1248 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1249 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1250 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1251 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1252 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1253 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1254 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
1257 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
1258 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001259
1260cleanup:
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
1263 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001264
1265 return( ret );
1266}
1267
1268/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001269 * Randomize jacobian coordinates:
1270 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001271 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001272 *
1273 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001276 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1277{
1278 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 mbedtls_mpi l, ll;
Janos Follathb0697532016-08-18 12:38:46 +01001280 size_t p_size;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001281 int count = 0;
1282
Janos Follathb0697532016-08-18 12:38:46 +01001283#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001284 if( mbedtls_internal_ecp_grp_capable( grp ) )
1285 return( mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng ) );
Janos Follath372697b2016-10-28 16:53:11 +01001286#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001287
1288 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001290
1291 /* Generate l such that 1 < l < p */
1292 do
1293 {
Ron Eldorca6ff582017-01-12 14:50:50 +02001294 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1297 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001298
1299 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001301 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001303
1304 /* Z = l * Z */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001306
1307 /* X = l^2 * X */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1309 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001310
1311 /* Y = l^3 * Y */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1313 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001314
1315cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001317
1318 return( ret );
1319}
1320
1321/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001322 * Check and define parameters used by the comb method (see below for details)
1323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
1325#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001326#endif
1327
1328/* d = ceil( n / w ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001330
1331/* number of precomputed points */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001333
1334/*
1335 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001336 *
1337 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001338 * modified version that provides resistance to SPA by avoiding zero
1339 * digits in the representation as in [3]. We modify the method further by
1340 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001341 * representation uses one more K_i, due to carries, but saves on the size of
1342 * the precomputed table.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001343 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001344 * Summary of the comb method and its modifications:
1345 *
1346 * - The goal is to compute m*P for some w*d-bit integer m.
1347 *
1348 * - The basic comb method splits m into the w-bit integers
1349 * x[0] .. x[d-1] where x[i] consists of the bits in m whose
1350 * index has residue i modulo d, and computes m * P as
1351 * S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
1352 * S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
1353 *
1354 * - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
1355 * .. + 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]] ..,
1356 * thereby successively converting it into a form where all summands
1357 * are nonzero, at the cost of negative summands. This is the basic idea of [3].
1358 *
1359 * - More generally, even if x[i+1] != 0, we can first transform the sum as
1360 * .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
1361 * and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
1362 * Performing and iterating this procedure for those x[i] that are even
1363 * (keeping track of carry), we can transform the original sum into one of the form
1364 * S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
1365 * with all x'[i] odd. It is therefore only necessary to know S at odd indices,
1366 * which is why we are only computing half of it in the first place in
1367 * ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
1368 *
1369 * - For the sake of compactness, only the seven low-order bits of x[i]
1370 * are used to represent its absolute value (K_i in the paper), and the msb
1371 * of x[i] encodes the the sign (s_i in the paper): it is set if and only if
1372 * if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001373 *
1374 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001375 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001376 * - w is the size, ie number of teeth, of the comb, and must be between
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001378 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1379 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001380 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001381static void ecp_comb_recode_core( unsigned char x[], size_t d,
1382 unsigned char w, const mbedtls_mpi *m )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001383{
1384 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001385 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001386
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001387 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001388
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001389 /* First get the classical comb values (except for x_d = 0) */
1390 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001391 for( j = 0; j < w; j++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001393
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001394 /* Now make sure x_1 .. x_d are odd */
1395 c = 0;
1396 for( i = 1; i <= d; i++ )
1397 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001398 /* Add carry and update it */
1399 cc = x[i] & c;
1400 x[i] = x[i] ^ c;
1401 c = cc;
1402
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001403 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001404 adjust = 1 - ( x[i] & 0x01 );
1405 c |= x[i] & ( x[i-1] * adjust );
1406 x[i] = x[i] ^ ( x[i-1] * adjust );
1407 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001408 }
1409}
1410
1411/*
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001412 * Precompute points for the adapted comb method
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001413 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001414 * Assumption: T must be able to hold 2^{w - 1} elements.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001415 *
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001416 * Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
1417 * sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001418 *
1419 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001420 *
1421 * Note: Even comb values (those where P would be omitted from the
1422 * sum defining T[i] above) are not needed in our adaption
1423 * the the comb method. See ecp_comb_recode_core().
1424 *
1425 * This function currently works in four steps:
1426 * (1) Computation of intermediate T[i] for 2-powers values of i
1427 * (restart state is ecp_rsm_init).
1428 * (2) Normalization of coordinates of these T[i]
1429 * (restart state is ecp_rsm_pre_norm_dbl).
1430 * (3) Computation of all T[i] (restart state is ecp_rsm_pre_add).
1431 * (4) Normalization of all T[i] (restart state is ecp_rsm_pre_norm_add)
1432 * The final restart state is ecp_rsm_T_done.
1433 *
1434 * Step 1 can be interrupted but not the others; together with the final
1435 * coordinate normalization they are the largest steps done at once, depending
1436 * on the window size. Here are operation counts for P-256:
1437 *
1438 * step (2) (3) (4)
1439 * w = 5 142 165 208
1440 * w = 4 136 77 160
1441 * w = 3 130 33 136
1442 * w = 2 124 11 124
1443 *
1444 * So if ECC operations are blocking for too long even with a low max_ops
1445 * value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
1446 * to minimize maximum blocking time.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
1449 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001450 unsigned char w, size_t d,
1451 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001452{
1453 int ret;
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001454 unsigned char i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001455 size_t j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001456 const unsigned char T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001458
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001459#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001460 (void) rs_ctx;
1461#endif
1462
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001463#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001464 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001465 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001466 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_add )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001467 goto norm_add;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001468 if( rs_ctx->rsm->state == ecp_rsm_pre_add )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001469 goto add;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001470 if( rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl )
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001471 goto norm_dbl;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001472 }
1473#endif
1474
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001475 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001476 * Set T[0] = P and
1477 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001480
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001481#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001482 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
1483 j = rs_ctx->rsm->i;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001484 else
1485#endif
1486 j = 0;
1487
1488 for( ; j < d * ( w - 1 ); j++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001489 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001490 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL );
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001491
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001492 i = 1U << ( j / d );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001493 cur = T + i;
Manuel Pégourié-Gonnardae557072017-03-20 12:21:24 +01001494
1495 if( j % d == 0 )
1496 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
1497
1498 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001499 }
1500
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001501#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001502 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001503 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001504 rs_ctx->rsm->i = 0;
1505 rs_ctx->rsm->state++;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001506 }
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001507#endif
1508
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001509 /*
1510 * Normalize current elements in T. As T has holes,
1511 * use an auxiliary array of pointers to elements in T.
1512 */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001513#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001514norm_dbl:
1515#endif
1516
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001517 j = 0;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001518 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001519 TT[j++] = T + i;
1520
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001521 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001522
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001523 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001524
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001525#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001526 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1527 rs_ctx->rsm->state++;
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001528#endif
1529
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001530 /*
1531 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001532 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001533 */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001534#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001535add:
1536#endif
1537
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001538 MBEDTLS_ECP_BUDGET( ( T_size - 1 ) * MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001539
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001540 for( i = 1; i < T_size; i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001541 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001542 j = i;
1543 while( j-- )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001545 }
1546
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001547#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001548 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1549 rs_ctx->rsm->state++;
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001550#endif
1551
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001552 /*
1553 * Normalize final elements in T. Even though there are no holes now,
1554 * we still need the auxiliary array for homogeneity with last time.
Manuel Pégourié-Gonnard67c83fb2017-03-20 10:08:53 +01001555 * Also, skip T[0] which is already normalised, being a copy of P.
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001556 */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001557#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001558norm_add:
1559#endif
1560
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001561 for( j = 0; j + 1 < T_size; j++ )
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001562 TT[j] = T + j + 1;
1563
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001564 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV + 6 * j - 2 );
Manuel Pégourié-Gonnarde2d7cb32017-03-20 10:24:17 +01001565
Manuel Pégourié-Gonnardfc3e0be2017-03-20 09:29:31 +01001566 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, j ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001567
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001568#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001569 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1570 rs_ctx->rsm->state++;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001571#endif
1572
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001573cleanup:
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001574#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001575 if( rs_ctx != NULL && rs_ctx->rsm != NULL &&
1576 ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001577 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001578 if( rs_ctx->rsm->state == ecp_rsm_init )
1579 rs_ctx->rsm->i = j;
Manuel Pégourié-Gonnard213541a2017-03-20 12:50:41 +01001580 }
1581#endif
Janos Follathb0697532016-08-18 12:38:46 +01001582
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001583 return( ret );
1584}
1585
1586/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001587 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001588 *
1589 * See ecp_comb_recode_core() for background
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001592 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001593 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001594{
1595 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001596 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001597
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001598 /* Ignore the "sign" bit and scale down */
1599 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001600
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001601 /* Read the whole table to thwart cache-based timing attacks */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001602 for( j = 0; j < T_size; j++ )
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1605 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001606 }
1607
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001608 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001610
1611cleanup:
1612 return( ret );
1613}
1614
1615/*
1616 * Core multiplication algorithm for the (modified) comb method.
1617 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001618 *
1619 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001622 const mbedtls_ecp_point T[], unsigned char T_size,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001623 const unsigned char x[], size_t d,
1624 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001625 void *p_rng,
1626 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001627{
1628 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 mbedtls_ecp_point Txi;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001630 size_t i;
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 mbedtls_ecp_point_init( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001633
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001634#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001635 (void) rs_ctx;
1636#endif
1637
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001638#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001639 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0 )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001640 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001641 /* restore current index (R already pointing to rs_ctx->rsm->R) */
1642 i = rs_ctx->rsm->i;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001643 }
1644 else
1645#endif
1646 {
1647 /* Start with a non-zero point and randomize its coordinates */
1648 i = d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001649 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, T_size, x[i] ) );
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001650 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
1651 if( f_rng != 0 )
1652 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
1653 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001654
1655 while( i-- != 0 )
1656 {
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001657 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001659 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, T_size, x[i] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001661 }
1662
1663cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_ecp_point_free( &Txi );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001666
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001667#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001668 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001669 {
1670 if( ret == 0 )
1671 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001672 rs_ctx->rsm->state++;
1673 rs_ctx->rsm->i = 0;
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001674 }
1675 else if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
1676 {
1677 /* was decreased before actually doing it */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001678 rs_ctx->rsm->i = i + 1;
1679 /* no need to save R, already pointing to rs_ctx->rsm->R */
Manuel Pégourié-Gonnardc5d844b2017-03-15 13:06:28 +01001680 }
1681 }
1682#endif
1683
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001684 return( ret );
1685}
1686
1687/*
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001688 * Recode the scalar to get constant-time comb multiplication
1689 *
1690 * As the actual scalar recoding needs an odd scalar as a starting point,
1691 * this wrapper ensures that by replacing m by N - m if necessary, and
1692 * informs the caller that the result of multiplication will be negated.
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001693 *
1694 * See ecp_comb_recode_core() for background.
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001695 */
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001696static int ecp_comb_recode_scalar( const mbedtls_ecp_group *grp,
1697 const mbedtls_mpi *m,
1698 unsigned char k[COMB_MAX_D + 1],
1699 size_t d,
1700 unsigned char w,
1701 unsigned char *parity_trick )
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001702{
1703 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001704 mbedtls_mpi M, mm;
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001705
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001706 mbedtls_mpi_init( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001707 mbedtls_mpi_init( &mm );
1708
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001709 /* N is odd with all real-world curves, just make extra sure */
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001710 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
1711 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
1712
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001713 /* do we need the parity trick? */
1714 *parity_trick = ( mbedtls_mpi_get_bit( m, 0 ) == 0 );
1715
1716 /* execute parity fix in constant time */
1717 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001718 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001719 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, *parity_trick ) );
1720
1721 /* actual scalar recoding */
1722 ecp_comb_recode_core( k, d, w, &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001723
1724cleanup:
1725 mbedtls_mpi_free( &mm );
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001726 mbedtls_mpi_free( &M );
Manuel Pégourié-Gonnardec5606a2017-03-09 12:46:45 +01001727
1728 return( ret );
1729}
1730
1731/*
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001732 * Perform comb multiplication (for short Weierstrass curves)
1733 * once the auxiliary table has been pre-computed.
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001734 *
1735 * Scalar recoding may use a parity trick that makes us compute -m * P,
1736 * if that is the case we'll need to recover m * P at the end.
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001737 */
1738static int ecp_mul_comb_after_precomp( const mbedtls_ecp_group *grp,
1739 mbedtls_ecp_point *R,
1740 const mbedtls_mpi *m,
1741 const mbedtls_ecp_point *T,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001742 unsigned char T_size,
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001743 unsigned char w,
1744 size_t d,
1745 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001746 void *p_rng,
1747 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001748{
1749 int ret;
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001750 unsigned char parity_trick;
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001751 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001752 mbedtls_ecp_point *RR = R;
1753
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001754#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001755 (void) rs_ctx;
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001756#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001757
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001758#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001759 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1760 RR = &rs_ctx->rsm->R;
1761#endif
1762
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001763#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001764 if( rs_ctx == NULL || rs_ctx->rsm == NULL ||
1765 rs_ctx->rsm->state < ecp_rsm_final_norm )
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01001766#endif
1767 {
1768 MBEDTLS_MPI_CHK( ecp_comb_recode_scalar( grp, m, k, d, w,
1769 &parity_trick ) );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001770 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, RR, T, T_size, k, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001771 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01001772 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, RR, parity_trick ) );
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001773
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001774#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001775 if( rs_ctx != NULL && rs_ctx->rsm != NULL )
1776 rs_ctx->rsm->state++;
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01001777#endif
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01001778 }
Manuel Pégourié-Gonnard62738e92017-03-14 10:00:21 +01001779
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02001780 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001781 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, RR ) );
1782
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001783#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8962ddb2017-03-14 12:11:21 +01001784 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, RR ) );
1785#endif
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001786
1787cleanup:
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001788 return( ret );
1789}
1790
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001791/*
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01001792 * Pick window size based on curve size and whether we optimize for base point
1793 */
1794static unsigned char ecp_pick_window_size( const mbedtls_ecp_group *grp,
1795 unsigned char p_eq_g )
1796{
1797 unsigned char w;
1798
1799 /*
1800 * Minimize the number of multiplications, that is minimize
1801 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
1802 * (see costs of the various parts, with 1S = 1M)
1803 */
1804 w = grp->nbits >= 384 ? 5 : 4;
1805
1806 /*
1807 * If P == G, pre-compute a bit more, since this may be re-used later.
1808 * Just adding one avoids upping the cost of the first mul too much,
1809 * and the memory cost too.
1810 */
1811 if( p_eq_g )
1812 w++;
1813
1814 /*
1815 * Make sure w is within bounds.
1816 * (The last test is useful only for very small curves in the test suite.)
1817 */
1818 if( w > MBEDTLS_ECP_WINDOW_SIZE )
1819 w = MBEDTLS_ECP_WINDOW_SIZE;
1820 if( w >= grp->nbits )
1821 w = 2;
1822
1823 return( w );
1824}
1825
1826/*
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01001827 * Multiplication using the comb method - for curves in short Weierstrass form
1828 *
1829 * This function is mainly responsible for administrative work:
1830 * - managing the restart context if enabled
1831 * - managing the table of precomputed points (passed between the above two
1832 * functions): allocation, computation, ownership tranfer, freeing.
1833 *
1834 * It delegates the actual arithmetic work to:
1835 * ecp_precompute_comb() and ecp_mul_comb_with_precomp()
1836 *
1837 * See comments on ecp_comb_recode_core() regarding the computation strategy.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1840 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001841 int (*f_rng)(void *, unsigned char *, size_t),
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001842 void *p_rng,
1843 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001844{
1845 int ret;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001846 unsigned char w, p_eq_g = 0, i;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001847 size_t d;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001848 unsigned char T_size = 0, T_ok = 0;
Manuel Pégourié-Gonnard7306dff2017-03-09 12:23:45 +01001849 mbedtls_ecp_point *T = NULL;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001850
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001851#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001852 (void) rs_ctx;
1853#endif
1854
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001855#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard646393b2017-04-20 10:03:45 +02001856 /* set up our own sub-context if needed */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001857 if( ecp_max_ops != 0 && rs_ctx != NULL && rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01001858 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001859 rs_ctx->rsm = mbedtls_calloc( 1, sizeof( mbedtls_ecp_restart_mul_ctx ) );
1860 if( rs_ctx->rsm == NULL )
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01001861 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
Manuel Pégourié-Gonnard78d564a2017-03-14 11:48:38 +01001862
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001863 ecp_restart_mul_init( rs_ctx->rsm );
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01001864 }
Manuel Pégourié-Gonnard510d5ca2017-03-08 11:41:47 +01001865#endif
1866
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01001867 /* Is P the base point ? */
1868#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
1869 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1870 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard22be6352017-03-09 13:02:35 +01001871#endif
1872
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001873 /* Pick window size and deduce related sizes */
Manuel Pégourié-Gonnard4b2336d2017-03-09 13:23:50 +01001874 w = ecp_pick_window_size( grp, p_eq_g );
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001875 T_size = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001876 d = ( grp->nbits + w - 1 ) / w;
1877
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001878 /* Pre-computed table: do we have it already for the base point? */
1879 if( p_eq_g && grp->T != NULL )
1880 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001881 /* second pointer to the same table, will be deleted on exit */
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001882 T = grp->T;
1883 T_ok = 1;
1884 }
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001885
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001886#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001887 /* Pre-computed table: do we have one in progress? complete? */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001888 if( rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL && T == NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001889 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01001890 /* transfer ownership of T from rsm to local function */
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001891 T = rs_ctx->rsm->T;
1892 rs_ctx->rsm->T = NULL;
1893 rs_ctx->rsm->T_size = 0;
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001894
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001895 if( rs_ctx->rsm->state >= ecp_rsm_T_done )
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001896 T_ok = 1;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001897 }
1898#endif
1899
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001900 /* Allocate table if we didn't have any */
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001901 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001902 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001903 T = mbedtls_calloc( T_size, sizeof( mbedtls_ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001904 if( T == NULL )
1905 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001906 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001907 goto cleanup;
1908 }
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001909 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001910
Manuel Pégourié-Gonnard085b1df2017-03-16 16:56:04 +01001911 /* Compute table (or finish computing it) if not done already */
1912 if( !T_ok )
1913 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001914 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001915
1916 if( p_eq_g )
1917 {
Manuel Pégourié-Gonnard7037e222017-08-23 14:30:36 +02001918 /* almost transfer ownership of T to the group, but keep a copy of
1919 * the pointer to use for caling the next function more easily */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001920 grp->T = T;
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001921 grp->T_size = T_size;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001922 }
1923 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001924
Manuel Pégourié-Gonnard391f4412017-03-13 12:26:21 +01001925 /* Actual comb multiplication using precomputed points */
1926 MBEDTLS_MPI_CHK( ecp_mul_comb_after_precomp( grp, R, m,
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001927 T, T_size, w, d,
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001928 f_rng, p_rng, rs_ctx ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001929
1930cleanup:
1931
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01001932 /* does T belong to the group? */
1933 if( T == grp->T )
1934 T = NULL;
1935
1936 /* does T belong to the restart context? */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001937#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001938 if( rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL )
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001939 {
Manuel Pégourié-Gonnard45fd0162017-03-22 08:24:42 +01001940 /* transfer ownership of T from local function to rsm */
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001941 rs_ctx->rsm->T_size = T_size;
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001942 rs_ctx->rsm->T = T;
Manuel Pégourié-Gonnardc9c0aa62017-03-16 14:53:26 +01001943 T = NULL;
1944 }
1945#endif
1946
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01001947 /* did T belong to us? then let's destroy it! */
1948 if( T != NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001949 {
Manuel Pégourié-Gonnard92cceb22017-08-23 16:27:29 +02001950 for( i = 0; i < T_size; i++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_ecp_point_free( &T[i] );
1952 mbedtls_free( T );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001953 }
1954
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01001955 /* don't free R while in progress in case R == P */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001956#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard2fad7ae2017-03-14 13:13:13 +01001957 if( ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1958#endif
Manuel Pégourié-Gonnard07bf6f52017-03-16 17:21:38 +01001959 /* prevent caller from using invalid value */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001960 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001962
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02001963 /* clear our sub-context when not in progress (done or error) */
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02001964#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +02001965 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
1966 {
Manuel Pégourié-Gonnard3cade222017-04-20 09:31:00 +02001967 ecp_restart_mul_free( rs_ctx->rsm );
1968 mbedtls_free( rs_ctx->rsm );
1969 rs_ctx->rsm = NULL;
Manuel Pégourié-Gonnard77af79a2017-03-14 10:58:00 +01001970 }
1971#endif
1972
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001973 return( ret );
1974}
1975
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001976#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001977
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001978#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001979/*
1980 * For Montgomery curves, we do all the internal arithmetic in projective
1981 * coordinates. Import/export of points uses only the x coordinates, which is
1982 * internaly represented as X / Z.
1983 *
1984 * For scalar multiplication, we'll use a Montgomery ladder.
1985 */
1986
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001987/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001988 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1989 * Cost: 1M + 1I
1990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001992{
1993 int ret;
1994
Janos Follathb0697532016-08-18 12:38:46 +01001995#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02001996 if( mbedtls_internal_ecp_grp_capable( grp ) )
1997 return( mbedtls_internal_ecp_normalize_mxz( grp, P ) );
Janos Follath372697b2016-10-28 16:53:11 +01001998#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
2001 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
2002 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002003
2004cleanup:
2005 return( ret );
2006}
2007
2008/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002009 * Randomize projective x/z coordinates:
2010 * (X, Z) -> (l X, l Z) for random l
2011 * This is sort of the reverse operation of ecp_normalize_mxz().
2012 *
2013 * This countermeasure was first suggested in [2].
2014 * Cost: 2M
2015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002017 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2018{
2019 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002020 mbedtls_mpi l;
Janos Follathb0697532016-08-18 12:38:46 +01002021 size_t p_size;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002022 int count = 0;
2023
Janos Follathb0697532016-08-18 12:38:46 +01002024#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002025 if( mbedtls_internal_ecp_grp_capable( grp ) )
2026 return( mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Janos Follath372697b2016-10-28 16:53:11 +01002027#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002028
2029 p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 mbedtls_mpi_init( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002031
2032 /* Generate l such that 1 < l < p */
2033 do
2034 {
Ron Eldorca6ff582017-01-12 14:50:50 +02002035 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
2038 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002039
2040 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
2046 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002047
2048cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 mbedtls_mpi_free( &l );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002050
2051 return( ret );
2052}
2053
2054/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002055 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
2056 * for Montgomery curves in x/z coordinates.
2057 *
2058 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
2059 * with
2060 * d = X1
2061 * P = (X2, Z2)
2062 * Q = (X3, Z3)
2063 * R = (X4, Z4)
2064 * S = (X5, Z5)
2065 * and eliminating temporary variables tO, ..., t4.
2066 *
2067 * Cost: 5M + 4S
2068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002069static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
2070 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
2071 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
2072 const mbedtls_mpi *d )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002073{
2074 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002076
Janos Follathb0697532016-08-18 12:38:46 +01002077#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002078 if( mbedtls_internal_ecp_grp_capable( grp ) )
2079 return( mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d ) );
Janos Follath372697b2016-10-28 16:53:11 +01002080#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Janos Follathb0697532016-08-18 12:38:46 +01002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
2083 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
2084 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
2087 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
2088 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
2089 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
2090 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
2091 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
2092 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
2093 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
2094 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
2095 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
2096 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
2097 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
2098 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
2099 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
2100 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
2101 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
2102 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
2103 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002104
2105cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
2107 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
2108 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002109
2110 return( ret );
2111}
2112
2113/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002114 * Multiplication with Montgomery ladder in x/z coordinates,
2115 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2118 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002119 int (*f_rng)(void *, unsigned char *, size_t),
2120 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002121{
2122 int ret;
2123 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002124 unsigned char b;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 mbedtls_ecp_point RP;
2126 mbedtls_mpi PX;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002129
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002130 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
2132 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002133
2134 /* Set R to zero in modified x/z coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002135 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
2136 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
2137 mbedtls_mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002138
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01002139 /* RP.X might be sligtly larger than P, so reduce it */
2140 MOD_ADD( RP.X );
2141
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01002142 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01002143 if( f_rng != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002145
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002146 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002147 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002148 while( i-- > 0 )
2149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 b = mbedtls_mpi_get_bit( m, i );
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01002151 /*
2152 * if (b) R = 2R + P else R = 2R,
2153 * which is:
2154 * if (b) double_add( RP, R, RP, R )
2155 * else double_add( R, RP, R, RP )
2156 * but using safe conditional swaps to avoid leaks
2157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2159 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
2160 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
2161 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
2162 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002163 }
2164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002166
2167cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002169
2170 return( ret );
2171}
2172
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002173#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01002174
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01002175/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002176 * Restartable multiplication R = m * P
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002177 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002178int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002180 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
2181 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002182{
Janos Follathb0697532016-08-18 12:38:46 +01002183 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Janos Follathc44ab972016-11-18 16:38:23 +00002184#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2185 char is_grp_capable = 0;
2186#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002187
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002188#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002189 /* reset ops count for this call if top-level */
2190 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2191 rs_ctx->ops_done = 0;
2192#endif
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002193
Janos Follathc44ab972016-11-18 16:38:23 +00002194#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002195 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Janos Follathc44ab972016-11-18 16:38:23 +00002196 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Janos Follathc44ab972016-11-18 16:38:23 +00002197#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002198
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002199#if defined(MBEDTLS_ECP_RESTARTABLE) && defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002200 /* skip argument check when restarting */
2201 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS ||
2202 rs_ctx == NULL || rs_ctx->rsm == NULL )
2203#endif
2204 {
Manuel Pégourié-Gonnard5314f232017-04-21 12:36:59 +02002205 /* check_privkey is free */
2206 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_CHK );
2207
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002208 /* Common sanity checks */
2209 MBEDTLS_MPI_CHK( mbedtls_ecp_check_privkey( grp, m ) );
2210 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, P ) );
Manuel Pégourié-Gonnarda08cd1a2017-04-20 11:29:43 +02002211 }
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002212
2213 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002214#if defined(ECP_MONTGOMERY)
2215 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002216 MBEDTLS_MPI_CHK( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Janos Follath430d3372016-11-03 14:25:37 +00002217#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002218#if defined(ECP_SHORTWEIERSTRASS)
2219 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002220 MBEDTLS_MPI_CHK( ecp_mul_comb( grp, R, m, P, f_rng, p_rng, rs_ctx ) );
Janos Follath430d3372016-11-03 14:25:37 +00002221#endif
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002222
Janos Follath6c8ccd52016-11-29 15:37:09 +00002223cleanup:
Janos Follathb0697532016-08-18 12:38:46 +01002224
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002225#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002226 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002227 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002228#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002229
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002230#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3a256122017-04-20 11:20:26 +02002231 if( rs_ctx != NULL )
2232 rs_ctx->depth--;
2233#endif
2234
Janos Follathb0697532016-08-18 12:38:46 +01002235 return( ret );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002236}
2237
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002238/*
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002239 * Multiplication R = m * P
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002240 */
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002241int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002242 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002243 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002244{
Manuel Pégourié-Gonnard884569c2017-04-20 10:10:59 +02002245 return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) );
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002246}
Manuel Pégourié-Gonnardb739a712017-04-19 10:11:56 +02002247
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002248#if defined(ECP_SHORTWEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01002249/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002250 * Check that an affine point is valid as a public key,
2251 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002254{
2255 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_mpi YY, RHS;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002257
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002258 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002259 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
2260 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
2261 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
2262 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
2263 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002266
2267 /*
2268 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02002269 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
2272 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002273
2274 /* Special case for A = -3 */
2275 if( grp->A.p == NULL )
2276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002278 }
2279 else
2280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01002282 }
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
2285 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
2288 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002289
2290cleanup:
2291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002293
2294 return( ret );
2295}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002296#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002297
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002298/*
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002299 * R = m * P with shortcuts for m == 1 and m == -1
2300 * NOT constant-time - ONLY for short Weierstrass!
2301 */
2302static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
2303 mbedtls_ecp_point *R,
2304 const mbedtls_mpi *m,
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002305 const mbedtls_ecp_point *P,
2306 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002307{
2308 int ret;
2309
2310 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
2311 {
2312 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2313 }
2314 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
2315 {
2316 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
2317 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
2318 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
2319 }
2320 else
2321 {
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002322 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, R, m, P,
2323 NULL, NULL, rs_ctx ) );
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002324 }
2325
2326cleanup:
2327 return( ret );
2328}
2329
2330/*
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002331 * Restartable linear combination
Manuel Pégourié-Gonnardde9f9532015-10-23 15:50:37 +02002332 * NOT constant-time
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002333 */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002334int mbedtls_ecp_muladd_restartable(
2335 mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002336 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002337 const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
2338 mbedtls_ecp_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002339{
2340 int ret;
2341 mbedtls_ecp_point mP;
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002342 mbedtls_ecp_point *pmP = &mP;
2343 mbedtls_ecp_point *pR = R;
Janos Follathc44ab972016-11-18 16:38:23 +00002344#if defined(MBEDTLS_ECP_INTERNAL_ALT)
2345 char is_grp_capable = 0;
2346#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002347
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002348#if !defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002349 (void) rs_ctx;
2350#endif
2351
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002352 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
2353 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
2354
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002355 mbedtls_ecp_point_init( &mP );
2356
2357#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002358 if( ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) ) )
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002359 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002360#endif /* MBEDTLS_ECP_INTERNAL_ALT */
2361
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002362#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002363 /* reset ops count for this call if top-level */
2364 if( rs_ctx != NULL && rs_ctx->depth++ == 0 )
2365 rs_ctx->ops_done = 0;
2366
2367 /* set up our own sub-context if needed */
2368 if( ecp_max_ops != 0 && rs_ctx != NULL && rs_ctx->ma == NULL )
2369 {
2370 rs_ctx->ma = mbedtls_calloc( 1, sizeof( mbedtls_ecp_restart_muladd_ctx ) );
2371 if( rs_ctx->ma == NULL )
2372 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
2373
2374 ecp_restart_muladd_init( rs_ctx->ma );
2375 }
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002376
2377 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2378 {
2379 /* redirect intermediate results to restart context */
2380 pmP = &rs_ctx->ma->mP;
2381 pR = &rs_ctx->ma->R;
2382
2383 /* jump to next operation */
2384 if( rs_ctx->ma->state == ecp_rsma_mul2 )
2385 goto mul2;
2386 if( rs_ctx->ma->state == ecp_rsma_add )
2387 goto add;
2388 if( rs_ctx->ma->state == ecp_rsma_norm )
2389 goto norm;
2390 }
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002391#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002392
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002393 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pmP, m, P, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002394#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002395 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2396 rs_ctx->ma->state++;
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002397
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002398mul2:
2399#endif
2400 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, pR, n, Q, rs_ctx ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002401#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002402 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2403 rs_ctx->ma->state++;
Manuel Pégourié-Gonnard1a7c5ef2015-08-13 10:19:09 +02002404
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002405add:
2406#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002407 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_ADD );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002408 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, pR, pmP, pR ) );
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002409#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002410 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2411 rs_ctx->ma->state++;
Janos Follath430d3372016-11-03 14:25:37 +00002412
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002413norm:
2414#endif
Manuel Pégourié-Gonnardc7511482017-04-20 16:31:00 +02002415 MBEDTLS_ECP_BUDGET( MBEDTLS_ECP_OPS_INV );
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002416 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, pR ) );
2417
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002418#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002419 if( rs_ctx != NULL && rs_ctx->ma != NULL )
2420 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, pR ) );
2421#endif
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002422
2423cleanup:
Janos Follathc44ab972016-11-18 16:38:23 +00002424#if defined(MBEDTLS_ECP_INTERNAL_ALT)
Manuel Pégourié-Gonnardebac5d32017-08-23 16:23:36 +02002425 if( is_grp_capable )
Janos Follathc44ab972016-11-18 16:38:23 +00002426 mbedtls_internal_ecp_free( grp );
Janos Follathc44ab972016-11-18 16:38:23 +00002427#endif /* MBEDTLS_ECP_INTERNAL_ALT */
Manuel Pégourié-Gonnard1631d632017-04-20 14:48:56 +02002428
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002429 mbedtls_ecp_point_free( &mP );
2430
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002431#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002432 /* clear our sub-context when not in progress (done or error) */
Manuel Pégourié-Gonnard31f0ef72017-05-17 10:05:58 +02002433 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
2434 {
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002435 ecp_restart_muladd_free( rs_ctx->ma );
2436 mbedtls_free( rs_ctx->ma );
2437 rs_ctx->ma = NULL;
2438 }
2439
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002440 if( rs_ctx != NULL )
2441 rs_ctx->depth--;
Manuel Pégourié-Gonnard4b9c51e2017-04-20 15:50:26 +02002442#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002443
Manuel Pégourié-Gonnard56cc88a2015-05-11 18:40:45 +02002444 return( ret );
2445}
2446
Manuel Pégourié-Gonnard54dd6522017-04-20 13:36:18 +02002447/*
2448 * Linear combination
2449 * NOT constant-time
2450 */
2451int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
2452 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
2453 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
2454{
2455 return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) );
2456}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002457
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002458#if defined(ECP_MONTGOMERY)
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002459/*
2460 * Check validity of a public key for Montgomery curves with x-only schemes
2461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002463{
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002464 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
2466 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002467
2468 return( 0 );
2469}
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002470#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002471
2472/*
2473 * Check that a point is valid as a public key
2474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002476{
2477 /* Must use affine coordinates */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
2479 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002480
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002481#if defined(ECP_MONTGOMERY)
2482 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002483 return( ecp_check_pubkey_mx( grp, pt ) );
2484#endif
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002485#if defined(ECP_SHORTWEIERSTRASS)
2486 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002487 return( ecp_check_pubkey_sw( grp, pt ) );
2488#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002490}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002491
2492/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493 * Check that an mbedtls_mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002496{
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002497#if defined(ECP_MONTGOMERY)
2498 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002499 {
Manuel Pégourié-Gonnard07894332015-06-23 00:18:41 +02002500 /* see [Curve25519] page 5 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
2502 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
2503 mbedtls_mpi_get_bit( d, 2 ) != 0 ||
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002504 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002506 else
2507 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002508 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002509#endif /* ECP_MONTGOMERY */
2510#if defined(ECP_SHORTWEIERSTRASS)
2511 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002512 {
2513 /* see SEC1 3.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2515 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
2516 return( MBEDTLS_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002517 else
2518 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01002519 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002520#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02002523}
2524
2525/*
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002526 * Generate a private key
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002527 */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002528int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
2529 mbedtls_mpi *d,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002530 int (*f_rng)(void *, unsigned char *, size_t),
2531 void *p_rng )
2532{
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002533 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Paul Bakker66d5d072014-06-17 16:39:18 +02002534 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002535
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002536#if defined(ECP_MONTGOMERY)
2537 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002538 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002539 /* [M225] page 5 */
2540 size_t b;
2541
Janos Follath98e28a72016-05-31 14:03:54 +01002542 do {
2543 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
2544 } while( mbedtls_mpi_bitlen( d ) == 0);
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002545
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002546 /* Make sure the most significant bit is nbits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02002547 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002548 if( b > grp->nbits )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002550 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002552
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002553 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
2555 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
2556 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002557 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002558#endif /* ECP_MONTGOMERY */
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002559
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002560#if defined(ECP_SHORTWEIERSTRASS)
2561 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002562 {
2563 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01002564 int count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002565 unsigned char rnd[MBEDTLS_ECP_MAX_BYTES];
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01002566
2567 /*
2568 * Match the procedure given in RFC 6979 (deterministic ECDSA):
2569 * - use the same byte ordering;
2570 * - keep the leftmost nbits bits of the generated octet string;
2571 * - try until result is in the desired range.
2572 * This also avoids any biais, which is especially important for ECDSA.
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02002573 *
2574 * Each try has at worst a probability 1/2 of failing (the msb has
2575 * a probability 1/2 of being 0, and then the result will be < N),
2576 * so after 30 tries failure probability is a most 2**(-30).
2577 *
2578 * For most curves, 1 try is enough with overwhelming probability,
2579 * since N starts with a lot of 1s in binary, but some curves
2580 * such as secp224k1 are actually very close to the worst case.
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01002581 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002582 do
2583 {
Manuel Pégourié-Gonnard67543962017-04-21 13:19:43 +02002584 if( ++count > 30 )
2585 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
2586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587 MBEDTLS_MPI_CHK( f_rng( p_rng, rnd, n_size ) );
2588 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( d, rnd, n_size ) );
2589 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002590 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
2592 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01002593 }
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02002594#endif /* ECP_SHORTWEIERSTRASS */
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002595
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002596cleanup:
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002597 return( ret );
2598}
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01002599
Manuel Pégourié-Gonnarda7937f92017-04-20 15:37:46 +02002600/*
2601 * Generate a keypair with configurable base point
2602 */
2603int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
2604 const mbedtls_ecp_point *G,
2605 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2606 int (*f_rng)(void *, unsigned char *, size_t),
2607 void *p_rng )
2608{
2609 int ret;
2610
2611 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) );
2612 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
2613
2614cleanup:
2615 return( ret );
Manuel Pégourié-Gonnardd9a3f472015-08-11 14:31:03 +02002616}
2617
2618/*
2619 * Generate key pair, wrapper for conventional base point
2620 */
2621int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
2622 mbedtls_mpi *d, mbedtls_ecp_point *Q,
2623 int (*f_rng)(void *, unsigned char *, size_t),
2624 void *p_rng )
2625{
2626 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01002627}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01002628
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002629/*
2630 * Generate a keypair, prettier wrapper
2631 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002633 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
2634{
2635 int ret;
2636
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02002637 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002638 return( ret );
2639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01002641}
2642
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002643/*
2644 * Check a public-private key pair
2645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002646int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002647{
2648 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002649 mbedtls_ecp_point Q;
2650 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002652 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002653 pub->grp.id != prv->grp.id ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
2655 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
2656 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002658 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002659 }
2660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002661 mbedtls_ecp_point_init( &Q );
2662 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002664 /* mbedtls_ecp_mul() needs a non-const group... */
2665 mbedtls_ecp_group_copy( &grp, &prv->grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002666
2667 /* Also checks d is valid */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002668 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002670 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
2671 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
2672 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002674 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002675 goto cleanup;
2676 }
2677
2678cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002679 mbedtls_ecp_point_free( &Q );
2680 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01002681
2682 return( ret );
2683}
2684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002685#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002686
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01002687/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002688 * Checkup routine
2689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690int mbedtls_ecp_self_test( int verbose )
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002691{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002692 int ret;
2693 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 mbedtls_ecp_group grp;
2695 mbedtls_ecp_point R, P;
2696 mbedtls_mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002697 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002698 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02002699 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002700 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002701 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002702 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01002703 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01002704 "400000000000000000000000000000000000000000000000", /* one and zeros */
2705 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
2706 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002707 };
2708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709 mbedtls_ecp_group_init( &grp );
2710 mbedtls_ecp_point_init( &R );
2711 mbedtls_ecp_point_init( &P );
2712 mbedtls_mpi_init( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002713
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002714 /* Use secp192r1 if available, or any available curve */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02002716 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02002717#else
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +02002718 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02002719#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002720
2721 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002722 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002723
2724 /* Do a dummy multiplication first to trigger precomputation */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
2726 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002727
2728 add_count = 0;
2729 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002730 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002731 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
2732 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002733
2734 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2735 {
2736 add_c_prev = add_count;
2737 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002738 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002739 add_count = 0;
2740 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002741 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
2744 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002745
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002746 if( add_count != add_c_prev ||
2747 dbl_count != dbl_c_prev ||
2748 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002749 {
2750 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002752
2753 ret = 1;
2754 goto cleanup;
2755 }
2756 }
2757
2758 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002760
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002761 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002763 /* We computed P = 2G last time, use it */
2764
2765 add_count = 0;
2766 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002767 mul_count = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
2769 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002770
2771 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2772 {
2773 add_c_prev = add_count;
2774 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002775 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002776 add_count = 0;
2777 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002778 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
2781 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002782
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002783 if( add_count != add_c_prev ||
2784 dbl_count != dbl_c_prev ||
2785 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002786 {
2787 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002789
2790 ret = 1;
2791 goto cleanup;
2792 }
2793 }
2794
2795 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002797
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002798cleanup:
2799
2800 if( ret < 0 && verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 mbedtls_ecp_group_free( &grp );
2804 mbedtls_ecp_point_free( &R );
2805 mbedtls_ecp_point_free( &P );
2806 mbedtls_mpi_free( &m );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002807
2808 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 mbedtls_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002810
2811 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002812}
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002815
Janos Follathb0697532016-08-18 12:38:46 +01002816#endif /* !MBEDTLS_ECP_ALT */
2817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_ECP_C */