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