فهرست منبع

internal/subtle: add Google App Engine support

The new package subtle added in golang.org/cl/112236 breaks
compatibility with App Engine due to the import of unsafe.

This changes adds an App Engine alternative without using unsafe.

Tested with:

  $ go test -test.tags=appengine -v
  === RUN   TestAliasing
  --- PASS: TestAliasing (0.00s)
  PASS
  ok  	golang.org/x/crypto/internal/subtle	0.009s

Change-Id: I2fc6b02a860b3ee11fa31652ba302fc7db9df153
Reviewed-on: https://go-review.googlesource.com/119095
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Alex Vaghin 7 سال پیش
والد
کامیت
7f39a6fea4
3فایلهای تغییر یافته به همراه39 افزوده شده و 0 حذف شده
  1. 2 0
      internal/subtle/aliasing.go
  2. 35 0
      internal/subtle/aliasing_appengine.go
  3. 2 0
      internal/subtle/aliasing_test.go

+ 2 - 0
internal/subtle/aliasing.go

@@ -2,6 +2,8 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// +build !appengine
+
 // Package subtle implements functions that are often useful in cryptographic
 // code but require careful thought to use correctly.
 package subtle // import "golang.org/x/crypto/internal/subtle"

+ 35 - 0
internal/subtle/aliasing_appengine.go

@@ -0,0 +1,35 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build appengine
+
+// Package subtle implements functions that are often useful in cryptographic
+// code but require careful thought to use correctly.
+package subtle // import "golang.org/x/crypto/internal/subtle"
+
+// This is the Google App Engine standard variant based on reflect
+// because the unsafe package and cgo are disallowed.
+
+import "reflect"
+
+// AnyOverlap reports whether x and y share memory at any (not necessarily
+// corresponding) index. The memory beyond the slice length is ignored.
+func AnyOverlap(x, y []byte) bool {
+	return len(x) > 0 && len(y) > 0 &&
+		reflect.ValueOf(&x[0]).Pointer() <= reflect.ValueOf(&y[len(y)-1]).Pointer() &&
+		reflect.ValueOf(&y[0]).Pointer() <= reflect.ValueOf(&x[len(x)-1]).Pointer()
+}
+
+// InexactOverlap reports whether x and y share memory at any non-corresponding
+// index. The memory beyond the slice length is ignored. Note that x and y can
+// have different lengths and still not have any inexact overlap.
+//
+// InexactOverlap can be used to implement the requirements of the crypto/cipher
+// AEAD, Block, BlockMode and Stream interfaces.
+func InexactOverlap(x, y []byte) bool {
+	if len(x) == 0 || len(y) == 0 || &x[0] == &y[0] {
+		return false
+	}
+	return AnyOverlap(x, y)
+}

+ 2 - 0
internal/subtle/aliasing_test.go

@@ -27,6 +27,8 @@ var aliasingTests = []struct {
 	{a[:], nil, false, false},
 	{nil, nil, false, false},
 	{a[:], a[:0], false, false},
+	{a[:10], a[:10:20], true, false},
+	{a[:10], a[5:10:20], true, true},
 }
 
 func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) {