Compare UDP authentication tags in constant time
This commit is contained in:
committed by
Brandon McGinty
parent
9fdcf6171d
commit
e68546eec5
@@ -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
|
||||||
|
|||||||
@@ -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")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user