From 893704e38f1f59799e30429f7e69f4947def340c Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Sun, 10 Apr 2016 20:23:51 +0200 Subject: [PATCH] implement sealing for NullAEAD --- crypto/AEAD.go | 1 + crypto/NullAEAD.go | 16 ++++++++++++++++ crypto/NullAEAD_test.go | 9 +++++++++ 3 files changed, 26 insertions(+) diff --git a/crypto/AEAD.go b/crypto/AEAD.go index f92b90b4..8dff1545 100644 --- a/crypto/AEAD.go +++ b/crypto/AEAD.go @@ -8,4 +8,5 @@ import ( // An AEAD implements QUIC's authenticated encryption and associated data type AEAD interface { Open(associatedData []byte, ciphertext io.Reader) (*bytes.Reader, error) + Seal(b *bytes.Buffer, associatedData []byte, r *bytes.Reader) } diff --git a/crypto/NullAEAD.go b/crypto/NullAEAD.go index 7b726ac3..d3ebf8f7 100644 --- a/crypto/NullAEAD.go +++ b/crypto/NullAEAD.go @@ -6,6 +6,8 @@ import ( "errors" "io" "io/ioutil" + + "github.com/lucas-clemente/quic-go/utils" ) // NullAEAD handles not-yet encrypted packets @@ -36,3 +38,17 @@ func (*NullAEAD) Open(associatedData []byte, r io.Reader) (*bytes.Reader, error) } return bytes.NewReader(ciphertext[12:]), nil } + +// Seal writes hash and ciphertext to the buffer +func (*NullAEAD) Seal(b *bytes.Buffer, associatedData []byte, r *bytes.Reader) { + plaintext, _ := ioutil.ReadAll(r) + + hash := New128a() + hash.Write(associatedData) + hash.Write(plaintext) + high, low := hash.Sum128() + + utils.WriteUint64(b, low) + utils.WriteUint32(b, uint32(high)) + b.Write(plaintext) +} diff --git a/crypto/NullAEAD_test.go b/crypto/NullAEAD_test.go index 25952613..fe74bfb8 100644 --- a/crypto/NullAEAD_test.go +++ b/crypto/NullAEAD_test.go @@ -32,4 +32,13 @@ var _ = Describe("Crypto/NullAEAD", func() { _, err := aead.Open(aad, bytes.NewReader(cipherText)) Expect(err).To(HaveOccurred()) }) + + It("seals", 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.") + b := &bytes.Buffer{} + aead := &crypto.NullAEAD{} + aead.Seal(b, aad, bytes.NewReader(plainText)) + Expect(b.Bytes()).To(Equal(append([]byte{0x98, 0x9b, 0x33, 0x3f, 0xe8, 0xde, 0x32, 0x5c, 0xa6, 0x7f, 0x9c, 0xf7}, []byte("They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.")...))) + }) })