Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/linkage.h>
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/syscalls.h>
- #include <linux/fcntl.h>
- #include <linux/fs.h>
- #include <linux/crypto.h>
- #include <linux/err.h>
- #include <linux/scatterlist.h>
- #include <linux/uaccess.h>
- #include <asm/segment.h>
- #include <linux/buffer_head.h>
- #include <linux/moduleloader.h>
- #include <keys/ceph-type.h>
- #include <linux/ceph/decode.h>
- struct xcryptParam {
- int encrypt;
- char *infile;
- char *outfile;
- char *password;
- int passlen;
- };
- asmlinkage extern long (*sysptr)(void *argu);
- static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
- asmlinkage long xcrypt(void *argu)
- {
- struct xcryptParam *ptr;
- struct xcryptParam *arg = (struct xcryptParam *)argu;
- int len;
- int toEncrypt;
- //printk("Pass len: %i\n", arg->passlen);
- /*
- * Hardcoded in and out files!!!
- *
- * Remember to remove these when accepting input
- */
- char *infile = "outfile.txt";
- char *outfile = "newfile.txt";
- //char *tempKey = "key";
- char *key = NULL;
- void *iv;
- int copyCheckBytes;
- int bytesRead = 0;
- int ivsize;
- int page_size = PAGE_SIZE;
- struct file *filpRead = NULL, *filpWrite = NULL; // Read and writing file poinrters
- char inbuf[page_size], outbuf[page_size];
- //ssize_t result = 0;
- struct crypto_blkcipher *cbc;
- struct blkcipher_desc desc = { .tfm = NULL, .flags = 0};
- struct scatterlist inS[1], outS[1]; // [0] is source, [1] is desination
- //size_t dst_size;
- mm_segment_t old_fs;
- printk("%i\n", page_size);
- len = sizeof(struct xcryptParam);
- if(access_ok(VERIFY_READ, arg, len)) {
- printk("Arg is good\n");
- }
- else {
- printk("Arg is not good, size %i, needs %i\n", len, sizeof(arg));
- return -EFAULT;
- }
- ptr = kmalloc(len, GFP_KERNEL);
- if((copyCheckBytes = copy_from_user(ptr, arg, len))) {
- printk("%i bytes not copied!\n", copyCheckBytes);
- }
- printk("%i\n", len);
- infile = getname(ptr->infile);
- outfile = getname(ptr->outfile);
- toEncrypt = ptr->encrypt;
- if (infile == NULL || outfile == NULL) {
- printk("Nulls set\n");
- return -EFAULT;
- }
- printk("%s\n", infile);
- printk("%s\n", outfile);
- printk("%i\n", toEncrypt);
- if(access_ok(VERIFY_READ, ptr->password, ptr->passlen)) {
- printk("Password is good\n");
- }
- else {
- printk("Password is not good\n");
- return -EINVAL;
- }
- key = kmalloc(ptr->passlen, GFP_KERNEL);
- printk("Pass len: %i\n", ptr->passlen);
- if((copyCheckBytes = copy_from_user(key, ptr->password, ptr->passlen))) {
- printk("%i bytes not copied!\n", copyCheckBytes);
- }
- printk("%s\n", key);
- old_fs = get_fs(); // Keep track of the old filesystem to reset at the end
- set_fs(KERNEL_DS);
- printk("Starting to open\n");
- filpRead = filp_open(infile, O_RDONLY, 0);
- filpWrite = filp_open(outfile, O_CREAT | O_WRONLY | O_TRUNC, 0666); // If file does not exits, create with the correct permissions
- if (IS_ERR(filpRead)) {
- return -1;
- }
- if (IS_ERR(filpWrite)) {
- return -1;
- }
- printk("Files open\n");
- printk(KERN_DEBUG);
- printk("Set Debug\n");
- cbc = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
- printk("Defined CBC\n");
- crypto_blkcipher_setkey(cbc, key, sizeof(key));
- printk("Set CBC key\n");
- desc.tfm = cbc;
- printk("Entering read loop\n");
- bytesRead = filpRead->f_op->read(filpRead, inbuf, page_size, &filpRead->f_pos);
- /*while ((bytesRead = filpRead->f_op->read(filpRead, inbuf, page_size, &filpRead->f_pos)) > 0) {
- sg_init_table(inS, 1);
- sg_init_table(outS, 1);
- sg_set_buf(&inS[0], inbuf, bytesRead);
- sg_set_buf(&outS[0], outbuf, bytesRead);
- //sg_set_buf(sg_out, dst, *dst_len);
- iv = crypto_blkcipher_crt(cbc)->iv;
- ivsize = crypto_blkcipher_ivsize(cbc);
- memcpy(iv, aes_iv, ivsize);
- if(toEncrypt)
- crypto_blkcipher_encrypt(&desc, &outS[0], &inS[0], bytesRead);
- else
- crypto_blkcipher_decrypt(&desc, &outS[0], &inS[0], bytesRead);
- printk("%i\n", bytesRead);
- filpWrite->f_op->write(filpWrite, outbuf, bytesRead, &filpWrite->f_pos);
- printk("%s", outbuf);
- }*/
- printk("%s", inbuf);
- printk("%i\n", bytesRead);
- crypto_free_blkcipher(cbc);
- printk("\n");
- filp_close(filpRead, NULL);
- filp_close(filpWrite, NULL);
- kfree(ptr);
- kfree(key);
- set_fs(old_fs);
- return 0;
- }
- static int __init init_sys_xcrypt(void)
- {
- printk("installed new sys_xcrypt module\n");
- if (sysptr == NULL)
- sysptr = xcrypt;
- return 0;
- }
- static void __exit exit_sys_xcrypt(void)
- {
- if (sysptr != NULL)
- sysptr = NULL;
- printk("removed sys_xcrypt module\n");
- }
- module_init(init_sys_xcrypt);
- module_exit(exit_sys_xcrypt);
- MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement