Black Lives Matter. Support the Equal Justice Initiative.

Source file src/crypto/internal/subtle/aliasing_appengine.go

Documentation: crypto/internal/subtle

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

View as plain text