Java/Clojure实现LoRaWAN OTAA Join-Accept MIC计算结果异常问题
LoRaWAN OTAA Join-Accept MIC计算结果不符合预期问题排查
问题背景
- 自研LoRaWAN服务器,适配Semtech LNS协议,网关采用Basic Station服务
- OTAA加入流程中,返回Join-Accept响应时,MIC计算、AppSKey/NwkSKey派生环节出错
参考计算规则
Join-Accept的MIC计算规则如下:
cmac = aes128_cmac(NwkKey, MHDR | JoinNonce | NetID | DevAddr | DLSettings | RxDelay | CFList) MIC = cmac[0..3]
开发环境
- 技术栈: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))))))))
工具函数
(defn byte->xstr "Convert a byte to a hexadecimal string representation (2 digits fixed)." [b] (format "%02x" b))
问题现象
- 预期MIC输出:
55 12 1d e0 - 实际MIC输出:
7d fd eb 7b - 已确认cmac计算前的字节缓冲区内容为
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,符合预期 - 已排查BouncyCastle库自动填充问题,调整填充逻辑后仍无法得到正确结果
问题定位与修正方案
核心错误点
额外手动添加RFC4493填充属于多余操作。AES-CMAC算法本身已经内置了符合RFC4493规范的填充逻辑,不需要开发者手动对输入数据做填充,手动填充会导致输入数据与规则要求的输入不一致,最终计算出错误的哈希值。
修正步骤
- 直接删除代码中手动填充的两行逻辑:
;; 删掉以下两行代码 (.put buffer (unchecked-byte 2r1000000)) (.put buffer (to-byte-array (map (constantly 0x00) (range 2))))
- 调整缓冲区分配大小,去掉为填充预留的3字节空间,原缓冲区分配的32字节改为29字节即可(1+3+3+4+1+1+16=29),或者直接拼接完所有字段后提取前29个有效字节再传入cmac计算。
验证说明
修改后传入cmac的输入数据为缓冲区前29字节: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,用该输入计算AES128-CMAC即可得到预期的55 12 1d e0 MIC值。
额外注意事项
如果后续计算AppSKey、NwkSKey仍有问题,注意检查密钥派生时的字段字节序、以及是否混淆了NwkKey和AppKey的使用场景。
内容的提问来源于stack exchange,提问作者7tupel
相关产品推荐
相关产品推荐

