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 | |
| 37 | #include <string.h> |
| 38 | |
| 39 | /* |
| 40 | * helper to validate the mpi size and import it |
| 41 | */ |
| 42 | static int dhm_read_bignum( mpi *X, |
| 43 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 44 | const unsigned char *end ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | { |
| 46 | int ret, n; |
| 47 | |
| 48 | if( end - *p < 2 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 49 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | |
| 51 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 52 | (*p) += 2; |
| 53 | |
| 54 | if( (int)( end - *p ) < n ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 55 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | |
| 57 | if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 58 | return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | |
| 60 | (*p) += n; |
| 61 | |
| 62 | return( 0 ); |
| 63 | } |
| 64 | |
| 65 | /* |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 66 | * Verify sanity of public parameter with regards to P |
| 67 | * |
| 68 | * Public parameter should be: 2 <= public_param <= P - 2 |
| 69 | * |
| 70 | * For more information on the attack, see: |
| 71 | * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf |
| 72 | * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643 |
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 | static int dhm_check_range( const mpi *public_param, const mpi *P ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 75 | { |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 76 | mpi L, U; |
| 77 | int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA; |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 78 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 79 | mpi_init( &L, &U, NULL ); |
| 80 | mpi_lset( &L, 2 ); |
| 81 | mpi_sub_int( &U, P, 2 ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 82 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 83 | if( mpi_cmp_mpi( public_param, &L ) >= 0 && |
| 84 | mpi_cmp_mpi( public_param, &U ) <= 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 85 | { |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 86 | ret = 0; |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 89 | mpi_free( &L, &U, NULL ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 90 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 91 | return( ret ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | * Parse the ServerKeyExchange parameters |
| 96 | */ |
| 97 | int dhm_read_params( dhm_context *ctx, |
| 98 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 99 | const unsigned char *end ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | { |
| 101 | int ret, n; |
| 102 | |
| 103 | memset( ctx, 0, sizeof( dhm_context ) ); |
| 104 | |
| 105 | if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 || |
| 106 | ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 || |
| 107 | ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 ) |
| 108 | return( ret ); |
| 109 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 110 | if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) |
| 111 | return( ret ); |
| 112 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | ctx->len = mpi_size( &ctx->P ); |
| 114 | |
| 115 | if( end - *p < 2 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 116 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | |
| 118 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 119 | (*p) += 2; |
| 120 | |
| 121 | if( end != *p + n ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 122 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | |
| 124 | return( 0 ); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Setup and write the ServerKeyExchange parameters |
| 129 | */ |
| 130 | int dhm_make_params( dhm_context *ctx, int x_size, |
| 131 | unsigned char *output, int *olen, |
| 132 | int (*f_rng)(void *), void *p_rng ) |
| 133 | { |
| 134 | int i, ret, n, n1, n2, n3; |
| 135 | unsigned char *p; |
| 136 | |
| 137 | /* |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 138 | * Generate X as large as possible ( < P ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | */ |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 140 | n = x_size / sizeof( t_int ) + 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame^] | 142 | mpi_fill_random( &ctx->X, n, f_rng, p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | |
| 144 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 145 | mpi_shift_r( &ctx->X, 1 ); |
| 146 | |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame] | 147 | /* |
| 148 | * Calculate GX = G^X mod P |
| 149 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 151 | &ctx->P , &ctx->RP ) ); |
| 152 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 153 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 154 | return( ret ); |
| 155 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | /* |
| 157 | * export P, G, GX |
| 158 | */ |
| 159 | #define DHM_MPI_EXPORT(X,n) \ |
| 160 | MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \ |
| 161 | *p++ = (unsigned char)( n >> 8 ); \ |
| 162 | *p++ = (unsigned char)( n ); p += n; |
| 163 | |
| 164 | n1 = mpi_size( &ctx->P ); |
| 165 | n2 = mpi_size( &ctx->G ); |
| 166 | n3 = mpi_size( &ctx->GX ); |
| 167 | |
| 168 | p = output; |
| 169 | DHM_MPI_EXPORT( &ctx->P , n1 ); |
| 170 | DHM_MPI_EXPORT( &ctx->G , n2 ); |
| 171 | DHM_MPI_EXPORT( &ctx->GX, n3 ); |
| 172 | |
| 173 | *olen = p - output; |
| 174 | |
| 175 | ctx->len = n1; |
| 176 | |
| 177 | cleanup: |
| 178 | |
| 179 | if( ret != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 180 | return( ret | POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | |
| 182 | return( 0 ); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Import the peer's public value G^Y |
| 187 | */ |
| 188 | int dhm_read_public( dhm_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 189 | const unsigned char *input, int ilen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | { |
| 191 | int ret; |
| 192 | |
| 193 | if( ctx == NULL || ilen < 1 || ilen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 194 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | |
| 196 | if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 197 | return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | |
| 199 | return( 0 ); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Create own private value X and export G^X |
| 204 | */ |
| 205 | int dhm_make_public( dhm_context *ctx, int x_size, |
| 206 | unsigned char *output, int olen, |
| 207 | int (*f_rng)(void *), void *p_rng ) |
| 208 | { |
| 209 | int ret, i, n; |
| 210 | unsigned char *p; |
| 211 | |
| 212 | if( ctx == NULL || olen < 1 || olen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 213 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * generate X and calculate GX = G^X mod P |
| 217 | */ |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 218 | n = x_size / sizeof( t_int ) + 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame^] | 220 | mpi_fill_random( &ctx->X, n, f_rng, p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | |
| 222 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 223 | mpi_shift_r( &ctx->X, 1 ); |
| 224 | |
| 225 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 226 | &ctx->P , &ctx->RP ) ); |
| 227 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 228 | if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 ) |
| 229 | return( ret ); |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 230 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) ); |
| 232 | |
| 233 | cleanup: |
| 234 | |
| 235 | if( ret != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 236 | return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 237 | |
| 238 | return( 0 ); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Derive and export the shared secret (G^Y)^X mod P |
| 243 | */ |
| 244 | int dhm_calc_secret( dhm_context *ctx, |
| 245 | unsigned char *output, int *olen ) |
| 246 | { |
| 247 | int ret; |
| 248 | |
| 249 | if( ctx == NULL || *olen < ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 250 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | |
| 252 | MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X, |
| 253 | &ctx->P, &ctx->RP ) ); |
| 254 | |
Paul Bakker | 345a6fe | 2011-02-28 21:20:02 +0000 | [diff] [blame] | 255 | if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) |
Paul Bakker | c47840e | 2011-02-20 16:37:30 +0000 | [diff] [blame] | 256 | return( ret ); |
| 257 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 258 | *olen = mpi_size( &ctx->K ); |
| 259 | |
| 260 | MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) ); |
| 261 | |
| 262 | cleanup: |
| 263 | |
| 264 | if( ret != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 265 | return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 266 | |
| 267 | return( 0 ); |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * Free the components of a DHM key |
| 272 | */ |
| 273 | void dhm_free( dhm_context *ctx ) |
| 274 | { |
| 275 | mpi_free( &ctx->RP, &ctx->K, &ctx->GY, |
| 276 | &ctx->GX, &ctx->X, &ctx->G, |
| 277 | &ctx->P, NULL ); |
| 278 | } |
| 279 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 280 | #if defined(POLARSSL_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * Checkup routine |
| 284 | */ |
| 285 | int dhm_self_test( int verbose ) |
| 286 | { |
| 287 | return( verbose++ ); |
| 288 | } |
| 289 | |
| 290 | #endif |
| 291 | |
| 292 | #endif |