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