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