static int ocd_decrypt(Npcrypt* crypt, char* data, int length, char** result, int* result_length) { struct openssl_crypt_data* priv; priv = (struct openssl_crypt_data*)crypt->priv; //printf(" LAST BYTES: %d %d %d %d %d\n", data[length-5],data[length-4],data[length-3],data[length-2], data[length-1]); return symmetric_decrypt(&priv->decrypt_ctx, data, length, result, result_length); }
int receive_file(int sk) { int ret; int name_size; // length of the name of the received file char* filename; // name of the received file int size,i; // size of the buffer for the plaintext char* buffer; // plaintext buffer FILE* file; // pointer to the file where the received message will be saved time_t t; t = time(NULL); srand ( time(NULL)); int local_random = rand(); send(sk, &local_random, sizeof(int), 0); printf("\n generated random value is %d",local_random); /* Reception of the length of the file name */ ret = recv(sk, &name_size, sizeof(name_size), MSG_WAITALL); if (ret != sizeof(name_size)) { printf("%d \n Error receiving the length of the file name\n",ret); return 1; } /* Memory allocation */ filename = malloc(sizeof(char) * (name_size + 1)); if(filename == NULL) { printf("\n Error allocating memory\n"); return 1; } /* Reception of the file name */ ret = recv(sk, filename, name_size, MSG_WAITALL); if(ret != name_size){ printf(" \n Error receiving the file name\n"); return 1; } filename[name_size] ='\0'; /* End of string */ /* Reception of the file size */ ret = recv(sk, &size, sizeof(size), MSG_WAITALL); if(ret != sizeof(size)) { printf("\n Error receiving the file size\n"); return 1; } /* Memory allocation */ buffer = malloc(size * sizeof(char)); if(buffer == NULL){ printf("\n Error allocating memory\n"); return 1; } /* Reception of the file */ ret = recv(sk, buffer, size, MSG_WAITALL); if(ret != size) { printf("\n Error receiving the file\n"); return 1; } //***** SEPARATE THE RECIEVED TEXT BEGIN *****// int remote_random; int md_len = 20; int rem_rand_size = sizeof(remote_random); char* recieved_hash; char* ciphertext; char* enc_key; int enckeysize =128; int enctextsize; recieved_hash = malloc(md_len); RSA* rsa = RSA_new(); enctextsize = size - enckeysize - rem_rand_size; ciphertext = malloc(enctextsize); enc_key = malloc(enckeysize); memcpy(ciphertext,buffer,enctextsize); memcpy(enc_key,&buffer[enctextsize],enckeysize); memcpy(&remote_random,&buffer[enctextsize+enckeysize],rem_rand_size); //****** Generate hash for freshness and origin(password) check *****// char* password; int password_size; int loc_rand_size; int fresh_size; char* fresh_txt; file = fopen("passofAhash.txt","r"); fseek(file,0,SEEK_END); password_size = ftell(file); password = malloc(password_size * sizeof (char)); fseek(file, 0, SEEK_SET); /* File reading */ ret = fread(&password[0], 1, password_size, file); fclose(file); loc_rand_size=sizeof(local_random); fresh_size=password_size+loc_rand_size+sizeof(remote_random); fresh_txt = malloc(fresh_size); memcpy(fresh_txt,&password[0],password_size); memcpy(&fresh_txt[password_size],&local_random,loc_rand_size); memcpy(&fresh_txt[password_size+loc_rand_size],&remote_random,rem_rand_size); const EVP_MD *md; md = EVP_get_digestbyname("sha1"); unsigned char md_value[EVP_MD_size(md)]; md_len=hash_gen(fresh_txt,&md_value[0]); printf("\n \n Freshness hash calculated in server is:\n "); for(i = 0; i < md_len; i++) printbyte(md_value[i]); printf("\n"); //***** DECRYPT THE KEY PART BEGIN *****// char* key; int flen; FILE* fp; fp = fopen("priv.pem","r"); OpenSSL_add_all_algorithms(); rsa = PEM_read_RSAPrivateKey(fp,&rsa,NULL,"password"); if(rsa == NULL) printf("\n ERROR reading rsa private key \n"); flen = RSA_size(rsa); key = malloc(flen); ret = RSA_private_decrypt(flen,enc_key,key,rsa,RSA_PKCS1_PADDING); printf("\n Recieved symmetric key is : \n"); for (i = 0; i < 8; i++) printbyte(key[i]); printf("\n"); free(enc_key); fclose(fp); RSA_free(rsa); ///*** DECRYPT the key part END ***/// ///*** DECRYPT THE MESSAGE PART BEGIN ***/// int nctot; char* decryptedtext; char* plaintext; int msg_len; decryptedtext = (char *)malloc(enctextsize+128); //call decryption function nctot = symmetric_decrypt(ciphertext,decryptedtext,key, enctextsize); msg_len = nctot - md_len; //size of plaintext message plaintext = malloc(msg_len); //separate the plain text from the freshness and password hash memcpy(plaintext,decryptedtext,msg_len); memcpy(recieved_hash,&decryptedtext[msg_len],md_len); printf("\n \n Freshness hash recieved from client is: \n"); for(i = 0; i < md_len; i++) printbyte(recieved_hash[i]); printf("\n"); //compare recieved digest with locally calculated digest to check freshness and password ret = strcmp(recieved_hash,&md_value[0]); if(ret==0)printf("\n MESSAGE FRESHNESS AND ORIGIN IS VERIFIED"); else printf("message not fresh with ret value %d",ret); //***** DECRYPT the message part END *****// /* Open the file to save the received message */ file = fopen(filename,"w"); if(file == NULL) { printf("\n File not found\n"); return 1; } /* Write the received message in the local file */ ret = fwrite(plaintext, 1, msg_len, file); if(ret < msg_len) { printf("\n Error writing the file \n"); return 1; } printf("\n Received file %s with size %d bytes\n", filename, size); fclose(file); free(filename); free(buffer); free(ciphertext); free(plaintext); return 0; }