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

View as plain text