A2osX/LIB/LIBCRYPT.S.txt

592 lines
12 KiB
Plaintext
Raw Normal View History

2015-03-14 21:48:35 +00:00
NEW
2019-05-12 20:45:11 +00:00
AUTO 3,1
2015-03-14 21:48:35 +00:00
.LIST OFF
.OP 65C02
.OR $2000
2018-11-08 14:23:06 +00:00
.TF LIB/LIBCRYPT
2015-03-14 21:48:35 +00:00
*--------------------------------------
2018-07-23 15:28:42 +00:00
.INB INC/MACROS.I
.INB INC/A2OSX.I
.INB INC/LIBCRYPT.I
2015-03-14 21:48:35 +00:00
*--------------------------------------
2019-09-08 12:09:47 +00:00
.DUMMY
.OR ZPLIB
ZS.START
ZPCtxPtr .BS 2
ZPDataPtr .BS 2
ZPHashPtr .BS 2
ZPChunkLen .BS 2
MD5.ABCD
MD5.A .BS 4 Chunk Level,A,B,C,D
MD5.B .BS 4
MD5.C .BS 4
MD5.D .BS 4
MD5.F1 .BS 4
MD5.F2 .BS 4
ZS.END .ED
*--------------------------------------
2015-03-14 21:48:35 +00:00
* DWORD Macros : Do NOT disturb carry with cpx, cpy...
*--------------------------------------
.MA ADC32
ldx #3
ldy #0
clc
:1 lda ]1,y
adc ]2,y
sta ]3,y
iny
dex
bpl :1
.EM
*--------------------------------------
.MA AND32
ldx #3
:1 lda ]1,x
and ]2,x
sta ]3,x
dex
bpl :1
.EM
*--------------------------------------
.MA ORA32
ldx #3
:1 lda ]1,x
ora ]2,x
sta ]3,x
dex
bpl :1
.EM
*--------------------------------------
.MA EOR32
ldx #3
:1 lda ]1,x
eor ]2,x
sta ]3,x
dex
bpl :1
.EM
*--------------------------------------
.MA NOT32
ldx #3
:1 lda ]1,x
eor #$FF
sta ]2,x
dex
bpl :1
.EM
*--------------------------------------
.MA MOV32
ldx #3
:1 lda ]1,x
sta ]2,x
dex
bpl :1
.EM
*--------------------------------------
.MA ROL32x
:2 lda ]1+3
rol
rol ]1
rol ]1+1
rol ]1+2
rol ]1+3
dex
bne :2
.EM
*--------------------------------------
* File Header (16 Bytes)
2015-03-14 21:48:35 +00:00
*--------------------------------------
CS.START cld
jmp (.1,x)
.DA #$61 6502,Level 1 (65c02)
.DA #1 BIN Layout Version 1
.DA 0
.DA CS.END-CS.START
.DA 0
.DA 0
.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
2015-03-14 21:48:35 +00:00
.1 .DA LIB.LOAD
.DA LIB.UNLOAD
.DA MD5
2015-03-14 21:48:35 +00:00
.DA MD5Init
.DA MD5Update
.DA MD5Finalize
2019-09-08 12:09:47 +00:00
J.MD5.XXXX .DA MD5.0015
.DA MD5.1631
.DA MD5.3247
.DA MD5.4863
2015-03-14 21:48:35 +00:00
.DA 0
*--------------------------------------
LIB.LOAD
LIB.UNLOAD clc
rts
*/--------------------------------------
2018-06-14 15:31:36 +00:00
* ## MD5
* Return MD5 Hash for input String
* # C
* `void md5 (const char* str, char* digest);`
* # ASM
* `>PUSHW digest`
2018-11-12 11:04:59 +00:00
* `>PUSHW str`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-14 15:31:36 +00:00
* CC
*\--------------------------------------
2018-11-12 11:04:59 +00:00
MD5 >PULLW ZPDataPtr
2018-06-14 15:31:36 +00:00
>PULLW ZPHashPtr
2018-11-12 11:04:59 +00:00
>LDYA ZPDataPtr
2019-05-05 17:15:37 +00:00
>SYSCALL2 strlen
>STYA ZPDataLen
2015-03-14 21:48:35 +00:00
.1 jsr MD5Init
bcs .9
2015-03-14 21:48:35 +00:00
pha save MD5 Context hMem
jsr MD5Update.I
jsr MD5Finalize.I
2015-03-14 21:48:35 +00:00
pla
2019-05-05 17:15:37 +00:00
>SYSCALL2 FreeMem Free MD5 Context
2015-03-14 21:48:35 +00:00
clc
.9 rts
*/--------------------------------------
2018-06-14 15:31:36 +00:00
* ## MD5Init
* Initialize a MD5 computation
* # C
2018-11-12 11:04:59 +00:00
* `hMD5 md5init ();`
2018-06-14 15:31:36 +00:00
* # ASM
2018-11-12 11:04:59 +00:00
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD5Init`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-14 15:31:36 +00:00
* A = hMem To S.MD5
*\--------------------------------------
2018-06-14 15:31:36 +00:00
MD5Init >LDYAI S.MD5
2019-05-05 17:15:37 +00:00
>SYSCALL2 getmem
2015-03-14 21:48:35 +00:00
bcs .9
>STYA ZPCtxPtr
2015-03-14 21:48:35 +00:00
ldy #0
2015-03-14 21:48:35 +00:00
.1 lda MD5.ABCDINIT,y
sta (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
iny
2018-11-08 14:23:06 +00:00
cpy #S.MD5.BITCOUNT
2015-03-14 21:48:35 +00:00
bne .1
2015-03-14 21:48:35 +00:00
lda #0
.2 sta (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
iny
2018-11-08 14:23:06 +00:00
cpy #S.MD5 Includes S.MD5.FINALIZED Flag
2015-03-14 21:48:35 +00:00
bne .2
2015-03-14 21:48:35 +00:00
txa
clc
.9 rts
*/--------------------------------------
2018-06-14 15:31:36 +00:00
* ## MD5Update
* Add Data to MD5 computation
* # C
2018-11-12 11:04:59 +00:00
* `void md5update (hMD5 md5, char* data, int len);`
2018-06-14 15:31:36 +00:00
* # ASM
* `>PUSHW len`
* `>PUSHW data`
* `>LDA.G md5`
2018-11-12 11:04:59 +00:00
* `>hLIBCRYPT,LIBCRYPT.MD5Update`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
*\--------------------------------------
2019-05-05 17:15:37 +00:00
MD5Update >SYSCALL2 GetMemPtr get MD5 Context
2018-06-14 15:31:36 +00:00
>STYA ZPCtxPtr
>PULLW ZPDataPtr get DATA
>PULLW ZPDataLen get LEN
MD5Update.I ldy #S.MD5.FINALIZED
lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
beq .1
2015-03-14 21:48:35 +00:00
sec
rts
.1 lda ZPDataLen+1 More than 256 Bytes remaining to hash ?
2015-03-14 21:48:35 +00:00
bne .3 yes
lda ZPDataLen
2015-03-14 21:48:35 +00:00
bne .2 Len = O ?
clc All data processed
rts
2015-03-14 21:48:35 +00:00
.2 cmp #64 More than 64 Bytes remaining to hash ?
bcc .50 yes
2015-03-14 21:48:35 +00:00
.3 lda #64
.50 sta ZPChunkLen Save Chunk Len
2015-03-14 21:48:35 +00:00
jsr MD5UpdateBitCount
2015-03-14 21:48:35 +00:00
ldy #0
.51 lda (ZPDataPtr),y Load Buffer with data
2015-03-14 21:48:35 +00:00
sta MD5.BUFFER64,y
iny
cpy ZPChunkLen
2015-03-14 21:48:35 +00:00
bne .51
2015-03-14 21:48:35 +00:00
cpy #64 Full 64 Bytes DATA ?
beq .58
* Append "$80" & Padding & original size
2015-03-14 21:48:35 +00:00
lda #$80 no, Append $80
sta MD5.BUFFER64,y
2015-03-14 21:48:35 +00:00
lda #0
2015-03-14 21:48:35 +00:00
.52 iny Pad with 0 to 64
cpy #64
beq .53
sta MD5.BUFFER64,y
bra .52
2015-03-14 21:48:35 +00:00
.53 ldy #S.MD5.EXTRABIT Mark MD5 Context as Appended
lda #$80
sta (ZPCtxPtr),y
lda ZPChunkLen
2015-03-14 21:48:35 +00:00
cmp #56 Enough room for BITCOUNT ?
bcs .58 no
2015-03-14 21:48:35 +00:00
jsr MD5AppendBitCount
2015-03-14 21:48:35 +00:00
.58 jsr MD5Transform
jsr MD5UpdateABCD0
.8 lda ZPDataLen Substract Bytes processed from LEN
2015-03-14 21:48:35 +00:00
sec
sbc ZPChunkLen get back chunk Len
sta ZPDataLen
2015-03-14 21:48:35 +00:00
bcs .81
dec ZPDataLen+1
.81 lda ZPDataPtr Add Bytes processed to DATA
2015-03-14 21:48:35 +00:00
clc
adc ZPChunkLen
sta ZPDataPtr
bcc .1
inc ZPDataPtr+1
bra .1
*/--------------------------------------
2018-06-14 15:31:36 +00:00
* ## MD5Finalize
* # C
2018-11-12 11:04:59 +00:00
* `void md5finalize (hMD5 md5, char* digest);`
2018-06-14 15:31:36 +00:00
* # ASM
* `>PUSHW digest`
* `>LDA.G md5`
2018-11-12 11:04:59 +00:00
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD5Finalize`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
*\--------------------------------------
2018-11-12 11:04:59 +00:00
MD5Finalize pha
2019-05-05 17:15:37 +00:00
>SYSCALL2 GetMemPtr
>STYA ZPCtxPtr get MD5 Context
>PULLW ZPHashPtr
jsr MD5Finalize.I
pla
2019-05-05 17:15:37 +00:00
>SYSCALL2 FreeMem
rts
MD5Finalize.I ldy #S.MD5.FINALIZED
lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
bne .11
2015-03-14 21:48:35 +00:00
jsr MD5ExtraChunk
.11 ldy #S.MD5.ABCD0
.1 lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
lsr
lsr
lsr
lsr
tax
lda MD5.DIGITS,x
sta (ZPHashPtr)
inc ZPHashPtr
2015-03-14 21:48:35 +00:00
bne .2
inc ZPHashPtr+1
.2 lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
and #$0F
tax
lda MD5.DIGITS,x
sta (ZPHashPtr)
inc ZPHashPtr
2015-03-14 21:48:35 +00:00
bne .3
inc ZPHashPtr+1
2015-03-14 21:48:35 +00:00
.3 iny
cpy #S.MD5.ABCD0+16
bne .1
lda #0
sta (ZPHashPtr)
2015-03-14 21:48:35 +00:00
clc
.9 rts
*--------------------------------------
* PRIVATE
*--------------------------------------
* MD5AppendBitCount
* In :
* ZPCtxPtr = MD5 Context
2015-03-14 21:48:35 +00:00
*--------------------------------------
MD5AppendBitCount
ldx #56
ldy #S.MD5.BITCOUNT Append 8 Bytes of BITCOUNT
2019-09-08 12:09:47 +00:00
.1 lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
sta MD5.BUFFER64,x
inx
iny
cpy #S.MD5.BITCOUNT+8
bne .1
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
ldy #S.MD5.FINALIZED Mark MD5 Context as Finalized
lda #$80
sta (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
rts
*--------------------------------------
* MD5UpdateBitCount
* In :
* CL = Size (in bytes) to add to MD5 context
* ZPCtxPtr = MD5 Context
2015-03-14 21:48:35 +00:00
*--------------------------------------
MD5UpdateBitCount
stz ZPChunkLen+1
lda ZPChunkLen
2015-03-14 21:48:35 +00:00
asl
rol ZPChunkLen+1
2015-03-14 21:48:35 +00:00
asl times 8 to get bit count
rol ZPChunkLen+1
2015-03-14 21:48:35 +00:00
asl
rol ZPChunkLen+1
2015-03-14 21:48:35 +00:00
clc
ldy #S.MD5.BITCOUNT
adc (ZPCtxPtr),y
sta (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
iny
lda ZPChunkLen+1
adc (ZPCtxPtr),y
sta (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
bcc .8
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
.1 iny
lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
inc
sta (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
bne .8
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
cpy #7
bne .1
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
.8 rts
*--------------------------------------
* MD5ExtraChunk
* In :
* ZPCtxPtr = MD5 Context
2015-03-14 21:48:35 +00:00
*--------------------------------------
MD5ExtraChunk ldy #S.MD5.FINALIZED
lda #$80
sta (ZPCtxPtr),y Mark MD5 Context as "finalized"
2015-03-14 21:48:35 +00:00
ldx #0
ldy #S.MD5.EXTRABIT Extra Bit already appended ?
lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
bne .1
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
lda #$80
sta MD5.BUFFER64 Add extra bit to buffer
sta (ZPCtxPtr),y Mark Extrabit added
2015-03-14 21:48:35 +00:00
inx
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
.1 stz MD5.BUFFER64,x pad with "0"...
inx
cpx #56
bne .1 ...until room for bitcount
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
jsr MD5AppendBitCount
jsr MD5Transform
*--------------------------------------
* MD5UpdateABCD0
* In:
* ZPCtxPtr = MD5 Context
2015-03-14 21:48:35 +00:00
*--------------------------------------
MD5UpdateABCD0 ldy #0
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
.1 clc ADC32 A+A0->A0, B, C, D....
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
.2 lda MD5.ABCD,y
adc (ZPCtxPtr),y
sta (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
iny
tya
and #3
bne .2
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
cpy #16
bne .1
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
rts
*--------------------------------------
* MD5Transform
* In:
* ZPCtxPtr = MD5 Context
2015-03-14 21:48:35 +00:00
* 512 Bits Padded chunk in MD5.BUFFER64
* Out:
* Updated MD5.ABCD
*--------------------------------------
MD5Transform ldy #15 init ABCD Chunk with Context ABCD0
2019-09-08 12:09:47 +00:00
.1 lda (ZPCtxPtr),y
2015-03-14 21:48:35 +00:00
sta MD5.ABCD,y
dey
bpl .1
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
stz MD5.i Start Proceed 0 to 63 bytes
2019-09-08 12:09:47 +00:00
lda #0 lda MD5.i
.2 lsr
lsr
lsr
and #6
tax
jsr MD5.XXXX
inc MD5.i
lda MD5.i
cmp #64
bne .2
rts
2015-03-14 21:48:35 +00:00
*--------------------------------------
2019-09-08 12:09:47 +00:00
MD5.XXXX jmp (J.MD5.XXXX,x)
*--------------------------------------
MD5.0015 >AND32 MD5.B,MD5.C,MD5.F1
2015-03-14 21:48:35 +00:00
>NOT32 MD5.B,MD5.F2
>AND32 MD5.F2,MD5.D,MD5.F2
>ORA32 MD5.F1,MD5.F2,MD5.F
2019-09-08 12:09:47 +00:00
bra MD5.XXXX.END
2015-03-14 21:48:35 +00:00
*--------------------------------------
2019-09-08 12:09:47 +00:00
MD5.1631 >AND32 MD5.D,MD5.B,MD5.F1
2015-03-14 21:48:35 +00:00
>NOT32 MD5.D,MD5.F2
>AND32 MD5.F2,MD5.C,MD5.F2
>ORA32 MD5.F1,MD5.F2,MD5.F
2019-09-08 12:09:47 +00:00
bra MD5.XXXX.END
2015-03-14 21:48:35 +00:00
*--------------------------------------
2019-09-08 12:09:47 +00:00
MD5.3247 >EOR32 MD5.B,MD5.C,MD5.F
2015-03-14 21:48:35 +00:00
>EOR32 MD5.F,MD5.D,MD5.F
2019-09-08 12:09:47 +00:00
bra MD5.XXXX.END
2015-03-14 21:48:35 +00:00
*--------------------------------------
2019-09-08 12:09:47 +00:00
MD5.4863 >NOT32 MD5.D,MD5.F1
2015-03-14 21:48:35 +00:00
>ORA32 MD5.F1,MD5.B,MD5.F2
>EOR32 MD5.F2,MD5.C,MD5.F
*--------------------------------------
2019-09-08 12:09:47 +00:00
MD5.XXXX.END >MOV32 MD5.D,MD5.DTemp
2015-03-14 21:48:35 +00:00
>MOV32 MD5.C,MD5.D
>MOV32 MD5.B,MD5.C
>ADC32 MD5.A,MD5.F,MD5.F
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
lda MD5.i
asl
asl
tay
ldx #0
clc
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
.1 lda MD5.K,y ADC32 MD5.K[i],MD5.F,MD5.F
adc MD5.F,x
sta MD5.F,x
iny
inx
txa
eor #4 DO NOT DISTURB carry with cpx !!!
bne .1
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
ldy MD5.i
lda MD5.g,y
asl
asl
tay
ldx #0
clc
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
.2 lda MD5.BUFFER64,y ADC32 M[g],MD5.F,MD5.F
adc MD5.F,x
sta MD5.F,x
iny
inx
txa
eor #4 DO NOT DISTURB carry with cpx !!!
bne .2
2019-09-08 12:09:47 +00:00
2015-03-14 21:48:35 +00:00
ldy MD5.i
2019-09-08 12:09:47 +00:00
ldx MD5.s,y get s[i] in x
2015-03-14 21:48:35 +00:00
>ROL32x MD5.F
>ADC32 MD5.B,MD5.F,MD5.B
>MOV32 MD5.DTemp,MD5.A
2019-09-08 12:09:47 +00:00
rts
2015-03-14 21:48:35 +00:00
*--------------------------------------
CS.END
MD5.ABCDINIT .HS 01234567.89ABCDEF.FEDCBA98.76543210
*--------------------------------------
MD5.s .DA #7,#12,#17,#22,#7,#12,#17,#22,#7,#12,#17,#22,#7,#12,#17,#22
.DA #5,#9,#14,#20,#5,#9,#14,#20,#5,#9,#14,#20,#5,#9,#14,#20
.DA #4,#11,#16,#23,#4,#11,#16,#23,#4,#11,#16,#23,#4,#11,#16,#23
.DA #6,#10,#15,#21,#6,#10,#15,#21,#6,#10,#15,#21,#6,#10,#15,#21
*--------------------------------------
MD5.g .DA #0,#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12,#13,#14,#15
.DA #1,#6,#11,#0,#5,#10,#15,#4,#9,#14,#3,#8,#13,#2,#7,#12
.DA #5,#8,#11,#14,#1,#4,#7,#10,#13,#0,#3,#6,#9,#12,#15,#2
.DA #0,#7,#14,#5,#12,#3,#10,#1,#8,#15,#6,#13,#4,#11,#2,#9
*--------------------------------------
MD5.K .HS 78A46AD7.56B7C7E8.DB702024.EECEBDC1
.HS AF0F7CF5.2AC68747.134630A8.019546FD
.HS D8988069.AFF7448B.B15BFFFF.BED75C89
.HS 2211906B.937198FD.8E4379A6.2108B449
.HS 62251EF6.40B340C0.515A5E26.AAC7B6E9
.HS 5D102FD6.53144402.81E6A1D8.C8FBD3E7
.HS E6CDE121.D60737C3.870DD5F4.ED145A45
.HS 05E9E3A9.F8A3EFFC.D9026F67.8A4C2A8D
.HS 4239FAFF.81F67187.22619D6D.0C38E5FD
.HS 44EABEA4.A9CFDE4B.604BBBF6.70BCBFBE
.HS C67E9B28.FA27A1EA.8530EFD4.051D8804
.HS 39D0D4D9.E599DBE6.F87CA21F.6556ACC4
.HS 442229F4.97FF2A43.A72394AB.39A093FC
.HS C3595B65.92CC0C8F.7DF4EFFF.D15D8485
.HS 4F7EA86F.E0E62CFE.144301A3.A111084E
.HS 827E53F7.35F23ABD.BBD2D72A.91D386EB
*--------------------------------------
MD5.DIGITS .AS "0123456789ABCDEF"
*--------------------------------------
2019-09-08 12:09:47 +00:00
ZPDataLen .BS 2
MD5.DTemp .BS 4
MD5.F .BS 4
MD5.i .BS 1
MD5.BUFFER64 .BS 64
*--------------------------------------
2015-03-14 21:48:35 +00:00
MAN
2018-11-17 17:17:13 +00:00
SAVE USR/SRC/LIB/LIBCRYPT.S
2015-03-14 21:48:35 +00:00
ASM