Advertisement
Guest User

enc

a guest
Aug 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.98 KB | None | 0 0
  1. #include <linux/slab.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/fs.h>
  6. #include <linux/errno.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/ctype.h>
  9. #include <linux/config.h>  
  10. #include <linux/types.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/fcntl.h>
  13. #include <asm/system.h>
  14. #include "enc.h"
  15.  
  16. MODULE_LICENSE("GPL"); // CONSTANT for any code includes modules
  17.  
  18. // Now we declare the functions of the fops
  19. static int open(struct inode* node, struct file* f) ; // OPEN OPERATION
  20. static int release(struct inode* node, struct file* f) ; // RELEASE OPERATION
  21. static ssize_t read(struct file* f, char* buf, size_t count, loff_t* fpos) ; // READING OPERATION
  22. static ssize_t write(struct file* f, const char* buf, size_t count, loff_t* fpos) ; // WRITING OPERATION
  23. int ioctl(struct inode* node, struct file* f, unsigned int cmd, unsigned long arg) ; // IOCTL OPERATION
  24.  
  25. // We assign the parameters of the module, with intialziation..
  26. int enc_devices = 4; // Intial value
  27. int memory_size = 4096; // Intial value
  28. MODULE_PARM(memory_size, "i"); // i - meaning integer
  29. MODULE_PARM(enc_devices, "i"); // i - meaning integer
  30.  
  31. static int major; // will hold the major # of my device driver
  32.  
  33. // Giving a struct for the fops..
  34. static struct file_operations fops =
  35. {
  36.     .open = open,
  37.     .release = release,
  38.     .read = read,
  39.     .write = write,
  40.     .ioctl = ioctl,
  41.     .owner = THIS_MODULE
  42. };
  43.  
  44. // Each device has unique data, so we give it a struct
  45. struct deviceData
  46. {
  47.     char* readENC;
  48.     char* writeENC;
  49.     char* buffer;
  50.     int key;
  51. };
  52.  
  53. static struct deviceData* dD = NULL; // Intial 'value'
  54.  
  55. // ------------ HELP FUNCTIONS --------------------------
  56. static void encrypt(char* buf, size_t size, int key)
  57. {
  58.     int i;
  59.     if(key%2 == 0) // we do the first encryption
  60.     {
  61.         for(i = 0; i < size; ++i)
  62.             buf[i] = key | ((unsigned char)buf[i]);
  63.     }
  64.     else // It means key%2 == 1, we do the second encryption
  65.     {
  66.         for(i = 0; i < size; ++i)
  67.             buf[i] = (((unsigned char)buf[i])+key*5)%255;
  68.     }
  69. }
  70.  
  71. static void reverseEncrypt(char* buf, size_t size, int key) // Reverse function to the second encryption
  72. {
  73.     int i = 0;
  74.     if(key%2 == 1)
  75.     {
  76.         for(i=0;i<size;i++)
  77.             bufr[i] = (((unsigned char)buf[i]) - 5*key + 255*5)%255; // we add 5*255 because 5*key can be
  78.                                                                          // maximum 255*5
  79.     }
  80.     else // key % 2 != 1...
  81.     {
  82.         return; // we do nothing... undefineable..
  83.     }
  84. }
  85.  
  86. // ------------------------------------------ SOME FUNCTIONS HAVE CONNECTION WITH MANAGING THE DEVICE -------------------------------
  87. void cleanup_module(void)
  88. {
  89.     int i;
  90.     // Free the buffers...
  91.     for (i = 0; i < enc_devices; ++i)
  92.         kfree(dD[i].buffer);
  93.     // Delete data...
  94.     kfree(dD);
  95.     // Delete the connection...
  96.     unregister_chrdev(major, "enc");
  97.     return;
  98. }
  99.  
  100. int init_module(void)
  101. {
  102.     major = register_chrdev(0, "enc", &fops); // Try to make connection between the device to the major.
  103.     if (major < 0) // Suppose it failed, then...
  104.     {
  105.         printk(KERN_WARNING "can't get dynamic major\n");
  106.         return -1;
  107.     }
  108.     // If we get here, connection made successfuly.. so..
  109.     printk(KERN_WARNING "enc: We got the major-  %d\n", major);
  110.     dD = kmalloc(sizeof(*dD) * enc_devices, GFP_KERNEL); // ALLOCATION, according to the number of the encyrpted devices..
  111.     int i;
  112.     for (i = 0; i<enc_devices; i++)
  113.     {
  114.         // Updating fields..
  115.         dD[i].writeENC = kmalloc(memory_size, GFP_KERNEL);
  116.         dD[i].readENC = dD[i].writeENC;
  117.         dD[i].buffer = dD[i].readENC;
  118.         dD[i].key = i;
  119.     }
  120.     printk(KERN_INFO "Intialization made successfuly with the following:  devices %d,memory size %d, device data %p\n",                                    
  121.     enc_devices, memory_size, dD);
  122.     return 0;
  123. }
  124.  
  125. static int open(struct inode* node, struct file* f)
  126. {
  127.     if (MINOR(node->i_rdev) > enc_devices)
  128.         return -ENODEV; // Process uses wrong minor number..
  129.     f->private_data = dD + MINOR(node->i_rdev); // Directing...
  130.     MOD_INC_USE_COUNT;
  131.     return 0;
  132. }
  133.  
  134. static int release(struct inode* node, struct file* f)
  135. {
  136.     MOD_DEC_USE_COUNT;
  137.     return 0;
  138. }
  139.  
  140. static int ioctl (struct inode* node, struct file* f, unsigned int cmd, unsigned long arg)
  141. {
  142.     int minor = MINOR(node->i_rdev);
  143.     switch (cmd)
  144.     {
  145.     case ENC_SEQ:
  146.         dD[minor].key = (dD[minor].key + 1)%256; // According to the written in hw2...
  147.         return dD[minor].key;
  148.     case ENC_RESET:
  149.         dD[minor].key = minor;
  150.         dD[minor].writeENC = dD[minor].buffer;
  151.         dD[minor].readENC = dD[minor].writeENC;
  152.         return 0;
  153.     default:
  154.         return -EINVAL; // proper error.
  155.     }
  156. }
  157.  
  158. static ssize_t write(struct file* f, const char* buf, size_t size, loff_t* fpos)
  159. {
  160.     size_t b_ytes;
  161.     struct dD* dData = f->private_data;
  162.     if (dData->readENC <= dData->writeENC )
  163.     {
  164.         b_ytes = memory_size - dData->writeENC + dData->readENC;
  165.         if (size > b_ytes) return -EINVAL; // According to HW2..
  166.         if(size <= memory_size + dData->buffer - dData->writeENC)
  167.         {
  168.             if (copy_from_user(dData-> writeENC, buf, size) != 0)
  169.                 return -EFAULT; // According to Hw2
  170.             dData->writeENC = size + dData->writeENC; // Advancing
  171.             if (dData->writeENC == memory_size + dData->buffer)
  172.                 dData-> writeENC = dData->buffer;
  173.         }
  174.         else
  175.         {
  176.             size_t defineSize = memory_size + dData->buffer - dData->writeENC;
  177.             if (copy_from_user(dData->writeENC, buf, defineSize))
  178.                 return -EFAULT; // according to hw2...
  179.             dData-> writeENC = dData->buffer;
  180.             if (copy_from_user(dData->writeENC, buf, size - defineSize))
  181.                 return -EFAULT; // according to hw2...
  182.             dData->writeENC = dData->writeENC + size; // Advancing
  183.         }
  184.     }
  185.     else
  186.     {
  187.         b_ytes = dData->readENC - dData->writep;
  188.         if (size > b_ytes)
  189.             return -EINVAL;
  190.         if (copy_from_user(dData->writeENC, buf, size))
  191.             return -EFAULT;
  192.         dData->writeENC += size;
  193.     }
  194.     *fpos = size + *fpos;
  195. }
  196.  
  197. static ssize_t read (struct file* f, char* buf, size_t size, loff_t* fpos)
  198. {
  199.     size_t b_ytes;
  200.     struct dD* dData = file->private_data;
  201.     if (dData->readENC <= dData->writeENC)
  202.         b_ytes = dData->writeENC - dData->readENC;
  203.     else
  204.         b_ytes = memory_size + dData->writeENC - dData->readENC);
  205.     if (b_ytes < size)
  206.         size = b_ytes;
  207.     if(dData->readENC <= dData->writeENC)
  208.     {
  209.         encrypt(dData->readENC, size, dData->key);
  210.         if(copy_to_user(buf, dData->readENC, size))
  211.             return -EFAULT; // according to hw2
  212.         memset(dData->readENC, 0,  size);
  213.         dData->readENC = size + dData->readENC; // now we can advance it after finished..
  214.     }
  215.     else
  216.     {
  217.         if(size <= memory_size + dData->buffer - dData->readENC ))
  218.         {
  219.             encrypt(dData->readENC, size, dData->key);
  220.             if(copy_to_user(buf, dData->readENC, size))
  221.                 return -EFAULT; // according to hw2
  222.             memset(dData->readENC, 0,  size);
  223.             dData->readENC = dData->readENC + size; // now we can advance it.
  224.         }
  225.         else
  226.         {
  227.             size_t theProperSize = memory_size - dData->readENC + dData->buffer);
  228.             encrypt(dData->readENC, theProperSize, dData->key);
  229.             if(copy_to_user(buf, dData->readENC, size))
  230.                 return -EFAULT; // according to hw2..
  231.             memset(dData->readENC, 0,  size);
  232.             dData->readENC = dData->buffer;
  233.             encrypt(dData->readENC, size - theProperSize, dData->key);
  234.             if(copy_to_user(buf, dData->readENC, size))
  235.                 return -EFAULT; // according to hw2..
  236.             memset(dData->readENC, 0,  size);
  237.             dData->readENC = size - theProperSize + dData->readENC; // advancing as demanded...
  238.         }
  239.     }
  240.     *fpos = *fpos + size;
  241.     return size;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement