mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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