Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Elliptic curves over GF(p) |
| 3 | * |
Paul Bakker | cf4365f | 2013-01-16 17:00:43 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * References: |
| 28 | * |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 29 | * SEC1 http://www.secg.org/index.php?action=secg,docs_secg |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 30 | * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 31 | * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 32 | * RFC 4492 for the related TLS structures and constants |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 33 | * |
| 34 | * [1] OKEYA, Katsuyuki and TAKAGI, Tsuyoshi. The width-w NAF method provides |
| 35 | * small memory and fast elliptic scalar multiplications secure against |
| 36 | * side channel attacks. In : Topics in Cryptology—CT-RSA 2003. Springer |
| 37 | * Berlin Heidelberg, 2003. p. 328-343. |
| 38 | * <http://rd.springer.com/chapter/10.1007/3-540-36563-X_23>. |
| 39 | * |
| 40 | * [2] CORON, Jean-Sébastien. Resistance against differential power analysis |
| 41 | * for elliptic curve cryptosystems. In : Cryptographic Hardware and |
| 42 | * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302. |
| 43 | * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | #include "polarssl/config.h" |
| 47 | |
| 48 | #if defined(POLARSSL_ECP_C) |
| 49 | |
| 50 | #include "polarssl/ecp.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 51 | |
| 52 | #if defined(POLARSSL_MEMORY_C) |
| 53 | #include "polarssl/memory.h" |
| 54 | #else |
| 55 | #define polarssl_malloc malloc |
| 56 | #define polarssl_free free |
| 57 | #endif |
| 58 | |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 59 | #include <limits.h> |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 60 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 62 | #if defined(POLARSSL_SELF_TEST) |
| 63 | /* |
| 64 | * Counts of point addition and doubling operations. |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 65 | * 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] | 66 | */ |
| 67 | unsigned long add_count, dbl_count; |
| 68 | #endif |
| 69 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 70 | /* |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 71 | * List of supported curves: |
| 72 | * - internal ID |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 73 | * - TLS NamedCurve ID (RFC 4492 section 5.1.1) |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 74 | * - size in bits |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 75 | * - readeble name |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 76 | */ |
Manuel Pégourié-Gonnard | a79d123 | 2013-09-17 15:42:35 +0200 | [diff] [blame] | 77 | const ecp_curve_info ecp_supported_curves[] = |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 78 | { |
| 79 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 80 | { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 81 | #endif |
| 82 | #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 83 | { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 84 | #endif |
| 85 | #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 86 | { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 87 | #endif |
| 88 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 89 | { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 90 | #endif |
| 91 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 92 | { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 93 | #endif |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 94 | { POLARSSL_ECP_DP_NONE, 0, 0, NULL }, |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /* |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 98 | * Initialize (the components of) a point |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 99 | */ |
| 100 | void ecp_point_init( ecp_point *pt ) |
| 101 | { |
| 102 | if( pt == NULL ) |
| 103 | return; |
| 104 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 105 | mpi_init( &pt->X ); |
| 106 | mpi_init( &pt->Y ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 107 | mpi_init( &pt->Z ); |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Initialize (the components of) a group |
| 112 | */ |
| 113 | void ecp_group_init( ecp_group *grp ) |
| 114 | { |
| 115 | if( grp == NULL ) |
| 116 | return; |
| 117 | |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 118 | memset( grp, 0, sizeof( ecp_group ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 122 | * Initialize (the components of) a key pair |
| 123 | */ |
| 124 | void ecp_keypair_init( ecp_keypair *key ) |
| 125 | { |
| 126 | if ( key == NULL ) |
| 127 | return; |
| 128 | |
| 129 | ecp_group_init( &key->grp ); |
| 130 | mpi_init( &key->d ); |
| 131 | ecp_point_init( &key->Q ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 135 | * Unallocate (the components of) a point |
| 136 | */ |
| 137 | void ecp_point_free( ecp_point *pt ) |
| 138 | { |
| 139 | if( pt == NULL ) |
| 140 | return; |
| 141 | |
| 142 | mpi_free( &( pt->X ) ); |
| 143 | mpi_free( &( pt->Y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 144 | mpi_free( &( pt->Z ) ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Unallocate (the components of) a group |
| 149 | */ |
| 150 | void ecp_group_free( ecp_group *grp ) |
| 151 | { |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 152 | size_t i; |
| 153 | |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 154 | if( grp == NULL ) |
| 155 | return; |
| 156 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 157 | mpi_free( &grp->P ); |
| 158 | mpi_free( &grp->B ); |
| 159 | ecp_point_free( &grp->G ); |
| 160 | mpi_free( &grp->N ); |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 161 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 162 | if( grp->T != NULL ) |
| 163 | { |
| 164 | for( i = 0; i < grp->T_size; i++ ) |
| 165 | ecp_point_free( &grp->T[i] ); |
| 166 | polarssl_free( grp->T ); |
| 167 | } |
| 168 | |
Manuel Pégourié-Gonnard | c972770 | 2013-09-16 18:56:28 +0200 | [diff] [blame] | 169 | memset( grp, 0, sizeof( ecp_group ) ); |
Manuel Pégourié-Gonnard | 1e8c8ec | 2012-10-31 19:24:21 +0100 | [diff] [blame] | 170 | } |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 171 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 172 | /* |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 173 | * Unallocate (the components of) a key pair |
| 174 | */ |
| 175 | void ecp_keypair_free( ecp_keypair *key ) |
| 176 | { |
| 177 | if ( key == NULL ) |
| 178 | return; |
| 179 | |
| 180 | ecp_group_free( &key->grp ); |
| 181 | mpi_free( &key->d ); |
| 182 | ecp_point_free( &key->Q ); |
Manuel Pégourié-Gonnard | b8c6e0e | 2013-07-01 13:40:52 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 186 | * Set point to zero |
| 187 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 188 | int ecp_set_zero( ecp_point *pt ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 189 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 190 | int ret; |
| 191 | |
| 192 | MPI_CHK( mpi_lset( &pt->X , 1 ) ); |
| 193 | MPI_CHK( mpi_lset( &pt->Y , 1 ) ); |
| 194 | MPI_CHK( mpi_lset( &pt->Z , 0 ) ); |
| 195 | |
| 196 | cleanup: |
| 197 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
Manuel Pégourié-Gonnard | 6545ca7 | 2013-01-26 16:05:22 +0100 | [diff] [blame] | 201 | * Tell if a point is zero |
| 202 | */ |
| 203 | int ecp_is_zero( ecp_point *pt ) |
| 204 | { |
| 205 | return( mpi_cmp_int( &pt->Z, 0 ) == 0 ); |
| 206 | } |
| 207 | |
| 208 | /* |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 209 | * Copy the contents of Q into P |
| 210 | */ |
| 211 | int ecp_copy( ecp_point *P, const ecp_point *Q ) |
| 212 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 213 | int ret; |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 214 | |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 215 | MPI_CHK( mpi_copy( &P->X, &Q->X ) ); |
| 216 | MPI_CHK( mpi_copy( &P->Y, &Q->Y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 217 | MPI_CHK( mpi_copy( &P->Z, &Q->Z ) ); |
Manuel Pégourié-Gonnard | 883f313 | 2012-11-02 09:40:25 +0100 | [diff] [blame] | 218 | |
| 219 | cleanup: |
| 220 | return( ret ); |
| 221 | } |
Manuel Pégourié-Gonnard | 5179e46 | 2012-10-31 19:37:54 +0100 | [diff] [blame] | 222 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 223 | /* |
Manuel Pégourié-Gonnard | e09631b | 2013-08-12 15:44:31 +0200 | [diff] [blame] | 224 | * Copy the contents of a group object |
| 225 | */ |
| 226 | int ecp_group_copy( ecp_group *dst, const ecp_group *src ) |
| 227 | { |
| 228 | return ecp_use_known_dp( dst, src->id ); |
| 229 | } |
| 230 | |
| 231 | /* |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 232 | * Import a non-zero point from ASCII strings |
| 233 | */ |
| 234 | int ecp_point_read_string( ecp_point *P, int radix, |
| 235 | const char *x, const char *y ) |
| 236 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 237 | int ret; |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 239 | MPI_CHK( mpi_read_string( &P->X, radix, x ) ); |
| 240 | MPI_CHK( mpi_read_string( &P->Y, radix, y ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 241 | MPI_CHK( mpi_lset( &P->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 242 | |
| 243 | cleanup: |
| 244 | return( ret ); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Import an ECP group from ASCII strings |
| 249 | */ |
| 250 | int ecp_group_read_string( ecp_group *grp, int radix, |
| 251 | const char *p, const char *b, |
| 252 | const char *gx, const char *gy, const char *n) |
| 253 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 254 | int ret; |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 255 | |
| 256 | MPI_CHK( mpi_read_string( &grp->P, radix, p ) ); |
| 257 | MPI_CHK( mpi_read_string( &grp->B, radix, b ) ); |
| 258 | MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) ); |
| 259 | MPI_CHK( mpi_read_string( &grp->N, radix, n ) ); |
| 260 | |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 261 | grp->pbits = mpi_msb( &grp->P ); |
| 262 | grp->nbits = mpi_msb( &grp->N ); |
| 263 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 264 | cleanup: |
| 265 | return( ret ); |
| 266 | } |
| 267 | |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 268 | /* |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 269 | * 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] | 270 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 271 | 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] | 272 | int format, size_t *olen, |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 273 | unsigned char *buf, size_t buflen ) |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 274 | { |
Paul Bakker | a280d0f | 2013-04-08 13:40:17 +0200 | [diff] [blame] | 275 | int ret = 0; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 276 | size_t plen; |
| 277 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 278 | if( format != POLARSSL_ECP_PF_UNCOMPRESSED && |
| 279 | format != POLARSSL_ECP_PF_COMPRESSED ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 280 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 281 | |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 282 | /* |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 283 | * Common case: P == 0 |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 284 | */ |
| 285 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 286 | { |
| 287 | if( buflen < 1 ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 288 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 289 | |
| 290 | buf[0] = 0x00; |
| 291 | *olen = 1; |
| 292 | |
| 293 | return( 0 ); |
| 294 | } |
| 295 | |
| 296 | plen = mpi_size( &grp->P ); |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 297 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 298 | if( format == POLARSSL_ECP_PF_UNCOMPRESSED ) |
| 299 | { |
| 300 | *olen = 2 * plen + 1; |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 301 | |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 302 | if( buflen < *olen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 303 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 304 | |
| 305 | buf[0] = 0x04; |
| 306 | MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 307 | MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) ); |
| 308 | } |
| 309 | else if( format == POLARSSL_ECP_PF_COMPRESSED ) |
| 310 | { |
| 311 | *olen = plen + 1; |
| 312 | |
| 313 | if( buflen < *olen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 314 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | 37d218a | 2012-11-24 15:19:55 +0100 | [diff] [blame] | 315 | |
| 316 | buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 ); |
| 317 | MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) ); |
| 318 | } |
Manuel Pégourié-Gonnard | e19feb5 | 2012-11-24 14:10:14 +0100 | [diff] [blame] | 319 | |
| 320 | cleanup: |
| 321 | return( ret ); |
| 322 | } |
| 323 | |
| 324 | /* |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 325 | * Import a point from unsigned binary data (SEC1 2.3.4) |
| 326 | */ |
Manuel Pégourié-Gonnard | 7e86025 | 2013-02-10 10:58:48 +0100 | [diff] [blame] | 327 | int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt, |
| 328 | const unsigned char *buf, size_t ilen ) { |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 329 | int ret; |
| 330 | size_t plen; |
| 331 | |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 332 | if( ilen == 1 && buf[0] == 0x00 ) |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 333 | return( ecp_set_zero( pt ) ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 334 | |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 335 | plen = mpi_size( &grp->P ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 336 | |
| 337 | if( ilen != 2 * plen + 1 || buf[0] != 0x04 ) |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 338 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 339 | |
Manuel Pégourié-Gonnard | d84895d | 2013-02-10 10:53:04 +0100 | [diff] [blame] | 340 | MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) ); |
| 341 | MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) ); |
| 342 | MPI_CHK( mpi_lset( &pt->Z, 1 ) ); |
Manuel Pégourié-Gonnard | 5e402d8 | 2012-11-24 16:19:42 +0100 | [diff] [blame] | 343 | |
| 344 | cleanup: |
| 345 | return( ret ); |
| 346 | } |
| 347 | |
| 348 | /* |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 349 | * Import a point from a TLS ECPoint record (RFC 4492) |
| 350 | * struct { |
| 351 | * opaque point <1..2^8-1>; |
| 352 | * } ECPoint; |
| 353 | */ |
| 354 | 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] | 355 | const unsigned char **buf, size_t buf_len ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 356 | { |
| 357 | unsigned char data_len; |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 358 | const unsigned char *buf_start; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * We must have at least two bytes (1 for length, at least of for data) |
| 362 | */ |
| 363 | if( buf_len < 2 ) |
| 364 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 365 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 366 | data_len = *(*buf)++; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 367 | if( data_len < 1 || data_len > buf_len - 1 ) |
| 368 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 369 | |
Manuel Pégourié-Gonnard | 98f5181 | 2013-02-10 13:38:29 +0100 | [diff] [blame] | 370 | /* |
| 371 | * Save buffer start for read_binary and update buf |
| 372 | */ |
| 373 | buf_start = *buf; |
| 374 | *buf += data_len; |
| 375 | |
| 376 | 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] | 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Export a point as a TLS ECPoint record (RFC 4492) |
| 381 | * struct { |
| 382 | * opaque point <1..2^8-1>; |
| 383 | * } ECPoint; |
| 384 | */ |
| 385 | 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] | 386 | int format, size_t *olen, |
| 387 | unsigned char *buf, size_t blen ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 388 | { |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 389 | int ret; |
| 390 | |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 391 | /* |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 392 | * 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] | 393 | */ |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 394 | if( blen < 1 ) |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 395 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 396 | |
Manuel Pégourié-Gonnard | 420f1eb | 2013-02-10 12:22:46 +0100 | [diff] [blame] | 397 | if( ( ret = ecp_point_write_binary( grp, pt, format, |
| 398 | olen, buf + 1, blen - 1) ) != 0 ) |
| 399 | return( ret ); |
| 400 | |
| 401 | /* |
| 402 | * write length to the first byte and update total length |
| 403 | */ |
| 404 | buf[0] = *olen; |
| 405 | ++*olen; |
| 406 | |
| 407 | return 0; |
Manuel Pégourié-Gonnard | 0079405 | 2013-02-09 19:00:07 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /* |
Manuel Pégourié-Gonnard | 773ed54 | 2012-11-18 13:19:07 +0100 | [diff] [blame] | 411 | * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi. |
| 412 | * See the documentation of struct ecp_group. |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 413 | */ |
| 414 | static int ecp_modp( mpi *N, const ecp_group *grp ) |
| 415 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 416 | int ret; |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 417 | |
| 418 | if( grp->modp == NULL ) |
| 419 | return( mpi_mod_mpi( N, N, &grp->P ) ); |
| 420 | |
| 421 | if( mpi_cmp_int( N, 0 ) < 0 || mpi_msb( N ) > 2 * grp->pbits ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 422 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 423 | |
| 424 | MPI_CHK( grp->modp( N ) ); |
| 425 | |
| 426 | while( mpi_cmp_int( N, 0 ) < 0 ) |
| 427 | MPI_CHK( mpi_add_mpi( N, N, &grp->P ) ); |
| 428 | |
| 429 | while( mpi_cmp_mpi( N, &grp->P ) >= 0 ) |
| 430 | MPI_CHK( mpi_sub_mpi( N, N, &grp->P ) ); |
| 431 | |
| 432 | cleanup: |
| 433 | return( ret ); |
| 434 | } |
| 435 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 436 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 437 | /* |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 438 | * 192 bits in terms of t_uint |
| 439 | */ |
| 440 | #define P192_SIZE_INT ( 192 / CHAR_BIT / sizeof( t_uint ) ) |
| 441 | |
| 442 | /* |
| 443 | * Table to get S1, S2, S3 of FIPS 186-3 D.2.1: |
| 444 | * -1 means let this chunk be 0 |
| 445 | * a positive value i means A_i. |
| 446 | */ |
| 447 | #define P192_CHUNKS 3 |
| 448 | #define P192_CHUNK_CHAR ( 64 / CHAR_BIT ) |
| 449 | #define P192_CHUNK_INT ( P192_CHUNK_CHAR / sizeof( t_uint ) ) |
| 450 | |
| 451 | const signed char p192_tbl[][P192_CHUNKS] = { |
| 452 | { -1, 3, 3 }, /* S1 */ |
| 453 | { 4, 4, -1 }, /* S2 */ |
| 454 | { 5, 5, 5 }, /* S3 */ |
| 455 | }; |
| 456 | |
| 457 | /* |
| 458 | * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1) |
| 459 | */ |
| 460 | static int ecp_mod_p192( mpi *N ) |
| 461 | { |
| 462 | int ret; |
| 463 | unsigned char i, j, offset; |
| 464 | signed char chunk; |
| 465 | mpi tmp, acc; |
| 466 | t_uint tmp_p[P192_SIZE_INT], acc_p[P192_SIZE_INT + 1]; |
| 467 | |
| 468 | tmp.s = 1; |
| 469 | tmp.n = sizeof( tmp_p ) / sizeof( tmp_p[0] ); |
| 470 | tmp.p = tmp_p; |
| 471 | |
| 472 | acc.s = 1; |
| 473 | acc.n = sizeof( acc_p ) / sizeof( acc_p[0] ); |
| 474 | acc.p = acc_p; |
| 475 | |
| 476 | MPI_CHK( mpi_grow( N, P192_SIZE_INT * 2 ) ); |
| 477 | |
| 478 | /* |
| 479 | * acc = T |
| 480 | */ |
| 481 | memset( acc_p, 0, sizeof( acc_p ) ); |
| 482 | memcpy( acc_p, N->p, P192_CHUNK_CHAR * P192_CHUNKS ); |
| 483 | |
| 484 | for( i = 0; i < sizeof( p192_tbl ) / sizeof( p192_tbl[0] ); i++) |
| 485 | { |
| 486 | /* |
| 487 | * tmp = S_i |
| 488 | */ |
| 489 | memset( tmp_p, 0, sizeof( tmp_p ) ); |
| 490 | for( j = 0, offset = P192_CHUNKS - 1; j < P192_CHUNKS; j++, offset-- ) |
| 491 | { |
| 492 | chunk = p192_tbl[i][j]; |
| 493 | if( chunk >= 0 ) |
| 494 | memcpy( tmp_p + offset * P192_CHUNK_INT, |
| 495 | N->p + chunk * P192_CHUNK_INT, |
| 496 | P192_CHUNK_CHAR ); |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * acc += tmp |
| 501 | */ |
| 502 | MPI_CHK( mpi_add_abs( &acc, &acc, &tmp ) ); |
| 503 | } |
| 504 | |
| 505 | MPI_CHK( mpi_copy( N, &acc ) ); |
| 506 | |
| 507 | cleanup: |
| 508 | return( ret ); |
| 509 | } |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 510 | #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */ |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 511 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 512 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 513 | /* |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 514 | * Size of p521 in terms of t_uint |
| 515 | */ |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 516 | #define P521_SIZE_INT ( 521 / CHAR_BIT / sizeof( t_uint ) + 1 ) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * Bits to keep in the most significant t_uint |
| 520 | */ |
| 521 | #if defined(POLARSS_HAVE_INT8) |
| 522 | #define P521_MASK 0x01 |
| 523 | #else |
| 524 | #define P521_MASK 0x01FF |
| 525 | #endif |
| 526 | |
| 527 | /* |
| 528 | * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5) |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 529 | */ |
| 530 | static int ecp_mod_p521( mpi *N ) |
| 531 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 532 | int ret; |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 533 | t_uint Mp[P521_SIZE_INT]; |
| 534 | mpi M; |
| 535 | |
| 536 | if( N->n < P521_SIZE_INT ) |
| 537 | return( 0 ); |
| 538 | |
| 539 | memset( Mp, 0, P521_SIZE_INT * sizeof( t_uint ) ); |
| 540 | memcpy( Mp, N->p, P521_SIZE_INT * sizeof( t_uint ) ); |
| 541 | Mp[P521_SIZE_INT - 1] &= P521_MASK; |
| 542 | |
| 543 | M.s = 1; |
| 544 | M.n = P521_SIZE_INT; |
| 545 | M.p = Mp; |
| 546 | |
| 547 | MPI_CHK( mpi_shift_r( N, 521 ) ); |
| 548 | |
| 549 | MPI_CHK( mpi_add_abs( N, N, &M ) ); |
| 550 | |
| 551 | cleanup: |
| 552 | return( ret ); |
| 553 | } |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 554 | #endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */ |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 555 | |
| 556 | /* |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 557 | * Domain parameters for secp192r1 |
| 558 | */ |
| 559 | #define SECP192R1_P \ |
| 560 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF" |
| 561 | #define SECP192R1_B \ |
| 562 | "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1" |
| 563 | #define SECP192R1_GX \ |
| 564 | "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012" |
| 565 | #define SECP192R1_GY \ |
| 566 | "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811" |
| 567 | #define SECP192R1_N \ |
| 568 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831" |
| 569 | |
| 570 | /* |
| 571 | * Domain parameters for secp224r1 |
| 572 | */ |
| 573 | #define SECP224R1_P \ |
| 574 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001" |
| 575 | #define SECP224R1_B \ |
| 576 | "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4" |
| 577 | #define SECP224R1_GX \ |
| 578 | "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21" |
| 579 | #define SECP224R1_GY \ |
| 580 | "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34" |
| 581 | #define SECP224R1_N \ |
| 582 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D" |
| 583 | |
| 584 | /* |
| 585 | * Domain parameters for secp256r1 |
| 586 | */ |
| 587 | #define SECP256R1_P \ |
| 588 | "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF" |
| 589 | #define SECP256R1_B \ |
| 590 | "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B" |
| 591 | #define SECP256R1_GX \ |
| 592 | "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" |
| 593 | #define SECP256R1_GY \ |
| 594 | "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5" |
| 595 | #define SECP256R1_N \ |
| 596 | "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551" |
| 597 | |
| 598 | /* |
| 599 | * Domain parameters for secp384r1 |
| 600 | */ |
| 601 | #define SECP384R1_P \ |
| 602 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 603 | "FFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF" |
| 604 | #define SECP384R1_B \ |
| 605 | "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE814112" \ |
| 606 | "0314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF" |
| 607 | #define SECP384R1_GX \ |
| 608 | "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B98" \ |
| 609 | "59F741E082542A385502F25DBF55296C3A545E3872760AB7" |
| 610 | #define SECP384R1_GY \ |
| 611 | "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147C" \ |
| 612 | "E9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F" |
| 613 | #define SECP384R1_N \ |
| 614 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 615 | "C7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973" |
| 616 | |
| 617 | /* |
| 618 | * Domain parameters for secp521r1 |
| 619 | */ |
| 620 | #define SECP521R1_P \ |
| 621 | "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 622 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 623 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 624 | #define SECP521R1_B \ |
| 625 | "00000051953EB9618E1C9A1F929A21A0B68540EEA2DA725B" \ |
| 626 | "99B315F3B8B489918EF109E156193951EC7E937B1652C0BD" \ |
| 627 | "3BB1BF073573DF883D2C34F1EF451FD46B503F00" |
| 628 | #define SECP521R1_GX \ |
| 629 | "000000C6858E06B70404E9CD9E3ECB662395B4429C648139" \ |
| 630 | "053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127" \ |
| 631 | "A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66" |
| 632 | #define SECP521R1_GY \ |
| 633 | "0000011839296A789A3BC0045C8A5FB42C7D1BD998F54449" \ |
| 634 | "579B446817AFBD17273E662C97EE72995EF42640C550B901" \ |
| 635 | "3FAD0761353C7086A272C24088BE94769FD16650" |
| 636 | #define SECP521R1_N \ |
| 637 | "000001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" \ |
| 638 | "FFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148" \ |
| 639 | "F709A5D03BB5C9B8899C47AEBB6FB71E91386409" |
| 640 | |
| 641 | /* |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 642 | * Set a group using well-known domain parameters |
| 643 | */ |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 644 | int ecp_use_known_dp( ecp_group *grp, ecp_group_id id ) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 645 | { |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 646 | grp->id = id; |
| 647 | |
| 648 | switch( id ) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 649 | { |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 650 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 651 | case POLARSSL_ECP_DP_SECP192R1: |
Manuel Pégourié-Gonnard | 8433824 | 2012-11-11 20:45:18 +0100 | [diff] [blame] | 652 | grp->modp = ecp_mod_p192; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 653 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 654 | SECP192R1_P, SECP192R1_B, |
| 655 | SECP192R1_GX, SECP192R1_GY, SECP192R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 656 | #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 657 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 658 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 659 | case POLARSSL_ECP_DP_SECP224R1: |
| 660 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 661 | SECP224R1_P, SECP224R1_B, |
| 662 | SECP224R1_GX, SECP224R1_GY, SECP224R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 663 | #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 664 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 665 | #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 666 | case POLARSSL_ECP_DP_SECP256R1: |
| 667 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 668 | SECP256R1_P, SECP256R1_B, |
| 669 | SECP256R1_GX, SECP256R1_GY, SECP256R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 670 | #endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 671 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 672 | #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 673 | case POLARSSL_ECP_DP_SECP384R1: |
| 674 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 675 | SECP384R1_P, SECP384R1_B, |
| 676 | SECP384R1_GX, SECP384R1_GY, SECP384R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 677 | #endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */ |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 678 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 679 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 680 | case POLARSSL_ECP_DP_SECP521R1: |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 681 | grp->modp = ecp_mod_p521; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 682 | return( ecp_group_read_string( grp, 16, |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 683 | SECP521R1_P, SECP521R1_B, |
| 684 | SECP521R1_GX, SECP521R1_GY, SECP521R1_N ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 685 | #endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */ |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 686 | |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 687 | default: |
| 688 | grp->id = POLARSSL_ECP_DP_NONE; |
| 689 | return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE ); |
| 690 | } |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* |
| 694 | * Set a group from an ECParameters record (RFC 4492) |
| 695 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 696 | 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] | 697 | { |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 698 | unsigned int named_curve; |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 699 | |
| 700 | /* |
| 701 | * We expect at least three bytes (see below) |
| 702 | */ |
| 703 | if( len < 3 ) |
| 704 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 705 | |
| 706 | /* |
| 707 | * First byte is curve_type; only named_curve is handled |
| 708 | */ |
Manuel Pégourié-Gonnard | 7c145c6 | 2013-02-10 13:20:52 +0100 | [diff] [blame] | 709 | if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE ) |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 710 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
| 711 | |
| 712 | /* |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 713 | * Next two bytes are the namedcurve value |
Manuel Pégourié-Gonnard | 1a96728 | 2013-02-09 17:03:58 +0100 | [diff] [blame] | 714 | */ |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 715 | named_curve = *(*buf)++; |
| 716 | named_curve <<= 8; |
| 717 | named_curve |= *(*buf)++; |
| 718 | return ecp_use_known_dp( grp, ecp_grp_id_from_named_curve( named_curve ) ); |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /* |
| 722 | * Write the ECParameters record corresponding to a group (RFC 4492) |
| 723 | */ |
| 724 | int ecp_tls_write_group( const ecp_group *grp, size_t *olen, |
| 725 | unsigned char *buf, size_t blen ) |
| 726 | { |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 727 | unsigned int named_curve; |
| 728 | |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 729 | /* |
| 730 | * We are going to write 3 bytes (see below) |
| 731 | */ |
| 732 | *olen = 3; |
| 733 | if( blen < *olen ) |
| 734 | return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL ); |
| 735 | |
| 736 | /* |
| 737 | * First byte is curve_type, always named_curve |
| 738 | */ |
| 739 | *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE; |
| 740 | |
| 741 | /* |
| 742 | * Next two bytes are the namedcurve value |
| 743 | */ |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 744 | named_curve = ecp_named_curve_from_grp_id( grp->id ); |
| 745 | buf[0] = named_curve >> 8; |
| 746 | buf[1] = named_curve & 0xFF; |
Manuel Pégourié-Gonnard | b325887 | 2013-02-10 12:06:19 +0100 | [diff] [blame] | 747 | |
| 748 | return 0; |
Manuel Pégourié-Gonnard | a5402fe | 2012-11-07 20:24:05 +0100 | [diff] [blame] | 749 | } |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 750 | |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 751 | /* |
| 752 | * Get the internal identifer from the TLS name |
| 753 | */ |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 754 | ecp_group_id ecp_grp_id_from_named_curve( uint16_t tls_id ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 755 | { |
Manuel Pégourié-Gonnard | a79d123 | 2013-09-17 15:42:35 +0200 | [diff] [blame] | 756 | const ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 757 | |
| 758 | for( curve_info = ecp_supported_curves; |
| 759 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 760 | curve_info++ ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 761 | { |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 762 | if( curve_info->tls_id == tls_id ) |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 763 | return( curve_info->grp_id ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 764 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 765 | |
| 766 | return( POLARSSL_ECP_DP_NONE ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 767 | } |
| 768 | |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 769 | /* |
| 770 | * Get the TLS name for the internal identifer |
| 771 | */ |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 772 | uint16_t ecp_named_curve_from_grp_id( ecp_group_id grp_id ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 773 | { |
Manuel Pégourié-Gonnard | a79d123 | 2013-09-17 15:42:35 +0200 | [diff] [blame] | 774 | const ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 775 | |
| 776 | for( curve_info = ecp_supported_curves; |
| 777 | curve_info->grp_id != POLARSSL_ECP_DP_NONE; |
| 778 | curve_info++ ) |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 779 | { |
Manuel Pégourié-Gonnard | 56cd319 | 2013-09-17 17:23:07 +0200 | [diff] [blame] | 780 | if( curve_info->grp_id == grp_id ) |
| 781 | return( curve_info->tls_id ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 782 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 783 | |
| 784 | return( 0 ); |
Manuel Pégourié-Gonnard | 7038039 | 2013-09-16 16:19:53 +0200 | [diff] [blame] | 785 | } |
Manuel Pégourié-Gonnard | 568c9cf | 2013-09-16 17:30:04 +0200 | [diff] [blame] | 786 | |
Manuel Pégourié-Gonnard | 847395a | 2012-11-05 13:13:44 +0100 | [diff] [blame] | 787 | /* |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 788 | * 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] | 789 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 790 | * In order to guarantee that, we need to ensure that operands of |
| 791 | * 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] | 792 | * bring the result back to this range. |
| 793 | * |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 794 | * The following macros are shortcuts for doing that. |
Manuel Pégourié-Gonnard | dada4da | 2012-11-10 14:23:17 +0100 | [diff] [blame] | 795 | */ |
| 796 | |
| 797 | /* |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 798 | * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi |
| 799 | */ |
Manuel Pégourié-Gonnard | 62aad14 | 2012-11-10 00:27:12 +0100 | [diff] [blame] | 800 | #define MOD_MUL( N ) MPI_CHK( ecp_modp( &N, grp ) ) |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 801 | |
| 802 | /* |
| 803 | * Reduce a mpi mod p in-place, to use after mpi_sub_mpi |
| 804 | */ |
| 805 | #define MOD_SUB( N ) \ |
| 806 | while( mpi_cmp_int( &N, 0 ) < 0 ) \ |
| 807 | MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) ) |
| 808 | |
| 809 | /* |
| 810 | * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int |
| 811 | */ |
| 812 | #define MOD_ADD( N ) \ |
| 813 | while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \ |
| 814 | MPI_CHK( mpi_sub_mpi( &N, &N, &grp->P ) ) |
| 815 | |
| 816 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 817 | * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 818 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 819 | static int ecp_normalize( const ecp_group *grp, ecp_point *pt ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 820 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 821 | int ret; |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 822 | mpi Zi, ZZi; |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 823 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 824 | if( mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 825 | return( 0 ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 826 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 827 | mpi_init( &Zi ); mpi_init( &ZZi ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 828 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 829 | /* |
| 830 | * X = X / Z^2 mod p |
| 831 | */ |
| 832 | MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) ); |
| 833 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 834 | 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] | 835 | |
| 836 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 837 | * Y = Y / Z^3 mod p |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 838 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 839 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y ); |
| 840 | 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] | 841 | |
| 842 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 843 | * Z = 1 |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 844 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 845 | MPI_CHK( mpi_lset( &pt->Z, 1 ) ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 846 | |
| 847 | cleanup: |
| 848 | |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 849 | mpi_free( &Zi ); mpi_free( &ZZi ); |
Manuel Pégourié-Gonnard | d070f51 | 2012-11-08 17:40:51 +0100 | [diff] [blame] | 850 | |
| 851 | return( ret ); |
| 852 | } |
| 853 | |
| 854 | /* |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 855 | * Normalize jacobian coordinates of an array of points, |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 856 | * 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] | 857 | * (See for example Cohen's "A Course in Computational Algebraic Number |
| 858 | * Theory", Algorithm 10.3.4.) |
| 859 | * |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 860 | * Warning: fails (returning an error) if one of the points is zero! |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 861 | * This should never happen, see choice of w in ecp_mul(). |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 862 | */ |
| 863 | static int ecp_normalize_many( const ecp_group *grp, |
| 864 | ecp_point T[], size_t t_len ) |
| 865 | { |
| 866 | int ret; |
| 867 | size_t i; |
| 868 | mpi *c, u, Zi, ZZi; |
| 869 | |
| 870 | if( t_len < 2 ) |
| 871 | return( ecp_normalize( grp, T ) ); |
| 872 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 873 | if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 874 | return( POLARSSL_ERR_ECP_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 875 | |
| 876 | mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi ); |
| 877 | for( i = 0; i < t_len; i++ ) |
| 878 | mpi_init( &c[i] ); |
| 879 | |
| 880 | /* |
| 881 | * c[i] = Z_0 * ... * Z_i |
| 882 | */ |
| 883 | MPI_CHK( mpi_copy( &c[0], &T[0].Z ) ); |
| 884 | for( i = 1; i < t_len; i++ ) |
| 885 | { |
| 886 | MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i].Z ) ); |
| 887 | MOD_MUL( c[i] ); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * u = 1 / (Z_0 * ... * Z_n) mod P |
| 892 | */ |
| 893 | MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) ); |
| 894 | |
| 895 | for( i = t_len - 1; ; i-- ) |
| 896 | { |
| 897 | /* |
| 898 | * Zi = 1 / Z_i mod p |
| 899 | * u = 1 / (Z_0 * ... * Z_i) mod P |
| 900 | */ |
| 901 | if( i == 0 ) { |
| 902 | MPI_CHK( mpi_copy( &Zi, &u ) ); |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi ); |
| 907 | MPI_CHK( mpi_mul_mpi( &u, &u, &T[i].Z ) ); MOD_MUL( u ); |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * proceed as in normalize() |
| 912 | */ |
| 913 | MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi ); |
| 914 | MPI_CHK( mpi_mul_mpi( &T[i].X, &T[i].X, &ZZi ) ); MOD_MUL( T[i].X ); |
| 915 | MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &ZZi ) ); MOD_MUL( T[i].Y ); |
| 916 | MPI_CHK( mpi_mul_mpi( &T[i].Y, &T[i].Y, &Zi ) ); MOD_MUL( T[i].Y ); |
| 917 | MPI_CHK( mpi_lset( &T[i].Z, 1 ) ); |
| 918 | |
| 919 | if( i == 0 ) |
| 920 | break; |
| 921 | } |
| 922 | |
| 923 | cleanup: |
| 924 | |
| 925 | mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi ); |
| 926 | for( i = 0; i < t_len; i++ ) |
| 927 | mpi_free( &c[i] ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 928 | polarssl_free( c ); |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 929 | |
| 930 | return( ret ); |
| 931 | } |
| 932 | |
| 933 | |
| 934 | /* |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 935 | * Point doubling R = 2 P, Jacobian coordinates (GECC 3.21) |
| 936 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 937 | static int ecp_double_jac( const ecp_group *grp, ecp_point *R, |
| 938 | const ecp_point *P ) |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 939 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 940 | int ret; |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 941 | mpi T1, T2, T3, X, Y, Z; |
| 942 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 943 | #if defined(POLARSSL_SELF_TEST) |
| 944 | dbl_count++; |
| 945 | #endif |
| 946 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 947 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 948 | return( ecp_set_zero( R ) ); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 949 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 950 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); |
| 951 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
| 952 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 953 | MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
| 954 | MPI_CHK( mpi_sub_mpi( &T2, &P->X, &T1 ) ); MOD_SUB( T2 ); |
| 955 | MPI_CHK( mpi_add_mpi( &T1, &P->X, &T1 ) ); MOD_ADD( T1 ); |
| 956 | MPI_CHK( mpi_mul_mpi( &T2, &T2, &T1 ) ); MOD_MUL( T2 ); |
| 957 | MPI_CHK( mpi_mul_int( &T2, &T2, 3 ) ); MOD_ADD( T2 ); |
| 958 | MPI_CHK( mpi_mul_int( &Y, &P->Y, 2 ) ); MOD_ADD( Y ); |
| 959 | MPI_CHK( mpi_mul_mpi( &Z, &Y, &P->Z ) ); MOD_MUL( Z ); |
| 960 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y ); |
| 961 | MPI_CHK( mpi_mul_mpi( &T3, &Y, &P->X ) ); MOD_MUL( T3 ); |
| 962 | MPI_CHK( mpi_mul_mpi( &Y, &Y, &Y ) ); MOD_MUL( Y ); |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 963 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 964 | /* |
| 965 | * For Y = Y / 2 mod p, we must make sure that Y is even before |
| 966 | * using right-shift. No need to reduce mod p afterwards. |
| 967 | */ |
| 968 | if( mpi_get_bit( &Y, 0 ) == 1 ) |
| 969 | MPI_CHK( mpi_add_mpi( &Y, &Y, &grp->P ) ); |
| 970 | MPI_CHK( mpi_shift_r( &Y, 1 ) ); |
| 971 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 972 | MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
| 973 | MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
| 974 | MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
| 975 | MPI_CHK( mpi_sub_mpi( &T1, &T3, &X ) ); MOD_SUB( T1 ); |
| 976 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &T2 ) ); MOD_MUL( T1 ); |
| 977 | MPI_CHK( mpi_sub_mpi( &Y, &T1, &Y ) ); MOD_SUB( Y ); |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 978 | |
| 979 | MPI_CHK( mpi_copy( &R->X, &X ) ); |
| 980 | MPI_CHK( mpi_copy( &R->Y, &Y ) ); |
| 981 | MPI_CHK( mpi_copy( &R->Z, &Z ) ); |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 982 | |
| 983 | cleanup: |
| 984 | |
| 985 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); |
| 986 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
| 987 | |
| 988 | return( ret ); |
| 989 | } |
| 990 | |
| 991 | /* |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 992 | * Addition or subtraction: R = P + Q or R = P + Q, |
| 993 | * mixed affine-Jacobian coordinates (GECC 3.22) |
| 994 | * |
| 995 | * The coordinates of Q must be normalized (= affine), |
| 996 | * but those of P don't need to. R is not normalized. |
| 997 | * |
| 998 | * If sign >= 0, perform addition, otherwise perform subtraction, |
| 999 | * taking advantage of the fact that, for Q != 0, we have |
| 1000 | * -Q = (Q.X, -Q.Y, Q.Z) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1001 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1002 | static int ecp_add_mixed( const ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1003 | const ecp_point *P, const ecp_point *Q, |
| 1004 | signed char sign ) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1005 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1006 | int ret; |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1007 | mpi T1, T2, T3, T4, X, Y, Z; |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1008 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1009 | #if defined(POLARSSL_SELF_TEST) |
| 1010 | add_count++; |
| 1011 | #endif |
| 1012 | |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1013 | /* |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1014 | * Trivial cases: P == 0 or Q == 0 |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1015 | * (Check Q first, so that we know Q != 0 when we compute -Q.) |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1016 | */ |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1017 | if( mpi_cmp_int( &Q->Z, 0 ) == 0 ) |
| 1018 | return( ecp_copy( R, P ) ); |
| 1019 | |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1020 | if( mpi_cmp_int( &P->Z, 0 ) == 0 ) |
| 1021 | { |
| 1022 | ret = ecp_copy( R, Q ); |
| 1023 | |
| 1024 | /* |
| 1025 | * -R.Y mod P = P - R.Y unless R.Y == 0 |
| 1026 | */ |
| 1027 | if( ret == 0 && sign < 0) |
| 1028 | if( mpi_cmp_int( &R->Y, 0 ) != 0 ) |
| 1029 | ret = mpi_sub_mpi( &R->Y, &grp->P, &R->Y ); |
| 1030 | |
| 1031 | return( ret ); |
| 1032 | } |
| 1033 | |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1034 | /* |
| 1035 | * Make sure Q coordinates are normalized |
| 1036 | */ |
| 1037 | if( mpi_cmp_int( &Q->Z, 1 ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1038 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1039 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1040 | mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 ); |
| 1041 | mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z ); |
Manuel Pégourié-Gonnard | ab38b70 | 2012-11-05 17:34:55 +0100 | [diff] [blame] | 1042 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1043 | MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 ); |
| 1044 | MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 ); |
| 1045 | MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 ); |
| 1046 | MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 ); |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1047 | |
| 1048 | /* |
| 1049 | * For subtraction, -Q.Y should have been used instead of Q.Y, |
| 1050 | * so we replace T2 by -T2, which is P - T2 mod P |
| 1051 | */ |
| 1052 | if( sign < 0 ) |
| 1053 | { |
| 1054 | MPI_CHK( mpi_sub_mpi( &T2, &grp->P, &T2 ) ); |
| 1055 | MOD_SUB( T2 ); |
| 1056 | } |
| 1057 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1058 | MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 ); |
| 1059 | 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] | 1060 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1061 | if( mpi_cmp_int( &T1, 0 ) == 0 ) |
| 1062 | { |
| 1063 | if( mpi_cmp_int( &T2, 0 ) == 0 ) |
| 1064 | { |
| 1065 | ret = ecp_double_jac( grp, R, P ); |
| 1066 | goto cleanup; |
| 1067 | } |
| 1068 | else |
| 1069 | { |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1070 | ret = ecp_set_zero( R ); |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1071 | goto cleanup; |
| 1072 | } |
| 1073 | } |
| 1074 | |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1075 | MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z ); |
| 1076 | MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 ); |
| 1077 | MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 ); |
| 1078 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 ); |
| 1079 | MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 ); |
| 1080 | MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X ); |
| 1081 | MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X ); |
| 1082 | MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X ); |
| 1083 | MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 ); |
| 1084 | MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 ); |
| 1085 | MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 ); |
| 1086 | 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] | 1087 | |
Manuel Pégourié-Gonnard | 84d1aea | 2012-11-09 02:09:38 +0100 | [diff] [blame] | 1088 | MPI_CHK( mpi_copy( &R->X, &X ) ); |
| 1089 | MPI_CHK( mpi_copy( &R->Y, &Y ) ); |
| 1090 | MPI_CHK( mpi_copy( &R->Z, &Z ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1091 | |
| 1092 | cleanup: |
| 1093 | |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1094 | mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 ); |
| 1095 | mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1096 | |
| 1097 | return( ret ); |
| 1098 | } |
| 1099 | |
| 1100 | /* |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1101 | * Addition: R = P + Q, result's coordinates normalized |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1102 | */ |
| 1103 | int ecp_add( const ecp_group *grp, ecp_point *R, |
| 1104 | const ecp_point *P, const ecp_point *Q ) |
| 1105 | { |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1106 | int ret; |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 1107 | |
Manuel Pégourié-Gonnard | 9674fd0 | 2012-11-19 21:23:27 +0100 | [diff] [blame] | 1108 | MPI_CHK( ecp_add_mixed( grp, R, P, Q , 1 ) ); |
| 1109 | MPI_CHK( ecp_normalize( grp, R ) ); |
| 1110 | |
| 1111 | cleanup: |
| 1112 | return( ret ); |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Subtraction: R = P - Q, result's coordinates normalized |
| 1117 | */ |
| 1118 | int ecp_sub( const ecp_group *grp, ecp_point *R, |
| 1119 | const ecp_point *P, const ecp_point *Q ) |
| 1120 | { |
| 1121 | int ret; |
| 1122 | |
| 1123 | MPI_CHK( ecp_add_mixed( grp, R, P, Q, -1 ) ); |
Manuel Pégourié-Gonnard | 1c2782c | 2012-11-19 20:16:28 +0100 | [diff] [blame] | 1124 | MPI_CHK( ecp_normalize( grp, R ) ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1125 | |
Manuel Pégourié-Gonnard | 989c32b | 2012-11-08 22:02:42 +0100 | [diff] [blame] | 1126 | cleanup: |
Manuel Pégourié-Gonnard | 7e0adfb | 2012-11-08 23:21:46 +0100 | [diff] [blame] | 1127 | return( ret ); |
Manuel Pégourié-Gonnard | ae180d0 | 2012-11-02 18:14:40 +0100 | [diff] [blame] | 1128 | } |
| 1129 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1130 | /* |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1131 | * Compute a modified width-w non-adjacent form (NAF) of a number, |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1132 | * with a fixed pattern for resistance to simple timing attacks (even SPA), |
| 1133 | * see [1]. (The resulting multiplication algorithm can also been seen as a |
| 1134 | * modification of 2^w-ary multiplication, with signed coefficients, all of |
| 1135 | * them odd.) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1136 | * |
| 1137 | * Input: |
| 1138 | * m must be an odd positive mpi less than w * k bits long |
| 1139 | * x must be an array of k elements |
| 1140 | * w must be less than a certain maximum (currently 8) |
| 1141 | * |
| 1142 | * The result is a sequence x[0], ..., x[k-1] with x[i] in the range |
| 1143 | * - 2^(width - 1) .. 2^(width - 1) - 1 such that |
| 1144 | * m = (2 * x[0] + 1) + 2^width * (2 * x[1] + 1) + ... |
| 1145 | * + 2^((k-1) * width) * (2 * x[k-1] + 1) |
| 1146 | * |
| 1147 | * Compared to "Algorithm SPA-resistant Width-w NAF with Odd Scalar" |
| 1148 | * p. 335 of the cited reference, here we return only u, not d_w since |
| 1149 | * it is known that the other d_w[j] will be 0. Moreover, the returned |
| 1150 | * string doesn't actually store u_i but x_i = u_i / 2 since it is known |
| 1151 | * that u_i is odd. Also, since we always select a positive value for d |
| 1152 | * mod 2^w, we don't need to check the sign of u[i-1] when the reference |
| 1153 | * does. Finally, there is an off-by-one error in the reference: the |
| 1154 | * last index should be k-1, not k. |
| 1155 | */ |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1156 | static int ecp_w_naf_fixed( signed char x[], size_t k, |
| 1157 | unsigned char w, const mpi *m ) |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1158 | { |
| 1159 | int ret; |
| 1160 | unsigned int i, u, mask, carry; |
| 1161 | mpi M; |
| 1162 | |
| 1163 | mpi_init( &M ); |
| 1164 | |
| 1165 | MPI_CHK( mpi_copy( &M, m ) ); |
| 1166 | mask = ( 1 << w ) - 1; |
| 1167 | carry = 1 << ( w - 1 ); |
| 1168 | |
| 1169 | for( i = 0; i < k; i++ ) |
| 1170 | { |
| 1171 | u = M.p[0] & mask; |
| 1172 | |
| 1173 | if( ( u & 1 ) == 0 && i > 0 ) |
| 1174 | x[i - 1] -= carry; |
| 1175 | |
| 1176 | x[i] = u >> 1; |
| 1177 | mpi_shift_r( &M, w ); |
| 1178 | } |
| 1179 | |
| 1180 | /* |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1181 | * We should have consumed all bits, unless the input value was too big |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1182 | */ |
| 1183 | if( mpi_cmp_int( &M, 0 ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1184 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 8555607 | 2012-11-17 19:54:20 +0100 | [diff] [blame] | 1185 | |
| 1186 | cleanup: |
| 1187 | |
| 1188 | mpi_free( &M ); |
| 1189 | |
| 1190 | return( ret ); |
| 1191 | } |
| 1192 | |
| 1193 | /* |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1194 | * Precompute odd multiples of P up to (2 * t_len - 1) P. |
| 1195 | * The table is filled with T[i] = (2 * i + 1) P. |
| 1196 | */ |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1197 | static int ecp_precompute( const ecp_group *grp, |
| 1198 | ecp_point T[], size_t t_len, |
| 1199 | const ecp_point *P ) |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1200 | { |
| 1201 | int ret; |
| 1202 | size_t i; |
| 1203 | ecp_point PP; |
| 1204 | |
| 1205 | ecp_point_init( &PP ); |
| 1206 | |
| 1207 | MPI_CHK( ecp_add( grp, &PP, P, P ) ); |
| 1208 | |
| 1209 | MPI_CHK( ecp_copy( &T[0], P ) ); |
| 1210 | |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1211 | for( i = 1; i < t_len; i++ ) |
Manuel Pégourié-Gonnard | cdd4432 | 2012-11-21 16:00:55 +0100 | [diff] [blame] | 1212 | MPI_CHK( ecp_add_mixed( grp, &T[i], &T[i-1], &PP, +1 ) ); |
| 1213 | |
| 1214 | /* |
| 1215 | * T[0] = P already has normalized coordinates |
| 1216 | */ |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1217 | MPI_CHK( ecp_normalize_many( grp, T + 1, t_len - 1 ) ); |
Manuel Pégourié-Gonnard | 7652a59 | 2012-11-21 10:00:45 +0100 | [diff] [blame] | 1218 | |
| 1219 | cleanup: |
| 1220 | |
| 1221 | ecp_point_free( &PP ); |
| 1222 | |
| 1223 | return( ret ); |
| 1224 | } |
| 1225 | |
| 1226 | /* |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1227 | * Randomize jacobian coordinates: |
| 1228 | * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l |
| 1229 | * This is sort of the reverse operation of ecp_normalize(). |
| 1230 | */ |
| 1231 | static int ecp_randomize_coordinates( const ecp_group *grp, ecp_point *pt, |
| 1232 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1233 | { |
| 1234 | int ret; |
| 1235 | mpi l, ll; |
| 1236 | size_t p_size = (grp->pbits + 7) / 8; |
| 1237 | int count = 0; |
| 1238 | |
| 1239 | mpi_init( &l ); mpi_init( &ll ); |
| 1240 | |
| 1241 | /* Generate l such that 1 < l < p */ |
| 1242 | do |
| 1243 | { |
| 1244 | mpi_fill_random( &l, p_size, f_rng, p_rng ); |
| 1245 | |
| 1246 | while( mpi_cmp_mpi( &l, &grp->P ) >= 0 ) |
| 1247 | mpi_shift_r( &l, 1 ); |
| 1248 | |
| 1249 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1250 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1251 | } |
| 1252 | while( mpi_cmp_int( &l, 1 ) <= 0 ); |
| 1253 | |
| 1254 | /* Z = l * Z */ |
| 1255 | MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z ); |
| 1256 | |
| 1257 | /* X = l^2 * X */ |
| 1258 | MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll ); |
| 1259 | MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X ); |
| 1260 | |
| 1261 | /* Y = l^3 * Y */ |
| 1262 | MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll ); |
| 1263 | MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y ); |
| 1264 | |
| 1265 | cleanup: |
| 1266 | mpi_free( &l ); mpi_free( &ll ); |
| 1267 | |
| 1268 | return( ret ); |
| 1269 | } |
| 1270 | |
| 1271 | /* |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1272 | * Maximum length of the precomputed table |
| 1273 | */ |
| 1274 | #define MAX_PRE_LEN ( 1 << (POLARSSL_ECP_WINDOW_SIZE - 1) ) |
| 1275 | |
| 1276 | /* |
| 1277 | * Maximum length of the NAF: ceil( grp->nbits + 1 ) / w |
| 1278 | * (that is: grp->nbits / w + 1) |
| 1279 | * Allow p_bits + 1 bits in case M = grp->N + 1 is one bit longer than N. |
| 1280 | */ |
Manuel Pégourié-Gonnard | b694b48 | 2013-08-08 13:30:57 +0200 | [diff] [blame] | 1281 | #define MAX_NAF_LEN ( POLARSSL_ECP_MAX_BITS / 2 + 1 ) |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1282 | |
| 1283 | /* |
| 1284 | * Integer multiplication: R = m * P |
| 1285 | * |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1286 | * Based on fixed-pattern width-w NAF, see comments of ecp_w_naf_fixed(). |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1287 | * |
| 1288 | * This function executes a fixed number of operations for |
| 1289 | * random m in the range 0 .. 2^nbits - 1. |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1290 | * |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1291 | * As an additional countermeasure against potential timing attacks, |
| 1292 | * we randomize coordinates before each addition. This was suggested as a |
Manuel Pégourié-Gonnard | 07de4b1 | 2013-09-02 16:26:04 +0200 | [diff] [blame] | 1293 | * countermeasure against DPA in 5.3 of [2] (with the obvious adaptation that |
| 1294 | * we use jacobian coordinates, not standard projective coordinates). |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1295 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1296 | int ecp_mul( ecp_group *grp, ecp_point *R, |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1297 | const mpi *m, const ecp_point *P, |
| 1298 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1299 | { |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1300 | int ret; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1301 | unsigned char w, m_is_odd, p_eq_g; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1302 | size_t pre_len, naf_len, i, j; |
| 1303 | signed char naf[ MAX_NAF_LEN ]; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1304 | ecp_point Q, *T = NULL, S[2]; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1305 | mpi M; |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1306 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1307 | if( mpi_cmp_int( m, 0 ) < 0 || mpi_msb( m ) > grp->nbits ) |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1308 | return( POLARSSL_ERR_ECP_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 4bdd47d | 2012-11-11 14:33:59 +0100 | [diff] [blame] | 1309 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1310 | mpi_init( &M ); |
| 1311 | ecp_point_init( &Q ); |
| 1312 | ecp_point_init( &S[0] ); |
| 1313 | ecp_point_init( &S[1] ); |
| 1314 | |
| 1315 | /* |
| 1316 | * Check if P == G |
| 1317 | */ |
| 1318 | p_eq_g = ( mpi_cmp_int( &P->Z, 1 ) == 0 && |
| 1319 | mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 && |
| 1320 | mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 ); |
| 1321 | |
| 1322 | /* |
| 1323 | * If P == G, pre-compute a lot of points: this will be re-used later, |
| 1324 | * otherwise, choose window size depending on curve size |
| 1325 | */ |
| 1326 | if( p_eq_g ) |
| 1327 | w = POLARSSL_ECP_WINDOW_SIZE; |
| 1328 | else |
| 1329 | w = grp->nbits >= 512 ? 6 : |
| 1330 | grp->nbits >= 224 ? 5 : |
| 1331 | 4; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1332 | |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1333 | /* |
| 1334 | * Make sure w is within the limits. |
| 1335 | * The last test ensures that none of the precomputed points is zero, |
| 1336 | * which wouldn't be handled correctly by ecp_normalize_many(). |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1337 | * It is only useful for very small curves as used in the test suite. |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1338 | */ |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1339 | if( w > POLARSSL_ECP_WINDOW_SIZE ) |
| 1340 | w = POLARSSL_ECP_WINDOW_SIZE; |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1341 | if( w < 2 || w >= grp->nbits ) |
| 1342 | w = 2; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1343 | |
| 1344 | pre_len = 1 << ( w - 1 ); |
| 1345 | naf_len = grp->nbits / w + 1; |
| 1346 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1347 | /* |
| 1348 | * Prepare precomputed points: if P == G we want to |
| 1349 | * use grp->T if already initialized, or initiliaze it. |
| 1350 | */ |
| 1351 | if( ! p_eq_g || grp->T == NULL ) |
| 1352 | { |
| 1353 | if( ( T = polarssl_malloc( pre_len * sizeof( ecp_point ) ) ) == NULL ) |
| 1354 | { |
| 1355 | ret = POLARSSL_ERR_ECP_MALLOC_FAILED; |
| 1356 | goto cleanup; |
| 1357 | } |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1358 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1359 | for( i = 0; i < pre_len; i++ ) |
| 1360 | ecp_point_init( &T[i] ); |
| 1361 | |
| 1362 | MPI_CHK( ecp_precompute( grp, T, pre_len, P ) ); |
| 1363 | |
| 1364 | if( p_eq_g ) |
| 1365 | { |
| 1366 | grp->T = T; |
| 1367 | grp->T_size = pre_len; |
| 1368 | } |
| 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | T = grp->T; |
| 1373 | |
| 1374 | /* Should never happen, but we want to be extra sure */ |
| 1375 | if( pre_len != grp->T_size ) |
| 1376 | { |
| 1377 | ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA; |
| 1378 | goto cleanup; |
| 1379 | } |
| 1380 | } |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1381 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1382 | /* |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1383 | * Make sure M is odd (M = m + 1 or M = m + 2) |
| 1384 | * later we'll get m * P by subtracting P or 2 * P to M * P. |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1385 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1386 | m_is_odd = ( mpi_get_bit( m, 0 ) == 1 ); |
| 1387 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1388 | MPI_CHK( mpi_copy( &M, m ) ); |
| 1389 | MPI_CHK( mpi_add_int( &M, &M, 1 + m_is_odd ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1390 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1391 | /* |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1392 | * Compute the fixed-pattern NAF of M |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1393 | */ |
| 1394 | MPI_CHK( ecp_w_naf_fixed( naf, naf_len, w, &M ) ); |
Manuel Pégourié-Gonnard | 4712325 | 2012-11-10 14:44:24 +0100 | [diff] [blame] | 1395 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1396 | /* |
| 1397 | * Compute M * P, using a variant of left-to-right 2^w-ary multiplication: |
| 1398 | * at each step we add (2 * naf[i] + 1) P, then multiply by 2^w. |
| 1399 | * |
| 1400 | * If naf[i] >= 0, we have (2 * naf[i] + 1) P == T[ naf[i] ] |
| 1401 | * Otherwise, (2 * naf[i] + 1) P == - ( 2 * ( - naf[i] - 1 ) + 1) P |
| 1402 | * == T[ - naf[i] - 1 ] |
| 1403 | */ |
| 1404 | MPI_CHK( ecp_set_zero( &Q ) ); |
| 1405 | i = naf_len - 1; |
| 1406 | while( 1 ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1407 | { |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1408 | /* Countermeasure (see comments above) */ |
| 1409 | if( f_rng != NULL ) |
| 1410 | ecp_randomize_coordinates( grp, &Q, f_rng, p_rng ); |
| 1411 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1412 | if( naf[i] < 0 ) |
| 1413 | { |
| 1414 | MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ - naf[i] - 1 ], -1 ) ); |
| 1415 | } |
| 1416 | else |
| 1417 | { |
| 1418 | MPI_CHK( ecp_add_mixed( grp, &Q, &Q, &T[ naf[i] ], +1 ) ); |
| 1419 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1420 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1421 | if( i == 0 ) |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1422 | break; |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1423 | i--; |
| 1424 | |
| 1425 | for( j = 0; j < w; j++ ) |
| 1426 | { |
| 1427 | MPI_CHK( ecp_double_jac( grp, &Q, &Q ) ); |
| 1428 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1429 | } |
| 1430 | |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1431 | /* |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1432 | * Now get m * P from M * P |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1433 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1434 | MPI_CHK( ecp_copy( &S[0], P ) ); |
| 1435 | MPI_CHK( ecp_add( grp, &S[1], P, P ) ); |
| 1436 | MPI_CHK( ecp_sub( grp, R, &Q, &S[m_is_odd] ) ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1437 | |
Manuel Pégourié-Gonnard | 3680c82 | 2012-11-21 18:49:45 +0100 | [diff] [blame] | 1438 | |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1439 | cleanup: |
| 1440 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1441 | if( T != NULL && ! p_eq_g ) |
| 1442 | { |
| 1443 | for( i = 0; i < pre_len; i++ ) |
| 1444 | ecp_point_free( &T[i] ); |
| 1445 | polarssl_free( T ); |
| 1446 | } |
| 1447 | |
| 1448 | ecp_point_free( &S[1] ); |
| 1449 | ecp_point_free( &S[0] ); |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1450 | ecp_point_free( &Q ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1451 | mpi_free( &M ); |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1452 | |
| 1453 | return( ret ); |
| 1454 | } |
| 1455 | |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1456 | /* |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1457 | * Check that a point is valid as a public key (SEC1 3.2.3.1) |
| 1458 | */ |
| 1459 | int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt ) |
| 1460 | { |
| 1461 | int ret; |
| 1462 | mpi YY, RHS; |
| 1463 | |
| 1464 | if( mpi_cmp_int( &pt->Z, 0 ) == 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1465 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1466 | |
| 1467 | /* |
| 1468 | * pt coordinates must be normalized for our checks |
| 1469 | */ |
| 1470 | if( mpi_cmp_int( &pt->Z, 1 ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1471 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1472 | |
| 1473 | if( mpi_cmp_int( &pt->X, 0 ) < 0 || |
| 1474 | mpi_cmp_int( &pt->Y, 0 ) < 0 || |
| 1475 | mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 || |
| 1476 | mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1477 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1478 | |
| 1479 | mpi_init( &YY ); mpi_init( &RHS ); |
| 1480 | |
| 1481 | /* |
| 1482 | * YY = Y^2 |
| 1483 | * RHS = X (X^2 - 3) + B = X^3 - 3X + B |
| 1484 | */ |
| 1485 | MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY ); |
| 1486 | MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS ); |
| 1487 | MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS ); |
| 1488 | MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS ); |
| 1489 | MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS ); |
| 1490 | |
| 1491 | if( mpi_cmp_mpi( &YY, &RHS ) != 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1492 | ret = POLARSSL_ERR_ECP_INVALID_KEY; |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1493 | |
| 1494 | cleanup: |
| 1495 | |
| 1496 | mpi_free( &YY ); mpi_free( &RHS ); |
| 1497 | |
| 1498 | return( ret ); |
| 1499 | } |
| 1500 | |
| 1501 | /* |
| 1502 | * Check that an mpi is valid as a private key (SEC1 3.2) |
| 1503 | */ |
Manuel Pégourié-Gonnard | de44a4a | 2013-07-09 16:05:52 +0200 | [diff] [blame] | 1504 | 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] | 1505 | { |
| 1506 | /* We want 1 <= d <= N-1 */ |
| 1507 | if ( mpi_cmp_int( d, 1 ) < 0 || mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1508 | return( POLARSSL_ERR_ECP_INVALID_KEY ); |
Manuel Pégourié-Gonnard | c8dc295 | 2013-07-01 14:06:13 +0200 | [diff] [blame] | 1509 | |
| 1510 | return( 0 ); |
| 1511 | } |
| 1512 | |
| 1513 | /* |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1514 | * Generate a keypair (SEC1 3.2.1) |
| 1515 | */ |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1516 | 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] | 1517 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1518 | void *p_rng ) |
| 1519 | { |
| 1520 | int count = 0; |
| 1521 | size_t n_size = (grp->nbits + 7) / 8; |
| 1522 | |
| 1523 | /* |
| 1524 | * Generate d such that 1 <= n < N |
| 1525 | */ |
| 1526 | do |
| 1527 | { |
| 1528 | mpi_fill_random( d, n_size, f_rng, p_rng ); |
| 1529 | |
| 1530 | while( mpi_cmp_mpi( d, &grp->N ) >= 0 ) |
| 1531 | mpi_shift_r( d, 1 ); |
| 1532 | |
| 1533 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 456d3b9 | 2013-09-16 18:04:38 +0200 | [diff] [blame] | 1534 | return( POLARSSL_ERR_ECP_RANDOM_FAILED ); |
Manuel Pégourié-Gonnard | 45a035a | 2013-01-26 14:42:45 +0100 | [diff] [blame] | 1535 | } |
| 1536 | while( mpi_cmp_int( d, 1 ) < 0 ); |
| 1537 | |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1538 | 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] | 1539 | } |
Manuel Pégourié-Gonnard | efaa31e | 2012-11-06 21:34:35 +0100 | [diff] [blame] | 1540 | |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1541 | #if defined(POLARSSL_SELF_TEST) |
| 1542 | |
Manuel Pégourié-Gonnard | b505c27 | 2012-11-05 17:27:54 +0100 | [diff] [blame] | 1543 | /* |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1544 | * Checkup routine |
| 1545 | */ |
| 1546 | int ecp_self_test( int verbose ) |
| 1547 | { |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1548 | int ret; |
| 1549 | size_t i; |
| 1550 | ecp_group grp; |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1551 | ecp_point R, P; |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1552 | mpi m; |
| 1553 | unsigned long add_c_prev, dbl_c_prev; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 1554 | const char *exponents[] = |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1555 | { |
Manuel Pégourié-Gonnard | b63f9e9 | 2012-11-21 13:00:58 +0100 | [diff] [blame] | 1556 | "000000000000000000000000000000000000000000000000", /* zero */ |
| 1557 | "000000000000000000000000000000000000000000000001", /* one */ |
| 1558 | "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* N */ |
| 1559 | "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1560 | "400000000000000000000000000000000000000000000000", |
| 1561 | "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", |
| 1562 | "555555555555555555555555555555555555555555555555", |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1563 | }; |
| 1564 | |
| 1565 | ecp_group_init( &grp ); |
| 1566 | ecp_point_init( &R ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1567 | ecp_point_init( &P ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1568 | mpi_init( &m ); |
| 1569 | |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 1570 | #if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1571 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) ); |
Paul Bakker | 5dc6b5f | 2013-06-29 23:26:34 +0200 | [diff] [blame] | 1572 | #else |
| 1573 | #if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) |
| 1574 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP224R1 ) ); |
| 1575 | #else |
| 1576 | #if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) |
| 1577 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP256R1 ) ); |
| 1578 | #else |
| 1579 | #if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) |
| 1580 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP384R1 ) ); |
| 1581 | #else |
| 1582 | #if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) |
| 1583 | MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP521R1 ) ); |
| 1584 | #else |
| 1585 | #error No curves defines |
| 1586 | #endif /* POLARSSL_ECP_DP_SECP512R1_ENABLED */ |
| 1587 | #endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */ |
| 1588 | #endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */ |
| 1589 | #endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */ |
| 1590 | #endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */ |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1591 | |
| 1592 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1593 | printf( " ECP test #1 (constant op_count, base point G): " ); |
| 1594 | |
| 1595 | /* Do a dummy multiplication first to trigger precomputation */ |
| 1596 | MPI_CHK( mpi_lset( &m, 2 ) ); |
| 1597 | 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] | 1598 | |
| 1599 | add_count = 0; |
| 1600 | dbl_count = 0; |
| 1601 | MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1602 | 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] | 1603 | |
| 1604 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 1605 | { |
| 1606 | add_c_prev = add_count; |
| 1607 | dbl_c_prev = dbl_count; |
| 1608 | add_count = 0; |
| 1609 | dbl_count = 0; |
| 1610 | |
| 1611 | MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) ); |
Manuel Pégourié-Gonnard | e09d2f8 | 2013-09-02 14:29:09 +0200 | [diff] [blame] | 1612 | 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] | 1613 | |
| 1614 | if( add_count != add_c_prev || dbl_count != dbl_c_prev ) |
| 1615 | { |
| 1616 | if( verbose != 0 ) |
| 1617 | printf( "failed (%zu)\n", i ); |
| 1618 | |
| 1619 | ret = 1; |
| 1620 | goto cleanup; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | if( verbose != 0 ) |
| 1625 | printf( "passed\n" ); |
| 1626 | |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1627 | if( verbose != 0 ) |
| 1628 | printf( " ECP test #2 (constant op_count, other point): " ); |
| 1629 | /* We computed P = 2G last time, use it */ |
| 1630 | |
| 1631 | add_count = 0; |
| 1632 | dbl_count = 0; |
| 1633 | MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) ); |
| 1634 | MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 1635 | |
| 1636 | for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ ) |
| 1637 | { |
| 1638 | add_c_prev = add_count; |
| 1639 | dbl_c_prev = dbl_count; |
| 1640 | add_count = 0; |
| 1641 | dbl_count = 0; |
| 1642 | |
| 1643 | MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) ); |
| 1644 | MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) ); |
| 1645 | |
| 1646 | if( add_count != add_c_prev || dbl_count != dbl_c_prev ) |
| 1647 | { |
| 1648 | if( verbose != 0 ) |
| 1649 | printf( "failed (%zu)\n", i ); |
| 1650 | |
| 1651 | ret = 1; |
| 1652 | goto cleanup; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | if( verbose != 0 ) |
| 1657 | printf( "passed\n" ); |
| 1658 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1659 | cleanup: |
| 1660 | |
| 1661 | if( ret < 0 && verbose != 0 ) |
| 1662 | printf( "Unexpected error, return code = %08X\n", ret ); |
| 1663 | |
| 1664 | ecp_group_free( &grp ); |
| 1665 | ecp_point_free( &R ); |
Manuel Pégourié-Gonnard | 161ef96 | 2013-09-17 19:13:10 +0200 | [diff] [blame^] | 1666 | ecp_point_free( &P ); |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 1667 | mpi_free( &m ); |
| 1668 | |
| 1669 | if( verbose != 0 ) |
| 1670 | printf( "\n" ); |
| 1671 | |
| 1672 | return( ret ); |
Manuel Pégourié-Gonnard | 39d2adb | 2012-10-31 09:26:55 +0100 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | #endif |
| 1676 | |
| 1677 | #endif |