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