Source file src/vendor/golang.org/x/crypto/internal/subtle/aliasing.go
1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build !purego 6 // +build !purego 7 8 // Package subtle implements functions that are often useful in cryptographic 9 // code but require careful thought to use correctly. 10 package subtle // import "golang.org/x/crypto/internal/subtle" 11 12 import "unsafe" 13 14 // AnyOverlap reports whether x and y share memory at any (not necessarily 15 // corresponding) index. The memory beyond the slice length is ignored. 16 func AnyOverlap(x, y []byte) bool { 17 return len(x) > 0 && len(y) > 0 && 18 uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) && 19 uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1])) 20 } 21 22 // InexactOverlap reports whether x and y share memory at any non-corresponding 23 // index. The memory beyond the slice length is ignored. Note that x and y can 24 // have different lengths and still not have any inexact overlap. 25 // 26 // InexactOverlap can be used to implement the requirements of the crypto/cipher 27 // AEAD, Block, BlockMode and Stream interfaces. 28 func InexactOverlap(x, y []byte) bool { 29 if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] { 30 return false 31 } 32 return AnyOverlap(x, y) 33 } 34