proto

package
v0.0.0-...-bace1bb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package proto 实现 Midea BLE 协议的传输/安全层:三层帧编解码、密码学原语(HKDF、 P-256 ECDH、AES-128-CCM)及握手状态机。设备无关,不包含任何设备类型的业务语义。

Index

Constants

View Source
const (
	T1 = 0x01
	T2 = 0x02
	T3 = 0x03
)

conn 层 type

View Source
const (
	C1 = 0x01
	C2 = 0x02
	C3 = 0x03
	C4 = 0x04
)

security 层 cmd

Variables

This section is empty.

Functions

func CipherMsg

func CipherMsg(key, plaintext []byte) ([]byte, error)

CipherMsg: nonce(8) || ciphertext || tag(8),AES-128-CCM。

func CreateKeypair

func CreateKeypair() (priv []byte, pub64 []byte, err error)

CreateKeypair 生成 P-256 keypair,返回 (priv 32B, pub64 = X||Y)。

func DecipherMsg

func DecipherMsg(key, blob []byte) ([]byte, error)

DecipherMsg 解开 nonce||ct||tag。

func DecodeBiz

func DecodeBiz(buf []byte) ([]byte, error)

DecodeBiz 取出 body(JS: slice(3, len-1))。

func DecodeConn

func DecodeConn(buf []byte) (typ byte, body []byte, seq byte, err error)

DecodeConn 解析连接层帧,返回 (type, body, seq)。

func DecodeSecurity

func DecodeSecurity(buf []byte) (cmd byte, body []byte, seq byte, err error)

DecodeSecurity 解析安全层帧,返回 (cmd, body, seq)。

func DeriveRootKey

func DeriveRootKey(advertisData []byte) ([]byte, error)

DeriveRootKey: rootKey = HKDF-SHA256(ikm=advertisData, salt=空, info="midea_bleapp", len=16) 注意:源码里 "midea_bleapp" 是 info 不是 salt;salt 为空(HMAC 下等价零盐)。

func DeriveSessionKey

func DeriveSessionKey(myPri, peerPub64 []byte) ([]byte, error)

DeriveSessionKey: sessionKey = SHA-256( P-256_ECDH_X(myPri, peerPub64) )[:16]

func EncodeBiz

func EncodeBiz(typ byte, body []byte) []byte

EncodeBiz 编码业务层帧(type|len|reserved|body|chk,校验覆盖 [0..len-2))。

func EncodeConn

func EncodeConn(typ byte, body []byte, seq byte) ([]byte, error)

EncodeConn 编码连接层帧。

func EncodeSecurity

func EncodeSecurity(cmd byte, body []byte, seq byte) []byte

EncodeSecurity 编码安全层帧。

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。

func (*HandshakeState) OnRecv

func (h *HandshakeState) OnRecv(raw []byte) (*RecvInfo, error)

OnRecv 解析一个完整 conn 帧。

type RecvInfo

type RecvInfo struct {
	Kind      string // t1/c1/c2/c3/biz/sec_error/unknown
	Result    int    // c1/c3 的 result(-1 表示无)
	PeerPub64 []byte // c2
	BizBody   []byte // biz
	Body      []byte
}

RecvInfo 入包解析结果。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL