Skip to content

Commit ad072b3

Browse files
FiloSottilegopherbot
authored andcommitted
crypto/internal/fips: import crypto/internal/fips/check throughout
The module must do the integrity self-check before any other operation in FIPS mode. For #69536 Change-Id: I8db52ea94e867812008a6e7a86ca2c648a0018c6 Reviewed-on: https://go-review.googlesource.com/c/go/+/629056 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Filippo Valsorda <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 1e1c0a7 commit ad072b3

File tree

19 files changed

+69
-15
lines changed

19 files changed

+69
-15
lines changed

src/crypto/internal/fips/aes/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package aes
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"errors"
1112
)
1213

src/crypto/internal/fips/aes/gcm/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package gcm
77
import (
88
"crypto/internal/fips"
99
"crypto/internal/fips/aes"
10+
_ "crypto/internal/fips/check"
1011
"errors"
1112
)
1213

src/crypto/internal/fips/bigmod/nat.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package bigmod
66

77
import (
8+
_ "crypto/internal/fips/check"
89
"crypto/internal/fipsdeps/byteorder"
910
"errors"
1011
"math/bits"

src/crypto/internal/fips/drbg/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package drbg
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"errors"
1112
)
1213

src/crypto/internal/fips/ecdh/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ecdh
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"crypto/internal/fips/nistec"
1112
"errors"
1213
"sync"

src/crypto/internal/fips/ecdsa/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ecdsa
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"crypto/internal/fips/sha512"
1112
"errors"
1213
"sync"

src/crypto/internal/fips/ed25519/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package ed25519
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"errors"
1112
"sync"
1213
)

src/crypto/internal/fips/edwards25519/edwards25519.go

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package edwards25519
66

77
import (
8+
_ "crypto/internal/fips/check"
89
"crypto/internal/fips/edwards25519/field"
910
"errors"
1011
)

src/crypto/internal/fips/edwards25519/field/fe.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package field
77

88
import (
9+
_ "crypto/internal/fips/check"
910
"crypto/internal/fips/subtle"
1011
"crypto/internal/fipsdeps/byteorder"
1112
"errors"

src/crypto/internal/fips/hkdf/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package hkdf
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"crypto/internal/fips/sha256"
1112
"errors"
1213
)

src/crypto/internal/fips/mlkem/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mlkem
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"errors"
1112
)
1213

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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 fiat
6+
7+
import _ "crypto/internal/fips/check"

src/crypto/internal/fips/nistec/nistec.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
// can't be represented.
1313
package nistec
1414

15+
import _ "crypto/internal/fips/check"
16+
1517
//go:generate go run generate.go

src/crypto/internal/fips/ssh/kdf.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
// Section 7.2 and allowed by SP 800-135 Revision 1.
77
package ssh
88

9-
import "crypto/internal/fips"
9+
import (
10+
"crypto/internal/fips"
11+
_ "crypto/internal/fips/check"
12+
)
1013

1114
type Direction struct {
1215
ivTag []byte

src/crypto/internal/fips/tls12/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package tls12
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"crypto/internal/fips/sha256"
1112
"errors"
1213
)

src/crypto/internal/fips/tls13/cast.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package tls13
77
import (
88
"bytes"
99
"crypto/internal/fips"
10+
_ "crypto/internal/fips/check"
1011
"crypto/internal/fips/sha256"
1112
"errors"
1213
)

src/crypto/internal/fipsdeps/fipsdeps_test.go

+40-10
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,54 @@ func TestImports(t *testing.T) {
4545
t.Fatalf("go list: %v\n%s", err, out)
4646
}
4747

48-
// Ensure we don't import any unexpected internal package from the FIPS
49-
// module, since we can't change the module source after it starts
50-
// validation. This locks in the API of otherwise internal packages.
48+
allPackages := make(map[string]bool)
49+
50+
// importCheck is the set of packages that import crypto/internal/fips/check.
51+
importCheck := make(map[string]bool)
52+
5153
for _, line := range strings.Split(string(out), "\n") {
5254
if line == "" {
5355
continue
5456
}
55-
parts := strings.Fields(line)
56-
if parts[1] == "crypto/internal/fips" ||
57-
strings.HasPrefix(parts[1], "crypto/internal/fips/") ||
58-
strings.HasPrefix(parts[1], "crypto/internal/fipsdeps/") {
57+
pkg, importedPkg, _ := strings.Cut(line, " ")
58+
59+
allPackages[pkg] = true
60+
61+
if importedPkg == "crypto/internal/fips/check" {
62+
importCheck[pkg] = true
63+
}
64+
65+
// Ensure we don't import any unexpected internal package from the FIPS
66+
// module, since we can't change the module source after it starts
67+
// validation. This locks in the API of otherwise internal packages.
68+
if importedPkg == "crypto/internal/fips" ||
69+
strings.HasPrefix(importedPkg, "crypto/internal/fips/") ||
70+
strings.HasPrefix(importedPkg, "crypto/internal/fipsdeps/") {
5971
continue
6072
}
61-
if AllowedInternalPackages[parts[1]] {
73+
if AllowedInternalPackages[importedPkg] {
6274
continue
6375
}
64-
if strings.Contains(parts[1], "internal") {
65-
t.Errorf("unexpected import of internal package: %s -> %s", parts[0], parts[1])
76+
if strings.Contains(importedPkg, "internal") {
77+
t.Errorf("unexpected import of internal package: %s -> %s", pkg, importedPkg)
78+
}
79+
}
80+
81+
// Ensure that all packages except check and check's dependencies import check.
82+
for pkg := range allPackages {
83+
switch pkg {
84+
case "crypto/internal/fips/check":
85+
case "crypto/internal/fips":
86+
case "crypto/internal/fips/alias":
87+
case "crypto/internal/fips/subtle":
88+
case "crypto/internal/fips/hmac":
89+
case "crypto/internal/fips/sha3":
90+
case "crypto/internal/fips/sha256":
91+
case "crypto/internal/fips/sha512":
92+
default:
93+
if !importCheck[pkg] {
94+
t.Errorf("package %s does not import crypto/internal/fips/check", pkg)
95+
}
6696
}
6797
}
6898
}

src/crypto/sha256/sha256.go

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package sha256
99
import (
1010
"crypto"
1111
"crypto/internal/boring"
12-
_ "crypto/internal/fips/check"
1312
"crypto/internal/fips/sha256"
1413
"hash"
1514
)

src/go/build/deps_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,14 @@ var depsRules = `
462462
< crypto/internal/fips
463463
< crypto/internal/fips/alias
464464
< crypto/internal/fips/subtle
465-
< crypto/internal/fips/aes
466-
< crypto/internal/fips/drbg
467-
< crypto/internal/fips/aes/gcm
468465
< crypto/internal/fips/sha256
469466
< crypto/internal/fips/sha512
470467
< crypto/internal/fips/sha3
471468
< crypto/internal/fips/hmac
472469
< crypto/internal/fips/check
470+
< crypto/internal/fips/aes
471+
< crypto/internal/fips/drbg
472+
< crypto/internal/fips/aes/gcm
473473
< crypto/internal/fips/hkdf
474474
< crypto/internal/fips/mlkem
475475
< crypto/internal/fips/ssh

0 commit comments

Comments
 (0)