Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange |
| 3 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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 | * Reference: |
| 27 | * |
| 28 | * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12) |
| 29 | */ |
| 30 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 31 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 33 | #if defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 35 | #include "polarssl/dhm.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 36 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | /* |
| 38 | * helper to validate the mpi size and import it |
| 39 | */ |
| 40 | static int dhm_read_bignum( mpi *X, |
| 41 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 42 | const unsigned char *end ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | { |
| 44 | int ret, n; |
| 45 | |
| 46 | if( end - *p < 2 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 47 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | |
| 49 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 50 | (*p) += 2; |
| 51 | |
| 52 | if( (int)( end - *p ) < n ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 53 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 54 | |
| 55 | if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 56 | return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | |
| 58 | (*p) += n; |
| 59 | |
| 60 | return( 0 ); |
| 61 | } |
| 62 | |
| 63 | /* |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 64 | * Verify sanity of parameter with regards to P |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 65 | * |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 66 | * Parameter should be: 2 <= public_param <= P - 2 |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 67 | * |
| 68 | * For more information on the attack, see: |
| 69 | * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf |
| 70 | * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643 |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 71 | */ |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 72 | static int dhm_check_range( const mpi *param, const mpi *P ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 73 | { |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 74 | mpi L, U; |
| 75 | int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA; |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 76 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 77 | mpi_init( &L ); mpi_init( &U ); |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 78 | mpi_lset( &L, 2 ); |
| 79 | mpi_sub_int( &U, P, 2 ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 80 | |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 81 | if( mpi_cmp_mpi( param, &L ) >= 0 && |
| 82 | mpi_cmp_mpi( param, &U ) <= 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 83 | { |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 84 | ret = 0; |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 87 | mpi_free( &L ); mpi_free( &U ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 88 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 89 | return( ret ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | * Parse the ServerKeyExchange parameters |
| 94 | */ |
| 95 | int dhm_read_params( dhm_context *ctx, |
| 96 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 97 | const unsigned char *end ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | { |
Paul Bakker | 13ed9ab | 2012-04-16 09:43:49 +0000 | [diff] [blame] | 99 | int ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | |
Manuel Pégourié-Gonnard | b72b4ed | 2013-09-13 13:55:26 +0200 | [diff] [blame^] | 101 | dhm_free( ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | |
| 103 | if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 || |
| 104 | ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 || |
| 105 | ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 ) |
| 106 | return( ret ); |
| 107 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 108 | if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) |
| 109 | return( ret ); |
| 110 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | ctx->len = mpi_size( &ctx->P ); |
| 112 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | return( 0 ); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Setup and write the ServerKeyExchange parameters |
| 118 | */ |
| 119 | int dhm_make_params( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 120 | unsigned char *output, size_t *olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 121 | int (*f_rng)(void *, unsigned char *, size_t), |
| 122 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | { |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 124 | int ret, count = 0; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 125 | size_t n1, n2, n3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | unsigned char *p; |
| 127 | |
Paul Bakker | b5b20f1 | 2012-09-16 15:07:49 +0000 | [diff] [blame] | 128 | if( mpi_cmp_int( &ctx->P, 0 ) == 0 ) |
| 129 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
| 130 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | /* |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 132 | * Generate X as large as possible ( < P ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | */ |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 134 | do |
| 135 | { |
| 136 | mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 138 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 139 | mpi_shift_r( &ctx->X, 1 ); |
| 140 | |
| 141 | if( count++ > 10 ) |
| 142 | return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED ); |
| 143 | } |
| 144 | while( dhm_check_range( &ctx->X, &ctx->P ) != 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 146 | /* |
| 147 | * Calculate GX = G^X mod P |
| 148 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 150 | &ctx->P , &ctx->RP ) ); |
| 151 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 152 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 153 | return( ret ); |
| 154 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | /* |
| 156 | * export P, G, GX |
| 157 | */ |
| 158 | #define DHM_MPI_EXPORT(X,n) \ |
| 159 | MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \ |
| 160 | *p++ = (unsigned char)( n >> 8 ); \ |
| 161 | *p++ = (unsigned char)( n ); p += n; |
| 162 | |
| 163 | n1 = mpi_size( &ctx->P ); |
| 164 | n2 = mpi_size( &ctx->G ); |
| 165 | n3 = mpi_size( &ctx->GX ); |
| 166 | |
| 167 | p = output; |
| 168 | DHM_MPI_EXPORT( &ctx->P , n1 ); |
| 169 | DHM_MPI_EXPORT( &ctx->G , n2 ); |
| 170 | DHM_MPI_EXPORT( &ctx->GX, n3 ); |
| 171 | |
| 172 | *olen = p - output; |
| 173 | |
| 174 | ctx->len = n1; |
| 175 | |
| 176 | cleanup: |
| 177 | |
| 178 | if( ret != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 179 | return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 180 | |
| 181 | return( 0 ); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Import the peer's public value G^Y |
| 186 | */ |
| 187 | int dhm_read_public( dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 188 | const unsigned char *input, size_t ilen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | { |
| 190 | int ret; |
| 191 | |
| 192 | if( ctx == NULL || ilen < 1 || ilen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 193 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 194 | |
| 195 | if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 196 | return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 197 | |
| 198 | return( 0 ); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Create own private value X and export G^X |
| 203 | */ |
| 204 | int dhm_make_public( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 205 | unsigned char *output, size_t olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 206 | int (*f_rng)(void *, unsigned char *, size_t), |
| 207 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | { |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 209 | int ret, count = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | |
| 211 | if( ctx == NULL || olen < 1 || olen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 212 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | |
Paul Bakker | b5b20f1 | 2012-09-16 15:07:49 +0000 | [diff] [blame] | 214 | if( mpi_cmp_int( &ctx->P, 0 ) == 0 ) |
| 215 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
| 216 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 217 | /* |
| 218 | * generate X and calculate GX = G^X mod P |
| 219 | */ |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 220 | do |
| 221 | { |
| 222 | mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 223 | |
Paul Bakker | aec37cb | 2012-04-26 18:59:59 +0000 | [diff] [blame] | 224 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 225 | mpi_shift_r( &ctx->X, 1 ); |
| 226 | |
| 227 | if( count++ > 10 ) |
| 228 | return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED ); |
| 229 | } |
| 230 | while( dhm_check_range( &ctx->X, &ctx->P ) != 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | |
| 232 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 233 | &ctx->P , &ctx->RP ) ); |
| 234 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 235 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
| 236 | return( ret ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 237 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) ); |
| 239 | |
| 240 | cleanup: |
| 241 | |
| 242 | if( ret != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 243 | return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | |
| 245 | return( 0 ); |
| 246 | } |
| 247 | |
| 248 | /* |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 249 | * Use the blinding method and optimisation suggested in section 10 of: |
| 250 | * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA, |
| 251 | * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer |
| 252 | * Berlin Heidelberg, 1996. p. 104-113. |
| 253 | */ |
| 254 | static int dhm_update_blinding( dhm_context *ctx, |
| 255 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 256 | { |
| 257 | int ret, count; |
| 258 | |
| 259 | /* |
Manuel Pégourié-Gonnard | ed8a02b | 2013-09-04 16:39:03 +0200 | [diff] [blame] | 260 | * If Vi is initialized, update it by squaring it |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 261 | */ |
Manuel Pégourié-Gonnard | ed8a02b | 2013-09-04 16:39:03 +0200 | [diff] [blame] | 262 | if( ctx->Vi.p != NULL ) |
| 263 | { |
| 264 | MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) ); |
| 265 | MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) ); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | /* Vi = random( 2, P-1 ) */ |
| 270 | count = 0; |
| 271 | do |
| 272 | { |
| 273 | mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng ); |
| 274 | |
| 275 | while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 ) |
| 276 | mpi_shift_r( &ctx->Vi, 1 ); |
| 277 | |
| 278 | if( count++ > 10 ) |
| 279 | return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE ); |
| 280 | } |
| 281 | while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 ); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * If X did not change, update Vf by squaring it too |
| 286 | */ |
| 287 | if( mpi_cmp_mpi( &ctx->X, &ctx->_X ) == 0 ) |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 288 | { |
| 289 | MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) ); |
Manuel Pégourié-Gonnard | ed8a02b | 2013-09-04 16:39:03 +0200 | [diff] [blame] | 290 | MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) ); |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 291 | return( 0 ); |
| 292 | } |
| 293 | |
| 294 | /* |
Manuel Pégourié-Gonnard | ed8a02b | 2013-09-04 16:39:03 +0200 | [diff] [blame] | 295 | * Otherwise, compute Vf from scratch |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 296 | */ |
| 297 | |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 298 | /* Vf = Vi^-X mod P */ |
| 299 | MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) ); |
| 300 | MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) ); |
| 301 | |
| 302 | /* Remember secret associated with Vi and Vf */ |
| 303 | MPI_CHK( mpi_copy( &ctx->_X, &ctx->X ) );; |
| 304 | |
| 305 | cleanup: |
| 306 | return( ret ); |
| 307 | } |
| 308 | |
| 309 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 310 | * Derive and export the shared secret (G^Y)^X mod P |
| 311 | */ |
| 312 | int dhm_calc_secret( dhm_context *ctx, |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 313 | unsigned char *output, size_t *olen, |
| 314 | int (*f_rng)(void *, unsigned char *, size_t), |
| 315 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 316 | { |
| 317 | int ret; |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 318 | mpi GYb; |
Manuel Pégourié-Gonnard | 2d62764 | 2013-09-04 14:22:07 +0200 | [diff] [blame] | 319 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 320 | if( ctx == NULL || *olen < ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 321 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 323 | if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 324 | return( ret ); |
| 325 | |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 326 | mpi_init( &GYb ); |
| 327 | |
| 328 | /* Blind peer's value */ |
Manuel Pégourié-Gonnard | ed8a02b | 2013-09-04 16:39:03 +0200 | [diff] [blame] | 329 | if( f_rng != NULL ) |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 330 | { |
| 331 | MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) ); |
| 332 | MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) ); |
| 333 | MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) ); |
| 334 | } |
| 335 | else |
| 336 | MPI_CHK( mpi_copy( &GYb, &ctx->GY ) ); |
| 337 | |
| 338 | /* Do modular exponentiation */ |
| 339 | MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X, |
| 340 | &ctx->P, &ctx->RP ) ); |
| 341 | |
| 342 | /* Unblind secret value */ |
Manuel Pégourié-Gonnard | ed8a02b | 2013-09-04 16:39:03 +0200 | [diff] [blame] | 343 | if( f_rng != NULL ) |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 344 | { |
| 345 | MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) ); |
| 346 | MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) ); |
| 347 | } |
| 348 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 349 | *olen = mpi_size( &ctx->K ); |
| 350 | |
| 351 | MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) ); |
| 352 | |
| 353 | cleanup: |
Manuel Pégourié-Gonnard | 143b502 | 2013-09-04 16:29:59 +0200 | [diff] [blame] | 354 | mpi_free( &GYb ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 355 | |
| 356 | if( ret != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 357 | return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 358 | |
| 359 | return( 0 ); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Free the components of a DHM key |
| 364 | */ |
| 365 | void dhm_free( dhm_context *ctx ) |
| 366 | { |
Manuel Pégourié-Gonnard | b72b4ed | 2013-09-13 13:55:26 +0200 | [diff] [blame^] | 367 | mpi_free( &ctx->_X); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 368 | mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY ); |
| 369 | mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G ); |
| 370 | mpi_free( &ctx->P ); |
Manuel Pégourié-Gonnard | b72b4ed | 2013-09-13 13:55:26 +0200 | [diff] [blame^] | 371 | |
| 372 | memset( ctx, 0, sizeof( dhm_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 375 | #if defined(POLARSSL_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 376 | |
| 377 | /* |
| 378 | * Checkup routine |
| 379 | */ |
| 380 | int dhm_self_test( int verbose ) |
| 381 | { |
| 382 | return( verbose++ ); |
| 383 | } |
| 384 | |
| 385 | #endif |
| 386 | |
| 387 | #endif |