Skip to content

Commit e8810ff

Browse files
committed
hmac: do not reuse input decoding buffer as a scratch buffer
1 parent 50b4023 commit e8810ff

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

hmac/hmac.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,30 @@ const (
1616
var hmacSignaturePrefix = []byte(HMACSignaturePrefix)
1717

1818
func VerifyHMACLoginAndPassword(mac hash.Hash, login, password []byte) bool {
19-
n, err := base64.RawURLEncoding.Decode(password, password)
19+
buf := make([]byte, base64.RawURLEncoding.DecodedLen(len(password)))
20+
n, err := base64.RawURLEncoding.Decode(buf, password)
2021
if err != nil {
2122
return false
2223
}
23-
password = password[:n]
24+
buf = buf[:n]
2425

2526
var expire int64
26-
if len(password) < int(unsafe.Sizeof(expire)) {
27+
if len(buf) < int(unsafe.Sizeof(expire)) {
2728
return false
2829
}
29-
expire = int64(binary.BigEndian.Uint64(password[:unsafe.Sizeof(expire)]))
30-
password = password[unsafe.Sizeof(expire):]
30+
expire = int64(binary.BigEndian.Uint64(buf[:unsafe.Sizeof(expire)]))
31+
buf = buf[unsafe.Sizeof(expire):]
3132

3233
if time.Unix(expire, 0).Before(time.Now()) {
3334
return false
3435
}
3536

37+
if len(buf) < mac.Size() {
38+
return false
39+
}
40+
3641
expectedMAC := CalculateHMACSignature(mac, login, expire)
37-
return hmac.Equal(password, expectedMAC)
42+
return hmac.Equal(buf[:mac.Size()], expectedMAC)
3843
}
3944

4045
func CalculateHMACSignature(mac hash.Hash, username []byte, expire int64) []byte {

0 commit comments

Comments
 (0)