add MD4 in LibCrypt

This commit is contained in:
Peter Ferrie 2021-06-05 18:35:30 -07:00
parent 8d350f1f29
commit 923b8847ce
2 changed files with 319 additions and 80 deletions

View File

@ -6,8 +6,12 @@ LIBCRYPT.MD4 .EQ 4
LIBCRYPT.MD4Init .EQ 6
LIBCRYPT.MD4Update .EQ 8
LIBCRYPT.MD4Finalize .EQ 10
LIBCRYPT.MD5 .EQ 12
LIBCRYPT.MD5Init .EQ LIBCRYPT.MD4Init
LIBCRYPT.MD5Update .EQ 14
LIBCRYPT.MD5Finalize .EQ LIBCRYPT.MD4Finalize
*--------------------------------------
* S.MD4 STRUCT
* S.MD4, S.MD5 STRUCT
*--------------------------------------
S.MD4.ABCD0 .EQ 0
S.MD4.A0 .EQ 0
@ -19,6 +23,17 @@ S.MD4.EXTRABIT .EQ 24
S.MD4.FINALIZED .EQ 25
S.MD4 .EQ 26
S.MD5.ABCD0 .EQ S.MD4.ABCD0
S.MD5.A0 .EQ S.MD4.A0
S.MD5.B0 .EQ S.MD4.B0
S.MD5.C0 .EQ S.MD4.C0
S.MD5.D0 .EQ S.MD4.D0
S.MD5.BITCOUNT .EQ S.MD4.BITCOUNT
S.MD5.EXTRABIT .EQ S.MD4.EXTRABIT
S.MD5.FINALIZED .EQ S.MD4.FINALIZED
S.MD5 .EQ S.MD4
*--------------------------------------
MAN
SAVE inc/libcrypt.i

View File

@ -20,28 +20,37 @@ 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
MD.MODE .BS 1
MD4.F1 .BS 4
MD4.F2 .BS 4
MD4.ABCD
MD5.ABCD
MD4.A
MD5.A .BS 4 Chunk Level,A,B,C,D
MD4.B
MD5.B .BS 4
MD4.C
MD5.C .BS 4
MD4.D
MD5.D .BS 4
MD4.F1
MD5.F1 .BS 4
MD4.F2
MD5.F2 .BS 4
ZS.END .ED
*--------------------------------------
* DWORD Macros : Do NOT disturb carry with cpx, cpy...
*--------------------------------------
.MA ADC32
ldx #3
ldy #0
ldy #3
ldx #0
clc
:1 lda ]1,y
adc ]2,y
sta ]3,y
iny
dex
:1 lda ]1,x
adc ]2,x
sta ]3,x
inx
dey
bpl :1
.EM
*--------------------------------------
@ -120,10 +129,15 @@ CS.START cld
.DA MD4Init
.DA MD4Update
.DA MD4Finalize
.DA MD5
.DA MD5Update
J.MD4.XXXX .DA MD4.0015
.DA MD4.1631
.DA MD4.3247
.DA MD4.4863
J.MD5.XXXX .DA MD5.0015
.DA MD5.1631
.DA MD5.3247
.DA MD5.4863
.DA 0
*--------------------------------------
LIB.LOAD
@ -137,10 +151,27 @@ LIB.UNLOAD clc
* # ASM
* `>PUSHW digest`
* `>PUSHW str`
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD4`
* ## RETURN VALUE
* CC
*\--------------------------------------
MD4 >PULLW ZPDataPtr
MD4 lda #0
.HS 2C BIT ABS
*/--------------------------------------
* ## MD5
* Return MD5 Hash for input String
* # C
* `void MD5 (const char* str, char* digest);`
* # ASM
* `>PUSHW digest`
* `>PUSHW str`
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD5`
* ## RETURN VALUE
* CC
*\--------------------------------------
MD5 lda #1
sta MD.MODE
>PULLW ZPDataPtr
>PULLW ZPHashPtr
>LDYA ZPDataPtr
>SYSCALL2 strlen
@ -148,24 +179,40 @@ MD4 >PULLW ZPDataPtr
.1 jsr MD4Init
bcs .9
pha save MD4 Context hMem
pha save MD4/MD5 Context hMem
lda MD.MODE
bne .2
jsr MD4Update.I
jsr MD4Finalize.I
bra .3
.2 jsr MD5Update.I
.3 jsr MD4Finalize.I
pla
>SYSCALL2 FreeMem Free MD4 Context
>SYSCALL2 FreeMem Free MD4/MD5 Context
clc
.9 rts
*/--------------------------------------
* ## MD4Init
* Initialize a MD4 computation
* # C
* `hMD4 MD4init();`
* `hMD4 MD5init();`
* # ASM
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD4Init`
* ## RETURN VALUE
* A = hMem To S.MD4
*\--------------------------------------
MD4Init >LDYAI S.MD4
*/--------------------------------------
* ## MD5Init
* Initialize a MD5 computation
* # C
* `hMD5 MD5init();`
* # ASM
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD5Init`
* ## RETURN VALUE
* A = hMem To S.MD5
*\--------------------------------------
MD4Init
MD5Init >LDYAI S.MD4
>SYSCALL2 getmem
bcs .9
@ -182,7 +229,7 @@ MD4Init >LDYAI S.MD4
.2 sta (ZPCtxPtr),y
iny
cpy #S.MD4 Includes S.MD4.FINALIZED Flag
cpy #S.MD4 Includes S.MD4/5.FINALIZED Flag
bne .2
txa
@ -200,12 +247,30 @@ MD4Init >LDYAI S.MD4
* `>hLIBCRYPT,LIBCRYPT.MD4Update`
* ## RETURN VALUE
*\--------------------------------------
MD4Update >SYSCALL2 GetMemPtr get MD4 Context
MD4Update lda #0
.HS 2C BIT ABS
*/--------------------------------------
* ## MD5Update
* Add Data to MD5 computation
* # C
* `void MD5update (hMD5 MD5, char* data, int len);`
* # ASM
* `>PUSHW len`
* `>PUSHW data`
* `>LDA.G MD5`
* `>hLIBCRYPT,LIBCRYPT.MD5Update`
* ## RETURN VALUE
*\--------------------------------------
MD5Update lda #1
sta MD.MODE
>SYSCALL2 GetMemPtr get MD5 Context
>STYA ZPCtxPtr
>PULLW ZPDataPtr get DATA
>PULLW ZPDataLen get LEN
MD4Update.I ldy #S.MD4.FINALIZED
MD4Update.I
MD5Update.I ldy #S.MD4.FINALIZED
lda (ZPCtxPtr),y
beq .1
@ -242,19 +307,18 @@ MD4Update.I ldy #S.MD4.FINALIZED
* Append "$80" & Padding & original size
lda #$80 no, Append $80
sta MD4.BUFFER64,y
.52 sta MD5.BUFFER64,y
lda #0
asl
* lda #0
.52 iny Pad with 0 to 64
iny Pad with 0 to 64
cpy #64
beq .53
bcc .52
sta MD4.BUFFER64,y
bra .52
.53 ldy #S.MD4.EXTRABIT Mark MD4 Context as Appended
lda #$80
ldy #S.MD4.EXTRABIT Mark MD5 Context as Appended
ror
* lda #$80
sta (ZPCtxPtr),y
lda ZPChunkLen
cmp #56 Enough room for BITCOUNT ?
@ -262,8 +326,13 @@ MD4Update.I ldy #S.MD4.FINALIZED
jsr MD4AppendBitCount
.58 jsr MD4Transform
jsr MD4UpdateABCD0
.58 lda MD.MODE
bne .59
jsr MD4Transform
bra .60
.59 jsr MD5Transform
.60 jsr MD4UpdateABCD0
.8 lda ZPDataLen Substract Bytes processed from LEN
sec
@ -291,16 +360,28 @@ MD4Update.I ldy #S.MD4.FINALIZED
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD4Finalize`
* ## RETURN VALUE
*\--------------------------------------
MD4Finalize pha
*/--------------------------------------
* ## MD5Finalize
* # C
* `void MD5finalize (hMD5 MD5, char* digest);`
* # ASM
* `>PUSHW digest`
* `>LDA.G MD5`
* `>LIBCALL hLIBCRYPT,LIBCRYPT.MD5Finalize`
* ## RETURN VALUE
*\--------------------------------------
MD4Finalize
MD5Finalize pha
>SYSCALL2 GetMemPtr
>STYA ZPCtxPtr get MD4 Context
>STYA ZPCtxPtr get MD5 Context
>PULLW ZPHashPtr
jsr MD4Finalize.I
pla
>SYSCALL2 FreeMem
rts
MD4Finalize.I ldy #S.MD4.FINALIZED
MD4Finalize.I
MD5Finalize.I ldy #S.MD4.FINALIZED
lda (ZPCtxPtr),y
bne .11
@ -347,7 +428,12 @@ MD4Finalize.I ldy #S.MD4.FINALIZED
* In :
* ZPCtxPtr = MD4 Context
*--------------------------------------
* MD5AppendBitCount
* In :
* ZPCtxPtr = MD5 Context
*--------------------------------------
MD4AppendBitCount
MD5AppendBitCount
ldx #56
ldy #S.MD4.BITCOUNT Append 8 Bytes of BITCOUNT
@ -358,7 +444,7 @@ MD4AppendBitCount
cpy #S.MD4.BITCOUNT+8
bne .1
ldy #S.MD4.FINALIZED Mark MD4 Context as Finalized
ldy #S.MD4.FINALIZED Mark MD5 Context as Finalized
lda #$80
sta (ZPCtxPtr),y
rts
@ -368,7 +454,14 @@ MD4AppendBitCount
* CL = Size (in bytes) to add to MD4 context
* ZPCtxPtr = MD4 Context
*--------------------------------------
*--------------------------------------
* MD5UpdateBitCount
* In :
* CL = Size (in bytes) to add to MD5 context
* ZPCtxPtr = MD5 Context
*--------------------------------------
MD4UpdateBitCount
MD5UpdateBitCount
stz ZPChunkLen+1
lda ZPChunkLen
asl
@ -402,9 +495,15 @@ MD4UpdateBitCount
* In :
* ZPCtxPtr = MD4 Context
*--------------------------------------
MD4ExtraChunk ldy #S.MD4.FINALIZED
*--------------------------------------
* MD5ExtraChunk
* In :
* ZPCtxPtr = MD5 Context
*--------------------------------------
MD4ExtraChunk
MD5ExtraChunk ldy #S.MD4.FINALIZED
lda #$80
sta (ZPCtxPtr),y Mark MD4 Context as "finalized"
sta (ZPCtxPtr),y Mark MD5 Context as "finalized"
ldx #0
ldy #S.MD4.EXTRABIT Extra Bit already appended ?
lda (ZPCtxPtr),y
@ -415,19 +514,30 @@ MD4ExtraChunk ldy #S.MD4.FINALIZED
sta (ZPCtxPtr),y Mark Extrabit added
inx
.1 stz MD4.BUFFER64,x pad with "0"...
.1 stz MD5.BUFFER64,x pad with "0"...
inx
cpx #56
bne .1 ...until room for bitcount
jsr MD4AppendBitCount
lda MD.MODE
bne .2
jsr MD4Transform
bra .3
.2 jsr MD5Transform
.3
*--------------------------------------
* MD4UpdateABCD0
* In:
* ZPCtxPtr = MD4 Context
*--------------------------------------
MD4UpdateABCD0 ldy #0
*--------------------------------------
* MD5UpdateABCD0
* In:
* ZPCtxPtr = MD5 Context
*--------------------------------------
MD4UpdateABCD0
MD5UpdateABCD0 ldy #0
.1 clc ADC32 A+A0->A0, B, C, D....
@ -451,7 +561,6 @@ MD4UpdateABCD0 ldy #0
* 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
@ -460,79 +569,81 @@ MD4Transform ldy #15 init ABCD Chunk with Context ABCD0
dey
bpl .1
stz MD4.i Start Proceed 0 to 63 bytes
stz MD4.i Start Proceed 0 to 47 bytes
lda #0 lda MD4.i
.2 lsr
lsr
lsr
and #6
tax
cmp #6
bne .3
lda #0
.3 tax
jsr MD4.XXXX
inc MD4.i
lda MD4.i
cmp #64
inc MD5.i
lda MD5.i
cmp #48
bne .2
rts
*--------------------------------------
MD4.XXXX jmp (J.MD4.XXXX,x)
*--------------------------------------
MD4.0015 >AND32 MD4.B,MD4.C,MD4.F1
>NOT32 MD4.B,MD4.F2
MD4.0015 >NOT32 MD4.B,MD4.F2
>AND32 MD4.B,MD4.C,MD4.F1
>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
MD4.1631 >AND32 MD4.B,MD4.C,MD4.F1
>AND32 MD4.B,MD4.D,MD4.F2
>ORA32 MD4.F1,MD4.F2,MD4.F1
>AND32 MD4.C,MD4.D,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
>ADC32 MD4.A,MD4.F,MD4.B
lda MD4.i
asl
asl
lsr
lsr
and #$0C
beq .3
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
.1 lda MD4.K-4,y ADC32 MD4.K[i],MD4.F,MD4.F
adc MD4.B,x
sta MD4.B,x
iny
inx
txa
eor #4 DO NOT DISTURB carry with cpx !!!
bne .1
.3 tax
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
adc MD4.B,x
sta MD4.B,x
iny
inx
txa
@ -542,25 +653,136 @@ MD4.XXXX.END >MOV32 MD4.D,MD4.DTemp
ldy MD4.i
ldx MD4.s,y get s[i] in x
>ROL32x MD4.F
>ADC32 MD4.B,MD4.F,MD4.B
>ROL32x MD4.B
>MOV32 MD4.DTemp,MD4.A
rts
*--------------------------------------
* MD5Transform
* In:
* ZPCtxPtr = MD5 Context
* 512 Bits Padded chunk in MD5.BUFFER64
* Out:
* Updated MD5.ABCD
* https://tools.ietf.org/html/rfc1321 MD5
*--------------------------------------
MD5Transform ldy #15 init ABCD Chunk with Context ABCD0
.1 lda (ZPCtxPtr),y
sta MD5.ABCD,y
dey
bpl .1
stz MD5.i Start Proceed 0 to 63 bytes
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
*--------------------------------------
MD5.XXXX jmp (J.MD5.XXXX,x)
*--------------------------------------
MD5.0015 >NOT32 MD5.B,MD5.F2
>AND32 MD5.B,MD5.C,MD5.F1
>AND32 MD5.F2,MD5.D,MD5.F2
>ORA32 MD5.F1,MD5.F2,MD5.F
bra MD5.XXXX.END
*--------------------------------------
MD5.1631 >NOT32 MD5.D,MD5.F2
>AND32 MD5.D,MD5.B,MD5.F1
>AND32 MD5.F2,MD5.C,MD5.F2
>ORA32 MD5.F1,MD5.F2,MD5.F
bra MD5.XXXX.END
*--------------------------------------
MD5.3247 >EOR32 MD5.B,MD5.C,MD5.F
>EOR32 MD5.F,MD5.D,MD5.F
bra MD5.XXXX.END
*--------------------------------------
MD5.4863 >NOT32 MD5.D,MD5.F1
>ORA32 MD5.F1,MD5.B,MD5.F2
>EOR32 MD5.F2,MD5.C,MD5.F
*--------------------------------------
MD5.XXXX.END >MOV32 MD5.D,MD5.DTemp
>MOV32 MD5.C,MD5.D
>MOV32 MD5.B,MD5.C
>ADC32 MD5.A,MD5.F,MD5.F
lda MD5.i
asl
asl
tay
ldx #0
clc
.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
tax
ldy MD5.i
lda MD5.g,y
asl
asl
tay
clc
.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
ldy MD5.i
ldx MD5.s,y get s[i] in x
>ROL32x MD5.F
>ADC32 MD5.B,MD5.F,MD5.B
>MOV32 MD5.DTemp,MD5.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
MD4.s .DA #3,#7,#11,#19,#3,#7,#11,#19,#3,#7,#11,#19,#3,#7,#11,#19
.DA #3,#5,#9,#13,#3,#5,#9,#13,#3,#5,#9,#13,#3,#5,#9,#13
.DA #3,#9,#11,#15,#3,#9,#11,#15,#3,#9,#11,#15,#3,#9,#11,#15
*--------------------------------------
MD4.g .DA #0,#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12,#13,#14,#15
.DA #0,#4,#8,#12,#1,#5,#9,#13,#2,#6,#10,#14,#3,#7,#11,#15
.DA #0,#8,#4,#12,#2,#10,#6,#14,#1,#9,#5,#13,#3,#11,#7,#15
*--------------------------------------
MD4.K .HS 9979825A.A1EBD96E
*--------------------------------------
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
*--------------------------------------
MD4.g .DA #0,#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12,#13,#14,#15
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
*--------------------------------------
MD4.K .HS 78A46AD7.56B7C7E8.DB702024.EECEBDC1
MD5.K .HS 78A46AD7.56B7C7E8.DB702024.EECEBDC1
.HS AF0F7CF5.2AC68747.134630A8.019546FD
.HS D8988069.AFF7448B.B15BFFFF.BED75C89
.HS 2211906B.937198FD.8E4379A6.2108B449
@ -577,16 +799,18 @@ MD4.K .HS 78A46AD7.56B7C7E8.DB702024.EECEBDC1
.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
MD4.DTemp
MD5.DTemp .BS 4
MD4.F
MD5.F .BS 4
MD4.i
MD5.i .BS 1
MD4.BUFFER64
MD5.BUFFER64 .BS 64
*--------------------------------------
MAN
SAVE usr/src/lib/libcrypt.s