Skip to content

Commit 6581639

Browse files
authored
ascon: don't output plaintext if authentication fails (#631)
This isn't a vulnerability on its own: if a caller uses the plaintext after authentication fails, they're already in trouble.
1 parent 901199c commit 6581639

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

cipher/ascon/ascon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func (a *Cipher) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, er
157157
a.finalize(tag1, &s)
158158

159159
if subtle.ConstantTimeCompare(tag0, tag1) == 0 {
160+
clear(plaintext)
160161
return nil, ErrDecryption
161162
}
162163

cipher/ascon/ascon_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ func TestBadInputs(t *testing.T) {
107107
test.CheckIsErr(t, err, "should panic due to bad ciphertext")
108108
}
109109

110+
func TestOpenClearsOnFailure(t *testing.T) {
111+
key, _ := hex.DecodeString("000102030405060708090A0B0C0D0E0F")
112+
nonce, _ := hex.DecodeString("000102030405060708090A0B0C0D0E0F")
113+
c, _ := ascon.New(key, ascon.Ascon128)
114+
115+
pt := []byte("helloworld")
116+
ct := c.Seal(nil, nonce, pt, nil)
117+
ct[len(ct)-1] ^= 0xFF // tamper the tag so authentication fails
118+
119+
dst := make([]byte, 0, len(pt))
120+
out, err := c.Open(dst, nonce, ct, nil)
121+
test.CheckIsErr(t, err, "should fail due to bad tag")
122+
if out != nil {
123+
test.ReportError(t, out, nil)
124+
}
125+
126+
buf := dst[:len(pt)]
127+
if !bytes.Equal(buf, make([]byte, len(pt))) {
128+
test.ReportError(t, buf, make([]byte, len(pt)))
129+
}
130+
}
131+
110132
func TestAPI(t *testing.T) {
111133
key, _ := hex.DecodeString("000102030405060708090A0B0C0D0E0F")
112134
nonce, _ := hex.DecodeString("000102030405060708090A0B0C0D0E0F")

0 commit comments

Comments
 (0)