phonet: fix building with clang
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / scripts / fips_crypto_utils.c
1 /*
2 * Utility functions called from fips_crypto_hmac.sh.
3 *
4 * executed during Kernel build
5 *
6 *
7 * Author : Rohit Kothari (r.kothari@samsung.com)
8 * Date : 11 Feb 2014
9 *
10 * Copyright (c) 2014 Samsung Electronics
11 *
12 */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 int main (int argc, char **argv)
18 {
19 if (argc < 2)
20 {
21 printf ("\nUsage : \n");
22 printf ("fips_crypto_utils -u vmlinux_file hmac_file offset");
23 printf ("fips_crypto_utils -g vmlinux_file section_name offset size out_file");
24 printf ("\n");
25 return -1;
26 }
27
28 if (!strcmp ("-u", argv[1]))
29 {
30 unsigned long offset = 0;
31 unsigned char * vmlinux_file = NULL;
32 unsigned char * hmac_file = NULL;
33
34 if (argc != 5)
35 {
36 printf ("\nUsage : \n");
37 printf ("fips_crypto_utils -u vmlinux_file hmac_file offset");
38 printf ("\n");
39 return -1;
40 }
41
42 vmlinux_file = argv[2];
43 hmac_file = argv[3];
44 offset = atol(argv[4]);
45
46 if (!vmlinux_file || !hmac_file || !offset)
47 {
48 printf ("./fips_crypto_utils -u vmlinux_file hmac_file offset");
49 return -1;
50 }
51
52 return update_crypto_hmac (vmlinux_file, hmac_file, offset);
53 }
54 else if (!strcmp ("-g", argv[1]))
55 {
56 const char * in_file = NULL;
57 const char * section_name = NULL;
58 unsigned long offset = 0;
59 unsigned long size = 0;
60 const char * out_file = NULL;
61
62 if (argc != 7)
63 {
64 printf ("\nUsage : \n");
65 printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file");
66 printf ("\n");
67 return -1;
68 }
69
70 in_file = argv[2];
71 section_name = argv[3];
72 offset = atol(argv[4]);
73 size = atol(argv[5]);
74 out_file = argv[6];
75
76 if (!in_file || !section_name || !offset || !size || !out_file)
77 {
78 printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file");
79 return -1;
80 }
81
82 return collect_crypto_bytes (in_file, section_name, offset, size, out_file);
83 }
84 else
85 {
86 printf ("\nUsage : \n");
87 printf ("fips_crypto_utils -u vmlinux_file hmac_file offset");
88 printf ("fips_crypto_utils -g vmlinux_file section_name offset size out_file");
89 printf ("\n");
90 }
91
92 return -1;
93 }
94
95 /*
96 * Given a vmlinux file, dumps "size" bytes from given "offset" to output file
97 * in_file : absolute path to vmlinux file
98 * section_name : Used only for printing / debugging
99 * offset : offset in file from where to dump bytes
100 * size : how many bytes to dump
101 * out_file : Output file, where to dump bytes.
102 * Open in append mode, to keep previous bytes, if present
103 * Caller need to clean up before 1st call
104 *
105 * Returns 0, if success
106 * -1, if error
107 */
108
109 int
110 collect_crypto_bytes (const char * in_file, const char * section_name, unsigned long offset,
111 unsigned long size, const char * out_file)
112 {
113 FILE * in_fp = NULL;
114 FILE * out_fp = NULL;
115 unsigned int i = 0;
116 unsigned char data = 0;
117
118 if (!in_file || !section_name || !offset || !size || !out_file)
119 {
120 printf ("collect_crypto_bytes : Invalid arguments");
121 return -1;
122 }
123
124 printf ("Section : %s\n", section_name);
125
126 in_fp = fopen (in_file, "r");
127 if (!in_fp)
128 {
129 printf ("Unable to open file : %s", in_file);
130 return -1;
131 }
132
133 if (fseek (in_fp, offset, SEEK_SET) != 0 )
134 {
135 printf ("Unable to seek file : %s", in_file);
136 fclose (in_fp);
137 return -1;
138 }
139
140 out_fp = fopen (out_file, "ab");
141 if (!out_fp)
142 {
143 printf ("Unable to open file : %s", out_file);
144 fclose(in_fp);
145 return -1;
146 }
147
148 for (i = 1; i <= size; i++)
149 {
150 if ( 1 != fread (&data, sizeof(unsigned char), 1, in_fp))
151 {
152 printf ("Unable to read 1 byte from file : %s", in_file);
153 fclose (in_fp);
154 fclose (out_fp);
155 return -1;
156 }
157
158 printf ("%02x ", data);
159
160 if (1 != fwrite (&data, 1, 1, out_fp))
161 {
162 printf ("Unable to write 1 byte to file : %s", out_file);
163 fclose (in_fp);
164 fclose (out_fp);
165 return -1;
166 }
167
168 if ( !(i % 16))
169 printf ("\n");
170 }
171
172 fclose (in_fp);
173 fclose (out_fp);
174
175 return 0;
176 }
177
178
179 #define SHA256_DIGEST_SIZE 32
180
181 /*
182 * Given a vmlinux file, overwrites bytes at given offset with hmac bytes, available in
183 * hmac file.
184 * Return 0, if Success
185 * -1, if Error
186 */
187 int
188 update_crypto_hmac (const char * vmlinux_path, const char * hmac_path, unsigned long offset)
189 {
190 FILE * vmlinux_fp = NULL;
191 FILE * hmac_fp = NULL;
192 int i = 0, j = 0;
193 unsigned char hmac[SHA256_DIGEST_SIZE];
194
195 if (!vmlinux_path || !hmac_path || !offset)
196 {
197 printf ("FIPS update_crypto_hmac : Invalid Params");
198 return -1;
199 }
200
201 vmlinux_fp = fopen (vmlinux_path, "r+b");
202 if (!vmlinux_fp)
203 {
204 printf ("Unable to open vmlinux file ");
205 return -1;
206 }
207
208 hmac_fp = fopen (hmac_path, "rb");
209
210 if (!hmac_fp)
211 {
212 printf ("Unable to open hmac file ");
213 fclose (vmlinux_fp);
214 return -1;
215 }
216
217 if (SHA256_DIGEST_SIZE != fread (&hmac, sizeof(unsigned char), SHA256_DIGEST_SIZE, hmac_fp))
218 {
219 printf ("Unable to read %d bytes from hmac file", SHA256_DIGEST_SIZE);
220 fclose (hmac_fp);
221 fclose (vmlinux_fp);
222 return -1;
223 }
224
225 #if 0
226 printf ("Hash : ");
227 for (i = 0; i < sizeof(hmac); i++)
228 printf ("%02x ", hmac[i]);
229 printf ("\n");
230
231 printf ("Offset : %ld", offset);
232 #endif
233
234 if (fseek (vmlinux_fp, offset, SEEK_SET) != 0 )
235 {
236 printf ("Unable to seek into vmlinux file.");
237 fclose (hmac_fp);
238 fclose (vmlinux_fp);
239 return -1;
240 }
241
242 if (SHA256_DIGEST_SIZE != fwrite (hmac, sizeof(unsigned char), SHA256_DIGEST_SIZE, vmlinux_fp))
243 {
244 printf ("Unable to write %d byte into vmlinux", SHA256_DIGEST_SIZE);
245 fclose (hmac_fp);
246 fclose (vmlinux_fp);
247 return -1;
248 }
249
250 fclose (vmlinux_fp);
251 fclose (hmac_fp);
252
253 return 0;
254 }