Skip to content

Commit 017a1aa

Browse files
committed
chacha20poly1305: panic on dst and additionalData overlap
The cipher.AEAD interface specifies that these should not overlap. This mirrors the check that the GCM implementation does. Fixes golang/go#75968 Updates golang/go#21624 Change-Id: If5fbb8611ff6c0aae44d50079bad29f56ce00f5b Reviewed-on: https://go-review.googlesource.com/c/crypto/+/712860 Reviewed-by: Roland Shoemaker <roland@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Daniel McCarney <daniel@binaryparadox.net> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent cf29fa9 commit 017a1aa

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

chacha20poly1305/chacha20poly1305_amd64.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ func (c *chacha20poly1305) seal(dst, nonce, plaintext, additionalData []byte) []
5656

5757
ret, out := sliceForAppend(dst, len(plaintext)+16)
5858
if alias.InexactOverlap(out, plaintext) {
59-
panic("chacha20poly1305: invalid buffer overlap")
59+
panic("chacha20poly1305: invalid buffer overlap of output and input")
60+
}
61+
if alias.AnyOverlap(out, additionalData) {
62+
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
6063
}
6164
chacha20Poly1305Seal(out[:], state[:], plaintext, additionalData)
6265
return ret
@@ -73,7 +76,10 @@ func (c *chacha20poly1305) open(dst, nonce, ciphertext, additionalData []byte) (
7376
ciphertext = ciphertext[:len(ciphertext)-16]
7477
ret, out := sliceForAppend(dst, len(ciphertext))
7578
if alias.InexactOverlap(out, ciphertext) {
76-
panic("chacha20poly1305: invalid buffer overlap")
79+
panic("chacha20poly1305: invalid buffer overlap of output and input")
80+
}
81+
if alias.AnyOverlap(out, additionalData) {
82+
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
7783
}
7884
if !chacha20Poly1305Open(out, state[:], ciphertext, additionalData) {
7985
for i := range out {

chacha20poly1305/chacha20poly1305_generic.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func (c *chacha20poly1305) sealGeneric(dst, nonce, plaintext, additionalData []b
3131
ret, out := sliceForAppend(dst, len(plaintext)+poly1305.TagSize)
3232
ciphertext, tag := out[:len(plaintext)], out[len(plaintext):]
3333
if alias.InexactOverlap(out, plaintext) {
34-
panic("chacha20poly1305: invalid buffer overlap")
34+
panic("chacha20poly1305: invalid buffer overlap of output and input")
35+
}
36+
if alias.AnyOverlap(out, additionalData) {
37+
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
3538
}
3639

3740
var polyKey [32]byte
@@ -67,7 +70,10 @@ func (c *chacha20poly1305) openGeneric(dst, nonce, ciphertext, additionalData []
6770

6871
ret, out := sliceForAppend(dst, len(ciphertext))
6972
if alias.InexactOverlap(out, ciphertext) {
70-
panic("chacha20poly1305: invalid buffer overlap")
73+
panic("chacha20poly1305: invalid buffer overlap of output and input")
74+
}
75+
if alias.AnyOverlap(out, additionalData) {
76+
panic("chacha20poly1305: invalid buffer overlap of output and additional data")
7177
}
7278
if !p.Verify(tag) {
7379
for i := range out {

0 commit comments

Comments
 (0)