Add zero bytes around SHA-256 state variables to optimize rotates.

This commit is contained in:
Stephen Heumann 2017-07-03 22:58:58 -05:00
parent 239024c3ce
commit bcc0efc327
4 changed files with 37 additions and 41 deletions

View File

@ -24,19 +24,23 @@
* Direct page locations * Direct page locations
length gequ 0 length gequ 0
extra gequ 8 extra gequ 8
idx gequ 10 zero gequ 10
a_ gequ 12 ; elements of state a_ gequ 12 ; elements of state
b gequ 16 zero2 gequ 16
c gequ 20 b gequ 18
d gequ 24 zero3 gequ 22
e gequ 28 c gequ 24
f gequ 32 zero4 gequ 28
g gequ 36 d gequ 30
h gequ 40 zero5 gequ 34
temp1 gequ 44 e gequ 36
temp2 gequ 48 zero6 gequ 40
ch gequ 52 f gequ 42
maj gequ 56 zero7 gequ 46
g gequ 48
zero8 gequ 52
h gequ 54
zero9 gequ 58
h0 gequ 60 h0 gequ 60
h1 gequ 64 h1 gequ 64
h2 gequ 68 h2 gequ 68
@ -46,10 +50,10 @@ h5 gequ 80
h6 gequ 84 h6 gequ 84
h7 gequ 88 h7 gequ 88
w gequ 92 w gequ 92
temp3 gequ 156 ch gequ 156
temp4 gequ 160 maj gequ 160
k_ptr gequ 164 k_ptr gequ 164
zero gequ 168 idx gequ 168
two gequ 170 two gequ 170
four gequ 172 four gequ 172
six gequ 174 six gequ 174
@ -65,6 +69,10 @@ twentyfour gequ 192
twentysix gequ 194 twentysix gequ 194
twentyeight gequ 196 twentyeight gequ 196
thirty gequ 198 thirty gequ 198
temp1 gequ 200
temp2 gequ 204
temp3 gequ 208
temp4 gequ 212
k private k private
@ -133,6 +141,14 @@ SHA256_INIT start
stz extra stz extra
stz zero stz zero
stz zero2
stz zero3
stz zero4
stz zero5
stz zero6
stz zero7
stz zero8
stz zero9
lda #2 lda #2
sta two sta two
lda #4 lda #4

View File

@ -18,12 +18,10 @@ struct sha256_context {
unsigned long length; unsigned long length;
unsigned long length2; unsigned long length2;
unsigned short extra; unsigned short extra;
unsigned short idx; unsigned char reserved1[50];
unsigned long vars[8];
unsigned char reserved1[16];
unsigned char hash[32]; unsigned char hash[32];
unsigned char block[64]; unsigned char block[64];
unsigned char reserved2[44]; unsigned char reserved2[60];
}; };
/* /*

View File

@ -359,21 +359,13 @@
sta temp1 sta temp1
sta temp2 sta temp2
lda &e-1 lda &e-1
and #$FF00 ora &e+3
sta temp1+2
lda &e+3
and #$00FF
ora temp1+2
sta temp1+2 sta temp1+2
sta temp2+2 sta temp2+2
ROTR4CONT temp2,3 ROTR4CONT temp2,3
ROTL4 temp1,2 ROTL4 temp1,2
lda &e-1 lda &e-1
and #$FF00 ora &e+3
sta temp3
lda &e+3
and #$00FF
ora temp3
sta temp3 sta temp3
lda &e+1 lda &e+1
sta temp3+2 sta temp3+2
@ -442,11 +434,7 @@
lda &a+1 lda &a+1
sta temp4+2 sta temp4+2
lda &a-1 lda &a-1
and #$FF00 ora &a+3
sta temp4
lda &a+3
and #$00FF
ora temp4
sta temp4 sta temp4
ROTL4CONT temp4,2 ROTL4CONT temp4,2
lda temp2 lda temp2

View File

@ -27,7 +27,7 @@ int main(int argc, char **argv) {
long double bytes_per_sec; long double bytes_per_sec;
struct sha256_context *context, **context_hndl; 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, {0x61,0x62,0x63,0x80,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
@ -57,12 +57,6 @@ int main(int argc, char **argv) {
sha256_init(context); sha256_init(context);
sha256_processblock(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 " 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", "%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], context->hash[3], context->hash[2], context->hash[1], context->hash[0],