From 8105d0ccc273afa717ba536f4d42dac3920c017e Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Fri, 12 Sep 2025 00:19:55 +0200 Subject: [PATCH] cmd/go,crypto/internal/fips140: prevent using FIPS 140-3 mode with purego tag Change-Id: I6a6a696414f8d5d9dc77c65b0ac9edfc982c2798 Reviewed-on: https://go-review.googlesource.com/c/go/+/703095 Auto-Submit: Filippo Valsorda LUCI-TryBot-Result: Go LUCI Reviewed-by: Mark Freeman Reviewed-by: Daniel McCarney Reviewed-by: Michael Knyszek --- src/cmd/dist/test.go | 2 ++ src/cmd/go/internal/fips140/fips140.go | 4 ++++ src/crypto/internal/fips140/fips140.go | 6 ++++++ src/crypto/internal/fips140/notpurego.go | 9 +++++++++ src/crypto/internal/fips140/purego.go | 9 +++++++++ 5 files changed, 30 insertions(+) create mode 100644 src/crypto/internal/fips140/notpurego.go create mode 100644 src/crypto/internal/fips140/purego.go diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go index ec4ff649b3..7c26d001bc 100644 --- a/src/cmd/dist/test.go +++ b/src/cmd/dist/test.go @@ -1823,6 +1823,8 @@ func isEnvSet(evar string) bool { func (t *tester) fipsSupported() bool { // Keep this in sync with [crypto/internal/fips140.Supported]. + // We don't test with the purego tag, so no need to check it. + // Use GOFIPS140 or GOEXPERIMENT=boringcrypto, but not both. if strings.Contains(goexperiment, "boringcrypto") { return false diff --git a/src/cmd/go/internal/fips140/fips140.go b/src/cmd/go/internal/fips140/fips140.go index 7ca0cde588..4194f0ff6a 100644 --- a/src/cmd/go/internal/fips140/fips140.go +++ b/src/cmd/go/internal/fips140/fips140.go @@ -94,6 +94,7 @@ import ( "os" "path" "path/filepath" + "slices" "strings" "golang.org/x/mod/module" @@ -121,6 +122,9 @@ func Init() { if cfg.ExperimentErr == nil && cfg.Experiment.BoringCrypto && Enabled() { base.Fatalf("go: cannot use GOFIPS140 with GOEXPERIMENT=boringcrypto") } + if slices.Contains(cfg.BuildContext.BuildTags, "purego") && Enabled() { + base.Fatalf("go: cannot use GOFIPS140 with the purego build tag") + } } var initDone bool diff --git a/src/crypto/internal/fips140/fips140.go b/src/crypto/internal/fips140/fips140.go index fd265718e0..ca96c88442 100644 --- a/src/crypto/internal/fips140/fips140.go +++ b/src/crypto/internal/fips140/fips140.go @@ -33,6 +33,12 @@ func init() { func Supported() error { // Keep this in sync with fipsSupported in cmd/dist/test.go. + // The purego tag changes too much of the implementation to claim the + // validation still applies. + if puregoEnabled { + return errors.New("FIPS 140-3 mode is incompatible with the purego build tag") + } + // ASAN disapproves of reading swaths of global memory in fips140/check. // One option would be to expose runtime.asanunpoison through // crypto/internal/fips140deps and then call it to unpoison the range diff --git a/src/crypto/internal/fips140/notpurego.go b/src/crypto/internal/fips140/notpurego.go new file mode 100644 index 0000000000..7d1ec4b28b --- /dev/null +++ b/src/crypto/internal/fips140/notpurego.go @@ -0,0 +1,9 @@ +// Copyright 2025 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. + +//go:build !purego + +package fips140 + +const puregoEnabled = false diff --git a/src/crypto/internal/fips140/purego.go b/src/crypto/internal/fips140/purego.go new file mode 100644 index 0000000000..335977eabb --- /dev/null +++ b/src/crypto/internal/fips140/purego.go @@ -0,0 +1,9 @@ +// Copyright 2025 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. + +//go:build purego + +package fips140 + +const puregoEnabled = true -- 2.52.0