Source file src/crypto/internal/fips140/fips140.go
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 package fips140 6 7 import ( 8 "crypto/internal/fips140deps/godebug" 9 "errors" 10 "hash" 11 "runtime" 12 ) 13 14 var Enabled bool 15 16 var debug bool 17 18 func init() { 19 v := godebug.Value("#fips140") 20 switch v { 21 case "on", "only": 22 Enabled = true 23 case "debug": 24 Enabled = true 25 debug = true 26 case "off", "": 27 default: 28 panic("fips140: unknown GODEBUG setting fips140=" + v) 29 } 30 } 31 32 // Supported returns an error if FIPS 140-3 mode can't be enabled. 33 func Supported() error { 34 // Keep this in sync with fipsSupported in cmd/dist/test.go. 35 36 // The purego tag changes too much of the implementation to claim the 37 // validation still applies. 38 if puregoEnabled { 39 return errors.New("FIPS 140-3 mode is incompatible with the purego build tag") 40 } 41 42 // ASAN disapproves of reading swaths of global memory in fips140/check. 43 // One option would be to expose runtime.asanunpoison through 44 // crypto/internal/fips140deps and then call it to unpoison the range 45 // before reading it, but it is unclear whether that would then cause 46 // false negatives. For now, FIPS+ASAN doesn't need to work. 47 if asanEnabled { 48 return errors.New("FIPS 140-3 mode is incompatible with ASAN") 49 } 50 51 // See EnableFIPS in cmd/internal/obj/fips.go for commentary. 52 switch { 53 case runtime.GOARCH == "wasm", 54 runtime.GOOS == "windows" && runtime.GOARCH == "386", 55 runtime.GOOS == "openbsd", // due to -fexecute-only, see #70880 56 runtime.GOOS == "aix": 57 return errors.New("FIPS 140-3 mode is not supported on " + runtime.GOOS + "-" + runtime.GOARCH) 58 } 59 60 if boringEnabled { 61 return errors.New("FIPS 140-3 mode is incompatible with GOEXPERIMENT=boringcrypto") 62 } 63 64 return nil 65 } 66 67 func Name() string { 68 return "Go Cryptographic Module" 69 } 70 71 // Version returns the formal version (such as "v1.0") if building against a 72 // frozen module with GOFIPS140. Otherwise, it returns "latest". 73 func Version() string { 74 // This return value is replaced by mkzip.go, it must not be changed or 75 // moved to a different file. 76 return "latest" //mkzip:version 77 } 78 79 // Hash is a legacy compatibility alias for hash.Hash. 80 // 81 // It's only here because [crypto/internal/fips140/ecdsa.TestingOnlyNewDRBG] 82 // takes a "func() fips140.Hash" in v1.0.0, instead of being generic. 83 type Hash = hash.Hash 84