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