Skip to content

Commit 7f39a6f

Browse files
Alex VaghinFiloSottile
authored andcommitted
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 <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 027cca1 commit 7f39a6f

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

internal/subtle/aliasing.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// +build !appengine
6+
57
// Package subtle implements functions that are often useful in cryptographic
68
// code but require careful thought to use correctly.
79
package subtle // import "golang.org/x/crypto/internal/subtle"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 "golang.org/x/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+
}

internal/subtle/aliasing_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ var aliasingTests = []struct {
2727
{a[:], nil, false, false},
2828
{nil, nil, false, false},
2929
{a[:], a[:0], false, false},
30+
{a[:10], a[:10:20], true, false},
31+
{a[:10], a[5:10:20], true, true},
3032
}
3133

3234
func testAliasing(t *testing.T, i int, x, y []byte, anyOverlap, inexactOverlap bool) {

0 commit comments

Comments
 (0)