Read iv and Encrypted data.
(b []byte)
| 69 | |
| 70 | // Read iv and Encrypted data. |
| 71 | func (c *CryptoConn) Read(b []byte) (n int, err error) { |
| 72 | if c.decStream == nil { |
| 73 | buf := make([]byte, c.info.ivLen+MagicSize) |
| 74 | if _, err = io.ReadFull(c.Conn, buf); err != nil { |
| 75 | log.WithError(err).Info("read full failed") |
| 76 | return |
| 77 | } |
| 78 | iv := buf[:c.info.ivLen] |
| 79 | header := buf[c.info.ivLen:] |
| 80 | if err = c.initDecrypt(iv); err != nil { |
| 81 | return |
| 82 | } |
| 83 | c.decrypt(header, header) |
| 84 | if !bytes.Equal(header[:MagicSize], MagicBytes[:]) { |
| 85 | err = errors.New("bad stream ETLS header") |
| 86 | return |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | cipherData := make([]byte, len(b)) |
| 91 | |
| 92 | n, err = c.Conn.Read(cipherData) |
| 93 | if err != nil { |
| 94 | return |
| 95 | } |
| 96 | if n > 0 { |
| 97 | c.decrypt(b[0:n], cipherData[0:n]) |
| 98 | } |
| 99 | return |
| 100 | } |
| 101 | |
| 102 | // Write iv and Encrypted data. |
| 103 | func (c *CryptoConn) Write(b []byte) (n int, err error) { |