-
Notifications
You must be signed in to change notification settings - Fork 1
/
cryptctl_driver.c
430 lines (414 loc) · 13.9 KB
/
cryptctl_driver.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <linux/init.h>
#include <linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/uaccess.h>
#include<linux/vmalloc.h>
#include<linux/string.h>
#include<linux/cdev.h>
#include<linux/device.h>
#include<linux/kdev_t.h>
#include "kernel_space.h"
#include "cryptctl_driver.h"
static int encrypt_open(struct inode *, struct file *);
static int create_char_dev(unsigned int ,unsigned int , const char* ,struct cdev*, const struct file_operations* );
static int destroy_char_dev( unsigned int, unsigned int , struct cdev* );
static long char_dev_ctl(struct file *, unsigned int, unsigned long );
static int create_pair(device_record*,const struct file_operations* );
static long encrypt_dev_ctl(struct file *, unsigned int, unsigned long);
static int destroy_pair(device_record*);
static ssize_t encrypt(struct file *, const char __user *, size_t, loff_t*);
static ssize_t decrypt(struct file * , char __user * , size_t , loff_t *);
static int add_alive_id(int);
static int Doomsday(void);
static int does_id_exist(int);
static int driver_major = 0;
static int driver_minor = 0;
static int Pair_Major = 0;
static int open_count = 0;
static char* current_key = NULL;
static dev_t cryptctl_dev = 0;
static struct class* crypt_class;
static struct cdev cryptctl;
static device_record* device_table = NULL;
static int live_ids[DEVICE_RECORDS_SIZE];
MODULE_LICENSE("GPL");
static const struct file_operations fops =
{
.owner = THIS_MODULE,
// .open = encrypt_open,
.unlocked_ioctl = char_dev_ctl //(struct file * open_file, unsigned int request, unsigned long dev_info)
};
static const struct file_operations fops_encrypt =
{
.owner = THIS_MODULE,
.write = encrypt,
.read = decrypt,
.unlocked_ioctl = encrypt_dev_ctl //(struct file * open_file, unsigned int request, unsigned long dev_info)
};
static long encrypt_dev_ctl(struct file* open_file, unsigned int request, unsigned long dev_id)
{
int error_code = 0;
int my_id =0;
copy_from_user((int*) &my_id,(int*) dev_id, sizeof(int));
if (request == 0 || dev_id <0)
{
printk("Process attempting to acess address 0. Don't try to kill the kernel, please!");
return -1;
}
switch(request)
{
case ENCRYPT_DEV_CODE:
{
// printk("ENCRYPT_DEV_CODE case running");
if(does_id_exist(my_id) == DOES_NOT_EXIST)
{
error_code = DOES_NOT_EXIST;
// copy_to_user(&dev_info, (const device_record*) user_dev_info, sizeof(device_record));
copy_to_user((int*)dev_id, &error_code, sizeof(int));
printk("doed not exist: %d\n", error_code);
return -1;
}
copy_to_user((int*)dev_id, &error_code, sizeof(int));
current_key = device_table[my_id].key_stream;
// printk( "current_key: %s", current_key);
// printk("key inside device table: %s", device_table[dev_id].key_stream);
}
break;
default:
break;
}
return 0;
}
static ssize_t encrypt(struct file * user_file, const char __user *user_message, size_t message_size, loff_t* inisde)
{
if(current_key == NULL)
{
printk(KERN_INFO "current_key is not valid");
return -1;
}
char message[message_size+ 1];
memset(message, '\0', message_size+ 1);
copy_from_user(&message,user_message, message_size);
message[message_size] = 0;
// int message_size = strlen(message);
int key_size = strlen(current_key);
char msgcpy[message_size + 1];
memset(msgcpy, '\0', message_size+1);
strcpy(msgcpy, message);
int i = 0;
for (i = 0; i < message_size; i++)
{
message[i] = ((msgcpy[i] + (current_key[i%key_size]-64))%95)+32;
}
copy_to_user(user_message, &message, strlen(message) + 1);
printk("encryption is done!");
return 0;
}
static ssize_t decrypt(struct file * user_file, char __user * user_message, size_t message_size, loff_t *inisde)
{
if(current_key == NULL)
{
printk(KERN_INFO "current_key is not valid");
return -1;
}
char message[message_size+ 1];
memset(message, '\0', message_size+ 1);
copy_from_user(&message,user_message, message_size);
message[message_size] = 0;
// int message_size = strlen(message);
int key_size = strlen(current_key);
char msgcpy[message_size];
memset(msgcpy, '\0', message_size+1);
strncpy(msgcpy, message, message_size);
int i = 0;
for (i = 0; i < message_size; i++)
{
message[i] = ( ( msgcpy[i] - current_key[i%key_size]+190)%95 )+(32);
}
copy_to_user(user_message, &message, strlen(message) + 1);
printk("decryption is done!:%s",message );
return 0;
}
static int encrypt_open(struct inode * file_data, struct file * open_file)
{
return 0;
}
static long char_dev_ctl(struct file * open_file, unsigned int request, unsigned long dev_info)
{
printk("kernel control running");
if (request == 0 || dev_info == 0)
{
printk("Process attempting to acess address 0. Don't try to kill the kernel, please!");
return -1;
}
device_record user_dev_info;
printk("sizeof:%d\n",sizeof(device_record) );
copy_from_user( &user_dev_info,(const device_record*) dev_info, sizeof(device_record) );
printk("copy_from_user: %s", user_dev_info.decrypt_name);
switch(request)
{
case CREATE_DEV_CODE:
{
if(does_id_exist(user_dev_info.device_id) == DOES_EXIST)
{
user_dev_info.error_code = DOES_EXIST;
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
return -1;
}
create_pair(&user_dev_info, &fops_encrypt);
user_dev_info.error_code = 0;
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
return 0;
}
break;
case DESTROY_DEV_CODE:
if(does_id_exist(user_dev_info.device_id) == DOES_NOT_EXIST)
{
user_dev_info.error_code = DOES_NOT_EXIST;
// copy_to_user(&dev_info, (const device_record*) user_dev_info, sizeof(device_record));
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
return -1;
}
destroy_pair(&user_dev_info);
user_dev_info.error_code = 0;
// copy_to_user(&dev_info, (const device_record*) user_dev_info, sizeof(device_record));
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
break;
case CHANGE_KEY_DEV_CODE:
{
if(does_id_exist(user_dev_info.device_id) == DOES_NOT_EXIST)
{
user_dev_info.error_code = DOES_NOT_EXIST;
// copy_to_user(&dev_info, (const device_record*) user_dev_info, sizeof(device_record));
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
return -1;
}
}
memcpy(device_table[user_dev_info.device_id].key_stream, user_dev_info.key_stream, 32);
user_dev_info.error_code = 0;
// copy_to_user(&dev_info, (const device_record*) user_dev_info, sizeof(device_record));
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
printk("key for this device: %s", (device_table[user_dev_info.device_id].key_stream));
break;
case DOOM_DEV_CODE:
Doomsday();
break;
case RENAME_DEV_CODE:
{
int old_id;;
old_id = user_dev_info.old_device_id;
int new_id;;
new_id = user_dev_info.device_id;
if(does_id_exist(new_id) == DOES_EXIST)
{
user_dev_info.error_code = DOES_EXIST;
// copy_to_user(&dev_info, (const device_record*) user_dev_info, sizeof(device_record));
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
return -1;
}
printk(KERN_INFO "old id:%d, new_id:%d", old_id, new_id);
destroy_pair(&device_table[old_id]);
strcpy(user_dev_info.key_stream, device_table[old_id].key_stream );
create_pair(&user_dev_info, &fops_encrypt);
user_dev_info.error_code = 0;
// copy_to_user(dev_info, (const device_record*) user_dev_info, sizeof(device_record));
copy_to_user((const device_record*) dev_info, &user_dev_info, sizeof(device_record));
return 0;
break;
}
default:
printk("Invalid request for cryptctl");
break;
}
printk("ioctl is done!");
return 0;
}
static int does_id_exist(int id)
{
int i = 0;
printk("id passed to exist: %d\n", id);
while(i<DEVICE_RECORDS_SIZE)
{
printk("current value for does_id_exist: %d\n", live_ids[i]);
if(live_ids[i] == -1)
{
return DOES_NOT_EXIST;
}
if(live_ids[i] == id )
{
// printk()
return DOES_EXIST;
}
i++;
}
return TABLE_CAPACITY_OVERFLOW;
}
static void init_device_table(void)
{
int i = 0;
device_table = (device_record*) vmalloc(sizeof(device_record) * DEVICE_RECORDS_SIZE);
if (device_table == NULL)
{
printk("vamalloc() messed up, sorry.");
return -1;;
}
int minor = 1;
while(i<DEVICE_RECORDS_SIZE)
{
device_table[i].device_id = i;
device_table[i].major = driver_major;
device_table[i].encrypt_minor = minor;
device_table[i].decrypt_minor = minor +1;
device_table[i].free = 1;
printk(KERN_INFO"minors: (%d,%d)",device_table[i].encrypt_minor,device_table[i].decrypt_minor );
minor += 2;
i++;
}
return;
}
int init_module(void)
{
init_device_table();
memset(live_ids, -1, DEVICE_RECORDS_SIZE);
// printk("init alive_id:%d", alive_ids[0]);
// printk("New version");
if(alloc_chrdev_region(&cryptctl_dev,driver_major , DEVICE_RECORDS_SIZE * 2, CRYPTCTL_NAME)<0) //this writes the device to /proc/devices
{
printk("There was an error registering this device\n");
return -1;
}
cdev_init(&cryptctl,&fops);
printk("cdev_init");
if(cdev_add(&cryptctl, cryptctl_dev, 1)<0)
{
printk(KERN_INFO"There was a problem adding this device :(");
return -1;
}
printk("cdev_add");
if( (crypt_class = class_create(THIS_MODULE, "crypt_class")) == NULL)
{
printk(KERN_INFO "There was a problem creating the class");
return -1;
}
// class->dev_groups = attr_groups;
if ((device_create(crypt_class, NULL, cryptctl_dev, NULL, "cryptctl")) == NULL) //adds the device file to /dev
{
printk(KERN_INFO "There was a problem creating the device ");
return -1;
}
printk(KERN_INFO"I think the device should be registered now:%d, %d\n", MAJOR(cryptctl_dev), MINOR(cryptctl_dev ));
driver_major = MAJOR(cryptctl_dev);
driver_minor = MINOR(cryptctl_dev);
return 0;
}
void cleanup_module(void)
{
// sysfs_remove_files(crypt_class->dev_kobj, &(crypt_class->class_attrs->attr));
cdev_del(&cryptctl );
device_destroy(crypt_class,cryptctl_dev);
unregister_chrdev_region(cryptctl_dev, DEVICE_RECORDS_SIZE * 2 );
unregister_chrdev(driver_major,CRYPTCTL_NAME);
class_unregister(crypt_class);
class_destroy(crypt_class);
Doomsday();
vfree(device_table);
printk("Device was unregustered. See ya later alligator.");
return 0;
}
static int create_pair(device_record* pair_info,const struct file_operations* fops )
{
int dev_id = pair_info->device_id;
add_alive_id(dev_id);
memcpy(&(device_table[dev_id].encrypt_name), pair_info->encrypt_name, 16);
memcpy(&(device_table[dev_id].decrypt_name), pair_info->decrypt_name, 16);
memcpy(&(device_table[dev_id].key_stream), pair_info->key_stream, 32);
device_table[dev_id].major = driver_major;
create_char_dev(MAJOR(cryptctl_dev),device_table[dev_id].encrypt_minor,device_table[dev_id].encrypt_name ,&(device_table[dev_id].encrypt_device), fops );
create_char_dev(MAJOR(cryptctl_dev),device_table[dev_id].decrypt_minor, device_table[dev_id].decrypt_name,&(device_table[dev_id].decrypt_device), fops );
device_table[dev_id].free = 0;
printk("pair:(major: %d,encrypt_minor: %d,decrypt_minor %d,encrypt_name: %s,decrypt_name: %s, key_stream:%s)",device_table[dev_id].major,device_table[dev_id].encrypt_minor,device_table[dev_id].decrypt_minor , device_table[dev_id].encrypt_name,device_table[dev_id].decrypt_name,device_table[dev_id].key_stream );
return 0;
}
static int add_alive_id(int id)
{
int i = 0;
while(i<DEVICE_RECORDS_SIZE)
{
if(live_ids[i] == -1 )
{
live_ids[i] = id;
return 0;
}
i++;
}
return -1;
}
static int remove_live_id(int id)
{
int i =0;
while(i<DEVICE_RECORDS_SIZE)
{
if(live_ids[i] == id )
{
live_ids[i] = -1;
return 0;
}
i++;
}
return -1;
}
static int destroy_pair(device_record* pair_info)
{
int dev_id = pair_info->device_id;
printk(KERN_INFO "id passed: %d", dev_id);
device_table[dev_id].free = 1;
destroy_char_dev(device_table[dev_id].major, device_table[dev_id].encrypt_minor, &(device_table[dev_id].encrypt_device));
destroy_char_dev(device_table[dev_id].major, device_table[dev_id].decrypt_minor, &(device_table[dev_id].decrypt_device));
return 0;
}
static int Doomsday()
{
int i =0;
while(i<DEVICE_RECORDS_SIZE)
{
if(live_ids[i] == -1)
{
return 0;
}
destroy_pair(&device_table[live_ids[i]] );
live_ids[i] = -1;
i++;
}
return 0;
}
static int create_char_dev(unsigned int major,unsigned int minor , const char* dev_name,struct cdev* new_device, const struct file_operations* fops)
{
// struct cdev new_device;
dev_t new_dev = MKDEV(major,minor);
cdev_init(new_device, fops);
printk("minor passed: %d\n", minor);
if( cdev_add(new_device,new_dev, 1 ) < 0)
{
printk(KERN_INFO "There was a problem with cdev_init pair#2");
return -1;
}
if ( (device_create(crypt_class, NULL, new_dev, NULL, dev_name)) == NULL) //adds the device file to /dev
{
printk(KERN_INFO "There was a problem creating the device ");
return -1;
}
printk("New Registered device:(%s, %d,%d)", dev_name, major, minor);
return 0;
}
static int destroy_char_dev( unsigned int major, unsigned int minor, struct cdev* device)
{
cdev_del(device );
device_destroy(crypt_class, MKDEV(major,minor));
// unregister_chrdev_region(MKDEV(major, minor), 1);
printk(KERN_INFO "Device was successfully destroyed");
return 0;
}
//module_init(char_dev_ctl);
//module_exit(hello_exit);
//printf("Hi Kernel\n");