-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfips_integrity.c
executable file
·308 lines (240 loc) · 6.93 KB
/
fips_integrity.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
/*
* Perform FIPS Integrity test on Kernel Crypto API
*
* At build time, hmac(sha256) of crypto code, avaiable in different ELF sections
* of vmlinux file, is generated. vmlinux file is updated with built-time hmac
* in a read-only data variable, so that it is available at run-time
*
* At run time, hmac(sha256) is again calculated using crypto bytes of a running
* kernel.
* Run time hmac is compared to built time hmac to verify the integrity.
*
*
* Author : Rohit Kothari ([email protected])
* Date : 11 Feb 2014
*
* Copyright (c) 2014 Samsung Electronics
*
*/
#include <linux/crypto.h>
#include <linux/kallsyms.h>
#include <linux/err.h>
#include <linux/scatterlist.h>
#include "internal.h" /* For Functional test macros */
static const char *
symtab[][3] = {{".text", "first_crypto_text", "last_crypto_text" },
{".rodata", "first_crypto_rodata", "last_crypto_rodata"},
{".init.text", "first_crypto_init", "last_crypto_init" },
{"asm.text", "first_crypto_asm_text", "last_crypto_asm_text" },
{"asm.rodata", "first_crypto_asm_rodata", "last_crypto_asm_rodata"},
{"asm.init.text", "first_crypto_asm_init", "last_crypto_asm_init" }};
extern const char * get_builtime_crypto_hmac(void);
#ifdef FIPS_DEBUG
static int
dump_bytes(const char * section_name, const char * first_symbol, const char * last_symbol)
{
u8 * start_addr = (u8 *) kallsyms_lookup_name (first_symbol);
u8 * end_addr = (u8 *) kallsyms_lookup_name (last_symbol);
if (!start_addr || !end_addr || start_addr >= end_addr)
{
printk(KERN_ERR "FIPS(%s): Error Invalid Addresses in Section : %s, Start_Addr : %p , End_Addr : %p",
__FUNCTION__,section_name, start_addr, end_addr);
return -1;
}
printk(KERN_INFO "FIPS CRYPTO RUNTIME : Section - %s, %s : %p, %s : %p \n", section_name, first_symbol, start_addr, last_symbol, end_addr);
print_hex_dump_bytes ("FIPS CRYPTO RUNTIME : ",DUMP_PREFIX_NONE, start_addr, end_addr - start_addr);
return 0;
}
#endif
static int
query_symbol_addresses (const char * first_symbol, const char * last_symbol,
unsigned long * start_addr,unsigned long * end_addr)
{
unsigned long start = kallsyms_lookup_name (first_symbol);
unsigned long end = kallsyms_lookup_name (last_symbol);
#ifdef FIPS_DEBUG
printk(KERN_INFO "FIPS CRYPTO RUNTIME : %s : %p, %s : %p\n", first_symbol, (u8*)start, last_symbol, (u8*)end);
#endif
if (!start || !end || start >= end)
{
printk(KERN_ERR "FIPS(%s): Error Invalid Addresses.", __FUNCTION__);
return -1;
}
*start_addr = start;
*end_addr = end;
return 0;
}
static int
init_hash (struct hash_desc * desc)
{
struct crypto_hash * tfm = NULL;
int ret = -1;
/* Same as build time */
const unsigned char * key = "The quick brown fox jumps over the lazy dog";
tfm = crypto_alloc_hash ("hmac(sha256)", 0, 0);
if (IS_ERR(tfm)) {
printk(KERN_ERR "FIPS(%s): integ failed to allocate tfm %ld", __FUNCTION__, PTR_ERR(tfm));
return -1;
}
ret = crypto_hash_setkey (tfm, key, strlen(key));
if (ret) {
printk(KERN_ERR "FIPS(%s): fail at crypto_hash_setkey", __FUNCTION__);
return -1;
}
desc->tfm = tfm;
desc->flags = 0;
ret = crypto_hash_init (desc);
if (ret) {
printk(KERN_ERR "FIPS(%s): fail at crypto_hash_init", __FUNCTION__);
return -1;
}
return 0;
}
static int
finalize_hash (struct hash_desc *desc, unsigned char * out, unsigned int out_size)
{
int ret = -1;
if (!desc || !desc->tfm || !out || !out_size)
{
printk(KERN_ERR "FIPS(%s): Invalid args", __FUNCTION__);
return ret;
}
if (crypto_hash_digestsize(desc->tfm) > out_size)
{
printk(KERN_ERR "FIPS(%s): Not enough space for digest", __FUNCTION__);
return ret;
}
ret = crypto_hash_final (desc, out);
if (ret)
{
printk(KERN_ERR "FIPS(%s): crypto_hash_final failed", __FUNCTION__);
return -1;
}
return 0;
}
static int
update_hash (struct hash_desc * desc, unsigned char * start_addr, unsigned int size)
{
struct scatterlist sg;
unsigned char * buf = NULL;
unsigned char * cur = NULL;
unsigned int bytes_remaining;
unsigned int bytes;
int ret = -1;
#if FIPS_FUNC_TEST == 2
static int total = 0;
#endif
buf = kmalloc (PAGE_SIZE, GFP_KERNEL);
if (!buf)
{
printk(KERN_ERR "FIPS(%s): kmalloc failed", __FUNCTION__);
return ret;
}
bytes_remaining = size;
cur = start_addr;
while (bytes_remaining > 0)
{
if (bytes_remaining >= PAGE_SIZE)
bytes = PAGE_SIZE;
else
bytes = bytes_remaining;
memcpy (buf, cur, bytes);
sg_init_one (&sg, buf, bytes);
#if FIPS_FUNC_TEST == 2
if (total == 0)
{
printk(KERN_INFO "FIPS : Failing Integrity Test");
buf[bytes / 2] += 1;
}
#endif
ret = crypto_hash_update (desc, &sg, bytes);
if (ret)
{
printk(KERN_ERR "FIPS(%s): crypto_hash_update failed", __FUNCTION__);
kfree(buf);
buf = 0;
return -1;
}
cur += bytes;
bytes_remaining -= bytes;
#if FIPS_FUNC_TEST == 2
total += bytes;
#endif
}
//printk(KERN_INFO "FIPS : total bytes = %d\n", total);
if (buf)
{
kfree(buf);
buf = 0;
}
return 0;
}
int
do_integrity_check (void)
{
int i,rows, err;
unsigned long start_addr = 0;
unsigned long end_addr = 0;
unsigned char runtime_hmac[32];
struct hash_desc desc;
const char * builtime_hmac = 0;
unsigned int size = 0;
err = init_hash (&desc);
if (err)
{
printk (KERN_ERR "FIPS(%s): init_hash failed", __FUNCTION__);
return -1;
}
rows = (unsigned int) sizeof (symtab) / sizeof (symtab[0]);
for (i = 0; i < rows; i++)
{
err = query_symbol_addresses (symtab[i][1], symtab[i][2], &start_addr, &end_addr);
if (err)
{
printk (KERN_ERR "FIPS(%s): Error to get start / end addresses", __FUNCTION__);
crypto_free_hash (desc.tfm);
return -1;
}
#ifdef FIPS_DEBUG
dump_bytes(symtab[i][0], symtab[i][1], symtab[i][2]);
#endif
size = end_addr - start_addr;
err = update_hash (&desc, (unsigned char *)start_addr, size);
if (err)
{
printk (KERN_ERR "FIPS(%s): Error to update hash", __FUNCTION__);
crypto_free_hash (desc.tfm);
return -1;
}
}
err = finalize_hash (&desc, runtime_hmac, sizeof(runtime_hmac));
crypto_free_hash (desc.tfm);
if (err)
{
printk (KERN_ERR "FIPS(%s): Error in finalize", __FUNCTION__);
return -1;
}
builtime_hmac = get_builtime_crypto_hmac();
if (!builtime_hmac)
{
printk (KERN_ERR "FIPS(%s): Unable to retrieve builtime_hmac", __FUNCTION__);
return -1;
}
#ifdef FIPS_DEBUG
print_hex_dump_bytes ("FIPS CRYPTO RUNTIME : runtime hmac = ",DUMP_PREFIX_NONE, runtime_hmac, sizeof(runtime_hmac));
print_hex_dump_bytes ("FIPS CRYPTO RUNTIME : builtime_hmac = ",DUMP_PREFIX_NONE, builtime_hmac , sizeof(runtime_hmac));
#endif
if (!memcmp (builtime_hmac, runtime_hmac, sizeof(runtime_hmac)))
{
printk (KERN_INFO "FIPS: Integrity Check Passed");
return 0;
}
else
{
printk (KERN_ERR "FIPS(%s): Integrity Check Failed", __FUNCTION__);
set_in_fips_err();
return -1;
}
return -1;
}
EXPORT_SYMBOL_GPL(do_integrity_check);