From 0386f928ff95dafafdc03981fd325a46948a8f9e Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sat, 1 Jul 2017 17:39:26 -0500 Subject: [PATCH] Add comments and copyright notices, and rename chunk to block. --- rotate.macros | 15 +++++++++++++++ sha1.asm | 30 +++++++++++++++++++++++++++--- sha1.cc | 46 ++++++++++++++++++++++++++++++++++------------ sha1.h | 41 ++++++++++++++++++++++++++++++++++++++--- sha1.macros | 17 ++++++++++++++++- sha1sum.c | 16 ++++++++++++++++ sha1test.c | 20 ++++++++++++++++++-- 7 files changed, 164 insertions(+), 21 deletions(-) diff --git a/rotate.macros b/rotate.macros index a14d9b7..2360413 100644 --- a/rotate.macros +++ b/rotate.macros @@ -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 diff --git a/sha1.asm b/sha1.asm index 8f2dcf6..6c3c504 100644 --- a/sha1.asm +++ b/sha1.asm @@ -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 diff --git a/sha1.cc b/sha1.cc index 615f678..fa3cc50 100644 --- a/sha1.cc +++ b/sha1.cc @@ -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 diff --git a/sha1.h b/sha1.h index 39ebb12..83152f1 100644 --- a/sha1.h +++ b/sha1.h @@ -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); diff --git a/sha1.macros b/sha1.macros index 3d8a063..c0aaf35 100644 --- a/sha1.macros +++ b/sha1.macros @@ -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 diff --git a/sha1sum.c b/sha1sum.c index 32e916d..03337aa 100644 --- a/sha1sum.c +++ b/sha1sum.c @@ -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 #include #include diff --git a/sha1test.c b/sha1test.c index 0298ae6..f8f5742 100644 --- a/sha1test.c +++ b/sha1test.c @@ -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 #include @@ -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;