github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/crypto/internal/subtle/aliasing_appengine.go (about)

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