Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ func xorBytes(dst, a, b []byte) int {
// recommended if: 1. you buffer the data yourself and wait for authentication
// before streaming to another source such as the network, or 2. you just don't
// care about authenticating unknown ciphertext before use :).
func newAuthReader(akey []byte, data, adata io.Reader, streaming bool) io.Reader {
func newAuthReader(akey []byte, data, rdata, adata io.Reader, streaming bool) io.Reader {
ar := authReader{
data: data,
adata: adata,
rdata: rdata,
mac: hmac.New(sha1.New, akey),
err: nil,
auth: false,
Expand All @@ -158,13 +159,13 @@ func newAuthReader(akey []byte, data, adata io.Reader, streaming bool) io.Reader
}
return &bufferedAuthReader{
ar,
new(bytes.Buffer),
}
}

// Streaming authentication
type authReader struct {
data io.Reader // data to be authenticated
rdata io.Reader // data to be return
adata io.Reader // the authentication code to read
mac hash.Hash // hmac hash
err error
Expand Down Expand Up @@ -210,7 +211,6 @@ func (a *authReader) Read(p []byte) (int, error) {
// buffered authentication
type bufferedAuthReader struct {
authReader
buf *bytes.Buffer // buffer to store data to authenticate
}

func (a *bufferedAuthReader) Read(b []byte) (int, error) {
Expand All @@ -220,7 +220,7 @@ func (a *bufferedAuthReader) Read(b []byte) (int, error) {
}
// make sure we have auth'ed before we send any data
if !a.auth {
_, err := io.Copy(a.buf, a.data)
_, err := io.Copy(a.mac, a.data)
if err != nil {
a.err = err
return 0, a.err
Expand All @@ -234,18 +234,13 @@ func (a *bufferedAuthReader) Read(b []byte) (int, error) {
a.err = ErrDecryption
return 0, a.err
}
_, err = a.mac.Write(a.buf.Bytes())
if err != nil {
a.err = err
return 0, a.err
}
if !a.checkAuthentication(ab.Bytes()) {
a.err = ErrAuthentication
return 0, a.err
}
}
// so we've authenticated the data, now just pass it on.
n, err := a.buf.Read(b)
n, err := a.rdata.Read(b)
if err != nil {
a.err = err
}
Expand Down Expand Up @@ -303,9 +298,10 @@ func newDecryptionReader(r *io.SectionReader, f *File) (io.Reader, error) {
// f.CompressedSize64 = uint64(dataLen)
// f.CompressedSize = uint32(dataLen)
data := io.NewSectionReader(r, dataOff, dataLen)
rdata := io.NewSectionReader(r, dataOff, dataLen)
authOff := dataOff + dataLen
authcode := io.NewSectionReader(r, authOff, 10)
ar := newAuthReader(authKey, data, authcode, f.DeferAuth)
ar := newAuthReader(authKey, data, rdata, authcode, f.DeferAuth)
dr := decryptStream(decKey, ar)
if dr == nil {
return nil, ErrDecryption
Expand Down