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  	// ASAN disapproves of reading swaths of global memory in fips140/check.
    37  	// One option would be to expose runtime.asanunpoison through
    38  	// crypto/internal/fips140deps and then call it to unpoison the range
    39  	// before reading it, but it is unclear whether that would then cause
    40  	// false negatives. For now, FIPS+ASAN doesn't need to work.
    41  	if asanEnabled {
    42  		return errors.New("FIPS 140-3 mode is incompatible with ASAN")
    43  	}
    44  
    45  	// See EnableFIPS in cmd/internal/obj/fips.go for commentary.
    46  	switch {
    47  	case runtime.GOARCH == "wasm",
    48  		runtime.GOOS == "windows" && runtime.GOARCH == "386",
    49  		runtime.GOOS == "windows" && runtime.GOARCH == "arm",
    50  		runtime.GOOS == "openbsd", // due to -fexecute-only, see #70880
    51  		runtime.GOOS == "aix":
    52  		return errors.New("FIPS 140-3 mode is not supported on " + runtime.GOOS + "-" + runtime.GOARCH)
    53  	}
    54  
    55  	if boringEnabled {
    56  		return errors.New("FIPS 140-3 mode is incompatible with GOEXPERIMENT=boringcrypto")
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func Name() string {
    63  	return "Go Cryptographic Module"
    64  }
    65  
    66  // Version returns the formal version (such as "v1.0") if building against a
    67  // frozen module with GOFIPS140. Otherwise, it returns "latest".
    68  func Version() string {
    69  	// This return value is replaced by mkzip.go, it must not be changed or
    70  	// moved to a different file.
    71  	return "latest" //mkzip:version
    72  }
    73  
    74  // Hash is a legacy compatibility alias for hash.Hash.
    75  //
    76  // It's only here because [crypto/internal/fips140/ecdsa.TestingOnlyNewDRBG]
    77  // takes a "func() fips140.Hash" in v1.0.0, instead of being generic.
    78  type Hash = hash.Hash
    79  

View as plain text