You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.2 KiB

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