Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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