update AEADs to allow in-place encryption and decryption

ref #217
This commit is contained in:
Lucas Clemente
2016-07-26 14:51:19 +02:00
parent eb9c23096d
commit d5255a4075
13 changed files with 99 additions and 78 deletions

View File

@@ -12,7 +12,7 @@ var _ = Describe("Crypto/NullAEAD", func() {
hash := []byte{0x98, 0x9b, 0x33, 0x3f, 0xe8, 0xde, 0x32, 0x5c, 0xa6, 0x7f, 0x9c, 0xf7}
cipherText := append(hash, plainText...)
aead := &NullAEAD{}
res, err := aead.Open(0, aad, cipherText)
res, err := aead.Open(nil, cipherText, 0, aad)
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.")))
})
@@ -23,7 +23,7 @@ var _ = Describe("Crypto/NullAEAD", func() {
hash := []byte{0x98, 0x9b, 0x33, 0x3f, 0xe8, 0xde, 0x32, 0x5c, 0xa6, 0x7f, 0x9c, 0xf7}
cipherText := append(hash, plainText...)
aead := &NullAEAD{}
_, err := aead.Open(0, aad, cipherText)
_, err := aead.Open(nil, cipherText, 0, aad)
Expect(err).To(HaveOccurred())
})
@@ -31,11 +31,21 @@ var _ = Describe("Crypto/NullAEAD", 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.")
aead := &NullAEAD{}
Expect(aead.Seal(0, aad, plainText)).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.")...)))
Expect(aead.Seal(nil, plainText, 0, aad)).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.")...)))
})
It("rejects short ciphertexts", func() {
_, err := NullAEAD{}.Open(0, nil, nil)
_, err := NullAEAD{}.Open(nil, nil, 0, nil)
Expect(err).To(MatchError("NullAEAD: ciphertext cannot be less than 12 bytes long"))
})
It("seals in-place", func() {
aead := &NullAEAD{}
buf := make([]byte, 6, 12+6)
copy(buf, []byte("foobar"))
res := aead.Seal(buf[0:0], buf, 0, nil)
buf = buf[:12+6]
Expect(buf[12:]).To(Equal([]byte("foobar")))
Expect(res[12:]).To(Equal([]byte("foobar")))
})
})