Compare UDP authentication tags in constant time

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-10 09:42:49 -04:00
committed by Brandon McGinty
parent 9fdcf6171d
commit e68546eec5
2 changed files with 5 additions and 5 deletions
+3 -4
View File
@@ -3,6 +3,7 @@ package gumble
import (
"crypto/aes"
"crypto/cipher"
"crypto/subtle"
"encoding/binary"
"errors"
"sync"
@@ -201,10 +202,8 @@ func ocbCrypt(block cipher.Block, nonce, data, ad []byte, encrypt bool) ([]byte,
out = append(out, offset[:tagLen]...)
} else {
tag := data[len(data)-tagLen:]
for j := 0; j < tagLen; j++ {
if tag[j] != offset[j] {
return nil, errors.New("gumble: OCB authentication failed")
}
if subtle.ConstantTimeCompare(tag, offset[:tagLen]) != 1 {
return nil, errors.New("gumble: OCB authentication failed")
}
}
return out, nil
+2 -1
View File
@@ -3,6 +3,7 @@ package gumble
import (
"bytes"
"crypto/aes"
"crypto/subtle"
"encoding/binary"
"errors"
"math"
@@ -314,7 +315,7 @@ func (cs *cryptState15) decrypt15(packet []byte) ([]byte, error) {
}
// Verify first 3 bytes of tag.
if tag[0] != expectedTag[0] || tag[1] != expectedTag[1] || tag[2] != expectedTag[2] {
if subtle.ConstantTimeCompare(tag[:3], expectedTag) != 1 {
cs.decryptIV = savedIV
return nil, errors.New("gumble: OCB authentication failed")
}