diff --git a/scripts/fips_crypto_utils.c b/scripts/fips_crypto_utils.c index 120796fc7afd..f8562dac23e5 100644 --- a/scripts/fips_crypto_utils.c +++ b/scripts/fips_crypto_utils.c @@ -4,98 +4,98 @@ * executed during Kernel build * * - * Author : Rohit Kothari (r.kothari@samsung.com) + * Author : Rohit Kothari (r.kothari@samsung.com) * Date : 11 Feb 2014 * * Copyright (c) 2014 Samsung Electronics - * + * */ #include #include - -int main (int argc, char **argv) +#include + +#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) { - if (argc < 2) + 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 ("\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"); + printf ("FIPS update_crypto_hmac : Invalid Params"); return -1; } - if (!strcmp ("-u", argv[1])) + vmlinux_fp = fopen (vmlinux_path, "r+b"); + if (!vmlinux_fp) { - unsigned long offset = 0; - unsigned char * vmlinux_file = NULL; - unsigned char * hmac_file = NULL; + printf ("Unable to open vmlinux file "); + return -1; + } - 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; - } + hmac_fp = fopen (hmac_path, "rb"); - return update_crypto_hmac (vmlinux_file, hmac_file, offset); - } - else if (!strcmp ("-g", argv[1])) + if (!hmac_fp) { - const char * in_file = NULL; - const char * section_name = NULL; - unsigned long offset = 0; - unsigned long size = 0; - const char * out_file = NULL; + printf ("Unable to open hmac file "); + fclose (vmlinux_fp); + return -1; + } - if (argc != 7) - { - printf ("\nUsage : \n"); - printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file"); - printf ("\n"); - 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; + } - in_file = argv[2]; - section_name = argv[3]; - offset = atol(argv[4]); - size = atol(argv[5]); - out_file = argv[6]; +#if 0 + printf ("Hash : "); + for (i = 0; i < sizeof(hmac); i++) + printf ("%02x ", hmac[i]); + printf ("\n"); - if (!in_file || !section_name || !offset || !size || !out_file) - { - printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file"); - return -1; - } + printf ("Offset : %ld", offset); +#endif - return collect_crypto_bytes (in_file, section_name, offset, size, out_file); + if (fseek (vmlinux_fp, offset, SEEK_SET) != 0 ) + { + printf ("Unable to seek into vmlinux file."); + fclose (hmac_fp); + fclose (vmlinux_fp); + return -1; } - else + + if (SHA256_DIGEST_SIZE != fwrite (hmac, sizeof(unsigned char), SHA256_DIGEST_SIZE, vmlinux_fp)) { - 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"); + printf ("Unable to write %d byte into vmlinux", SHA256_DIGEST_SIZE); + fclose (hmac_fp); + fclose (vmlinux_fp); + return -1; } - return -1; + fclose (vmlinux_fp); + fclose (hmac_fp); + + return 0; } /* * 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 + * 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. @@ -106,8 +106,8 @@ int main (int argc, char **argv) * -1, if error */ -int -collect_crypto_bytes (const char * in_file, const char * section_name, unsigned long offset, +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; @@ -128,7 +128,7 @@ collect_crypto_bytes (const char * in_file, const char * section_name, unsigned { printf ("Unable to open file : %s", in_file); return -1; - } + } if (fseek (in_fp, offset, SEEK_SET) != 0 ) { @@ -143,7 +143,7 @@ collect_crypto_bytes (const char * in_file, const char * section_name, unsigned printf ("Unable to open file : %s", out_file); fclose(in_fp); return -1; - } + } for (i = 1; i <= size; i++) { @@ -175,80 +175,85 @@ collect_crypto_bytes (const char * in_file, const char * section_name, unsigned 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) +int main (int argc, char **argv) { - 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) + if (argc < 2) { - printf ("FIPS update_crypto_hmac : Invalid Params"); + 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; } - vmlinux_fp = fopen (vmlinux_path, "r+b"); - if (!vmlinux_fp) + if (!strcmp ("-u", argv[1])) { - printf ("Unable to open vmlinux file "); - return -1; - } + unsigned long offset = 0; + unsigned char * vmlinux_file = NULL; + unsigned char * hmac_file = NULL; - hmac_fp = fopen (hmac_path, "rb"); + 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; + } - if (!hmac_fp) + return update_crypto_hmac (vmlinux_file, hmac_file, offset); + } + else if (!strcmp ("-g", argv[1])) { - printf ("Unable to open hmac file "); - fclose (vmlinux_fp); - return -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 (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 (argc != 7) + { + printf ("\nUsage : \n"); + printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file"); + printf ("\n"); + return -1; + } -#if 0 - printf ("Hash : "); - for (i = 0; i < sizeof(hmac); i++) - printf ("%02x ", hmac[i]); - printf ("\n"); + in_file = argv[2]; + section_name = argv[3]; + offset = atol(argv[4]); + size = atol(argv[5]); + out_file = argv[6]; - printf ("Offset : %ld", offset); -#endif + if (!in_file || !section_name || !offset || !size || !out_file) + { + printf ("./fips_crypto_utils -g vmlinux_file section_name offset size out_file"); + return -1; + } - if (fseek (vmlinux_fp, offset, SEEK_SET) != 0 ) - { - printf ("Unable to seek into vmlinux file."); - fclose (hmac_fp); - fclose (vmlinux_fp); - return -1; + return collect_crypto_bytes (in_file, section_name, offset, size, out_file); } - - if (SHA256_DIGEST_SIZE != fwrite (hmac, sizeof(unsigned char), SHA256_DIGEST_SIZE, vmlinux_fp)) + else { - printf ("Unable to write %d byte into vmlinux", SHA256_DIGEST_SIZE); - fclose (hmac_fp); - fclose (vmlinux_fp); - return -1; + 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"); } - fclose (vmlinux_fp); - fclose (hmac_fp); + return -1; +} + + + + - return 0; -} \ No newline at end of file