-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmd5.c
84 lines (67 loc) · 1.8 KB
/
md5.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
//*****************************************************************************
// CSE-506 Operating Systems - Spring 2013
//
// Home Work Assignment 3
//
// Author - Rishi Josan
// SBU ID # - 108996773
// Version - 1.0
// Date - 7 April 2013
//
//*****************************************************************************
#include "wrapfs.h"
#include <linux/crypto.h>
#include <crypto/hash.h>
#include <linux/scatterlist.h>
/* This function reads a file in chunks of size PAGE_SIZE and
* incrementally computes its MD5 value */
/* Adapted from HW1 solution */
int wrapfs_compute_md5(u8 * integ_val, char *key, int keySize)
{
int ret = 0, err = 0;
mm_segment_t oldfs;
struct crypto_hash *crypt_hash;
struct hash_desc desc;
struct scatterlist sg;
/* Setting crypto hash to MD5 for computation of MD5 */
crypt_hash = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
if (IS_ERR(crypt_hash)) {
printk(KERN_ERR "Unable to allocate struct crypto_hash");
err = -EPERM;
goto out_md5;
}
desc.tfm = crypt_hash;
desc.flags = 0;
/* Initialization for crypto API */
if (crypto_hash_init(&desc) < 0) {
printk(KERN_ERR "Crypto_hash_init() failed\n");
crypto_free_hash(crypt_hash);
err = -EPERM;
goto out_md5;
}
oldfs = get_fs();
set_fs(KERNEL_DS);
/* Passing read buffer to crypto API */
sg_init_one(&sg, key, keySize);
/* Updating the MD5 Hash value */
ret = crypto_hash_update(&desc, &sg, keySize);
if (ret < 0) {
printk(KERN_ERR "Crypto_hash_update() failed for\n");
crypto_free_hash(crypt_hash);
err = -EPERM;
goto out_md5;
}
set_fs(oldfs);
ret = crypto_hash_final(&desc, integ_val);
if (ret < 0) {
printk(KERN_ERR "Crypto_hash_final() failed\n");
crypto_free_hash(crypt_hash);
err = -EPERM;
goto out_md5;
}
out_md5:
if (err < 0)
return err;
else
return 0;
}