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 ( import (
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/subtle"
"encoding/binary" "encoding/binary"
"errors" "errors"
"sync" "sync"
@@ -201,10 +202,8 @@ func ocbCrypt(block cipher.Block, nonce, data, ad []byte, encrypt bool) ([]byte,
out = append(out, offset[:tagLen]...) out = append(out, offset[:tagLen]...)
} else { } else {
tag := data[len(data)-tagLen:] tag := data[len(data)-tagLen:]
for j := 0; j < tagLen; j++ { if subtle.ConstantTimeCompare(tag, offset[:tagLen]) != 1 {
if tag[j] != offset[j] { return nil, errors.New("gumble: OCB authentication failed")
return nil, errors.New("gumble: OCB authentication failed")
}
} }
} }
return out, nil return out, nil
+2 -1
View File
@@ -3,6 +3,7 @@ package gumble
import ( import (
"bytes" "bytes"
"crypto/aes" "crypto/aes"
"crypto/subtle"
"encoding/binary" "encoding/binary"
"errors" "errors"
"math" "math"
@@ -314,7 +315,7 @@ func (cs *cryptState15) decrypt15(packet []byte) ([]byte, error) {
} }
// Verify first 3 bytes of tag. // 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 cs.decryptIV = savedIV
return nil, errors.New("gumble: OCB authentication failed") return nil, errors.New("gumble: OCB authentication failed")
} }