ADDCRED	equ "RFC6238-ASCII ", <db "12345678901234567890">, 8
ADDCRED	equ "RFC6238-BASE32", <BASE32_DECODE "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ">, 8
ADDCRED	equ "RFC6238-HEX   ", <db 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30>, 8

; ADDCRED equ     "MY_TOTP",   <BASE32_DECODE "MZQXG3JAKRHVIUBAMNXWIZJAM5SW4ZLSMF2G64Q=">,        6,                    30
;             |   ^^^^^^^^^^   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^           | ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^ |
;             | give it a name |         shared secret in base32, hex, or text           | output digits 1-9 | step time in seconds |
; The last two parameters are optional and default to 6 digits output and 30 seconds step time
; The algorithm used is SHA1. No support for SHA256/SHA512
; The time offset is fixed at 0
; The shared secret must be <= 64 bytes after converstion to binary

; see	https://www.rfc-editor.org/info/rfc6238/ TOTP
;	https://www.rfc-editor.org/info/rfc4226/ HOTP
;	https://www.rfc-editor.org/info/rfc3174/ SHA1
;	https://www.rfc-editor.org/info/rfc2104/ HMAC

;	test vectors from rfc6238
; for T in 59 1111111109 1111111111 1234567890 2000000000 20000000000 ; do date -ud @$T -Is ; fasm totp.asm -d TIMEOVERRIDE=$T > /dev/null && ./totp ; done
;	output
; 1970-01-01T00:00:59+00:00
; RFC6238-ASCII : 94287082
; RFC6238-BASE32: 94287082
; RFC6238-HEX   : 94287082
; 2005-03-18T01:58:29+00:00
; RFC6238-ASCII : 07081804
; RFC6238-BASE32: 07081804
; RFC6238-HEX   : 07081804
; 2005-03-18T01:58:31+00:00
; RFC6238-ASCII : 14050471
; RFC6238-BASE32: 14050471
; RFC6238-HEX   : 14050471
; 2009-02-13T23:31:30+00:00
; RFC6238-ASCII : 89005924
; RFC6238-BASE32: 89005924
; RFC6238-HEX   : 89005924
; 2033-05-18T03:33:20+00:00
; RFC6238-ASCII : 69279037
; RFC6238-BASE32: 69279037
; RFC6238-HEX   : 69279037
; 2603-10-11T11:33:20+00:00
; RFC6238-ASCII : 65353130
; RFC6238-BASE32: 65353130
; RFC6238-HEX   : 65353130

format elf64 executable 0 at 1 shl 16
entry main

HANDLE_STD_OUTPUT	= 1
CLOCK_REALTIME		= 0

SYS64_WRITE		= 1
SYS64_CLOCK_GETTIME	= 228
SYS64_EXIT_GROUP	= 231

SHA1_BLOCK_LENGTH	= 64
SHA1_OUTPUT_LENGTH	= 20

HMAC_IPAD		= 0x36
HMAC_OPAD		= 0x5c

struc timespec {
	.tv_sec		rq 1
	.tv_nsec	rq 1
}virtual at 0
	timespec timespec
	timespec.len = $
end virtual

macro BASE32_DECODE secret {
	local .secret32, secret.len, char, bits, bits_count
	virtual at 0
		.secret32::
		db	secret
		secret.len = $
	end virtual
	bits = 0
	bits_count = 0
	while % <= secret.len
		load char byte from .secret32:%-1
		if char >= "A" & char <= "Z"
			bits = bits shl 5 or (char - "A")
		else if char >= "2" & char <= "7"
			bits = bits shl 5 or (char - "2" + "Z" - "A" + 1)
		else if char = "="
			break
		else
			err "invalid base32"
		end if
		bits_count = bits_count + 5
		if bits_count >= 8
			db bits shr (bits_count - 8)
			bits_count = bits_count - 8
			bits = bits and (1 shl bits_count - 1)
		end if
	end while
}

struc CRED name="", secret=<db "">, output=6, time_step=30 {
	local secret_len, name_len
	.secret_length	db secret_len
	.name_length	db name_len
	.output_length	db output
	.time_step	db time_step
	.secret:	secret
        secret_len	= $ - .secret
			assert secret_len <= 64
	.name		db name
        name_len	= $ - .name
}virtual at 0
	CRED CRED
	CRED.len = $
end virtual
macro CRED args& {
	local .anon
	.anon CRED args
}

segment executable readable

creds:
	irpv C, ADDCRED {CRED C}
	db	0

