Advertisement
Guest User

encrytion_module.c

a guest
Dec 10th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.12 KB | None | 0 0
  1. #include <linux/linkage.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/syscalls.h>
  5. #include <linux/fcntl.h>
  6. #include <linux/fs.h>
  7. #include <linux/crypto.h>
  8. #include <linux/err.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/uaccess.h>
  11. #include <asm/segment.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/moduleloader.h>
  14. #include <keys/ceph-type.h>
  15. #include <linux/ceph/decode.h>
  16.  
  17. struct xcryptParam {
  18.     int encrypt;
  19.     char *infile;
  20.     char *outfile;
  21.     char *password;
  22.     int passlen;
  23. };
  24.  
  25. asmlinkage extern long (*sysptr)(void *argu);
  26.  
  27. static const u8 *aes_iv = (u8 *)CEPH_AES_IV;
  28.  
  29. asmlinkage long xcrypt(void *argu)
  30. {
  31.  
  32.     struct xcryptParam *ptr;
  33.     struct xcryptParam *arg = (struct xcryptParam *)argu;
  34.     int len;
  35.     int toEncrypt;
  36.     //printk("Pass len: %i\n", arg->passlen);
  37.     /*
  38.     * Hardcoded in and out files!!!
  39.     *
  40.     * Remember to remove these when accepting input
  41.     */
  42.     char *infile = "outfile.txt";
  43.     char *outfile = "newfile.txt";
  44.     //char *tempKey = "key";
  45.     char *key = NULL;
  46.     void *iv;
  47.     int copyCheckBytes;
  48.     int bytesRead = 0;
  49.     int ivsize;
  50.     int page_size = PAGE_SIZE;
  51.     struct file *filpRead = NULL, *filpWrite = NULL;    // Read and writing file poinrters
  52.     char inbuf[page_size], outbuf[page_size];
  53.  
  54.     //ssize_t result = 0;
  55.     struct crypto_blkcipher *cbc;
  56.     struct blkcipher_desc desc = { .tfm = NULL, .flags = 0};
  57.     struct scatterlist inS[1], outS[1];  // [0] is source, [1] is desination
  58.     //size_t dst_size;
  59.     mm_segment_t old_fs;
  60.  
  61.     printk("%i\n", page_size);
  62.  
  63.     len = sizeof(struct xcryptParam);
  64.     if(access_ok(VERIFY_READ, arg, len)) {
  65.         printk("Arg is good\n");
  66.     }
  67.     else {
  68.         printk("Arg is not good, size %i, needs %i\n", len, sizeof(arg));
  69.         return -EFAULT;
  70.     }
  71.     ptr = kmalloc(len, GFP_KERNEL);
  72.     if((copyCheckBytes = copy_from_user(ptr, arg, len))) {
  73.         printk("%i bytes not copied!\n", copyCheckBytes);
  74.     }
  75.     printk("%i\n", len);
  76.     infile = getname(ptr->infile);
  77.     outfile = getname(ptr->outfile);
  78.     toEncrypt = ptr->encrypt;
  79.  
  80.     if (infile == NULL || outfile == NULL) {
  81.         printk("Nulls set\n");
  82.         return -EFAULT;
  83.     }
  84.  
  85.     printk("%s\n", infile);
  86.     printk("%s\n", outfile);
  87.     printk("%i\n", toEncrypt);
  88.     if(access_ok(VERIFY_READ, ptr->password, ptr->passlen)) {
  89.         printk("Password is good\n");
  90.     }
  91.     else {
  92.         printk("Password is not good\n");
  93.         return -EINVAL;
  94.     }
  95.     key = kmalloc(ptr->passlen, GFP_KERNEL);
  96.     printk("Pass len: %i\n", ptr->passlen);
  97.     if((copyCheckBytes = copy_from_user(key, ptr->password, ptr->passlen))) {
  98.         printk("%i bytes not copied!\n", copyCheckBytes);
  99.     }
  100.  
  101.     printk("%s\n", key);
  102.  
  103.     old_fs = get_fs(); // Keep track of the old filesystem to reset at the end
  104.     set_fs(KERNEL_DS);
  105.  
  106.     printk("Starting to open\n");
  107.  
  108.     filpRead = filp_open(infile, O_RDONLY, 0);
  109.     filpWrite = filp_open(outfile, O_CREAT | O_WRONLY | O_TRUNC, 0666); // If file does not exits, create with the correct permissions
  110.  
  111.     if (IS_ERR(filpRead)) {
  112.         return -1;
  113.     }
  114.     if (IS_ERR(filpWrite)) {
  115.         return -1;
  116.     }
  117.  
  118.     printk("Files open\n");
  119.  
  120.         printk(KERN_DEBUG);
  121.  
  122.     printk("Set Debug\n");
  123.         cbc = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  124.         printk("Defined CBC\n");
  125.         crypto_blkcipher_setkey(cbc, key, sizeof(key));
  126.         printk("Set CBC key\n");
  127.  
  128.  
  129.         desc.tfm = cbc;
  130.  
  131.         printk("Entering read loop\n");
  132.  
  133.         bytesRead = filpRead->f_op->read(filpRead, inbuf, page_size, &filpRead->f_pos);
  134.         /*while ((bytesRead = filpRead->f_op->read(filpRead, inbuf, page_size, &filpRead->f_pos)) > 0) {
  135.             sg_init_table(inS, 1);
  136.             sg_init_table(outS, 1);
  137.  
  138.             sg_set_buf(&inS[0], inbuf, bytesRead);
  139.             sg_set_buf(&outS[0], outbuf, bytesRead);
  140.             //sg_set_buf(sg_out, dst, *dst_len);
  141.             iv = crypto_blkcipher_crt(cbc)->iv;
  142.             ivsize = crypto_blkcipher_ivsize(cbc);
  143.  
  144.             memcpy(iv, aes_iv, ivsize);
  145.  
  146.             if(toEncrypt)
  147.                 crypto_blkcipher_encrypt(&desc, &outS[0], &inS[0], bytesRead);
  148.             else
  149.                 crypto_blkcipher_decrypt(&desc, &outS[0], &inS[0], bytesRead);
  150.             printk("%i\n", bytesRead);
  151.             filpWrite->f_op->write(filpWrite, outbuf, bytesRead, &filpWrite->f_pos);
  152.             printk("%s", outbuf);
  153.         }*/
  154.         printk("%s", inbuf);
  155.         printk("%i\n", bytesRead);
  156.         crypto_free_blkcipher(cbc);
  157.         printk("\n");
  158.         filp_close(filpRead, NULL);
  159.         filp_close(filpWrite, NULL);
  160.  
  161.  
  162.     kfree(ptr);
  163.     kfree(key);
  164.     set_fs(old_fs);
  165.     return 0;
  166. }
  167.  
  168. static int __init init_sys_xcrypt(void)
  169. {
  170.     printk("installed new sys_xcrypt module\n");
  171.     if (sysptr == NULL)
  172.         sysptr = xcrypt;
  173.     return 0;
  174. }
  175. static void  __exit exit_sys_xcrypt(void)
  176. {
  177.     if (sysptr != NULL)
  178.         sysptr = NULL;
  179.     printk("removed sys_xcrypt module\n");
  180. }
  181. module_init(init_sys_xcrypt);
  182. module_exit(exit_sys_xcrypt);
  183. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement