@@ -7,26 +7,26 @@ Subject: [PATCH] Implement crypto/internal/backend
7
7
.gitignore | 2 +
8
8
src/crypto/internal/backend/backend_test.go | 30 ++
9
9
src/crypto/internal/backend/backendgen.go | 20 +
10
- .../internal/backend/backendgen_test.go | 284 ++++++++++++++
10
+ .../internal/backend/backendgen_test.go | 284 +++++++++++++
11
11
src/crypto/internal/backend/bbig/big.go | 17 +
12
12
.../internal/backend/bbig/big_boring.go | 12 +
13
13
src/crypto/internal/backend/bbig/big_cng.go | 12 +
14
14
.../internal/backend/bbig/big_darwin.go | 12 +
15
15
.../internal/backend/bbig/big_openssl.go | 12 +
16
- src/crypto/internal/backend/boring_linux.go | 279 ++++++++++++++
16
+ src/crypto/internal/backend/boring_linux.go | 279 +++++++++++++
17
17
src/crypto/internal/backend/cng_windows.go | 336 ++++++++++++++++
18
18
src/crypto/internal/backend/common.go | 59 +++
19
19
src/crypto/internal/backend/darwin_darwin.go | 359 +++++++++++++++++
20
20
src/crypto/internal/backend/fips140/boring.go | 11 +
21
21
src/crypto/internal/backend/fips140/cng.go | 33 ++
22
22
src/crypto/internal/backend/fips140/darwin.go | 11 +
23
- .../internal/backend/fips140/fips140.go | 55 +++
23
+ .../internal/backend/fips140/fips140.go | 63 +++
24
24
.../internal/backend/fips140/isrequirefips.go | 9 +
25
25
.../internal/backend/fips140/norequirefips.go | 9 +
26
26
.../backend/fips140/nosystemcrypto.go | 11 +
27
27
.../internal/backend/fips140/openssl.go | 41 ++
28
- src/crypto/internal/backend/nobackend.go | 240 ++++++++++++
29
- src/crypto/internal/backend/openssl_linux.go | 360 ++++++++++++++++++
28
+ src/crypto/internal/backend/nobackend.go | 240 +++++++++++
29
+ src/crypto/internal/backend/openssl_linux.go | 377 ++++++++++++++++++
30
30
src/crypto/internal/backend/stub.s | 10 +
31
31
src/go/build/deps_test.go | 7 +-
32
32
.../exp_allowcryptofallback_off.go | 9 +
@@ -45,7 +45,7 @@ Subject: [PATCH] Implement crypto/internal/backend
45
45
...ckenderr_gen_requirefips_nosystemcrypto.go | 17 +
46
46
.../backenderr_gen_systemcrypto_nobackend.go | 16 +
47
47
src/runtime/runtime_boring.go | 5 +
48
- 41 files changed, 2492 insertions(+), 1 deletion(-)
48
+ 41 files changed, 2517 insertions(+), 1 deletion(-)
49
49
create mode 100644 src/crypto/internal/backend/backend_test.go
50
50
create mode 100644 src/crypto/internal/backend/backendgen.go
51
51
create mode 100644 src/crypto/internal/backend/backendgen_test.go
@@ -1676,27 +1676,32 @@ index 00000000000000..ef5af5d956163e
1676
1676
+ }
1677
1677
diff --git a/src/crypto/internal/backend/fips140/fips140.go b/src/crypto/internal/backend/fips140/fips140.go
1678
1678
new file mode 100644
1679
- index 00000000000000..f54d39970319af
1679
+ index 00000000000000..72f7a1644deedd
1680
1680
--- /dev/null
1681
1681
+++ b/src/crypto/internal/backend/fips140/fips140.go
1682
- @@ -0,0 +1,55 @@
1682
+ @@ -0,0 +1,63 @@
1683
1683
+ // Copyright 2024 The Go Authors. All rights reserved.
1684
1684
+ // Use of this source code is governed by a BSD-style
1685
1685
+ // license that can be found in the LICENSE file.
1686
1686
+
1687
1687
+ package fips140
1688
1688
+
1689
- + import "syscall"
1689
+ + import (
1690
+ + "internal/godebug"
1691
+ + "syscall"
1692
+ + )
1690
1693
+
1691
- + // Enabled reports whether FIPS 140 mode is enabled by using GOFIPS=1, GOLANG_FIPS=1,
1694
+ + var fips140GODEBUG = godebug.New("#fips140")
1695
+ +
1696
+ + // Enabled reports whether FIPS 140 mode is enabled by using GODEBUG, GOFIPS, GOLANG_FIPS,
1692
1697
+ // the 'requirefips' build tag, or any other platform-specific mechanism.
1693
1698
+ func Enabled() bool {
1694
1699
+ return enabled
1695
1700
+ }
1696
1701
+
1697
1702
+ var enabled bool
1698
1703
+
1699
- + // Disabled reports whether FIPS 140 mode is disabled by using GOFIPS=0 or GOLANG_FIPS=0 .
1704
+ + // Disabled reports whether FIPS 140 mode is disabled by using GOFIPS or GOLANG_FIPS.
1700
1705
+ func Disabled() bool {
1701
1706
+ return disabled
1702
1707
+ }
@@ -1709,9 +1714,12 @@ index 00000000000000..f54d39970319af
1709
1714
+ func init() {
1710
1715
+ // TODO: Decide which environment variable to use.
1711
1716
+ // See https://github.com/microsoft/go/issues/397.
1712
- + var value string
1713
1717
+ var ok bool
1714
- + if value, ok = syscall.Getenv("GOFIPS"); ok {
1718
+ + value := fips140GODEBUG.Value()
1719
+ + if value == "on" || value == "only" || value == "debug" {
1720
+ + Message = "environment variable GODEBUG=fips140=" + value
1721
+ + value = "1"
1722
+ + } else if value, ok = syscall.Getenv("GOFIPS"); ok {
1715
1723
+ Message = "environment variable GOFIPS"
1716
1724
+ } else if value, ok = syscall.Getenv("GOLANG_FIPS"); ok {
1717
1725
+ Message = "environment variable GOLANG_FIPS"
@@ -1735,7 +1743,6 @@ index 00000000000000..f54d39970319af
1735
1743
+ return
1736
1744
+ }
1737
1745
+ }
1738
- \ No newline at end of file
1739
1746
diff --git a/src/crypto/internal/backend/fips140/isrequirefips.go b/src/crypto/internal/backend/fips140/isrequirefips.go
1740
1747
new file mode 100644
1741
1748
index 00000000000000..b33d08c84e2dae
@@ -1787,7 +1794,7 @@ index 00000000000000..83691d7dd42d51
1787
1794
+ }
1788
1795
diff --git a/src/crypto/internal/backend/fips140/openssl.go b/src/crypto/internal/backend/fips140/openssl.go
1789
1796
new file mode 100644
1790
- index 00000000000000..118efa3a492a7d
1797
+ index 00000000000000..2d126bcfc053de
1791
1798
--- /dev/null
1792
1799
+++ b/src/crypto/internal/backend/fips140/openssl.go
1793
1800
@@ -0,0 +1,41 @@
@@ -1818,7 +1825,7 @@ index 00000000000000..118efa3a492a7d
1818
1825
+ // If there is an error reading we could either panic or assume FIPS is not enabled.
1819
1826
+ // Panicking would be too disruptive for apps that don't require FIPS.
1820
1827
+ // If an app wants to be 100% sure that is running in FIPS mode
1821
- + // it should use boring.Enabled() or GOFIPS =1.
1828
+ + // it should use boring.Enabled() or GODEBUG=fips140 =1.
1822
1829
+ return false
1823
1830
+ }
1824
1831
+ }
@@ -2080,10 +2087,10 @@ index 00000000000000..7c3a95c2c64a2d
2080
2087
+ }
2081
2088
diff --git a/src/crypto/internal/backend/openssl_linux.go b/src/crypto/internal/backend/openssl_linux.go
2082
2089
new file mode 100644
2083
- index 00000000000000..5ddcf98ea682a5
2090
+ index 00000000000000..0c4e0c9da6e1ce
2084
2091
--- /dev/null
2085
2092
+++ b/src/crypto/internal/backend/openssl_linux.go
2086
- @@ -0,0 +1,360 @@
2093
+ @@ -0,0 +1,377 @@
2087
2094
+ // Copyright 2017 The Go Authors. All rights reserved.
2088
2095
+ // Use of this source code is governed by a BSD-style
2089
2096
+ // license that can be found in the LICENSE file.
@@ -2150,12 +2157,29 @@ index 00000000000000..5ddcf98ea682a5
2150
2157
+ panic("opensslcrypto: can't initialize OpenSSL " + lcrypto + ": " + err.Error())
2151
2158
+ }
2152
2159
+ if fips140.Enabled() {
2153
- + // Use openssl.FIPSCapable instead of openssl.FIPS because some providers, e.g. SCOSSL, are FIPS compliant
2154
- + // even when FIPS mode is not enabled.
2160
+ + // Some distributions, e.g. Azure Linux 3, don't set the `fips=yes` property when running in FIPS mode,
2161
+ + // but they configure OpenSSL to use a FIPS-compliant provider (in the case of Azure Linux 3, the SCOSSL provider).
2162
+ + // In this cases, openssl.FIPS would return `false` and openssl.FIPSCapable would return `true`.
2163
+ + // We don't care about the `fips=yes` property as long as the provider is FIPS-compliant, so use
2164
+ + // openssl.FIPSCapable to determine whether FIPS mode is enabled.
2155
2165
+ if !openssl.FIPSCapable() {
2156
- + panic("opensslcrypto: FIPS mode requested (" + fips140.Message + ") but not available in " + openssl.VersionText())
2166
+ + // This path can be reached for the following reasons:
2167
+ + // - In OpenSSL 1, the active engine doesn't support FIPS mode.
2168
+ + // - In OpenSSL 1, the active engine supports FIPS mode, but it is not enabled.
2169
+ + // - In OpenSSL 3, the provider used by default doesn't match the `fips=yes` query.
2170
+ + //
2171
+ + // A best-effort attempt is made to enable FIPS mode. It will only succeed if the following conditions are met:
2172
+ + // - In OpenSSL 1, the active engine supports FIPS mode and FIPS mode can be enabled.
2173
+ + // - In OpenSSL 3, there is an available provider that supports the `fips=yes` query.
2174
+ + //
2175
+ + // Note that this best effort is mainly to support test environments. FIPS-compliant production environments
2176
+ + // like Mariner 2 and Azure Linux 3 (when executed in kernel FIPS mode) will already be properly configured.
2177
+ + if err := openssl.SetFIPS(true); err != nil {
2178
+ + panic("opensslcrypto: FIPS mode requested (" + fips140.Message + ") but not available in " + openssl.VersionText() + ": " + err.Error())
2179
+ + }
2157
2180
+ }
2158
2181
+ } else if fips140.Disabled() {
2182
+ + // TODO: Remove this block when GOFIPS=0 is no longer supported.
2159
2183
+ if openssl.FIPS() {
2160
2184
+ panic("opensslcrypto: FIPS mode explicitly disabled (" + fips140.Message + ") but enabled in " + openssl.VersionText())
2161
2185
+ }
0 commit comments