A2osX/LIB/LIBCRYPT.S.txt
2021-03-26 18:30:56 +01:00

594 lines
12 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NEW
AUTO 3,1
.LIST OFF
.OP 65C02
.OR $2000
.TF lib/libcrypt
*--------------------------------------
.INB inc/macros.i
.INB inc/a2osx.i
.INB inc/libcrypt.i
*--------------------------------------
* https://waterjuiceweb.wordpress.com/aes/
*--------------------------------------
.DUMMY
.OR ZPLIB
ZS.START
ZPCtxPtr .BS 2
ZPDataPtr .BS 2
ZPHashPtr .BS 2
ZPChunkLen .BS 2
MD4.ABCD
MD4.A .BS 4 Chunk Level,A,B,C,D
MD4.B .BS 4
MD4.C .BS 4
MD4.D .BS 4
MD4.F1 .BS 4
MD4.F2 .BS 4
ZS.END .ED
*--------------------------------------
* 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)
*--------------------------------------
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
*--------------------------------------
.1 .DA LIB.LOAD
.DA LIB.UNLOAD
.DA MD4
.DA MD4Init
.DA MD4Update
.DA MD4Finalize
J.MD4.XXXX .DA MD4.0015
.DA MD4.1631
.DA MD4.3247
.DA MD4.4863
.DA 0
*--------------------------------------
LIB.LOAD
LIB.UNLOAD clc
rts
*/--------------------------------------
* ## MD4
* Return MD4 Hash for input String
* # C
* `void MD4 (const char* str, char* digest);`
* # ASM
* `>PUSHW digest`
* `>PUSHW str`
* ## RETURN VALUE
* CC
*\--------------------------------------
MD4 >PULLW ZPDataPtr
>PULLW ZPHashPtr
>LDYA ZPDataPtr
>SYSCALL2 strlen
>STYA ZPDataLen
.1 jsr MD4Init
bcs .9
pha save MD4 Context hMem
jsr MD4Update.I
jsr MD4Finalize.I
pla
>SYSCALL2 FreeMem Free MD4 Context
clc
.9 rts
*/--------------------------------------
* ## MD4Init
* Initialize a MD4 computation
* # C
* `hMD4 MD4init();`
* # ASM
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD4Init`
* ## RETURN VALUE
* A = hMem To S.MD4
*\--------------------------------------
MD4Init >LDYAI S.MD4
>SYSCALL2 getmem
bcs .9
>STYA ZPCtxPtr
ldy #0
.1 lda MD4.ABCDINIT,y
sta (ZPCtxPtr),y
iny
cpy #S.MD4.BITCOUNT
bne .1
lda #0
.2 sta (ZPCtxPtr),y
iny
cpy #S.MD4 Includes S.MD4.FINALIZED Flag
bne .2
txa
clc
.9 rts
*/--------------------------------------
* ## MD4Update
* Add Data to MD4 computation
* # C
* `void MD4update (hMD4 MD4, char* data, int len);`
* # ASM
* `>PUSHW len`
* `>PUSHW data`
* `>LDA.G MD4`
* `>hLIBCRYPT,LIBCRYPT.MD4Update`
* ## RETURN VALUE
*\--------------------------------------
MD4Update >SYSCALL2 GetMemPtr get MD4 Context
>STYA ZPCtxPtr
>PULLW ZPDataPtr get DATA
>PULLW ZPDataLen get LEN
MD4Update.I ldy #S.MD4.FINALIZED
lda (ZPCtxPtr),y
beq .1
sec
rts
.1 lda ZPDataLen+1 More than 256 Bytes remaining to hash ?
bne .3 yes
lda ZPDataLen
bne .2 Len = O ?
clc All data processed
rts
.2 cmp #64 More than 64 Bytes remaining to hash ?
bcc .50 yes
.3 lda #64
.50 sta ZPChunkLen Save Chunk Len
jsr MD4UpdateBitCount
ldy #0
.51 lda (ZPDataPtr),y Load Buffer with data
sta MD4.BUFFER64,y
iny
cpy ZPChunkLen
bne .51
cpy #64 Full 64 Bytes DATA ?
beq .58
* Append "$80" & Padding & original size
lda #$80 no, Append $80
sta MD4.BUFFER64,y
lda #0
.52 iny Pad with 0 to 64
cpy #64
beq .53
sta MD4.BUFFER64,y
bra .52
.53 ldy #S.MD4.EXTRABIT Mark MD4 Context as Appended
lda #$80
sta (ZPCtxPtr),y
lda ZPChunkLen
cmp #56 Enough room for BITCOUNT ?
bcs .58 no
jsr MD4AppendBitCount
.58 jsr MD4Transform
jsr MD4UpdateABCD0
.8 lda ZPDataLen Substract Bytes processed from LEN
sec
sbc ZPChunkLen get back chunk Len
sta ZPDataLen
bcs .81
dec ZPDataLen+1
.81 lda ZPDataPtr Add Bytes processed to DATA
clc
adc ZPChunkLen
sta ZPDataPtr
bcc .1
inc ZPDataPtr+1
bra .1
*/--------------------------------------
* ## MD4Finalize
* # C
* `void MD4finalize (hMD4 MD4, char* digest);`
* # ASM
* `>PUSHW digest`
* `>LDA.G MD4`
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD4Finalize`
* ## RETURN VALUE
*\--------------------------------------
MD4Finalize pha
>SYSCALL2 GetMemPtr
>STYA ZPCtxPtr get MD4 Context
>PULLW ZPHashPtr
jsr MD4Finalize.I
pla
>SYSCALL2 FreeMem
rts
MD4Finalize.I ldy #S.MD4.FINALIZED
lda (ZPCtxPtr),y
bne .11
jsr MD4ExtraChunk
.11 ldy #S.MD4.ABCD0
.1 lda (ZPCtxPtr),y
lsr
lsr
lsr
lsr
tax
lda MD4.DIGITS,x
sta (ZPHashPtr)
inc ZPHashPtr
bne .2
inc ZPHashPtr+1
.2 lda (ZPCtxPtr),y
and #$0F
tax
lda MD4.DIGITS,x
sta (ZPHashPtr)
inc ZPHashPtr
bne .3
inc ZPHashPtr+1
.3 iny
cpy #S.MD4.ABCD0+16
bne .1
lda #0
sta (ZPHashPtr)
clc
.9 rts
*--------------------------------------
* PRIVATE
*--------------------------------------
* MD4AppendBitCount
* In :
* ZPCtxPtr = MD4 Context
*--------------------------------------
MD4AppendBitCount
ldx #56
ldy #S.MD4.BITCOUNT Append 8 Bytes of BITCOUNT
.1 lda (ZPCtxPtr),y
sta MD4.BUFFER64,x
inx
iny
cpy #S.MD4.BITCOUNT+8
bne .1
ldy #S.MD4.FINALIZED Mark MD4 Context as Finalized
lda #$80
sta (ZPCtxPtr),y
rts
*--------------------------------------
* MD4UpdateBitCount
* In :
* CL = Size (in bytes) to add to MD4 context
* ZPCtxPtr = MD4 Context
*--------------------------------------
MD4UpdateBitCount
stz ZPChunkLen+1
lda ZPChunkLen
asl
rol ZPChunkLen+1
asl times 8 to get bit count
rol ZPChunkLen+1
asl
rol ZPChunkLen+1
clc
ldy #S.MD4.BITCOUNT
adc (ZPCtxPtr),y
sta (ZPCtxPtr),y
iny
lda ZPChunkLen+1
adc (ZPCtxPtr),y
sta (ZPCtxPtr),y
bcc .8
.1 iny
lda (ZPCtxPtr),y
inc
sta (ZPCtxPtr),y
bne .8
cpy #7
bne .1
.8 rts
*--------------------------------------
* MD4ExtraChunk
* In :
* ZPCtxPtr = MD4 Context
*--------------------------------------
MD4ExtraChunk ldy #S.MD4.FINALIZED
lda #$80
sta (ZPCtxPtr),y Mark MD4 Context as "finalized"
ldx #0
ldy #S.MD4.EXTRABIT Extra Bit already appended ?
lda (ZPCtxPtr),y
bne .1
lda #$80
sta MD4.BUFFER64 Add extra bit to buffer
sta (ZPCtxPtr),y Mark Extrabit added
inx
.1 stz MD4.BUFFER64,x pad with "0"...
inx
cpx #56
bne .1 ...until room for bitcount
jsr MD4AppendBitCount
jsr MD4Transform
*--------------------------------------
* MD4UpdateABCD0
* In:
* ZPCtxPtr = MD4 Context
*--------------------------------------
MD4UpdateABCD0 ldy #0
.1 clc ADC32 A+A0->A0, B, C, D....
.2 lda MD4.ABCD,y
adc (ZPCtxPtr),y
sta (ZPCtxPtr),y
iny
tya
and #3
bne .2
cpy #16
bne .1
rts
*--------------------------------------
* MD4Transform
* In:
* ZPCtxPtr = MD4 Context
* 512 Bits Padded chunk in MD4.BUFFER64
* Out:
* Updated MD4.ABCD
* https://tools.ietf.org/html/rfc1320 MD4
* https://tools.ietf.org/html/rfc1321 MD5
*--------------------------------------
MD4Transform ldy #15 init ABCD Chunk with Context ABCD0
.1 lda (ZPCtxPtr),y
sta MD4.ABCD,y
dey
bpl .1
stz MD4.i Start Proceed 0 to 63 bytes
lda #0 lda MD4.i
.2 lsr
lsr
lsr
and #6
tax
jsr MD4.XXXX
inc MD4.i
lda MD4.i
cmp #64
bne .2
rts
*--------------------------------------
MD4.XXXX jmp (J.MD4.XXXX,x)
*--------------------------------------
MD4.0015 >AND32 MD4.B,MD4.C,MD4.F1
>NOT32 MD4.B,MD4.F2
>AND32 MD4.F2,MD4.D,MD4.F2
>ORA32 MD4.F1,MD4.F2,MD4.F
bra MD4.XXXX.END
*--------------------------------------
MD4.1631 >AND32 MD4.D,MD4.B,MD4.F1
>NOT32 MD4.D,MD4.F2
>AND32 MD4.F2,MD4.C,MD4.F2
>ORA32 MD4.F1,MD4.F2,MD4.F
bra MD4.XXXX.END
*--------------------------------------
MD4.3247 >EOR32 MD4.B,MD4.C,MD4.F
>EOR32 MD4.F,MD4.D,MD4.F
bra MD4.XXXX.END
*--------------------------------------
MD4.4863 >NOT32 MD4.D,MD4.F1
>ORA32 MD4.F1,MD4.B,MD4.F2
>EOR32 MD4.F2,MD4.C,MD4.F
*--------------------------------------
MD4.XXXX.END >MOV32 MD4.D,MD4.DTemp
>MOV32 MD4.C,MD4.D
>MOV32 MD4.B,MD4.C
>ADC32 MD4.A,MD4.F,MD4.F
lda MD4.i
asl
asl
tay
ldx #0
clc
.1 lda MD4.K,y ADC32 MD4.K[i],MD4.F,MD4.F
adc MD4.F,x
sta MD4.F,x
iny
inx
txa
eor #4 DO NOT DISTURB carry with cpx !!!
bne .1
ldy MD4.i
lda MD4.g,y
asl
asl
tay
ldx #0
clc
.2 lda MD4.BUFFER64,y ADC32 M[g],MD4.F,MD4.F
adc MD4.F,x
sta MD4.F,x
iny
inx
txa
eor #4 DO NOT DISTURB carry with cpx !!!
bne .2
ldy MD4.i
ldx MD4.s,y get s[i] in x
>ROL32x MD4.F
>ADC32 MD4.B,MD4.F,MD4.B
>MOV32 MD4.DTemp,MD4.A
rts
*--------------------------------------
CS.END
MD4.ABCDINIT .HS 01234567.89ABCDEF.FEDCBA98.76543210
*--------------------------------------
MD4.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
*--------------------------------------
MD4.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
*--------------------------------------
MD4.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
*--------------------------------------
MD4.SQRT2 .HS 5A827999
MD4.SQRT3 .HS 6ED9EBA1
MD4.DIGITS .AS "0123456789ABCDEF"
*--------------------------------------
ZPDataLen .BS 2
MD4.DTemp .BS 4
MD4.F .BS 4
MD4.i .BS 1
MD4.BUFFER64 .BS 64
*--------------------------------------
MAN
SAVE usr/src/lib/libcrypt.s
ASM