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