mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
To: barebox@lists.infradead.org
Subject: [PATCH 7/7] command: add hmac sum supportfor md5, sha1, sha224, sha256, sha384, sha512
Date: Tue, 10 Mar 2015 15:28:17 +0100	[thread overview]
Message-ID: <1425997697-27467-7-git-send-email-plagnioj@jcrosoft.com> (raw)
In-Reply-To: <1425997697-27467-1-git-send-email-plagnioj@jcrosoft.com>

pass the key via -k param

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/digest.c | 26 ++++++++++++++++++++++----
 common/digest.c   | 22 +++++++++++++++++-----
 include/digest.h  |  3 +++
 3 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/commands/digest.c b/commands/digest.c
index 20fa13f..713f5c6 100644
--- a/commands/digest.c
+++ b/commands/digest.c
@@ -25,6 +25,7 @@
 #include <xfuncs.h>
 #include <malloc.h>
 #include <digest.h>
+#include <getopt.h>
 
 static int do_digest(char *algorithm, int argc, char *argv[])
 {
@@ -32,11 +33,26 @@ static int do_digest(char *algorithm, int argc, char *argv[])
 	int ret = 0;
 	int i;
 	unsigned char *hash;
+	unsigned char *key = NULL;
+	size_t keylen = 0;
+	int opt;
+
+	while((opt = getopt(argc, argv, "k:")) > 0) {
+		switch(opt) {
+		case 'k':
+			key = optarg;
+			keylen = strlen(key);
+			break;
+		}
+	}
+
+	argc -= optind;
+	argv += optind;
 
 	d = digest_alloc(algorithm);
 	BUG_ON(!d);
 
-	if (argc < 2)
+	if (argc < 1)
 		return COMMAND_ERROR_USAGE;
 
 	hash = calloc(digest_length(d), sizeof(unsigned char));
@@ -45,7 +61,6 @@ static int do_digest(char *algorithm, int argc, char *argv[])
 		return COMMAND_ERROR_USAGE;
 	}
 
