Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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