From ffd5f497cf065b687c64f4f17e4d235f0338a760 Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Sat, 9 Jan 2021 16:35:30 +1100 Subject: [PATCH] catch occasional size mismatch? --- sungrow.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sungrow.go b/sungrow.go index 3e4f212..b4cef28 100644 --- a/sungrow.go +++ b/sungrow.go @@ -61,12 +61,15 @@ func (c *sungrowConn) Write(msg []byte) (int, error) { bs := c.block.BlockSize() padlen := bs - (len(msg) % bs) + if padlen == bs { + padlen = 0 + } req := make([]byte, 4+len(msg)+padlen) copy(req, []byte{1, 0, byte(len(msg)), byte(padlen)}) // 4 byte encryption header copy(req[4:], header) // 2 byte sungrow header, replaces txid copy(req[6:], msg[2:]) // rest of message - copy(req[4+len(msg):], padding) // padding + copy(req[4+len(msg):], padding[:padlen]) // padding // ECB mode......... for cp := req[4:]; len(cp) > 0; cp = cp[bs:] { @@ -93,13 +96,16 @@ func (c *sungrowConn) Read(out []byte) (int, error) { return 0, err } pktlen, padlen := int(hdr[2]), int(hdr[3]) + bs := c.block.BlockSize() + if (pktlen+padlen)%bs != 0 { + return 0, fmt.Errorf("pktlen + padlen = %d + %d = %d, want divisble by %d", pktlen, padlen, pktlen+padlen, bs) + } pkt := make([]byte, pktlen+padlen) if _, err := io.ReadFull(c.Conn, pkt); err != nil { return 0, err } // ECB mode here too......... - bs := c.block.BlockSize() for cp := pkt; len(cp) > 0; cp = cp[bs:] { c.block.Decrypt(cp, cp) }