crypto/mlkem/mlkemtest: error out in fips140=only mode

Updates #70514

Change-Id: I1d1a0b4a2c7ee4cb6e8e0700dd3463a46a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/728502
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
Filippo Valsorda 2025-12-09 00:39:54 +01:00 committed by Gopher Robot
parent db0ab834d6
commit c39fe18fea

View File

@ -7,6 +7,7 @@ package mlkemtest
import (
fips140mlkem "crypto/internal/fips140/mlkem"
"crypto/internal/fips140only"
"crypto/mlkem"
"errors"
)
@ -20,6 +21,9 @@ func Encapsulate768(ek *mlkem.EncapsulationKey768, random []byte) (sharedKey, ci
if len(random) != 32 {
return nil, nil, errors.New("mlkemtest: Encapsulate768: random must be 32 bytes")
}
if fips140only.Enforced() {
return nil, nil, errors.New("crypto/mlkem/mlkemtest: use of derandomized encapsulation is not allowed in FIPS 140-only mode")
}
k, err := fips140mlkem.NewEncapsulationKey768(ek.Bytes())
if err != nil {
return nil, nil, errors.New("mlkemtest: Encapsulate768: failed to reconstruct key: " + err.Error())
@ -37,6 +41,9 @@ func Encapsulate1024(ek *mlkem.EncapsulationKey1024, random []byte) (sharedKey,
if len(random) != 32 {
return nil, nil, errors.New("mlkemtest: Encapsulate1024: random must be 32 bytes")
}
if fips140only.Enforced() {
return nil, nil, errors.New("crypto/mlkem/mlkemtest: use of derandomized encapsulation is not allowed in FIPS 140-only mode")
}
k, err := fips140mlkem.NewEncapsulationKey1024(ek.Bytes())
if err != nil {
return nil, nil, errors.New("mlkemtest: Encapsulate1024: failed to reconstruct key: " + err.Error())