Skip to content

Commit 86bbea0

Browse files
DanielMorsinggopherbot
authored andcommitted
crypto/fips140: add WithoutEnforcement
WithoutEnforcement lets programs running under GODEBUG=fips140=only selectively opt out of strict enforcement. This is especially helpful for non-critical uses of cryptography routines like SHA-1 for content addressable storage backends (E.g. git). Fixes #74630 Change-Id: Iabba1f5eb63498db98047aca45e09c5dccf2fbdf Reviewed-on: https://go-review.googlesource.com/c/go/+/723720 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Filippo Valsorda <filippo@golang.org> Auto-Submit: Filippo Valsorda <filippo@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Roland Shoemaker <roland@golang.org>
1 parent e2cae9e commit 86bbea0

File tree

32 files changed

+252
-61
lines changed

32 files changed

+252
-61
lines changed

api/next/74630.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pkg crypto/fips140, func Enforced() bool #74630
2+
pkg crypto/fips140, func WithoutEnforcement(func()) #74630
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The new [WithoutEnforcement] and [Enforced] functions now allow running
2+
in `GODEBUG=fips140=only` mode while selectively disabling the strict FIPS 140-3 checks.

src/crypto/cipher/cbc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func NewCBCEncrypter(b Block, iv []byte) BlockMode {
5454
if b, ok := b.(*aes.Block); ok {
5555
return aes.NewCBCEncrypter(b, [16]byte(iv))
5656
}
57-
if fips140only.Enabled {
57+
if fips140only.Enforced() {
5858
panic("crypto/cipher: use of CBC with non-AES ciphers is not allowed in FIPS 140-only mode")
5959
}
6060
if cbc, ok := b.(cbcEncAble); ok {
@@ -133,7 +133,7 @@ func NewCBCDecrypter(b Block, iv []byte) BlockMode {
133133
if b, ok := b.(*aes.Block); ok {
134134
return aes.NewCBCDecrypter(b, [16]byte(iv))
135135
}
136-
if fips140only.Enabled {
136+
if fips140only.Enforced() {
137137
panic("crypto/cipher: use of CBC with non-AES ciphers is not allowed in FIPS 140-only mode")
138138
}
139139
if cbc, ok := b.(cbcDecAble); ok {

src/crypto/cipher/cfb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (x *cfb) XORKeyStream(dst, src []byte) {
6161
// CFB is also unoptimized and not validated as part of the FIPS 140-3 module.
6262
// If an unauthenticated [Stream] mode is required, use [NewCTR] instead.
6363
func NewCFBEncrypter(block Block, iv []byte) Stream {
64-
if fips140only.Enabled {
64+
if fips140only.Enforced() {
6565
panic("crypto/cipher: use of CFB is not allowed in FIPS 140-only mode")
6666
}
6767
return newCFB(block, iv, false)
@@ -77,7 +77,7 @@ func NewCFBEncrypter(block Block, iv []byte) Stream {
7777
// CFB is also unoptimized and not validated as part of the FIPS 140-3 module.
7878
// If an unauthenticated [Stream] mode is required, use [NewCTR] instead.
7979
func NewCFBDecrypter(block Block, iv []byte) Stream {
80-
if fips140only.Enabled {
80+
if fips140only.Enforced() {
8181
panic("crypto/cipher: use of CFB is not allowed in FIPS 140-only mode")
8282
}
8383
return newCFB(block, iv, true)

src/crypto/cipher/ctr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewCTR(block Block, iv []byte) Stream {
4242
if block, ok := block.(*aes.Block); ok {
4343
return aesCtrWrapper{aes.NewCTR(block, iv)}
4444
}
45-
if fips140only.Enabled {
45+
if fips140only.Enforced() {
4646
panic("crypto/cipher: use of CTR with non-AES ciphers is not allowed in FIPS 140-only mode")
4747
}
4848
if ctr, ok := block.(ctrAble); ok {

src/crypto/cipher/gcm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const (
2828
// An exception is when the underlying [Block] was created by aes.NewCipher
2929
// on systems with hardware support for AES. See the [crypto/aes] package documentation for details.
3030
func NewGCM(cipher Block) (AEAD, error) {
31-
if fips140only.Enabled {
31+
if fips140only.Enforced() {
3232
return nil, errors.New("crypto/cipher: use of GCM with arbitrary IVs is not allowed in FIPS 140-only mode, use NewGCMWithRandomNonce")
3333
}
3434
return newGCM(cipher, gcmStandardNonceSize, gcmTagSize)
@@ -42,7 +42,7 @@ func NewGCM(cipher Block) (AEAD, error) {
4242
// cryptosystem that uses non-standard nonce lengths. All other users should use
4343
// [NewGCM], which is faster and more resistant to misuse.
4444
func NewGCMWithNonceSize(cipher Block, size int) (AEAD, error) {
45-
if fips140only.Enabled {
45+
if fips140only.Enforced() {
4646
return nil, errors.New("crypto/cipher: use of GCM with arbitrary IVs is not allowed in FIPS 140-only mode, use NewGCMWithRandomNonce")
4747
}
4848
return newGCM(cipher, size, gcmTagSize)
@@ -57,7 +57,7 @@ func NewGCMWithNonceSize(cipher Block, size int) (AEAD, error) {
5757
// cryptosystem that uses non-standard tag lengths. All other users should use
5858
// [NewGCM], which is more resistant to misuse.
5959
func NewGCMWithTagSize(cipher Block, tagSize int) (AEAD, error) {
60-
if fips140only.Enabled {
60+
if fips140only.Enforced() {
6161
return nil, errors.New("crypto/cipher: use of GCM with arbitrary IVs is not allowed in FIPS 140-only mode, use NewGCMWithRandomNonce")
6262
}
6363
return newGCM(cipher, gcmStandardNonceSize, tagSize)
@@ -66,7 +66,7 @@ func NewGCMWithTagSize(cipher Block, tagSize int) (AEAD, error) {
6666
func newGCM(cipher Block, nonceSize, tagSize int) (AEAD, error) {
6767
c, ok := cipher.(*aes.Block)
6868
if !ok {
69-
if fips140only.Enabled {
69+
if fips140only.Enforced() {
7070
return nil, errors.New("crypto/cipher: use of GCM with non-AES ciphers is not allowed in FIPS 140-only mode")
7171
}
7272
return newGCMFallback(cipher, nonceSize, tagSize)

src/crypto/cipher/ofb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type ofb struct {
2929
// OFB is also unoptimized and not validated as part of the FIPS 140-3 module.
3030
// If an unauthenticated [Stream] mode is required, use [NewCTR] instead.
3131
func NewOFB(b Block, iv []byte) Stream {
32-
if fips140only.Enabled {
32+
if fips140only.Enforced() {
3333
panic("crypto/cipher: use of OFB is not allowed in FIPS 140-only mode")
3434
}
3535

src/crypto/des/cipher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type desCipher struct {
2929

3030
// NewCipher creates and returns a new [cipher.Block].
3131
func NewCipher(key []byte) (cipher.Block, error) {
32-
if fips140only.Enabled {
32+
if fips140only.Enforced() {
3333
return nil, errors.New("crypto/des: use of DES is not allowed in FIPS 140-only mode")
3434
}
3535

@@ -77,7 +77,7 @@ type tripleDESCipher struct {
7777

7878
// NewTripleDESCipher creates and returns a new [cipher.Block].
7979
func NewTripleDESCipher(key []byte) (cipher.Block, error) {
80-
if fips140only.Enabled {
80+
if fips140only.Enforced() {
8181
return nil, errors.New("crypto/des: use of TripleDES is not allowed in FIPS 140-only mode")
8282
}
8383

src/crypto/dsa/dsa.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const numMRTests = 64
6464
// GenerateParameters puts a random, valid set of DSA parameters into params.
6565
// This function can take many seconds, even on fast machines.
6666
func GenerateParameters(params *Parameters, rand io.Reader, sizes ParameterSizes) error {
67-
if fips140only.Enabled {
67+
if fips140only.Enforced() {
6868
return errors.New("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
6969
}
7070

@@ -162,7 +162,7 @@ GeneratePrimes:
162162
// GenerateKey generates a public&private key pair. The Parameters of the
163163
// [PrivateKey] must already be valid (see [GenerateParameters]).
164164
func GenerateKey(priv *PrivateKey, rand io.Reader) error {
165-
if fips140only.Enabled {
165+
if fips140only.Enforced() {
166166
return errors.New("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
167167
}
168168

@@ -212,7 +212,7 @@ func fermatInverse(k, P *big.Int) *big.Int {
212212
// Be aware that calling Sign with an attacker-controlled [PrivateKey] may
213213
// require an arbitrary amount of CPU.
214214
func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err error) {
215-
if fips140only.Enabled {
215+
if fips140only.Enforced() {
216216
return nil, nil, errors.New("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
217217
}
218218

@@ -284,7 +284,7 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
284284
// to the byte-length of the subgroup. This function does not perform that
285285
// truncation itself.
286286
func Verify(pub *PublicKey, hash []byte, r, s *big.Int) bool {
287-
if fips140only.Enabled {
287+
if fips140only.Enforced() {
288288
panic("crypto/dsa: use of DSA is not allowed in FIPS 140-only mode")
289289
}
290290

src/crypto/ecdh/nist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *nistCurve) GenerateKey(rand io.Reader) (*PrivateKey, error) {
4444
return k, nil
4545
}
4646

47-
if fips140only.Enabled && !fips140only.ApprovedRandomReader(rand) {
47+
if fips140only.Enforced() && !fips140only.ApprovedRandomReader(rand) {
4848
return nil, errors.New("crypto/ecdh: only crypto/rand.Reader is allowed in FIPS 140-only mode")
4949
}
5050

0 commit comments

Comments
 (0)