Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (client side) |
| 3 | * |
| 4 | * Copyright (C) 2006-2007 Christophe Devine |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 22 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 23 | #endif |
| 24 | |
| 25 | #include <string.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include "xyssl/net.h" |
| 29 | #include "xyssl/aes.h" |
| 30 | #include "xyssl/dhm.h" |
| 31 | #include "xyssl/rsa.h" |
| 32 | #include "xyssl/sha1.h" |
| 33 | #include "xyssl/havege.h" |
| 34 | |
| 35 | #define SERVER_NAME "localhost" |
| 36 | #define SERVER_PORT 11999 |
| 37 | |
| 38 | int main( void ) |
| 39 | { |
| 40 | FILE *f; |
| 41 | |
| 42 | int ret, n, buflen; |
| 43 | int server_fd = -1; |
| 44 | |
| 45 | unsigned char *p, *end; |
| 46 | unsigned char buf[1024]; |
| 47 | unsigned char hash[20]; |
| 48 | |
| 49 | havege_state hs; |
| 50 | rsa_context rsa; |
| 51 | dhm_context dhm; |
| 52 | aes_context aes; |
| 53 | |
| 54 | memset( &rsa, 0, sizeof( rsa ) ); |
| 55 | memset( &dhm, 0, sizeof( dhm ) ); |
| 56 | |
| 57 | /* |
| 58 | * 1. Setup the RNG |
| 59 | */ |
| 60 | printf( "\n . Seeding the random number generator" ); |
| 61 | fflush( stdout ); |
| 62 | |
| 63 | havege_init( &hs ); |
| 64 | |
| 65 | /* |
| 66 | * 2. Read the server's public RSA key |
| 67 | */ |
| 68 | printf( "\n . Reading public key from rsa_pub.txt" ); |
| 69 | fflush( stdout ); |
| 70 | |
| 71 | if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL ) |
| 72 | { |
| 73 | ret = 1; |
| 74 | printf( " failed\n ! Could not open rsa_pub.txt\n" \ |
| 75 | " ! Please run rsa_genkey first\n\n" ); |
| 76 | goto exit; |
| 77 | } |
| 78 | |
| 79 | rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL ); |
| 80 | |
| 81 | if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 || |
| 82 | ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 ) |
| 83 | { |
| 84 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 85 | goto exit; |
| 86 | } |
| 87 | |
| 88 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 89 | |
| 90 | fclose( f ); |
| 91 | |
| 92 | /* |
| 93 | * 3. Initiate the connection |
| 94 | */ |
| 95 | printf( "\n . Connecting to tcp/%s/%d", SERVER_NAME, |
| 96 | SERVER_PORT ); |
| 97 | fflush( stdout ); |
| 98 | |
| 99 | if( ( ret = net_connect( &server_fd, SERVER_NAME, |
| 100 | SERVER_PORT ) ) != 0 ) |
| 101 | { |
| 102 | printf( " failed\n ! net_connect returned %d\n\n", ret ); |
| 103 | goto exit; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * 4a. First get the buffer length |
| 108 | */ |
| 109 | printf( "\n . Receiving the server's DH parameters" ); |
| 110 | fflush( stdout ); |
| 111 | |
| 112 | memset( buf, 0, sizeof( buf ) ); |
| 113 | |
| 114 | if( ( ret = net_recv( &server_fd, buf, 2 ) ) != 2 ) |
| 115 | { |
| 116 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 117 | goto exit; |
| 118 | } |
| 119 | |
| 120 | n = buflen = ( buf[0] << 8 ) | buf[1]; |
| 121 | if( buflen < 1 || buflen > (int) sizeof( buf ) ) |
| 122 | { |
| 123 | printf( " failed\n ! Got an invalid buffer length\n\n" ); |
| 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P |
| 129 | */ |
| 130 | memset( buf, 0, sizeof( buf ) ); |
| 131 | |
| 132 | if( ( ret = net_recv( &server_fd, buf, n ) ) != n ) |
| 133 | { |
| 134 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 135 | goto exit; |
| 136 | } |
| 137 | |
| 138 | p = buf, end = buf + buflen; |
| 139 | |
| 140 | if( ( ret = dhm_read_params( &dhm, &p, end ) ) != 0 ) |
| 141 | { |
| 142 | printf( " failed\n ! dhm_read_params returned %d\n\n", ret ); |
| 143 | goto exit; |
| 144 | } |
| 145 | |
| 146 | if( dhm.len < 64 || dhm.len > 256 ) |
| 147 | { |
| 148 | ret = 1; |
| 149 | printf( " failed\n ! Invalid DHM modulus size\n\n" ); |
| 150 | goto exit; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * 5. Check that the server's RSA signature matches |
| 155 | * the SHA-1 hash of (P,G,Ys) |
| 156 | */ |
| 157 | printf( "\n . Verifying the server's RSA signature" ); |
| 158 | fflush( stdout ); |
| 159 | |
| 160 | if( ( n = (int)( end - p ) ) != rsa.len ) |
| 161 | { |
| 162 | ret = 1; |
| 163 | printf( " failed\n ! Invalid RSA signature size\n\n" ); |
| 164 | goto exit; |
| 165 | } |
| 166 | |
| 167 | sha1( buf, (int)( p - 2 - buf ), hash ); |
| 168 | |
| 169 | if( ( ret = rsa_pkcs1_verify( &rsa, RSA_PUBLIC, RSA_SHA1, |
| 170 | 0, hash, p ) ) != 0 ) |
| 171 | { |
| 172 | printf( " failed\n ! rsa_pkcs1_verify returned %d\n\n", ret ); |
| 173 | goto exit; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * 6. Send our public value: Yc = G ^ Xc mod P |
| 178 | */ |
| 179 | printf( "\n . Sending own public value to server" ); |
| 180 | fflush( stdout ); |
| 181 | |
| 182 | n = dhm.len; |
| 183 | if( ( ret = dhm_make_public( &dhm, 256, buf, n, |
| 184 | havege_rand, &hs ) ) != 0 ) |
| 185 | { |
| 186 | printf( " failed\n ! dhm_make_public returned %d\n\n", ret ); |
| 187 | goto exit; |
| 188 | } |
| 189 | |
| 190 | if( ( ret = net_send( &server_fd, buf, n ) ) != n ) |
| 191 | { |
| 192 | printf( " failed\n ! net_send returned %d\n\n", ret ); |
| 193 | goto exit; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * 7. Derive the shared secret: K = Ys ^ Xc mod P |
| 198 | */ |
| 199 | printf( "\n . Shared secret: " ); |
| 200 | fflush( stdout ); |
| 201 | |
| 202 | n = dhm.len; |
| 203 | if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 ) |
| 204 | { |
| 205 | printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret ); |
| 206 | goto exit; |
| 207 | } |
| 208 | |
| 209 | for( n = 0; n < 16; n++ ) |
| 210 | printf( "%02x", buf[n] ); |
| 211 | |
| 212 | /* |
| 213 | * 8. Setup the AES-256 decryption key |
| 214 | * |
| 215 | * This is an overly simplified example; best practice is |
| 216 | * to hash the shared secret with a random value to derive |
| 217 | * the keying material for the encryption/decryption keys, |
| 218 | * IVs and MACs. |
| 219 | */ |
| 220 | printf( "...\n . Receiving and decrypting the ciphertext" ); |
| 221 | fflush( stdout ); |
| 222 | |
| 223 | aes_setkey_dec( &aes, buf, 256 ); |
| 224 | |
| 225 | memset( buf, 0, sizeof( buf ) ); |
| 226 | |
| 227 | if( ( ret = net_recv( &server_fd, buf, 16 ) ) != 16 ) |
| 228 | { |
| 229 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 230 | goto exit; |
| 231 | } |
| 232 | |
| 233 | aes_crypt_ecb( &aes, AES_DECRYPT, buf, buf ); |
| 234 | buf[16] = '\0'; |
| 235 | printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf ); |
| 236 | |
| 237 | exit: |
| 238 | |
| 239 | net_close( server_fd ); |
| 240 | rsa_free( &rsa ); |
| 241 | dhm_free( &dhm ); |
| 242 | |
| 243 | #ifdef WIN32 |
| 244 | printf( " + Press Enter to exit this program.\n" ); |
| 245 | fflush( stdout ); getchar(); |
| 246 | #endif |
| 247 | |
| 248 | return( ret ); |
| 249 | } |