Add initialization function to permit computation of SHA-224 hashes.

SHA-224 uses the exact same computation as SHA-256, just with different initial values.
This commit is contained in:
Stephen Heumann 2017-07-03 23:40:36 -05:00
parent 11148c78d1
commit 458e769212
3 changed files with 77 additions and 8 deletions

View File

@ -94,13 +94,56 @@ k private
dc i4'$90befffa, $a4506ceb, $bef9a3f7, $c67178f2' dc i4'$90befffa, $a4506ceb, $bef9a3f7, $c67178f2'
end end
* Initialize a SHA-256 context. * Initialize a context for SHA-256 computation.
* This must be called before any of the other SHA-256 functions. * An init function must be called before any of the other SHA-256 functions.
sha256_init start sha256_init start
CFunction SHA256_INIT CFunction SHA256_INIT
end end
SHA256_INIT start * Initialize a context for SHA-224 computation.
* To compute a SHA-224 hash, call this function, and then call the below
* functions as if computing a SHA-256 hash. After calling sha256_finalize,
* the first 28 bytes of context->hash will contain the SHA-224 hash.
sha224_init start
CFunction SHA224_INIT
end
SHA224_INIT start
lda #$9ed8
sta h0
lda #$c105
sta h0+2
lda #$d507
sta h1
lda #$367c
sta h1+2
lda #$dd17
sta h2
lda #$3070
sta h2+2
lda #$5939
sta h3
lda #$f70e
sta h3+2
lda #$0b31
sta h4
lda #$ffc0
sta h4+2
lda #$1511
sta h5
lda #$6858
sta h5+2
lda #$8fa7
sta h6
lda #$64f9
sta h6+2
lda #$4fa4
sta h7
lda #$befa
sta h7+2
bra initdp
SHA256_INIT entry
lda #$e667 lda #$e667
sta h0 sta h0
lda #$6a09 lda #$6a09
@ -134,7 +177,7 @@ SHA256_INIT start
lda #$5be0 lda #$5be0
sta h7+2 sta h7+2
stz length initdp stz length
stz length+2 stz length+2
stz length+4 stz length+4
stz length+6 stz length+6

View File

@ -29,11 +29,19 @@ struct sha256_context {
*/ */
/* /*
* Initialize a SHA-256 context. * Initialize a context for SHA-256 computation.
* This must be called before any of the other SHA-256 functions. * An init function must be called before any of the other SHA-256 functions.
*/ */
void sha256_init(struct sha256_context *context); void sha256_init(struct sha256_context *context);
/*
* Initialize a context for SHA-224 computation.
* To compute a SHA-224 hash, call this function, and then call the below
* functions as if computing a SHA-256 hash. After calling sha256_finalize,
* the first 28 bytes of context->hash will contain the SHA-224 hash.
*/
void sha224_init(struct sha256_context *context);
/* /*
* Update a SHA-256 context based on the specified data. * Update a SHA-256 context based on the specified data.
*/ */

View File

@ -57,7 +57,8 @@ int main(int argc, char **argv) {
sha256_init(context); sha256_init(context);
sha256_processblock(context); sha256_processblock(context);
printf("h[..] = %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x "
printf("SHA-256:%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],
context->hash[7], context->hash[6], context->hash[5], context->hash[4], context->hash[7], context->hash[6], context->hash[5], context->hash[4],
@ -68,6 +69,23 @@ int main(int argc, char **argv) {
context->hash[27], context->hash[26], context->hash[25], context->hash[24], context->hash[27], context->hash[26], context->hash[25], context->hash[24],
context->hash[31], context->hash[30], context->hash[29], context->hash[28]); context->hash[31], context->hash[30], context->hash[29], context->hash[28]);
*context = context_init;
sha224_init(context);
sha256_processblock(context);
printf("SHA-224:%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],
context->hash[7], context->hash[6], context->hash[5], context->hash[4],
context->hash[11], context->hash[10], context->hash[9], context->hash[8],
context->hash[15], context->hash[14], context->hash[13], context->hash[12],
context->hash[19], context->hash[18], context->hash[17], context->hash[16],
context->hash[23], context->hash[22], context->hash[21], context->hash[20],
context->hash[27], context->hash[26], context->hash[25], context->hash[24]);
tick_count = GetTick(); tick_count = GetTick();
for (i = 0; i < 1000; i++) { for (i = 0; i < 1000; i++) {
sha256_processblock(context); sha256_processblock(context);