mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
0:cdf462088d13
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /*
markrad 0:cdf462088d13 2 * Elliptic curves over GF(p): generic functions
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 5 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 6 *
markrad 0:cdf462088d13 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 8 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 9 * You may obtain a copy of the License at
markrad 0:cdf462088d13 10 *
markrad 0:cdf462088d13 11 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 16 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 17 * limitations under the License.
markrad 0:cdf462088d13 18 *
markrad 0:cdf462088d13 19 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 20 */
markrad 0:cdf462088d13 21
markrad 0:cdf462088d13 22 /*
markrad 0:cdf462088d13 23 * References:
markrad 0:cdf462088d13 24 *
markrad 0:cdf462088d13 25 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
markrad 0:cdf462088d13 26 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
markrad 0:cdf462088d13 27 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
markrad 0:cdf462088d13 28 * RFC 4492 for the related TLS structures and constants
markrad 0:cdf462088d13 29 *
markrad 0:cdf462088d13 30 * [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf
markrad 0:cdf462088d13 31 *
markrad 0:cdf462088d13 32 * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
markrad 0:cdf462088d13 33 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
markrad 0:cdf462088d13 34 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
markrad 0:cdf462088d13 35 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
markrad 0:cdf462088d13 36 *
markrad 0:cdf462088d13 37 * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
markrad 0:cdf462088d13 38 * render ECC resistant against Side Channel Attacks. IACR Cryptology
markrad 0:cdf462088d13 39 * ePrint Archive, 2004, vol. 2004, p. 342.
markrad 0:cdf462088d13 40 * <http://eprint.iacr.org/2004/342.pdf>
markrad 0:cdf462088d13 41 */
markrad 0:cdf462088d13 42
markrad 0:cdf462088d13 43 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 44 #include "mbedtls/config.h"
markrad 0:cdf462088d13 45 #else
markrad 0:cdf462088d13 46 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 47 #endif
markrad 0:cdf462088d13 48
markrad 0:cdf462088d13 49 #if defined(MBEDTLS_ECP_C)
markrad 0:cdf462088d13 50
markrad 0:cdf462088d13 51 #include "mbedtls/ecp.h"
Jasper Wallace 2:bbdeda018a3c 52 #include "mbedtls/threading.h"
markrad 0:cdf462088d13 53
markrad 0:cdf462088d13 54 #include <string.h>
markrad 0:cdf462088d13 55
Jasper Wallace 2:bbdeda018a3c 56 #if !defined(MBEDTLS_ECP_ALT)
Jasper Wallace 2:bbdeda018a3c 57
markrad 0:cdf462088d13 58 #if defined(MBEDTLS_PLATFORM_C)
markrad 0:cdf462088d13 59 #include "mbedtls/platform.h"
markrad 0:cdf462088d13 60 #else
markrad 0:cdf462088d13 61 #include <stdlib.h>
markrad 0:cdf462088d13 62 #include <stdio.h>
markrad 0:cdf462088d13 63 #define mbedtls_printf printf
markrad 0:cdf462088d13 64 #define mbedtls_calloc calloc
markrad 0:cdf462088d13 65 #define mbedtls_free free
markrad 0:cdf462088d13 66 #endif
markrad 0:cdf462088d13 67
Jasper Wallace 2:bbdeda018a3c 68 #include "mbedtls/ecp_internal.h"
Jasper Wallace 2:bbdeda018a3c 69
markrad 0:cdf462088d13 70 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
markrad 0:cdf462088d13 71 !defined(inline) && !defined(__cplusplus)
markrad 0:cdf462088d13 72 #define inline __inline
markrad 0:cdf462088d13 73 #endif
markrad 0:cdf462088d13 74
markrad 0:cdf462088d13 75 /* Implementation that should never be optimized out by the compiler */
markrad 0:cdf462088d13 76 static void mbedtls_zeroize( void *v, size_t n ) {
markrad 0:cdf462088d13 77 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
markrad 0:cdf462088d13 78 }
markrad 0:cdf462088d13 79
markrad 0:cdf462088d13 80 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 81 /*
markrad 0:cdf462088d13 82 * Counts of point addition and doubling, and field multiplications.
markrad 0:cdf462088d13 83 * Used to test resistance of point multiplication to simple timing attacks.
markrad 0:cdf462088d13 84 */
markrad 0:cdf462088d13 85 static unsigned long add_count, dbl_count, mul_count;
markrad 0:cdf462088d13 86 #endif
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
markrad 0:cdf462088d13 89 defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
markrad 0:cdf462088d13 90 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
markrad 0:cdf462088d13 91 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
markrad 0:cdf462088d13 92 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
markrad 0:cdf462088d13 93 defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
markrad 0:cdf462088d13 94 defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
markrad 0:cdf462088d13 95 defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
markrad 0:cdf462088d13 96 defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
markrad 0:cdf462088d13 97 defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
markrad 0:cdf462088d13 98 defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
markrad 0:cdf462088d13 99 #define ECP_SHORTWEIERSTRASS
markrad 0:cdf462088d13 100 #endif
markrad 0:cdf462088d13 101
markrad 0:cdf462088d13 102 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
markrad 0:cdf462088d13 103 #define ECP_MONTGOMERY
markrad 0:cdf462088d13 104 #endif
markrad 0:cdf462088d13 105
markrad 0:cdf462088d13 106 /*
markrad 0:cdf462088d13 107 * Curve types: internal for now, might be exposed later
markrad 0:cdf462088d13 108 */
markrad 0:cdf462088d13 109 typedef enum
markrad 0:cdf462088d13 110 {
markrad 0:cdf462088d13 111 ECP_TYPE_NONE = 0,
markrad 0:cdf462088d13 112 ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
markrad 0:cdf462088d13 113 ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
markrad 0:cdf462088d13 114 } ecp_curve_type;
markrad 0:cdf462088d13 115
markrad 0:cdf462088d13 116 /*
markrad 0:cdf462088d13 117 * List of supported curves:
markrad 0:cdf462088d13 118 * - internal ID
markrad 0:cdf462088d13 119 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
markrad 0:cdf462088d13 120 * - size in bits
markrad 0:cdf462088d13 121 * - readable name
markrad 0:cdf462088d13 122 *
markrad 0:cdf462088d13 123 * Curves are listed in order: largest curves first, and for a given size,
markrad 0:cdf462088d13 124 * fastest curves first. This provides the default order for the SSL module.
markrad 0:cdf462088d13 125 *
markrad 0:cdf462088d13 126 * Reminder: update profiles in x509_crt.c when adding a new curves!
markrad 0:cdf462088d13 127 */
markrad 0:cdf462088d13 128 static const mbedtls_ecp_curve_info ecp_supported_curves[] =
markrad 0:cdf462088d13 129 {
markrad 0:cdf462088d13 130 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
markrad 0:cdf462088d13 131 { MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
markrad 0:cdf462088d13 132 #endif
markrad 0:cdf462088d13 133 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
markrad 0:cdf462088d13 134 { MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
markrad 0:cdf462088d13 135 #endif
markrad 0:cdf462088d13 136 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
markrad 0:cdf462088d13 137 { MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
markrad 0:cdf462088d13 138 #endif
markrad 0:cdf462088d13 139 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
markrad 0:cdf462088d13 140 { MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
markrad 0:cdf462088d13 141 #endif
markrad 0:cdf462088d13 142 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
markrad 0:cdf462088d13 143 { MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
markrad 0:cdf462088d13 144 #endif
markrad 0:cdf462088d13 145 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
markrad 0:cdf462088d13 146 { MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
markrad 0:cdf462088d13 147 #endif
markrad 0:cdf462088d13 148 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
markrad 0:cdf462088d13 149 { MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
markrad 0:cdf462088d13 150 #endif
markrad 0:cdf462088d13 151 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
markrad 0:cdf462088d13 152 { MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
markrad 0:cdf462088d13 153 #endif
markrad 0:cdf462088d13 154 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
markrad 0:cdf462088d13 155 { MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
markrad 0:cdf462088d13 156 #endif
markrad 0:cdf462088d13 157 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
markrad 0:cdf462088d13 158 { MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
markrad 0:cdf462088d13 159 #endif
markrad 0:cdf462088d13 160 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
markrad 0:cdf462088d13 161 { MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
markrad 0:cdf462088d13 162 #endif
markrad 0:cdf462088d13 163 { MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
markrad 0:cdf462088d13 164 };
markrad 0:cdf462088d13 165
markrad 0:cdf462088d13 166 #define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
markrad 0:cdf462088d13 167 sizeof( ecp_supported_curves[0] )
markrad 0:cdf462088d13 168
markrad 0:cdf462088d13 169 static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
markrad 0:cdf462088d13 170
markrad 0:cdf462088d13 171 /*
markrad 0:cdf462088d13 172 * List of supported curves and associated info
markrad 0:cdf462088d13 173 */
markrad 0:cdf462088d13 174 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
markrad 0:cdf462088d13 175 {
markrad 0:cdf462088d13 176 return( ecp_supported_curves );
markrad 0:cdf462088d13 177 }
markrad 0:cdf462088d13 178
markrad 0:cdf462088d13 179 /*
markrad 0:cdf462088d13 180 * List of supported curves, group ID only
markrad 0:cdf462088d13 181 */
markrad 0:cdf462088d13 182 const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
markrad 0:cdf462088d13 183 {
markrad 0:cdf462088d13 184 static int init_done = 0;
markrad 0:cdf462088d13 185
markrad 0:cdf462088d13 186 if( ! init_done )
markrad 0:cdf462088d13 187 {
markrad 0:cdf462088d13 188 size_t i = 0;
markrad 0:cdf462088d13 189 const mbedtls_ecp_curve_info *curve_info;
markrad 0:cdf462088d13 190
markrad 0:cdf462088d13 191 for( curve_info = mbedtls_ecp_curve_list();
markrad 0:cdf462088d13 192 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
markrad 0:cdf462088d13 193 curve_info++ )
markrad 0:cdf462088d13 194 {
markrad 0:cdf462088d13 195 ecp_supported_grp_id[i++] = curve_info->grp_id;
markrad 0:cdf462088d13 196 }
markrad 0:cdf462088d13 197 ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
markrad 0:cdf462088d13 198
markrad 0:cdf462088d13 199 init_done = 1;
markrad 0:cdf462088d13 200 }
markrad 0:cdf462088d13 201
markrad 0:cdf462088d13 202 return( ecp_supported_grp_id );
markrad 0:cdf462088d13 203 }
markrad 0:cdf462088d13 204
markrad 0:cdf462088d13 205 /*
markrad 0:cdf462088d13 206 * Get the curve info for the internal identifier
markrad 0:cdf462088d13 207 */
markrad 0:cdf462088d13 208 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
markrad 0:cdf462088d13 209 {
markrad 0:cdf462088d13 210 const mbedtls_ecp_curve_info *curve_info;
markrad 0:cdf462088d13 211
markrad 0:cdf462088d13 212 for( curve_info = mbedtls_ecp_curve_list();
markrad 0:cdf462088d13 213 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
markrad 0:cdf462088d13 214 curve_info++ )
markrad 0:cdf462088d13 215 {
markrad 0:cdf462088d13 216 if( curve_info->grp_id == grp_id )
markrad 0:cdf462088d13 217 return( curve_info );
markrad 0:cdf462088d13 218 }
markrad 0:cdf462088d13 219
markrad 0:cdf462088d13 220 return( NULL );
markrad 0:cdf462088d13 221 }
markrad 0:cdf462088d13 222
markrad 0:cdf462088d13 223 /*
markrad 0:cdf462088d13 224 * Get the curve info from the TLS identifier
markrad 0:cdf462088d13 225 */
markrad 0:cdf462088d13 226 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
markrad 0:cdf462088d13 227 {
markrad 0:cdf462088d13 228 const mbedtls_ecp_curve_info *curve_info;
markrad 0:cdf462088d13 229
markrad 0:cdf462088d13 230 for( curve_info = mbedtls_ecp_curve_list();
markrad 0:cdf462088d13 231 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
markrad 0:cdf462088d13 232 curve_info++ )
markrad 0:cdf462088d13 233 {
markrad 0:cdf462088d13 234 if( curve_info->tls_id == tls_id )
markrad 0:cdf462088d13 235 return( curve_info );
markrad 0:cdf462088d13 236 }
markrad 0:cdf462088d13 237
markrad 0:cdf462088d13 238 return( NULL );
markrad 0:cdf462088d13 239 }
markrad 0:cdf462088d13 240
markrad 0:cdf462088d13 241 /*
markrad 0:cdf462088d13 242 * Get the curve info from the name
markrad 0:cdf462088d13 243 */
markrad 0:cdf462088d13 244 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
markrad 0:cdf462088d13 245 {
markrad 0:cdf462088d13 246 const mbedtls_ecp_curve_info *curve_info;
markrad 0:cdf462088d13 247
markrad 0:cdf462088d13 248 for( curve_info = mbedtls_ecp_curve_list();
markrad 0:cdf462088d13 249 curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
markrad 0:cdf462088d13 250 curve_info++ )
markrad 0:cdf462088d13 251 {
markrad 0:cdf462088d13 252 if( strcmp( curve_info->name, name ) == 0 )
markrad 0:cdf462088d13 253 return( curve_info );
markrad 0:cdf462088d13 254 }
markrad 0:cdf462088d13 255
markrad 0:cdf462088d13 256 return( NULL );
markrad 0:cdf462088d13 257 }
markrad 0:cdf462088d13 258
markrad 0:cdf462088d13 259 /*
markrad 0:cdf462088d13 260 * Get the type of a curve
markrad 0:cdf462088d13 261 */
markrad 0:cdf462088d13 262 static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
markrad 0:cdf462088d13 263 {
markrad 0:cdf462088d13 264 if( grp->G.X.p == NULL )
markrad 0:cdf462088d13 265 return( ECP_TYPE_NONE );
markrad 0:cdf462088d13 266
markrad 0:cdf462088d13 267 if( grp->G.Y.p == NULL )
markrad 0:cdf462088d13 268 return( ECP_TYPE_MONTGOMERY );
markrad 0:cdf462088d13 269 else
markrad 0:cdf462088d13 270 return( ECP_TYPE_SHORT_WEIERSTRASS );
markrad 0:cdf462088d13 271 }
markrad 0:cdf462088d13 272
markrad 0:cdf462088d13 273 /*
markrad 0:cdf462088d13 274 * Initialize (the components of) a point
markrad 0:cdf462088d13 275 */
markrad 0:cdf462088d13 276 void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 277 {
markrad 0:cdf462088d13 278 if( pt == NULL )
markrad 0:cdf462088d13 279 return;
markrad 0:cdf462088d13 280
markrad 0:cdf462088d13 281 mbedtls_mpi_init( &pt->X );
markrad 0:cdf462088d13 282 mbedtls_mpi_init( &pt->Y );
markrad 0:cdf462088d13 283 mbedtls_mpi_init( &pt->Z );
markrad 0:cdf462088d13 284 }
markrad 0:cdf462088d13 285
markrad 0:cdf462088d13 286 /*
markrad 0:cdf462088d13 287 * Initialize (the components of) a group
markrad 0:cdf462088d13 288 */
markrad 0:cdf462088d13 289 void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
markrad 0:cdf462088d13 290 {
markrad 0:cdf462088d13 291 if( grp == NULL )
markrad 0:cdf462088d13 292 return;
markrad 0:cdf462088d13 293
markrad 0:cdf462088d13 294 memset( grp, 0, sizeof( mbedtls_ecp_group ) );
markrad 0:cdf462088d13 295 }
markrad 0:cdf462088d13 296
markrad 0:cdf462088d13 297 /*
markrad 0:cdf462088d13 298 * Initialize (the components of) a key pair
markrad 0:cdf462088d13 299 */
markrad 0:cdf462088d13 300 void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
markrad 0:cdf462088d13 301 {
markrad 0:cdf462088d13 302 if( key == NULL )
markrad 0:cdf462088d13 303 return;
markrad 0:cdf462088d13 304
markrad 0:cdf462088d13 305 mbedtls_ecp_group_init( &key->grp );
markrad 0:cdf462088d13 306 mbedtls_mpi_init( &key->d );
markrad 0:cdf462088d13 307 mbedtls_ecp_point_init( &key->Q );
markrad 0:cdf462088d13 308 }
markrad 0:cdf462088d13 309
markrad 0:cdf462088d13 310 /*
markrad 0:cdf462088d13 311 * Unallocate (the components of) a point
markrad 0:cdf462088d13 312 */
markrad 0:cdf462088d13 313 void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 314 {
markrad 0:cdf462088d13 315 if( pt == NULL )
markrad 0:cdf462088d13 316 return;
markrad 0:cdf462088d13 317
markrad 0:cdf462088d13 318 mbedtls_mpi_free( &( pt->X ) );
markrad 0:cdf462088d13 319 mbedtls_mpi_free( &( pt->Y ) );
markrad 0:cdf462088d13 320 mbedtls_mpi_free( &( pt->Z ) );
markrad 0:cdf462088d13 321 }
markrad 0:cdf462088d13 322
markrad 0:cdf462088d13 323 /*
markrad 0:cdf462088d13 324 * Unallocate (the components of) a group
markrad 0:cdf462088d13 325 */
markrad 0:cdf462088d13 326 void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
markrad 0:cdf462088d13 327 {
markrad 0:cdf462088d13 328 size_t i;
markrad 0:cdf462088d13 329
markrad 0:cdf462088d13 330 if( grp == NULL )
markrad 0:cdf462088d13 331 return;
markrad 0:cdf462088d13 332
markrad 0:cdf462088d13 333 if( grp->h != 1 )
markrad 0:cdf462088d13 334 {
markrad 0:cdf462088d13 335 mbedtls_mpi_free( &grp->P );
markrad 0:cdf462088d13 336 mbedtls_mpi_free( &grp->A );
markrad 0:cdf462088d13 337 mbedtls_mpi_free( &grp->B );
markrad 0:cdf462088d13 338 mbedtls_ecp_point_free( &grp->G );
markrad 0:cdf462088d13 339 mbedtls_mpi_free( &grp->N );
markrad 0:cdf462088d13 340 }
markrad 0:cdf462088d13 341
markrad 0:cdf462088d13 342 if( grp->T != NULL )
markrad 0:cdf462088d13 343 {
markrad 0:cdf462088d13 344 for( i = 0; i < grp->T_size; i++ )
markrad 0:cdf462088d13 345 mbedtls_ecp_point_free( &grp->T[i] );
markrad 0:cdf462088d13 346 mbedtls_free( grp->T );
markrad 0:cdf462088d13 347 }
markrad 0:cdf462088d13 348
markrad 0:cdf462088d13 349 mbedtls_zeroize( grp, sizeof( mbedtls_ecp_group ) );
markrad 0:cdf462088d13 350 }
markrad 0:cdf462088d13 351
markrad 0:cdf462088d13 352 /*
markrad 0:cdf462088d13 353 * Unallocate (the components of) a key pair
markrad 0:cdf462088d13 354 */
markrad 0:cdf462088d13 355 void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
markrad 0:cdf462088d13 356 {
markrad 0:cdf462088d13 357 if( key == NULL )
markrad 0:cdf462088d13 358 return;
markrad 0:cdf462088d13 359
markrad 0:cdf462088d13 360 mbedtls_ecp_group_free( &key->grp );
markrad 0:cdf462088d13 361 mbedtls_mpi_free( &key->d );
markrad 0:cdf462088d13 362 mbedtls_ecp_point_free( &key->Q );
markrad 0:cdf462088d13 363 }
markrad 0:cdf462088d13 364
markrad 0:cdf462088d13 365 /*
markrad 0:cdf462088d13 366 * Copy the contents of a point
markrad 0:cdf462088d13 367 */
markrad 0:cdf462088d13 368 int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
markrad 0:cdf462088d13 369 {
markrad 0:cdf462088d13 370 int ret;
markrad 0:cdf462088d13 371
markrad 0:cdf462088d13 372 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
markrad 0:cdf462088d13 373 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
markrad 0:cdf462088d13 374 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
markrad 0:cdf462088d13 375
markrad 0:cdf462088d13 376 cleanup:
markrad 0:cdf462088d13 377 return( ret );
markrad 0:cdf462088d13 378 }
markrad 0:cdf462088d13 379
markrad 0:cdf462088d13 380 /*
markrad 0:cdf462088d13 381 * Copy the contents of a group object
markrad 0:cdf462088d13 382 */
markrad 0:cdf462088d13 383 int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
markrad 0:cdf462088d13 384 {
markrad 0:cdf462088d13 385 return mbedtls_ecp_group_load( dst, src->id );
markrad 0:cdf462088d13 386 }
markrad 0:cdf462088d13 387
markrad 0:cdf462088d13 388 /*
markrad 0:cdf462088d13 389 * Set point to zero
markrad 0:cdf462088d13 390 */
markrad 0:cdf462088d13 391 int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 392 {
markrad 0:cdf462088d13 393 int ret;
markrad 0:cdf462088d13 394
markrad 0:cdf462088d13 395 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
markrad 0:cdf462088d13 396 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
markrad 0:cdf462088d13 397 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
markrad 0:cdf462088d13 398
markrad 0:cdf462088d13 399 cleanup:
markrad 0:cdf462088d13 400 return( ret );
markrad 0:cdf462088d13 401 }
markrad 0:cdf462088d13 402
markrad 0:cdf462088d13 403 /*
markrad 0:cdf462088d13 404 * Tell if a point is zero
markrad 0:cdf462088d13 405 */
markrad 0:cdf462088d13 406 int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 407 {
markrad 0:cdf462088d13 408 return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
markrad 0:cdf462088d13 409 }
markrad 0:cdf462088d13 410
markrad 0:cdf462088d13 411 /*
markrad 0:cdf462088d13 412 * Compare two points lazyly
markrad 0:cdf462088d13 413 */
markrad 0:cdf462088d13 414 int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
markrad 0:cdf462088d13 415 const mbedtls_ecp_point *Q )
markrad 0:cdf462088d13 416 {
markrad 0:cdf462088d13 417 if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 &&
markrad 0:cdf462088d13 418 mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 &&
markrad 0:cdf462088d13 419 mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 )
markrad 0:cdf462088d13 420 {
markrad 0:cdf462088d13 421 return( 0 );
markrad 0:cdf462088d13 422 }
markrad 0:cdf462088d13 423
markrad 0:cdf462088d13 424 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 425 }
markrad 0:cdf462088d13 426
markrad 0:cdf462088d13 427 /*
markrad 0:cdf462088d13 428 * Import a non-zero point from ASCII strings
markrad 0:cdf462088d13 429 */
markrad 0:cdf462088d13 430 int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
markrad 0:cdf462088d13 431 const char *x, const char *y )
markrad 0:cdf462088d13 432 {
markrad 0:cdf462088d13 433 int ret;
markrad 0:cdf462088d13 434
markrad 0:cdf462088d13 435 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
markrad 0:cdf462088d13 436 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
markrad 0:cdf462088d13 437 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
markrad 0:cdf462088d13 438
markrad 0:cdf462088d13 439 cleanup:
markrad 0:cdf462088d13 440 return( ret );
markrad 0:cdf462088d13 441 }
markrad 0:cdf462088d13 442
markrad 0:cdf462088d13 443 /*
markrad 0:cdf462088d13 444 * Export a point into unsigned binary data (SEC1 2.3.3)
markrad 0:cdf462088d13 445 */
markrad 0:cdf462088d13 446 int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
markrad 0:cdf462088d13 447 int format, size_t *olen,
markrad 0:cdf462088d13 448 unsigned char *buf, size_t buflen )
markrad 0:cdf462088d13 449 {
markrad 0:cdf462088d13 450 int ret = 0;
markrad 0:cdf462088d13 451 size_t plen;
markrad 0:cdf462088d13 452
markrad 0:cdf462088d13 453 if( format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
markrad 0:cdf462088d13 454 format != MBEDTLS_ECP_PF_COMPRESSED )
markrad 0:cdf462088d13 455 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 456
markrad 0:cdf462088d13 457 /*
markrad 0:cdf462088d13 458 * Common case: P == 0
markrad 0:cdf462088d13 459 */
markrad 0:cdf462088d13 460 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
markrad 0:cdf462088d13 461 {
markrad 0:cdf462088d13 462 if( buflen < 1 )
markrad 0:cdf462088d13 463 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 464
markrad 0:cdf462088d13 465 buf[0] = 0x00;
markrad 0:cdf462088d13 466 *olen = 1;
markrad 0:cdf462088d13 467
markrad 0:cdf462088d13 468 return( 0 );
markrad 0:cdf462088d13 469 }
markrad 0:cdf462088d13 470
markrad 0:cdf462088d13 471 plen = mbedtls_mpi_size( &grp->P );
markrad 0:cdf462088d13 472
markrad 0:cdf462088d13 473 if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
markrad 0:cdf462088d13 474 {
markrad 0:cdf462088d13 475 *olen = 2 * plen + 1;
markrad 0:cdf462088d13 476
markrad 0:cdf462088d13 477 if( buflen < *olen )
markrad 0:cdf462088d13 478 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 479
markrad 0:cdf462088d13 480 buf[0] = 0x04;
markrad 0:cdf462088d13 481 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
markrad 0:cdf462088d13 482 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
markrad 0:cdf462088d13 483 }
markrad 0:cdf462088d13 484 else if( format == MBEDTLS_ECP_PF_COMPRESSED )
markrad 0:cdf462088d13 485 {
markrad 0:cdf462088d13 486 *olen = plen + 1;
markrad 0:cdf462088d13 487
markrad 0:cdf462088d13 488 if( buflen < *olen )
markrad 0:cdf462088d13 489 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 490
markrad 0:cdf462088d13 491 buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
markrad 0:cdf462088d13 492 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
markrad 0:cdf462088d13 493 }
markrad 0:cdf462088d13 494
markrad 0:cdf462088d13 495 cleanup:
markrad 0:cdf462088d13 496 return( ret );
markrad 0:cdf462088d13 497 }
markrad 0:cdf462088d13 498
markrad 0:cdf462088d13 499 /*
markrad 0:cdf462088d13 500 * Import a point from unsigned binary data (SEC1 2.3.4)
markrad 0:cdf462088d13 501 */
markrad 0:cdf462088d13 502 int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
markrad 0:cdf462088d13 503 const unsigned char *buf, size_t ilen )
markrad 0:cdf462088d13 504 {
markrad 0:cdf462088d13 505 int ret;
markrad 0:cdf462088d13 506 size_t plen;
markrad 0:cdf462088d13 507
markrad 0:cdf462088d13 508 if( ilen < 1 )
markrad 0:cdf462088d13 509 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 510
markrad 0:cdf462088d13 511 if( buf[0] == 0x00 )
markrad 0:cdf462088d13 512 {
markrad 0:cdf462088d13 513 if( ilen == 1 )
markrad 0:cdf462088d13 514 return( mbedtls_ecp_set_zero( pt ) );
markrad 0:cdf462088d13 515 else
markrad 0:cdf462088d13 516 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 517 }
markrad 0:cdf462088d13 518
markrad 0:cdf462088d13 519 plen = mbedtls_mpi_size( &grp->P );
markrad 0:cdf462088d13 520
markrad 0:cdf462088d13 521 if( buf[0] != 0x04 )
markrad 0:cdf462088d13 522 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 523
markrad 0:cdf462088d13 524 if( ilen != 2 * plen + 1 )
markrad 0:cdf462088d13 525 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 526
markrad 0:cdf462088d13 527 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
markrad 0:cdf462088d13 528 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
markrad 0:cdf462088d13 529 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
markrad 0:cdf462088d13 530
markrad 0:cdf462088d13 531 cleanup:
markrad 0:cdf462088d13 532 return( ret );
markrad 0:cdf462088d13 533 }
markrad 0:cdf462088d13 534
markrad 0:cdf462088d13 535 /*
markrad 0:cdf462088d13 536 * Import a point from a TLS ECPoint record (RFC 4492)
markrad 0:cdf462088d13 537 * struct {
markrad 0:cdf462088d13 538 * opaque point <1..2^8-1>;
markrad 0:cdf462088d13 539 * } ECPoint;
markrad 0:cdf462088d13 540 */
markrad 0:cdf462088d13 541 int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
markrad 0:cdf462088d13 542 const unsigned char **buf, size_t buf_len )
markrad 0:cdf462088d13 543 {
markrad 0:cdf462088d13 544 unsigned char data_len;
markrad 0:cdf462088d13 545 const unsigned char *buf_start;
markrad 0:cdf462088d13 546
markrad 0:cdf462088d13 547 /*
markrad 0:cdf462088d13 548 * We must have at least two bytes (1 for length, at least one for data)
markrad 0:cdf462088d13 549 */
markrad 0:cdf462088d13 550 if( buf_len < 2 )
markrad 0:cdf462088d13 551 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 552
markrad 0:cdf462088d13 553 data_len = *(*buf)++;
markrad 0:cdf462088d13 554 if( data_len < 1 || data_len > buf_len - 1 )
markrad 0:cdf462088d13 555 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 556
markrad 0:cdf462088d13 557 /*
markrad 0:cdf462088d13 558 * Save buffer start for read_binary and update buf
markrad 0:cdf462088d13 559 */
markrad 0:cdf462088d13 560 buf_start = *buf;
markrad 0:cdf462088d13 561 *buf += data_len;
markrad 0:cdf462088d13 562
markrad 0:cdf462088d13 563 return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len );
markrad 0:cdf462088d13 564 }
markrad 0:cdf462088d13 565
markrad 0:cdf462088d13 566 /*
markrad 0:cdf462088d13 567 * Export a point as a TLS ECPoint record (RFC 4492)
markrad 0:cdf462088d13 568 * struct {
markrad 0:cdf462088d13 569 * opaque point <1..2^8-1>;
markrad 0:cdf462088d13 570 * } ECPoint;
markrad 0:cdf462088d13 571 */
markrad 0:cdf462088d13 572 int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
markrad 0:cdf462088d13 573 int format, size_t *olen,
markrad 0:cdf462088d13 574 unsigned char *buf, size_t blen )
markrad 0:cdf462088d13 575 {
markrad 0:cdf462088d13 576 int ret;
markrad 0:cdf462088d13 577
markrad 0:cdf462088d13 578 /*
markrad 0:cdf462088d13 579 * buffer length must be at least one, for our length byte
markrad 0:cdf462088d13 580 */
markrad 0:cdf462088d13 581 if( blen < 1 )
markrad 0:cdf462088d13 582 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 583
markrad 0:cdf462088d13 584 if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
markrad 0:cdf462088d13 585 olen, buf + 1, blen - 1) ) != 0 )
markrad 0:cdf462088d13 586 return( ret );
markrad 0:cdf462088d13 587
markrad 0:cdf462088d13 588 /*
markrad 0:cdf462088d13 589 * write length to the first byte and update total length
markrad 0:cdf462088d13 590 */
markrad 0:cdf462088d13 591 buf[0] = (unsigned char) *olen;
markrad 0:cdf462088d13 592 ++*olen;
markrad 0:cdf462088d13 593
markrad 0:cdf462088d13 594 return( 0 );
markrad 0:cdf462088d13 595 }
markrad 0:cdf462088d13 596
markrad 0:cdf462088d13 597 /*
markrad 0:cdf462088d13 598 * Set a group from an ECParameters record (RFC 4492)
markrad 0:cdf462088d13 599 */
markrad 0:cdf462088d13 600 int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len )
markrad 0:cdf462088d13 601 {
markrad 0:cdf462088d13 602 uint16_t tls_id;
markrad 0:cdf462088d13 603 const mbedtls_ecp_curve_info *curve_info;
markrad 0:cdf462088d13 604
markrad 0:cdf462088d13 605 /*
markrad 0:cdf462088d13 606 * We expect at least three bytes (see below)
markrad 0:cdf462088d13 607 */
markrad 0:cdf462088d13 608 if( len < 3 )
markrad 0:cdf462088d13 609 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 610
markrad 0:cdf462088d13 611 /*
markrad 0:cdf462088d13 612 * First byte is curve_type; only named_curve is handled
markrad 0:cdf462088d13 613 */
markrad 0:cdf462088d13 614 if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
markrad 0:cdf462088d13 615 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 616
markrad 0:cdf462088d13 617 /*
markrad 0:cdf462088d13 618 * Next two bytes are the namedcurve value
markrad 0:cdf462088d13 619 */
markrad 0:cdf462088d13 620 tls_id = *(*buf)++;
markrad 0:cdf462088d13 621 tls_id <<= 8;
markrad 0:cdf462088d13 622 tls_id |= *(*buf)++;
markrad 0:cdf462088d13 623
markrad 0:cdf462088d13 624 if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
markrad 0:cdf462088d13 625 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 626
markrad 0:cdf462088d13 627 return mbedtls_ecp_group_load( grp, curve_info->grp_id );
markrad 0:cdf462088d13 628 }
markrad 0:cdf462088d13 629
markrad 0:cdf462088d13 630 /*
markrad 0:cdf462088d13 631 * Write the ECParameters record corresponding to a group (RFC 4492)
markrad 0:cdf462088d13 632 */
markrad 0:cdf462088d13 633 int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
markrad 0:cdf462088d13 634 unsigned char *buf, size_t blen )
markrad 0:cdf462088d13 635 {
markrad 0:cdf462088d13 636 const mbedtls_ecp_curve_info *curve_info;
markrad 0:cdf462088d13 637
markrad 0:cdf462088d13 638 if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
markrad 0:cdf462088d13 639 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 640
markrad 0:cdf462088d13 641 /*
markrad 0:cdf462088d13 642 * We are going to write 3 bytes (see below)
markrad 0:cdf462088d13 643 */
markrad 0:cdf462088d13 644 *olen = 3;
markrad 0:cdf462088d13 645 if( blen < *olen )
markrad 0:cdf462088d13 646 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 647
markrad 0:cdf462088d13 648 /*
markrad 0:cdf462088d13 649 * First byte is curve_type, always named_curve
markrad 0:cdf462088d13 650 */
markrad 0:cdf462088d13 651 *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
markrad 0:cdf462088d13 652
markrad 0:cdf462088d13 653 /*
markrad 0:cdf462088d13 654 * Next two bytes are the namedcurve value
markrad 0:cdf462088d13 655 */
markrad 0:cdf462088d13 656 buf[0] = curve_info->tls_id >> 8;
markrad 0:cdf462088d13 657 buf[1] = curve_info->tls_id & 0xFF;
markrad 0:cdf462088d13 658
markrad 0:cdf462088d13 659 return( 0 );
markrad 0:cdf462088d13 660 }
markrad 0:cdf462088d13 661
markrad 0:cdf462088d13 662 /*
markrad 0:cdf462088d13 663 * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
markrad 0:cdf462088d13 664 * See the documentation of struct mbedtls_ecp_group.
markrad 0:cdf462088d13 665 *
markrad 0:cdf462088d13 666 * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
markrad 0:cdf462088d13 667 */
markrad 0:cdf462088d13 668 static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
markrad 0:cdf462088d13 669 {
markrad 0:cdf462088d13 670 int ret;
markrad 0:cdf462088d13 671
markrad 0:cdf462088d13 672 if( grp->modp == NULL )
markrad 0:cdf462088d13 673 return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
markrad 0:cdf462088d13 674
markrad 0:cdf462088d13 675 /* N->s < 0 is a much faster test, which fails only if N is 0 */
markrad 0:cdf462088d13 676 if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
markrad 0:cdf462088d13 677 mbedtls_mpi_bitlen( N ) > 2 * grp->pbits )
markrad 0:cdf462088d13 678 {
markrad 0:cdf462088d13 679 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 680 }
markrad 0:cdf462088d13 681
markrad 0:cdf462088d13 682 MBEDTLS_MPI_CHK( grp->modp( N ) );
markrad 0:cdf462088d13 683
markrad 0:cdf462088d13 684 /* N->s < 0 is a much faster test, which fails only if N is 0 */
markrad 0:cdf462088d13 685 while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
markrad 0:cdf462088d13 686 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
markrad 0:cdf462088d13 687
markrad 0:cdf462088d13 688 while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
markrad 0:cdf462088d13 689 /* we known P, N and the result are positive */
markrad 0:cdf462088d13 690 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
markrad 0:cdf462088d13 691
markrad 0:cdf462088d13 692 cleanup:
markrad 0:cdf462088d13 693 return( ret );
markrad 0:cdf462088d13 694 }
markrad 0:cdf462088d13 695
markrad 0:cdf462088d13 696 /*
markrad 0:cdf462088d13 697 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
markrad 0:cdf462088d13 698 *
markrad 0:cdf462088d13 699 * In order to guarantee that, we need to ensure that operands of
markrad 0:cdf462088d13 700 * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
markrad 0:cdf462088d13 701 * bring the result back to this range.
markrad 0:cdf462088d13 702 *
markrad 0:cdf462088d13 703 * The following macros are shortcuts for doing that.
markrad 0:cdf462088d13 704 */
markrad 0:cdf462088d13 705
markrad 0:cdf462088d13 706 /*
markrad 0:cdf462088d13 707 * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
markrad 0:cdf462088d13 708 */
markrad 0:cdf462088d13 709 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 710 #define INC_MUL_COUNT mul_count++;
markrad 0:cdf462088d13 711 #else
markrad 0:cdf462088d13 712 #define INC_MUL_COUNT
markrad 0:cdf462088d13 713 #endif
markrad 0:cdf462088d13 714
markrad 0:cdf462088d13 715 #define MOD_MUL( N ) do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
markrad 0:cdf462088d13 716 while( 0 )
markrad 0:cdf462088d13 717
markrad 0:cdf462088d13 718 /*
markrad 0:cdf462088d13 719 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
markrad 0:cdf462088d13 720 * N->s < 0 is a very fast test, which fails only if N is 0
markrad 0:cdf462088d13 721 */
markrad 0:cdf462088d13 722 #define MOD_SUB( N ) \
markrad 0:cdf462088d13 723 while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 ) \
markrad 0:cdf462088d13 724 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
markrad 0:cdf462088d13 725
markrad 0:cdf462088d13 726 /*
markrad 0:cdf462088d13 727 * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
markrad 0:cdf462088d13 728 * We known P, N and the result are positive, so sub_abs is correct, and
markrad 0:cdf462088d13 729 * a bit faster.
markrad 0:cdf462088d13 730 */
markrad 0:cdf462088d13 731 #define MOD_ADD( N ) \
markrad 0:cdf462088d13 732 while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
markrad 0:cdf462088d13 733 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
markrad 0:cdf462088d13 734
markrad 0:cdf462088d13 735 #if defined(ECP_SHORTWEIERSTRASS)
markrad 0:cdf462088d13 736 /*
markrad 0:cdf462088d13 737 * For curves in short Weierstrass form, we do all the internal operations in
markrad 0:cdf462088d13 738 * Jacobian coordinates.
markrad 0:cdf462088d13 739 *
markrad 0:cdf462088d13 740 * For multiplication, we'll use a comb method with coutermeasueres against
markrad 0:cdf462088d13 741 * SPA, hence timing attacks.
markrad 0:cdf462088d13 742 */
markrad 0:cdf462088d13 743
markrad 0:cdf462088d13 744 /*
markrad 0:cdf462088d13 745 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
markrad 0:cdf462088d13 746 * Cost: 1N := 1I + 3M + 1S
markrad 0:cdf462088d13 747 */
markrad 0:cdf462088d13 748 static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 749 {
markrad 0:cdf462088d13 750 int ret;
markrad 0:cdf462088d13 751 mbedtls_mpi Zi, ZZi;
markrad 0:cdf462088d13 752
markrad 0:cdf462088d13 753 if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
markrad 0:cdf462088d13 754 return( 0 );
markrad 0:cdf462088d13 755
Jasper Wallace 2:bbdeda018a3c 756 #if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
Jasper Wallace 2:bbdeda018a3c 757 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 758 {
Jasper Wallace 2:bbdeda018a3c 759 return mbedtls_internal_ecp_normalize_jac( grp, pt );
Jasper Wallace 2:bbdeda018a3c 760 }
Jasper Wallace 2:bbdeda018a3c 761 #endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
markrad 0:cdf462088d13 762 mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
markrad 0:cdf462088d13 763
markrad 0:cdf462088d13 764 /*
markrad 0:cdf462088d13 765 * X = X / Z^2 mod p
markrad 0:cdf462088d13 766 */
markrad 0:cdf462088d13 767 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
markrad 0:cdf462088d13 768 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
markrad 0:cdf462088d13 769 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
markrad 0:cdf462088d13 770
markrad 0:cdf462088d13 771 /*
markrad 0:cdf462088d13 772 * Y = Y / Z^3 mod p
markrad 0:cdf462088d13 773 */
markrad 0:cdf462088d13 774 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
markrad 0:cdf462088d13 775 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
markrad 0:cdf462088d13 776
markrad 0:cdf462088d13 777 /*
markrad 0:cdf462088d13 778 * Z = 1
markrad 0:cdf462088d13 779 */
markrad 0:cdf462088d13 780 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
markrad 0:cdf462088d13 781
markrad 0:cdf462088d13 782 cleanup:
markrad 0:cdf462088d13 783
markrad 0:cdf462088d13 784 mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
markrad 0:cdf462088d13 785
markrad 0:cdf462088d13 786 return( ret );
markrad 0:cdf462088d13 787 }
markrad 0:cdf462088d13 788
markrad 0:cdf462088d13 789 /*
markrad 0:cdf462088d13 790 * Normalize jacobian coordinates of an array of (pointers to) points,
markrad 0:cdf462088d13 791 * using Montgomery's trick to perform only one inversion mod P.
markrad 0:cdf462088d13 792 * (See for example Cohen's "A Course in Computational Algebraic Number
markrad 0:cdf462088d13 793 * Theory", Algorithm 10.3.4.)
markrad 0:cdf462088d13 794 *
markrad 0:cdf462088d13 795 * Warning: fails (returning an error) if one of the points is zero!
markrad 0:cdf462088d13 796 * This should never happen, see choice of w in ecp_mul_comb().
markrad 0:cdf462088d13 797 *
markrad 0:cdf462088d13 798 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
markrad 0:cdf462088d13 799 */
markrad 0:cdf462088d13 800 static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
markrad 0:cdf462088d13 801 mbedtls_ecp_point *T[], size_t t_len )
markrad 0:cdf462088d13 802 {
markrad 0:cdf462088d13 803 int ret;
markrad 0:cdf462088d13 804 size_t i;
markrad 0:cdf462088d13 805 mbedtls_mpi *c, u, Zi, ZZi;
markrad 0:cdf462088d13 806
markrad 0:cdf462088d13 807 if( t_len < 2 )
markrad 0:cdf462088d13 808 return( ecp_normalize_jac( grp, *T ) );
markrad 0:cdf462088d13 809
Jasper Wallace 2:bbdeda018a3c 810 #if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
Jasper Wallace 2:bbdeda018a3c 811 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 812 {
Jasper Wallace 2:bbdeda018a3c 813 return mbedtls_internal_ecp_normalize_jac_many(grp, T, t_len);
Jasper Wallace 2:bbdeda018a3c 814 }
Jasper Wallace 2:bbdeda018a3c 815 #endif
Jasper Wallace 2:bbdeda018a3c 816
markrad 0:cdf462088d13 817 if( ( c = mbedtls_calloc( t_len, sizeof( mbedtls_mpi ) ) ) == NULL )
markrad 0:cdf462088d13 818 return( MBEDTLS_ERR_ECP_ALLOC_FAILED );
markrad 0:cdf462088d13 819
markrad 0:cdf462088d13 820 mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
markrad 0:cdf462088d13 821
markrad 0:cdf462088d13 822 /*
markrad 0:cdf462088d13 823 * c[i] = Z_0 * ... * Z_i
markrad 0:cdf462088d13 824 */
markrad 0:cdf462088d13 825 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
markrad 0:cdf462088d13 826 for( i = 1; i < t_len; i++ )
markrad 0:cdf462088d13 827 {
markrad 0:cdf462088d13 828 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
markrad 0:cdf462088d13 829 MOD_MUL( c[i] );
markrad 0:cdf462088d13 830 }
markrad 0:cdf462088d13 831
markrad 0:cdf462088d13 832 /*
markrad 0:cdf462088d13 833 * u = 1 / (Z_0 * ... * Z_n) mod P
markrad 0:cdf462088d13 834 */
markrad 0:cdf462088d13 835 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
markrad 0:cdf462088d13 836
markrad 0:cdf462088d13 837 for( i = t_len - 1; ; i-- )
markrad 0:cdf462088d13 838 {
markrad 0:cdf462088d13 839 /*
markrad 0:cdf462088d13 840 * Zi = 1 / Z_i mod p
markrad 0:cdf462088d13 841 * u = 1 / (Z_0 * ... * Z_i) mod P
markrad 0:cdf462088d13 842 */
markrad 0:cdf462088d13 843 if( i == 0 ) {
markrad 0:cdf462088d13 844 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
markrad 0:cdf462088d13 845 }
markrad 0:cdf462088d13 846 else
markrad 0:cdf462088d13 847 {
markrad 0:cdf462088d13 848 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
markrad 0:cdf462088d13 849 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
markrad 0:cdf462088d13 850 }
markrad 0:cdf462088d13 851
markrad 0:cdf462088d13 852 /*
markrad 0:cdf462088d13 853 * proceed as in normalize()
markrad 0:cdf462088d13 854 */
markrad 0:cdf462088d13 855 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
markrad 0:cdf462088d13 856 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
markrad 0:cdf462088d13 857 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
markrad 0:cdf462088d13 858 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
markrad 0:cdf462088d13 859
markrad 0:cdf462088d13 860 /*
markrad 0:cdf462088d13 861 * Post-precessing: reclaim some memory by shrinking coordinates
markrad 0:cdf462088d13 862 * - not storing Z (always 1)
markrad 0:cdf462088d13 863 * - shrinking other coordinates, but still keeping the same number of
markrad 0:cdf462088d13 864 * limbs as P, as otherwise it will too likely be regrown too fast.
markrad 0:cdf462088d13 865 */
markrad 0:cdf462088d13 866 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
markrad 0:cdf462088d13 867 MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
markrad 0:cdf462088d13 868 mbedtls_mpi_free( &T[i]->Z );
markrad 0:cdf462088d13 869
markrad 0:cdf462088d13 870 if( i == 0 )
markrad 0:cdf462088d13 871 break;
markrad 0:cdf462088d13 872 }
markrad 0:cdf462088d13 873
markrad 0:cdf462088d13 874 cleanup:
markrad 0:cdf462088d13 875
markrad 0:cdf462088d13 876 mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
markrad 0:cdf462088d13 877 for( i = 0; i < t_len; i++ )
markrad 0:cdf462088d13 878 mbedtls_mpi_free( &c[i] );
markrad 0:cdf462088d13 879 mbedtls_free( c );
markrad 0:cdf462088d13 880
markrad 0:cdf462088d13 881 return( ret );
markrad 0:cdf462088d13 882 }
markrad 0:cdf462088d13 883
markrad 0:cdf462088d13 884 /*
markrad 0:cdf462088d13 885 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
markrad 0:cdf462088d13 886 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
markrad 0:cdf462088d13 887 */
markrad 0:cdf462088d13 888 static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
markrad 0:cdf462088d13 889 mbedtls_ecp_point *Q,
markrad 0:cdf462088d13 890 unsigned char inv )
markrad 0:cdf462088d13 891 {
markrad 0:cdf462088d13 892 int ret;
markrad 0:cdf462088d13 893 unsigned char nonzero;
markrad 0:cdf462088d13 894 mbedtls_mpi mQY;
markrad 0:cdf462088d13 895
markrad 0:cdf462088d13 896 mbedtls_mpi_init( &mQY );
markrad 0:cdf462088d13 897
markrad 0:cdf462088d13 898 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
markrad 0:cdf462088d13 899 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
markrad 0:cdf462088d13 900 nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
markrad 0:cdf462088d13 901 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
markrad 0:cdf462088d13 902
markrad 0:cdf462088d13 903 cleanup:
markrad 0:cdf462088d13 904 mbedtls_mpi_free( &mQY );
markrad 0:cdf462088d13 905
markrad 0:cdf462088d13 906 return( ret );
markrad 0:cdf462088d13 907 }
markrad 0:cdf462088d13 908
markrad 0:cdf462088d13 909 /*
markrad 0:cdf462088d13 910 * Point doubling R = 2 P, Jacobian coordinates
markrad 0:cdf462088d13 911 *
markrad 0:cdf462088d13 912 * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
markrad 0:cdf462088d13 913 *
markrad 0:cdf462088d13 914 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
markrad 0:cdf462088d13 915 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
markrad 0:cdf462088d13 916 *
markrad 0:cdf462088d13 917 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
markrad 0:cdf462088d13 918 *
markrad 0:cdf462088d13 919 * Cost: 1D := 3M + 4S (A == 0)
markrad 0:cdf462088d13 920 * 4M + 4S (A == -3)
markrad 0:cdf462088d13 921 * 3M + 6S + 1a otherwise
markrad 0:cdf462088d13 922 */
markrad 0:cdf462088d13 923 static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 924 const mbedtls_ecp_point *P )
markrad 0:cdf462088d13 925 {
markrad 0:cdf462088d13 926 int ret;
markrad 0:cdf462088d13 927 mbedtls_mpi M, S, T, U;
markrad 0:cdf462088d13 928
markrad 0:cdf462088d13 929 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 930 dbl_count++;
markrad 0:cdf462088d13 931 #endif
markrad 0:cdf462088d13 932
Jasper Wallace 2:bbdeda018a3c 933 #if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
Jasper Wallace 2:bbdeda018a3c 934 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 935 {
Jasper Wallace 2:bbdeda018a3c 936 return mbedtls_internal_ecp_double_jac( grp, R, P );
Jasper Wallace 2:bbdeda018a3c 937 }
Jasper Wallace 2:bbdeda018a3c 938 #endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
Jasper Wallace 2:bbdeda018a3c 939
markrad 0:cdf462088d13 940 mbedtls_mpi_init( &M ); mbedtls_mpi_init( &S ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &U );
markrad 0:cdf462088d13 941
markrad 0:cdf462088d13 942 /* Special case for A = -3 */
markrad 0:cdf462088d13 943 if( grp->A.p == NULL )
markrad 0:cdf462088d13 944 {
markrad 0:cdf462088d13 945 /* M = 3(X + Z^2)(X - Z^2) */
markrad 0:cdf462088d13 946 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
markrad 0:cdf462088d13 947 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
markrad 0:cdf462088d13 948 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
markrad 0:cdf462088d13 949 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
markrad 0:cdf462088d13 950 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
markrad 0:cdf462088d13 951 }
markrad 0:cdf462088d13 952 else
markrad 0:cdf462088d13 953 {
markrad 0:cdf462088d13 954 /* M = 3.X^2 */
markrad 0:cdf462088d13 955 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
markrad 0:cdf462088d13 956 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
markrad 0:cdf462088d13 957
markrad 0:cdf462088d13 958 /* Optimize away for "koblitz" curves with A = 0 */
markrad 0:cdf462088d13 959 if( mbedtls_mpi_cmp_int( &grp->A, 0 ) != 0 )
markrad 0:cdf462088d13 960 {
markrad 0:cdf462088d13 961 /* M += A.Z^4 */
markrad 0:cdf462088d13 962 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
markrad 0:cdf462088d13 963 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
markrad 0:cdf462088d13 964 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
markrad 0:cdf462088d13 965 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
markrad 0:cdf462088d13 966 }
markrad 0:cdf462088d13 967 }
markrad 0:cdf462088d13 968
markrad 0:cdf462088d13 969 /* S = 4.X.Y^2 */
markrad 0:cdf462088d13 970 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
markrad 0:cdf462088d13 971 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
markrad 0:cdf462088d13 972 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
markrad 0:cdf462088d13 973 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
markrad 0:cdf462088d13 974
markrad 0:cdf462088d13 975 /* U = 8.Y^4 */
markrad 0:cdf462088d13 976 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
markrad 0:cdf462088d13 977 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
markrad 0:cdf462088d13 978
markrad 0:cdf462088d13 979 /* T = M^2 - 2.S */
markrad 0:cdf462088d13 980 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
markrad 0:cdf462088d13 981 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
markrad 0:cdf462088d13 982 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
markrad 0:cdf462088d13 983
markrad 0:cdf462088d13 984 /* S = M(S - T) - U */
markrad 0:cdf462088d13 985 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
markrad 0:cdf462088d13 986 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
markrad 0:cdf462088d13 987 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
markrad 0:cdf462088d13 988
markrad 0:cdf462088d13 989 /* U = 2.Y.Z */
markrad 0:cdf462088d13 990 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
markrad 0:cdf462088d13 991 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
markrad 0:cdf462088d13 992
markrad 0:cdf462088d13 993 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &T ) );
markrad 0:cdf462088d13 994 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &S ) );
markrad 0:cdf462088d13 995 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &U ) );
markrad 0:cdf462088d13 996
markrad 0:cdf462088d13 997 cleanup:
markrad 0:cdf462088d13 998 mbedtls_mpi_free( &M ); mbedtls_mpi_free( &S ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &U );
markrad 0:cdf462088d13 999
markrad 0:cdf462088d13 1000 return( ret );
markrad 0:cdf462088d13 1001 }
markrad 0:cdf462088d13 1002
markrad 0:cdf462088d13 1003 /*
markrad 0:cdf462088d13 1004 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
markrad 0:cdf462088d13 1005 *
markrad 0:cdf462088d13 1006 * The coordinates of Q must be normalized (= affine),
markrad 0:cdf462088d13 1007 * but those of P don't need to. R is not normalized.
markrad 0:cdf462088d13 1008 *
markrad 0:cdf462088d13 1009 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
markrad 0:cdf462088d13 1010 * None of these cases can happen as intermediate step in ecp_mul_comb():
markrad 0:cdf462088d13 1011 * - at each step, P, Q and R are multiples of the base point, the factor
markrad 0:cdf462088d13 1012 * being less than its order, so none of them is zero;
markrad 0:cdf462088d13 1013 * - Q is an odd multiple of the base point, P an even multiple,
markrad 0:cdf462088d13 1014 * due to the choice of precomputed points in the modified comb method.
markrad 0:cdf462088d13 1015 * So branches for these cases do not leak secret information.
markrad 0:cdf462088d13 1016 *
markrad 0:cdf462088d13 1017 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
markrad 0:cdf462088d13 1018 *
markrad 0:cdf462088d13 1019 * Cost: 1A := 8M + 3S
markrad 0:cdf462088d13 1020 */
markrad 0:cdf462088d13 1021 static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1022 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
markrad 0:cdf462088d13 1023 {
markrad 0:cdf462088d13 1024 int ret;
markrad 0:cdf462088d13 1025 mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
markrad 0:cdf462088d13 1026
markrad 0:cdf462088d13 1027 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 1028 add_count++;
markrad 0:cdf462088d13 1029 #endif
markrad 0:cdf462088d13 1030
Jasper Wallace 2:bbdeda018a3c 1031 #if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
Jasper Wallace 2:bbdeda018a3c 1032 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 1033 {
Jasper Wallace 2:bbdeda018a3c 1034 return mbedtls_internal_ecp_add_mixed( grp, R, P, Q );
Jasper Wallace 2:bbdeda018a3c 1035 }
Jasper Wallace 2:bbdeda018a3c 1036 #endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
Jasper Wallace 2:bbdeda018a3c 1037
markrad 0:cdf462088d13 1038 /*
markrad 0:cdf462088d13 1039 * Trivial cases: P == 0 or Q == 0 (case 1)
markrad 0:cdf462088d13 1040 */
markrad 0:cdf462088d13 1041 if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
markrad 0:cdf462088d13 1042 return( mbedtls_ecp_copy( R, Q ) );
markrad 0:cdf462088d13 1043
markrad 0:cdf462088d13 1044 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
markrad 0:cdf462088d13 1045 return( mbedtls_ecp_copy( R, P ) );
markrad 0:cdf462088d13 1046
markrad 0:cdf462088d13 1047 /*
markrad 0:cdf462088d13 1048 * Make sure Q coordinates are normalized
markrad 0:cdf462088d13 1049 */
markrad 0:cdf462088d13 1050 if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
markrad 0:cdf462088d13 1051 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 1052
markrad 0:cdf462088d13 1053 mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
markrad 0:cdf462088d13 1054 mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
markrad 0:cdf462088d13 1055
markrad 0:cdf462088d13 1056 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
markrad 0:cdf462088d13 1057 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
markrad 0:cdf462088d13 1058 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
markrad 0:cdf462088d13 1059 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
markrad 0:cdf462088d13 1060 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
markrad 0:cdf462088d13 1061 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
markrad 0:cdf462088d13 1062
markrad 0:cdf462088d13 1063 /* Special cases (2) and (3) */
markrad 0:cdf462088d13 1064 if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
markrad 0:cdf462088d13 1065 {
markrad 0:cdf462088d13 1066 if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
markrad 0:cdf462088d13 1067 {
markrad 0:cdf462088d13 1068 ret = ecp_double_jac( grp, R, P );
markrad 0:cdf462088d13 1069 goto cleanup;
markrad 0:cdf462088d13 1070 }
markrad 0:cdf462088d13 1071 else
markrad 0:cdf462088d13 1072 {
markrad 0:cdf462088d13 1073 ret = mbedtls_ecp_set_zero( R );
markrad 0:cdf462088d13 1074 goto cleanup;
markrad 0:cdf462088d13 1075 }
markrad 0:cdf462088d13 1076 }
markrad 0:cdf462088d13 1077
markrad 0:cdf462088d13 1078 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
markrad 0:cdf462088d13 1079 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
markrad 0:cdf462088d13 1080 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
markrad 0:cdf462088d13 1081 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
markrad 0:cdf462088d13 1082 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
markrad 0:cdf462088d13 1083 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
markrad 0:cdf462088d13 1084 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
markrad 0:cdf462088d13 1085 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
markrad 0:cdf462088d13 1086 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
markrad 0:cdf462088d13 1087 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
markrad 0:cdf462088d13 1088 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
markrad 0:cdf462088d13 1089 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
markrad 0:cdf462088d13 1090
markrad 0:cdf462088d13 1091 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
markrad 0:cdf462088d13 1092 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
markrad 0:cdf462088d13 1093 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
markrad 0:cdf462088d13 1094
markrad 0:cdf462088d13 1095 cleanup:
markrad 0:cdf462088d13 1096
markrad 0:cdf462088d13 1097 mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
markrad 0:cdf462088d13 1098 mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
markrad 0:cdf462088d13 1099
markrad 0:cdf462088d13 1100 return( ret );
markrad 0:cdf462088d13 1101 }
markrad 0:cdf462088d13 1102
markrad 0:cdf462088d13 1103 /*
markrad 0:cdf462088d13 1104 * Randomize jacobian coordinates:
markrad 0:cdf462088d13 1105 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
markrad 0:cdf462088d13 1106 * This is sort of the reverse operation of ecp_normalize_jac().
markrad 0:cdf462088d13 1107 *
markrad 0:cdf462088d13 1108 * This countermeasure was first suggested in [2].
markrad 0:cdf462088d13 1109 */
markrad 0:cdf462088d13 1110 static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
markrad 0:cdf462088d13 1111 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
markrad 0:cdf462088d13 1112 {
markrad 0:cdf462088d13 1113 int ret;
markrad 0:cdf462088d13 1114 mbedtls_mpi l, ll;
Jasper Wallace 2:bbdeda018a3c 1115 size_t p_size;
markrad 0:cdf462088d13 1116 int count = 0;
markrad 0:cdf462088d13 1117
Jasper Wallace 2:bbdeda018a3c 1118 #if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
Jasper Wallace 2:bbdeda018a3c 1119 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 1120 {
Jasper Wallace 2:bbdeda018a3c 1121 return mbedtls_internal_ecp_randomize_jac( grp, pt, f_rng, p_rng );
Jasper Wallace 2:bbdeda018a3c 1122 }
Jasper Wallace 2:bbdeda018a3c 1123 #endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
Jasper Wallace 2:bbdeda018a3c 1124
Jasper Wallace 2:bbdeda018a3c 1125 p_size = ( grp->pbits + 7 ) / 8;
markrad 0:cdf462088d13 1126 mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
markrad 0:cdf462088d13 1127
markrad 0:cdf462088d13 1128 /* Generate l such that 1 < l < p */
markrad 0:cdf462088d13 1129 do
markrad 0:cdf462088d13 1130 {
Jasper Wallace 2:bbdeda018a3c 1131 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
markrad 0:cdf462088d13 1132
markrad 0:cdf462088d13 1133 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
markrad 0:cdf462088d13 1134 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
markrad 0:cdf462088d13 1135
markrad 0:cdf462088d13 1136 if( count++ > 10 )
markrad 0:cdf462088d13 1137 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
markrad 0:cdf462088d13 1138 }
markrad 0:cdf462088d13 1139 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
markrad 0:cdf462088d13 1140
markrad 0:cdf462088d13 1141 /* Z = l * Z */
markrad 0:cdf462088d13 1142 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
markrad 0:cdf462088d13 1143
markrad 0:cdf462088d13 1144 /* X = l^2 * X */
markrad 0:cdf462088d13 1145 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
markrad 0:cdf462088d13 1146 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
markrad 0:cdf462088d13 1147
markrad 0:cdf462088d13 1148 /* Y = l^3 * Y */
markrad 0:cdf462088d13 1149 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
markrad 0:cdf462088d13 1150 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
markrad 0:cdf462088d13 1151
markrad 0:cdf462088d13 1152 cleanup:
markrad 0:cdf462088d13 1153 mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
markrad 0:cdf462088d13 1154
markrad 0:cdf462088d13 1155 return( ret );
markrad 0:cdf462088d13 1156 }
markrad 0:cdf462088d13 1157
markrad 0:cdf462088d13 1158 /*
markrad 0:cdf462088d13 1159 * Check and define parameters used by the comb method (see below for details)
markrad 0:cdf462088d13 1160 */
markrad 0:cdf462088d13 1161 #if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
markrad 0:cdf462088d13 1162 #error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
markrad 0:cdf462088d13 1163 #endif
markrad 0:cdf462088d13 1164
markrad 0:cdf462088d13 1165 /* d = ceil( n / w ) */
markrad 0:cdf462088d13 1166 #define COMB_MAX_D ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
markrad 0:cdf462088d13 1167
markrad 0:cdf462088d13 1168 /* number of precomputed points */
markrad 0:cdf462088d13 1169 #define COMB_MAX_PRE ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
markrad 0:cdf462088d13 1170
markrad 0:cdf462088d13 1171 /*
markrad 0:cdf462088d13 1172 * Compute the representation of m that will be used with our comb method.
markrad 0:cdf462088d13 1173 *
markrad 0:cdf462088d13 1174 * The basic comb method is described in GECC 3.44 for example. We use a
markrad 0:cdf462088d13 1175 * modified version that provides resistance to SPA by avoiding zero
markrad 0:cdf462088d13 1176 * digits in the representation as in [3]. We modify the method further by
markrad 0:cdf462088d13 1177 * requiring that all K_i be odd, which has the small cost that our
markrad 0:cdf462088d13 1178 * representation uses one more K_i, due to carries.
markrad 0:cdf462088d13 1179 *
markrad 0:cdf462088d13 1180 * Also, for the sake of compactness, only the seven low-order bits of x[i]
markrad 0:cdf462088d13 1181 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
markrad 0:cdf462088d13 1182 * the paper): it is set if and only if if s_i == -1;
markrad 0:cdf462088d13 1183 *
markrad 0:cdf462088d13 1184 * Calling conventions:
markrad 0:cdf462088d13 1185 * - x is an array of size d + 1
markrad 0:cdf462088d13 1186 * - w is the size, ie number of teeth, of the comb, and must be between
markrad 0:cdf462088d13 1187 * 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
markrad 0:cdf462088d13 1188 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
markrad 0:cdf462088d13 1189 * (the result will be incorrect if these assumptions are not satisfied)
markrad 0:cdf462088d13 1190 */
markrad 0:cdf462088d13 1191 static void ecp_comb_fixed( unsigned char x[], size_t d,
markrad 0:cdf462088d13 1192 unsigned char w, const mbedtls_mpi *m )
markrad 0:cdf462088d13 1193 {
markrad 0:cdf462088d13 1194 size_t i, j;
markrad 0:cdf462088d13 1195 unsigned char c, cc, adjust;
markrad 0:cdf462088d13 1196
markrad 0:cdf462088d13 1197 memset( x, 0, d+1 );
markrad 0:cdf462088d13 1198
markrad 0:cdf462088d13 1199 /* First get the classical comb values (except for x_d = 0) */
markrad 0:cdf462088d13 1200 for( i = 0; i < d; i++ )
markrad 0:cdf462088d13 1201 for( j = 0; j < w; j++ )
markrad 0:cdf462088d13 1202 x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
markrad 0:cdf462088d13 1203
markrad 0:cdf462088d13 1204 /* Now make sure x_1 .. x_d are odd */
markrad 0:cdf462088d13 1205 c = 0;
markrad 0:cdf462088d13 1206 for( i = 1; i <= d; i++ )
markrad 0:cdf462088d13 1207 {
markrad 0:cdf462088d13 1208 /* Add carry and update it */
markrad 0:cdf462088d13 1209 cc = x[i] & c;
markrad 0:cdf462088d13 1210 x[i] = x[i] ^ c;
markrad 0:cdf462088d13 1211 c = cc;
markrad 0:cdf462088d13 1212
markrad 0:cdf462088d13 1213 /* Adjust if needed, avoiding branches */
markrad 0:cdf462088d13 1214 adjust = 1 - ( x[i] & 0x01 );
markrad 0:cdf462088d13 1215 c |= x[i] & ( x[i-1] * adjust );
markrad 0:cdf462088d13 1216 x[i] = x[i] ^ ( x[i-1] * adjust );
markrad 0:cdf462088d13 1217 x[i-1] |= adjust << 7;
markrad 0:cdf462088d13 1218 }
markrad 0:cdf462088d13 1219 }
markrad 0:cdf462088d13 1220
markrad 0:cdf462088d13 1221 /*
markrad 0:cdf462088d13 1222 * Precompute points for the comb method
markrad 0:cdf462088d13 1223 *
markrad 0:cdf462088d13 1224 * If i = i_{w-1} ... i_1 is the binary representation of i, then
markrad 0:cdf462088d13 1225 * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
markrad 0:cdf462088d13 1226 *
markrad 0:cdf462088d13 1227 * T must be able to hold 2^{w - 1} elements
markrad 0:cdf462088d13 1228 *
markrad 0:cdf462088d13 1229 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
markrad 0:cdf462088d13 1230 */
markrad 0:cdf462088d13 1231 static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
markrad 0:cdf462088d13 1232 mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
markrad 0:cdf462088d13 1233 unsigned char w, size_t d )
markrad 0:cdf462088d13 1234 {
markrad 0:cdf462088d13 1235 int ret;
markrad 0:cdf462088d13 1236 unsigned char i, k;
markrad 0:cdf462088d13 1237 size_t j;
markrad 0:cdf462088d13 1238 mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
markrad 0:cdf462088d13 1239
markrad 0:cdf462088d13 1240 /*
markrad 0:cdf462088d13 1241 * Set T[0] = P and
markrad 0:cdf462088d13 1242 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
markrad 0:cdf462088d13 1243 */
markrad 0:cdf462088d13 1244 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
markrad 0:cdf462088d13 1245
markrad 0:cdf462088d13 1246 k = 0;
markrad 0:cdf462088d13 1247 for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
markrad 0:cdf462088d13 1248 {
markrad 0:cdf462088d13 1249 cur = T + i;
markrad 0:cdf462088d13 1250 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
markrad 0:cdf462088d13 1251 for( j = 0; j < d; j++ )
markrad 0:cdf462088d13 1252 MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
markrad 0:cdf462088d13 1253
markrad 0:cdf462088d13 1254 TT[k++] = cur;
markrad 0:cdf462088d13 1255 }
markrad 0:cdf462088d13 1256
markrad 0:cdf462088d13 1257 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
markrad 0:cdf462088d13 1258
markrad 0:cdf462088d13 1259 /*
markrad 0:cdf462088d13 1260 * Compute the remaining ones using the minimal number of additions
markrad 0:cdf462088d13 1261 * Be careful to update T[2^l] only after using it!
markrad 0:cdf462088d13 1262 */
markrad 0:cdf462088d13 1263 k = 0;
markrad 0:cdf462088d13 1264 for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
markrad 0:cdf462088d13 1265 {
markrad 0:cdf462088d13 1266 j = i;
markrad 0:cdf462088d13 1267 while( j-- )
markrad 0:cdf462088d13 1268 {
markrad 0:cdf462088d13 1269 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
markrad 0:cdf462088d13 1270 TT[k++] = &T[i + j];
markrad 0:cdf462088d13 1271 }
markrad 0:cdf462088d13 1272 }
markrad 0:cdf462088d13 1273
markrad 0:cdf462088d13 1274 MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
markrad 0:cdf462088d13 1275
markrad 0:cdf462088d13 1276 cleanup:
Jasper Wallace 2:bbdeda018a3c 1277
markrad 0:cdf462088d13 1278 return( ret );
markrad 0:cdf462088d13 1279 }
markrad 0:cdf462088d13 1280
markrad 0:cdf462088d13 1281 /*
markrad 0:cdf462088d13 1282 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
markrad 0:cdf462088d13 1283 */
markrad 0:cdf462088d13 1284 static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1285 const mbedtls_ecp_point T[], unsigned char t_len,
markrad 0:cdf462088d13 1286 unsigned char i )
markrad 0:cdf462088d13 1287 {
markrad 0:cdf462088d13 1288 int ret;
markrad 0:cdf462088d13 1289 unsigned char ii, j;
markrad 0:cdf462088d13 1290
markrad 0:cdf462088d13 1291 /* Ignore the "sign" bit and scale down */
markrad 0:cdf462088d13 1292 ii = ( i & 0x7Fu ) >> 1;
markrad 0:cdf462088d13 1293
markrad 0:cdf462088d13 1294 /* Read the whole table to thwart cache-based timing attacks */
markrad 0:cdf462088d13 1295 for( j = 0; j < t_len; j++ )
markrad 0:cdf462088d13 1296 {
markrad 0:cdf462088d13 1297 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
markrad 0:cdf462088d13 1298 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
markrad 0:cdf462088d13 1299 }
markrad 0:cdf462088d13 1300
markrad 0:cdf462088d13 1301 /* Safely invert result if i is "negative" */
markrad 0:cdf462088d13 1302 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
markrad 0:cdf462088d13 1303
markrad 0:cdf462088d13 1304 cleanup:
markrad 0:cdf462088d13 1305 return( ret );
markrad 0:cdf462088d13 1306 }
markrad 0:cdf462088d13 1307
markrad 0:cdf462088d13 1308 /*
markrad 0:cdf462088d13 1309 * Core multiplication algorithm for the (modified) comb method.
markrad 0:cdf462088d13 1310 * This part is actually common with the basic comb method (GECC 3.44)
markrad 0:cdf462088d13 1311 *
markrad 0:cdf462088d13 1312 * Cost: d A + d D + 1 R
markrad 0:cdf462088d13 1313 */
markrad 0:cdf462088d13 1314 static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1315 const mbedtls_ecp_point T[], unsigned char t_len,
markrad 0:cdf462088d13 1316 const unsigned char x[], size_t d,
markrad 0:cdf462088d13 1317 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 1318 void *p_rng )
markrad 0:cdf462088d13 1319 {
markrad 0:cdf462088d13 1320 int ret;
markrad 0:cdf462088d13 1321 mbedtls_ecp_point Txi;
markrad 0:cdf462088d13 1322 size_t i;
markrad 0:cdf462088d13 1323
markrad 0:cdf462088d13 1324 mbedtls_ecp_point_init( &Txi );
markrad 0:cdf462088d13 1325
markrad 0:cdf462088d13 1326 /* Start with a non-zero point and randomize its coordinates */
markrad 0:cdf462088d13 1327 i = d;
markrad 0:cdf462088d13 1328 MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
markrad 0:cdf462088d13 1329 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
markrad 0:cdf462088d13 1330 if( f_rng != 0 )
markrad 0:cdf462088d13 1331 MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
markrad 0:cdf462088d13 1332
markrad 0:cdf462088d13 1333 while( i-- != 0 )
markrad 0:cdf462088d13 1334 {
markrad 0:cdf462088d13 1335 MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
markrad 0:cdf462088d13 1336 MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
markrad 0:cdf462088d13 1337 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
markrad 0:cdf462088d13 1338 }
markrad 0:cdf462088d13 1339
markrad 0:cdf462088d13 1340 cleanup:
Jasper Wallace 2:bbdeda018a3c 1341
markrad 0:cdf462088d13 1342 mbedtls_ecp_point_free( &Txi );
markrad 0:cdf462088d13 1343
markrad 0:cdf462088d13 1344 return( ret );
markrad 0:cdf462088d13 1345 }
markrad 0:cdf462088d13 1346
markrad 0:cdf462088d13 1347 /*
markrad 0:cdf462088d13 1348 * Multiplication using the comb method,
markrad 0:cdf462088d13 1349 * for curves in short Weierstrass form
markrad 0:cdf462088d13 1350 */
markrad 0:cdf462088d13 1351 static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1352 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
markrad 0:cdf462088d13 1353 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 1354 void *p_rng )
markrad 0:cdf462088d13 1355 {
markrad 0:cdf462088d13 1356 int ret;
markrad 0:cdf462088d13 1357 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
markrad 0:cdf462088d13 1358 size_t d;
markrad 0:cdf462088d13 1359 unsigned char k[COMB_MAX_D + 1];
markrad 0:cdf462088d13 1360 mbedtls_ecp_point *T;
markrad 0:cdf462088d13 1361 mbedtls_mpi M, mm;
markrad 0:cdf462088d13 1362
markrad 0:cdf462088d13 1363 mbedtls_mpi_init( &M );
markrad 0:cdf462088d13 1364 mbedtls_mpi_init( &mm );
markrad 0:cdf462088d13 1365
markrad 0:cdf462088d13 1366 /* we need N to be odd to trnaform m in an odd number, check now */
markrad 0:cdf462088d13 1367 if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
markrad 0:cdf462088d13 1368 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 1369
markrad 0:cdf462088d13 1370 /*
markrad 0:cdf462088d13 1371 * Minimize the number of multiplications, that is minimize
markrad 0:cdf462088d13 1372 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
markrad 0:cdf462088d13 1373 * (see costs of the various parts, with 1S = 1M)
markrad 0:cdf462088d13 1374 */
markrad 0:cdf462088d13 1375 w = grp->nbits >= 384 ? 5 : 4;
markrad 0:cdf462088d13 1376
markrad 0:cdf462088d13 1377 /*
markrad 0:cdf462088d13 1378 * If P == G, pre-compute a bit more, since this may be re-used later.
markrad 0:cdf462088d13 1379 * Just adding one avoids upping the cost of the first mul too much,
markrad 0:cdf462088d13 1380 * and the memory cost too.
markrad 0:cdf462088d13 1381 */
markrad 0:cdf462088d13 1382 #if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
markrad 0:cdf462088d13 1383 p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
markrad 0:cdf462088d13 1384 mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
markrad 0:cdf462088d13 1385 if( p_eq_g )
markrad 0:cdf462088d13 1386 w++;
markrad 0:cdf462088d13 1387 #else
markrad 0:cdf462088d13 1388 p_eq_g = 0;
markrad 0:cdf462088d13 1389 #endif
markrad 0:cdf462088d13 1390
markrad 0:cdf462088d13 1391 /*
markrad 0:cdf462088d13 1392 * Make sure w is within bounds.
markrad 0:cdf462088d13 1393 * (The last test is useful only for very small curves in the test suite.)
markrad 0:cdf462088d13 1394 */
markrad 0:cdf462088d13 1395 if( w > MBEDTLS_ECP_WINDOW_SIZE )
markrad 0:cdf462088d13 1396 w = MBEDTLS_ECP_WINDOW_SIZE;
markrad 0:cdf462088d13 1397 if( w >= grp->nbits )
markrad 0:cdf462088d13 1398 w = 2;
markrad 0:cdf462088d13 1399
markrad 0:cdf462088d13 1400 /* Other sizes that depend on w */
markrad 0:cdf462088d13 1401 pre_len = 1U << ( w - 1 );
markrad 0:cdf462088d13 1402 d = ( grp->nbits + w - 1 ) / w;
markrad 0:cdf462088d13 1403
markrad 0:cdf462088d13 1404 /*
markrad 0:cdf462088d13 1405 * Prepare precomputed points: if P == G we want to
markrad 0:cdf462088d13 1406 * use grp->T if already initialized, or initialize it.
markrad 0:cdf462088d13 1407 */
markrad 0:cdf462088d13 1408 T = p_eq_g ? grp->T : NULL;
markrad 0:cdf462088d13 1409
markrad 0:cdf462088d13 1410 if( T == NULL )
markrad 0:cdf462088d13 1411 {
markrad 0:cdf462088d13 1412 T = mbedtls_calloc( pre_len, sizeof( mbedtls_ecp_point ) );
markrad 0:cdf462088d13 1413 if( T == NULL )
markrad 0:cdf462088d13 1414 {
markrad 0:cdf462088d13 1415 ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
markrad 0:cdf462088d13 1416 goto cleanup;
markrad 0:cdf462088d13 1417 }
markrad 0:cdf462088d13 1418
markrad 0:cdf462088d13 1419 MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
markrad 0:cdf462088d13 1420
markrad 0:cdf462088d13 1421 if( p_eq_g )
markrad 0:cdf462088d13 1422 {
markrad 0:cdf462088d13 1423 grp->T = T;
markrad 0:cdf462088d13 1424 grp->T_size = pre_len;
markrad 0:cdf462088d13 1425 }
markrad 0:cdf462088d13 1426 }
markrad 0:cdf462088d13 1427
markrad 0:cdf462088d13 1428 /*
markrad 0:cdf462088d13 1429 * Make sure M is odd (M = m or M = N - m, since N is odd)
markrad 0:cdf462088d13 1430 * using the fact that m * P = - (N - m) * P
markrad 0:cdf462088d13 1431 */
markrad 0:cdf462088d13 1432 m_is_odd = ( mbedtls_mpi_get_bit( m, 0 ) == 1 );
markrad 0:cdf462088d13 1433 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
markrad 0:cdf462088d13 1434 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
markrad 0:cdf462088d13 1435 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
markrad 0:cdf462088d13 1436
markrad 0:cdf462088d13 1437 /*
markrad 0:cdf462088d13 1438 * Go for comb multiplication, R = M * P
markrad 0:cdf462088d13 1439 */
markrad 0:cdf462088d13 1440 ecp_comb_fixed( k, d, w, &M );
markrad 0:cdf462088d13 1441 MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
markrad 0:cdf462088d13 1442
markrad 0:cdf462088d13 1443 /*
markrad 0:cdf462088d13 1444 * Now get m * P from M * P and normalize it
markrad 0:cdf462088d13 1445 */
markrad 0:cdf462088d13 1446 MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
markrad 0:cdf462088d13 1447 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
markrad 0:cdf462088d13 1448
markrad 0:cdf462088d13 1449 cleanup:
markrad 0:cdf462088d13 1450
markrad 0:cdf462088d13 1451 if( T != NULL && ! p_eq_g )
markrad 0:cdf462088d13 1452 {
markrad 0:cdf462088d13 1453 for( i = 0; i < pre_len; i++ )
markrad 0:cdf462088d13 1454 mbedtls_ecp_point_free( &T[i] );
markrad 0:cdf462088d13 1455 mbedtls_free( T );
markrad 0:cdf462088d13 1456 }
markrad 0:cdf462088d13 1457
markrad 0:cdf462088d13 1458 mbedtls_mpi_free( &M );
markrad 0:cdf462088d13 1459 mbedtls_mpi_free( &mm );
markrad 0:cdf462088d13 1460
markrad 0:cdf462088d13 1461 if( ret != 0 )
markrad 0:cdf462088d13 1462 mbedtls_ecp_point_free( R );
markrad 0:cdf462088d13 1463
markrad 0:cdf462088d13 1464 return( ret );
markrad 0:cdf462088d13 1465 }
markrad 0:cdf462088d13 1466
markrad 0:cdf462088d13 1467 #endif /* ECP_SHORTWEIERSTRASS */
markrad 0:cdf462088d13 1468
markrad 0:cdf462088d13 1469 #if defined(ECP_MONTGOMERY)
markrad 0:cdf462088d13 1470 /*
markrad 0:cdf462088d13 1471 * For Montgomery curves, we do all the internal arithmetic in projective
markrad 0:cdf462088d13 1472 * coordinates. Import/export of points uses only the x coordinates, which is
markrad 0:cdf462088d13 1473 * internaly represented as X / Z.
markrad 0:cdf462088d13 1474 *
markrad 0:cdf462088d13 1475 * For scalar multiplication, we'll use a Montgomery ladder.
markrad 0:cdf462088d13 1476 */
markrad 0:cdf462088d13 1477
markrad 0:cdf462088d13 1478 /*
markrad 0:cdf462088d13 1479 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
markrad 0:cdf462088d13 1480 * Cost: 1M + 1I
markrad 0:cdf462088d13 1481 */
markrad 0:cdf462088d13 1482 static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
markrad 0:cdf462088d13 1483 {
markrad 0:cdf462088d13 1484 int ret;
markrad 0:cdf462088d13 1485
Jasper Wallace 2:bbdeda018a3c 1486 #if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
Jasper Wallace 2:bbdeda018a3c 1487 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 1488 {
Jasper Wallace 2:bbdeda018a3c 1489 return mbedtls_internal_ecp_normalize_mxz( grp, P );
Jasper Wallace 2:bbdeda018a3c 1490 }
Jasper Wallace 2:bbdeda018a3c 1491 #endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
Jasper Wallace 2:bbdeda018a3c 1492
markrad 0:cdf462088d13 1493 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
markrad 0:cdf462088d13 1494 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
markrad 0:cdf462088d13 1495 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
markrad 0:cdf462088d13 1496
markrad 0:cdf462088d13 1497 cleanup:
markrad 0:cdf462088d13 1498 return( ret );
markrad 0:cdf462088d13 1499 }
markrad 0:cdf462088d13 1500
markrad 0:cdf462088d13 1501 /*
markrad 0:cdf462088d13 1502 * Randomize projective x/z coordinates:
markrad 0:cdf462088d13 1503 * (X, Z) -> (l X, l Z) for random l
markrad 0:cdf462088d13 1504 * This is sort of the reverse operation of ecp_normalize_mxz().
markrad 0:cdf462088d13 1505 *
markrad 0:cdf462088d13 1506 * This countermeasure was first suggested in [2].
markrad 0:cdf462088d13 1507 * Cost: 2M
markrad 0:cdf462088d13 1508 */
markrad 0:cdf462088d13 1509 static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
markrad 0:cdf462088d13 1510 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
markrad 0:cdf462088d13 1511 {
markrad 0:cdf462088d13 1512 int ret;
markrad 0:cdf462088d13 1513 mbedtls_mpi l;
Jasper Wallace 2:bbdeda018a3c 1514 size_t p_size;
markrad 0:cdf462088d13 1515 int count = 0;
markrad 0:cdf462088d13 1516
Jasper Wallace 2:bbdeda018a3c 1517 #if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
Jasper Wallace 2:bbdeda018a3c 1518 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 1519 {
Jasper Wallace 2:bbdeda018a3c 1520 return mbedtls_internal_ecp_randomize_mxz( grp, P, f_rng, p_rng );
Jasper Wallace 2:bbdeda018a3c 1521 }
Jasper Wallace 2:bbdeda018a3c 1522 #endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
Jasper Wallace 2:bbdeda018a3c 1523
Jasper Wallace 2:bbdeda018a3c 1524 p_size = ( grp->pbits + 7 ) / 8;
markrad 0:cdf462088d13 1525 mbedtls_mpi_init( &l );
markrad 0:cdf462088d13 1526
markrad 0:cdf462088d13 1527 /* Generate l such that 1 < l < p */
markrad 0:cdf462088d13 1528 do
markrad 0:cdf462088d13 1529 {
Jasper Wallace 2:bbdeda018a3c 1530 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng ) );
markrad 0:cdf462088d13 1531
markrad 0:cdf462088d13 1532 while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
markrad 0:cdf462088d13 1533 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
markrad 0:cdf462088d13 1534
markrad 0:cdf462088d13 1535 if( count++ > 10 )
markrad 0:cdf462088d13 1536 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
markrad 0:cdf462088d13 1537 }
markrad 0:cdf462088d13 1538 while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
markrad 0:cdf462088d13 1539
markrad 0:cdf462088d13 1540 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
markrad 0:cdf462088d13 1541 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
markrad 0:cdf462088d13 1542
markrad 0:cdf462088d13 1543 cleanup:
markrad 0:cdf462088d13 1544 mbedtls_mpi_free( &l );
markrad 0:cdf462088d13 1545
markrad 0:cdf462088d13 1546 return( ret );
markrad 0:cdf462088d13 1547 }
markrad 0:cdf462088d13 1548
markrad 0:cdf462088d13 1549 /*
markrad 0:cdf462088d13 1550 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
markrad 0:cdf462088d13 1551 * for Montgomery curves in x/z coordinates.
markrad 0:cdf462088d13 1552 *
markrad 0:cdf462088d13 1553 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
markrad 0:cdf462088d13 1554 * with
markrad 0:cdf462088d13 1555 * d = X1
markrad 0:cdf462088d13 1556 * P = (X2, Z2)
markrad 0:cdf462088d13 1557 * Q = (X3, Z3)
markrad 0:cdf462088d13 1558 * R = (X4, Z4)
markrad 0:cdf462088d13 1559 * S = (X5, Z5)
markrad 0:cdf462088d13 1560 * and eliminating temporary variables tO, ..., t4.
markrad 0:cdf462088d13 1561 *
markrad 0:cdf462088d13 1562 * Cost: 5M + 4S
markrad 0:cdf462088d13 1563 */
markrad 0:cdf462088d13 1564 static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
markrad 0:cdf462088d13 1565 mbedtls_ecp_point *R, mbedtls_ecp_point *S,
markrad 0:cdf462088d13 1566 const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
markrad 0:cdf462088d13 1567 const mbedtls_mpi *d )
markrad 0:cdf462088d13 1568 {
markrad 0:cdf462088d13 1569 int ret;
markrad 0:cdf462088d13 1570 mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
markrad 0:cdf462088d13 1571
Jasper Wallace 2:bbdeda018a3c 1572 #if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
Jasper Wallace 2:bbdeda018a3c 1573 if ( mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 1574 {
Jasper Wallace 2:bbdeda018a3c 1575 return mbedtls_internal_ecp_double_add_mxz( grp, R, S, P, Q, d );
Jasper Wallace 2:bbdeda018a3c 1576 }
Jasper Wallace 2:bbdeda018a3c 1577 #endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
Jasper Wallace 2:bbdeda018a3c 1578
markrad 0:cdf462088d13 1579 mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
markrad 0:cdf462088d13 1580 mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
markrad 0:cdf462088d13 1581 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
markrad 0:cdf462088d13 1582
markrad 0:cdf462088d13 1583 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
markrad 0:cdf462088d13 1584 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
markrad 0:cdf462088d13 1585 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
markrad 0:cdf462088d13 1586 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
markrad 0:cdf462088d13 1587 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
markrad 0:cdf462088d13 1588 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
markrad 0:cdf462088d13 1589 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
markrad 0:cdf462088d13 1590 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
markrad 0:cdf462088d13 1591 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
markrad 0:cdf462088d13 1592 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
markrad 0:cdf462088d13 1593 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
markrad 0:cdf462088d13 1594 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
markrad 0:cdf462088d13 1595 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
markrad 0:cdf462088d13 1596 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
markrad 0:cdf462088d13 1597 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
markrad 0:cdf462088d13 1598 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
markrad 0:cdf462088d13 1599 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
markrad 0:cdf462088d13 1600 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
markrad 0:cdf462088d13 1601
markrad 0:cdf462088d13 1602 cleanup:
markrad 0:cdf462088d13 1603 mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
markrad 0:cdf462088d13 1604 mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
markrad 0:cdf462088d13 1605 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
markrad 0:cdf462088d13 1606
markrad 0:cdf462088d13 1607 return( ret );
markrad 0:cdf462088d13 1608 }
markrad 0:cdf462088d13 1609
markrad 0:cdf462088d13 1610 /*
markrad 0:cdf462088d13 1611 * Multiplication with Montgomery ladder in x/z coordinates,
markrad 0:cdf462088d13 1612 * for curves in Montgomery form
markrad 0:cdf462088d13 1613 */
markrad 0:cdf462088d13 1614 static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1615 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
markrad 0:cdf462088d13 1616 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 1617 void *p_rng )
markrad 0:cdf462088d13 1618 {
markrad 0:cdf462088d13 1619 int ret;
markrad 0:cdf462088d13 1620 size_t i;
markrad 0:cdf462088d13 1621 unsigned char b;
markrad 0:cdf462088d13 1622 mbedtls_ecp_point RP;
markrad 0:cdf462088d13 1623 mbedtls_mpi PX;
markrad 0:cdf462088d13 1624
markrad 0:cdf462088d13 1625 mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
markrad 0:cdf462088d13 1626
markrad 0:cdf462088d13 1627 /* Save PX and read from P before writing to R, in case P == R */
markrad 0:cdf462088d13 1628 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
markrad 0:cdf462088d13 1629 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
markrad 0:cdf462088d13 1630
markrad 0:cdf462088d13 1631 /* Set R to zero in modified x/z coordinates */
markrad 0:cdf462088d13 1632 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
markrad 0:cdf462088d13 1633 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
markrad 0:cdf462088d13 1634 mbedtls_mpi_free( &R->Y );
markrad 0:cdf462088d13 1635
markrad 0:cdf462088d13 1636 /* RP.X might be sligtly larger than P, so reduce it */
markrad 0:cdf462088d13 1637 MOD_ADD( RP.X );
markrad 0:cdf462088d13 1638
markrad 0:cdf462088d13 1639 /* Randomize coordinates of the starting point */
markrad 0:cdf462088d13 1640 if( f_rng != NULL )
markrad 0:cdf462088d13 1641 MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
markrad 0:cdf462088d13 1642
markrad 0:cdf462088d13 1643 /* Loop invariant: R = result so far, RP = R + P */
markrad 0:cdf462088d13 1644 i = mbedtls_mpi_bitlen( m ); /* one past the (zero-based) most significant bit */
markrad 0:cdf462088d13 1645 while( i-- > 0 )
markrad 0:cdf462088d13 1646 {
markrad 0:cdf462088d13 1647 b = mbedtls_mpi_get_bit( m, i );
markrad 0:cdf462088d13 1648 /*
markrad 0:cdf462088d13 1649 * if (b) R = 2R + P else R = 2R,
markrad 0:cdf462088d13 1650 * which is:
markrad 0:cdf462088d13 1651 * if (b) double_add( RP, R, RP, R )
markrad 0:cdf462088d13 1652 * else double_add( R, RP, R, RP )
markrad 0:cdf462088d13 1653 * but using safe conditional swaps to avoid leaks
markrad 0:cdf462088d13 1654 */
markrad 0:cdf462088d13 1655 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
markrad 0:cdf462088d13 1656 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
markrad 0:cdf462088d13 1657 MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
markrad 0:cdf462088d13 1658 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
markrad 0:cdf462088d13 1659 MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
markrad 0:cdf462088d13 1660 }
markrad 0:cdf462088d13 1661
markrad 0:cdf462088d13 1662 MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
markrad 0:cdf462088d13 1663
markrad 0:cdf462088d13 1664 cleanup:
markrad 0:cdf462088d13 1665 mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
markrad 0:cdf462088d13 1666
markrad 0:cdf462088d13 1667 return( ret );
markrad 0:cdf462088d13 1668 }
markrad 0:cdf462088d13 1669
markrad 0:cdf462088d13 1670 #endif /* ECP_MONTGOMERY */
markrad 0:cdf462088d13 1671
markrad 0:cdf462088d13 1672 /*
markrad 0:cdf462088d13 1673 * Multiplication R = m * P
markrad 0:cdf462088d13 1674 */
markrad 0:cdf462088d13 1675 int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1676 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
markrad 0:cdf462088d13 1677 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
markrad 0:cdf462088d13 1678 {
Jasper Wallace 2:bbdeda018a3c 1679 int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jasper Wallace 2:bbdeda018a3c 1680 #if defined(MBEDTLS_ECP_INTERNAL_ALT)
Jasper Wallace 2:bbdeda018a3c 1681 char is_grp_capable = 0;
Jasper Wallace 2:bbdeda018a3c 1682 #endif
markrad 0:cdf462088d13 1683
markrad 0:cdf462088d13 1684 /* Common sanity checks */
markrad 0:cdf462088d13 1685 if( mbedtls_mpi_cmp_int( &P->Z, 1 ) != 0 )
markrad 0:cdf462088d13 1686 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 1687
markrad 0:cdf462088d13 1688 if( ( ret = mbedtls_ecp_check_privkey( grp, m ) ) != 0 ||
markrad 0:cdf462088d13 1689 ( ret = mbedtls_ecp_check_pubkey( grp, P ) ) != 0 )
markrad 0:cdf462088d13 1690 return( ret );
markrad 0:cdf462088d13 1691
Jasper Wallace 2:bbdeda018a3c 1692 #if defined(MBEDTLS_ECP_INTERNAL_ALT)
Jasper Wallace 2:bbdeda018a3c 1693 if ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 1694 {
Jasper Wallace 2:bbdeda018a3c 1695 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Jasper Wallace 2:bbdeda018a3c 1696 }
Jasper Wallace 2:bbdeda018a3c 1697
Jasper Wallace 2:bbdeda018a3c 1698 #endif /* MBEDTLS_ECP_INTERNAL_ALT */
markrad 0:cdf462088d13 1699 #if defined(ECP_MONTGOMERY)
markrad 0:cdf462088d13 1700 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
Jasper Wallace 2:bbdeda018a3c 1701 ret = ecp_mul_mxz( grp, R, m, P, f_rng, p_rng );
Jasper Wallace 2:bbdeda018a3c 1702
markrad 0:cdf462088d13 1703 #endif
markrad 0:cdf462088d13 1704 #if defined(ECP_SHORTWEIERSTRASS)
markrad 0:cdf462088d13 1705 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
Jasper Wallace 2:bbdeda018a3c 1706 ret = ecp_mul_comb( grp, R, m, P, f_rng, p_rng );
Jasper Wallace 2:bbdeda018a3c 1707
markrad 0:cdf462088d13 1708 #endif
Jasper Wallace 2:bbdeda018a3c 1709 #if defined(MBEDTLS_ECP_INTERNAL_ALT)
Jasper Wallace 2:bbdeda018a3c 1710 cleanup:
Jasper Wallace 2:bbdeda018a3c 1711
Jasper Wallace 2:bbdeda018a3c 1712 if ( is_grp_capable )
Jasper Wallace 2:bbdeda018a3c 1713 {
Jasper Wallace 2:bbdeda018a3c 1714 mbedtls_internal_ecp_free( grp );
Jasper Wallace 2:bbdeda018a3c 1715 }
Jasper Wallace 2:bbdeda018a3c 1716
Jasper Wallace 2:bbdeda018a3c 1717 #endif /* MBEDTLS_ECP_INTERNAL_ALT */
Jasper Wallace 2:bbdeda018a3c 1718 return( ret );
markrad 0:cdf462088d13 1719 }
markrad 0:cdf462088d13 1720
markrad 0:cdf462088d13 1721 #if defined(ECP_SHORTWEIERSTRASS)
markrad 0:cdf462088d13 1722 /*
markrad 0:cdf462088d13 1723 * Check that an affine point is valid as a public key,
markrad 0:cdf462088d13 1724 * short weierstrass curves (SEC1 3.2.3.1)
markrad 0:cdf462088d13 1725 */
markrad 0:cdf462088d13 1726 static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 1727 {
markrad 0:cdf462088d13 1728 int ret;
markrad 0:cdf462088d13 1729 mbedtls_mpi YY, RHS;
markrad 0:cdf462088d13 1730
markrad 0:cdf462088d13 1731 /* pt coordinates must be normalized for our checks */
markrad 0:cdf462088d13 1732 if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
markrad 0:cdf462088d13 1733 mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
markrad 0:cdf462088d13 1734 mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
markrad 0:cdf462088d13 1735 mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
markrad 0:cdf462088d13 1736 return( MBEDTLS_ERR_ECP_INVALID_KEY );
markrad 0:cdf462088d13 1737
markrad 0:cdf462088d13 1738 mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
markrad 0:cdf462088d13 1739
markrad 0:cdf462088d13 1740 /*
markrad 0:cdf462088d13 1741 * YY = Y^2
markrad 0:cdf462088d13 1742 * RHS = X (X^2 + A) + B = X^3 + A X + B
markrad 0:cdf462088d13 1743 */
markrad 0:cdf462088d13 1744 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
markrad 0:cdf462088d13 1745 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
markrad 0:cdf462088d13 1746
markrad 0:cdf462088d13 1747 /* Special case for A = -3 */
markrad 0:cdf462088d13 1748 if( grp->A.p == NULL )
markrad 0:cdf462088d13 1749 {
markrad 0:cdf462088d13 1750 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
markrad 0:cdf462088d13 1751 }
markrad 0:cdf462088d13 1752 else
markrad 0:cdf462088d13 1753 {
markrad 0:cdf462088d13 1754 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
markrad 0:cdf462088d13 1755 }
markrad 0:cdf462088d13 1756
markrad 0:cdf462088d13 1757 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
markrad 0:cdf462088d13 1758 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
markrad 0:cdf462088d13 1759
markrad 0:cdf462088d13 1760 if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
markrad 0:cdf462088d13 1761 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
markrad 0:cdf462088d13 1762
markrad 0:cdf462088d13 1763 cleanup:
markrad 0:cdf462088d13 1764
markrad 0:cdf462088d13 1765 mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
markrad 0:cdf462088d13 1766
markrad 0:cdf462088d13 1767 return( ret );
markrad 0:cdf462088d13 1768 }
markrad 0:cdf462088d13 1769 #endif /* ECP_SHORTWEIERSTRASS */
markrad 0:cdf462088d13 1770
markrad 0:cdf462088d13 1771 /*
markrad 0:cdf462088d13 1772 * R = m * P with shortcuts for m == 1 and m == -1
markrad 0:cdf462088d13 1773 * NOT constant-time - ONLY for short Weierstrass!
markrad 0:cdf462088d13 1774 */
markrad 0:cdf462088d13 1775 static int mbedtls_ecp_mul_shortcuts( mbedtls_ecp_group *grp,
markrad 0:cdf462088d13 1776 mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1777 const mbedtls_mpi *m,
markrad 0:cdf462088d13 1778 const mbedtls_ecp_point *P )
markrad 0:cdf462088d13 1779 {
markrad 0:cdf462088d13 1780 int ret;
markrad 0:cdf462088d13 1781
markrad 0:cdf462088d13 1782 if( mbedtls_mpi_cmp_int( m, 1 ) == 0 )
markrad 0:cdf462088d13 1783 {
markrad 0:cdf462088d13 1784 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
markrad 0:cdf462088d13 1785 }
markrad 0:cdf462088d13 1786 else if( mbedtls_mpi_cmp_int( m, -1 ) == 0 )
markrad 0:cdf462088d13 1787 {
markrad 0:cdf462088d13 1788 MBEDTLS_MPI_CHK( mbedtls_ecp_copy( R, P ) );
markrad 0:cdf462088d13 1789 if( mbedtls_mpi_cmp_int( &R->Y, 0 ) != 0 )
markrad 0:cdf462088d13 1790 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &R->Y, &grp->P, &R->Y ) );
markrad 0:cdf462088d13 1791 }
markrad 0:cdf462088d13 1792 else
markrad 0:cdf462088d13 1793 {
markrad 0:cdf462088d13 1794 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, R, m, P, NULL, NULL ) );
markrad 0:cdf462088d13 1795 }
markrad 0:cdf462088d13 1796
markrad 0:cdf462088d13 1797 cleanup:
markrad 0:cdf462088d13 1798 return( ret );
markrad 0:cdf462088d13 1799 }
markrad 0:cdf462088d13 1800
markrad 0:cdf462088d13 1801 /*
markrad 0:cdf462088d13 1802 * Linear combination
markrad 0:cdf462088d13 1803 * NOT constant-time
markrad 0:cdf462088d13 1804 */
markrad 0:cdf462088d13 1805 int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
markrad 0:cdf462088d13 1806 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
markrad 0:cdf462088d13 1807 const mbedtls_mpi *n, const mbedtls_ecp_point *Q )
markrad 0:cdf462088d13 1808 {
markrad 0:cdf462088d13 1809 int ret;
markrad 0:cdf462088d13 1810 mbedtls_ecp_point mP;
Jasper Wallace 2:bbdeda018a3c 1811 #if defined(MBEDTLS_ECP_INTERNAL_ALT)
Jasper Wallace 2:bbdeda018a3c 1812 char is_grp_capable = 0;
Jasper Wallace 2:bbdeda018a3c 1813 #endif
markrad 0:cdf462088d13 1814
markrad 0:cdf462088d13 1815 if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS )
markrad 0:cdf462088d13 1816 return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
markrad 0:cdf462088d13 1817
markrad 0:cdf462088d13 1818 mbedtls_ecp_point_init( &mP );
markrad 0:cdf462088d13 1819
markrad 0:cdf462088d13 1820 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, &mP, m, P ) );
markrad 0:cdf462088d13 1821 MBEDTLS_MPI_CHK( mbedtls_ecp_mul_shortcuts( grp, R, n, Q ) );
markrad 0:cdf462088d13 1822
Jasper Wallace 2:bbdeda018a3c 1823 #if defined(MBEDTLS_ECP_INTERNAL_ALT)
Jasper Wallace 2:bbdeda018a3c 1824 if ( is_grp_capable = mbedtls_internal_ecp_grp_capable( grp ) )
Jasper Wallace 2:bbdeda018a3c 1825 {
Jasper Wallace 2:bbdeda018a3c 1826 MBEDTLS_MPI_CHK( mbedtls_internal_ecp_init( grp ) );
Jasper Wallace 2:bbdeda018a3c 1827 }
Jasper Wallace 2:bbdeda018a3c 1828
Jasper Wallace 2:bbdeda018a3c 1829 #endif /* MBEDTLS_ECP_INTERNAL_ALT */
markrad 0:cdf462088d13 1830 MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, &mP, R ) );
markrad 0:cdf462088d13 1831 MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
markrad 0:cdf462088d13 1832
markrad 0:cdf462088d13 1833 cleanup:
Jasper Wallace 2:bbdeda018a3c 1834
Jasper Wallace 2:bbdeda018a3c 1835 #if defined(MBEDTLS_ECP_INTERNAL_ALT)
Jasper Wallace 2:bbdeda018a3c 1836 if ( is_grp_capable )
Jasper Wallace 2:bbdeda018a3c 1837 {
Jasper Wallace 2:bbdeda018a3c 1838 mbedtls_internal_ecp_free( grp );
Jasper Wallace 2:bbdeda018a3c 1839 }
Jasper Wallace 2:bbdeda018a3c 1840
Jasper Wallace 2:bbdeda018a3c 1841 #endif /* MBEDTLS_ECP_INTERNAL_ALT */
markrad 0:cdf462088d13 1842 mbedtls_ecp_point_free( &mP );
markrad 0:cdf462088d13 1843
markrad 0:cdf462088d13 1844 return( ret );
markrad 0:cdf462088d13 1845 }
markrad 0:cdf462088d13 1846
markrad 0:cdf462088d13 1847
markrad 0:cdf462088d13 1848 #if defined(ECP_MONTGOMERY)
markrad 0:cdf462088d13 1849 /*
markrad 0:cdf462088d13 1850 * Check validity of a public key for Montgomery curves with x-only schemes
markrad 0:cdf462088d13 1851 */
markrad 0:cdf462088d13 1852 static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 1853 {
markrad 0:cdf462088d13 1854 /* [Curve25519 p. 5] Just check X is the correct number of bytes */
markrad 0:cdf462088d13 1855 if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
markrad 0:cdf462088d13 1856 return( MBEDTLS_ERR_ECP_INVALID_KEY );
markrad 0:cdf462088d13 1857
markrad 0:cdf462088d13 1858 return( 0 );
markrad 0:cdf462088d13 1859 }
markrad 0:cdf462088d13 1860 #endif /* ECP_MONTGOMERY */
markrad 0:cdf462088d13 1861
markrad 0:cdf462088d13 1862 /*
markrad 0:cdf462088d13 1863 * Check that a point is valid as a public key
markrad 0:cdf462088d13 1864 */
markrad 0:cdf462088d13 1865 int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
markrad 0:cdf462088d13 1866 {
markrad 0:cdf462088d13 1867 /* Must use affine coordinates */
markrad 0:cdf462088d13 1868 if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
markrad 0:cdf462088d13 1869 return( MBEDTLS_ERR_ECP_INVALID_KEY );
markrad 0:cdf462088d13 1870
markrad 0:cdf462088d13 1871 #if defined(ECP_MONTGOMERY)
markrad 0:cdf462088d13 1872 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
markrad 0:cdf462088d13 1873 return( ecp_check_pubkey_mx( grp, pt ) );
markrad 0:cdf462088d13 1874 #endif
markrad 0:cdf462088d13 1875 #if defined(ECP_SHORTWEIERSTRASS)
markrad 0:cdf462088d13 1876 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
markrad 0:cdf462088d13 1877 return( ecp_check_pubkey_sw( grp, pt ) );
markrad 0:cdf462088d13 1878 #endif
markrad 0:cdf462088d13 1879 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 1880 }
markrad 0:cdf462088d13 1881
markrad 0:cdf462088d13 1882 /*
markrad 0:cdf462088d13 1883 * Check that an mbedtls_mpi is valid as a private key
markrad 0:cdf462088d13 1884 */
markrad 0:cdf462088d13 1885 int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d )
markrad 0:cdf462088d13 1886 {
markrad 0:cdf462088d13 1887 #if defined(ECP_MONTGOMERY)
markrad 0:cdf462088d13 1888 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
markrad 0:cdf462088d13 1889 {
markrad 0:cdf462088d13 1890 /* see [Curve25519] page 5 */
markrad 0:cdf462088d13 1891 if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
markrad 0:cdf462088d13 1892 mbedtls_mpi_get_bit( d, 1 ) != 0 ||
markrad 0:cdf462088d13 1893 mbedtls_mpi_get_bit( d, 2 ) != 0 ||
markrad 0:cdf462088d13 1894 mbedtls_mpi_bitlen( d ) - 1 != grp->nbits ) /* mbedtls_mpi_bitlen is one-based! */
markrad 0:cdf462088d13 1895 return( MBEDTLS_ERR_ECP_INVALID_KEY );
markrad 0:cdf462088d13 1896 else
markrad 0:cdf462088d13 1897 return( 0 );
markrad 0:cdf462088d13 1898 }
markrad 0:cdf462088d13 1899 #endif /* ECP_MONTGOMERY */
markrad 0:cdf462088d13 1900 #if defined(ECP_SHORTWEIERSTRASS)
markrad 0:cdf462088d13 1901 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
markrad 0:cdf462088d13 1902 {
markrad 0:cdf462088d13 1903 /* see SEC1 3.2 */
markrad 0:cdf462088d13 1904 if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
markrad 0:cdf462088d13 1905 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
markrad 0:cdf462088d13 1906 return( MBEDTLS_ERR_ECP_INVALID_KEY );
markrad 0:cdf462088d13 1907 else
markrad 0:cdf462088d13 1908 return( 0 );
markrad 0:cdf462088d13 1909 }
markrad 0:cdf462088d13 1910 #endif /* ECP_SHORTWEIERSTRASS */
markrad 0:cdf462088d13 1911
markrad 0:cdf462088d13 1912 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 1913 }
markrad 0:cdf462088d13 1914
markrad 0:cdf462088d13 1915 /*
markrad 0:cdf462088d13 1916 * Generate a keypair with configurable base point
markrad 0:cdf462088d13 1917 */
markrad 0:cdf462088d13 1918 int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
markrad 0:cdf462088d13 1919 const mbedtls_ecp_point *G,
markrad 0:cdf462088d13 1920 mbedtls_mpi *d, mbedtls_ecp_point *Q,
markrad 0:cdf462088d13 1921 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 1922 void *p_rng )
markrad 0:cdf462088d13 1923 {
markrad 0:cdf462088d13 1924 int ret;
markrad 0:cdf462088d13 1925 size_t n_size = ( grp->nbits + 7 ) / 8;
markrad 0:cdf462088d13 1926
markrad 0:cdf462088d13 1927 #if defined(ECP_MONTGOMERY)
markrad 0:cdf462088d13 1928 if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY )
markrad 0:cdf462088d13 1929 {
markrad 0:cdf462088d13 1930 /* [M225] page 5 */
markrad 0:cdf462088d13 1931 size_t b;
markrad 0:cdf462088d13 1932
markrad 0:cdf462088d13 1933 do {
markrad 0:cdf462088d13 1934 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
markrad 0:cdf462088d13 1935 } while( mbedtls_mpi_bitlen( d ) == 0);
markrad 0:cdf462088d13 1936
markrad 0:cdf462088d13 1937 /* Make sure the most significant bit is nbits */
markrad 0:cdf462088d13 1938 b = mbedtls_mpi_bitlen( d ) - 1; /* mbedtls_mpi_bitlen is one-based */
markrad 0:cdf462088d13 1939 if( b > grp->nbits )
markrad 0:cdf462088d13 1940 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
markrad 0:cdf462088d13 1941 else
markrad 0:cdf462088d13 1942 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
markrad 0:cdf462088d13 1943
markrad 0:cdf462088d13 1944 /* Make sure the last three bits are unset */
markrad 0:cdf462088d13 1945 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
markrad 0:cdf462088d13 1946 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
markrad 0:cdf462088d13 1947 MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
markrad 0:cdf462088d13 1948 }
markrad 0:cdf462088d13 1949 else
markrad 0:cdf462088d13 1950 #endif /* ECP_MONTGOMERY */
markrad 0:cdf462088d13 1951 #if defined(ECP_SHORTWEIERSTRASS)
markrad 0:cdf462088d13 1952 if( ecp_get_type( grp ) == ECP_TYPE_SHORT_WEIERSTRASS )
markrad 0:cdf462088d13 1953 {
markrad 0:cdf462088d13 1954 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
markrad 0:cdf462088d13 1955 int count = 0;
markrad 0:cdf462088d13 1956 unsigned char rnd[MBEDTLS_ECP_MAX_BYTES];
markrad 0:cdf462088d13 1957
markrad 0:cdf462088d13 1958 /*
markrad 0:cdf462088d13 1959 * Match the procedure given in RFC 6979 (deterministic ECDSA):
markrad 0:cdf462088d13 1960 * - use the same byte ordering;
markrad 0:cdf462088d13 1961 * - keep the leftmost nbits bits of the generated octet string;
markrad 0:cdf462088d13 1962 * - try until result is in the desired range.
markrad 0:cdf462088d13 1963 * This also avoids any biais, which is especially important for ECDSA.
markrad 0:cdf462088d13 1964 */
markrad 0:cdf462088d13 1965 do
markrad 0:cdf462088d13 1966 {
markrad 0:cdf462088d13 1967 MBEDTLS_MPI_CHK( f_rng( p_rng, rnd, n_size ) );
markrad 0:cdf462088d13 1968 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( d, rnd, n_size ) );
markrad 0:cdf462088d13 1969 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
markrad 0:cdf462088d13 1970
markrad 0:cdf462088d13 1971 /*
markrad 0:cdf462088d13 1972 * Each try has at worst a probability 1/2 of failing (the msb has
markrad 0:cdf462088d13 1973 * a probability 1/2 of being 0, and then the result will be < N),
markrad 0:cdf462088d13 1974 * so after 30 tries failure probability is a most 2**(-30).
markrad 0:cdf462088d13 1975 *
markrad 0:cdf462088d13 1976 * For most curves, 1 try is enough with overwhelming probability,
markrad 0:cdf462088d13 1977 * since N starts with a lot of 1s in binary, but some curves
markrad 0:cdf462088d13 1978 * such as secp224k1 are actually very close to the worst case.
markrad 0:cdf462088d13 1979 */
markrad 0:cdf462088d13 1980 if( ++count > 30 )
markrad 0:cdf462088d13 1981 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
markrad 0:cdf462088d13 1982 }
markrad 0:cdf462088d13 1983 while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
markrad 0:cdf462088d13 1984 mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
markrad 0:cdf462088d13 1985 }
markrad 0:cdf462088d13 1986 else
markrad 0:cdf462088d13 1987 #endif /* ECP_SHORTWEIERSTRASS */
markrad 0:cdf462088d13 1988 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 1989
markrad 0:cdf462088d13 1990 cleanup:
markrad 0:cdf462088d13 1991 if( ret != 0 )
markrad 0:cdf462088d13 1992 return( ret );
markrad 0:cdf462088d13 1993
markrad 0:cdf462088d13 1994 return( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) );
markrad 0:cdf462088d13 1995 }
markrad 0:cdf462088d13 1996
markrad 0:cdf462088d13 1997 /*
markrad 0:cdf462088d13 1998 * Generate key pair, wrapper for conventional base point
markrad 0:cdf462088d13 1999 */
markrad 0:cdf462088d13 2000 int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp,
markrad 0:cdf462088d13 2001 mbedtls_mpi *d, mbedtls_ecp_point *Q,
markrad 0:cdf462088d13 2002 int (*f_rng)(void *, unsigned char *, size_t),
markrad 0:cdf462088d13 2003 void *p_rng )
markrad 0:cdf462088d13 2004 {
markrad 0:cdf462088d13 2005 return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) );
markrad 0:cdf462088d13 2006 }
markrad 0:cdf462088d13 2007
markrad 0:cdf462088d13 2008 /*
markrad 0:cdf462088d13 2009 * Generate a keypair, prettier wrapper
markrad 0:cdf462088d13 2010 */
markrad 0:cdf462088d13 2011 int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
markrad 0:cdf462088d13 2012 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
markrad 0:cdf462088d13 2013 {
markrad 0:cdf462088d13 2014 int ret;
markrad 0:cdf462088d13 2015
markrad 0:cdf462088d13 2016 if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 )
markrad 0:cdf462088d13 2017 return( ret );
markrad 0:cdf462088d13 2018
markrad 0:cdf462088d13 2019 return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
markrad 0:cdf462088d13 2020 }
markrad 0:cdf462088d13 2021
markrad 0:cdf462088d13 2022 /*
markrad 0:cdf462088d13 2023 * Check a public-private key pair
markrad 0:cdf462088d13 2024 */
markrad 0:cdf462088d13 2025 int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
markrad 0:cdf462088d13 2026 {
markrad 0:cdf462088d13 2027 int ret;
markrad 0:cdf462088d13 2028 mbedtls_ecp_point Q;
markrad 0:cdf462088d13 2029 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 2030
markrad 0:cdf462088d13 2031 if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
markrad 0:cdf462088d13 2032 pub->grp.id != prv->grp.id ||
markrad 0:cdf462088d13 2033 mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
markrad 0:cdf462088d13 2034 mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
markrad 0:cdf462088d13 2035 mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
markrad 0:cdf462088d13 2036 {
markrad 0:cdf462088d13 2037 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
markrad 0:cdf462088d13 2038 }
markrad 0:cdf462088d13 2039
markrad 0:cdf462088d13 2040 mbedtls_ecp_point_init( &Q );
markrad 0:cdf462088d13 2041 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 2042
markrad 0:cdf462088d13 2043 /* mbedtls_ecp_mul() needs a non-const group... */
markrad 0:cdf462088d13 2044 mbedtls_ecp_group_copy( &grp, &prv->grp );
markrad 0:cdf462088d13 2045
markrad 0:cdf462088d13 2046 /* Also checks d is valid */
markrad 0:cdf462088d13 2047 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
markrad 0:cdf462088d13 2048
markrad 0:cdf462088d13 2049 if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
markrad 0:cdf462088d13 2050 mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
markrad 0:cdf462088d13 2051 mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
markrad 0:cdf462088d13 2052 {
markrad 0:cdf462088d13 2053 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
markrad 0:cdf462088d13 2054 goto cleanup;
markrad 0:cdf462088d13 2055 }
markrad 0:cdf462088d13 2056
markrad 0:cdf462088d13 2057 cleanup:
markrad 0:cdf462088d13 2058 mbedtls_ecp_point_free( &Q );
markrad 0:cdf462088d13 2059 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 2060
markrad 0:cdf462088d13 2061 return( ret );
markrad 0:cdf462088d13 2062 }
markrad 0:cdf462088d13 2063
markrad 0:cdf462088d13 2064 #if defined(MBEDTLS_SELF_TEST)
markrad 0:cdf462088d13 2065
markrad 0:cdf462088d13 2066 /*
markrad 0:cdf462088d13 2067 * Checkup routine
markrad 0:cdf462088d13 2068 */
markrad 0:cdf462088d13 2069 int mbedtls_ecp_self_test( int verbose )
markrad 0:cdf462088d13 2070 {
markrad 0:cdf462088d13 2071 int ret;
markrad 0:cdf462088d13 2072 size_t i;
markrad 0:cdf462088d13 2073 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 2074 mbedtls_ecp_point R, P;
markrad 0:cdf462088d13 2075 mbedtls_mpi m;
markrad 0:cdf462088d13 2076 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
markrad 0:cdf462088d13 2077 /* exponents especially adapted for secp192r1 */
markrad 0:cdf462088d13 2078 const char *exponents[] =
markrad 0:cdf462088d13 2079 {
markrad 0:cdf462088d13 2080 "000000000000000000000000000000000000000000000001", /* one */
markrad 0:cdf462088d13 2081 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
markrad 0:cdf462088d13 2082 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
markrad 0:cdf462088d13 2083 "400000000000000000000000000000000000000000000000", /* one and zeros */
markrad 0:cdf462088d13 2084 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
markrad 0:cdf462088d13 2085 "555555555555555555555555555555555555555555555555", /* 101010... */
markrad 0:cdf462088d13 2086 };
markrad 0:cdf462088d13 2087
markrad 0:cdf462088d13 2088 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 2089 mbedtls_ecp_point_init( &R );
markrad 0:cdf462088d13 2090 mbedtls_ecp_point_init( &P );
markrad 0:cdf462088d13 2091 mbedtls_mpi_init( &m );
markrad 0:cdf462088d13 2092
markrad 0:cdf462088d13 2093 /* Use secp192r1 if available, or any available curve */
markrad 0:cdf462088d13 2094 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
markrad 0:cdf462088d13 2095 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
markrad 0:cdf462088d13 2096 #else
markrad 0:cdf462088d13 2097 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &grp, mbedtls_ecp_curve_list()->grp_id ) );
markrad 0:cdf462088d13 2098 #endif
markrad 0:cdf462088d13 2099
markrad 0:cdf462088d13 2100 if( verbose != 0 )
markrad 0:cdf462088d13 2101 mbedtls_printf( " ECP test #1 (constant op_count, base point G): " );
markrad 0:cdf462088d13 2102
markrad 0:cdf462088d13 2103 /* Do a dummy multiplication first to trigger precomputation */
markrad 0:cdf462088d13 2104 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
markrad 0:cdf462088d13 2105 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
markrad 0:cdf462088d13 2106
markrad 0:cdf462088d13 2107 add_count = 0;
markrad 0:cdf462088d13 2108 dbl_count = 0;
markrad 0:cdf462088d13 2109 mul_count = 0;
markrad 0:cdf462088d13 2110 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
markrad 0:cdf462088d13 2111 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
markrad 0:cdf462088d13 2112
markrad 0:cdf462088d13 2113 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
markrad 0:cdf462088d13 2114 {
markrad 0:cdf462088d13 2115 add_c_prev = add_count;
markrad 0:cdf462088d13 2116 dbl_c_prev = dbl_count;
markrad 0:cdf462088d13 2117 mul_c_prev = mul_count;
markrad 0:cdf462088d13 2118 add_count = 0;
markrad 0:cdf462088d13 2119 dbl_count = 0;
markrad 0:cdf462088d13 2120 mul_count = 0;
markrad 0:cdf462088d13 2121
markrad 0:cdf462088d13 2122 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
markrad 0:cdf462088d13 2123 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
markrad 0:cdf462088d13 2124
markrad 0:cdf462088d13 2125 if( add_count != add_c_prev ||
markrad 0:cdf462088d13 2126 dbl_count != dbl_c_prev ||
markrad 0:cdf462088d13 2127 mul_count != mul_c_prev )
markrad 0:cdf462088d13 2128 {
markrad 0:cdf462088d13 2129 if( verbose != 0 )
markrad 0:cdf462088d13 2130 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
markrad 0:cdf462088d13 2131
markrad 0:cdf462088d13 2132 ret = 1;
markrad 0:cdf462088d13 2133 goto cleanup;
markrad 0:cdf462088d13 2134 }
markrad 0:cdf462088d13 2135 }
markrad 0:cdf462088d13 2136
markrad 0:cdf462088d13 2137 if( verbose != 0 )
markrad 0:cdf462088d13 2138 mbedtls_printf( "passed\n" );
markrad 0:cdf462088d13 2139
markrad 0:cdf462088d13 2140 if( verbose != 0 )
markrad 0:cdf462088d13 2141 mbedtls_printf( " ECP test #2 (constant op_count, other point): " );
markrad 0:cdf462088d13 2142 /* We computed P = 2G last time, use it */
markrad 0:cdf462088d13 2143
markrad 0:cdf462088d13 2144 add_count = 0;
markrad 0:cdf462088d13 2145 dbl_count = 0;
markrad 0:cdf462088d13 2146 mul_count = 0;
markrad 0:cdf462088d13 2147 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
markrad 0:cdf462088d13 2148 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
markrad 0:cdf462088d13 2149
markrad 0:cdf462088d13 2150 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
markrad 0:cdf462088d13 2151 {
markrad 0:cdf462088d13 2152 add_c_prev = add_count;
markrad 0:cdf462088d13 2153 dbl_c_prev = dbl_count;
markrad 0:cdf462088d13 2154 mul_c_prev = mul_count;
markrad 0:cdf462088d13 2155 add_count = 0;
markrad 0:cdf462088d13 2156 dbl_count = 0;
markrad 0:cdf462088d13 2157 mul_count = 0;
markrad 0:cdf462088d13 2158
markrad 0:cdf462088d13 2159 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
markrad 0:cdf462088d13 2160 MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
markrad 0:cdf462088d13 2161
markrad 0:cdf462088d13 2162 if( add_count != add_c_prev ||
markrad 0:cdf462088d13 2163 dbl_count != dbl_c_prev ||
markrad 0:cdf462088d13 2164 mul_count != mul_c_prev )
markrad 0:cdf462088d13 2165 {
markrad 0:cdf462088d13 2166 if( verbose != 0 )
markrad 0:cdf462088d13 2167 mbedtls_printf( "failed (%u)\n", (unsigned int) i );
markrad 0:cdf462088d13 2168
markrad 0:cdf462088d13 2169 ret = 1;
markrad 0:cdf462088d13 2170 goto cleanup;
markrad 0:cdf462088d13 2171 }
markrad 0:cdf462088d13 2172 }
markrad 0:cdf462088d13 2173
markrad 0:cdf462088d13 2174 if( verbose != 0 )
markrad 0:cdf462088d13 2175 mbedtls_printf( "passed\n" );
markrad 0:cdf462088d13 2176
markrad 0:cdf462088d13 2177 cleanup:
markrad 0:cdf462088d13 2178
markrad 0:cdf462088d13 2179 if( ret < 0 && verbose != 0 )
markrad 0:cdf462088d13 2180 mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
markrad 0:cdf462088d13 2181
markrad 0:cdf462088d13 2182 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 2183 mbedtls_ecp_point_free( &R );
markrad 0:cdf462088d13 2184 mbedtls_ecp_point_free( &P );
markrad 0:cdf462088d13 2185 mbedtls_mpi_free( &m );
markrad 0:cdf462088d13 2186
markrad 0:cdf462088d13 2187 if( verbose != 0 )
markrad 0:cdf462088d13 2188 mbedtls_printf( "\n" );
markrad 0:cdf462088d13 2189
markrad 0:cdf462088d13 2190 return( ret );
markrad 0:cdf462088d13 2191 }
markrad 0:cdf462088d13 2192
markrad 0:cdf462088d13 2193 #endif /* MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 2194
Jasper Wallace 2:bbdeda018a3c 2195 #endif /* !MBEDTLS_ECP_ALT */
Jasper Wallace 2:bbdeda018a3c 2196
markrad 0:cdf462088d13 2197 #endif /* MBEDTLS_ECP_C */