summaryrefstree
path: root/kernel/scripts/fips_crypto_utils.c
blob: 715d2ded23f3084747e489302a7608c59d943b36 (plain)
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
/*
 * Utility functions called from fips_crypto_hmac.sh.
 *
 * executed during Kernel build
 * 
 *
 * Author : Rohit Kothari (r.kothari@samsung.com) 
 * Date	  : 11 Feb 2014
 *
 * Copyright (c) 2014 Samsung Electronics
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int update_crypto_hmac(const char *, const char *, unsigned long);
int collect_crypto_bytes(const char *, const char *, unsigned long, 
			 unsigned long, const char *);

int main (int argc, char **argv)
{
	if (argc < 2)
	{
		printf ("\nUsage : \n");
		printf ("fips_crypto_utils -u vmlinux_file hmac_file offset");
		printf ("fips_crypto_utils -g vmlinux_file section_name offset size out_file");
		printf ("\n");
		return -1;
	}

	if (!strcmp ("-u", argv[1]))
	{
		unsigned long offset = 0;
		unsigned char * vmlinux_file = NULL;
		unsigned char * hmac_file    = NULL;

		if (argc != 5)
		{
			printf ("\nUsage : \n");
			printf ("fips_crypto_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_crypto_utils -u vmlinux_file hmac_file offset");
			return -1;
		}

		return update_crypto_hmac (vmlinux_file, hmac_file, offset);
	}
	else if (!strcmp ("-g", argv[1]))
	{
		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_crypto_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_crypto_utils -g vmlinux_file section_name offset size out_file");
			return -1;
		}

		return collect_crypto_bytes (in_file, section_name, offset, size, out_file);
	}
	else
	{
		printf ("\nUsage : \n");
		printf ("fips_crypto_utils -u vmlinux_file hmac_file offset");
		printf ("fips_crypto_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_crypto_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_crypto_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);
		fclose (in_fp);
		return -1;
	}

	out_fp = fopen (out_file, "ab");
	if (!out_fp)
	{
		printf ("Unable to open file : %s", out_file);
		fclose(in_fp);
		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);
			fclose (in_fp);
			fclose (out_fp);
			return -1;
		}

		printf ("%02x ", data);

		if (1 != fwrite (&data, 1, 1, out_fp))
		{
			printf ("Unable to write 1 byte to file : %s", out_file);
			fclose (in_fp);
			fclose (out_fp);
			return -1;
		}

		if ( !(i % 16))
			printf ("\n");
	}

	fclose (in_fp);
	fclose (out_fp);

	return 0;
}


#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_crypto_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_crypto_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 ");
		fclose (vmlinux_fp);
		return -1;
	}	

	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);
		fclose (hmac_fp);
		fclose (vmlinux_fp);
		return -1;
	}

#if 0
	printf ("Hash : ");
	for (i = 0; i < sizeof(hmac); i++)
		printf ("%02x ", hmac[i]);
	printf ("\n");

	printf ("Offset : %ld", offset);
#endif

	if (fseek (vmlinux_fp, offset, SEEK_SET) != 0 )
	{
		printf ("Unable to seek into vmlinux file.");
		fclose (hmac_fp);
		fclose (vmlinux_fp);
		return -1;
	}

	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);
		fclose (hmac_fp);
		fclose (vmlinux_fp);
		return -1;
	}

	fclose (vmlinux_fp);
	fclose (hmac_fp);

	return 0;
}