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 | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 64 | * Verify sanity of public parameter with regards to P |
| 65 | * |
| 66 | * Public parameter should be: 2 <= public_param <= P - 2 |
| 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 | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 72 | static int dhm_check_range( const mpi *public_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 | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 81 | if( mpi_cmp_mpi( public_param, &L ) >= 0 && |
| 82 | mpi_cmp_mpi( public_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 | |
| 101 | memset( ctx, 0, sizeof( dhm_context ) ); |
| 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 | |
| 113 | if( end - *p < 2 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 114 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | return( 0 ); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Setup and write the ServerKeyExchange parameters |
| 121 | */ |
| 122 | int dhm_make_params( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 123 | unsigned char *output, size_t *olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 124 | int (*f_rng)(void *, unsigned char *, size_t), |
| 125 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 127 | int ret, n; |
| 128 | size_t n1, n2, n3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | unsigned char *p; |
| 130 | |
| 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 | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 134 | n = x_size / sizeof( t_uint ) + 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 136 | mpi_fill_random( &ctx->X, n, f_rng, p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | |
| 138 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 139 | mpi_shift_r( &ctx->X, 1 ); |
| 140 | |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 141 | /* |
| 142 | * Calculate GX = G^X mod P |
| 143 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 145 | &ctx->P , &ctx->RP ) ); |
| 146 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 147 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 148 | return( ret ); |
| 149 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | /* |
| 151 | * export P, G, GX |
| 152 | */ |
| 153 | #define DHM_MPI_EXPORT(X,n) \ |
| 154 | MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \ |
| 155 | *p++ = (unsigned char)( n >> 8 ); \ |
| 156 | *p++ = (unsigned char)( n ); p += n; |
| 157 | |
| 158 | n1 = mpi_size( &ctx->P ); |
| 159 | n2 = mpi_size( &ctx->G ); |
| 160 | n3 = mpi_size( &ctx->GX ); |
| 161 | |
| 162 | p = output; |
| 163 | DHM_MPI_EXPORT( &ctx->P , n1 ); |
| 164 | DHM_MPI_EXPORT( &ctx->G , n2 ); |
| 165 | DHM_MPI_EXPORT( &ctx->GX, n3 ); |
| 166 | |
| 167 | *olen = p - output; |
| 168 | |
| 169 | ctx->len = n1; |
| 170 | |
| 171 | cleanup: |
| 172 | |
| 173 | if( ret != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 174 | return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 | |
| 176 | return( 0 ); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Import the peer's public value G^Y |
| 181 | */ |
| 182 | int dhm_read_public( dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 183 | const unsigned char *input, size_t ilen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | { |
| 185 | int ret; |
| 186 | |
| 187 | if( ctx == NULL || ilen < 1 || ilen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 188 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | |
| 190 | if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 191 | return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | |
| 193 | return( 0 ); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Create own private value X and export G^X |
| 198 | */ |
| 199 | int dhm_make_public( dhm_context *ctx, int x_size, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 200 | unsigned char *output, size_t olen, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 201 | int (*f_rng)(void *, unsigned char *, size_t), |
| 202 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | { |
Paul Bakker | 99a03af | 2011-04-01 11:39:39 +0000 | [diff] [blame] | 204 | int ret, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | |
| 206 | if( ctx == NULL || olen < 1 || olen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 207 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * generate X and calculate GX = G^X mod P |
| 211 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 212 | n = x_size / sizeof( t_uint ) + 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 214 | mpi_fill_random( &ctx->X, n, f_rng, p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | |
| 216 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 217 | mpi_shift_r( &ctx->X, 1 ); |
| 218 | |
| 219 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 220 | &ctx->P , &ctx->RP ) ); |
| 221 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 222 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
| 223 | return( ret ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 224 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) ); |
| 226 | |
| 227 | cleanup: |
| 228 | |
| 229 | if( ret != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 230 | return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | |
| 232 | return( 0 ); |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Derive and export the shared secret (G^Y)^X mod P |
| 237 | */ |
| 238 | int dhm_calc_secret( dhm_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 239 | unsigned char *output, size_t *olen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 240 | { |
| 241 | int ret; |
| 242 | |
| 243 | if( ctx == NULL || *olen < ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 244 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | |
| 246 | MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X, |
| 247 | &ctx->P, &ctx->RP ) ); |
| 248 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 249 | if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 250 | return( ret ); |
| 251 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | *olen = mpi_size( &ctx->K ); |
| 253 | |
| 254 | MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) ); |
| 255 | |
| 256 | cleanup: |
| 257 | |
| 258 | if( ret != 0 ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 259 | return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | |
| 261 | return( 0 ); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Free the components of a DHM key |
| 266 | */ |
| 267 | void dhm_free( dhm_context *ctx ) |
| 268 | { |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 269 | mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY ); |
| 270 | mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G ); |
| 271 | mpi_free( &ctx->P ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 274 | #if defined(POLARSSL_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * Checkup routine |
| 278 | */ |
| 279 | int dhm_self_test( int verbose ) |
| 280 | { |
| 281 | return( verbose++ ); |
| 282 | } |
| 283 | |
| 284 | #endif |
| 285 | |
| 286 | #endif |