家庭智能网关elinkc

流程图及数据格式以下:

这里写图片描述

这里写图片描述

这里写图片描述

流程如上图:

代码框架

function send2GwDH()
    local reqinfo={}
    reqinfo.type="dh"
    reqinfo.sequence=devinfo.sequence
    reqinfo.mac=devinfo.mac
    reqinfo.data={}

    local p, pub, priv=dh.gkey()
    devinfo.pubkey=pub
    devinfo.privkey=priv

    reqinfo.data.dh_key=encodeBase64(devinfo.pubkey)
    reqinfo.data.dh_p=encodeBase64(p)
    reqinfo.data.dh_g=encodeBase64(string.pack('B','5'))
    print("sendto gw dh")

    data=json.encode(reqinfo)
    print(json.encode(reqinfo))
    tcpSendData(data)
    print("recive from gw dh")
end

function reg()
    while true do
        local recdata = tcpRecivedData()
        if recdata then
            recjson = json.decode(recdata)
            if recjson then
                if recjson.type == "keyngack" then
                    send2GwDH() --第四,五步
                elseif recjson.type == "dh" then 
                devinfo.secret=dh.gsecret(decodeBase64(recjson.data.dh_key), devinfo.privkey)
--第六步根据网关的公钥和本身的私钥生成共享密钥(共享密钥同网关共享密钥),组网终端与智能家庭网关采用共享密钥加解密通信参数。
                    send2GwReg() --发送设备注册信息
                    return keepRun()
                end
            end
        else
            return nil
        end
    end
end
--第三步
function send2GwKeyngreq()
    local reqinfo={}
    reqinfo.type="keyngreq"
    reqinfo.sequence=devinfo.sequence
    reqinfo.mac=devinfo.mac
    reqinfo.keymodelist={}
    reqinfo.keymodelist[1]={}
    reqinfo.keymodelist[1].keymode="dh"

    data=json.encode(reqinfo)
    print(json.encode(reqinfo))
    tcpSendData(data)
    print("recive from gw Keyngreq")
    --print(tcpRecivedData())
end
--第三步
while true do
    if buildDevinfo() then
        if fromSysGet("uttcli get sysConf brideg_mode_flag") == "0" then
            os.execute("uttcli set sysConf sysConf brideg_mode_flag 1 ")
            os.execute("elink_route_bridge.sh ")
        end
        send2GwKeyngreq()
        reg()
        tcp:close()
    end
    sleep(2)
end

第三步开始,如上图发送的协商密钥生成方式格式组包lua代码以下:

function send2GwKeyngreq()
    local reqinfo={}
    reqinfo.type="keyngreq"
    reqinfo.sequence=devinfo.sequence
    reqinfo.mac=devinfo.mac
    reqinfo.keymodelist={}
    reqinfo.keymodelist[1]={}
    reqinfo.keymodelist[1].keymode="dh"

    data=json.encode(reqinfo)
    print(json.encode(reqinfo))
    tcpSendData(data)
    print("recive from gw Keyngreq")
    --print(tcpRecivedData())
end

第四步,智能家庭网关完成双方密钥协商,组网终端采用DH加密算法,发送组网终端公钥给智能家庭网关:

function send2GwDH()
    local reqinfo={}
    reqinfo.type="dh"
    reqinfo.sequence=devinfo.sequence
    reqinfo.mac=devinfo.mac
    reqinfo.data={}
    --生成公钥和私钥
    local p, pub, priv=dh.gkey() 
    devinfo.pubkey=pub
    devinfo.privkey=priv
    --生成公钥和私钥
    --通过base64编码后根据指定格式发送数据给家庭智能网关
    reqinfo.data.dh_key=encodeBase64(devinfo.pubkey)
    reqinfo.data.dh_p=encodeBase64(p)
    reqinfo.data.dh_g=encodeBase64(string.pack('B','5'))
    print("sendto gw dh")
    --通过base64编码后根据指定格式发送数据给家庭智能网关

    data=json.encode(reqinfo)
    print(json.encode(reqinfo))
    tcpSendData(data)
    print("recive from gw dh")
end

协商前发送数据代码:

function tcpSendData(data)
    devinfo.sequence=devinfo.sequence+1
    tcp:send(string.pack('>I>I',0x3f721fb5,#data)..data)
end

接收数据代码:

function tcpRecivedData()
    local data=tcp:receive(8)
    if data then
        flag, datalen=string.unpack('>I>I',data)
        --print("tcp recive 4 byte len "..datalen)
        data=tcp:receive(datalen)
        return data
    else
        return nil
    end
end

协商后发送加密数据代码:

function sendSecretData(reqinfo)
    local jsondata= json.encode(reqinfo)
    print("send: "..jsondata)
    local aesdata= aes.encrypt_cbc(jsondata,devinfo.secret)
    --私钥devinfo.secret参与加密
    tcpSendData(aesdata)
end

解密收到数据代码:

function recivedSecretData()
    local data = tcpRecivedData()
    if data then
        local respjson=aes.decrypt_cbc(data, devinfo.secret) 
        --私钥devinfo.secret参与解密
        print("get:"..respjson)
        local jsondata= json.decode(respjson)
        return jsondata
    else
        print("get rec error")
        return nil
    end
end