command: digest/hashsum: set key at command level
and only set the key when specified Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
parent
e49a47fba8
commit
5afec9c781
|
@ -17,8 +17,7 @@
|
||||||
|
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
|
||||||
int __do_digest(struct digest *d, unsigned char *key, int keylen,
|
int __do_digest(struct digest *d, unsigned char *sig,
|
||||||
unsigned char *sig,
|
|
||||||
int argc, char *argv[])
|
int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int ret = COMMAND_ERROR_USAGE;
|
int ret = COMMAND_ERROR_USAGE;
|
||||||
|
@ -28,17 +27,6 @@ int __do_digest(struct digest *d, unsigned char *key, int keylen,
|
||||||
if (argc < 1)
|
if (argc < 1)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
if (key) {
|
|
||||||
ret = digest_set_key(d, key, keylen);
|
|
||||||
if (ret) {
|
|
||||||
perror("set_key");
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
} else if (digest_is_flags(d, DIGEST_ALGO_NEED_KEY)) {
|
|
||||||
eprintf("%s need a key to be used\n", digest_name(d));
|
|
||||||
goto err;
|
|
||||||
}
|
|
||||||
|
|
||||||
hash = calloc(digest_length(d), sizeof(unsigned char));
|
hash = calloc(digest_length(d), sizeof(unsigned char));
|
||||||
if (!hash) {
|
if (!hash) {
|
||||||
perror("calloc");
|
perror("calloc");
|
||||||
|
@ -147,10 +135,15 @@ static int do_digest(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = digest_set_key(d, key, keylen);
|
if (key) {
|
||||||
free(tmp_key);
|
ret = digest_set_key(d, key, keylen);
|
||||||
if (ret)
|
free(tmp_key);
|
||||||
|
if (ret)
|
||||||
|
goto err;
|
||||||
|
} else if (digest_is_flags(d, DIGEST_ALGO_NEED_KEY)) {
|
||||||
|
eprintf("%s need a key to be used\n", digest_name(d));
|
||||||
goto err;
|
goto err;
|
||||||
|
}
|
||||||
|
|
||||||
if (sigfile) {
|
if (sigfile) {
|
||||||
sig = tmp_sig = read_file(sigfile, &siglen);
|
sig = tmp_sig = read_file(sigfile, &siglen);
|
||||||
|
@ -178,7 +171,7 @@ static int do_digest(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = __do_digest(d, NULL, 0, sig, argc, argv);
|
ret = __do_digest(d, sig, argc, argv);
|
||||||
free(tmp_sig);
|
free(tmp_sig);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ static int do_hash(char *algo, int argc, char *argv[])
|
||||||
struct digest *d;
|
struct digest *d;
|
||||||
unsigned char *key = NULL;
|
unsigned char *key = NULL;
|
||||||
size_t keylen = 0;
|
size_t keylen = 0;
|
||||||
int opt;
|
int opt, ret;
|
||||||
|
|
||||||
while((opt = getopt(argc, argv, "h:")) > 0) {
|
while((opt = getopt(argc, argv, "h:")) > 0) {
|
||||||
switch(opt) {
|
switch(opt) {
|
||||||
|
@ -49,15 +49,22 @@ static int do_hash(char *algo, int argc, char *argv[])
|
||||||
char *tmp = asprintf("hmac(%s)", algo);
|
char *tmp = asprintf("hmac(%s)", algo);
|
||||||
d = digest_alloc(tmp);
|
d = digest_alloc(tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
|
BUG_ON(!d);
|
||||||
|
|
||||||
|
ret = digest_set_key(d, key, keylen);
|
||||||
|
if (ret) {
|
||||||
|
perror("set_key");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
d = digest_alloc(algo);
|
d = digest_alloc(algo);
|
||||||
|
BUG_ON(!d);
|
||||||
}
|
}
|
||||||
BUG_ON(!d);
|
|
||||||
|
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
return __do_digest(d, key, keylen, NULL, argc, argv);
|
return __do_digest(d, NULL, argc, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_CMD_MD5SUM
|
#ifdef CONFIG_CMD_MD5SUM
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
int __do_digest(struct digest *d, unsigned char *key, int keylen,
|
int __do_digest(struct digest *d, unsigned char *sig,
|
||||||
unsigned char *sig,
|
|
||||||
int argc, char *argv[]);
|
int argc, char *argv[]);
|
||||||
|
|
Loading…
Reference in New Issue