当进行安全通讯时,一般通讯双方都需要预置一个根秘钥,为了保证安全,不被暴力破解,通讯根秘钥的使用频率和使用场景都需要仔细设计,尽量使用根秘钥来协商出临时通讯秘钥,使用临时通讯秘钥来进行日常通讯。这里简单笔记一些常用的秘钥协商流程。
CONTENTS
三方双向校验流程
当我们进行三方通讯时,比如云端通过网关操作子设备,子设备中预置了一个根秘钥,考虑到安全,我们不希望网关获取子设备的根秘钥,子设备的根秘钥只能在云端和子设备端进行共享,此时秘钥协商流程如下:
```sequence title: 三方双向校验流程 participant Dev participant Gateway participant Cloud note over Gateway,Cloud:Gateway和Cloud已经建立了可信连接 note over Dev:生成RandA,并计算 \n AES(key=RootKey,RandA) \n hmac(key=RootKey,RandA||DevID) Dev->Gateway:发送DevID \n AES(RandA) hmac(RandA||DevID) Gateway->Cloud:透传DevID \n AES(RandA) hmac(RandA||DevID) Cloud->Cloud:根据DevID查找RootKey note over Cloud:根据RootKey解密出RandA,并计算\nhmac(key=RootKey,RandA)\nhmac(key=RootKey,RandA||DevID) Cloud->Cloud:校验hmac(RandA||DevID)是否一致 note over Cloud:生成RandB,并计算\nAES(key=RootKey,RandB)\nhmac(key=RootKey,RandB) note over Cloud:根据RootKey,RandA,RandB生成\nTempKey=(RootKey,RandA,RandB) Cloud->Gateway:发送RandA hmac(RandA) hmac(RandB) AES(RandB) TempKey Gateway->Dev:透传hmac(RandA) AES(RandB) note over Dev:计算hmac(key=RootKey,RandA) Dev->Dev:校验hmac(RandA)是否一致 note over Dev:校验一致,说明Cloud可信 note over Dev:根据RootKey解密出RandB,并计算hmac(key=RootKey,RandB) Dev->Gateway:发送hmac(RandB) Gateway->Gateway:校验Cloud的hmac(RandB)和Dev的hmac(RandB)是否一致 note over Gateway:校验一致,说明Dev可信 note over Dev:根据RootKey,RandA,RandB计算\nTempKey=(RootKey,RandA,RandB) note over Dev,Gateway:使用TempKey临时通讯 ```
两方双向校验流程
当我们进行双方通讯时,比如网关操作子设备,此时网关和子设备中已经共享了一个安全秘钥,此时秘钥协商流程如下:
```sequence title: 两方双向校验流程 participant Dev participant Gateway note over Dev:生成RandA,并计算 \n AES(key=SecKey,RandA) \n hmac(key=SecKey,RandA) Dev->Gateway:发送AES(RandA) note over Gateway:根据SecKey解密出RandA,并计算 \n hmac(key=SecKey,RandA) note over Gateway:生成RandB,并计算 \n AES(key=SecKey,RandB) \n hmac(key=SecKey,RandB) Gateway->Dev:发送hmac(RandA) AES(RandB) Dev->Dev:校验hmac(RandA)是否一致 note over Dev:校验一致,说明Gateway可信 note over Dev:根据SecKey解密出RandB,并计算\nhmac(key=SecKey,RandB) Dev->Gateway:发送hmac(RandB) Gateway->Gateway:校验hmac(RandB)是否一致 note over Gateway:校验一致,说明Dev可信 note over Dev:根据SecKey,RandA,RandB计算\nTempKey=(SecKey,RandA,RandB) note over Gateway:根据SecKey,RandA,RandB计算\nTempKey=(SecKey,RandA,RandB) note over Dev,Gateway:使用TempKey临时通讯 ```
数据直接发送流程
当我们进行双方通讯时,在一些特殊情况下,很难进行双向协商通讯,只能单项通讯,比如网关接受低功耗子设备的数据,由于低功耗设备工作时间很短,无法完整协商通讯秘钥,此时只能使用秘钥协商算法进行单次报文加密发送,如下图所示,考虑到防重放,在设备本地允许写入本地存储的情况下,可以对Rand进行拓展,比如拓展成一个累加计数,每次发送完就写入到本地存储,网关进行计数校验即可。
```sequence title: 数据直接发送流程 participant Dev participant Gateway note over Dev:生成Rand,并计算\n SingleKey=(Rand,DevKey) note over Dev:根据SingleKey和data封装packet note over Dev,Gateway:packet格式为:\n[head][Rand][AES(key=Singlekey,[len(data)][data])][hmac(Singlekey,[head,data]][tail] Dev->Gateway:发送DevID packet Gateway->Gateway:根据DevID查询DevKey note over Gateway:校验head tail 是否合法 note over Gateway:从packet中解析出Rand,并计算\n SingleKey=(Rand,DevKey) note over Gateway:校验hmac是否合法 note over Gateway:解密获取len,data ```
发表评论