aliasing_purego.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  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. //go:build purego
  5. // +build purego
  6. // Package subtle implements functions that are often useful in cryptographic
  7. // code but require careful thought to use correctly.
  8. package subtle // import "golang.org/x/crypto/internal/subtle"
  9. // This is the Google App Engine standard variant based on reflect
  10. // because the unsafe package and cgo are disallowed.
  11. import "reflect"
  12. // AnyOverlap reports whether x and y share memory at any (not necessarily
  13. // corresponding) index. The memory beyond the slice length is ignored.
  14. func AnyOverlap(x, y []byte) bool {
  15. return len(x) > 0 && len(y) > 0 &&
  16. reflect.ValueOf(&x[0]).Pointer() <= reflect.ValueOf(&y[len(y)-1]).Pointer() &&
  17. reflect.ValueOf(&y[0]).Pointer() <= reflect.ValueOf(&x[len(x)-1]).Pointer()
  18. }
  19. // InexactOverlap reports whether x and y share memory at any non-corresponding
  20. // index. The memory beyond the slice length is ignored. Note that x and y can
  21. // have different lengths and still not have any inexact overlap.
  22. //
  23. // InexactOverlap can be used to implement the requirements of the crypto/cipher
  24. // AEAD, Block, BlockMode and Stream interfaces.
  25. func InexactOverlap(x, y []byte) bool {
  26. if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
  27. return false
  28. }
  29. return AnyOverlap(x, y)
  30. }