assembly - 实施 TOTP 8086 组装 (TASM)
问题描述
我正在尝试在 8086 程序集上实现 TOTP。返回 unix time/30 和 HMAC-SHA1 的程序运行良好(已检查)。我使用的密钥“0000000000”等于 0x30303030303030303030(base32 中的 GAYDAMBQGAYDAMBQ),我得到的结果与 Google Authenticator 应用程序不同。这是我的代码:
proc GoogleAuthenticator
call EpochTimeDiv30 ;get epoch time in seconds/30 in dx:ax
xchg dh, dl ;we need it to be big endian in the memory
xchg ah, al
;---------------------HMAC-SHA1 preparation------------------
mov [word HmacMsg], dx
mov [word HmacMsg+2], ax ;msg is epoch time
mov [HmacMsgLen], 4 ;msg length is 4bytes
mov [HKeyLen], 10 ;key length is 10bytes
lea bx, [HmacMsg]
mov [HmacMsgOffset], bx ;put the offset of the msg
call HMAC_SHA1 ;Key is already in Key var
;Now the result is in msgHash (result is big-endian)
mov al, [msgHash+19] ;last byte of hashed msg
and al, 0Fh ;we need only the last nibble
xor ah, ah
mov si, ax
mov dx, [word msgHash+si] ;get offset, offset+1
mov ax, [word msgHash+si+2] ;get offfset+2, offset+3
xchg dh, dl ;back to big endian
xchg ah, al ;back to big endian
and dh, 7Fh ;removing the most significant bit(MSB)
Mod32 0Fh, 4240h ;dx:ax modulo 1,000,000
ret
endp GoogleAuthenticator
编辑:我试图实现的算法:
function GoogleAuthenticatorCode(string secret)
key := 5B5E7MMX344QRHYO
message := floor(current Unix time / 30)
hash := HMAC-SHA1(key, message)
offset := last nibble of hash
truncatedHash := hash[offset..offset+3] //4 bytes starting at the offset
Set the first bit of truncatedHash to zero //remove the most significant bit
code := truncatedHash mod 1000000
pad code with 0 from the left until length of code is 6
return code
解决方案
我发现了问题。消息长度应为 8 个字节。这是工作代码:
proc GoogleAuthenticator
call EpochTimeDiv30 ;get epoch time in seconds/30 in dx:ax
xchg dh, dl ;we need it to be big endian in the memory
xchg ah, al
;---------------------HMAC-SHA1 preparation------------------
mov [word HmacMsg], 0
mov [word HmacMsg+2], 0
mov [word HmacMsg+4], dx
mov [word HmacMsg+6], ax ;msg is epoch time
mov [HmacMsgLen], 8 ;msg length is 4bytes
mov [HKeyLen], 10 ;key length is 10bytes
lea bx, [HmacMsg]
mov [HmacMsgOffset], bx ;put the offset of the msg
call HMAC_SHA1 ;Key is already in Key var
;Now the result is in msgHash (result is big-endian)
mov al, [msgHash+19] ;last byte of hashed msg
and al, 0Fh ;we need only the last nibble
xor ah, ah
mov si, ax
mov dx, [word msgHash+si] ;get offset, offset+1
mov ax, [word msgHash+si+2] ;get offfset+2, offset+3
xchg dh, dl ;back to big endian
xchg ah, al ;back to big endian
and dh, 7Fh ;removing the most significant bit(MSB)
Mod32 0Fh, 4240h ;dx:ax modulo 1,000,000
ret
endp GoogleAuthenticator
推荐阅读
- javascript - React:如何通过单击按钮更改元素的不透明度?
- r - 如何通过解压缩提取大型数据集的一部分
- bash - Bash 脚本,while 循环中的 if 语句,不需要的重复输出
- docker-compose - 配置 Prometheus 监控多个微服务
- python - Jetson Xavier NX 无法读取 pyserial (usb)
- java - 模拟静态 void 方法时抛出“UnfinishedStubingException:在此处检测到未完成的存根”
- python - 从列表中删除字典
- python - Pandas Dataframe 中的索引列返回 NaN
- python - 寻找混淆矩阵的重叠粒子
- javascript - 如何在反应的多个功能组件中使用相同的道具