mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/5] move digest to crypto/
@ 2011-10-08 14:41 Jean-Christophe PLAGNIOL-VILLARD
  2011-10-08 14:41 ` [PATCH 2/5] digest: factorise file digest to common/digest.c Jean-Christophe PLAGNIOL-VILLARD
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-10-08 14:41 UTC (permalink / raw)
  To: barebox

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 Makefile                 |    2 +-
 arch/arm/Kconfig         |    1 +
 arch/blackfin/Kconfig    |    2 +-
 arch/mips/Kconfig        |    1 +
 arch/nios2/Kconfig       |    2 +-
 arch/ppc/Kconfig         |    1 +
 arch/sandbox/Kconfig     |    1 +
 arch/x86/Kconfig         |    1 +
 {lib => crypto}/Kconfig  |   15 ---------------
 crypto/Makefile          |    5 +++++
 {lib => crypto}/crc16.c  |    0
 {lib => crypto}/crc32.c  |    0
 {lib => crypto}/md5.c    |    0
 {lib => crypto}/sha1.c   |    0
 {lib => crypto}/sha256.c |    0
 lib/Kconfig              |   22 ----------------------
 lib/Makefile             |    5 -----
 scripts/bareboxenv.c     |    2 +-
 scripts/mkimage.c        |    2 +-
 19 files changed, 15 insertions(+), 47 deletions(-)
 copy {lib => crypto}/Kconfig (56%)
 create mode 100644 crypto/Makefile
 rename {lib => crypto}/crc16.c (100%)
 rename {lib => crypto}/crc32.c (100%)
 rename {lib => crypto}/md5.c (100%)
 rename {lib => crypto}/sha1.c (100%)
 rename {lib => crypto}/sha256.c (100%)

diff --git a/Makefile b/Makefile
index ccf012c..175444d 100644
--- a/Makefile
+++ b/Makefile
@@ -410,7 +410,7 @@ scripts: scripts_basic include/config/auto.conf
 	$(Q)$(MAKE) $(build)=$(@)
 
 # Objects we will link into barebox / subdirs we need to visit
-common-y		:= common/ drivers/ commands/ lib/ net/ fs/
+common-y		:= common/ drivers/ commands/ lib/ crypto/ net/ fs/
 
 ifeq ($(dot-config),1)
 # Read in config
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index d123787..da33000 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -144,3 +144,4 @@ source net/Kconfig
 source drivers/Kconfig
 source fs/Kconfig
 source lib/Kconfig
+source crypto/Kconfig
diff --git a/arch/blackfin/Kconfig b/arch/blackfin/Kconfig
index 587f802..1c58ba8 100644
--- a/arch/blackfin/Kconfig
+++ b/arch/blackfin/Kconfig
@@ -73,4 +73,4 @@ source net/Kconfig
 source drivers/Kconfig
 source fs/Kconfig
 source lib/Kconfig
-
+source crypto/Kconfig
diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig
index 8970470..50d5c67 100644
--- a/arch/mips/Kconfig
+++ b/arch/mips/Kconfig
@@ -225,3 +225,4 @@ source net/Kconfig
 source drivers/Kconfig
 source fs/Kconfig
 source lib/Kconfig
+source crypto/Kconfig
diff --git a/arch/nios2/Kconfig b/arch/nios2/Kconfig
index b4b0429..e1af0c0 100644
--- a/arch/nios2/Kconfig
+++ b/arch/nios2/Kconfig
@@ -36,4 +36,4 @@ source net/Kconfig
 source drivers/Kconfig
 source fs/Kconfig
 source lib/Kconfig
-
+source crypto/Kconfig
diff --git a/arch/ppc/Kconfig b/arch/ppc/Kconfig
index 50ccaac..4c7b7cd 100644
--- a/arch/ppc/Kconfig
+++ b/arch/ppc/Kconfig
@@ -55,3 +55,4 @@ source net/Kconfig
 source drivers/Kconfig
 source fs/Kconfig
 source lib/Kconfig
+source crypto/Kconfig
diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index 36f8afb..10e6829 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -20,3 +20,4 @@ source net/Kconfig
 source drivers/Kconfig
 source fs/Kconfig
 source lib/Kconfig
+source crypto/Kconfig
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index 6e70760..711bbfe 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -65,3 +65,4 @@ source net/Kconfig
 source drivers/Kconfig
 source fs/Kconfig
 source lib/Kconfig
+source crypto/Kconfig
diff --git a/lib/Kconfig b/crypto/Kconfig
similarity index 56%
copy from lib/Kconfig
copy to crypto/Kconfig
index ad2b3cf..9f01810 100644
--- a/lib/Kconfig
+++ b/crypto/Kconfig
@@ -1,9 +1,3 @@
-config ZLIB
-	bool
-
-config BZLIB
-	bool
-
 config CRC32
 	bool
 
@@ -25,12 +19,3 @@ config SHA256
 	bool "SHA256"
 
 endif
-
-config GENERIC_FIND_NEXT_BIT
-	def_bool n
-
-config PROCESS_ESCAPE_SEQUENCE
-	def_bool n
-
-source lib/lzo/Kconfig
-
diff --git a/crypto/Makefile b/crypto/Makefile
new file mode 100644
index 0000000..a88c5b7
--- /dev/null
+++ b/crypto/Makefile
@@ -0,0 +1,5 @@
+obj-$(CONFIG_CRC32)	+= crc32.o
+obj-$(CONFIG_CRC16)	+= crc16.o
+obj-$(CONFIG_MD5)	+= md5.o
+obj-$(CONFIG_SHA1)	+= sha1.o
+obj-$(CONFIG_SHA256)	+= sha256.o
diff --git a/lib/crc16.c b/crypto/crc16.c
similarity index 100%
rename from lib/crc16.c
rename to crypto/crc16.c
diff --git a/lib/crc32.c b/crypto/crc32.c
similarity index 100%
rename from lib/crc32.c
rename to crypto/crc32.c
diff --git a/lib/md5.c b/crypto/md5.c
similarity index 100%
rename from lib/md5.c
rename to crypto/md5.c
diff --git a/lib/sha1.c b/crypto/sha1.c
similarity index 100%
rename from lib/sha1.c
rename to crypto/sha1.c
diff --git a/lib/sha256.c b/crypto/sha256.c
similarity index 100%
rename from lib/sha256.c
rename to crypto/sha256.c
diff --git a/lib/Kconfig b/lib/Kconfig
index ad2b3cf..51f43e0 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -4,28 +4,6 @@ config ZLIB
 config BZLIB
 	bool
 
-config CRC32
-	bool
-
-config CRC16
-	bool
-
-menuconfig DIGEST
-	bool "Digest                        "
-
-if DIGEST
-
-config MD5
-	bool "MD5"
-
-config SHA1
-	bool "SHA1"
-
-config SHA256
-	bool "SHA256"
-
-endif
-
 config GENERIC_FIND_NEXT_BIT
 	def_bool n
 
diff --git a/lib/Makefile b/lib/Makefile
index d96cfe7..c66da7a 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -17,8 +17,6 @@ obj-y			+= recursive_action.o
 obj-y			+= make_directory.o
 obj-$(CONFIG_BZLIB)	+= bzlib.o bzlib_crctable.o bzlib_decompress.o bzlib_huffman.o bzlib_randtable.o
 obj-$(CONFIG_ZLIB)	+= zlib.o gunzip.o
-obj-$(CONFIG_CRC32)	+= crc32.o
-obj-$(CONFIG_CRC16)	+= crc16.o
 obj-$(CONFIG_CMDLINE_EDITING)	+= readline.o
 obj-$(CONFIG_SIMPLE_READLINE)	+= readline_simple.o
 obj-$(CONFIG_GLOB)		+= fnmatch.o
@@ -31,6 +29,3 @@ obj-y			+= lzo/
 obj-y			+= show_progress.o
 obj-$(CONFIG_LZO_DECOMPRESS)		+= decompress_unlzo.o
 obj-$(CONFIG_PROCESS_ESCAPE_SEQUENCE)	+= process_escape_sequence.o
-obj-$(CONFIG_MD5)	+= md5.o
-obj-$(CONFIG_SHA1)	+= sha1.o
-obj-$(CONFIG_SHA256)	+= sha256.o
diff --git a/scripts/bareboxenv.c b/scripts/bareboxenv.c
index 5c7f10e..b0d5818 100644
--- a/scripts/bareboxenv.c
+++ b/scripts/bareboxenv.c
@@ -117,7 +117,7 @@ char *concat_subpath_file(const char *path, const char *f)
 
 #include "../lib/recursive_action.c"
 #include "../include/envfs.h"
-#include "../lib/crc32.c"
+#include "../crypto/crc32.c"
 #include "../lib/make_directory.c"
 #include "../include/environment.h"
 #include "../common/environment.c"
diff --git a/scripts/mkimage.c b/scripts/mkimage.c
index d3a8bfb..3beab91 100644
--- a/scripts/mkimage.c
+++ b/scripts/mkimage.c
@@ -34,7 +34,7 @@
 char *cmdname;
 
 #include "../include/zlib.h"
-#include "../lib/crc32.c"
+#include "../crypto/crc32.c"
 
 //extern unsigned long crc32 (unsigned long crc, const char *buf, unsigned int len);
 
-- 
1.7.6.3


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/5] digest: factorise file digest to common/digest.c
  2011-10-08 14:41 [PATCH 1/5] move digest to crypto/ Jean-Christophe PLAGNIOL-VILLARD
@ 2011-10-08 14:41 ` Jean-Christophe PLAGNIOL-VILLARD
  2011-10-08 14:41 ` [PATCH 3/5] sha1: drop never used sha1_hmac Jean-Christophe PLAGNIOL-VILLARD
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-10-08 14:41 UTC (permalink / raw)
  To: barebox

rename it to digest_file_window

introduce digest_file to digest a file and digest_file_by_name
where we specify the algo by name

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/digest.c |   78 ++++++++--------------------------------
 common/digest.c   |  101 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/digest.h  |    8 ++++
 3 files changed, 125 insertions(+), 62 deletions(-)

diff --git a/commands/digest.c b/commands/digest.c
index 2a699e6..1fbffb6 100644
--- a/commands/digest.c
+++ b/commands/digest.c
@@ -29,71 +29,12 @@
 #include <malloc.h>
 #include <digest.h>
 
-static int file_digest(struct digest *d, char *filename,
-		       ulong start, ulong size)
-{
-	ulong len = 0;
-	int fd, now, i, ret = 0;
-	unsigned char *buf;
-
-	d->init(d);
-
-	fd = open(filename, O_RDONLY);
-	if (fd < 0) {
-		perror(filename);
-		return fd;
-	}
-
-	if (start > 0) {
-		ret = lseek(fd, start, SEEK_SET);
-		if (ret == -1) {
-			perror("lseek");
-			goto out;
-		}
-	}
-
-	buf = xmalloc(4096);
-
-	while (size) {
-		now = min((ulong)4096, size);
-		now = read(fd, buf, now);
-		if (now < 0) {
-			ret = now;
-			perror("read");
-			goto out_free;
-		}
-		if (!now)
-			break;
-
-		if (ctrlc()) {
-			ret = -EINTR;
-			goto out_free;
-		}
-
-		d->update(d, buf, now);
-		size -= now;
-		len += now;
-	}
-
-	d->final(d, buf);
-
-	for (i = 0; i < d->length; i++)
-		printf("%02x", buf[i]);
-
-	printf("  %s\t0x%08lx ... 0x%08lx\n", filename, start, start + len);
-
-out_free:
-	free(buf);
-out:
-	close(fd);
-
-	return ret;
-}
-
 static int do_digest(char *algorithm, int argc, char *argv[])
 {
 	struct digest *d;
 	int ret = 0;
+	int i;
+	unsigned char *hash;
 
 	d = digest_get_by_name(algorithm);
 	BUG_ON(!d);
@@ -101,6 +42,12 @@ static int do_digest(char *algorithm, int argc, char *argv[])
 	if (argc < 2)
 		return COMMAND_ERROR_USAGE;
 
+	hash = calloc(d->length, sizeof(unsigned char));
+	if (!hash) {
+		perror("calloc");
+		return COMMAND_ERROR_USAGE;
+	}
+
 	argv++;
 	while (*argv) {
 		char *filename = "/dev/mem";
@@ -113,12 +60,19 @@ static int do_digest(char *algorithm, int argc, char *argv[])
 				argv++;
 		}
 
-		if (file_digest(d, filename, start, size) < 0)
+		if (digest_file_window(d, filename, hash, start, size) < 0)
 			ret = 1;
 
+		for (i = 0; i < d->length; i++)
+			printf("%02x", hash[i]);
+
+		printf("  %s\t0x%08lx ... 0x%08lx\n", filename, start, start + size);
+
 		argv++;
 	}
 
+	free(hash);
+
 	return ret;
 }
 
diff --git a/common/digest.c b/common/digest.c
index 10ad060..a327395 100644
--- a/common/digest.c
+++ b/common/digest.c
@@ -23,6 +23,9 @@
 #include <common.h>
 #include <digest.h>
 #include <malloc.h>
+#include <fs.h>
+#include <fcntl.h>
+#include <linux/stat.h>
 #include <errno.h>
 #include <module.h>
 #include <linux/err.h>
@@ -75,3 +78,101 @@ struct digest* digest_get_by_name(char* name)
 	return NULL;
 }
 EXPORT_SYMBOL_GPL(digest_get_by_name);
+
+int digest_file_window(struct digest *d, char *filename,
+		       unsigned char *hash,
+		       ulong start, ulong size)
+{
+	ulong len = 0;
+	int fd, now, ret = 0;
+	unsigned char *buf;
+	int flags;
+
+	d->init(d);
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		perror(filename);
+		return fd;
+	}
+
+	buf = memmap(fd, PROT_READ);
+	if (buf == (void *)-1) {
+		buf = xmalloc(4096);
+		flags = 1;
+	}
+
+	if (start > 0) {
+		if (flags) {
+			ret = lseek(fd, start, SEEK_SET);
+			if (ret == -1) {
+				perror("lseek");
+				goto out;
+			}
+		} else {
+			buf += start;
+		}
+	}
+
+	while (size) {
+		now = min((ulong)4096, size);
+		if (flags) {
+			now = read(fd, buf, now);
+			if (now < 0) {
+				ret = now;
+				perror("read");
+				goto out_free;
+			}
+			if (!now)
+				break;
+		}
+
+		if (ctrlc()) {
+			ret = -EINTR;
+			goto out_free;
+		}
+
+		d->update(d, buf, now);
+		size -= now;
+		len += now;
+	}
+
+	d->final(d, hash);
+
+out_free:
+	if (flags)
+		free(buf);
+out:
+	close(fd);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(digest_file_window);
+
+int digest_file(struct digest *d, char *filename,
+		       unsigned char *hash)
+{
+	struct stat st;
+	int ret;
+
+	ret = stat(filename, &st);
+
+	if (ret < 0)
+		return ret;
+
+	return digest_file_window(d, filename, hash, 0, st.st_size);
+}
+EXPORT_SYMBOL_GPL(digest_file);
+
+int digest_file_by_name(char *algo, char *filename,
+		       unsigned char *hash)
+{
+	struct digest *d;
+
+	d = digest_get_by_name(algo);
+	if (!d)
+		return -EIO;
+
+	return digest_file(d, filename, hash);
+}
+EXPORT_SYMBOL_GPL(digest_file_by_name);
diff --git a/include/digest.h b/include/digest.h
index 1dcfd9d..36a8e37 100644
--- a/include/digest.h
+++ b/include/digest.h
@@ -46,4 +46,12 @@ void digest_unregister(struct digest *d);
 
 struct digest* digest_get_by_name(char* name);
 
+int digest_file_window(struct digest *d, char *filename,
+		       unsigned char *hash,
+		       ulong start, ulong size);
+int digest_file(struct digest *d, char *filename,
+		       unsigned char *hash);
+int digest_file_by_name(char *algo, char *filename,
+		       unsigned char *hash);
+
 #endif /* __SH_ST_DEVICES_H__ */
-- 
1.7.6.3


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/5] sha1: drop never used sha1_hmac
  2011-10-08 14:41 [PATCH 1/5] move digest to crypto/ Jean-Christophe PLAGNIOL-VILLARD
  2011-10-08 14:41 ` [PATCH 2/5] digest: factorise file digest to common/digest.c Jean-Christophe PLAGNIOL-VILLARD
