From bcc0efc32770b48700adf2a4d8e93aa27796a9e2 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Mon, 3 Jul 2017 22:58:58 -0500 Subject: [PATCH] Add zero bytes around SHA-256 state variables to optimize rotates. --- sha256.asm | 46 +++++++++++++++++++++++++++++++--------------- sha256.h | 6 ++---- sha256.macros | 18 +++--------------- sha256test.c | 8 +------- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/sha256.asm b/sha256.asm index 44d5972..2a7940f 100644 --- a/sha256.asm +++ b/sha256.asm @@ -24,19 +24,23 @@ * Direct page locations length gequ 0 extra gequ 8 -idx gequ 10 +zero gequ 10 a_ gequ 12 ; elements of state -b gequ 16 -c gequ 20 -d gequ 24 -e gequ 28 -f gequ 32 -g gequ 36 -h gequ 40 -temp1 gequ 44 -temp2 gequ 48 -ch gequ 52 -maj gequ 56 +zero2 gequ 16 +b gequ 18 +zero3 gequ 22 +c gequ 24 +zero4 gequ 28 +d gequ 30 +zero5 gequ 34 +e gequ 36 +zero6 gequ 40 +f gequ 42 +zero7 gequ 46 +g gequ 48 +zero8 gequ 52 +h gequ 54 +zero9 gequ 58 h0 gequ 60 h1 gequ 64 h2 gequ 68 @@ -46,10 +50,10 @@ h5 gequ 80 h6 gequ 84 h7 gequ 88 w gequ 92 -temp3 gequ 156 -temp4 gequ 160 +ch gequ 156 +maj gequ 160 k_ptr gequ 164 -zero gequ 168 +idx gequ 168 two gequ 170 four gequ 172 six gequ 174 @@ -65,6 +69,10 @@ twentyfour gequ 192 twentysix gequ 194 twentyeight gequ 196 thirty gequ 198 +temp1 gequ 200 +temp2 gequ 204 +temp3 gequ 208 +temp4 gequ 212 k private @@ -133,6 +141,14 @@ SHA256_INIT start stz extra stz zero + stz zero2 + stz zero3 + stz zero4 + stz zero5 + stz zero6 + stz zero7 + stz zero8 + stz zero9 lda #2 sta two lda #4 diff --git a/sha256.h b/sha256.h index 6062dbe..2b15c07 100644 --- a/sha256.h +++ b/sha256.h @@ -18,12 +18,10 @@ struct sha256_context { unsigned long length; unsigned long length2; unsigned short extra; - unsigned short idx; - unsigned long vars[8]; - unsigned char reserved1[16]; + unsigned char reserved1[50]; unsigned char hash[32]; unsigned char block[64]; - unsigned char reserved2[44]; + unsigned char reserved2[60]; }; /* diff --git a/sha256.macros b/sha256.macros index 9bbcf9d..e6b3af4 100644 --- a/sha256.macros +++ b/sha256.macros @@ -359,21 +359,13 @@ sta temp1 sta temp2 lda &e-1 - and #$FF00 - sta temp1+2 - lda &e+3 - and #$00FF - ora temp1+2 + ora &e+3 sta temp1+2 sta temp2+2 ROTR4CONT temp2,3 ROTL4 temp1,2 lda &e-1 - and #$FF00 - sta temp3 - lda &e+3 - and #$00FF - ora temp3 + ora &e+3 sta temp3 lda &e+1 sta temp3+2 @@ -442,11 +434,7 @@ lda &a+1 sta temp4+2 lda &a-1 - and #$FF00 - sta temp4 - lda &a+3 - and #$00FF - ora temp4 + ora &a+3 sta temp4 ROTL4CONT temp4,2 lda temp2 diff --git a/sha256test.c b/sha256test.c index 3be187b..13d2311 100644 --- a/sha256test.c +++ b/sha256test.c @@ -27,7 +27,7 @@ int main(int argc, char **argv) { long double bytes_per_sec; struct sha256_context *context, **context_hndl; - struct sha256_context context_init = {0,0,0,0, {0}, {0}, {0}, + struct sha256_context context_init = {0,0,0, {0}, {0}, {0x61,0x62,0x63,0x80, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, @@ -57,12 +57,6 @@ int main(int argc, char **argv) { sha256_init(context); sha256_processblock(context); - printf("a...h = "); - for (i = 0; i < 8; i++) { - printf("%08lx ", context->vars[i]); - } - printf("\n"); - printf("h[..] = %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x " "%02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n", context->hash[3], context->hash[2], context->hash[1], context->hash[0],