-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption_device.c
More file actions
189 lines (152 loc) · 5.74 KB
/
Copy pathencryption_device.c
File metadata and controls
189 lines (152 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/crypto.h>
#include <crypto/skcipher.h>
#include <linux/scatterlist.h>
#include <linux/string.h>
#define DEVICE_NAME "encryption_device"
#define CLASS_NAME "encryption_class"
static int major_number;
static struct class* encryption_class = NULL;
static struct device* encryption_device = NULL;
//static char *key = "1234567890123456"; // 16-byte AES key (128-bit)
static u8 key[16] = { '1', '2', '3', '4', '5', '6', '7', '8',
'9', '0', '1', '2', '3', '4', '5', '6' };
#define BUFFER_SIZE 256
static char encrypted_storage[BUFFER_SIZE] = {0};
static int stored_length = 0;
static int dev_open(struct inode *, struct file *);
static int dev_release(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static void encrypt_decrypt_data(const char *input, char *output, bool encrypt);
static struct file_operations fops = {
.open = dev_open,
.read = dev_read,
.write = dev_write,
.release = dev_release,
};
static u8 iv[16] = {0}; // Ensure IV is properly initialized
static void encrypt_decrypt_data(const char *input, char *output, bool encrypt) {
struct scatterlist sg;
struct crypto_skcipher *tfm;
struct skcipher_request *req;
int err;
char *temp_buf;
tfm = crypto_alloc_skcipher("cbc(aes)", 0, 0);
if (IS_ERR(tfm)) {
printk(KERN_ALERT "Failed to allocate crypto transformation\n");
return;
}
err = crypto_skcipher_setkey(tfm, key, sizeof(key));
if (err) {
printk(KERN_ALERT "Failed to set key\n");
crypto_free_skcipher(tfm);
return;
}
req = skcipher_request_alloc(tfm, GFP_KERNEL);
if (!req) {
printk(KERN_ALERT "Failed to allocate skcipher request\n");
crypto_free_skcipher(tfm);
return;
}
// Allocate buffer
temp_buf = kmalloc(strlen(input) + 1, GFP_KERNEL);
if (!temp_buf) {
printk(KERN_ALERT "Failed to allocate buffer\n");
skcipher_request_free(req);
crypto_free_skcipher(tfm);
return;
}
memcpy(iv, "0123456789abcdef", 16); // Use a non-null IV
sg_init_one(&sg, input, strlen(input));
skcipher_request_set_crypt(req, &sg, &sg, strlen(input), iv);
err = encrypt ? crypto_skcipher_encrypt(req) : crypto_skcipher_decrypt(req);
if (err) {
printk(KERN_ALERT "Encryption/Decryption failed: %d\n", err);
} else {
memcpy(temp_buf, sg_virt(&sg), strlen(input)+1);
temp_buf[strlen(input)] = '\0';
strcpy(output, temp_buf);
}
kfree(temp_buf);
skcipher_request_free(req);
crypto_free_skcipher(tfm);
}
static int dev_open(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "Encryption device opened\n");
return 0;
}
static ssize_t dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset) {
if (*offset > 0 || stored_length == 0) {
return 0; // Stop infinite reading
}
if (copy_to_user(buffer, encrypted_storage, stored_length) != 0) {
printk(KERN_ALERT "Failed to send encrypted data to user space\n");
return -EFAULT;
}
*offset += stored_length;
printk(KERN_INFO "Sent encrypted file data to user\n");
return stored_length;
}
static ssize_t dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) {
if (len > BUFFER_SIZE - 1)
len = BUFFER_SIZE - 1; // Prevent overflow
if (copy_from_user(encrypted_storage, buffer, len) != 0) {
printk(KERN_ALERT "Failed to receive data from user space\n");
return -EFAULT;
}
encrypted_storage[len] = '\0';
stored_length = len;
char temp_encrypted[BUFFER_SIZE] = {0};
encrypt_decrypt_data(encrypted_storage, temp_encrypted, true);
strcpy(encrypted_storage, temp_encrypted);
printk(KERN_INFO "Encrypted and stored file data\n");
return len;
}
static int dev_release(struct inode *inodep, struct file *filep) {
printk(KERN_INFO "Encryption device closed\n");
return 0;
}
static int __init encryption_init(void) {
printk(KERN_INFO "Initializing the Encryption Module\n");
major_number = register_chrdev(0, DEVICE_NAME, &fops);
if (major_number < 0) {
printk(KERN_ALERT "Failed to register a major number\n");
return major_number;
}
printk(KERN_INFO "Registered correctly with major number %d\n", major_number);
encryption_class = class_create(CLASS_NAME);
if (IS_ERR(encryption_class)) {
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "Failed to register device class\n");
return PTR_ERR(encryption_class);
}
printk(KERN_INFO "Device class registered correctly\n");
encryption_device = device_create(encryption_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
if (IS_ERR(encryption_device)) {
class_destroy(encryption_class);
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_ALERT "Failed to create the device\n");
return PTR_ERR(encryption_device);
}
printk(KERN_INFO "Device created correctly\n");
return 0;
}
static void __exit encryption_exit(void) {
device_destroy(encryption_class, MKDEV(major_number, 0));
class_unregister(encryption_class);
class_destroy(encryption_class);
unregister_chrdev(major_number, DEVICE_NAME);
printk(KERN_INFO "Encryption Module unloaded\n");
}
module_init(encryption_init);
module_exit(encryption_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A Linux char device for encryption and decryption");
MODULE_VERSION("0.1");