@ 2011-10-08 14:41 ` Jean-Christophe PLAGNIOL-VILLARD
  2011-10-09  9:38   ` Sascha Hauer
  2011-10-08 14:41 ` [PATCH 4/5] crypto: add sha224 support Jean-Christophe PLAGNIOL-VILLARD
  2011-10-08 14:41 ` [PATCH 5/5] command/digest: " Jean-Christophe PLAGNIOL-VILLARD
  3 siblings, 1 reply; 8+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-10-08 14:41 UTC (permalink / raw)
  To: barebox

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 crypto/sha1.c |   39 ---------------------------------------
 1 files changed, 0 insertions(+), 39 deletions(-)

diff --git a/crypto/sha1.c b/crypto/sha1.c
index b4e2abc..e989c8a 100644
--- a/crypto/sha1.c
+++ b/crypto/sha1.c
@@ -291,45 +291,6 @@ static void sha1_finish (sha1_context * ctx, uint8_t output[20])
 	PUT_UINT32_BE (ctx->state[4], output, 16);
 }
 
-/*
- * Output = HMAC-SHA-1( input buffer, hmac key )
- */
-void sha1_hmac (uint8_t *key, uint32_t keylen,
-		uint8_t *input, uint32_t ilen, uint8_t output[20])
-{
-	uint32_t i;
-	sha1_context ctx;
-	uint8_t k_ipad[64];
-	uint8_t k_opad[64];
-	uint8_t tmpbuf[20];
-
-	memset (k_ipad, 0x36, 64);
-	memset (k_opad, 0x5C, 64);
-
-	for (i = 0; i < keylen; i++) {
-		if (i >= 64)
-			break;
-
-		k_ipad[i] ^= key[i];
-		k_opad[i] ^= key[i];
-	}
-
-	sha1_starts (&ctx);
-	sha1_update (&ctx, k_ipad, 64);
-	sha1_update (&ctx, input, ilen);
-	sha1_finish (&ctx, tmpbuf);
-
-	sha1_starts (&ctx);
-	sha1_update (&ctx, k_opad, 64);
-	sha1_update (&ctx, tmpbuf, 20);
-	sha1_finish (&ctx, output);
-
-	memset (k_ipad, 0, 64);
-	memset (k_opad, 0, 64);
-	memset (tmpbuf, 0, 20);
-	memset (&ctx, 0, sizeof (sha1_context));
-}
-
 struct sha1 {
 	sha1_context context;
 	struct digest d;
-- 
1.7.6.3


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 4/5] crypto: add sha224 support
  2011-10-08 14:41 [PATCH 1/5] move digest to crypto/ Jean-Christophe PLAGNIOL-VILLARD
  2011-10-08 14:41 ` [PATCH 2/5] digest: factorise file digest to common/digest.c Jean-Christophe PLAGNIOL-VILLARD
  2011-10-08 14:41 ` [PATCH 3/5] sha1: drop never used sha1_hmac Jean-Christophe PLAGNIOL-VILLARD
