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