mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Brian Daniels
Date:
Thu Apr 07 11:11:18 2016 +0100
Revision:
4:bef26f687287
Parent:
1:24750b9ad5ef
Adding ported selftest test case

Who changed what in which revision?

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