@ 2011-10-08 14:41 ` Jean-Christophe PLAGNIOL-VILLARD
  2011-10-08 14:41 ` [PATCH 5/5] command/digest: " Jean-Christophe PLAGNIOL-VILLARD
  3 siblings, 0 replies; 8+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-10-08 14:41 UTC (permalink / raw)
  To: barebox

the sha224sum is nearly the same as sha256sum except for the init of the
context and the hash length

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 crypto/Kconfig              |    3 +
 crypto/Makefile             |    3 +-
 crypto/{sha256.c => sha2.c} |  130 +++++++++++++++++++++++++++++-------------
 3 files changed, 95 insertions(+), 41 deletions(-)
 rename crypto/{sha256.c => sha2.c} (75%)

diff --git a/crypto/Kconfig b/crypto/Kconfig
index 9f01810..a391ae6 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -15,6 +15,9 @@ config MD5
 config SHA1
 	bool "SHA1"
 
+config SHA224
+	bool "SHA224"
+
 config SHA256
 	bool "SHA256"
 
diff --git a/crypto/Makefile b/crypto/Makefile
index a88c5b7..955a66d 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -2,4 +2,5 @@ obj-$(CONFIG_CRC32)	+= crc32.o
 obj-$(CONFIG_CRC16)	+= crc16.o
 obj-$(CONFIG_MD5)	+= md5.o
 obj-$(CONFIG_SHA1)	+= sha1.o
