From 7f39a6fea4fe9364fb61e1def6a268a51b4f3a06 Mon Sep 17 00:00:00 2001 From: Alex Vaghin Date: Fri, 15 Jun 2018 18:03:23 +0200 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- internal/subtle/aliasing.go | 2 ++ internal/subtle/aliasing_appengine.go | 35 +++++++++++++++++++++++++++ internal/subtle/aliasing_test.go | 2 ++ 3 files changed, 39 insertions(+) create mode 100644 internal/subtle/aliasing_appengine.go diff --git a/internal/subtle/aliasing.go b/internal/subtle/aliasing.go index 61b67bf..f38797b 100644 --- a/internal/subtle/aliasing.go +++ b/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" diff --git a/internal/subtle/aliasing_appengine.go b/internal/subtle/aliasing_appengine.go new file mode 100644 index 0000000..0cc4a8a --- /dev/null +++ b/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) +} diff --git a/internal/subtle/aliasing_test.go b/internal/subtle/aliasing_test.go index dfa7368..a5b62ff 100644 --- a/internal/subtle/aliasing_test.go +++ b/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) {