catch occasional size mismatch?

This commit is contained in:
Josh Deprez 2021-01-09 16:35:30 +11:00
parent 9fbf73c1e9
commit ffd5f497cf

View file

@ -61,12 +61,15 @@ func (c *sungrowConn) Write(msg []byte) (int, error) {
bs := c.block.BlockSize() bs := c.block.BlockSize()
padlen := bs - (len(msg) % bs) padlen := bs - (len(msg) % bs)
if padlen == bs {
padlen = 0
}
req := make([]byte, 4+len(msg)+padlen) req := make([]byte, 4+len(msg)+padlen)
copy(req, []byte{1, 0, byte(len(msg)), byte(padlen)}) // 4 byte encryption header 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[4:], header) // 2 byte sungrow header, replaces txid
copy(req[6:], msg[2:]) // rest of message 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......... // ECB mode.........
for cp := req[4:]; len(cp) > 0; cp = cp[bs:] { 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 return 0, err
} }
pktlen, padlen := int(hdr[2]), int(hdr[3]) 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) pkt := make([]byte, pktlen+padlen)
if _, err := io.ReadFull(c.Conn, pkt); err != nil { if _, err := io.ReadFull(c.Conn, pkt); err != nil {
return 0, err return 0, err
} }
// ECB mode here too......... // ECB mode here too.........
bs := c.block.BlockSize()
for cp := pkt; len(cp) > 0; cp = cp[bs:] { for cp := pkt; len(cp) > 0; cp = cp[bs:] {
c.block.Decrypt(cp, cp) c.block.Decrypt(cp, cp)
} }