[git commit master 1/1] shaN: code shrink

Denys Vlasenko vda.linux at googlemail.com
Sat Oct 16 23:35:16 UTC 2010


commit: http://git.busybox.net/busybox/commit/?id=a971a192e8af4279fb384be9ff0f0e8387b229cb
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
init512_lo                                            32      40      +8
init256                                               32      40      +8
sha256_begin                                          42      28     -14
sha512_begin                                          81      53     -28
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/2 up/down: 16/-42)            Total: -26 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 include/libbb.h |    4 ++--
 libbb/sha1.c    |   15 ++++++++++-----
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index f406fc6..d05b2d4 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1514,7 +1514,7 @@ void FAST_FUNC read_base64(FILE *src_stream, FILE *dst_stream, int flags);
 
 typedef struct sha1_ctx_t {
 	uint32_t hash[8];    /* 5, +3 elements for sha256 */
-	uint64_t total64;
+	uint64_t total64;    /* must be directly after hash[] */
 	uint8_t wbuffer[64]; /* NB: always correctly aligned for uint64_t */
 	void (*process_block)(struct sha1_ctx_t*) FAST_FUNC;
 } sha1_ctx_t;
@@ -1527,7 +1527,7 @@ void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
 #define sha256_end sha1_end
 typedef struct sha512_ctx_t {
 	uint64_t hash[8];
-	uint64_t total64[2];
+	uint64_t total64[2];  /* must be directly after hash[] */
 	uint8_t wbuffer[128]; /* NB: always correctly aligned for uint64_t */
 } sha512_ctx_t;
 void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
diff --git a/libbb/sha1.c b/libbb/sha1.c
index 6d2f884..70efd58 100644
--- a/libbb/sha1.c
+++ b/libbb/sha1.c
@@ -329,7 +329,9 @@ static const uint32_t init256[] = {
 	0x510e527f,
 	0x9b05688c,
 	0x1f83d9ab,
-	0x5be0cd19
+	0x5be0cd19,
+	0,
+	0,
 };
 static const uint32_t init512_lo[] = {
 	0xf3bcc908,
@@ -339,7 +341,9 @@ static const uint32_t init512_lo[] = {
 	0xade682d1,
 	0x2b3e6c1f,
 	0xfb41bd6b,
-	0x137e2179
+	0x137e2179,
+	0,
+	0,
 };
 
 /* Initialize structure containing state of computation.
@@ -347,7 +351,7 @@ static const uint32_t init512_lo[] = {
 void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
 {
 	memcpy(ctx->hash, init256, sizeof(init256));
-	ctx->total64 = 0;
+	/*ctx->total64 = 0; - done by extending init256 with two 32-bit zeros */
 	ctx->process_block = sha256_process_block64;
 }
 
@@ -356,9 +360,10 @@ void FAST_FUNC sha256_begin(sha256_ctx_t *ctx)
 void FAST_FUNC sha512_begin(sha512_ctx_t *ctx)
 {
 	int i;
-	for (i = 0; i < 8; i++)
+	/* Two extra iterations zero out ctx->total64[] */
+	for (i = 0; i < 8+2; i++)
 		ctx->hash[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i];
-	ctx->total64[0] = ctx->total64[1] = 0;
+	/*ctx->total64[0] = ctx->total64[1] = 0; - already done */
 }
 
 
-- 
1.7.1



More information about the busybox-cvs mailing list