Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange |
| 3 | * |
Paul Bakker | fc8c436 | 2010-03-21 17:37:16 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | /* |
| 22 | * Reference: |
| 23 | * |
| 24 | * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12) |
| 25 | */ |
| 26 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_DHM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 30 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 31 | #include "polarssl/dhm.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
| 33 | #include <string.h> |
| 34 | |
| 35 | /* |
| 36 | * helper to validate the mpi size and import it |
| 37 | */ |
| 38 | static int dhm_read_bignum( mpi *X, |
| 39 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 40 | const unsigned char *end ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | { |
| 42 | int ret, n; |
| 43 | |
| 44 | if( end - *p < 2 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 45 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | |
| 47 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 48 | (*p) += 2; |
| 49 | |
| 50 | if( (int)( end - *p ) < n ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 51 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | |
| 53 | if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 54 | return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | |
| 56 | (*p) += n; |
| 57 | |
| 58 | return( 0 ); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Parse the ServerKeyExchange parameters |
| 63 | */ |
| 64 | int dhm_read_params( dhm_context *ctx, |
| 65 | unsigned char **p, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 66 | const unsigned char *end ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | { |
| 68 | int ret, n; |
| 69 | |
| 70 | memset( ctx, 0, sizeof( dhm_context ) ); |
| 71 | |
| 72 | if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 || |
| 73 | ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 || |
| 74 | ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 ) |
| 75 | return( ret ); |
| 76 | |
| 77 | ctx->len = mpi_size( &ctx->P ); |
| 78 | |
| 79 | if( end - *p < 2 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 80 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 81 | |
| 82 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 83 | (*p) += 2; |
| 84 | |
| 85 | if( end != *p + n ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 86 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | |
| 88 | return( 0 ); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Setup and write the ServerKeyExchange parameters |
| 93 | */ |
| 94 | int dhm_make_params( dhm_context *ctx, int x_size, |
| 95 | unsigned char *output, int *olen, |
| 96 | int (*f_rng)(void *), void *p_rng ) |
| 97 | { |
| 98 | int i, ret, n, n1, n2, n3; |
| 99 | unsigned char *p; |
| 100 | |
| 101 | /* |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame^] | 102 | * Generate X as large as possible ( < P ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | */ |
| 104 | n = x_size / sizeof( t_int ); |
| 105 | MPI_CHK( mpi_grow( &ctx->X, n ) ); |
| 106 | MPI_CHK( mpi_lset( &ctx->X, 0 ) ); |
| 107 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | p = (unsigned char *) ctx->X.p; |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame^] | 109 | for( i = 0; i < x_size - 1; i++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | *p++ = (unsigned char) f_rng( p_rng ); |
| 111 | |
| 112 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 113 | mpi_shift_r( &ctx->X, 1 ); |
| 114 | |
Paul Bakker | ff7fe67 | 2010-07-18 09:45:05 +0000 | [diff] [blame^] | 115 | /* |
| 116 | * Calculate GX = G^X mod P |
| 117 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 119 | &ctx->P , &ctx->RP ) ); |
| 120 | |
| 121 | /* |
| 122 | * export P, G, GX |
| 123 | */ |
| 124 | #define DHM_MPI_EXPORT(X,n) \ |
| 125 | MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \ |
| 126 | *p++ = (unsigned char)( n >> 8 ); \ |
| 127 | *p++ = (unsigned char)( n ); p += n; |
| 128 | |
| 129 | n1 = mpi_size( &ctx->P ); |
| 130 | n2 = mpi_size( &ctx->G ); |
| 131 | n3 = mpi_size( &ctx->GX ); |
| 132 | |
| 133 | p = output; |
| 134 | DHM_MPI_EXPORT( &ctx->P , n1 ); |
| 135 | DHM_MPI_EXPORT( &ctx->G , n2 ); |
| 136 | DHM_MPI_EXPORT( &ctx->GX, n3 ); |
| 137 | |
| 138 | *olen = p - output; |
| 139 | |
| 140 | ctx->len = n1; |
| 141 | |
| 142 | cleanup: |
| 143 | |
| 144 | if( ret != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 145 | return( ret | POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | |
| 147 | return( 0 ); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Import the peer's public value G^Y |
| 152 | */ |
| 153 | int dhm_read_public( dhm_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 154 | const unsigned char *input, int ilen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | { |
| 156 | int ret; |
| 157 | |
| 158 | if( ctx == NULL || ilen < 1 || ilen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 159 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | |
| 161 | if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 162 | return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | |
| 164 | return( 0 ); |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Create own private value X and export G^X |
| 169 | */ |
| 170 | int dhm_make_public( dhm_context *ctx, int x_size, |
| 171 | unsigned char *output, int olen, |
| 172 | int (*f_rng)(void *), void *p_rng ) |
| 173 | { |
| 174 | int ret, i, n; |
| 175 | unsigned char *p; |
| 176 | |
| 177 | if( ctx == NULL || olen < 1 || olen > ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 178 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * generate X and calculate GX = G^X mod P |
| 182 | */ |
| 183 | n = x_size / sizeof( t_int ); |
| 184 | MPI_CHK( mpi_grow( &ctx->X, n ) ); |
| 185 | MPI_CHK( mpi_lset( &ctx->X, 0 ) ); |
| 186 | |
Paul Bakker | 2a1fadf | 2009-07-27 21:05:24 +0000 | [diff] [blame] | 187 | n = x_size - 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 188 | p = (unsigned char *) ctx->X.p; |
| 189 | for( i = 0; i < n; i++ ) |
| 190 | *p++ = (unsigned char) f_rng( p_rng ); |
| 191 | |
| 192 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 193 | mpi_shift_r( &ctx->X, 1 ); |
| 194 | |
| 195 | MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X, |
| 196 | &ctx->P , &ctx->RP ) ); |
| 197 | |
| 198 | MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) ); |
| 199 | |
| 200 | cleanup: |
| 201 | |
| 202 | if( ret != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 203 | return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 | |
| 205 | return( 0 ); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Derive and export the shared secret (G^Y)^X mod P |
| 210 | */ |
| 211 | int dhm_calc_secret( dhm_context *ctx, |
| 212 | unsigned char *output, int *olen ) |
| 213 | { |
| 214 | int ret; |
| 215 | |
| 216 | if( ctx == NULL || *olen < ctx->len ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 217 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 218 | |
| 219 | MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X, |
| 220 | &ctx->P, &ctx->RP ) ); |
| 221 | |
| 222 | *olen = mpi_size( &ctx->K ); |
| 223 | |
| 224 | MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) ); |
| 225 | |
| 226 | cleanup: |
| 227 | |
| 228 | if( ret != 0 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 229 | return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED | ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | |
| 231 | return( 0 ); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Free the components of a DHM key |
| 236 | */ |
| 237 | void dhm_free( dhm_context *ctx ) |
| 238 | { |
| 239 | mpi_free( &ctx->RP, &ctx->K, &ctx->GY, |
| 240 | &ctx->GX, &ctx->X, &ctx->G, |
| 241 | &ctx->P, NULL ); |
| 242 | } |
| 243 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 244 | #if defined(POLARSSL_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * Checkup routine |
| 248 | */ |
| 249 | int dhm_self_test( int verbose ) |
| 250 | { |
| 251 | return( verbose++ ); |
| 252 | } |
| 253 | |
| 254 | #endif |
| 255 | |
| 256 | #endif |