-obj-$(CONFIG_SHA256)	+= sha256.o
+obj-$(CONFIG_SHA224)	+= sha2.o
+obj-$(CONFIG_SHA256)	+= sha2.o
diff --git a/crypto/sha256.c b/crypto/sha2.c
similarity index 75%
rename from crypto/sha256.c
rename to crypto/sha2.c
index 975ebe9..17303e6 100644
--- a/crypto/sha256.c
+++ b/crypto/sha2.c
@@ -24,13 +24,15 @@
 #include <linux/string.h>
 #include <asm/byteorder.h>
 
+#define SHA224_SUM_LEN	28
 #define SHA256_SUM_LEN	32
 
 typedef struct {
 	uint32_t total[2];
 	uint32_t state[8];
 	uint8_t buffer[64];
-} sha256_context;
+	int is224;
+} sha2_context;
 
 /*
  * 32-bit integer manipulation macros (big endian)
@@ -38,22 +40,42 @@ typedef struct {
 #define GET_UINT32_BE(n,b,i) (n) = be32_to_cpu(((uint32_t*)(b))[i / 4])
 #define PUT_UINT32_BE(n,b,i) ((uint32_t*)(b))[i / 4] = cpu_to_be32(n)
 
-static void sha256_starts(sha256_context * ctx)
+static void sha2_starts(sha2_context * ctx, int is224)
 {
 	ctx->total[0] = 0;
 	ctx->total[1] = 0;
 
-	ctx->state[0] = 0x6A09E667;
-	ctx->state[1] = 0xBB67AE85;
-	ctx->state[2] = 0x3C6EF372;
-	ctx->state[3] = 0xA54FF53A;
-	ctx->state[4] = 0x510E527F;
-	ctx->state[5] = 0x9B05688C;
-	ctx->state[6] = 0x1F83D9AB;
-	ctx->state[7] = 0x5BE0CD19;
+#ifdef CONFIG_SHA256
+	if (is224 == 0) {
+		/* SHA-256 */
+		ctx->state[0] = 0x6A09E667;
+		ctx->state[1] = 0xBB67AE85;
+		ctx->state[2] = 0x3C6EF372;
+		ctx->state[3] = 0xA54FF53A;
+		ctx->state[4] = 0x510E527F;
+		ctx->state[5] = 0x9B05688C;
+		ctx->state[6] = 0x1F83D9AB;
+		ctx->state[7] = 0x5BE0CD19;
+	}
+#endif
+#ifdef CONFIG_SHA224
+	if (is224 == 1) {
+		/* SHA-224 */
+		ctx->state[0] = 0xC1059ED8;
+		ctx->state[1] = 0x367CD507;
+		ctx->state[2] = 0x3070DD17;
+		ctx->state[3] = 0xF70E5939;
+		ctx->state[4] = 0xFFC00B31;
+		ctx->state[5] = 0x68581511;
+		ctx->state[6] = 0x64F98FA7;
+		ctx->state[7] = 0xBEFA4FA4;
+	}
+#endif
+
+	ctx->is224 = is224;
 }
 
-static void sha256_process(sha256_context * ctx, uint8_t data[64])
+static void sha2_process(sha2_context * ctx, const uint8_t data[64])
 {
 	uint32_t temp1, temp2;
 	uint32_t W[64];
@@ -184,32 +206,33 @@ static void sha256_process(sha256_context * ctx, uint8_t data[64])
 	ctx->state[7] += H;
 }
 
-static void sha256_update(sha256_context * ctx, uint8_t * input, uint32_t length)
+static void sha2_update(sha2_context * ctx, const uint8_t * input, size_t length)
 {
-	uint32_t left, fill;
+	size_t fill;
+	uint32_t left;
 
-	if (!length)
+	if (length <= 0)
 		return;
 
 	left = ctx->total[0] & 0x3F;
 	fill = 64 - left;
 
-	ctx->total[0] += length;
+	ctx->total[0] += (uint32_t)length;
 	ctx->total[0] &= 0xFFFFFFFF;
 
-	if (ctx->total[0] < length)
+	if (ctx->total[0] < (uint32_t)length)
 		ctx->total[1]++;
 
 	if (left && length >= fill) {
 		memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-		sha256_process(ctx, ctx->buffer);
+		sha2_process(ctx, ctx->buffer);
 		length -= fill;
 		input += fill;
 		left = 0;
 	}
 
 	while (length >= 64) {
-		sha256_process(ctx, input);
+		sha2_process(ctx, input);
 		length -= 64;
 		input += 64;
 	}
@@ -218,14 +241,14 @@ static void sha256_update(sha256_context * ctx, uint8_t * input, uint32_t length
 		memcpy((void *) (ctx->buffer + left), (void *) input, length);
 }
 
-static uint8_t sha256_padding[64] = {
+static const uint8_t sha2_padding[64] = {
 	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 	   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
-static void sha256_finish(sha256_context * ctx, uint8_t digest[32])
+static void sha2_finish(sha2_context * ctx, uint8_t digest[32])
 {
 	uint32_t last, padn;
 	uint32_t high, low;
@@ -241,8 +264,8 @@ static void sha256_finish(sha256_context * ctx, uint8_t digest[32])
 	last = ctx->total[0] & 0x3F;
 	padn = (last < 56) ? (56 - last) : (120 - last);
 
-	sha256_update(ctx, sha256_padding, padn);
-	sha256_update(ctx, msglen, 8);
+	sha2_update(ctx, sha2_padding, padn);
+	sha2_update(ctx, msglen, 8);
 
 	PUT_UINT32_BE(ctx->state[0], digest, 0);
 	PUT_UINT32_BE(ctx->state[1], digest, 4);
@@ -254,53 +277,80 @@ static void sha256_finish(sha256_context * ctx, uint8_t digest[32])
 	PUT_UINT32_BE(ctx->state[7], digest, 28);
 }
 
-struct sha256 {
-	sha256_context context;
+struct sha2 {
+	sha2_context context;
 	struct digest d;
 };
 
-static int digest_sha256_init(struct digest *d)
+static int digest_sha2_update(struct digest *d, const void *data,
+				unsigned long len)
 {
-	struct sha256 *m = container_of(d, struct sha256, d);
+	struct sha2 *m = container_of(d, struct sha2, d);
 
-	sha256_starts(&m->context);
+	sha2_update(&m->context, (uint8_t *)data, len);
 
 	return 0;
 }
 
-static int digest_sha256_update(struct digest *d, const void *data,
-				unsigned long len)
+static int digest_sha2_final(struct digest *d, unsigned char *md)
 {
-	struct sha256 *m = container_of(d, struct sha256, d);
+	struct sha2 *m = container_of(d, struct sha2, d);
 
-	sha256_update(&m->context, (uint8_t *)data, len);
+	sha2_finish(&m->context, md);
 
 	return 0;
 }
 
-static int digest_sha256_final(struct digest *d, unsigned char *md)
+#ifdef CONFIG_SHA224
+static int digest_sha224_init(struct digest *d)
+{
+	struct sha2 *m = container_of(d, struct sha2, d);
+
+	sha2_starts(&m->context, 1);
+
+	return 0;
+}
+
+static struct sha2 m224 = {
+	.d = {
+		.init = digest_sha224_init,
+		.update = digest_sha2_update,
+		.final = digest_sha2_final,
+		.length = SHA224_SUM_LEN,
+	}
+};
+#endif
+
+#ifdef CONFIG_SHA256
+static int digest_sha256_init(struct digest *d)
 {
-	struct sha256 *m = container_of(d, struct sha256, d);
+	struct sha2 *m = container_of(d, struct sha2, d);
 
-	sha256_finish(&m->context, md);
+	sha2_starts(&m->context, 0);
 
 	return 0;
 }
 
-static struct sha256 m = {
+static struct sha2 m256 = {
 	.d = {
 		.name = "sha256",
 		.init = digest_sha256_init,
-		.update = digest_sha256_update,
-		.final = digest_sha256_final,
+		.update = digest_sha2_update,
+		.final = digest_sha2_final,
 		.length = SHA256_SUM_LEN,
 	}
 };
+#endif
 
-static int sha256_digest_register(void)
+static int sha2_digest_register(void)
 {
-	digest_register(&m.d);
+#ifdef CONFIG_SHA224
+	digest_register(&m224.d);
+#endif
+#ifdef CONFIG_SHA256
+	digest_register(&m256.d);
+#endif
 
 	return 0;
 }
-device_initcall(sha256_digest_register);
+device_initcall(sha2_digest_register);
-- 
1.7.6.3


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 5/5] command/digest: add sha224 support
  2011-10-08 14:41 [PATCH 1/5] move digest to crypto/ Jean-Christophe PLAGNIOL-VILLARD
                   ` (2 preceding siblings ...)
  2011-10-08 14:41 ` [PATCH 4/5] crypto: add sha224 support Jean-Christophe PLAGNIOL-VILLARD
@ 2011-10-08 14:41 ` Jean-Christophe PLAGNIOL-VILLARD
  3 siblings, 0 replies; 8+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-10-08 14:41 UTC (permalink / raw)
  To: barebox

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/Kconfig  |    6 ++++++
 commands/digest.c |   20 ++++++++++++++++++++
 2 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index 39bdb0f..095c1b2 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -252,6 +252,12 @@ config CMD_SHA256SUM
 	select SHA256
 	prompt "sha256sum"
 
+config CMD_SHA224SUM
+	tristate
+	select CMD_DIGEST
+	select SHA224
+	prompt "sha224sum"
+
 config CMD_MTEST
 	tristate
 	prompt "mtest"
diff --git a/commands/digest.c b/commands/digest.c
index 1fbffb6..fbeadf9 100644
--- a/commands/digest.c
+++ b/commands/digest.c
@@ -116,6 +116,26 @@ BAREBOX_CMD_END
 
 #endif /* CMD_CMD_SHA1SUM */
 
+#ifdef CONFIG_CMD_SHA224SUM
+
+static int do_sha224(struct command *cmdtp, int argc, char *argv[])
+{
+	return do_digest("sha224", argc, argv);
+}
+
+BAREBOX_CMD_HELP_START(sha224sum)
+BAREBOX_CMD_HELP_USAGE("sha224sum [[FILE] [AREA]]...\n")
+BAREBOX_CMD_HELP_SHORT("Calculate a sha224 checksum of a memory area.\n")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(sha224sum)
+	.cmd		= do_sha224,
+	.usage		= "sha224 checksum calculation",
+	BAREBOX_CMD_HELP(cmd_sha224sum_help)
+BAREBOX_CMD_END
+
+#endif /* CMD_CMD_SHA224SUM */
+
 #ifdef CONFIG_CMD_SHA256SUM
 
 static int do_sha256(struct command *cmdtp, int argc, char *argv[])
-- 
1.7.6.3


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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/5] sha1: drop never used sha1_hmac
  2011-10-08 14:41 ` [PATCH 3/5] sha1: drop never used sha1_hmac Jean-Christophe PLAGNIOL-VILLARD
@ 2011-10-09  9:38   ` Sascha Hauer
  2011-10-10 18:10     ` Jean-Christophe PLAGNIOL-VILLARD
  0 siblings, 1 reply; 8+ messages in thread
From: Sascha Hauer @ 2011-10-09  9:38 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

On Sat, Oct 08, 2011 at 04:41:57PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
> ---
>  crypto/sha1.c |   39 ---------------------------------------
>  1 files changed, 0 insertions(+), 39 deletions(-)

This one does not apply. We do not have a crypto/ dir in the tree.

Sascha

> 
> diff --git a/crypto/sha1.c b/crypto/sha1.c
> index b4e2abc..e989c8a 100644
> --- a/crypto/sha1.c
> +++ b/crypto/sha1.c
> @@ -291,45 +291,6 @@ static void sha1_finish (sha1_context * ctx, uint8_t output[20])
>  	PUT_UINT32_BE (ctx->state[4], output, 16);
>  }
>  
> -/*
> - * Output = HMAC-SHA-1( input buffer, hmac key )
> - */
> -void sha1_hmac (uint8_t *key, uint32_t keylen,
> -		uint8_t *input, uint32_t ilen, uint8_t output[20])
> -{
> -	uint32_t i;
> -	sha1_context ctx;
> -	uint8_t k_ipad[64];
> -	uint8_t k_opad[64];
> -	uint8_t tmpbuf[20];
> -
> -	memset (k_ipad, 0x36, 64);
> -	memset (k_opad, 0x5C, 64);
> -
> -	for (i = 0; i < keylen; i++) {
> -		if (i >= 64)
> -			break;
> -
> -		k_ipad[i] ^= key[i];
> -		k_opad[i] ^= key[i];
> -	}
> -
> -	sha1_starts (&ctx);
> -	sha1_update (&ctx, k_ipad, 64);
> -	sha1_update (&ctx, input, ilen);
> -	sha1_finish (&ctx, tmpbuf);
> -
> -	sha1_starts (&ctx);
> -	sha1_update (&ctx, k_opad, 64);
> -	sha1_update (&ctx, tmpbuf, 20);
> -	sha1_finish (&ctx, output);
> -
> -	memset (k_ipad, 0, 64);
> -	memset (k_opad, 0, 64);
> -	memset (tmpbuf, 0, 20);
> -	memset (&ctx, 0, sizeof (sha1_context));
> -}
> -
>  struct sha1 {
>  	sha1_context context;
>  	struct digest d;
> -- 
> 1.7.6.3
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/5] sha1: drop never used sha1_hmac
  2011-10-09  9:38   ` Sascha Hauer
@ 2011-10-10 18:10     ` Jean-Christophe PLAGNIOL-VILLARD
  2011-10-12  6:53       ` Sascha Hauer
  0 siblings, 1 reply; 8+ messages in thread
From: Jean-Christophe PLAGNIOL-VILLARD @ 2011-10-10 18:10 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: barebox

On 11:38 Sun 09 Oct     , Sascha Hauer wrote:
> On Sat, Oct 08, 2011 at 04:41:57PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> > Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
> > ---
> >  crypto/sha1.c |   39 ---------------------------------------
> >  1 files changed, 0 insertions(+), 39 deletions(-)
> 
> This one does not apply. We do not have a crypto/ dir in the tree.
the frist patch do the move it

Best Regards,
J.

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/5] sha1: drop never used sha1_hmac
  2011-10-10 18:10     ` Jean-Christophe PLAGNIOL-VILLARD
@ 2011-10-12  6:53       ` Sascha Hauer
  0 siblings, 0 replies; 8+ messages in thread
From: Sascha Hauer @ 2011-10-12  6:53 UTC (permalink / raw)
  To: Jean-Christophe PLAGNIOL-VILLARD; +Cc: barebox

On Mon, Oct 10, 2011 at 08:10:45PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> On 11:38 Sun 09 Oct     , Sascha Hauer wrote:
> > On Sat, Oct 08, 2011 at 04:41:57PM +0200, Jean-Christophe PLAGNIOL-VILLARD wrote:
> > > Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
> > > ---
> > >  crypto/sha1.c |   39 ---------------------------------------
> > >  1 files changed, 0 insertions(+), 39 deletions(-)
> > 
> > This one does not apply. We do not have a crypto/ dir in the tree.
> the frist patch do the move it

Ah, ok. I probably skipped the first patch because I thought it's the
introductory mail.

Sascha


-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-10-12  6:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-08 14:41 [PATCH 1/5] move digest to crypto/ Jean-Christophe PLAGNIOL-VILLARD
2011-10-08 14:41 ` [PATCH 2/5] digest: factorise file digest to common/digest.c Jean-Christophe PLAGNIOL-VILLARD
2011-10-08 14:41 ` [PATCH 3/5] sha1: drop never used sha1_hmac Jean-Christophe PLAGNIOL-VILLARD
2011-10-09  9:38   ` Sascha Hauer
2011-10-10 18:10     ` Jean-Christophe PLAGNIOL-VILLARD
2011-10-12  6:53       ` Sascha Hauer
2011-10-08 14:41 ` [PATCH 4/5] crypto: add sha224 support Jean-Christophe PLAGNIOL-VILLARD
2011-10-08 14:41 ` [PATCH 5/5] command/digest: " Jean-Christophe PLAGNIOL-VILLARD

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox