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) }