/
sha.c
253 lines (215 loc) · 5.9 KB
/
sha.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
#include <linux/crypto.h>
#include <linux/err.h>
#include <linux/scatterlist.h>
#include <linux/semaphore.h>
#include <net/tcp.h>
#include <net/ip.h>
#include <net/sock.h>
#include <linux/percpu.h>
#include "sha.h"
//spinlock_t sha_lock = SPIN_LOCK_UNLOCKED;
//DEFINE_MUTEX(sha_lock);
//DECLARE_MUTEX(hash_mutex);
/* - pool: digest algorithm, hash description and scratch buffer */
struct tcp_sha1sig_pool {
struct hash_desc sha1_desc;
};
static unsigned long tcp_sha1sig_users;
static struct tcp_sha1sig_pool **tcp_sha1sig_pool;
static DEFINE_SPINLOCK(tcp_sha1sig_pool_lock);
static void __tcp_free_sha1sig_pool(struct tcp_sha1sig_pool **pool)
{
int cpu;
for_each_possible_cpu(cpu) {
struct tcp_sha1sig_pool *p = *per_cpu_ptr(pool, cpu);
if (p) {
if (p->sha1_desc.tfm)
crypto_free_hash(p->sha1_desc.tfm);
kfree(p);
p = NULL;
}
}
free_percpu(pool);
}
void tcp_free_sha1sig_pool(void)
{
struct tcp_sha1sig_pool **pool = NULL;
spin_lock_bh(&tcp_sha1sig_pool_lock);
if (--tcp_sha1sig_users == 0) {
pool = tcp_sha1sig_pool;
tcp_sha1sig_pool = NULL;
}
spin_unlock_bh(&tcp_sha1sig_pool_lock);
if (pool)
__tcp_free_sha1sig_pool(pool);
}
struct tcp_sha1sig_pool *__tcp_get_sha1sig_pool(int cpu)
{
struct tcp_sha1sig_pool **p;
spin_lock_bh(&tcp_sha1sig_pool_lock);
p = tcp_sha1sig_pool;
if (p) {
tcp_sha1sig_users++;
}
spin_unlock_bh(&tcp_sha1sig_pool_lock);
return (p ? *per_cpu_ptr(p, cpu) : NULL);
}
struct tcp_sha1sig_pool *tcp_get_sha1sig_pool(void)
{
int cpu = get_cpu();
struct tcp_sha1sig_pool *ret = __tcp_get_sha1sig_pool(cpu);
if (!ret) {
put_cpu();
}
return ret;
}
static struct tcp_sha1sig_pool **__tcp_alloc_sha1sig_pool(void)
{
int cpu;
struct tcp_sha1sig_pool **pool;
pool = alloc_percpu(struct tcp_sha1sig_pool *);
if (!pool)
return NULL;
for_each_possible_cpu(cpu) {
struct tcp_sha1sig_pool *p;
struct crypto_hash *hash;
p = kzalloc(sizeof(*p), GFP_ATOMIC);
if (!p)
goto out_free;
*per_cpu_ptr(pool, cpu) = p;
hash = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
if (!hash || IS_ERR(hash))
goto out_free;
p->sha1_desc.tfm = hash;
}
return pool;
out_free:
__tcp_free_sha1sig_pool(pool);
return NULL;
}
struct tcp_sha1sig_pool **tcp_alloc_sha1sig_pool(void)
{
struct tcp_sha1sig_pool **pool;
int alloc = 0;
retry:
spin_lock_bh(&tcp_sha1sig_pool_lock);
pool = tcp_sha1sig_pool;
if (tcp_sha1sig_users++ == 0) {
alloc = 1;
spin_unlock_bh(&tcp_sha1sig_pool_lock);
} else if (!pool) {
tcp_sha1sig_users--;
spin_unlock_bh(&tcp_sha1sig_pool_lock);
cpu_relax();
goto retry;
} else
spin_unlock_bh(&tcp_sha1sig_pool_lock);
if (alloc) {
/* we cannot hold spinlock here because this may sleep. */
struct tcp_sha1sig_pool **p = __tcp_alloc_sha1sig_pool();
spin_lock_bh(&tcp_sha1sig_pool_lock);
if (!p) {
tcp_sha1sig_users--;
spin_unlock_bh(&tcp_sha1sig_pool_lock);
return NULL;
}
pool = tcp_sha1sig_pool;
if (pool) {
/* oops, it has already been assigned. */
spin_unlock_bh(&tcp_sha1sig_pool_lock);
__tcp_free_sha1sig_pool(p);
} else {
tcp_sha1sig_pool = pool = p;
spin_unlock_bh(&tcp_sha1sig_pool_lock);
}
}
return pool;
}
void __tcp_put_sha1sig_pool(void)
{
tcp_free_sha1sig_pool();
}
static inline void tcp_put_sha1sig_pool(void)
{
__tcp_put_sha1sig_pool();
put_cpu();
}
int tcp_sha1_hash_data(struct tcp_sha1sig_pool *hp, char *src, int len)
{
struct scatterlist sg;
int err;
sg_init_one(&sg, (u8 *)src, len);
err = crypto_hash_update(&hp->sha1_desc, &sg, len);
return err;
}
int tcp_v4_sha1_hash_data(char *sha1_hash, char *src, int len)
{
struct tcp_sha1sig_pool *hp;
struct hash_desc *desc;
hp = tcp_get_sha1sig_pool();
if (!hp)
goto clear_hash_noput;
desc = &hp->sha1_desc;
if (crypto_hash_init(desc))
goto clear_hash;
if (tcp_sha1_hash_data(hp, src, len))
goto clear_hash;
if (crypto_hash_final(desc, sha1_hash))
goto clear_hash;
tcp_put_sha1sig_pool();
return 0;
clear_hash:
tcp_put_sha1sig_pool();
clear_hash_noput:
memset(sha1_hash, 0, 20);
return 1;
}
/**
* ecryptfs_calculate_sha - calculates the SHA-1 of @src
* @dst: Pointer to 20 bytes of allocated memory
* @src: Data to be SHA-1'd
* @len: Length of @src
*
* Uses the allocated crypto context that crypt_stat references to
* generate the MD5 sum of the contents of src.
*/
int ecryptfs_calculate_sha1(char *dst, char *src, int len) {
/*
* http://lxr.oss.org.cn/source/fs/ecryptfs/crypto.c?v=2.6.30
* http://lxr.oss.org.cn/source/net/ipv4/tcp.c?v=2.6.30#L2667
*/
struct scatterlist sg;
struct hash_desc desc;
int rc = 0;
//int i;
//mutex_lock(&sha_lock);
//down(&hash_mutex);
//spin_lock_bh(&sha_lock);
sg_init_one(&sg, (u8 *)src, len);
desc.tfm = crypto_alloc_hash("sha1", 0, CRYPTO_ALG_ASYNC);
desc.flags = 0;
rc = crypto_hash_init(&desc);
if (rc) {
printk(KERN_ERR "%s: Error initializing crypto hash; rc = [%d]\n", __func__, rc);
BUG();
goto out;
}
rc = crypto_hash_update(&desc, &sg, len);
if (rc) {
printk(KERN_ERR "%s: Error updating crypto hash; rc = [%d]\n", __func__, rc);
BUG();
goto out;
}
rc = crypto_hash_final(&desc, dst);
if (rc) {
printk(KERN_ERR "%s: Error finalizing crypto hash; rc = [%d]\n", __func__, rc);
BUG();
goto out;
}
crypto_free_hash(desc.tfm);
out:
//mutex_unlock(&sha_lock);
//up(&hash_mutex);
//spin_unlock_bh(&sha_lock);
return rc;
}