Documentation
¶
Overview ¶
Package proto 实现 Midea BLE 协议的传输/安全层:三层帧编解码、密码学原语(HKDF、 P-256 ECDH、AES-128-CCM)及握手状态机。设备无关,不包含任何设备类型的业务语义。
Index ¶
- Constants
- func CipherMsg(key, plaintext []byte) ([]byte, error)
- func CreateKeypair() (priv []byte, pub64 []byte, err error)
- func DecipherMsg(key, blob []byte) ([]byte, error)
- func DecodeBiz(buf []byte) ([]byte, error)
- func DecodeConn(buf []byte) (typ byte, body []byte, seq byte, err error)
- func DecodeSecurity(buf []byte) (cmd byte, body []byte, seq byte, err error)
- func DeriveRootKey(advertisData []byte) ([]byte, error)
- func DeriveSessionKey(myPri, peerPub64 []byte) ([]byte, error)
- func EncodeBiz(typ byte, body []byte) []byte
- func EncodeConn(typ byte, body []byte, seq byte) ([]byte, error)
- func EncodeSecurity(cmd byte, body []byte, seq byte) []byte
- type HandshakeState
- func (h *HandshakeState) BuildBiz(bizType byte, bizBody []byte) ([]byte, error)
- func (h *HandshakeState) BuildC1() ([]byte, error)
- func (h *HandshakeState) BuildC2() ([]byte, error)
- func (h *HandshakeState) BuildC3() ([]byte, error)
- func (h *HandshakeState) BuildGetVersion() ([]byte, error)
- func (h *HandshakeState) OnRecv(raw []byte) (*RecvInfo, error)
- type RecvInfo
Constants ¶
const ( T1 = 0x01 T2 = 0x02 T3 = 0x03 )
conn 层 type
const ( C1 = 0x01 C2 = 0x02 C3 = 0x03 C4 = 0x04 )
security 层 cmd
Variables ¶
This section is empty.
Functions ¶
func CreateKeypair ¶
CreateKeypair 生成 P-256 keypair,返回 (priv 32B, pub64 = X||Y)。
func DecipherMsg ¶
DecipherMsg 解开 nonce||ct||tag。
func DecodeConn ¶
DecodeConn 解析连接层帧,返回 (type, body, seq)。
func DecodeSecurity ¶
DecodeSecurity 解析安全层帧,返回 (cmd, body, seq)。
func DeriveRootKey ¶
DeriveRootKey: rootKey = HKDF-SHA256(ikm=advertisData, salt=空, info="midea_bleapp", len=16) 注意:源码里 "midea_bleapp" 是 info 不是 salt;salt 为空(HMAC 下等价零盐)。
func DeriveSessionKey ¶
DeriveSessionKey: sessionKey = SHA-256( P-256_ECDH_X(myPri, peerPub64) )[:16]
func EncodeConn ¶
EncodeConn 编码连接层帧。
Types ¶
type HandshakeState ¶
type HandshakeState struct {
AdvertisData []byte
OpenID6 []byte
RootKey []byte
SessionKey []byte
MyPri []byte
MyPub64 []byte
PeerPub64 []byte
// contains filtered or unexported fields
}
HandshakeState 握手状态机(出包构造 + 入包解析)。
func NewHandshakeState ¶
func NewHandshakeState(advertisData, openID6 []byte) (*HandshakeState, error)
NewHandshakeState 用广播数据与 openId6 初始化(自动派生 rootKey)。
func (*HandshakeState) BuildBiz ¶
func (h *HandshakeState) BuildBiz(bizType byte, bizBody []byte) ([]byte, error)
BuildBiz: c4 业务命令,sessionKey 加密,外层 t3。
func (*HandshakeState) BuildC1 ¶
func (h *HandshakeState) BuildC1() ([]byte, error)
BuildC1: body=openId6,rootKey 加密,外层 t2。
func (*HandshakeState) BuildC2 ¶
func (h *HandshakeState) BuildC2() ([]byte, error)
BuildC2: 空 body,rootKey 加密。
func (*HandshakeState) BuildC3 ¶
func (h *HandshakeState) BuildC3() ([]byte, error)
BuildC3: body=pubKey(64)||cipherMsg(sessionKey, advertisData),rootKey 加密。
func (*HandshakeState) BuildGetVersion ¶
func (h *HandshakeState) BuildGetVersion() ([]byte, error)
BuildGetVersion conn t1 + 固定 body。