char *do_crypt (char *str, Crypt *key, int flag) { size_t c; char *free_it = NULL; c = strlen(str); if (flag) { if (key->prog) free_it = str = (char*)prog_crypt(str, &c, key, flag); else my_encrypt(str, c, key->key); str = enquote_it(str, c); } else { str = dequote_it(str, &c); if (key->prog) str = (char*)prog_crypt(free_it = str, &c, key, flag); else my_decrypt(str, c, key->key); } new_free(&free_it); return (str); }
int main(void){ char *source = (char *)malloc(24); memcpy(source,"Welcome to LinkC Project",24); source[24] = 0; const char *OPENSSLKEY = "test.key"; const char *PUBLICKEY = "test_pub.key"; char *ptr_en,*ptr_de; printf("source is :%s\n",source); ptr_en=my_encrypt(source,PUBLICKEY); printf("after encrypt:%s\n",ptr_en); ptr_de=my_decrypt(ptr_en,OPENSSLKEY); printf("after decrypt:%s\n",ptr_de); if(ptr_en!=NULL){ free(ptr_en); } if(ptr_de!=NULL){ free(ptr_de); } return 0; }
static void new_dcc_message_transmit (char *user, const char *text, const char *text_display, int type, int flag) { GetFile *gf = NULL; char tmp[BIG_BUFFER_SIZE+1]; int len = 0; char buffer[NICKNAME_LEN+10]; *tmp = 0; switch(type) { case NAP_CHAT_CONNECTED: break; default: return; } gf = find_in_getfile(0, user, NULL, NULL, 0, type); if (!gf) { if ((gf = find_in_getfile(0, user, NULL, NULL, 0, NAP_CHAT))) say("No active DCC CHAT connection for %s", user); else say("No DCC CHAT connection for %s", user); return; } strmcat(tmp, text, BIG_BUFFER_SIZE-3); len = strlen(tmp); my_encrypt(tmp, len, gf->passwd); tmp[len++] = '\n'; tmp[len] = 0; write(gf->socket, tmp, len); gf->received += len; if (flag) put_it(FORMAT_SENDDCC, user, gf->ip, text_display?text_display:text); sprintf(buffer, "=%s", user); addtabkey(buffer, "msg", (char *)(text_display ? text_display : text)); }
int wrapfs_write_end(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata) { char *page_data = (char *)kmap(page); struct file *lower_file = wrapfs_lower_file(file); struct inode *inode = page->mapping->host; struct inode *lower_inode = NULL; unsigned from = pos & (PAGE_CACHE_SIZE - 1); mm_segment_t old_fs; int err = 0; #ifdef WRAPFS_CRYPTO struct inode *cur_inode = page->mapping->host; pgoff_t index = pos >> (PAGE_CACHE_SHIFT); loff_t cur_inode_size = cur_inode->i_size; pgoff_t cur_inode_last_index = cur_inode_size >> (PAGE_CACHE_SHIFT); unsigned int cur_inode_end_offset; loff_t extra_padding = pos - cur_inode_size; char *encrypted_buf = NULL; unsigned copied1 = copied; cur_inode_end_offset = cur_inode_size & (PAGE_CACHE_SIZE - 1); #endif wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops, ""); wrapfs_debug(""); if (lower_file == NULL) { wrapfs_debug("lower_file is NULL!!\n"); err = -EACCES; goto out; } wrapfs_debug("pos : %lld", pos); wrapfs_debug("from : %u", from); wrapfs_debug("copied : %u", copied); wrapfs_debug("lower_file->f_pos : %lld", lower_file->f_pos); #ifdef WRAPFS_CRYPTO if (extra_padding > 0 && (cur_inode_last_index == index)) { copied = copied + pos - cur_inode_size; from = cur_inode_end_offset; } encrypted_buf = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL); if (encrypted_buf == NULL) { wrapfs_debug("encrypted_buf is NULL!!"); err = -ENOMEM; goto out; } err = my_encrypt(page_data, PAGE_CACHE_SIZE, encrypted_buf, PAGE_CACHE_SIZE, WRAPFS_SB(file->f_dentry->d_sb)->key, WRAPFS_CRYPTO_KEY_LEN); if (err < 0) { wrapfs_debug("Encrypt failed!!"); err = -EINVAL; kfree(encrypted_buf); goto out; } #endif lower_file->f_pos = page_offset(page) + from; if (!PageUptodate(page)) SetPageUptodate(page); wrapfs_debug("pos : %lld", pos); wrapfs_debug("from : %u", from); wrapfs_debug("copied : %u", copied); wrapfs_debug("lower_file->f_pos : %lld", lower_file->f_pos); old_fs = get_fs(); set_fs(KERNEL_DS); #ifndef WRAPFS_CRYPTO err = vfs_write(lower_file, page_data + from, copied, &lower_file->f_pos); #else err = vfs_write(lower_file, encrypted_buf + from, copied, &lower_file->f_pos); /* If zeroes need to be placed, then err exceeds copied. * In this case, we need to make err=copied1 to avoid oops in iov_iter */ if (err > 0 && extra_padding > 0) err = copied1; #endif wrapfs_debug("err : %d", err); set_fs(old_fs); if (err < 0) { wrapfs_debug("vfs_write error : %d!!\n", err); err = -EINVAL; #ifdef WRAPFS_CRYPTO kfree(encrypted_buf); #endif goto out; } lower_inode = lower_file->f_path.dentry->d_inode; if (!lower_inode) lower_inode = wrapfs_lower_inode(inode); BUG_ON(!lower_inode); fsstack_copy_inode_size(inode, lower_inode); fsstack_copy_attr_times(inode, lower_inode); out: kunmap(page); unlock_page(page); page_cache_release(page); wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops, "err : %d", err); return err; }
/* I have followed the behavior from ecryptfs. write_begin sets up the page. * for writing. Following changes are made : * 1. If Encrypt is not enabled, then just grab the page and set it up for * write_begin. It is almost similar to ecryptfs. When we seek to a position * after EOF and write, then the copied bytes are adjusted accordingly and * passed. For example, if the file contains 2000 bytes and if we write * 1000 bytes from 3000th position(by lseeking), then from contains 3000 and * copied contains 1000. So we can directly copy 1000 bytes to lower file. * 2. When Encrypt is enabled, three cases are possible which are commented * below. We must handle zero bytes cases explicitly. */ int wrapfs_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata) { struct page *page; char *page_data; pgoff_t index; int err = 0; struct inode *cur_inode, *lower_inode; unsigned int offset = 0; #ifdef WRAPFS_CRYPTO /* pgoff_t is unsigned long, loff_t is long long */ loff_t cur_inode_size; pgoff_t cur_inode_last_index; unsigned int cur_inode_end_offset; unsigned int zero_count; char *page_data_zeros; struct page *page_to_zeros = NULL; pgoff_t tempindex; pgoff_t tempoffset; pgoff_t bytes_to_write; struct file *lower_file = wrapfs_lower_file(file); char *encrypted_buf; mm_segment_t old_fs; #endif wrapfs_debug(""); wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops, ""); index = pos >> PAGE_CACHE_SHIFT; offset = pos & (PAGE_CACHE_SIZE - 1); wrapfs_debug("index : %lu, offset : %d\n", index, offset); page = grab_cache_page_write_begin(mapping, index, flags); if (!page) { wrapfs_debug("grab_cache_page_write_begin returned NULL!!"); err = -ENOMEM; goto out; } page_data = (char *)kmap(page); *pagep = page; cur_inode = file->f_path.dentry->d_inode; if (cur_inode) lower_inode = wrapfs_lower_inode(cur_inode); #ifdef WRAPFS_CRYPTO /* cur_inode* refers to the file's existing attributes */ cur_inode_size = cur_inode->i_size; cur_inode_last_index = cur_inode_size >> (PAGE_CACHE_SHIFT); cur_inode_end_offset = cur_inode_size & (PAGE_CACHE_SIZE - 1); wrapfs_debug( "cur_inode->i_size : %lu, i_size_read(page->mapping->host) : %lu\n", (unsigned long)cur_inode->i_size, (unsigned long)i_size_read(page->mapping->host)); if (index == cur_inode_last_index) { /* The page to write is same as last page in file */ wrapfs_debug(""); if (pos > cur_inode_size) { /* Need to fill zeroes upto pos, * from cur_inode_size */ wrapfs_debug(""); zero_count = pos - cur_inode_size; memset(page_data + cur_inode_end_offset, 0x00, zero_count); } else if (pos == cur_inode_size) { wrapfs_debug(""); /* Fine. Do a normal encryption in write_end */ } else if (pos < cur_inode_size) { /* Fine. Do a normal encryption in write_end */ wrapfs_debug(""); } } else if (index < cur_inode_last_index) { /* The page to write is an intermediate file page. * No special cases need to be handled here. */ wrapfs_debug(""); } else if (index > cur_inode_last_index) { /* If we skip to a page more than the last page in file. * Need to fill holes between cur_inode_last_index and index. * First filling hole in the new index page upto offset. */ wrapfs_debug(""); memset(page_data, 0x00, offset); tempoffset = cur_inode_end_offset; tempindex = cur_inode_last_index; lower_file->f_pos = cur_inode_size; encrypted_buf = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL); if (encrypted_buf == NULL) { wrapfs_debug("kmalloc failed!!"); err = -ENOMEM; goto out_holes; } /* Fill zeroes in page cur_inode_last_index from cur off to end * Then fill all pages from (cur_inode_last_index + 1) to index * These must also be encrypted and written to lower file here * itself as they are not reflected in write_end. */ while (tempindex < index) { page_to_zeros = grab_cache_page_write_begin(cur_inode->i_mapping, tempindex, flags); if (page_to_zeros == NULL) { wrapfs_debug("grab_cache_page failed!!"); kfree(encrypted_buf); err = -ENOMEM; goto out_holes; } page_data_zeros = (char *)kmap(page_to_zeros); bytes_to_write = PAGE_CACHE_SIZE - tempoffset; memset(page_data_zeros + tempoffset, 0x00, bytes_to_write); err = my_encrypt(page_data_zeros, PAGE_CACHE_SIZE, encrypted_buf, PAGE_CACHE_SIZE, WRAPFS_SB(file->f_dentry->d_sb)->key, WRAPFS_CRYPTO_KEY_LEN); if (err < 0) { wrapfs_debug("Encryption failed!!"); kfree(encrypted_buf); err = -EINVAL; goto free_pages_holes; } flush_dcache_page(page_to_zeros); old_fs = get_fs(); set_fs(KERNEL_DS); err = vfs_write(lower_file, encrypted_buf + tempoffset, bytes_to_write, &lower_file->f_pos); set_fs(old_fs); free_pages_holes: kunmap(page_to_zeros); unlock_page(page_to_zeros); page_cache_release(page_to_zeros); if (err < 0) { kfree(encrypted_buf); goto out_holes; } err = 0; mark_inode_dirty_sync(cur_inode); tempoffset = 0; tempindex++; } /* while ends */ out_holes: if ((err < 0) && (page_to_zeros != NULL)) ClearPageUptodate(page_to_zeros); } #endif out: if (page) kunmap(page); if (unlikely(err)) { unlock_page(page); page_cache_release(page); *pagep = NULL; } wrapfs_debug_aops(WRAPFS_SB(file->f_dentry->d_sb)->wrapfs_debug_a_ops, "err : %d", err); return err; }
/* * put_file() - send a file to the server accessible via the given socket fd */ void put_file(int fd, char *put_name) { /* open file */ FILE* file = fopen(put_name,"r"); if(file == NULL) die("open file error","can not open file"); /* read contents from file*/ fseek(file,0L,SEEK_END); long length = ftell(file); fseek(file,0L,SEEK_SET); char* buf = (char*)malloc(sizeof(char) * length + 1); bzero(buf,length + 1); fread(buf,length,1,file); /* encrypt file contents */ char* pData = (char*)malloc(length + 256); bzero(pData,length + 256); char* pcrystr = (char*)malloc(257); bzero(pcrystr,257); size_t bytes = 0; int j = 0; while(1) { if(j != 256) { if(buf[bytes + j]) { pcrystr[j] = buf[bytes + j]; j++; } else { pcrystr[j] = 0; pcrystr = my_encrypt(pcrystr,PUBLICKEY); int k = 0; for(;k < 256;k++) pData[bytes + k] = pcrystr[k]; bytes += 256; pData[bytes] = '\n'; break; } } else { if(buf[bytes + j]) { pcrystr[j] = 0; pcrystr = my_encrypt(pcrystr,PUBLICKEY); int k = 0; for(;k < 256;k++) pData[bytes + k] = pcrystr[k]; bytes += 256; j = 0; } else { pcrystr[j] = 0; pcrystr = my_encrypt(pcrystr,PUBLICKEY); int k = 0; for(;k < 256;k++) pData[bytes + k] = pcrystr[k]; bytes += 256; pData[bytes] = '\n'; break; } } } /* construct tcp message */ char message[512]; bzero(message,512); char* ptr_mes = message; /* add "PUT\n" in tcp message */ size_t len = strlen("PUT\n"); sprintf(ptr_mes,"PUT\n"); ptr_mes += len; /* add "filename\n" in tcp message */ len = strlen(put_name); sprintf(ptr_mes,"%s\n",put_name); ptr_mes += len + 1; /* add "# bytes\n" in tcp message */ char temp[100]; bzero(temp,100); sprintf(temp,"%lu bytes\n",bytes); len = strlen(temp); sprintf(ptr_mes,"%s",temp); /* send first tcp message to server */ size_t nremain = strlen(message); ssize_t nsofar; ptr_mes = message; while (nremain > 0) { if ((nsofar = write(fd, ptr_mes, nremain)) <= 0) { if (errno != EINTR) { fprintf(stderr, "Write error: %s\n", strerror(errno)); exit(0); } nsofar = 0; } nremain -= nsofar; ptr_mes += nsofar; } /* handle server reply */ char rcv[100] = {0}; nsofar = read(fd,rcv,100); char *p = pData; if(nsofar == 3) { nremain = bytes + 1; while(nremain > 0) { if ((nsofar = write(fd, p, nremain)) <= 0) { if (errno != EINTR) { fprintf(stderr, "Write error: %s\n", strerror(errno)); exit(0); } nsofar = 0; } nremain -= nsofar; p += nsofar; } bzero(rcv,100); nsofar = read(fd,rcv,100); if(rcv[0] != 'O') die("Put file error",rcv); else printf("put file %s success\n",put_name); } }