-	argv++;
 	while (*argv) {
 		char *filename = "/dev/mem";
 		loff_t start = 0, size = ~0;
@@ -53,11 +68,14 @@ static int do_digest(char *algorithm, int argc, char *argv[])
 		/* arguments are either file, file+area or area */
 		if (parse_area_spec(*argv, &start, &size)) {
 			filename = *argv;
-			if (argv[1] && !parse_area_spec(argv[1], &start, &size))
+			if (argv[0] && !parse_area_spec(argv[0], &start, &size))
 				argv++;
 		}
 
-		if (digest_file_window(d, filename, hash, start, size) < 0) {
+		ret = digest_file_window(d, filename,
+					 key, keylen,
+					 hash, start, size);
+		if (ret < 0) {
 			ret = 1;
 		} else {
 			for (i = 0; i < digest_length(d); i++)
diff --git a/common/digest.c b/common/digest.c
index 7c09ce2..dcbb2c1 100644
--- a/common/digest.c
+++ b/common/digest.c
@@ -154,6 +154,7 @@ void digest_hmac_final(struct digest *d, unsigned char *md)
 EXPORT_SYMBOL_GPL(digest_hmac_final);
 
 int digest_file_window(struct digest *d, char *filename,
+		       unsigned char *key, size_t keylen,
 		       unsigned char *hash,
 		       ulong start, ulong size)
 {
@@ -162,7 +163,10 @@ int digest_file_window(struct digest *d, char *filename,
 	unsigned char *buf;
 	int flags = 0;
 
-	digest_init(d);
+	if (key)
+		digest_hmac_init(d, key, keylen);
+	else
+		digest_init(d);
 
 	fd = open(filename, O_RDONLY);
 	if (fd < 0) {
@@ -206,12 +210,18 @@ int digest_file_window(struct digest *d, char *filename,
 			goto out_free;
 		}
 
-		digest_update(d, buf, now);
+		if (key)
+			digest_hmac_update(d, buf, now);
+		else
+			digest_update(d, buf, now);
 		size -= now;
 		len += now;
 	}
 
-	digest_final(d, hash);
+	if (key)
+		digest_hmac_final(d, hash);
+	else
+		digest_final(d, hash);
 
 out_free:
 	if (flags)
@@ -224,6 +234,7 @@ out:
 EXPORT_SYMBOL_GPL(digest_file_window);
 
 int digest_file(struct digest *d, char *filename,
+		       unsigned char *key, size_t keylen,
 		       unsigned char *hash)
 {
 	struct stat st;
@@ -234,11 +245,12 @@ int digest_file(struct digest *d, char *filename,
 	if (ret < 0)
 		return ret;
 
-	return digest_file_window(d, filename, hash, 0, st.st_size);
+	return digest_file_window(d, filename, key, keylen, hash, 0, st.st_size);
 }
 EXPORT_SYMBOL_GPL(digest_file);
 
 int digest_file_by_name(char *algo, char *filename,
+		       unsigned char *key, size_t keylen,
 		       unsigned char *hash)
 {
 	struct digest *d;
@@ -248,7 +260,7 @@ int digest_file_by_name(char *algo, char *filename,
 	if (!d)
 		return -EIO;
 
-	ret = digest_file(d, filename, hash);
+	ret = digest_file(d, filename, key, keylen, hash);
 	digest_free(d);
 	return ret;
 }
diff --git a/include/digest.h b/include/digest.h
index f04f467..776b71c 100644
--- a/include/digest.h
+++ b/include/digest.h
@@ -56,11 +56,14 @@ struct digest* digest_alloc(char* name);
 void digest_free(struct digest *d);
 
 int digest_file_window(struct digest *d, char *filename,
+		       unsigned char *key, size_t keylen,
 		       unsigned char *hash,
 		       ulong start, ulong size);
 int digest_file(struct digest *d, char *filename,
+		       unsigned char *key, size_t keylen,
 		       unsigned char *hash);
 int digest_file_by_name(char *algo, char *filename,
+		       unsigned char *key, size_t keylen,
 		       unsigned char *hash);
 
 static inline void digest_init(struct digest *d)
-- 
2.1.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2015-03-10 14:28 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-10 14:16 [PATCH 0/7] add sha384/sha512 and hmac support Jean-Christophe PLAGNIOL-VILLARD
2015-03-10 14:28 ` [PATCH 1/7] digest: introduce digest_{init/update/final/length} Jean-Christophe PLAGNIOL-VILLARD
2015-03-10 14:28   ` [PATCH 2/7] digest: make it multi-instance Jean-Christophe PLAGNIOL-VILLARD
2015-03-11  5:21     ` Sascha Hauer
2015-03-11  5:32     ` Sascha Hauer
2015-03-11 10:44       ` Jean-Christophe PLAGNIOL-VILLARD
2015-03-11 11:45         ` Sascha Hauer
2015-03-10 14:28   ` [PATCH 3/7] crypto: add sha384 & sha512 support Jean-Christophe PLAGNIOL-VILLARD
2015-03-10 14:28   ` [PATCH 4/7] command: add sha384sum and sha512sum support Jean-Christophe PLAGNIOL-VILLARD
2015-03-10 14:28   ` [PATCH 5/7] password: add support for sha512 Jean-Christophe PLAGNIOL-VILLARD
2015-03-10 14:28   ` [PATCH 6/7] digest: add HMAC support for md5, sha1, sha224, sha256, sha384, sha512 Jean-Christophe PLAGNIOL-VILLARD
2015-03-11  5:40     ` Sascha Hauer
2015-03-11 10:47       ` Jean-Christophe PLAGNIOL-VILLARD
2015-03-10 14:28   ` Jean-Christophe PLAGNIOL-VILLARD [this message]
2015-03-11  5:43     ` [PATCH 7/7] command: add hmac sum supportfor " Sascha Hauer
2015-03-11 10:52       ` Jean-Christophe PLAGNIOL-VILLARD
2015-03-11 11:00         ` Jean-Christophe PLAGNIOL-VILLARD
2015-03-11 11:41           ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1425997697-27467-7-git-send-email-plagnioj@jcrosoft.com \
    --to=plagnioj@jcrosoft.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox