Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (server 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_PORT 11999 |
| 36 | #define PLAINTEXT "==Hello there!==" |
| 37 | |
| 38 | int main( void ) |
| 39 | { |
| 40 | FILE *f; |
| 41 | |
| 42 | int ret, n, buflen; |
| 43 | int listen_fd = -1; |
| 44 | int client_fd = -1; |
| 45 | |
| 46 | unsigned char buf[1024]; |
| 47 | unsigned char hash[20]; |
| 48 | unsigned char buf2[2]; |
| 49 | |
| 50 | havege_state hs; |
| 51 | rsa_context rsa; |
| 52 | dhm_context dhm; |
| 53 | aes_context aes; |
| 54 | |
| 55 | memset( &rsa, 0, sizeof( rsa ) ); |
| 56 | memset( &dhm, 0, sizeof( dhm ) ); |
| 57 | |
| 58 | /* |
| 59 | * 1. Setup the RNG |
| 60 | */ |
| 61 | printf( "\n . Seeding the random number generator" ); |
| 62 | fflush( stdout ); |
| 63 | |
| 64 | havege_init( &hs ); |
| 65 | |
| 66 | /* |
| 67 | * 2a. Read the server's private RSA key |
| 68 | */ |
| 69 | printf( "\n . Reading private key from rsa_priv.txt" ); |
| 70 | fflush( stdout ); |
| 71 | |
| 72 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) |
| 73 | { |
| 74 | ret = 1; |
| 75 | printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
| 76 | " ! Please run rsa_genkey first\n\n" ); |
| 77 | goto exit; |
| 78 | } |
| 79 | |
| 80 | rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL ); |
| 81 | |
| 82 | if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || |
| 83 | ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || |
| 84 | ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || |
| 85 | ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || |
| 86 | ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || |
| 87 | ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || |
| 88 | ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || |
| 89 | ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) |
| 90 | { |
| 91 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 92 | goto exit; |
| 93 | } |
| 94 | |
| 95 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 96 | |
| 97 | fclose( f ); |
| 98 | |
| 99 | /* |
| 100 | * 2b. Get the DHM modulus and generator |
| 101 | */ |
| 102 | printf( "\n . Reading DH parameters from dh_prime.txt" ); |
| 103 | fflush( stdout ); |
| 104 | |
| 105 | if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL ) |
| 106 | { |
| 107 | ret = 1; |
| 108 | printf( " failed\n ! Could not open dh_prime.txt\n" \ |
| 109 | " ! Please run dh_genprime first\n\n" ); |
| 110 | goto exit; |
| 111 | } |
| 112 | |
| 113 | if( mpi_read_file( &dhm.P, 16, f ) != 0 || |
| 114 | mpi_read_file( &dhm.G, 16, f ) != 0 ) |
| 115 | { |
| 116 | printf( " failed\n ! Invalid DH parameter file\n\n" ); |
| 117 | goto exit; |
| 118 | } |
| 119 | |
| 120 | fclose( f ); |
| 121 | |
| 122 | /* |
| 123 | * 3. Wait for a client to connect |
| 124 | */ |
| 125 | printf( "\n . Waiting for a remote connection" ); |
| 126 | fflush( stdout ); |
| 127 | |
| 128 | if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 ) |
| 129 | { |
| 130 | printf( " failed\n ! net_bind returned %d\n\n", ret ); |
| 131 | goto exit; |
| 132 | } |
| 133 | |
| 134 | if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 ) |
| 135 | { |
| 136 | printf( " failed\n ! net_accept returned %d\n\n", ret ); |
| 137 | goto exit; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * 4. Setup the DH parameters (P,G,Ys) |
| 142 | */ |
| 143 | printf( "\n . Sending the server's DH parameters" ); |
| 144 | fflush( stdout ); |
| 145 | |
| 146 | memset( buf, 0, sizeof( buf ) ); |
| 147 | |
| 148 | if( ( ret = dhm_make_params( &dhm, 256, buf, &n, |
| 149 | havege_rand, &hs ) ) != 0 ) |
| 150 | { |
| 151 | printf( " failed\n ! dhm_make_params returned %d\n\n", ret ); |
| 152 | goto exit; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * 5. Sign the parameters and send them |
| 157 | */ |
| 158 | sha1( buf, n, hash ); |
| 159 | |
| 160 | buf[n ] = (unsigned char)( rsa.len >> 8 ); |
| 161 | buf[n + 1] = (unsigned char)( rsa.len ); |
| 162 | |
| 163 | if( ( ret = rsa_pkcs1_sign( &rsa, RSA_PRIVATE, RSA_SHA1, |
| 164 | 0, hash, buf + n + 2 ) ) != 0 ) |
| 165 | { |
| 166 | printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret ); |
| 167 | goto exit; |
| 168 | } |
| 169 | |
| 170 | buflen = n + 2 + rsa.len; |
| 171 | buf2[0] = (unsigned char)( buflen >> 8 ); |
| 172 | buf2[1] = (unsigned char)( buflen ); |
| 173 | |
| 174 | if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 || |
| 175 | ( ret = net_send( &client_fd, buf, buflen ) ) != buflen ) |
| 176 | { |
| 177 | printf( " failed\n ! net_send returned %d\n\n", ret ); |
| 178 | goto exit; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * 6. Get the client's public value: Yc = G ^ Xc mod P |
| 183 | */ |
| 184 | printf( "\n . Receiving the client's public value" ); |
| 185 | fflush( stdout ); |
| 186 | |
| 187 | memset( buf, 0, sizeof( buf ) ); |
| 188 | n = dhm.len; |
| 189 | |
| 190 | if( ( ret = net_recv( &client_fd, buf, n ) ) != n ) |
| 191 | { |
| 192 | printf( " failed\n ! net_recv returned %d\n\n", ret ); |
| 193 | goto exit; |
| 194 | } |
| 195 | |
| 196 | if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 ) |
| 197 | { |
| 198 | printf( " failed\n ! dhm_read_public returned %d\n\n", ret ); |
| 199 | goto exit; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * 7. Derive the shared secret: K = Ys ^ Xc mod P |
| 204 | */ |
| 205 | printf( "\n . Shared secret: " ); |
| 206 | fflush( stdout ); |
| 207 | |
| 208 | if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 ) |
| 209 | { |
| 210 | printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret ); |
| 211 | goto exit; |
| 212 | } |
| 213 | |
| 214 | for( n = 0; n < 16; n++ ) |
| 215 | printf( "%02x", buf[n] ); |
| 216 | |
| 217 | /* |
| 218 | * 8. Setup the AES-256 encryption key |
| 219 | * |
| 220 | * This is an overly simplified example; best practice is |
| 221 | * to hash the shared secret with a random value to derive |
| 222 | * the keying material for the encryption/decryption keys |
| 223 | * and MACs. |
| 224 | */ |
| 225 | printf( "...\n . Encrypting and sending the ciphertext" ); |
| 226 | fflush( stdout ); |
| 227 | |
| 228 | aes_setkey_enc( &aes, buf, 256 ); |
| 229 | memcpy( buf, PLAINTEXT, 16 ); |
| 230 | aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf ); |
| 231 | |
| 232 | if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 ) |
| 233 | { |
| 234 | printf( " failed\n ! net_send returned %d\n\n", ret ); |
| 235 | goto exit; |
| 236 | } |
| 237 | |
| 238 | printf( "\n\n" ); |
| 239 | |
| 240 | exit: |
| 241 | |
| 242 | net_close( client_fd ); |
| 243 | rsa_free( &rsa ); |
| 244 | dhm_free( &dhm ); |
| 245 | |
| 246 | #ifdef WIN32 |
| 247 | printf( " + Press Enter to exit this program.\n" ); |
| 248 | fflush( stdout ); getchar(); |
| 249 | #endif |
| 250 | |
| 251 | return( ret ); |
| 252 | } |