better error handling in AuthenticationReader

pull/314/head
Darien Raymond 8 years ago
parent 919b749578
commit 5f2eb09226
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -36,10 +36,16 @@ func NewAuthenticationReader(aead cipher.AEAD, reader io.Reader, ivGen BytesGene
}
func (v *AuthenticationReader) NextChunk() error {
if v.buffer.Len() < 2 {
return errInsufficientBuffer
}
size := int(serial.BytesToUint16(v.buffer.BytesTo(2)))
if size > v.buffer.Len()-2 {
return errInsufficientBuffer
}
if size == v.aead.Overhead() {
return io.EOF
}
cipherChunk := v.buffer.BytesRange(2, size+2)
plainChunk, err := v.aead.Open(cipherChunk, v.ivGen(), cipherChunk, v.extraGen())
if err != nil {
@ -67,9 +73,11 @@ func (v *AuthenticationReader) Read(b []byte) (int, error) {
err := v.NextChunk()
if err == errInsufficientBuffer {
v.buffer.FillFrom(v.reader)
} else if err != nil {
return 0, io.ErrUnexpectedEOF
_, err = v.buffer.FillFrom(v.reader)
}
if err != nil {
return 0, err
}
totalBytes := 0

Loading…
Cancel
Save