main:
	lea	r10,[creds]
	lea	rsi,[rsp - timespec.len]
	mov	edi,CLOCK_REALTIME
	mov	eax,SYS64_CLOCK_GETTIME
	syscall
	mov	rax,[rsi + timespec.tv_sec]
	if defined TIMEOVERRIDE
		mov rax,TIMEOVERRIDE
	end if
	xor	edx,edx
	movzx	ecx,[r10 + CRED.time_step]
	div	rcx
	mov	r9,rax
    .next_credential:
	cmp	byte[r10],0
	jz	.done
	mov	rcx,r9
	call	print_totp
	movzx	eax,[r10 + CRED.name_length]
	movzx	ebx,[r10 + CRED.secret_length]
	lea	r10,[r10 + rax + CRED.len]
	add	r10,rbx
	jmp	.next_credential
    .done:
	xor	edi,edi
	mov	eax,SYS64_EXIT_GROUP
	syscall
	
print_totp:
	;inputs
	; r10 = cred
	; rcx = counter
	movzx	ebp,[r10 + CRED.secret_length]
	lea	rsi,[r10 + CRED.secret]
	call	hotp
	mov	rbp,rsp
	sub	rsp,0x1000
	mov	ebx,1
	movzx	ecx,byte[r10 + CRED.output_length]
    .make_divisor:
	imul	ebx,10					; base 10
	loop	.make_divisor
	xor	edx,edx
	div	ebx
	lea	rsi,[rbp-1]
	mov	byte[rsi],0x0a
	movzx	ecx,byte[r10 + CRED.output_length]
	mov	eax,edx
	mov	ebx,10					; base 10
    .make_decimal:
	xor	edx,edx
	div	ebx
	dec	rsi
	add	dl,"0"
	mov	[rsi],dl
	loop	.make_decimal
	sub	rsi,2
	mov	word[rsi],": "
	movzx	ecx,[r10 + CRED.name_length]
	movzx	ebx,[r10 + CRED.secret_length]
	lea	rbx,[r10 + rbx + CRED.secret - 1]
    .add_name:
	mov	al,[rbx + rcx]
	dec	rsi
	mov	[rsi],al
	loop	.add_name
	; output
	mov	eax,SYS64_WRITE
	mov	edi,HANDLE_STD_OUTPUT
	mov	rdx,rbp
	sub	rdx,rsi
	syscall
	mov	rsp,rbp
	ret

hotp:	;inputs
	; rcx = counter
	; rsi = cred
	; rbp = cred length
	;outputs
	; eax = raw result
	sub	rsp,(SHA1_OUTPUT_LENGTH - 1) and -8
	bswap	rcx
	push	rcx
	mov	rdx,8				; message length (QWORD)
	mov	rbx,rsp				; message (counter)
	mov	ah,HMAC_IPAD
	push	rsi rbp
	call	mac
	pop	rbp rsi
	mov	rbx,rsp				; message
	mov	[rbx+00],r11d
	mov	[rbx+04],r12d
	mov	[rbx+08],r13d
	mov	[rbx+12],r14d
	mov	[rbx+16],r15d
	mov	rdx,SHA1_OUTPUT_LENGTH		; message length
	mov	ah,HMAC_OPAD
	call	mac
	mov	[rsp+00],r11d
	mov	[rsp+04],r12d
	mov	[rsp+08],r13d
	mov	[rsp+12],r14d
	mov	[rsp+16],r15d
	mov	al,[rsp + SHA1_OUTPUT_LENGTH - 1]
	and	eax,0xf
	mov	eax,[rsp + rax]
	and	al,0x7f
	bswap	eax
	add	rsp,(SHA1_OUTPUT_LENGTH +7) and -8
	ret

