Add comments and copyright notices, and rename chunk to block.
This commit is contained in:
parent
b2d3d3ffa8
commit
0386f928ff
|
@ -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
|
* Right-rotate 32-bit value in &loc (DP or 16-bit address) by &n positions
|
||||||
macro
|
macro
|
||||||
ROTR4 &loc,&n
|
ROTR4 &loc,&n
|
||||||
|
|
30
sha1.asm
30
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
|
case on
|
||||||
mcopy sha1.macros
|
mcopy sha1.macros
|
||||||
|
|
||||||
|
@ -20,6 +40,8 @@ h4 gequ 56
|
||||||
w gequ 60
|
w gequ 60
|
||||||
|
|
||||||
|
|
||||||
|
* Initialize a SHA-1 context.
|
||||||
|
* This must be called before any of the other SHA-1 functions.
|
||||||
sha1_init start
|
sha1_init start
|
||||||
CFunction SHA1_INIT
|
CFunction SHA1_INIT
|
||||||
end
|
end
|
||||||
|
@ -59,11 +81,13 @@ SHA1_INIT start
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
sha1_processchunk start
|
* Process one 64-byte block through the SHA-1 hashing function.
|
||||||
CFunction SHA1_PROCESSCHUNK
|
* This is a low-level function; users should normally not call this directly.
|
||||||
|
sha1_processblock start
|
||||||
|
CFunction SHA1_PROCESSBLOCK
|
||||||
end
|
end
|
||||||
|
|
||||||
SHA1_PROCESSCHUNK start
|
SHA1_PROCESSBLOCK start
|
||||||
lda h0
|
lda h0
|
||||||
sta a_
|
sta a_
|
||||||
lda h0+2
|
lda h0+2
|
||||||
|
|
46
sha1.cc
46
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 noroot
|
||||||
#pragma lint -1
|
#pragma lint -1
|
||||||
#pragma optimize -1
|
#pragma optimize -1
|
||||||
|
@ -10,8 +26,11 @@
|
||||||
#define hash_offset 40
|
#define hash_offset 40
|
||||||
#define data_offset 60
|
#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,
|
void sha1_update(struct sha1_context *context,
|
||||||
const unsigned char *data,
|
const unsigned char *data,
|
||||||
unsigned long length)
|
unsigned long length)
|
||||||
|
@ -24,58 +43,61 @@ void sha1_update(struct sha1_context *context,
|
||||||
|
|
||||||
if (extra > 0) {
|
if (extra > 0) {
|
||||||
if (length >= 64 - extra) {
|
if (length >= 64 - extra) {
|
||||||
memcpy(&context->chunk[extra], data, 64 - extra);
|
memcpy(&context->block[extra], data, 64 - extra);
|
||||||
asm {
|
asm {
|
||||||
phd
|
phd
|
||||||
lda context
|
lda context
|
||||||
tcd
|
tcd
|
||||||
jsl SHA1_PROCESSCHUNK
|
jsl SHA1_PROCESSBLOCK
|
||||||
pld
|
pld
|
||||||
}
|
}
|
||||||
length -= 64 - extra;
|
length -= 64 - extra;
|
||||||
data += 64 - extra;
|
data += 64 - extra;
|
||||||
} else {
|
} else {
|
||||||
memcpy(&context->chunk[extra], data, length);
|
memcpy(&context->block[extra], data, length);
|
||||||
context->extra += length;
|
context->extra += length;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (length >= 64) {
|
while (length >= 64) {
|
||||||
memcpy(&context->chunk, data, 64);
|
memcpy(&context->block, data, 64);
|
||||||
asm {
|
asm {
|
||||||
phd
|
phd
|
||||||
lda context
|
lda context
|
||||||
tcd
|
tcd
|
||||||
jsl SHA1_PROCESSCHUNK
|
jsl SHA1_PROCESSBLOCK
|
||||||
pld
|
pld
|
||||||
}
|
}
|
||||||
length -= 64;
|
length -= 64;
|
||||||
data += 64;
|
data += 64;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(&context->chunk, data, length);
|
memcpy(&context->block, data, length);
|
||||||
context->extra = length;
|
context->extra = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Finish SHA-1 processing and generate the final hash code.
|
||||||
|
*/
|
||||||
void sha1_finalize(struct sha1_context *context)
|
void sha1_finalize(struct sha1_context *context)
|
||||||
{
|
{
|
||||||
unsigned int extra = context->extra;
|
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) {
|
if (extra > 64 - 8) {
|
||||||
asm {
|
asm {
|
||||||
phd
|
phd
|
||||||
lda context
|
lda context
|
||||||
tcd
|
tcd
|
||||||
jsl SHA1_PROCESSCHUNK
|
jsl SHA1_PROCESSBLOCK
|
||||||
pld
|
pld
|
||||||
}
|
}
|
||||||
memset(&context->chunk, 0, 64 - 8);
|
memset(&context->block, 0, 64 - 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
asm {
|
asm {
|
||||||
|
@ -111,7 +133,7 @@ void sha1_finalize(struct sha1_context *context)
|
||||||
sta data_offset+62
|
sta data_offset+62
|
||||||
|
|
||||||
/* Process final block */
|
/* Process final block */
|
||||||
jsl SHA1_PROCESSCHUNK
|
jsl SHA1_PROCESSBLOCK
|
||||||
|
|
||||||
/* Flip hash state words to big-endian order */
|
/* Flip hash state words to big-endian order */
|
||||||
lda hash_offset
|
lda hash_offset
|
||||||
|
|
41
sha1.h
41
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 {
|
struct sha1_context {
|
||||||
unsigned long length;
|
unsigned long length;
|
||||||
unsigned long length2;
|
unsigned long length2;
|
||||||
unsigned short extra;
|
unsigned short extra;
|
||||||
unsigned char reserved1[30];
|
unsigned char reserved1[30];
|
||||||
unsigned char hash[20];
|
unsigned char hash[20];
|
||||||
unsigned char chunk[64];
|
unsigned char block[64];
|
||||||
unsigned char reserved2[16];
|
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);
|
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);
|
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);
|
||||||
|
|
17
sha1.macros
17
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
|
* Right-rotate 32-bit value in &loc (DP or 16-bit address) by &n positions
|
||||||
macro
|
macro
|
||||||
ROTR4 &loc,&n
|
ROTR4 &loc,&n
|
||||||
|
@ -169,7 +184,7 @@ dorotl3
|
||||||
ComputeSchedule &part
|
ComputeSchedule &part
|
||||||
lcla &i
|
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
|
aif &part<>1,.skippart1
|
||||||
.loop1
|
.loop1
|
||||||
lda w+&i*4
|
lda w+&i*4
|
||||||
|
|
16
sha1sum.c
16
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 <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
20
sha1test.c
20
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 "sha1.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <MiscTool.h>
|
#include <MiscTool.h>
|
||||||
|
@ -39,7 +55,7 @@ int main(int argc, char **argv) {
|
||||||
*context = context_init;
|
*context = context_init;
|
||||||
|
|
||||||
sha1_init(context);
|
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",
|
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],
|
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();
|
tick_count = GetTick();
|
||||||
for (i = 0; i < 1000; i++) {
|
for (i = 0; i < 1000; i++) {
|
||||||
sha1_processchunk(context);
|
sha1_processblock(context);
|
||||||
}
|
}
|
||||||
tick_count = GetTick() - tick_count;
|
tick_count = GetTick() - tick_count;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue