commands/digest: add verify support
PATCH 1/1 commands/digest: add verify support
--- commands/digest.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 11 deletions(-)
commands/digest.c +83 -11
@@ -25,24 +25,76 @@#include <xfuncs.h> #include <malloc.h> #include <digest.h> +#include <linux/ctype.h> +#include <getopt.h> + +static inline unsigned char parse_hexchar (char c) +{ +if (!isxdigit (c)) +return 0 ; + +return isdigit (c) ? (c -'0' ) : ((islower (c) ?toupper (c) : c) -'A' +0xA ); +} + +static inline unsigned char parse_hexbyte (const char *p) +{ +return (parse_hexchar (*p) <<4 ) |parse_hexchar (*(p +1 )); +}static int do_digest (char *algorithm,int argc,char *argv[]) {struct digest *d;int ret =0 ;int i; -unsigned char *hash; +unsigned char *hash =0 ; +int opt; +unsigned char *verify_hash =0 ; +int verify =0 ; +int min_argc =2 ; d =digest_get_by_name (algorithm);BUG_ON (!d); -if (argc <2 ) -return COMMAND_ERROR_USAGE ; +while ((opt =getopt (argc, argv,"v:" )) >0 ) { +switch (opt) { +case 'v' : + verify =1 ; + min_argc +=2 ; + +if (d->length != (strlen (optarg) /2 )) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify_hash =calloc (d->length,sizeof (unsigned char )); +if (!verify_hash) { +perror ("calloc" ); + ret = -ENOMEM ; + goto out; + } + +for (i =0 ; i < d->length; i++) + verify_hash[i] =parse_hexbyte (&optarg[i *2 ]); + +break ; +default : + ret =COMMAND_ERROR_USAGE ; + goto out; + } + } + +if (argc < min_argc) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + argv += min_argc -2 ; hash =calloc (d->length,sizeof (unsigned char ));if (!hash) {perror ("calloc" ); -return COMMAND_ERROR_USAGE ; + ret = -ENOMEM ; + goto out; } argv++;
@@ -60,17 +112,33 @@ static int do_digest(char *algorithm, int argc, char *argv[])if (digest_file_window (d, filename, hash, start, size) <0 ) { ret =1 ; }else { -for (i =0 ; i < d->length; i++) +for (i =0 ; i < d->length; i++) {printf ("%02x" , hash[i]); +if (verify >0 && hash[i] != verify_hash[i]) + verify =-1 ; + } -printf (" %s\t0x%08llx ... 0x%08llx\n" , +printf (" %s\t0x%08llx ... 0x%08llx" , filename, start, start + size); + +if (verify <0 ) { +printf (" ** ERROR **" ); + ret =1 ; + verify =1 ; + } + +printf ("\n" ); } argv++; } -free (hash); +out: +if (hash) +free (hash); + +if (verify_hash) +free (verify_hash);return ret; }
@@ -83,8 +151,9 @@ static int do_md5(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(md5sum) -BAREBOX_CMD_HELP_USAGE("md5sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("md5sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a md5 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (md5sum )
@@ -103,8 +172,9 @@ static int do_sha1(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(sha1sum) -BAREBOX_CMD_HELP_USAGE("sha1sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("sha1sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a sha1 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (sha1sum )
@@ -123,8 +193,9 @@ static int do_sha224(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(sha224sum) -BAREBOX_CMD_HELP_USAGE("sha224sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("sha224sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a sha224 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (sha224sum )
@@ -143,8 +214,9 @@ static int do_sha256(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(sha256sum) -BAREBOX_CMD_HELP_USAGE("sha256sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("sha256sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a sha256 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (sha256sum )
Signature · 7 lines
1.8.1.3 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Quoted 28 lines · Hubert Feurstein
Signed-off-by Hubert Feurstein <[email protected]>--- commands/digest.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 11 deletions(-)commands/digest.c +15
-0@@ -25,24 +25,76 @@#include <xfuncs.h> #include <malloc.h> #include <digest.h> +#include <linux/ctype.h> +#include <getopt.h> + +static inline unsigned char parse_hexchar (char c) +{ +if (!isxdigit (c)) +return 0 ; + +return isdigit (c) ? (c -'0' ) : ((islower (c) ?toupper (c) : c) -'A' +0xA ); +} + +static inline unsigned char parse_hexbyte (const char *p) +{ +return (parse_hexchar (*p) <<4 ) |parse_hexchar (*(p +1 )); +}
commands/digest.c line 51
Quoted 10 lines
commands/digest.c +3
-1static int do_digest (char *algorithm,int argc,char *argv[]) {struct digest *d;int ret =0 ;int i; -unsigned char *hash; +unsigned char *hash =0 ; +int opt; +unsigned char *verify_hash =0 ;
NULL for pointers please.
Quoted 8 lines
commands/digest.c +2
-2+int verify =0 ; +int min_argc =2 ; d =digest_get_by_name (algorithm);BUG_ON (!d); -if (argc <2 ) -return COMMAND_ERROR_USAGE ;
commands/digest.c line 67
Quoted 10 lines
commands/digest.c +10
-0+while ((opt =getopt (argc, argv,"v:" )) >0 ) { +switch (opt) { +case 'v' : + verify =1 ; + min_argc +=2 ; + +if (d->length != (strlen (optarg) /2 )) { + ret =COMMAND_ERROR_USAGE ; + goto out; + }
Maybe give the user a hint here? It's not really obvious to the user what's wrong here.
commands/digest.c line 79
Quoted 12 lines
commands/digest.c +12
-0+ + verify_hash =calloc (d->length,sizeof (unsigned char )); +if (!verify_hash) { +perror ("calloc" ); + ret = -ENOMEM ; + goto out; + } + +for (i =0 ; i < d->length; i++) + verify_hash[i] =parse_hexbyte (&optarg[i *2 ]); + +break ;
You should only save optarg for later use in the loop. Otherwise when somebody gives -v multiple times you allocate memory each time which you don't free later.
Quoted 35 lines
commands/digest.c +19
-3+default : + ret =COMMAND_ERROR_USAGE ; + goto out; + } + } + +if (argc < min_argc) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + argv += min_argc -2 ; hash =calloc (d->length,sizeof (unsigned char ));if (!hash) {perror ("calloc" ); -return COMMAND_ERROR_USAGE ; + ret = -ENOMEM ; + goto out; } argv++;@@ -60,17 +112,33 @@ static int do_digest(char *algorithm, int argc, char *argv[])if (digest_file_window (d, filename, hash, start, size) <0 ) { ret =1 ; }else { -for (i =0 ; i < d->length; i++) +for (i =0 ; i < d->length; i++) {printf ("%02x" , hash[i]); +if (verify >0 && hash[i] != verify_hash[i]) + verify =-1 ; + } - printf(" %s\t0x%08llx ... 0x%08llx\n", + printf(" %s\t0x%08llx ... 0x%08llx",
commands/digest.c static int do_digest(char *algorithm, int argc, char *argv[]) · line 131
Quoted 10 lines
commands/digest.c +8
-0filename, start, start + size); + +if (verify <0 ) { +printf (" ** ERROR **" ); + ret =1 ; + verify =1 ; + } + +printf ("\n" ); }
The digest commands can work on multiple files. When verifying you verify multiple files against a single hash. Is this really useful behaviour? I'd suggest to allow only a single file in verify mode.
commands/digest.c static int do_digest(char *algorithm, int argc, char *argv[]) · line 141
Quoted 11 lines
commands/digest.c +6
-1argv++; } -free (hash); +out: +if (hash) +free (hash); + +if (verify_hash) +free (verify_hash);
No need to check. free() works fine on NULL pointers.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 1/1 commands/digest: add verify support
---
Changes:
v2:
- updated according to comments from Sascha
- add support for verify against hash-file
commands/digest.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 131 insertions(+), 10 deletions(-)
commands/digest.c +131 -10
@@ -25,24 +25,122 @@#include <xfuncs.h> #include <malloc.h> #include <digest.h> +#include <linux/ctype.h> +#include <getopt.h> + +static inline unsigned char parse_hexchar (char c) +{ +if (!isxdigit (c)) +return 0 ; + +return isdigit (c) ? (c -'0' ) : ((islower (c) ?toupper (c) : c) -'A' +0xA ); +} + +static inline unsigned char parse_hexbyte (const char *p) +{ +return (parse_hexchar (*p) <<4 ) |parse_hexchar (*(p +1 )); +} + +static unsigned char *parse_hash (int hash_len,const char *hexstr) +{ +int i; +unsigned char *p; + + p =calloc (hash_len,sizeof (unsigned char )); +if (!p) { +perror ("calloc" ); +return NULL ; + } + +for (i =0 ; i < hash_len; i++) + p[i] =parse_hexbyte (&hexstr[i *2 ]); + +return p; +}static int do_digest (char *algorithm,int argc,char *argv[]) {struct digest *d;int ret =0 ;int i; -unsigned char *hash; +unsigned char *hash =NULL ; +int opt; +unsigned char *verify_hash =NULL ; +int verify =0 ; +int min_argc =2 ; +void *buf; +ssize_t bufsz; d =digest_get_by_name (algorithm);BUG_ON (!d); -if (argc <2 ) -return COMMAND_ERROR_USAGE ; +while ((opt =getopt (argc, argv,"v:V:" )) >0 ) { +switch (opt) { +case 'v' : +if (verify) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify =1 ; + min_argc +=2 ; + +if (d->length != (strlen (optarg) /2 )) { +printf ("invalid hash length (%d chars required)\n" , + d->length *2 ); + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify_hash =parse_hash (d->length, optarg); +if (!verify_hash) { + ret = -ENOMEM ; + goto out; + } + +break ; +case 'V' : +if (verify) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify =1 ; + min_argc +=2 ; + + buf =read_file (optarg, &bufsz); +if (!buf) { + ret = -ENOMEM ; + goto out; + } + +if (bufsz < d->length) { +free (buf); + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify_hash =parse_hash (d->length, buf); +free (buf); +break ; +default : + ret =COMMAND_ERROR_USAGE ; + goto out; + } + } + +if (argc < min_argc) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + argv += min_argc -2 ; hash =calloc (d->length,sizeof (unsigned char ));if (!hash) {perror ("calloc" ); -return COMMAND_ERROR_USAGE ; + ret = -ENOMEM ; + goto out; } argv++;
@@ -60,17 +158,32 @@ static int do_digest(char *algorithm, int argc, char *argv[])if (digest_file_window (d, filename, hash, start, size) <0 ) { ret =1 ; }else { -for (i =0 ; i < d->length; i++) +for (i =0 ; i < d->length; i++) {printf ("%02x" , hash[i]); +if (verify >0 && hash[i] != verify_hash[i]) + verify =-1 ; + } -printf (" %s\t0x%08llx ... 0x%08llx\n" , +printf (" %s\t0x%08llx ... 0x%08llx" , filename, start, start + size); + +if (verify <0 ) { +printf (" ** ERROR **" ); + ret =1 ; + } + +printf ("\n" ); + +if (verify) +break ; } argv++; } +out:free (hash); +free (verify_hash);return ret; }
@@ -83,8 +196,10 @@ static int do_md5(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(md5sum) -BAREBOX_CMD_HELP_USAGE("md5sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("md5sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a md5 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n") +BAREBOX_CMD_HELP_OPT ("-V <hash-file>", "Verify hash file\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (md5sum )
@@ -103,8 +218,10 @@ static int do_sha1(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(sha1sum) -BAREBOX_CMD_HELP_USAGE("sha1sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("sha1sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a sha1 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n") +BAREBOX_CMD_HELP_OPT ("-V <hash-file>", "Verify hash file\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (sha1sum )
@@ -123,8 +240,10 @@ static int do_sha224(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(sha224sum) -BAREBOX_CMD_HELP_USAGE("sha224sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("sha224sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a sha224 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n") +BAREBOX_CMD_HELP_OPT ("-V <hash-file>", "Verify hash file\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (sha224sum )
@@ -143,8 +262,10 @@ static int do_sha256(int argc, char *argv[]) } BAREBOX_CMD_HELP_START(sha256sum) -BAREBOX_CMD_HELP_USAGE("sha256sum [[FILE] [AREA]]...\n") +BAREBOX_CMD_HELP_USAGE("sha256sum [OPTION] [[FILE] [AREA]]...\n") BAREBOX_CMD_HELP_SHORT("Calculate a sha256 checksum of a memory area.\n") +BAREBOX_CMD_HELP_OPT ("-v <hash>", "Verify\n") +BAREBOX_CMD_HELP_OPT ("-V <hash-file>", "Verify hash file\n")BAREBOX_CMD_HELP_END BAREBOX_CMD_START (sha256sum )
Signature · 7 lines
1.8.1.3 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Quoted 114 lines · Hubert Feurstein
Signed-off-by Hubert Feurstein <[email protected]>---
Changes:
v2:
- updated according to comments from Sascha
- add support for verify against hash-filecommands/digest.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 131 insertions(+), 10 deletions(-)commands/digest.c +83
-3@@ -25,24 +25,122 @@#include <xfuncs.h> #include <malloc.h> #include <digest.h> +#include <linux/ctype.h> +#include <getopt.h> + +static inline unsigned char parse_hexchar (char c) +{ +if (!isxdigit (c)) +return 0 ; + +return isdigit (c) ? (c -'0' ) : ((islower (c) ?toupper (c) : c) -'A' +0xA ); +} + +static inline unsigned char parse_hexbyte (const char *p) +{ +return (parse_hexchar (*p) <<4 ) |parse_hexchar (*(p +1 )); +} + +static unsigned char *parse_hash (int hash_len,const char *hexstr) +{ +int i; +unsigned char *p; + + p =calloc (hash_len,sizeof (unsigned char )); +if (!p) { +perror ("calloc" ); +return NULL ; + } + +for (i =0 ; i < hash_len; i++) + p[i] =parse_hexbyte (&hexstr[i *2 ]); + +return p; +}static int do_digest (char *algorithm,int argc,char *argv[]) {struct digest *d;int ret =0 ;int i; -unsigned char *hash; +unsigned char *hash =NULL ; +int opt; +unsigned char *verify_hash =NULL ; +int verify =0 ; +int min_argc =2 ; +void *buf; +ssize_t bufsz; d =digest_get_by_name (algorithm);BUG_ON (!d); -if (argc <2 ) -return COMMAND_ERROR_USAGE ; +while ((opt =getopt (argc, argv,"v:V:" )) >0 ) { +switch (opt) { +case 'v' : +if (verify) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify =1 ; + min_argc +=2 ; + +if (d->length != (strlen (optarg) /2 )) { +printf ("invalid hash length (%d chars required)\n" , + d->length *2 ); + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify_hash =parse_hash (d->length, optarg); +if (!verify_hash) { + ret = -ENOMEM ; + goto out; + } + +break ; +case 'V' : +if (verify) { + ret =COMMAND_ERROR_USAGE ; + goto out; + } + + verify =1 ; + min_argc +=2 ; + + buf =read_file (optarg, &bufsz); +if (!buf) { + ret = -ENOMEM ; + goto out; + } + +if (bufsz < d->length) { +free (buf); + ret =COMMAND_ERROR_USAGE ; + goto out;
commands/digest.c line 130
Quoted 10 lines
commands/digest.c +10
-0+ } + + verify_hash =parse_hash (d->length, buf); +free (buf); +break ; +default : + ret =COMMAND_ERROR_USAGE ; + goto out; + } + }
Please use something around the following lines to avoid some code duplication.
case 'v':
verify++;
free(hash);
hash = xstrdup(optarg);
break;
case 'V':
verify++;
free(hash);
hash = read_file(optarg, &bufsiz);
break;
default:
ret = COMMAND_ERROR_USAGE;
goto out;
}
if (verify > 1)
return COMMAND_ERROR_USAGE;
if (verify) {
min_argc += 2;
verify_hash = parse_hash(d, buf);
if (!verify_hash)
return -ESOMETHING;
}Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox