00001
00002
00003 #include "interface.h"
00004 #ifdef _WIN32
00005 #include "winsock2.h"
00006 #include <memory.h>
00007 #else
00008 #include <sys/socket.h>
00009 #endif
00010
00011
00012
00013
00014 #ifdef OBS
00015 static void mystrncpy_binary(char *dest, char *src, int start_index, int length)
00016 {
00017
00018 int i, j, len;
00019 j=start_index;
00020 len = strlen(src);
00021 memset(dest, '\0', strlen(dest) );
00022 for(i=0; j < (start_index+length) ; i++,j++ ){
00023 dest[i] = src[j];
00024 }
00025 }
00026 #endif // OBS
00027
00028 static void separate_key_parts(char *key, char *N, char *E, char *D, char *P, char *Q, char *DP, char *DQ, char*QP)
00029 {
00030 int i = 0, j = 0;
00031 #ifdef COM
00032 printf("key : '%s'\n",key);
00033 printf(" . key length %d\n", strlen(key) );
00034 #endif
00035 while( key[i] != '\n') {
00036 N[j] = key[i];
00037 i++;
00038 j++;
00039 }
00040 N[j++]='\n';
00041 N[j] = '\0';
00042 i++;
00043 j=0;
00044 while(key[i] != '\n'){
00045 E[j++]=key[i++];
00046 }
00047 E[j++]='\n';
00048 E[j]='\0';
00049
00050 if( strlen(key) > 500 )
00051 {
00052 j=0; i++;
00053 while(key[i] != '\n')
00054 D[j++] = key[i++];
00055 D[j++]='\n';
00056 D[j] = '\0';
00057
00058 j=0; i++;
00059 while(key[i] != '\n')
00060 P[j++] = key[i++];
00061 P[j++]='\n';
00062 P[j]='\0';
00063
00064 j=0; i++;
00065 while(key[i] != '\n')
00066 Q[j++] = key[i++];
00067 Q[j++]='\n';
00068 Q[j] = '\0';
00069
00070 j=0; i++;
00071 while(key[i] != '\n')
00072 DP[j++] = key[i++];
00073 DP[j++]='\n';
00074 DP[j] = '\0';
00075
00076 j=0; i++;
00077 while(key[i] != '\n')
00078 DQ[j++] = key[i++];
00079 DQ[j++]='\n';
00080 DQ[j] = '\0';
00081
00082 j=0; i++;
00083 while(key[i] != '\n')
00084 QP[j++] = key[i++];
00085 QP[j++]='\n';
00086 QP[j]='\0';
00087
00088 }
00089 }
00090
00091 int rsa_encryption(char *publickey, char *plaintext, char *ciphertext)
00092 {
00093 int ret, ilen, status, mode;
00094 rsa_context rsa;
00095 char N[265], E[13];
00096
00097 memset(N, '\0', 265);
00098 memset(E, '\0', 13);
00099
00100 separate_key_parts(publickey, N, E, NULL, NULL, NULL, NULL, NULL, NULL);
00101
00102 rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL );
00103 if( ( ret = mpi_read_mystring( &rsa.N, 16, N ) ) != 0 ||
00104 ( ret = mpi_read_mystring( &rsa.E, 16, E ) ) != 0 )
00105 {
00106 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
00107 return -1;
00108 }
00109 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
00110
00111 if (rsa_check_pubkey(&rsa) != 0){
00112 printf("Public key is invalid \n");
00113 printf("please ensure: Remove space at the end of each public key from known_host file \n");
00114 return -1;
00115 }
00116
00117
00118 ilen = strlen(plaintext);
00119 mode = RSA_EN;
00120 status = rsa_pkcs1_encrypt(
00121 &rsa,
00122 mode,
00123 ilen,
00124 (unsigned char*)plaintext,
00125 (unsigned char*)ciphertext );
00126 #ifdef COM
00127 printf( " . Encryption status = %d \n",status );
00128 #endif
00129 return status;
00130
00131 }
00132
00133 int rsa_decryption(char *ciphertext, char *plaintext, char *privatekey)
00134 {
00135 rsa_context rsa;
00136 int ret, olen, status, mode;
00137
00138 char N[265], E[13], D[265], P[140], Q[140], DP[140], DQ[140], QP[140];
00139
00140 memset(N,'\0',265); memset(E,'\0',13); memset(D,'\0',265); memset(P,'\0',140);
00141 memset(Q,'\0',140); memset(DP,'\0',140); memset(DQ,'\0',140); memset(QP,'\0',140);
00142
00143 separate_key_parts(privatekey, N, E, D, P, Q, DP, DQ, QP);
00144
00145 rsa_init( &rsa, RSA_PKCS_V15, 0, NULL, NULL );
00146 if( ( ret = mpi_read_mystring( &rsa.N , 16, N ) ) != 0 ||
00147 ( ret = mpi_read_mystring( &rsa.E , 16, E ) ) != 0 ||
00148 ( ret = mpi_read_mystring( &rsa.D , 16, D ) ) != 0 ||
00149 ( ret = mpi_read_mystring( &rsa.P , 16, P ) ) != 0 ||
00150 ( ret = mpi_read_mystring( &rsa.Q , 16, Q ) ) != 0 ||
00151 ( ret = mpi_read_mystring( &rsa.DP, 16, DP ) ) != 0 ||
00152 ( ret = mpi_read_mystring( &rsa.DQ, 16, DQ ) ) != 0 ||
00153 ( ret = mpi_read_mystring( &rsa.QP, 16, QP ) ) != 0 )
00154 {
00155 #ifdef COM
00156 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
00157 #endif
00158 return -1;
00159 }
00160 if (rsa_check_privkey(&rsa) != 0){
00161 #ifdef COM
00162 printf("Private key is invalid \n");
00163 #endif
00164 return -1;
00165 }else
00166 #ifdef COM
00167 printf("Private Key is valid \n");
00168 #endif
00169
00170 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
00171
00172 mode = RSA_DE;
00173
00174 status = rsa_pkcs1_decrypt(
00175 &rsa,
00176 mode,
00177 &olen,
00178 (unsigned char*)ciphertext,
00179 (unsigned char*)plaintext );
00180 plaintext[strlen(plaintext)] = '\0';
00181 #ifdef COM
00182 printf( " . Decryption status = %d \n",status );
00183 #endif
00184 return status;
00185 }
00186
00187 static int append_nonce_to_MA(int *my_nonce, char *MA_file )
00188 {
00189 char nonce_str[10];
00190 FILE *fd;
00191 #ifdef COM
00192 printf("Append nonce to MA file \n");
00193 #endif
00194
00195 if ( ( fd = fopen(MA_file, "a+") ) == NULL ){
00196 #ifdef COM
00197 printf("fopen() error. \n");
00198 #endif
00199 return -1;
00200 }
00201
00202 (*my_nonce)+=2;
00203 memset(nonce_str, '\0', 10);
00204 sprintf(nonce_str, "%d", *my_nonce );
00205 fputs("\n", fd);
00206 if (fputs (nonce_str, fd) == EOF ){
00207 #ifdef COM
00208 printf("fputs() error. \n");
00209 #endif
00210 fclose(fd);
00211 return -1;
00212 }
00213 #ifdef COM
00214 printf(". nonce append \n");
00215 #endif
00216 fclose(fd);
00217 return 1;
00218 }
00219
00220 static int remove_nonce_from_MA(char *MA_file){
00221
00222 FILE *fd;
00223 struct stat stbuf;
00224 int len;
00225 char *MA_buffer, *p;
00226 int numbytes;
00227
00228 if ( ( fd = fopen(MA_file, "r+") ) == NULL ){
00229 printf(" . Unable to open MA file \n");
00230 return -1;
00231 }
00232
00233 stat(MA_file ,&stbuf );
00234
00235
00236 MA_buffer = (char*) malloc (sizeof(char)*((int)stbuf.st_size + 2) );
00237 if (MA_buffer == NULL){
00238 printf("malloc() error. \n");
00239 fclose(fd);
00240 return -1;
00241 }
00242 memset(MA_buffer, '\0', (int)stbuf.st_size);
00243
00244 len = (int)stbuf.st_size;
00245 fread ( MA_buffer, len, 1, fd);
00246 fclose(fd);
00247 p = strstr (MA_buffer, "</MOBILEC_MESSAGE>");
00248 p[18] = '\r';
00249 p[19] = '\0';
00250
00251 len = strlen(MA_buffer);
00252
00253 if( (fd = fopen(MA_file, "w")) == NULL ){
00254 printf("fopen() error. ");
00255 free(MA_buffer);
00256 return -1;
00257 }
00258 numbytes = fwrite (MA_buffer , strlen(MA_buffer), 1, fd );
00259 fclose(fd);
00260 free(MA_buffer);
00261 return 1;
00262 }
00263
00264 static int extract_nonce_from_MA(int sockfd, int *my_nonce, char *MA_file)
00265 {
00266 char nonce_str[15];
00267 FILE *fd;
00268 struct stat stbuf;
00269 int rece_nonce;
00270 char ack[4];
00271 int numbytes;
00272 #ifdef COM
00273 printf("Extract and verify Nonce from MA file \n");
00274 #endif
00275 if ( ( fd = fopen(MA_file, "r+") ) == NULL ){
00276 printf("Error while opening a file \n");
00277 return -1;
00278 }
00279
00280
00281 stat(MA_file ,&stbuf );
00282
00283
00284 fseek ( fd , ((int)stbuf.st_size)-11 , SEEK_SET );
00285
00286
00287 memset(nonce_str, '\0', 15);
00288 fread ( nonce_str, 11, 1, fd);
00289
00290 if( strlen(nonce_str) < 10 ){
00291 printf(" . Error while reading nonce \n");
00292 fclose(fd);
00293 return -1;
00294 }
00295 rece_nonce = atoi(nonce_str);
00296 fclose(fd);
00297
00298
00299 remove_nonce_from_MA(MA_file);
00300
00301
00302 (*my_nonce)++;
00303 if( rece_nonce != (*my_nonce) ){
00304 #ifdef COM
00305 printf(" . nonce doesn't matches \n");
00306 printf(" . remove received MA file\n");
00307 #endif
00308 remove(MA_file);
00309 strcpy(ack,"-ve");
00310 }
00311 else{
00312 strcpy(ack,"+ve");
00313 }
00314
00315 #ifdef COM
00316 printf(" . sending acknowledge \n");
00317 #endif
00318 if ( (numbytes=send(sockfd , ack, 20, 0)) == -1){
00319 perror("send \n");
00320 return -1;
00321 }
00322 #ifdef COM
00323 printf(" . send \n");
00324 #endif
00325 return 1;
00326 }
00327
00328
00329 int read_known_host_file(char *pubkey, char *hname, char *filename)
00330 {
00331 FILE *fd;
00332 char c;
00333 char hostname[40];
00334 int first,second,i,j;
00335 int found=-1;
00336 char temp_E[15];
00337 memset(pubkey,'\0', 300);
00338 memset(temp_E, '\0', 15);
00339 second = i = j= 0;
00340 first = 1;
00341 #ifdef COM
00342 printf("Known host file lookup for peer %s's public key ... \n", hname);
00343 #endif
00344
00345 if ( (fd = fopen (filename,"r"))== NULL ) {
00346 printf("fopen() error. %s:%d\n", __FILE__, __LINE__);
00347 return -1;
00348 }
00349 while( (c=fgetc(fd)) != EOF ){
00350 if(c =='#' ){
00351 pubkey[j]='\0';
00352 first = 1; second = 0;
00353 i = j = 0;
00354 c = fgetc(fd);
00355 if(c == ' ')
00356 while(fgetc(fd)==' ');
00357 if (found == 1)
00358 break;
00359 }
00360 else if( second ){
00361
00362 }
00363 else if(c == ' ' && first){
00364 first = 0; second = 1;
00365 hostname[i]='\0';
00366 if ( !strcmp(hostname, hname) ){
00367 found=1;
00368 for (i=0; i<260; i++)
00369 pubkey[j++]= fgetc(fd);
00370 pubkey[j]='\n';
00371 while(fgetc(fd)!='\n');
00372 j++;
00373 for (i=0; i<10; i++)
00374 pubkey[j++]=fgetc(fd);
00375 pubkey[j]='\n';
00376
00377 break;
00378 }else{
00379 memset(hostname, '\0', 40);
00380 memset(pubkey, '\0', 300);
00381 }
00382 }
00383 else if(first && c!=' ' && c !='\r' && c!='\n'){
00384 hostname[i++] = c;
00385 }
00386 }
00387
00388 fclose(fd);
00389 #ifdef COM
00390 printf("read_known_host_file(): Public key of connected peer : %s \n", pubkey);
00391 #endif
00392
00393 if(found == -1)
00394 pubkey=NULL;
00395 return found;
00396 }
00397
00398 int read_encrypted_file(char *enfile, char *string, unsigned char *passphase)
00399 {
00400 char keyfile[] = "keyfile";
00401 int num=0;
00402 FILE *f;
00403 int result;
00404 struct stat stbuf;
00405 #ifdef COM
00406 printf("Reading private key file \n");
00407 #endif
00408
00409 if( aes_en_de(1, enfile, keyfile, passphase, &num, 0) == -1){
00410 printf("aes_en_de() error. \n");
00411 return -1;
00412 }
00413
00414 if( ( f = fopen( keyfile, "rb+" ) ) == NULL ){
00415 #ifdef COM
00416
00417 printf( " failed ! Could not open to read %s \n", enfile);
00418 #endif
00419 return -1;
00420 }
00421 stat(keyfile ,&stbuf );
00422 result = fread ( string, 1, (int)stbuf.st_size, f);
00423 fclose(f);
00424 if( remove(keyfile) ) printf("read_encrypted_file(): remove error");
00425
00426 #ifdef COM
00427 printf("Successfully read the key file \n");
00428 #endif
00429 return 1;
00430 }
00431
00432 int initiate_migration_process(int new_fd, int *my_nonce, char *pubkey, char *privkey, unsigned char *aes_key)
00433 {
00434
00435 char my_challengetext[80], rece_challengetext[80];
00436 char my_MD5_digest_hex[35], rece_MD5_digest_hex[35], solve_MD5_digest_hex[35];
00437 char rece_plaintext[135];
00438 char my_nonce_str[11], rece_nonce_str[11];
00439 char auth_status[5];
00440 char rece_auth_status[5];
00441 char ciphertext[135];
00442 int rece_nonce;
00443
00444 int len_n, len_c;
00445 int numbytes, status = -1, i, j;
00446 char number[10];
00447
00448 unsigned char digest[16];
00449 char ch,temp[5];
00450
00451 char publickey[1024];
00452 char privatekey[1210];
00453
00454
00455 memset(privatekey, '\0', 1210);
00456 memset(publickey, '\0', 1024);
00457 memset(my_challengetext, '\0', 80);
00458 memset(rece_challengetext, '\0', 80);
00459 memset(my_MD5_digest_hex, '\0', 35);
00460 memset(rece_MD5_digest_hex, '\0', 35);
00461 memset(solve_MD5_digest_hex, '\0', 35);
00462 memset(my_nonce_str, '\0', 11);
00463 memset(rece_nonce_str, '\0', 11);
00464 memset(ciphertext, '\0', 135);
00465 memset(rece_plaintext, '\0', 135);
00466 memset(temp, '\0', 5);
00467 memset(number, '\0', 10);
00468 memset(auth_status, '\0', 5);
00469 memset(rece_auth_status, '\0', 5);
00470 strcpy(publickey , pubkey);
00471 strcpy(privatekey, privkey);
00472 strcpy(auth_status, "-ve");
00473
00479 #ifdef COM
00480 printf("\n ################################### \n");
00481 printf("Create challenge and nonce, encrypt them and sending other peer for authentication. \n");
00482 printf("Send at socket %d \n", new_fd);
00483 #endif
00484 generate_AES_key(my_challengetext);
00485 strcpy((char*)aes_key, my_challengetext);
00486
00487 #ifdef COM
00488 printf("AES key = %s size %d \n",aes_key, strlen(aes_key) );
00489 printf(" . challenge text created \n");
00490 #endif
00491
00492 while ( (*my_nonce) < 1000000020 ){
00493 havege_state hs;
00494 havege_init( &hs );
00495 (*my_nonce) = havege_rand(&hs);
00496
00497
00498
00499 (*my_nonce) = (*my_nonce)-10;
00500 }
00501
00502 sprintf(my_nonce_str, "%d", *my_nonce);
00503 #ifdef COM
00504 printf(" . nonce generated \n");
00505 #endif
00506
00507
00508 len_c =strlen(my_challengetext);
00509 len_n = strlen(my_nonce_str);
00510 for(i=len_c, j=0 ; i < (len_c+len_n); i++, j++ )
00511 my_challengetext[i] = my_nonce_str[j];
00512
00513
00514 if( rsa_encryption (publickey, my_challengetext, ciphertext) != 0){
00515
00516 printf(" . fail to encrypt challengetext \n");
00517 return -1;
00518 }
00519 #ifdef COM
00520 printf(" . encrypted \n");
00521 #endif
00522 if ((numbytes = send(new_fd , ciphertext, 135, 0)) == -1){
00523
00524 perror("send \n");
00525 return -1;
00526 }
00527 #ifdef COM
00528 printf(" . send \n");
00529 #endif
00530
00531
00532
00533 md5( (unsigned char *) my_challengetext, strlen(my_challengetext), digest );
00534
00535
00536
00537 for( i = 0,j=0; i < 16; i++,j=j+2 ){
00538 sprintf(temp, "%02x", digest[i]);
00539 my_MD5_digest_hex[j] = temp[0];
00540 my_MD5_digest_hex[j+1] = temp[1];
00541 }
00542
00549
00550 memset(ciphertext, '\0', 135);
00551 #ifdef COM
00552 printf(" Receive solution of challenge that it send previously challenge from otehr peer and incremented nonce \n");
00553 #endif
00554
00555 #ifndef _WIN32
00556 numbytes = recvfrom(new_fd,
00557 (void *) ciphertext,
00558 (size_t) 135,
00559 0,
00560 (struct sockaddr *) 0,
00561 0);
00562
00563 #else
00564 numbytes = recvfrom(new_fd,
00565 ciphertext,
00566 (size_t) 135,
00567 0,
00568 (struct sockaddr *) 0,
00569 0);
00570 #endif
00571 if (numbytes < 0) {
00572 printf("An Error occur while receiving data \n");
00573 perror("recvfrom \n");
00574 return -1;
00575 }
00576 else if (numbytes == 0) {
00577 printf("No message is available or peer close the connection \n");
00578 return -1;
00579 }
00580
00581 memset(rece_plaintext, '\0', 75);
00582
00583
00584 if( rsa_decryption( ciphertext, rece_plaintext, privatekey) != 0){
00585 printf(" . Error while decryption \n");
00586 return -2;
00587 }
00588 #ifdef COM
00589 printf(" . received %s size = %d \n", rece_plaintext, strlen(rece_plaintext));
00590 #endif
00591
00592
00593 for(i=0; i<32; i++){
00594 rece_challengetext[i]=rece_plaintext[i];
00595 }
00596
00597
00598 i=32; j=0;
00599 for(i=32; i<(32+32); i++,j++)
00600 rece_MD5_digest_hex[j] = rece_plaintext[i];
00601
00602
00603 i=64; j=0;
00604 ch=rece_plaintext[i];
00605 while(ch!='\0'){
00606 rece_nonce_str[j] = ch;
00607 i++; j++;
00608 ch=rece_plaintext[i];
00609 }
00610 rece_nonce = atoi(rece_nonce_str);
00611
00612 #ifdef COM
00613
00614 printf(" . rece_nonce = %d, my_nonce = %d \n", rece_nonce, (*my_nonce) );
00615 #endif
00616 if( ( ++(*my_nonce) ) == rece_nonce)
00617 {
00618 if( !strcmp(rece_MD5_digest_hex, my_MD5_digest_hex) )
00619 {
00620 status = 1;
00621 memset(auth_status, '\0', 3);
00622 strcpy(auth_status, "+ve");
00623 #ifdef COM
00624 printf(" . received solution is verified \n");
00625 printf(" . MD5 digest matches \n");
00626 #endif
00627 }
00628 #ifdef COM
00629 else
00630 printf(" . MD5 digest doesn't matches \n");
00631 printf(" . nonce matches \n");
00632 #endif
00633 }
00634 #ifdef COM
00635 else
00636 printf(" . nonce doesn't match \n");
00637 #endif
00638
00639
00640
00641 len_c = strlen(rece_challengetext);
00642 len_n = strlen(rece_nonce_str);
00643 for( i=len_c, j=0 ; i < (len_c+len_n); i++,j++ )
00644 rece_challengetext[i] = rece_nonce_str[j];
00645
00646
00647 memset(digest, '\0', 16);
00648 md5( (unsigned char *) rece_challengetext, strlen(rece_challengetext) , digest);
00649
00650
00651 for( i = 0,j=0; i < 16; i++,j=j+2 ){
00652 sprintf(temp, "%02x", digest[i]);
00653 solve_MD5_digest_hex[j] = temp[0];
00654 solve_MD5_digest_hex[j+1] = temp[1];
00655 }
00656 #ifdef COM
00657 printf(" . challenged solved \n");
00658 #endif
00659
00660
00661 (*my_nonce)++;
00662 memset(my_nonce_str, '\0', 11);
00663 sprintf(my_nonce_str, "%d", *my_nonce);
00664
00665 memset(rece_challengetext, '\0', 50);
00666 strcat(rece_challengetext, auth_status);
00667 strcat(rece_challengetext, solve_MD5_digest_hex);
00668 strcat(rece_challengetext, my_nonce_str);
00669
00670
00671 memset(ciphertext, '\0', 135);
00672 if( rsa_encryption (publickey, rece_challengetext, ciphertext) != 0){
00673 printf(" . fail to encrypt challengetext \n");
00674 return -1;
00675 }
00676 #ifdef COM
00677 printf(" . encrypted \n");
00678 #endif
00679 if (send(new_fd , ciphertext, 135, 0) == -1){
00680 perror("send \n");
00681 return -1;
00682 }
00683 if(status != 1 ){
00684 return -1;
00685 }
00686 #ifdef COM
00687 printf(" . send \n");
00688 #endif
00689
00695
00696
00697
00698 memset(ciphertext, '\0', 135);
00699 #ifdef COM
00700 printf("Receiving auth_status and incremented nonce from peer \n");
00701 #endif
00702
00703 #ifndef _WIN32
00704 numbytes = recvfrom(new_fd,
00705 (void *) ciphertext,
00706 (size_t) 135,
00707 0,
00708 (struct sockaddr *) 0,
00709 0);
00710
00711 #else
00712 numbytes = recvfrom(new_fd,
00713 ciphertext,
00714 (size_t) 135,
00715 0,
00716 (struct sockaddr *) 0,
00717 0);
00718 #endif
00719 if (numbytes < 0) {
00720 printf("An Error occur while receiving data \n");
00721 perror("recvfrom \n");
00722 return -1;
00723 }
00724 else if (numbytes == 0) {
00725 printf("No message is available or peer close the connection \n");
00726 return -1;
00727 }
00728
00729
00730 #ifdef COM
00731 printf(" . received \n");
00732 #endif
00733 memset(rece_plaintext, '\0', 130);
00734
00735 if(rsa_decryption( ciphertext, rece_plaintext, privatekey)!= 0){
00736 printf(" . Error while decryption \n");
00737 return -2;
00738 }
00739 #ifdef COM
00740 printf(" . decrypted \n");
00741 #endif
00742
00743 for(i=0; i<3; i++)
00744 rece_auth_status[i]=rece_plaintext[i];
00745 rece_auth_status[i]='\0';
00746
00747
00748 memset(rece_nonce_str, '\0', 11);
00749 i=3; j=0;
00750 ch=rece_plaintext[i];
00751 while(ch != '\0'){
00752 rece_nonce_str[j] = ch;
00753 i++; j++;
00754 ch=rece_plaintext[i];
00755 }
00756 rece_nonce = atoi(rece_nonce_str);
00757 if( (++(*my_nonce) ) != rece_nonce){
00758 printf(" warning: Nonce doesn't matches %s:%d\n", __FILE__, __LINE__);
00759 return -1;
00760 }
00761 #ifdef COM
00762 printf("rece_auth_status = %s \n", rece_auth_status);
00763 #endif
00764
00765 if( !strcmp(rece_auth_status, "+ve") ){
00766 #ifdef COM
00767 printf("Client: Authentication Process is successfull. \n");
00768 printf("AES key is exchanged. \n");
00769 #endif
00770 return 1;
00771 }
00772 else{
00773 return -2;
00774 }
00775 }
00776
00777 int reply_migration_process(int sockfd, int *my_nonce, char *pubkey, char *privkey, unsigned char *aes_key)
00778 {
00779 char my_challengetext[80], rece_challengetext[50];
00780 char my_MD5_digest_hex[35], rece_MD5_digest_hex[35], solve_MD5_digest_hex[35];
00781 char rece_plaintext[135];
00782 char my_nonce_str[15], rece_nonce_str[15];
00783 char temp[75], ch;
00784 char auth_status[]="-ve";
00785 char rece_auth_status[5];
00786 char ciphertext[135];
00787 int rece_nonce;
00788
00789 int len_n, len_c;
00790 int numbytes, status = -1, i, j;
00791 char number[15];
00792
00793 unsigned char digest[16];
00794
00795 char publickey[1024];
00796 char privatekey[1210];
00797
00798
00799
00800 memset(privatekey, '\0', 1210); memset(publickey, '\0', 1024);
00801 memset(temp, '\0', 75 );
00802 memset(my_challengetext, '\0', 80); memset(rece_challengetext, '\0', 50);
00803 memset(my_MD5_digest_hex, '\0', 35); memset(rece_MD5_digest_hex, '\0', 35);
00804 memset(solve_MD5_digest_hex, '\0', 35);
00805 memset(my_nonce_str, '\0', 15); memset(rece_nonce_str, '\0', 15);
00806 memset(ciphertext, '\0', 135);
00807 memset(number, '\0', 15);
00808 memset(ciphertext, '\0', 135);
00809 memset(rece_plaintext, '\0', 135);
00810
00811 strcpy(publickey , pubkey );
00812 strcpy(privatekey, privkey);
00813
00819
00820
00821 #ifdef COM
00822 printf("\n ################################### \n");
00823 printf("Authenticating connected peer ");
00824 printf("Wait to receiving challenge from peer \n");
00825 #endif
00826
00827 #ifndef _WIN32
00828 numbytes = recvfrom(sockfd,
00829 (void *) ciphertext,
00830 (size_t) 135,
00831 0,
00832 (struct sockaddr *) 0,
00833 0);
00834
00835 #else
00836 numbytes = recvfrom(sockfd,
00837 ciphertext,
00838 (size_t) 135,
00839 0,
00840 (struct sockaddr *) 0,
00841 0);
00842 #endif
00843 if (numbytes < 0) {
00844 printf("An Error occur while receiving data \n");
00845 perror("recvfrom \n");
00846 return -1;
00847 }
00848 else if (numbytes == 0) {
00849 printf("No message is available or peer close the connection \n");
00850 return -1;
00851 }
00852
00853
00854 #ifdef COM
00855 printf(" . challenge received %d bytes \n", numbytes);
00856 printf("Solving the challenge and send back to peer \n");
00857 #endif
00858
00859 if ( rsa_decryption( ciphertext, rece_plaintext, privatekey) != 0){
00860 printf(" . fail to decrypt\n");
00861 return -1;
00862 }
00863 #ifdef COM
00864 printf(" . decrypted \n");
00865 printf("rece_challenge = %s \n",rece_plaintext);
00866 #endif
00867
00868
00869 for(i=0; i<32; i++)
00870 rece_challengetext[i] = rece_plaintext[i];
00871 strcpy((char*)aes_key, rece_challengetext);
00872 #ifdef COM
00873 printf("AES key = %s size %d \n",aes_key, strlen(aes_key) );
00874 #endif
00875
00876 i=32; j=0;
00877 ch=rece_plaintext[i];
00878 while(ch!='\0'){
00879 rece_nonce_str[j] = ch;
00880 i++; j++;
00881 ch = rece_plaintext[i];
00882 }
00883
00884
00885 rece_nonce = atoi(rece_nonce_str);
00886
00887 (*my_nonce) = rece_nonce;
00888 (*my_nonce)++;
00889 #ifdef COM
00890 printf(" . rece_nonce = %d my_nonce = %d", rece_nonce, (*my_nonce));
00891 #endif
00892
00893 sprintf(my_nonce_str, "%d", *my_nonce);
00894
00895
00896
00897 md5( (unsigned char *) rece_plaintext, strlen(rece_plaintext), digest );
00898
00899
00900
00901 for( i = 0,j=0; i < 16; i++,j=j+2 ){
00902 sprintf(temp, "%02x", digest[i]);
00903 solve_MD5_digest_hex[j] = temp[0];
00904 solve_MD5_digest_hex[j+1] = temp[1];
00905 }
00906 #ifdef COM
00907 printf(" . challenge solved \n" );
00908 printf(" . Creating challenge text for other peer \n");
00909 #endif
00910
00911 generate_AES_key(my_challengetext);
00912 #ifdef COM
00913 printf(" . challenge text created \n");
00914 #endif
00915
00916
00917
00918 strcpy(temp,my_challengetext);
00919 len_c = strlen(temp);
00920 len_n = strlen(my_nonce_str);
00921 for(i=len_c, j=0; i < (len_c+len_n); i++, j++)
00922 temp[i] = my_nonce_str[j];
00923
00924 md5( (unsigned char *) temp, strlen(temp), digest );
00925
00926
00927 for( i = 0,j=0; i < 16; i++,j=j+2 ){
00928 sprintf(temp, "%02x", digest[i]);
00929 my_MD5_digest_hex[j] = temp[0];
00930 my_MD5_digest_hex[j+1] = temp[1];
00931 }
00932
00933
00934 strcat(my_challengetext, solve_MD5_digest_hex);
00935 strcat(my_challengetext, my_nonce_str);
00936
00937 #ifdef COM
00938 printf(" . send = %s size = %d \n", my_challengetext,strlen(my_challengetext));
00939 #endif
00940
00941 memset(ciphertext, '\0', 135);
00942 if( rsa_encryption(publickey, my_challengetext, ciphertext ) != 0){
00943 printf(" . fail to encrypt \n");
00944 return -1;
00945 }
00946 #ifdef COM
00947 printf(" . encrypted \n");
00948 #endif
00949
00950 if ( (numbytes = send(sockfd , ciphertext, 135, 0)) == -1){
00951 perror("send \n");
00952 return -1;
00953 }
00954 #ifdef COM
00955 printf(" . send \n");
00956 #endif
00957
00964
00965
00966 memset(ciphertext, '\0', 135);
00967 #ifdef COM
00968 printf("Receiving Auth_status, soultion to challenge it send previously and incremented nonce from peer \n");
00969 #endif
00970
00971 #ifndef _WIN32
00972 numbytes = recvfrom(sockfd,
00973 (void *) ciphertext,
00974 (size_t) 135,
00975 0,
00976 (struct sockaddr *) 0,
00977 0);
00978 #else
00979 numbytes = recvfrom(sockfd,
00980 ciphertext,
00981 (size_t) 135,
00982 0,
00983 (struct sockaddr *) 0,
00984 0);
00985 #endif
00986 if (numbytes < 0) {
00987 printf("An Error occur while receiving data \n");
00988 perror("recvfrom \n");
00989 return -1;
00990 }
00991 else if (numbytes == 0) {
00992 printf("No message is available or peer close the connection \n");
00993 return -1;
00994 }
00995
00996 #ifdef COM
00997 printf(" . received %d bytes\n", numbytes);
00998 #endif
00999 memset(rece_plaintext, '\0', 75);
01000
01001
01002 if(rsa_decryption( ciphertext, rece_plaintext, privatekey)!= 0){
01003 printf("rsa_decryption() warning. \n");
01004 return -2;
01005 }
01006
01007
01008
01009 for(i=0; i<3; i++)
01010 rece_auth_status[i]=rece_plaintext[i];
01011 if( !strcmp(rece_auth_status,"-ve") )
01012 return -2;
01013
01014
01015
01016 memset(rece_MD5_digest_hex, '\0', 35);
01017 j=0;
01018 for(i=3; i<35; i++, j++)
01019 rece_MD5_digest_hex[j] = rece_plaintext[i];
01020 #ifdef COM
01021 printf(" rece_MD5_digest_hex = %s \n", rece_MD5_digest_hex);
01022 printf(" my_MD5_digest_hex = %s \n", my_MD5_digest_hex);
01023 #endif
01024
01025 ch = rece_plaintext[i];
01026 j=0;
01027 memset(rece_nonce_str, '\0', 15);
01028 while(ch != '\0'){
01029 rece_nonce_str[j] = ch;
01030 i++; j++;
01031 ch = rece_plaintext[i];
01032 }
01033
01034 rece_nonce = atoi(rece_nonce_str);
01035
01036
01037 if( (++(*my_nonce)) == rece_nonce)
01038 {
01039 if( !strcmp(rece_MD5_digest_hex, my_MD5_digest_hex) )
01040 {
01041 status = 1;
01042 memset(auth_status, '\0', 3);
01043 strcpy(auth_status, "+ve");
01044 #ifdef COM
01045 printf(" . received solution is verified \n");
01046 printf(" . MD5 digest matches \n");
01047 #endif
01048 }
01049 #ifdef COM
01050 else
01051 printf(" . MD5 digest doesn't matches \n");
01052 printf(" . nonce matches \n");
01053 #endif
01054 }
01055 #ifdef COM
01056 else
01057 printf(" . nonce doesn't match \n");
01058 #endif
01059
01060
01061 (*my_nonce)++;
01062 memset(my_nonce_str, '\0', 15);
01063 sprintf(my_nonce_str, "%d", (*my_nonce) );
01064 memset(rece_challengetext, '\0', 50);
01065 strcat(rece_challengetext, auth_status);
01066 strcat(rece_challengetext, my_nonce_str);
01067 #ifdef COM
01068 printf("auth_status = %s \n", auth_status);
01069 printf(" . Sending Auth_status \n");
01070 #endif
01071
01072 memset(ciphertext, '\0', 135);
01073 if( rsa_encryption (publickey, rece_challengetext, ciphertext) != 0){
01074 printf(" . fail to encrypt challengetext \n");
01075 return -1;
01076 }
01077 #ifdef COM
01078 printf(" . encrypted \n");
01079 #endif
01080 if ((numbytes = send(sockfd , ciphertext, 135, 0)) == -1){
01081 perror("send \n");
01082 return -1;
01083 }
01084 #ifdef COM
01085 printf(" . send \n");
01086 #endif
01087
01088 if ( !strcmp(auth_status, "+ve") ){
01089 #ifdef COM
01090 printf("Server: Authentication Process is successfull \n");
01091 printf(" AES key is received successfully \n");
01092 #endif
01093 return 1;
01094 }
01095 else
01096 return -1;
01097 }
01098
01099 void generate_AES_key(char *key)
01100 {
01101 havege_state hs;
01102
01103 int num,i;
01104 char number[10];
01105 memset(number, '\0', 10);
01106
01107 havege_init( &hs );
01108 for (i=0; i<4; i++){
01109 num = havege_rand(&hs);
01110 if(num < 0)
01111 num=num*(-1);
01112 sprintf(number, "%X",num);
01113 if(strlen(number) == 8)
01114 strcat(key, number);
01115 else
01116 i--;
01117 memset(number, '\0', 10);
01118 }
01119 }
01120
01121
01122 int aes_en_de(int mode, char *infile, char *outfile, unsigned char *AES_key, int *nonce, int new_fd)
01123 {
01124 int ret = -1, i, n;
01125 int keylen, lastn;
01126 FILE *fin, *fout;
01127 int flag= -1;
01128 char *p;
01129 unsigned char IV[16];
01130 unsigned char key[512];
01131 unsigned char digest[35];
01132 unsigned char buffer[1024];
01133 struct stat stbuf;
01134
01135 aes_context aes_ctx;
01136 sha2_context sha_ctx;
01137
01138 off_t filesize, offset;
01139
01140 if( mode == MODE_ENCRYPT && (*nonce) != 0 ) {
01141
01142 if( append_nonce_to_MA( nonce, infile ) == -1){
01143 printf("Unable to write nonce to MA file\n");
01144
01145 return -1;
01146 }else
01147 flag=1;
01148 }
01149
01150 if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
01151 {
01152 printf("invalid operation mode \n" );
01153
01154 return -1;
01155 }
01156
01157 if( strcmp( infile, outfile ) == 0 )
01158 {
01159 printf("input and output filenames must differ\n" );
01160
01161 return -1;
01162 }
01163
01164 if( ( fin = fopen( infile, "rb" ) ) == NULL )
01165 {
01166 printf("fopen(%s,rb) failed\n", infile );
01167 return -1;
01168 }
01169
01170 if( ( fout = fopen( outfile, "wb+" ) ) == NULL )
01171 {
01172 printf("fopen(%s,wb+) failed\n", outfile );
01173 fclose(fin);
01174 return -1;
01175 }
01176
01177 keylen = strlen( (const char*)AES_key );
01178
01179 if( keylen > (int) sizeof( key ) )
01180 keylen = (int) sizeof( key );
01181
01182 memcpy( key, AES_key, keylen );
01183
01184
01185
01186 stat(infile ,&stbuf );
01187 filesize = (int)stbuf.st_size;
01188
01189
01190 if( mode == MODE_ENCRYPT )
01191 {
01192
01193
01194
01195 for( i = 0; i < 8; i++ )
01196 buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
01197
01198 p = infile;
01199
01200 sha2_starts( &sha_ctx, 0 );
01201 sha2_update( &sha_ctx, buffer, 8 );
01202 sha2_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
01203 sha2_finish( &sha_ctx, digest );
01204
01205 memcpy( IV, digest, 16 );
01206
01207
01208
01209
01210
01211 lastn = (int)( filesize & 0x0F );
01212
01213 IV[15] = (unsigned char)
01214 ( ( IV[15] & 0xF0 ) | lastn );
01215
01216
01217
01218
01219 if( fwrite( IV, 1, 16, fout ) != 16 )
01220 {
01221 printf("fwrite() failed\n" );
01222 fclose(fin); fclose(fout);
01223 return -1;
01224 }
01225
01226
01227
01228
01229
01230 memset( digest, 0, 32 );
01231 memcpy( digest, IV, 16 );
01232
01233 for( i = 0; i < 8192; i++ )
01234 {
01235 sha2_starts( &sha_ctx, 0 );
01236 sha2_update( &sha_ctx, digest, 32 );
01237 sha2_update( &sha_ctx, key, keylen );
01238 sha2_finish( &sha_ctx, digest );
01239 }
01240
01241 memset( key, 0, sizeof( key ) );
01242
01243 aes_setkey_enc( &aes_ctx, digest, 256 );
01244 sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
01245
01246
01247
01248
01249 for( offset = 0; offset < filesize; offset += 16 )
01250 {
01251 n = ( filesize - offset > 16 ) ? 16 : (int)
01252 ( filesize - offset );
01253
01254 if( fread( buffer, 1, n, fin ) != (size_t) n )
01255 {
01256 printf("fread() failed\n");
01257 fclose(fin); fclose(fout);
01258 return -1;
01259 }
01260
01261 for( i = 0; i < 16; i++ )
01262 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
01263
01264 aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
01265 sha2_hmac_update( &sha_ctx, buffer, 16 );
01266
01267 if( fwrite( buffer, 1, 16, fout ) != 16 )
01268 {
01269 printf("fwrite() failed\n");
01270 fclose(fin); fclose(fout);
01271 return -1;
01272 }
01273
01274 memcpy( IV, buffer, 16 );
01275 }
01276
01277
01278
01279
01280 sha2_hmac_finish( &sha_ctx, digest );
01281
01282 if( fwrite( digest, 1, 32, fout ) != 32 )
01283 {
01284 printf("fwrite() failed\n");
01285 fclose(fin); fclose(fout);
01286 return -1;
01287 }
01288 }
01289
01290
01291 if( mode == MODE_DECRYPT )
01292 {
01293 unsigned char tmp[16];
01294
01295
01296
01297
01298
01299
01300
01301
01302 if( filesize < 48 )
01303 {
01304 printf( "File too short to be encrypted.\n" );
01305 fclose(fin); fclose(fout);
01306 return -1;
01307 }
01308
01309 if( ( filesize & 0x0F ) != 0 )
01310 {
01311 printf("File size not a multiple of 16.\n ");
01312 fclose(fin); fclose(fout);
01313 return -1;
01314 }
01315
01316
01317
01318
01319 filesize -= ( 16 + 32 );
01320
01321
01322 if( fread( buffer, 1, 16, fin ) != 16 )
01323 {
01324 printf( "fread() failed\n");
01325 fclose(fin); fclose(fout);
01326 return -1;
01327 }
01328
01329 memcpy( IV, buffer, 16 );
01330 lastn = IV[15] & 0x0F;
01331
01332
01333
01334
01335
01336 memset( digest, 0, 32 );
01337 memcpy( digest, IV, 16 );
01338
01339 for( i = 0; i < 8192; i++ )
01340 {
01341 sha2_starts( &sha_ctx, 0 );
01342 sha2_update( &sha_ctx, digest, 32 );
01343 sha2_update( &sha_ctx, key, keylen );
01344 sha2_finish( &sha_ctx, digest );
01345 }
01346
01347 memset( key, 0, sizeof( key ) );
01348 aes_setkey_dec( &aes_ctx, digest, 256 );
01349 sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
01350
01351
01352
01353
01354 for( offset = 0; offset < filesize; offset += 16 )
01355 {
01356 if( fread( buffer, 1, 16, fin ) != 16 )
01357 {
01358 printf(" fread() failed \n ");
01359 fclose(fin); fclose(fout);
01360 return -1;
01361 }
01362
01363 memcpy( tmp, buffer, 16 );
01364
01365 sha2_hmac_update( &sha_ctx, buffer, 16 );
01366 aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
01367
01368 for( i = 0; i < 16; i++ )
01369 buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
01370
01371 memcpy( IV, tmp, 16 );
01372
01373 n = ( lastn > 0 && offset == filesize - 16 )
01374 ? lastn : 16;
01375 if( fwrite( buffer, 1, n, fout ) != (size_t) n )
01376 {
01377 printf(" fwrite() failed \n ");
01378 fclose(fin); fclose(fout);
01379 return -1;
01380 }
01381 }
01382
01383
01384
01385 sha2_hmac_finish( &sha_ctx, digest );
01386
01387 if( fread( buffer, 1, 32, fin ) != 32 )
01388 {
01389 printf(" fread() failed\n");
01390 fclose(fin); fclose(fout);
01391 return -1;
01392 }
01393 #ifdef COM
01394 printf("verify the message authentication code MAC \n");
01395 #endif
01396 if( memcmp( digest, buffer, 32 ) != 0 )
01397 {
01398 printf(" HMAC check failed: wrong key, or file corrupted.\n ");
01399 fclose(fin); fclose(fout);
01400 return -1;
01401 }
01402 #ifdef COM
01403 printf("MAC done ... \n");
01404 #endif
01405 }
01406 fclose(fin);
01407 fclose(fout);
01408 ret = 1;
01409
01410 if( (*nonce)!=0 && mode == MODE_DECRYPT){
01411 if ( extract_nonce_from_MA(new_fd, nonce, outfile) != 1){
01412 printf("Unable to extract nonce from MA file");
01413 return -1;
01414 }
01415 }
01416 return 1;
01417 }
01418
01419 int send_AES_en_MA(int sockfd, int *my_nonce ,char *outfile, char *pubkey)
01420 {
01421 FILE *fd;
01422 struct stat stbuf;
01423 char *MA_buffer;
01424 int numbytes;
01425 char data[20];
01426 char size_str[5];
01427 int size;
01428 char ciphertext[135];
01429 char publickey[1024];
01430
01431 #ifdef COM
01432 printf("Send AES encrypted MA \n");
01433 #endif
01434
01435 memset(publickey, '\0', 1024);
01436 memset(data, '\0', 20);
01437 memset(ciphertext, '\0', 135);
01438
01439 strcpy(publickey, pubkey);
01440
01441 if ( (fd = fopen(outfile, "rb")) == NULL ){
01442 printf("Error while opening MA file");
01443 return -1;
01444 }
01445
01446 stat(outfile ,&stbuf );
01447
01448
01449 MA_buffer = (char*) malloc (sizeof(char)*(int)stbuf.st_size);
01450 if (MA_buffer == NULL){
01451 printf("Memory error: Cann't allocate memory for MA \n");
01452 return -1;
01453 }
01454
01455
01456 numbytes = fread ( MA_buffer, 1, (int)stbuf.st_size, fd);
01457 if (numbytes != (int)stbuf.st_size){
01458 printf("Error: Reading MA file");
01459 free(MA_buffer);
01460 return -1;
01461 }
01462 fclose(fd);
01463
01464
01465
01466 #ifdef COM
01467 printf("Prepare to send size of encrypted message \n");
01468 #endif
01469
01470 size = (int)stbuf.st_size;
01471
01472 (*my_nonce)--;
01473 sprintf(data, "%d", *my_nonce );
01474 sprintf(size_str, "%d", size);
01475 strcat(data, size_str);
01476 #ifdef COM
01477 printf("Size = %d \n", size);
01478 printf("my_nonce = %d \n", *my_nonce);
01479 printf("data to send = %s \n", data);
01480 #endif
01481 if( rsa_encryption (publickey, data, ciphertext) != 0){
01482 printf(" . fail to encrypt challengetext \n");
01483 return -1;
01484 }
01485
01486 if (numbytes = send(sockfd , ciphertext, 135, 0) == -1){
01487 printf("Error while sending size of encryptee message to receiver \n");
01488 return -1;
01489 }
01490
01491 #ifdef COM
01492 printf("Send. \n");
01493 printf("Size of encrypted msg is send. \n");
01494 printf("Now Sending Encrypted msg. \n");
01495 #endif
01496
01497
01498 if ( numbytes = send(sockfd , MA_buffer, (int)stbuf.st_size, 0) == -1 ) {
01499
01500 perror("send");
01501 #ifdef COM
01502 printf("Cannot send encrypted MA \n");
01503 #endif
01504 return -1;
01505 }
01506 free(MA_buffer);
01507 #ifdef COM
01508 printf(" . encrypted message is send \n");
01509 printf(" . waiting for acknowlegement \n");
01510 #endif
01511
01512
01513 memset(data, '\0', 20);
01514
01515 #ifndef _WIN32
01516 numbytes = recvfrom(sockfd,
01517 (void *) data,
01518 (size_t) 20,
01519 0,
01520 (struct sockaddr *) 0,
01521 0);
01522
01523 #else
01524 numbytes = recvfrom(sockfd,
01525 data,
01526 (size_t) 20,
01527 0,
01528 (struct sockaddr *) 0,
01529 0);
01530 #endif
01531
01532 if (numbytes < 0) {
01533 printf("An Error occur while receiving data \n");
01534 perror("recvfrom \n");
01535 return -1;
01536 }
01537 else if (numbytes == 0) {
01538 printf("No message is available or peer close the connection \n");
01539 return -1;
01540 }
01541 #ifdef COM
01542 printf(" . acknowledgment received = %s \n", data);
01543 #endif
01544 if(!strcmp(data, "-ve" ) ){
01545 printf("Error: receiver cannot get MA successfuly \n");
01546
01547 return -2;
01548 }
01549 #ifdef COM
01550 printf("------------------------------ \n");
01551 #endif
01552 return 1;
01553 }
01554
01555 int receive_AES_en_MA(int new_fd, int *nonce, char *infile, char *privkey)
01556 {
01557 FILE *fd;
01558 int numbytes;
01559
01560 char *buffer;
01561 char plaintext [135];
01562 int i,j;
01563 int rece_nonce;
01564 char nonce_str[15];
01565 int size;
01566 char size_str[5], ch;
01567 char privatekey[1210];
01568
01569 memset(plaintext, '\0', 135 );
01570 memset(nonce_str, '\0', 15);
01571 strcpy(privatekey, privkey );
01572
01573 #ifdef COM
01574 printf("Receiving Size of encrypted Message and then message \n ");
01575 printf("Receiving size ... \n");
01576 #endif
01577
01578 buffer = (char*) malloc(sizeof(char)*135);
01579 if (buffer == NULL){
01580 printf("Memory error: Cann't allocate memory for MA \n");
01581 return -1;
01582 }
01583 memset(buffer, '\0', 135);
01584
01585 #ifndef _WIN32
01586 numbytes = recvfrom(new_fd,
01587 (void *) buffer,
01588 (size_t) 135,
01589 0,
01590 (struct sockaddr *) 0,
01591 0);
01592 #else
01593 numbytes = recvfrom(new_fd,
01594 buffer,
01595 (size_t) 135,
01596 0,
01597 (struct sockaddr *) 0,
01598 0);
01599 #endif
01600
01601 if (numbytes < 0) {
01602 printf("An Error occur while receiving data \n");
01603 perror("recvfrom \n");
01604 return -1;
01605 }
01606 else if (numbytes == 0) {
01607 printf("No message is available or peer close the connection \n");
01608 return -1;
01609 }
01610 #ifdef COM
01611 printf(" decrypting receive msg (Size of En. Msg.) \n");
01612 #endif
01613
01614 if ( rsa_decryption( buffer, plaintext, privatekey) != 0){
01615 printf(" . fail to decrypt\n");
01616 return -1;
01617 }
01618
01619 free(buffer);
01620 for(i=0; i<10; i++)
01621 nonce_str[i]=plaintext[i];
01622 j=0;
01623 ch = plaintext[i];
01624 while(ch != '\0'){
01625 size_str[j++] = ch;
01626 ch = plaintext[++i];
01627 }
01628 size = atoi(size_str);
01629 rece_nonce = atoi(nonce_str);
01630
01631 (*nonce)++;
01632 if((*nonce) != rece_nonce ){
01633 printf("Nonce Doesnot matches \n");
01634 return -1;
01635 }
01636
01637 buffer = (char*) malloc (sizeof(char)*size);
01638 if (buffer == NULL){
01639 printf("Memory error: Cann't allocate memory for MA \n");
01640 return -1;
01641 }
01642
01643 #ifdef COM
01644 printf("size = %d \n", size);
01645 printf("nonce_rece = %d \n", rece_nonce);
01646 printf("Receiving encrypted message ... \n");
01647 #endif
01648
01649 #ifndef _WIN32
01650 numbytes = recvfrom(new_fd,
01651 (void *) buffer,
01652 (size_t) size,
01653 0,
01654 (struct sockaddr *) 0,
01655 0);
01656 #else
01657 numbytes = recvfrom(new_fd,
01658 buffer,
01659 (size_t) size,
01660 0,
01661 (struct sockaddr *) 0,
01662 0);
01663 #endif
01664
01665 if (numbytes < 0) {
01666 printf("An Error occur while receiving data \n");
01667 perror("recvfrom \n");
01668 return -1;
01669 }
01670 else if (numbytes == 0) {
01671 printf("No message is available or peer close the connection \n");
01672 return -1;
01673 }
01674 #ifdef COM
01675 printf(" . received %d bytes \n", numbytes);
01676 #endif
01677
01678 if( (fd = fopen(infile,"wb") ) == NULL ){
01679 printf("Error: while writing MA content in a file");
01680 return -1;
01681 }
01682 fwrite (buffer , 1 , numbytes , fd );
01683 free(buffer);
01684 fclose(fd);
01685 return 1;
01686 }
01687
01688
01692
01693 int generate_RSA_keys_plaintext(char *pubkeyfile, char *privkeyfile)
01694 {
01695
01696 FILE *fpub, *fpriv;
01697 rsa_context rsa;
01698 havege_state hs;
01699 int ret;
01700
01701 printf( "\nSeeding the random number generator \n" );
01702 fflush( stdout );
01703 havege_init( &hs );
01704
01705 printf( "Generating the RSA key [ %d-bit ] \n", KEY_SIZE );
01706 rsa_init( &rsa, RSA_PKCS_V15, 0, havege_rand, &hs );
01707 if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT ) ) != 0 )
01708 {
01709 printf( " failed, rsa_gen_key returned %d\n", ret );
01710 return -1;
01711 }
01712
01713 printf( "Exporting public key in %s \n", pubkeyfile );
01714 fflush( stdout );
01715 if( ( fpub = fopen( pubkeyfile, "wb+" ) ) == NULL )
01716 {
01717 printf( " failed, could not open %s for writing\n",pubkeyfile);
01718 return -1;
01719 }
01720
01721 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
01722 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
01723 {
01724 printf( " failed, mpi_write_file returned %d\n", ret );
01725 return -1;
01726 }
01727
01728 printf( "Exporting the private key in %s \n", privkeyfile );
01729 fflush( stdout );
01730
01731 if( ( fpriv = fopen( privkeyfile, "wb+" ) ) == NULL )
01732 {
01733 printf( " failed, could not open %s for writing\n", privkeyfile);
01734 }
01735
01736 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
01737 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
01738 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
01739 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
01740 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
01741 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
01742 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
01743 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
01744 {
01745 printf( " failed, mpi_write_file returned %d\n", ret );
01746 return -1;
01747 }
01748 printf(" done. \n");
01749 return 1;
01750 }
01751
01752 int generate_RSA_keys_ciphertext(char *pubkeyfile, char *privkeyfile, unsigned char *passphrase)
01753 {
01754
01755 FILE *fpub, *fpriv;
01756 rsa_context rsa;
01757 havege_state hs;
01758 int ret;
01759 int num=0;
01760
01761 printf( "\nSeeding the random number generator \n" );
01762 fflush( stdout );
01763 havege_init( &hs );
01764
01765 printf( "Generating the RSA key [ %d-bit ] \n", KEY_SIZE );
01766 rsa_init( &rsa, RSA_PKCS_V15, 0, havege_rand, &hs );
01767 if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT ) ) != 0 )
01768 {
01769 printf( " failed, rsa_gen_key returned %d\n", ret );
01770 return -1;
01771 }
01772
01773 printf( "Exporting public key in %s \n", pubkeyfile );
01774 fflush( stdout );
01775 if( ( fpub = fopen( pubkeyfile, "wb+" ) ) == NULL )
01776 {
01777 printf( " failed, could not open %s for writing\n",pubkeyfile);
01778 return -1;
01779 }
01780
01781 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
01782 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
01783 {
01784 printf( " failed, mpi_write_file returned %d\n", ret );
01785 return -1;
01786 }
01787 fclose(fpub);
01788 printf( "Exporting the private key in %s \n", privkeyfile );
01789 fflush( stdout );
01790
01791 if( ( fpriv = fopen( "temp_priv", "wb+" ) ) == NULL )
01792 {
01793 printf( "failed, could not open temp_priv for writing\n");
01794 return -1;
01795 }
01796
01797 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
01798 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
01799 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
01800 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
01801 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
01802 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
01803 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
01804 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
01805 {
01806 printf( " failed, mpi_write_file returned %d\n", ret );
01807 return -1;
01808 }
01809 fclose(fpriv);
01810
01811 if( aes_en_de(0, "temp_priv" ,privkeyfile, passphrase, &num, 0) == -1){
01812 printf("failed to encrypt private key file \n");
01813 if( remove("temp_priv") ) printf("interface.c : remove error 1");
01814 return -1;
01815 }
01816 else
01817 printf("encrypted. \ndone. \n");
01818 if( remove("temp_priv") ) printf("interface.c : remove error 2");
01819 return 1;
01820 }