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