2 * Utility functions called from fips_crypto_hmac.sh.
4 * executed during Kernel build
7 * Author : Rohit Kothari (r.kothari@samsung.com)
10 * Copyright (c) 2014 Samsung Electronics
17 int main (int argc
, char **argv
)
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");
28 if (!strcmp ("-u", argv
[1]))
30 unsigned long offset
= 0;
31 unsigned char * vmlinux_file
= NULL
;
32 unsigned char * hmac_file
= NULL
;
36 printf ("\nUsage : \n");
37 printf ("fips_crypto_utils -u vmlinux_file hmac_file offset");
42 vmlinux_file
= argv
[2];
44 offset
= atol(argv
[4]);
46 if (!vmlinux_file
|| !hmac_file
|| !offset
)
48 printf ("./fips_crypto_utils -u vmlinux_file hmac_file offset");
52 return update_crypto_hmac (vmlinux_file
, hmac_file
, offset
);
54 else if (!strcmp ("-g", argv
[1]))
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
;
64 printf ("\nUsage : \n");
65 printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file");
71 section_name
= argv
[3];
72 offset
= atol(argv
[4]);
76 if (!in_file
|| !section_name
|| !offset
|| !size
|| !out_file
)
78 printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file");
82 return collect_crypto_bytes (in_file
, section_name
, offset
, size
, out_file
);
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");
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
105 * Returns 0, if success
110 collect_crypto_bytes (const char * in_file
, const char * section_name
, unsigned long offset
,
111 unsigned long size
, const char * out_file
)
114 FILE * out_fp
= NULL
;
116 unsigned char data
= 0;
118 if (!in_file
|| !section_name
|| !offset
|| !size
|| !out_file
)
120 printf ("collect_crypto_bytes : Invalid arguments");
124 printf ("Section : %s\n", section_name
);
126 in_fp
= fopen (in_file
, "r");
129 printf ("Unable to open file : %s", in_file
);
133 if (fseek (in_fp
, offset
, SEEK_SET
) != 0 )
135 printf ("Unable to seek file : %s", in_file
);
140 out_fp
= fopen (out_file
, "ab");
143 printf ("Unable to open file : %s", out_file
);
148 for (i
= 1; i
<= size
; i
++)
150 if ( 1 != fread (&data
, sizeof(unsigned char), 1, in_fp
))
152 printf ("Unable to read 1 byte from file : %s", in_file
);
158 printf ("%02x ", data
);
160 if (1 != fwrite (&data
, 1, 1, out_fp
))
162 printf ("Unable to write 1 byte to file : %s", out_file
);
179 #define SHA256_DIGEST_SIZE 32
182 * Given a vmlinux file, overwrites bytes at given offset with hmac bytes, available in
184 * Return 0, if Success
188 update_crypto_hmac (const char * vmlinux_path
, const char * hmac_path
, unsigned long offset
)
190 FILE * vmlinux_fp
= NULL
;
191 FILE * hmac_fp
= NULL
;
193 unsigned char hmac
[SHA256_DIGEST_SIZE
];
195 if (!vmlinux_path
|| !hmac_path
|| !offset
)
197 printf ("FIPS update_crypto_hmac : Invalid Params");
201 vmlinux_fp
= fopen (vmlinux_path
, "r+b");
204 printf ("Unable to open vmlinux file ");
208 hmac_fp
= fopen (hmac_path
, "rb");
212 printf ("Unable to open hmac file ");
217 if (SHA256_DIGEST_SIZE
!= fread (&hmac
, sizeof(unsigned char), SHA256_DIGEST_SIZE
, hmac_fp
))
219 printf ("Unable to read %d bytes from hmac file", SHA256_DIGEST_SIZE
);
227 for (i
= 0; i
< sizeof(hmac
); i
++)
228 printf ("%02x ", hmac
[i
]);
231 printf ("Offset : %ld", offset
);
234 if (fseek (vmlinux_fp
, offset
, SEEK_SET
) != 0 )
236 printf ("Unable to seek into vmlinux file.");
242 if (SHA256_DIGEST_SIZE
!= fwrite (hmac
, sizeof(unsigned char), SHA256_DIGEST_SIZE
, vmlinux_fp
))
244 printf ("Unable to write %d byte into vmlinux", SHA256_DIGEST_SIZE
);