Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange |
| 3 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame^] | 4 | * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org> |
| 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame^] | 7 | * Joined copyright on original XySSL code with: Christophe Devine |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | */ |
| 23 | /* |
| 24 | * Reference: |
| 25 | * |
| 26 | * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12) |
| 27 | */ |
| 28 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 29 | #include "polarssl/config.h" |
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 | #if defined(POLARSSL_DHM_C) |
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 | #include "polarssl/dhm.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
| 35 | #include <string.h> |
| 36 | |
| 37 | /* |
| 38 | * helper to validate the mpi size and import it |
| 39 | */ |
| 40 | static int dhm_read_bignum( mpi *X, |
| 41 | unsigned char **p, |
| 42 | unsigned char *end ) |
| 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 | 40e4694 | 2009-01-03 21:51:57 +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 | /* |
| 64 | * Parse the ServerKeyExchange parameters |
| 65 | */ |
| 66 | int dhm_read_params( dhm_context *ctx, |
| 67 | unsigned char **p, |
| 68 | unsigned char *end ) |
| 69 | { |
| 70 | int ret, n; |
| 71 | |
| 72 | memset( ctx, 0, sizeof( dhm_context ) ); |
| 73 | |
| 74 | if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 || |
| 75 | ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 || |
| 76 | ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 ) |
| 77 | return( ret ); |
| 78 | |
| 79 | ctx->len = mpi_size( &ctx->P ); |
| 80 | |
| 81 | if( end - *p < 2 ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 82 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | |
| 84 | n = ( (*p)[0] << 8 ) | (*p)[1]; |
| 85 | (*p) += 2; |
| 86 | |
| 87 | if( end != *p + n ) |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 88 | return( POLARSSL_ERR_DHM_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | |
| 90 | return( 0 ); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Setup and write the ServerKeyExchange parameters |
| 95 | */ |
| 96 | int dhm_make_params( dhm_context *ctx, int x_size, |
| 97 | unsigned char *output, int *olen, |
| 98 | int (*f_rng)(void *), void *p_rng ) |
| 99 | { |
| 100 | int i, ret, n, n1, n2, n3; |
| 101 | unsigned char *p; |
| 102 | |
| 103 | /* |
| 104 | * generate X and calculate GX = G^X mod P |
| 105 | */ |
| 106 | n = x_size / sizeof( t_int ); |
| 107 | MPI_CHK( mpi_grow( &ctx->X, n ) ); |
| 108 | MPI_CHK( mpi_lset( &ctx->X, 0 ) ); |
| 109 | |
Paul Bakker | 2a1fadf | 2009-07-27 21:05:24 +0000 | [diff] [blame] | 110 | n = x_size - 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | p = (unsigned char *) ctx->X.p; |
| 112 | for( i = 0; i < n; i++ ) |
| 113 | *p++ = (unsigned char) f_rng( p_rng ); |
| 114 | |
| 115 | while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 ) |
| 116 | mpi_shift_r( &ctx->X, 1 ); |
| 117 | |
| 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, |
| 154 | unsigned char *input, int ilen ) |
| 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 |