add NullAEAD implementation

This commit is contained in:
Lucas Clemente 2016-04-07 17:02:26 +02:00
parent fc853a6e22
commit 990660ae3e
2 changed files with 63 additions and 0 deletions

38
crypto/NullAEAD.go Normal file
View file

@ -0,0 +1,38 @@
package crypto
import (
"bytes"
"encoding/binary"
"errors"
"io"
"io/ioutil"
)
// NullAEAD handles not-yet encrypted packets
type NullAEAD struct{}
var _ AEAD = &NullAEAD{}
// Open and verify the ciphertext
func (*NullAEAD) Open(associatedData []byte, r io.Reader) (io.Reader, error) {
ciphertext, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
if len(ciphertext) < 12 {
return nil, errors.New("NullAEAD: ciphertext cannot be less than 12 bytes long")
}
hash := New128a()
hash.Write(associatedData)
hash.Write(ciphertext[12:])
testHigh, testLow := hash.Sum128()
low := binary.LittleEndian.Uint64(ciphertext)
high := binary.LittleEndian.Uint32(ciphertext[8:])
if uint32(testHigh&0xffffffff) != high || testLow != low {
return nil, errors.New("NullAEAD: failed to authenticate received data")
}
return bytes.NewReader(ciphertext[12:]), nil
}

25
crypto/NullAEAD_test.go Normal file
View file

@ -0,0 +1,25 @@
package crypto_test
import (
"bytes"
"io/ioutil"
"github.com/lucas-clemente/quic-go/crypto"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Crypto/NullAEAD", func() {
It("opens", func() {
aad := []byte("All human beings are born free and equal in dignity and rights.")
plainText := []byte("They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.")
hash := []byte{0x98, 0x9b, 0x33, 0x3f, 0xe8, 0xde, 0x32, 0x5c, 0xa6, 0x7f, 0x9c, 0xf7}
cipherText := append(hash, plainText...)
aead := &crypto.NullAEAD{}
r, err := aead.Open(aad, bytes.NewReader(cipherText))
Expect(err).ToNot(HaveOccurred())
res, err := ioutil.ReadAll(r)
Expect(err).ToNot(HaveOccurred())
Expect(res).To(Equal([]byte("They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.")))
})
})