mac:	;inputs
	; ah = whitening
	; rsi = cred
	; rbp = cred length
	; rbx = message
	; rdx = message length
	;outputs
	; r11d - r15d sha1 result
	sub	rsp,(SHA1_BLOCK_LENGTH + SHA1_OUTPUT_LENGTH + 7) and -8
	mov	rdi,rsp
	mov	ecx,SHA1_BLOCK_LENGTH
	lea	r8,[(rdx + SHA1_BLOCK_LENGTH) * 8]	; r8 = length
	neg	ebp
	add	ebp,ecx
    .padding1:
	mov	al,0
	cmp	ebp,ecx
	jae	.whiten
	lodsb
    .whiten:
	xor	al,ah
	stosb
	loop	.padding1
	lea	rbp,[rsp + SHA1_BLOCK_LENGTH]	; rbp = result
	mov	dword[rbp+00],0x67452301
	mov	dword[rbp+04],0xefcdab89
	mov	dword[rbp+08],0x98badcfe
	mov	dword[rbp+12],0x10325476
	mov	dword[rbp+16],0xc3d2e1f0
	mov	rsi,rsp
	push	rbx rdx
	call	sha1_block
	pop	rcx rsi
	lea	ebx,[ecx - SHA1_BLOCK_LENGTH + 8]
	mov	rdi,rsp
	rep	movsb
	mov	al,0x80
    .padding2:
	stosb
	mov	al,0
	inc	ebx
	jnz	.padding2
	bswap	r8
	mov	[rdi],r8
	mov	rsi,rsp
	call	sha1_block
	mov	r11d,[rbp+00]
	mov	r12d,[rbp+04]
	mov	r13d,[rbp+08]
	mov	r14d,[rbp+12]
	mov	r15d,[rbp+16]
	bswap	r11d
	bswap	r12d
	bswap	r13d
	bswap	r14d
	bswap	r15d
	add	rsp,(SHA1_BLOCK_LENGTH + SHA1_OUTPUT_LENGTH + 7) and -8
	ret

sha1_block:
	; rsi = input block
	; rbp = accumulated result

	mov	r11d,[rbp+00]
	mov	r12d,[rbp+04]
	mov	r13d,[rbp+08]
	mov	r14d,[rbp+12]
	mov	r15d,[rbp+16]

	; make big-endian
	mov	ecx,SHA1_BLOCK_LENGTH shr 2
    .make_big_endian:
	dec	ecx
	mov	eax,[rsi+rcx*4]
	bswap	eax
	mov	[rsi+rcx*4],eax
	jne	.make_big_endian
	xor	edi,edi		; rdi = "W"

	; choose rounds
	mov	cl,20*5
    .choose_loop:
	call	prepare
	mov	edx,r13d
	xor	edx,r14d
	and	edx,r12d
	xor	edx,r14d
	lea	edx,[edx+eax+0x5a827999]	; rounds  0 to 19 - floor(sqrt(2)*2^30)
	call	rotate
	sub	ecx,5
	jnz	.choose_loop

	; parity rounds
	mov	cl,20
    .parity_loop1:
	call	prepare
	mov	edx,r12d
	xor	edx,r13d
	xor	edx,r14d
	lea	edx,[edx+eax+0x6ed9eba1]	; rounds 20 to 39 - floor(sqrt(3)*2^30
	call	rotate
	loop	.parity_loop1

	; majority rounds
	mov	cl,20
    .majority_loop:
	call	prepare
	mov	edx,r12d
	xor	edx,r13d
	and	edx,r14d
	mov	ebx,r12d
	and	ebx,r13d
	add	edx,ebx
	lea	edx,[edx+eax+0x8f1bbcdc]	; rounds 40 to 59 - floor(sqrt(5)*2^30)
	call	rotate
	loop	.majority_loop

	; parity rounds
	mov	cl,20
    .parity_loop2:
	call	prepare
	mov	edx,r12d
	xor	edx,r13d
	xor	edx,r14d
	lea	edx,[edx+eax+0xca62c1d6]	; rounds 60 to 79 - floor(sqrt(10)*2^30)
	call	rotate
	loop	.parity_loop2

	; accumulate
	add	[rbp+00],r11d
	add	[rbp+04],r12d
	add	[rbp+08],r13d
	add	[rbp+12],r14d
	add	[rbp+16],r15d
	ret

prepare:
	cmp	ecx,20				; the first 16 rounds of "choose" don't do the extra mixing
	ja	.skip
	lea	eax,[edi+13]
	and	eax,0xf
	mov	edx,[rsi+rax*4]
	lea	eax,[edi+08]
	and	eax,0xf
	xor	edx,[rsi+rax*4]
	lea	eax,[edi+02]
	and	eax,0xf
	xor	edx,[rsi+rax*4]
	xor	edx,[rsi+rdi*4]
	rol	edx,1
	mov	[rsi+rdi*4],edx
    .skip:
	mov	rax,r11
	rol	eax,5
	ret

rotate:
	add	edx,[rsi+rdi*4]
	lea	rax,[r15+rdx]
	rol	r12d,30
	mov	r15,r14
	mov	r14,r13
	mov	r13,r12
	mov	r12,r11
	mov	r11,rax
	inc	edi
	and	edi,0x0f
	ret
