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
|
/*
* Utility functions called from fips_fmp_hmac.sh.
*
* executed during Kernel build
*
* Copyright (c) 2014 Samsung Electronics
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 0x100000
int update_fmp_hmac(const char *vmlinux_path, const char *hmac_path, unsigned long offset);
int collect_fmp_bytes(const char *in_file, const char *section_name, unsigned long offset,
unsigned long size, const char *out_file);
int main(int argc, char **argv)
{
if (argc < 2) {
printf("\nUsage : \n");
printf("fips_fmp_utils -u vmlinux_file hmac_file offset");
printf("fips_fmp_utils -g vmlinux_file section_name offset size out_file");
printf("\n");
return -1;
}
if (!strncmp("-u", argv[1], sizeof("-u"))) {
unsigned long offset = 0;
unsigned char *vmlinux_file = NULL;
unsigned char *hmac_file = NULL;
if (argc != 5) {
printf("\nUsage : \n");
printf("fips_fmp_utils -u vmlinux_file hmac_file offset");
printf("\n");
return -1;
}
vmlinux_file = argv[2];
hmac_file = argv[3];
offset = atol(argv[4]);
if (!vmlinux_file || !hmac_file || !offset) {
printf ("./fips_fmp_utils -u vmlinux_file hmac_file offset");
return -1;
}
return update_fmp_hmac(vmlinux_file, hmac_file, offset);
} else if (!strncmp("-g", argv[1], sizeof("-g"))) {
const char *in_file = NULL;
const char *section_name = NULL;
unsigned long offset = 0;
unsigned long size = 0;
const char *out_file = NULL;
if (argc != 7) {
printf("\nUsage : \n");
printf("./fips_fmp_utils -g vmlinux_file section_name offset size out_file");
printf("\n");
return -1;
}
in_file = argv[2];
section_name = argv[3];
offset = atol(argv[4]);
size = atol(argv[5]);
out_file = argv[6];
if (!in_file || !section_name || !offset || !size || !out_file) {
printf("./fips_fmp_utils -g vmlinux_file section_name offset size out_file");
return -1;
}
if (size > MAX_SIZE) {
printf("Fail fips_fmp_utils due to invalid size: %ld\n", size);
return -1;
}
return collect_fmp_bytes(in_file, section_name, offset, size, out_file);
} else {
printf("\nUsage : \n");
printf("fips_fmp_utils -u vmlinux_file hmac_file offset");
printf("fips_fmp_utils -g vmlinux_file section_name offset size out_file");
printf("\n");
}
return -1;
}
/*
* Given a vmlinux file, dumps "size" bytes from given "offset" to output file
* in_file : absolute path to vmlinux file
* section_name : Used only for printing / debugging
* offset : offset in file from where to dump bytes
* size : how many bytes to dump
* out_file : Output file, where to dump bytes.
* Open in append mode, to keep previous bytes, if present
* Caller need to clean up before 1st call
*
* Returns 0, if success
* -1, if error
*/
int collect_fmp_bytes(const char *in_file, const char *section_name, unsigned long offset,
unsigned long size, const char *out_file)
{
FILE *in_fp = NULL;
FILE *out_fp = NULL;
unsigned int i = 0;
unsigned char data = 0;
if (!in_file || !section_name || !offset || !size || !out_file) {
printf ("collect_fmp_bytes : Invalid arguments");
return -1;
}
printf("Section : %s\n", section_name);
in_fp = fopen(in_file, "r");
if (!in_fp) {
printf("Unable to open file : %s", in_file);
return -1;
}
if (fseek(in_fp, offset, SEEK_SET) != 0) {
printf ("Unable to seek file : %s", in_file);
goto err_fseek;
}
out_fp = fopen(out_file, "ab");
if (!out_fp) {
printf ("Unable to open file : %s", out_file);
goto err_open_out_fp;
}
if (size > MAX_SIZE) {
printf("Fail fips_fmp_utils due to invalid size: %ld\n", size);
goto err;
}
if (size > MAX_SIZE) {
printf("Fail fips_fmp_utils due to invalid size: %ld\n", size);
return -1;
}
for (i = 1; i <= size; i++) {
if ( 1 != fread (&data, sizeof(unsigned char), 1, in_fp)) {
printf("Unable to read 1 byte from file : %s", in_file);
goto err;
}
printf ("%02x ", data);
if (1 != fwrite (&data, 1, 1, out_fp)) {
printf("Unable to write 1 byte to file : %s", out_file);
goto err;
}
if (!(i % 16))
printf("\n");
}
fclose(out_fp);
fclose(in_fp);
return 0;
err:
fclose(out_fp);
err_open_out_fp:
err_fseek:
fclose(in_fp);
return -1;
}
#define SHA256_DIGEST_SIZE 32
/*
* Given a vmlinux file, overwrites bytes at given offset with hmac bytes, available in
* hmac file.
* Return 0, if Success
* -1, if Error
*/
int update_fmp_hmac(const char *vmlinux_path, const char *hmac_path, unsigned long offset)
{
FILE *vmlinux_fp = NULL;
FILE *hmac_fp = NULL;
int i = 0, j = 0;
unsigned char hmac[SHA256_DIGEST_SIZE];
if (!vmlinux_path || !hmac_path || !offset) {
printf("FIPS update_fmp_hmac : Invalid Params");
return -1;
}
vmlinux_fp = fopen(vmlinux_path, "r+b");
if (!vmlinux_fp) {
printf("Unable to open vmlinux file ");
return -1;
}
hmac_fp = fopen(hmac_path, "rb");
if (!hmac_fp) {
printf("Unable to open hmac file ");
goto err_open_hmac_fp;
}
if (SHA256_DIGEST_SIZE != fread(&hmac, sizeof(unsigned char), SHA256_DIGEST_SIZE, hmac_fp)) {
printf("Unable to read %d bytes from hmac file", SHA256_DIGEST_SIZE);
goto err;
}
if (fseek(vmlinux_fp, offset, SEEK_SET) != 0) {
printf("Unable to seek into vmlinux file.");
goto err;
}
if (SHA256_DIGEST_SIZE != fwrite (hmac, sizeof(unsigned char), SHA256_DIGEST_SIZE, vmlinux_fp)) {
printf("Unable to write %d byte into vmlinux", SHA256_DIGEST_SIZE);
goto err;
}
fclose(vmlinux_fp);
fclose(hmac_fp);
return 0;
err:
fclose(hmac_fp);
err_open_hmac_fp:
fclose(vmlinux_fp);
return -1;
}
|