]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.boringcrypto] crypto/rand: use BoringCrypto
authorRuss Cox <rsc@golang.org>
Thu, 3 Aug 2017 03:25:07 +0000 (23:25 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 17 Aug 2017 15:23:06 +0000 (15:23 +0000)
Change-Id: Ie630eff90f7fee9b359683930aec2daf96c1bdfe
Reviewed-on: https://go-review.googlesource.com/55473
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Adam Langley <agl@golang.org>
src/crypto/internal/boring/boring.go
src/crypto/internal/boring/notboring.go
src/crypto/internal/boring/rand.go [new file with mode: 0644]
src/crypto/rand/rand_unix.go

index 5982a2274338d2a48193382451e6cef496eaf4bc..615b1efadc9a42ed19fc02f42f37dcf1e18e6ec2 100644 (file)
@@ -37,3 +37,7 @@ func UnreachableExceptTests() {
                panic("boringcrypto: invalid code execution")
        }
 }
+
+type fail string
+
+func (e fail) Error() string { return "boringcrypto: " + string(e) + " failed" }
index 42c10c667b475c4e07d4f620230e782064e9e06a..9aa25ddc7b7ccc924676ab4b4d333ea370f7bf7d 100644 (file)
@@ -15,3 +15,11 @@ func Unreachable() {}
 // UnreachableExceptTests marks code that should be unreachable
 // when BoringCrypto is in use. It is a no-op without BoringCrypto.
 func UnreachableExceptTests() {}
+
+type randReader int
+
+func (randReader) Read(b []byte) (int, error) {
+       panic("boringcrypto: not available")
+}
+
+const RandReader = randReader(0)
diff --git a/src/crypto/internal/boring/rand.go b/src/crypto/internal/boring/rand.go
new file mode 100644 (file)
index 0000000..522bc33
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2017 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 linux,amd64
+// +build !cmd_go_bootstrap
+
+package boring
+
+// #include "goboringcrypto.h"
+import "C"
+import "unsafe"
+
+type randReader int
+
+func (randReader) Read(b []byte) (int, error) {
+       // Note: RAND_bytes should never fail; the return value exists only for historical reasons.
+       // We check it even so.
+       if len(b) > 0 && C._goboringcrypto_RAND_bytes((*C.uint8_t)(unsafe.Pointer(&b[0])), C.size_t(len(b))) == 0 {
+               return 0, fail("RAND_bytes")
+       }
+       return len(b), nil
+}
+
+const RandReader = randReader(0)
index 631972b92ac5e889870eed11e22ef680ff042333..f7cd74693de8183ec0b83c36aeda7ee140d44160 100644 (file)
@@ -13,6 +13,7 @@ import (
        "bufio"
        "crypto/aes"
        "crypto/cipher"
+       "crypto/internal/boring"
        "io"
        "os"
        "runtime"
@@ -26,6 +27,10 @@ const urandomDevice = "/dev/urandom"
 // This is sufficient on Linux, OS X, and FreeBSD.
 
 func init() {
+       if boring.Enabled {
+               Reader = boring.RandReader
+               return
+       }
        if runtime.GOOS == "plan9" {
                Reader = newReader(nil)
        } else {
@@ -45,6 +50,7 @@ type devReader struct {
 var altGetRandom func([]byte) (ok bool)
 
 func (r *devReader) Read(b []byte) (n int, err error) {
+       boring.Unreachable()
        if altGetRandom != nil && r.name == urandomDevice && altGetRandom(b) {
                return len(b), nil
        }
@@ -108,6 +114,7 @@ type reader struct {
 }
 
 func (r *reader) Read(b []byte) (n int, err error) {
+       boring.Unreachable()
        r.mu.Lock()
        defer r.mu.Unlock()
        n = len(b)