Skip to content

Commit

Permalink
crypto/internal/boring: Use alias.InexactOverlap
Browse files Browse the repository at this point in the history
Signed-off-by: aimuz <[email protected]>
  • Loading branch information
aimuz committed Sep 13, 2024
1 parent ad6ee21 commit 44e7744
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/crypto/internal/boring/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import "C"
import (
"bytes"
"crypto/cipher"
"crypto/internal/alias"
"errors"
"runtime"
"strconv"
Expand Down Expand Up @@ -368,7 +369,7 @@ func (g *aesGCM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, er
dst = dst[:n+len(ciphertext)-gcmTagSize]

// 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")
}

Expand All @@ -385,16 +386,3 @@ func (g *aesGCM) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, er
}
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)
}

0 comments on commit 44e7744

Please sign in to comment.