1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:24:40 -07:00

crypto/internal/boring: Use alias.InexactOverlap

Signed-off-by: aimuz <mr.imuz@gmail.com>
This commit is contained in:
aimuz 2024-09-13 15:29:14 +08:00
parent ad6ee21bbf
commit fe20b73606

View File

@ -46,6 +46,7 @@ import "C"
import ( import (
"bytes" "bytes"
"crypto/cipher" "crypto/cipher"
"crypto/internal/alias"
"errors" "errors"
"runtime" "runtime"
"strconv" "strconv"
@ -89,7 +90,7 @@ func NewAESCipher(key []byte) (cipher.Block, error) {
func (c *aesCipher) BlockSize() int { return aesBlockSize } func (c *aesCipher) BlockSize() int { return aesBlockSize }
func (c *aesCipher) Encrypt(dst, src []byte) { func (c *aesCipher) Encrypt(dst, src []byte) {
if inexactOverlap(dst, src) { if alias.InexactOverlap(dst, src) {
panic("crypto/cipher: invalid buffer overlap") panic("crypto/cipher: invalid buffer overlap")
} }
if len(src) < aesBlockSize { if len(src) < aesBlockSize {
@ -105,7 +106,7 @@ func (c *aesCipher) Encrypt(dst, src []byte) {
} }
func (c *aesCipher) Decrypt(dst, src []byte) { func (c *aesCipher) Decrypt(dst, src []byte) {
if inexactOverlap(dst, src) { if alias.InexactOverlap(dst, src) {
panic("crypto/cipher: invalid buffer overlap") panic("crypto/cipher: invalid buffer overlap")
} }
if len(src) < aesBlockSize { if len(src) < aesBlockSize {
@ -129,7 +130,7 @@ type aesCBC struct {
func (x *aesCBC) BlockSize() int { return aesBlockSize } func (x *aesCBC) BlockSize() int { return aesBlockSize }
func (x *aesCBC) CryptBlocks(dst, src []byte) { func (x *aesCBC) CryptBlocks(dst, src []byte) {
if inexactOverlap(dst, src) { if alias.InexactOverlap(dst, src) {
panic("crypto/cipher: invalid buffer overlap") panic("crypto/cipher: invalid buffer overlap")
} }
if len(src)%aesBlockSize != 0 { if len(src)%aesBlockSize != 0 {
@ -174,7 +175,7 @@ type aesCTR struct {
} }
func (x *aesCTR) XORKeyStream(dst, src []byte) { func (x *aesCTR) XORKeyStream(dst, src []byte) {
if inexactOverlap(dst, src) { if alias.InexactOverlap(dst, src) {
panic("crypto/cipher: invalid buffer overlap") panic("crypto/cipher: invalid buffer overlap")
} }
if len(dst) < len(src) { if len(dst) < len(src) {
@ -329,7 +330,7 @@ func (g *aesGCM) Seal(dst, nonce, plaintext, additionalData []byte) []byte {
dst = dst[:n+len(plaintext)+gcmTagSize] dst = dst[:n+len(plaintext)+gcmTagSize]
// Check delayed until now to make sure len(dst) is accurate. // Check delayed until now to make sure len(dst) is accurate.
if inexactOverlap(dst[n:], plaintext) { if alias.InexactOverlap(dst[n:], plaintext) {
panic("cipher: invalid buffer overlap") panic("cipher: invalid buffer overlap")
} }
@ -368,7 +369,7 @@ func (g *aesGCM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, er
dst = dst[:n+len(ciphertext)-gcmTagSize] dst = dst[:n+len(ciphertext)-gcmTagSize]
// Check delayed until now to make sure len(dst) is accurate. // Check delayed until now to make sure len(dst) is accurate.
if inexactOverlap(dst[n:], ciphertext) { if alias.InexactOverlap(dst[n:], ciphertext) {
panic("cipher: invalid buffer overlap") panic("cipher: invalid buffer overlap")
} }
@ -385,16 +386,3 @@ func (g *aesGCM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, er
} }
return dst[:n+int(outLen)], nil return dst[:n+int(outLen)], nil
} }
func anyOverlap(x, y []byte) bool {
return len(x) > 0 && len(y) > 0 &&
uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) &&
uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1]))
}
func inexactOverlap(x, y []byte) bool {
if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
return false
}
return anyOverlap(x, y)
}