问题描述
我正在尝试实现我自己的 LoraWAN 服务器以进行个人研究。我使用 Semtech 的 LNS 协议和基本站作为网关服务器。现在,当设备尝试执行 OTAA 加入时,我会收到消息并尝试发回加入接受响应。但是我在 AppSKey 和 NwkSKey 的 MIC 计算和密钥派生方面遇到了问题。
从文档中,我必须计算加入-接受消息的 MIC,例如
cmac = aes128_cmac(NwkKey,MHDR | JoinNonce | NetID | DevAddr | DLSettings | RxDelay | CFList)
MIC = cmac[0..3]
下面的代码应该完全按照文档说的去做,加入消息部分并计算它的 cmac 哈希。但是当我尝试使用取自 this website
的一些示例数据来计算 MIC 时我正在使用 Java 和 Clojure 的组合(主要是 Clojure,但我在加密部分使用 bouncycastle 库)。
这是示例代码:
(let [;; Example data
NwkKey [0xB6 0xB5 0x3F 0x4A 0x16 0x8A 0x7A 0x88 0xBD 0xF7 0xEA 0x13 0x5C 0xE9 0xCF 0xCA]
MHDR (unchecked-byte 0x20)
JoinNonce [0xe5 0x06 0x3a]
NetID [0x00 0x00 0x13]
DevAddr [0x26 0x01 0x2e 0x43]
DLSettings (unchecked-byte 0x03)
RxDelay (unchecked-byte 0x01)
CFList [0x18 0x4f 0x84 0xe8 0x56 0x84 0xb8 0x5e 0x84 0x88 0x66 0x84 0x58 0x6e 0x84 0x00]]
;; prepare data for hashing
NwkKey' (to-byte-array NwkKey) ; 16 bytes
buffer (ByteBuffer/allocate 32)
CFList' (concat (reverse (subvec CFList 0 3)) ; 16 bytes,correct endianess to LE
(reverse (subvec CFList 3 6))
(reverse (subvec CFList 6 9))
(reverse (subvec CFList 9 12))
(reverse (subvec CFList 12 15))
[(get CFList 15)])]
;; fill buffer with data
(.put buffer MHDR) ; 1 bytes
(.put buffer (to-byte-array (reverse JoinNonce))) ; 3 bytes,little endian
(.put buffer (to-byte-array (reverse NetID))) ; 3 bytes,little endian
(.put buffer (to-byte-array (reverse DevAddr))) ; 4 bytes,little endian
(.put buffer DLSettings) ; 1 bytes
(.put buffer RxDelay) ; 1 bytes
(.put buffer (to-byte-array CFList')) ; 16 bytes,already LE (see abvove)
;; padding: 1 byte with value 1 then n bytes with valu 0 until a 128 bit block is full (RFC 4493)
(.put buffer (unchecked-byte 2r1000000))
(.put buffer (to-byte-array (map (constantly 0x00) (range 2))))
;; content of the byte buffer before calculating the cmac hash
(println (string/join " " (map byte->xstr (.array buffer))))
;; get the first four bytes of the hash,this should be the MIC
(println (string/join " " (map byte->xstr (take 4 (cmac NwkKey' (.array buffer))))))))
该示例应该产生一个值为 55 12 1d e0
的 MIC,但它给了我 7d fd eb 7b
。查看缓冲区,它的值 20 3a 06 e5 13 00 00 43 2e 01 26 03 01 84 4f 18 84 56 e8 84 5e b8 84 66 88 84 6e 58 00 40 00 00
似乎是正确的。
byte->xstr
函数是一个辅助函数,它接受一个字节并将其转换为一个字符串,其值为十六进制。
我已经尝试处理填充,以防 bouncycastle 在后台做了一些我不知道的填充,但我仍然没有得到正确的结果。
现在我有点迷失在这里,因为我找不到我的错误。我希望你能发现我的错误并指出正确的方向来解决它。
编辑:
(defn byte->xstr
"Convert a byte to a hexadecimal string representation (2 digits fixed)."
[b]
(format "%02x" b))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)