Skip to content

Commit 7dacc38

Browse files
FiloSottilegopherbot
authored andcommitted
chacha20poly1305: error out in fips140=only mode
We don't guarantee fips140=only support in x/crypto, but chacha20poly1305 is special in that it's vendored into the standard library. We could wrap all the callsites, but it's more robust to just error out at construction time. Change-Id: I4b1e451bd250429c4c5c5b61c8b2141c6a6a6964 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/728480 Reviewed-by: Roland Shoemaker <roland@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Auto-Submit: Filippo Valsorda <filippo@golang.org>
1 parent 19acf81 commit 7dacc38

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

chacha20poly1305/chacha20poly1305.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type chacha20poly1305 struct {
3838

3939
// New returns a ChaCha20-Poly1305 AEAD that uses the given 256-bit key.
4040
func New(key []byte) (cipher.AEAD, error) {
41+
if fips140Enforced() {
42+
return nil, errors.New("chacha20poly1305: use of ChaCha20Poly1305 is not allowed in FIPS 140-only mode")
43+
}
4144
if len(key) != KeySize {
4245
return nil, errors.New("chacha20poly1305: bad key length")
4346
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2025 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+
//go:build !go1.26
6+
7+
package chacha20poly1305
8+
9+
func fips140Enforced() bool { return false }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2025 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+
//go:build go1.26
6+
7+
package chacha20poly1305
8+
9+
import "crypto/fips140"
10+
11+
func fips140Enforced() bool { return fips140.Enforced() }

chacha20poly1305/xchacha20poly1305.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ type xchacha20poly1305 struct {
2222
// preferred when nonce uniqueness cannot be trivially ensured, or whenever
2323
// nonces are randomly generated.
2424
func NewX(key []byte) (cipher.AEAD, error) {
25+
if fips140Enforced() {
26+
return nil, errors.New("chacha20poly1305: use of ChaCha20Poly1305 is not allowed in FIPS 140-only mode")
27+
}
2528
if len(key) != KeySize {
2629
return nil, errors.New("chacha20poly1305: bad key length")
2730
}

0 commit comments

Comments
 (0)