-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Go 1.18] Restrict key generation and add test script (#109)
* Add strict fips mode * fixup whitespace * Add crypto test script This commit adds crypto-test.sh, which runs the Go crypto and tls tests in both default fips and strict fips modes.
- Loading branch information
Showing
16 changed files
with
257 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/bin/bash | ||
|
||
set -eE | ||
|
||
quiet () { | ||
2>&1>/dev/null $@ | ||
} | ||
|
||
# Find the GOROOT. | ||
# If using a release branch, expect the GOROOT | ||
# in the go submodule directory. | ||
GOROOT=$(readlink -f $(dirname $0)/..) | ||
quiet pushd $GOROOT | ||
if 2>/dev/null cat .gitmodules | grep -q "url = https://github.com/golang/go.git"; then | ||
GOROOT=${GOROOT}/go | ||
fi | ||
quiet popd | ||
|
||
export GOCACHE=/tmp/go-cache | ||
export GO=${GOROOT}/bin/go | ||
|
||
# Test suites to run | ||
SUITES="crypto,tls" | ||
# Verbosity flags to pass to Go | ||
VERBOSE="" | ||
|
||
# Parse command line arguments | ||
while [[ $# -gt 0 ]]; do | ||
case $1 in | ||
--suites) | ||
SUITES=$2 | ||
shift;shift | ||
;; | ||
-v) | ||
VERBOSE="$VERBOSE -v" | ||
set -x | ||
shift | ||
;; | ||
*) | ||
>&2 echo "unsupported option $1" | ||
exit 1 | ||
;; | ||
esac | ||
done | ||
|
||
notify_running() { | ||
local mode=$1 | ||
local suite=$2 | ||
echo -e "\n##### ${suite} (${mode})" | ||
} | ||
|
||
run_crypto_test_suite () { | ||
local mode=$1 | ||
local tags=$2 | ||
local suite="crypto-fips" | ||
notify_running ${mode} ${suite} | ||
quiet pushd ${GOROOT}/src/crypto | ||
GOLANG_FIPS=1 OPENSSL_FORCE_FIPS_MODE=1 \ | ||
$GO test $tags -count=1 $($GO list ./... | grep -v tls) $VERBOSE | ||
|
||
local suite="crypto-fips-parity-nocgo" | ||
notify_running ${mode} ${suite} | ||
GOLANG_FIPS=1 OPENSSL_FORCE_FIPS_MODE=1 \ | ||
CGO_ENABLED=0 $GO test $tags -count=1 $($GO list ./... | grep -v tls) $VERBOSE | ||
quiet popd | ||
} | ||
|
||
run_tls_test_suite () { | ||
local mode=$1 | ||
local tags=$2 | ||
local suite="tls-fips" | ||
notify_running ${mode} ${suite} | ||
quiet pushd ${GOROOT}/src | ||
GOLANG_FIPS=1 OPENSSL_FORCE_FIPS_MODE=1 \ | ||
$GO test $tags -count=1 crypto/tls -run "^TestBoring" $VERBOSE | ||
quiet popd | ||
} | ||
|
||
|
||
run_full_test_suite () { | ||
local mode=$1 | ||
local tags=$2 | ||
for suite in ${SUITES//,/ }; do | ||
if [[ "$suite" == "crypto" ]]; then | ||
run_crypto_test_suite ${mode} ${tags} | ||
elif [[ "$suite" == "tls" ]]; then | ||
run_tls_test_suite ${mode} ${tags} | ||
fi | ||
done | ||
} | ||
|
||
# Run in default mode | ||
run_full_test_suite default "" | ||
|
||
# Run in strict fips mode | ||
export GOEXPERIMENT=strictfipsruntime | ||
run_full_test_suite strictfips "-tags=strictfipsruntime" | ||
|
||
echo ALL TESTS PASSED | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package boring | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func hostFIPSModeEnabled() bool { | ||
// Look at /proc/sys/crypto/fips_enabled to see if FIPS mode is enabled. | ||
// If it is, log an error and exit. | ||
// If we run into an error reading that file because it doesn't exist, assume FIPS mode is not enabled. | ||
data, err := os.ReadFile("/proc/sys/crypto/fips_enabled") | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
fmt.Fprintf(os.Stderr, "error reading /proc/sys/crypto/fips_enabled: %v\n", err) | ||
os.Exit(1) | ||
} | ||
return len(data) > 0 && data[0] == '1' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//go:build !goexperiment.strictfipsruntime | ||
// +build !goexperiment.strictfipsruntime | ||
|
||
package boring | ||
|
||
var isStrictFIPS bool = false | ||
|
||
func strictFIPSOpenSSLRuntimeCheck() { | ||
} | ||
|
||
func strictFIPSNonCompliantBinaryCheck() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build goexperiment.strictfipsruntime | ||
// +build goexperiment.strictfipsruntime | ||
|
||
package boring | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
var isStrictFIPS bool = true | ||
|
||
func strictFIPSOpenSSLRuntimeCheck() { | ||
if hostFIPSModeEnabled() && !Enabled() { | ||
fmt.Fprintln(os.Stderr, "FIPS mode is enabled, but the required OpenSSL backend is unavailable") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func strictFIPSNonCompliantBinaryCheck() { | ||
if hostFIPSModeEnabled() { | ||
fmt.Fprintln(os.Stderr, "FIPS mode is enabled, but this binary is not compiled with FIPS compliant mode enabled") | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters