NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/hmacmd5
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/mli.e.i
				.INB inc/libcrypt.i
*--------------------------------------
FILEBUF.SIZE	.EQ 4096
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPDataBufPtr	.BS 2
ZPDataLen		.BS 2
ArgIndex		.BS 1
aOptD				.BS 1
aOptK				.BS 1
bText			.BS 1
bGotKey			.BS 1
hFile			.BS 1
hDataBuf		.BS 1
hHMACMD5Ctx		.BS 1

ZS.END			.ED
*--------------------------------------
*			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		CS
				.DA DS.END-DS.START		DS
				.DA #16					SS
				.DA #ZS.END-ZS.START	ZP
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.LIBCRYPT		.DA LIBCRYPT
L.MSG.USAGE		.DA MSG.USAGE
L.HMACMD5.IPAD		.DA HMACMD5.IPAD
L.HMACMD5.OPAD		.DA HMACMD5.OPAD
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBCRYPT
				>SYSCALL LoadLib
				bcs .9

				sta hLIBCRYPT

*				clc
.9				rts
*--------------------------------------
CS.RUN			ldy #S.PS.ARGC
				lda (pPS),y
				beq .99
.10				lda #0
				pha

.1				inc ArgIndex
				lda ArgIndex

				>SYSCALL ArgV
				plx
				bcs .8

				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #'-'
				bne .4
				ldy #1
				lda (ZPPtr1),y
				beq .99

				ldy #OptionEnd-OptionList-1

.2				cmp OptionList,y
				beq .3
				dey
				bpl .2

.99				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL PrintF
				lda #E.SYN
				sec
				rts

.3				tya
				lsr
				pha
				tax
				sec
				ror bText,x
				bra .1

.4				lda ArgIndex
				sta aOptD,x
				bra .10

.8				lda bGotKey
				beq .99

				lda bText
				beq CS.RUN.FILE
*--------------------------------------
CS.RUN.TEXT		lda aOptD
				>SYSCALL ArgV
				>PUSHYA
				lda aOptK
				>SYSCALL ArgV
				>PUSHYA
				>PUSHEA.G HMACMD5Buf
				>LIBCALL hLIBCRYPT,LIBCRYPT.HMACMD5
				jmp CS.RUN.PRINT
*--------------------------------------
CS.RUN.FILE		>LDYAI FILEBUF.SIZE
				>SYSCALL GetMem
				bcs .9

				>STYA ZPDataBufPtr
				stx hDataBuf

				lda aOptD
				>SYSCALL ArgV
				>PUSHYA
				>PUSHBI	O.RDONLY
				>PUSHBI 0				ftype
				>PUSHWZ					Aux type
				>SYSCALL FOpen
				bcc .1

.9				rts

.1				sta hFile

				lda aOptK
				>SYSCALL ArgV
				>STYA ZPDataBufPtr
				>SYSCALL2 strlen
				>STYA ZPDataLen
				cpy #65
				bcs .2
				tay
				beq .3

.2				>SYSCALL MD5Init
				bcs .9

				sta hHMACMD5Ctx

				>SLEEP

				>PUSHB hHMACMD5Ctx
				lda aOptK
				>SYSCALL ArgV
				>PUSHYA
				>PUSHW ZPDataLen
				>SYSCALL MD5Update
				>PUSHB hHMACMD5Ctx
				>PUSHEA.G HMACMD5Buf
				>SYSCALL MD5Finalize

				jsr RevertASCII

.3				ldy #0
.4				lda #$36
				cpy ZPDataLen
				bcs .5
				eor (ZPDataBufPtr),y
.5				sta HMACMD5.IPAD,y
				eor #$6A			#$36 eor #$5C
				sta HMACMD5.OPAD,y
				iny
				cpy #64
				bne .4
				stz HMACMD5.IPAD+64
				stz HMACMD5.OPAD+64

rebase
				>SYSCALL MD5Init
				bcc .6

				rts

.6				sta hHMACMD5Ctx

				>PUSHB hHMACMD5Ctx
				>PUSHW L.HMACMD5.IPAD
				>PUSHWI 64
				>SYSCALL MD5Update

.7				>SLEEP

				>PUSHB hFile
				>PUSHW ZPDataBufPtr		Dst Ptr
				>PUSHWI FILEBUF.SIZE	Bytes To Read
				>SYSCALL FRead
				bcc .8

				cmp #MLI.E.EOF
				beq .10

.99				rts

.8				>STYA ZPDataLen

				>SLEEP

				>PUSHB hHMACMD5Ctx
				>PUSHW ZPDataBufPtr
				>PUSHW ZPDataLen
				>SYSCALL MD5Update
				bra .7

.10				>PUSHB hHMACMD5Ctx
				>PUSHEA.G HMACMD5Buf
				>SYSCALL MD5Finalize

				jsr RevertASCII

				>SYSCALL MD5Init
				bcs .99

				sta hHMACMD5Ctx

				>PUSHB hHMACMD5Ctx
				>PUSHW L.HMACMD5.OPAD
				>PUSHWI 64
				>SYSCALL MD5Update

				>PUSHB hHMACMD5Ctx
				>PUSHW L.HMACMD5.IPAD
				>PUSHWI 16
				>SYSCALL MD5Update

				>PUSHB hHMACMD5Ctx
				>PUSHEA.G HMACMD5Buf
				>SYSCALL MD5Finalize

CS.RUN.PRINT	>LEA.G HMACMD5Buf
				>SYSCALL PutS
				lda #0
				sec
				rts

*--------------------------------------
RevertASCII		>LEA.G HMACMD5Buf
				>STYA ZPDataBufPtr
				ldx #16
				stx ZPDataLen
				ldy #31
.1				lda (ZPDataBufPtr),y
				cmp #$3A
				bcc .2
				sbc #7
.2				and #$0f
				sta bText
				dey
				lda (ZPDataBufPtr),y
				cmp #$3A
				bcc .3
				sbc #7
.3				asl
				asl
				asl
				asl
				ora bText
				sta HMACMD5.IPAD-1,x
				dex
				dey
				bpl .1
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			lda hLIBCRYPT
				beq .1

				>SYSCALL UnloadLib

.1				lda hDataBuf
				beq .2

				>SYSCALL FreeMem
.2				lda hFile
				beq .3

				>SYSCALL FClose

.3				clc
				rts
*--------------------------------------
CS.END
LIBCRYPT		.AZ "libcrypt"
hLIBCRYPT		.BS 1
*--------------------------------------
OptionList		.AS "DdKk"
OptionEnd
*--------------------------------------
MSG.USAGE		.CZ "Usage : HMACMD5 [ -d input text | input file ] [ -k key ]\r\n"
*--------------------------------------
HMACMD5.IPAD	.BS 65
HMACMD5.OPAD	.BS 65
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
HMACMD5Buf		.BS 33
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/hmacmd5.s
ASM
