Add comments and copyright notices, and rename chunk to block.

This commit is contained in:
Stephen Heumann 2017-07-01 17:39:26 -05:00
parent b2d3d3ffa8
commit 0386f928ff
7 changed files with 164 additions and 21 deletions

View File

@ -1,3 +1,18 @@
* 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.
* Right-rotate 32-bit value in &loc (DP or 16-bit address) by &n positions
macro
ROTR4 &loc,&n

View File

@ -1,3 +1,23 @@
* 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.
* Implementation of the SHA-1 hash function for the 65816
*
* The basic structure of the hash computation is described in FIPS PUB 180-4,
* although this implementation rearranges some things for better performance.
case on
mcopy sha1.macros
@ -20,6 +40,8 @@ h4 gequ 56
w gequ 60
* Initialize a SHA-1 context.
* This must be called before any of the other SHA-1 functions.
sha1_init start
CFunction SHA1_INIT
end
@ -59,11 +81,13 @@ SHA1_INIT start
end
sha1_processchunk start
CFunction SHA1_PROCESSCHUNK
* Process one 64-byte block through the SHA-1 hashing function.
* This is a low-level function; users should normally not call this directly.
sha1_processblock start
CFunction SHA1_PROCESSBLOCK
end
SHA1_PROCESSCHUNK start
SHA1_PROCESSBLOCK start
lda h0
sta a_
lda h0+2

46
sha1.cc
View File

@ -1,3 +1,19 @@
/*
* 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
@ -10,8 +26,11 @@
#define hash_offset 40
#define data_offset 60
extern void SHA1_PROCESSCHUNK(void);
extern void SHA1_PROCESSBLOCK(void);
/*
* Update a SHA-1 context based on the specified data.
*/
void sha1_update(struct sha1_context *context,
const unsigned char *data,
unsigned long length)
@ -24,58 +43,61 @@ void sha1_update(struct sha1_context *context,
if (extra > 0) {
if (length >= 64 - extra) {
memcpy(&context->chunk[extra], data, 64 - extra);
memcpy(&context->block[extra], data, 64 - extra);
asm {
phd
lda context
tcd
jsl SHA1_PROCESSCHUNK
jsl SHA1_PROCESSBLOCK
pld
}
length -= 64 - extra;
data += 64 - extra;
} else {
memcpy(&context->chunk[extra], data, length);
memcpy(&context->block[extra], data, length);
context->extra += length;
return;
}
}
while (length >= 64) {
memcpy(&context->chunk, data, 64);
memcpy(&context->block, data, 64);
asm {
phd
lda context
tcd
jsl SHA1_PROCESSCHUNK
jsl SHA1_PROCESSBLOCK
pld
}
length -= 64;
data += 64;
}
memcpy(&context->chunk, data, length);
memcpy(&context->block, data, length);
context->extra = length;
}
/*
* Finish SHA-1 processing and generate the final hash code.
*/
void sha1_finalize(struct sha1_context *context)
{
unsigned int extra = context->extra;
context->chunk[extra++] = 0x80;
context->block[extra++] = 0x80;
memset(&context->chunk[extra], 0, 64 - extra);
memset(&context->block[extra], 0, 64 - extra);
if (extra > 64 - 8) {
asm {
phd
lda context
tcd
jsl SHA1_PROCESSCHUNK
jsl SHA1_PROCESSBLOCK
pld
}
memset(&context->chunk, 0, 64 - 8);
memset(&context->block, 0, 64 - 8);
}
asm {
@ -111,7 +133,7 @@ void sha1_finalize(struct sha1_context *context)
sta data_offset+62
/* Process final block */
jsl SHA1_PROCESSCHUNK
jsl SHA1_PROCESSBLOCK
/* Flip hash state words to big-endian order */
lda hash_offset

41
sha1.h
View File

@ -1,16 +1,51 @@
/*
* 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.
*/
struct sha1_context {
unsigned long length;
unsigned long length2;
unsigned short extra;
unsigned char reserved1[30];
unsigned char hash[20];
unsigned char chunk[64];
unsigned char block[64];
unsigned char reserved2[16];
};
void sha1_init(struct sha1_context *context);
void sha1_processchunk(struct sha1_context *context);
/*
* The context structure must be in bank 0, preferably page-aligned.
*/
/*
* Initialize a SHA-1 context.
* This must be called before any of the other SHA-1 functions.
*/
void sha1_init(struct sha1_context *context);
/*
* Update a SHA-1 context based on the specified data.
*/
void sha1_update(struct sha1_context *context, const unsigned char *data, unsigned long length);
/*
* Finish SHA-1 processing and generate the final hash code.
*/
void sha1_finalize(struct sha1_context *context);
/*
* Process one 64-byte block through the SHA-1 hashing function.
* This is a low-level function; users should normally not call this directly.
*/
void sha1_processblock(struct sha1_context *context);

View File

@ -1,3 +1,18 @@
* 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.
* Right-rotate 32-bit value in &loc (DP or 16-bit address) by &n positions
macro
ROTR4 &loc,&n
@ -169,7 +184,7 @@ dorotl3
ComputeSchedule &part
lcla &i
; Flip the endianness of W_0 to W_15 (the current chunk of the message)
; Flip the endianness of W_0 to W_15 (the current block of the message)
aif &part<>1,.skippart1
.loop1
lda w+&i*4

View File

@ -1,3 +1,19 @@
/*
* 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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

View File

@ -1,3 +1,19 @@
/*
* 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.
*/
#include "sha1.h"
#include <stdio.h>
#include <MiscTool.h>
@ -39,7 +55,7 @@ int main(int argc, char **argv) {
*context = context_init;
sha1_init(context);
sha1_processchunk(context);
sha1_processblock(context);
printf("h[..] = %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],
@ -50,7 +66,7 @@ int main(int argc, char **argv) {
tick_count = GetTick();
for (i = 0; i < 1000; i++) {
sha1_processchunk(context);
sha1_processblock(context);
}
tick_count = GetTick() - tick_count;