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