int main(int argc, char const *argv[]) { struct addrinfo hints, *servinfo, *p; struct sockaddr_in address; int sock = 0, valread, rv; struct sockaddr_in serv_addr; char buffer[BUFFSIZE] = {0}; const char *filename; int PORT = 8088; const char* server_ip; char received_md5[MD5_DIGEST_LENGTH]; char calculated_md5[MD5_DIGEST_LENGTH]; char ack[]="ACK!"; char nack[] = "NACK"; if(argc < 4){ printf("Error need exactly 3 arguments. Arg1: Server-IP Arg2: Port# Arg3: File Name\n"); exit(0); }else{ server_ip = argv[1]; PORT = atoi(argv[2]); filename = argv[3]; } memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; //argv[2] is the port# arg if ((rv = getaddrinfo(server_ip, argv[2], &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } //connect to the first result from our getaddrinfo lookup on the specified ip for(p = servinfo; p != NULL; p = p->ai_next) { if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; } break; } memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Convert IPv4 and IPv6 addresses from text to binary form if(inet_pton(AF_INET, server_ip, &serv_addr.sin_addr)<=0) { printf("\nInvalid address/Address not supported \n"); return -1; } if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf("\nConnection Failed \n"); return -1; } printf("Connection request sent\n"); // receive connection message valread = recv( sock , buffer, BUFFSIZE,0); printf("%s\n",buffer); memset(&buffer,'\0',BUFFSIZE); //send the filename to the server send(sock , filename , strlen(filename) , 0 ); printf("Filename request sent\n"); // Receive ACK or NACK message valread = recv(sock,buffer,5,0); printf("%s\n",buffer); if(buffer[0] == 'N') { printf("Error file does not exist in server.\n"); exit(1); } //Receive the checksum memset(&buffer,'\0',BUFFSIZE); valread = recv(sock,buffer,BUFFSIZE,0); memcpy(received_md5,buffer,MD5_DIGEST_LENGTH); //RECEIVE SIZE INFO //next message should be file size int read_code = recv(sock, buffer, MSGSIZE,0); printf("%s\n",buffer); int size = atoi(buffer); printf("Received size of file: %d bytes\n", size); int bytes_received = 0; FILE *jpeg; char* out_file; int fail_count = 0; int write_fail_count = 0; int packets_received = 0; jpeg = fopen("out_file.jpg","wb"); while(bytes_received < size){ memset(&buffer,'\0',BUFFSIZE); // valread = recv(sock,work_buffer+wb_offset,BUFFSIZE,0); valread = recv(sock,buffer,BUFFSIZE,0); if(valread < 0){ //an error has occured printf("Error recieving packet\n"); fail_count++; break; }else if(valread == 0){ //the connection has been closed printf("Server has closed the connection\n"); break; }else{ packets_received++; int write_error = fwrite(buffer,sizeof(char),valread, jpeg); if(write_error <= 0){ write_fail_count++; } bytes_received += valread; printf("bytes received: %d / %d (%d received %d dropped)\n",bytes_received,size,packets_received, fail_count); // printf("%s\n",buffer); } } fclose(jpeg); //check the received md5 with the calculated one jpeg = fopen("out_file.jpg","rb"); generate_md5(jpeg, calculated_md5); printf("Calculated MD5:\n"); print_md5(calculated_md5); printf("Received MD5:\n"); print_md5(received_md5); if(strncmp(calculated_md5, received_md5, MD5_DIGEST_LENGTH) == 0){ //send match send(sock,ack,strlen(ack),0); printf("Checksums match\n"); }else{ //send mismatch send(sock,nack,strlen(nack),0); printf("Checksum mismatch\n"); } printf("Write fails: %d\n", write_fail_count); printf("File received\n"); fclose(jpeg); shutdown(sock,2); return 0; }
asmlinkage long xcrypt(void *arg) { if (arg == NULL) { printk("User level argument is NULL\n"); return -EINVAL; } else { struct myargs *arguments; int result, paddedresult, padvalue, preambleWritten, temp, paddingWritten, paddingRead; struct file *filp=NULL, *filp1=NULL; struct dentry *dentry=NULL, *olddentry=NULL, *newdentry=NULL; long int inputFileLen=-1, outputFileLen=-1, keyLen=-1; char *srcbuffer, *destbuffer, *padding, *keyFromFile=NULL, *md5_hash=NULL, *tempOutFile; umode_t inputFileMode, outputFileMode; bool outFileCreated=false, renamed=false, success = false; int err=0; arguments = (struct myargs *)kmalloc(sizeof(struct myargs), GFP_KERNEL); if (arguments==NULL) { printk("Failed to allocate kernel memory\n"); err = -ENOMEM; goto out1; } result = copy_from_user((void *)arguments, arg, sizeof(struct myargs)); if(result!=0) { printk("Copying from user failed\n"); err = -EFAULT; goto out2; } if (((struct myargs*)arg)->inputFile == NULL) { /*Checking whether user passed NULL input File*/ printk("user level input file argument is NULL\n"); err = -EINVAL; goto out2; } inputFileLen = strnlen_user(((struct myargs*)arg)->inputFile, 32767); //TODO get the maximum value from getname if (inputFileLen == -1) { printk("Finding User inputFile string length Failed\n"); err = -EFAULT; goto out2; } arguments->inputFile = (char *)kmalloc(inputFileLen*sizeof(char), GFP_KERNEL); if ((arguments->inputFile)==NULL) { printk("Failed to allocate kernel memory for input file\n"); err = -ENOMEM; goto out2; } result = strncpy_from_user(arguments->inputFile, ((struct myargs*)arg)->inputFile, inputFileLen); if(result!=(inputFileLen-1)) { printk("Copying input file string from user failed\n"); err = -EFAULT; goto out3; } if((arguments->inputFile)==NULL) { printk("Copying input file string from user failed\n"); err = -EFAULT; goto out3; } if (((struct myargs*)arg)->outputFile == NULL) { printk("user level output file argument is NULL\n"); err = -EINVAL; goto out3; } outputFileLen = strnlen_user(((struct myargs*)arg)->outputFile, 32767); //TODO get the maximum value from getname if (outputFileLen == -1) { printk("Finding User outputFile string length Failed\n"); err = -EFAULT; goto out3; } arguments->outputFile = (char *)kmalloc(outputFileLen*sizeof(char), GFP_KERNEL); if ((arguments->outputFile)==NULL) { printk("Failed to allocate kernel memory for outputfile\n"); err = -ENOMEM; goto out3; } result = strncpy_from_user(arguments->outputFile, ((struct myargs*)arg)->outputFile, outputFileLen); if(result!=(outputFileLen-1)) { printk("Copying output file string from user failed\n"); err = -EFAULT; goto out4; } if((arguments->outputFile)==NULL) { printk("Copying output file string from user failed\n"); err = -EFAULT; goto out4; } if (((struct myargs*)arg)->keyBuf == NULL) { printk("user level key buffer argument is NULL\n"); err = -EINVAL; goto out4; } keyLen = strnlen_user(((struct myargs*)arg)->keyBuf, 32767); //TODO get the maximum value from getname if (keyLen == -1) { printk("Finding User keyBuf string length Failed\n"); err = -EFAULT; goto out4; } arguments->keyBuf = (char *)kmalloc(keyLen*sizeof(char), GFP_KERNEL); if ((arguments->keyBuf)==NULL) { printk("Failed to allocate kernel memory\n"); err = -ENOMEM; goto out4; } result = strncpy_from_user(arguments->keyBuf, ((struct myargs*)arg)->keyBuf, keyLen); if(result!=(keyLen-1)) { printk("Copying key buf string from user failed\n"); err = -EFAULT; goto out5; } if((arguments->keyBuf)==NULL) { printk("Copying key buf string from user failed\n"); err = -EFAULT; goto out5; } if (strlen(arguments->keyBuf) != arguments->keyLen) { printk("User key buffer length and kernel key buffer lengths differ\n"); err = -EINVAL; goto out5; } if((arguments->flags)!=0 && (arguments->flags)!=1) { printk("Invalid values for flag argument, it should be either 1 or 0\n"); err = -EINVAL; goto out5; } srcbuffer = (char *)kmalloc((PAGE_SIZE+1)*sizeof(char), GFP_KERNEL); if (srcbuffer==NULL) { printk("Failed to allocate kernel memory for srcbuffer\n"); err = -ENOMEM; goto out5; } destbuffer = (char *)kmalloc((PAGE_SIZE+1)*sizeof(char), GFP_KERNEL); if (destbuffer==NULL) { printk("Failed to allocate kernel memory for destbuffer\n"); err = -ENOMEM; goto out6; } padding = (char *)kmalloc(4*sizeof(char), GFP_KERNEL); if (padding==NULL) { printk("Failed to allocate kernel memory for padding\n"); err = -ENOMEM; goto out7; } initialisePadding(padding); /*Initialising the padding array to all 0's */ result=PAGE_SIZE; filp = filp_open(arguments->inputFile, O_RDONLY, 0); if (PTR_ERR(filp)==-ENOENT) { printk("Input File doesnot exists\n"); err = -ENOENT; goto out8; } else { printk("Input File exists\n"); } if (!filp || IS_ERR(filp)) { printk("Read error for input file %d\n", (int) PTR_ERR(filp)); err = PTR_ERR(filp); goto out8; } inputFileMode = filp->f_inode->i_mode; if(!S_ISREG(inputFileMode)) { printk("Input File is not a regular file\n"); err = -EISDIR; goto out9; } if (!filp->f_op->read) { printk("Error in reading input file\n"); err = PTR_ERR(filp); goto out9; } filp1 = filp_open(arguments->outputFile, O_WRONLY, 0); if (PTR_ERR(filp1)==-ENOENT) { printk("Output File doesnot exists, creating it\n"); filp1 = filp_open(arguments->outputFile, O_CREAT, inputFileMode); /*Creating output file if it doesnot exists with input file permissions*/ if(!filp1 || IS_ERR(filp1)) { printk("Error in creating output file\n"); err = PTR_ERR(filp1); goto out8_1; } else { printk("Output File created succesfully\n"); outFileCreated = true; } } else { printk("Output File exists\n"); } if(!outFileCreated) { if(!filp1 || IS_ERR(filp1)) { printk("Error in opening output file\n"); err = PTR_ERR(filp1); goto out8_1; } } if(!outFileCreated) { outputFileMode = filp1->f_inode->i_mode; if(!S_ISREG(outputFileMode)) { printk("Output File is not a regular file\n"); err = -EISDIR; goto out9; } } if(!outFileCreated) { if (filp->f_inode->i_ino == filp1->f_inode->i_ino) { printk("Both input and output files are same, they should be different\n"); err = -EPERM; goto out9; } } if(!outFileCreated) { olddentry = filp1->f_path.dentry; filp_close(filp1, NULL); tempOutFile = (char *)kmalloc((strlen(arguments->outputFile)+5)*sizeof(char), GFP_KERNEL); strncpy(tempOutFile, arguments->outputFile, strlen(arguments->outputFile)); strcat(tempOutFile, ".tmp"); tempOutFile[(strlen(arguments->outputFile)+5)]='\0'; filp1 = filp_open(tempOutFile, O_WRONLY, 0); if (PTR_ERR(filp1)==-ENOENT || IS_ERR(filp1)) { printk("temp Output File doesnot exists, creating it\n"); } else { printk("temp output File exists, truncating and creating new one\n"); dentry = filp1->f_path.dentry; filp_close(filp1, NULL); err = file_unlink(dentry->d_parent->d_inode, dentry); if(err != 0) { printk("unlink of already existing temporary file failed\n"); err = -EBUSY; goto out9; } printk("unlink function returned : %d\n", err); } filp1 = filp_open(tempOutFile, O_CREAT, outputFileMode); if(!filp1 || IS_ERR(filp1)) { printk("Error in creating temp output file\n"); err = PTR_ERR(filp1); goto out8_1; } else { printk("temp output File created succesfully\n"); } } newdentry = filp1->f_path.dentry; if (!filp1 || IS_ERR(filp1)) { printk("Write error for output file %d\n", (int) PTR_ERR(filp1)); err = PTR_ERR(filp1); goto out10; } if (!filp1->f_op->write) { printk("Error in writing to temp output file\n"); err = PTR_ERR(filp1); goto out10; } md5_hash = kmalloc(17*sizeof(char), GFP_KERNEL); if (md5_hash==NULL) { printk("Failed to allocate kernel memory for key from file\n"); err = -ENOMEM; goto out10; } err = generate_md5(arguments->keyBuf, md5_hash, strlen(arguments->keyBuf)); if(err != 0) { printk("kernel MD5 generation failed\n"); goto out11; } md5_hash[strlen(arguments->keyBuf)] = '\0'; if (arguments->flags) { //ENCRYPTION preambleWritten = wrapfs_write_file(filp1, (void *)(md5_hash), strlen(md5_hash)); /*Writing Key hash to the file*/ if(preambleWritten < 0) { printk("Writing preamble failed\n"); err = -EFAULT; goto out11; } paddingWritten = wrapfs_write_file(filp1, (void *)(padding), 4); /*Writing Inital Padding to the file*/ if (paddingWritten < 0) { printk("Writing padding failed\n"); err = -EFAULT; goto out11; } while(result==PAGE_SIZE) { result = wrapfs_read_file(filp, (void *)srcbuffer, PAGE_SIZE); if(result < 0) { printk("Reading from input file failed\n"); err = -EFAULT; goto out11; } err = encrypt_Cipher(arguments->keyBuf, srcbuffer, destbuffer, result, &paddedresult); if (err < 0) { printk("Error occured while encrypting\n"); goto out11; } if(paddedresult!=result) { padvalue = paddedresult - result; buildPadding(padding, padvalue); result = paddedresult; } result = wrapfs_write_file(filp1, (void *)destbuffer, result); if (result < 0) { printk("Writing to output file failed\n"); err = -EFAULT; goto out11; } } paddingWritten = wrapfs_write_file_pos(filp1, (void *)padding, 4, preambleWritten); if (paddingWritten < 0) { printk("Writing padding failed\n"); err = -EFAULT; goto out11; } } else { //DECRYPTION keyFromFile = kmalloc(16*sizeof(char), GFP_KERNEL); if (keyFromFile==NULL) { printk("Failed to allocate kernel memory for key from file\n"); err = -ENOMEM; goto out11; } temp = wrapfs_read_file(filp, (void *)keyFromFile, strlen(md5_hash)); if (temp != strlen(md5_hash)) { printk("reading key from file failed\n"); err = -EFAULT; goto out12; } if (compareKeys(md5_hash, keyFromFile, temp)) { printk("Both Keys Match\n"); } else { printk("Both keys doesnot match\n"); err = -EINVAL; //TODO : Return proper error Value. goto out12; } paddingRead = wrapfs_read_file(filp, (void *)padding, 4); if(paddingRead < 0) { printk("Reading padding failed\n"); err = -EFAULT; goto out12; } padvalue = reconstructPadding(padding); if (padvalue < 0) { printk("Reconstructing padding value failed(negative value not acceptable)\n"); err = -EFAULT; goto out12; } printk("Pad value returned : %d\n", padvalue); while(result==PAGE_SIZE) { result = wrapfs_read_file(filp, (void *)srcbuffer, PAGE_SIZE); if (result < 0) { printk("Reading from input file failed\n"); err = -EFAULT; goto out12; } printk("result read from file : %u\n", result); err = decrypt_Cipher(arguments->keyBuf, srcbuffer, destbuffer, result); if (err < 0) { printk("Error occured while encrypting\n"); goto out12; } if (result<PAGE_SIZE) { result = result - padvalue; } result = wrapfs_write_file(filp1, (void *)destbuffer, result); if(result < 0) { printk("writing to output file failed\n"); err = -EFAULT; //TODO goto goto out12; } } } if(!outFileCreated) { err = file_rename(newdentry->d_parent->d_inode, newdentry, olddentry->d_parent->d_inode, olddentry); if(err!=0) { printk("renaming of tempfile to output file failed\n"); err = -EBUSY; goto out12; } else renamed = true; } else success = true; out12: if(keyFromFile) kfree(keyFromFile); out11: kfree(md5_hash); out10: if(filp1) filp_close(filp1, NULL); if((!renamed && !outFileCreated)||(!success && outFileCreated)) file_unlink(newdentry->d_parent->d_inode, newdentry); out9: if(filp1) filp_close(filp1, NULL); out8_1: if(filp) filp_close(filp, NULL); out8: kfree(padding); out7: kfree(destbuffer); out6: kfree(srcbuffer); out5: kfree(arguments->keyBuf); out4: kfree(arguments->outputFile); out3: kfree(arguments->inputFile); out2: kfree(arguments); out1: return err; } }