Skip to content

Commit eb0c2b2

Browse files
FiloSottilegopherbot
authored andcommitted
crypto/internal/fips140: add Supported
Move the logic duplicated in multiple places to a central function. Change-Id: I6a6a4656469c91dd62b0be716ec8367358f4a3e1 Reviewed-on: https://go-review.googlesource.com/c/go/+/639336 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Filippo Valsorda <[email protected]> Reviewed-by: Daniel McCarney <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent f0a9b6d commit eb0c2b2

File tree

8 files changed

+64
-45
lines changed

8 files changed

+64
-45
lines changed

src/cmd/dist/test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,8 @@ func isEnvSet(evar string) bool {
17971797
}
17981798

17991799
func (t *tester) fipsSupported() bool {
1800+
// Keep this in sync with [crypto/internal/fips140.Supported].
1801+
18001802
// Use GOFIPS140 or GOEXPERIMENT=boringcrypto, but not both.
18011803
if strings.Contains(goexperiment, "boringcrypto") {
18021804
return false

src/crypto/internal/fips140/check/asan.go renamed to src/crypto/internal/fips140/asan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
//go:build asan
66

7-
package check
7+
package fips140
88

99
const asanEnabled = true

src/crypto/internal/fips140/boring.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2024 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+
// Keep in sync with notboring.go and crypto/internal/boring/boring.go.
6+
//go:build boringcrypto && linux && (amd64 || arm64) && !android && !msan && cgo
7+
8+
package fips140
9+
10+
const boringEnabled = true

src/crypto/internal/fips140/check/check.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,13 @@ import (
1919
"crypto/internal/fips140deps/byteorder"
2020
"crypto/internal/fips140deps/godebug"
2121
"io"
22-
"runtime"
2322
"unsafe"
2423
)
2524

2625
// Verified is set when verification succeeded. It can be expected to always be
2726
// true when [fips140.Enabled] is true, or init would have panicked.
2827
var Verified bool
2928

30-
// Supported reports whether the current GOOS/GOARCH is Supported at all.
31-
func Supported() bool {
32-
// See cmd/internal/obj/fips.go's EnableFIPS for commentary.
33-
switch {
34-
case runtime.GOARCH == "wasm",
35-
runtime.GOOS == "windows" && runtime.GOARCH == "386",
36-
runtime.GOOS == "windows" && runtime.GOARCH == "arm",
37-
runtime.GOOS == "aix":
38-
return false
39-
}
40-
return true
41-
}
42-
4329
// Linkinfo holds the go:fipsinfo symbol prepared by the linker.
4430
// See cmd/link/internal/ld/fips.go for details.
4531
//
@@ -70,19 +56,8 @@ func init() {
7056
return
7157
}
7258

73-
if asanEnabled {
74-
// ASAN disapproves of reading swaths of global memory below.
75-
// One option would be to expose runtime.asanunpoison through
76-
// crypto/internal/fips140deps and then call it to unpoison the range
77-
// before reading it, but it is unclear whether that would then cause
78-
// false negatives. For now, FIPS+ASAN doesn't need to work.
79-
// If this is made to work, also re-enable the test in check_test.go
80-
// and in cmd/dist/test.go.
81-
panic("fips140: cannot verify in asan mode")
82-
}
83-
84-
if !Supported() {
85-
panic("fips140: unavailable on " + runtime.GOOS + "-" + runtime.GOARCH)
59+
if err := fips140.Supported(); err != nil {
60+
panic("fips140: " + err.Error())
8661
}
8762

8863
if Linkinfo.Magic[0] != 0xff || string(Linkinfo.Magic[1:]) != fipsMagic || Linkinfo.Sum == zeroSum {

src/crypto/internal/fips140/fips140.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
package fips140
66

7-
import "crypto/internal/fips140deps/godebug"
7+
import (
8+
"crypto/internal/fips140deps/godebug"
9+
"errors"
10+
"runtime"
11+
)
812

913
var Enabled bool
1014

@@ -24,6 +28,35 @@ func init() {
2428
}
2529
}
2630

31+
// Supported returns an error if FIPS 140-3 mode can't be enabled.
32+
func Supported() error {
33+
// Keep this in sync with fipsSupported in cmd/dist/test.go.
34+
35+
// ASAN disapproves of reading swaths of global memory in fips140/check.
36+
// One option would be to expose runtime.asanunpoison through
37+
// crypto/internal/fips140deps and then call it to unpoison the range
38+
// before reading it, but it is unclear whether that would then cause
39+
// false negatives. For now, FIPS+ASAN doesn't need to work.
40+
if asanEnabled {
41+
return errors.New("FIPS 140-3 mode is incompatible with ASAN")
42+
}
43+
44+
// See EnableFIPS in cmd/internal/obj/fips.go for commentary.
45+
switch {
46+
case runtime.GOARCH == "wasm",
47+
runtime.GOOS == "windows" && runtime.GOARCH == "386",
48+
runtime.GOOS == "windows" && runtime.GOARCH == "arm",
49+
runtime.GOOS == "aix":
50+
return errors.New("FIPS 140-3 mode is not supported on " + runtime.GOOS + "-" + runtime.GOARCH)
51+
}
52+
53+
if boringEnabled {
54+
return errors.New("FIPS 140-3 mode is incompatible with GOEXPERIMENT=boringcrypto")
55+
}
56+
57+
return nil
58+
}
59+
2760
func Name() string {
2861
return "Go Cryptographic Module"
2962
}

src/crypto/internal/fips140/check/noasan.go renamed to src/crypto/internal/fips140/notasan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
//go:build !asan
66

7-
package check
7+
package fips140
88

99
const asanEnabled = false
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2024 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 !(boringcrypto && linux && (amd64 || arm64) && !android && !msan && cgo)
6+
7+
package fips140
8+
9+
const boringEnabled = false

src/crypto/internal/fips140test/check_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
package fipstest
66

77
import (
8-
"crypto/internal/boring"
8+
"crypto/internal/fips140"
99
. "crypto/internal/fips140/check"
1010
"crypto/internal/fips140/check/checktest"
1111
"fmt"
1212
"internal/abi"
13-
"internal/asan"
1413
"internal/godebug"
1514
"internal/testenv"
1615
"os"
17-
"runtime"
1816
"testing"
1917
"unicode"
2018
"unsafe"
@@ -23,10 +21,6 @@ import (
2321
const enableFIPSTest = true
2422

2523
func TestFIPSCheckVerify(t *testing.T) {
26-
if boring.Enabled {
27-
t.Skip("not testing fips140 with boringcrypto enabled")
28-
}
29-
3024
if Verified {
3125
t.Logf("verified")
3226
return
@@ -40,12 +34,8 @@ func TestFIPSCheckVerify(t *testing.T) {
4034
return
4135
}
4236

43-
if !Supported() {
44-
t.Skipf("skipping on %s-%s", runtime.GOOS, runtime.GOARCH)
45-
}
46-
if asan.Enabled {
47-
// Verification panics with asan; don't bother.
48-
t.Skipf("skipping with -asan")
37+
if err := fips140.Supported(); err != nil {
38+
t.Skipf("skipping: %v", err)
4939
}
5040

5141
cmd := testenv.Command(t, os.Args[0], "-test.v", "-test.run=TestFIPSCheck")
@@ -62,8 +52,8 @@ func TestFIPSCheckInfo(t *testing.T) {
6252
return
6353
}
6454

65-
if !Supported() {
66-
t.Skipf("skipping on %s-%s", runtime.GOOS, runtime.GOARCH)
55+
if err := fips140.Supported(); err != nil {
56+
t.Skipf("skipping: %v", err)
6757
}
6858

6959
// Check that the checktest symbols are initialized properly.

0 commit comments

Comments
 (0)