Add update and finalize functions for SHA-256.

This commit is contained in:
Stephen Heumann 2017-07-03 23:53:43 -05:00
parent 458e769212
commit e7aa261e19
2 changed files with 215 additions and 4 deletions

207
sha256.cc Normal file
View File

@ -0,0 +1,207 @@
/*
* Copyright (c) 2017 Stephen Heumann
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma noroot
#pragma lint -1
#pragma optimize -1
#include "sha256.h"
#include <string.h>
#define length_offset 0
#define extra_offset 8
#define hash_offset 60
#define data_offset 92
extern void SHA256_PROCESSBLOCK(void);
/*
* Update a SHA-256 context based on the specified data.
*/
void sha256_update(struct sha256_context *context,
const unsigned char *data,
unsigned long length)
{
unsigned int extra = context->extra;
unsigned long oldlength = context->length;
if ((context->length += length) < oldlength)
context->length2++;
if (extra > 0) {
if (length >= 64 - extra) {
memcpy(&context->block[extra], data, 64 - extra);
asm {
phd
lda context
tcd
jsl SHA256_PROCESSBLOCK
pld
}
length -= 64 - extra;
data += 64 - extra;
} else {
memcpy(&context->block[extra], data, length);
context->extra += length;
return;
}
}
while (length >= 64) {
memcpy(&context->block, data, 64);
asm {
phd
lda context
tcd
jsl SHA256_PROCESSBLOCK
pld
}
length -= 64;
data += 64;
}
memcpy(&context->block, data, length);
context->extra = length;
}
/*
* Finish SHA-256 processing and generate the final hash code.
*/
void sha256_finalize(struct sha256_context *context)
{
unsigned int extra = context->extra;
context->block[extra++] = 0x80;
memset(&context->block[extra], 0, 64 - extra);
if (extra > 64 - 8) {
asm {
phd
lda context
tcd
jsl SHA256_PROCESSBLOCK
pld
}
memset(&context->block, 0, 64 - 8);
}
asm {
phd
lda context
tcd
/* Append total length in bits */
asl length_offset
rol length_offset+2
rol length_offset+4
rol length_offset+6
asl length_offset
rol length_offset+2
rol length_offset+4
rol length_offset+6
asl length_offset
rol length_offset+2
rol length_offset+4
rol length_offset+6
lda length_offset+6
xba
sta data_offset+56
lda length_offset+4
xba
sta data_offset+58
lda length_offset+2
xba
sta data_offset+60
lda length_offset
xba
sta data_offset+62
/* Process final block */
jsl SHA256_PROCESSBLOCK
/* Flip hash state words to big-endian order */
lda hash_offset
xba
tay
lda hash_offset+2
xba
sta hash_offset
sty hash_offset+2
lda hash_offset+4
xba
tay
lda hash_offset+4+2
xba
sta hash_offset+4
sty hash_offset+4+2
lda hash_offset+8
xba
tay
lda hash_offset+8+2
xba
sta hash_offset+8
sty hash_offset+8+2
lda hash_offset+12
xba
tay
lda hash_offset+12+2
xba
sta hash_offset+12
sty hash_offset+12+2
lda hash_offset+16
xba
tay
lda hash_offset+16+2
xba
sta hash_offset+16
sty hash_offset+16+2
lda hash_offset+20
xba
tay
lda hash_offset+20+2
xba
sta hash_offset+20
sty hash_offset+20+2
lda hash_offset+24
xba
tay
lda hash_offset+24+2
xba
sta hash_offset+24
sty hash_offset+24+2
lda hash_offset+28
xba
tay
lda hash_offset+28+2
xba
sta hash_offset+28
sty hash_offset+28+2
pld
}
}
#append "sha256.asm"

View File

@ -94,7 +94,7 @@ int main(int argc, char **argv) {
bytes_per_sec = (long double)1000 * 64 * 60 / tick_count;
printf("Time for 1000 iters = %lu ticks (%lf bytes/sec)\n", tick_count, bytes_per_sec);
/*
tick_count = GetTick();
sha256_init(context);
sha256_update(context, (void*)0x030000, 64000);
@ -108,12 +108,16 @@ int main(int argc, char **argv) {
sha256_update(context, argv[1], strlen(argv[1]));
sha256_finalize(context);
printf("h[..] = %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\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[0], context->hash[1], context->hash[2], context->hash[3],
context->hash[4], context->hash[5], context->hash[6], context->hash[7],
context->hash[8], context->hash[9], context->hash[10], context->hash[11],
context->hash[12], context->hash[13], context->hash[14], context->hash[15],
context->hash[16], context->hash[17], context->hash[18], context->hash[19]);
context->hash[16], context->hash[17], context->hash[18], context->hash[19],
context->hash[20], context->hash[21], context->hash[22], context->hash[23],
context->hash[24], context->hash[25], context->hash[26], context->hash[27],
context->hash[28], context->hash[29], context->hash[30], context->hash[31]);
}
*/
}