Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
4.5 kB
1
Indexable
Never
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/uaccess.h>

#define BUF_SIZE 40

static int major_number;
static int minor_encrypt = 0;
static int minor_decrypt = 1;
static int mod_param = 3;

module_param(mod_param, int, 0644);

static char encrypt_buffer[BUF_SIZE];

static struct cdev my_cdev;
static struct class *my_class = NULL;
static struct device *my_device = NULL;

static int my_open(struct inode *inode, struct file *file)
{
    // Implement open operation
    return 0;
}

static int my_release(struct inode *inode, struct file *file)
{
    // Implement release operation
    return 0;
}

static ssize_t my_read(struct file *file, char __user *user_buffer, size_t count, loff_t *offset)
{
    // Implement read operation
    int bytes_to_copy = min(count, BUF_SIZE);

    if (copy_to_user(user_buffer, encrypt_buffer, bytes_to_copy))
        return -EFAULT;

    return bytes_to_copy;
}

static ssize_t my_write(struct file *file, const char __user *user_buffer, size_t count, loff_t *offset)
{
    // Implement write operation
    int bytes_to_copy = min(count, BUF_SIZE);

    if (copy_from_user(encrypt_buffer, user_buffer, bytes_to_copy))
        return -EFAULT;

    // Encrypt the buffer based on mod_param
    int i;
    for (i = 0; i < bytes_to_copy; ++i)
        encrypt_buffer[i] += mod_param;

    return bytes_to_copy;
}

static struct file_operations my_fops = {
    .owner = THIS_MODULE,
    .open = my_open,
    .release = my_release,
    .read = my_read,
    .write = my_write,
};

static int __init my_init(void)
{
    // Dynamically allocate a major number
    if ((major_number = register_chrdev(0, "my_module", &my_fops)) < 0)
    {
        printk(KERN_ALERT "Failed to register a major number\n");
        return major_number;
    }

    // Create a class
    my_class = class_create(THIS_MODULE, "my_class");
    if (IS_ERR(my_class))
    {
        unregister_chrdev(major_number, "my_module");
        printk(KERN_ALERT "Failed to register device class\n");
        return PTR_ERR(my_class);
    }

    // Create a device
    my_device = device_create(my_class, NULL, MKDEV(major_number, minor_encrypt), NULL, "encrypt");
    if (IS_ERR(my_device))
    {
        class_destroy(my_class);
        unregister_chrdev(major_number, "my_module");
        printk(KERN_ALERT "Failed to create the device\n");
        return PTR_ERR(my_device);
    }

    // Initialize the character device
    cdev_init(&my_cdev, &my_fops);

    // Add the character device to the kernel
    if (cdev_add(&my_cdev, MKDEV(major_number, minor_encrypt), 1) < 0)
    {
        device_destroy(my_class, MKDEV(major_number, minor_encrypt));
        class_destroy(my_class);
        unregister_chrdev(major_number, "my_module");
        printk(KERN_ALERT "Failed to add the device to the kernel\n");
        return -1;
    }

    // Create /dev/decrypt node
    my_device = device_create(my_class, NULL, MKDEV(major_number, minor_decrypt), NULL, "decrypt");
    if (IS_ERR(my_device))
    {
        class_destroy(my_class);
        unregister_chrdev(major_number, "my_module");
        printk(KERN_ALERT "Failed to create the device\n");
        return PTR_ERR(my_device);
    }

    // Initialize the character device for /dev/decrypt
    cdev_init(&my_cdev, &my_fops);

    // Add the character device to the kernel for /dev/decrypt
    if (cdev_add(&my_cdev, MKDEV(major_number, minor_decrypt), 1) < 0)
    {
        device_destroy(my_class, MKDEV(major_number, minor_decrypt));
        class_destroy(my_class);
        unregister_chrdev(major_number, "my_module");
        printk(KERN_ALERT "Failed to add the device to the kernel\n");
        return -1;
    }

    printk(KERN_INFO "Module loaded with major number %d\n", major_number);
    return 0;
}

static void __exit my_exit(void)
{
    // Clean up the module
    device_destroy(my_class, MKDEV(major_number, minor_encrypt));
    device_destroy(my_class, MKDEV(major_number, minor_decrypt));
    class_unregister(my_class);
    class_destroy(my_class);
    unregister_chrdev(major_number, "my_module");

    printk(KERN_INFO "Module unloaded\n");
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Simple character device driver for encryption/decryption");
Leave a Comment