mirror of
https://github.com/golang/go.git
synced 2025-05-06 08:03:03 +00:00
crypto: fix fips140=only detection of SHA-3
Both fips140only and the service indicator checks in crypto/internal/fips140/... expect to type assert to crypto/internal/fips140/{sha256,sha512,sha3}.Digest. However, crypto/sha3 returns a wrapper concrete type around sha3.Digest. Add a new fips140hash.Unwrap function to turn the wrapper into the underlying sha3.Digest, and use it consistently before calling into fips140only or the FIPS 140-3 module. In crypto/rsa, also made the fips140only checks apply consistently after the Go+BoringCrypto shims, so we can instantiate the hash, and avoid having to wrap the New function. Note that fips140=only is incompatible with Go+BoringCrypto. Fixes #70879 Change-Id: I6a6a4656ec55c3e13f6cbfadb9cf89c0f9183bdc Reviewed-on: https://go-review.googlesource.com/c/go/+/640855 Auto-Submit: Filippo Valsorda <filippo@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
4225c6cb37
commit
f5a89dff67
@ -23,6 +23,7 @@ import (
|
|||||||
"crypto/internal/boring"
|
"crypto/internal/boring"
|
||||||
"crypto/internal/boring/bbig"
|
"crypto/internal/boring/bbig"
|
||||||
"crypto/internal/fips140/ecdsa"
|
"crypto/internal/fips140/ecdsa"
|
||||||
|
"crypto/internal/fips140hash"
|
||||||
"crypto/internal/fips140only"
|
"crypto/internal/fips140only"
|
||||||
"crypto/internal/randutil"
|
"crypto/internal/randutil"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
@ -281,10 +282,11 @@ func signFIPSDeterministic[P ecdsa.Point[P]](c *ecdsa.Curve[P], hashFunc crypto.
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if fips140only.Enabled && !fips140only.ApprovedHash(hashFunc.New()) {
|
h := fips140hash.UnwrapNew(hashFunc.New)
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedHash(h()) {
|
||||||
return nil, errors.New("crypto/ecdsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
return nil, errors.New("crypto/ecdsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
}
|
}
|
||||||
sig, err := ecdsa.SignDeterministic(c, hashFunc.New, k, hash)
|
sig, err := ecdsa.SignDeterministic(c, h, k, hash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ package hkdf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/internal/fips140/hkdf"
|
"crypto/internal/fips140/hkdf"
|
||||||
|
"crypto/internal/fips140hash"
|
||||||
"crypto/internal/fips140only"
|
"crypto/internal/fips140only"
|
||||||
"errors"
|
"errors"
|
||||||
"hash"
|
"hash"
|
||||||
@ -24,10 +25,11 @@ import (
|
|||||||
// Expand invocations and different context values. Most common scenarios,
|
// Expand invocations and different context values. Most common scenarios,
|
||||||
// including the generation of multiple keys, should use [Key] instead.
|
// including the generation of multiple keys, should use [Key] instead.
|
||||||
func Extract[H hash.Hash](h func() H, secret, salt []byte) ([]byte, error) {
|
func Extract[H hash.Hash](h func() H, secret, salt []byte) ([]byte, error) {
|
||||||
if err := checkFIPS140Only(h, secret); err != nil {
|
fh := fips140hash.UnwrapNew(h)
|
||||||
|
if err := checkFIPS140Only(fh, secret); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return hkdf.Extract(h, secret, salt), nil
|
return hkdf.Extract(fh, secret, salt), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expand derives a key from the given hash, key, and optional context info,
|
// Expand derives a key from the given hash, key, and optional context info,
|
||||||
@ -38,35 +40,37 @@ func Extract[H hash.Hash](h func() H, secret, salt []byte) ([]byte, error) {
|
|||||||
// random or pseudorandom cryptographically strong key. See RFC 5869, Section
|
// random or pseudorandom cryptographically strong key. See RFC 5869, Section
|
||||||
// 3.3. Most common scenarios will want to use [Key] instead.
|
// 3.3. Most common scenarios will want to use [Key] instead.
|
||||||
func Expand[H hash.Hash](h func() H, pseudorandomKey []byte, info string, keyLength int) ([]byte, error) {
|
func Expand[H hash.Hash](h func() H, pseudorandomKey []byte, info string, keyLength int) ([]byte, error) {
|
||||||
if err := checkFIPS140Only(h, pseudorandomKey); err != nil {
|
fh := fips140hash.UnwrapNew(h)
|
||||||
|
if err := checkFIPS140Only(fh, pseudorandomKey); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
limit := h().Size() * 255
|
limit := fh().Size() * 255
|
||||||
if keyLength > limit {
|
if keyLength > limit {
|
||||||
return nil, errors.New("hkdf: requested key length too large")
|
return nil, errors.New("hkdf: requested key length too large")
|
||||||
}
|
}
|
||||||
|
|
||||||
return hkdf.Expand(h, pseudorandomKey, info, keyLength), nil
|
return hkdf.Expand(fh, pseudorandomKey, info, keyLength), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Key derives a key from the given hash, secret, salt and context info,
|
// Key derives a key from the given hash, secret, salt and context info,
|
||||||
// returning a []byte of length keyLength that can be used as cryptographic key.
|
// returning a []byte of length keyLength that can be used as cryptographic key.
|
||||||
// Salt and info can be nil.
|
// Salt and info can be nil.
|
||||||
func Key[Hash hash.Hash](h func() Hash, secret, salt []byte, info string, keyLength int) ([]byte, error) {
|
func Key[Hash hash.Hash](h func() Hash, secret, salt []byte, info string, keyLength int) ([]byte, error) {
|
||||||
if err := checkFIPS140Only(h, secret); err != nil {
|
fh := fips140hash.UnwrapNew(h)
|
||||||
|
if err := checkFIPS140Only(fh, secret); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
limit := h().Size() * 255
|
limit := fh().Size() * 255
|
||||||
if keyLength > limit {
|
if keyLength > limit {
|
||||||
return nil, errors.New("hkdf: requested key length too large")
|
return nil, errors.New("hkdf: requested key length too large")
|
||||||
}
|
}
|
||||||
|
|
||||||
return hkdf.Key(h, secret, salt, info, keyLength), nil
|
return hkdf.Key(fh, secret, salt, info, keyLength), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkFIPS140Only[H hash.Hash](h func() H, key []byte) error {
|
func checkFIPS140Only[Hash hash.Hash](h func() Hash, key []byte) error {
|
||||||
if !fips140only.Enabled {
|
if !fips140only.Enabled {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ package hmac
|
|||||||
import (
|
import (
|
||||||
"crypto/internal/boring"
|
"crypto/internal/boring"
|
||||||
"crypto/internal/fips140/hmac"
|
"crypto/internal/fips140/hmac"
|
||||||
|
"crypto/internal/fips140hash"
|
||||||
"crypto/internal/fips140only"
|
"crypto/internal/fips140only"
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"hash"
|
"hash"
|
||||||
@ -43,6 +44,7 @@ func New(h func() hash.Hash, key []byte) hash.Hash {
|
|||||||
}
|
}
|
||||||
// BoringCrypto did not recognize h, so fall through to standard Go code.
|
// BoringCrypto did not recognize h, so fall through to standard Go code.
|
||||||
}
|
}
|
||||||
|
h = fips140hash.UnwrapNew(h)
|
||||||
if fips140only.Enabled {
|
if fips140only.Enabled {
|
||||||
if len(key) < 112/8 {
|
if len(key) < 112/8 {
|
||||||
panic("crypto/hmac: use of keys shorter than 112 bits is not allowed in FIPS 140-only mode")
|
panic("crypto/hmac: use of keys shorter than 112 bits is not allowed in FIPS 140-only mode")
|
||||||
|
34
src/crypto/internal/fips140hash/hash.go
Normal file
34
src/crypto/internal/fips140hash/hash.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright 2024 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package fips140hash
|
||||||
|
|
||||||
|
import (
|
||||||
|
fsha3 "crypto/internal/fips140/sha3"
|
||||||
|
"crypto/sha3"
|
||||||
|
"hash"
|
||||||
|
_ "unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:linkname sha3Unwrap
|
||||||
|
func sha3Unwrap(*sha3.SHA3) *fsha3.Digest
|
||||||
|
|
||||||
|
// Unwrap returns h, or a crypto/internal/fips140 inner implementation of h.
|
||||||
|
//
|
||||||
|
// The return value can be type asserted to one of
|
||||||
|
// [crypto/internal/fips140/sha256.Digest],
|
||||||
|
// [crypto/internal/fips140/sha512.Digest], or
|
||||||
|
// [crypto/internal/fips140/sha3.Digest] if it is a FIPS 140-3 approved hash.
|
||||||
|
func Unwrap(h hash.Hash) hash.Hash {
|
||||||
|
if sha3, ok := h.(*sha3.SHA3); ok {
|
||||||
|
return sha3Unwrap(sha3)
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnwrapNew returns a function that calls newHash and applies [Unwrap] to the
|
||||||
|
// return value.
|
||||||
|
func UnwrapNew[Hash hash.Hash](newHash func() Hash) func() hash.Hash {
|
||||||
|
return func() hash.Hash { return Unwrap(newHash()) }
|
||||||
|
}
|
@ -12,6 +12,7 @@ package pbkdf2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/internal/fips140/pbkdf2"
|
"crypto/internal/fips140/pbkdf2"
|
||||||
|
"crypto/internal/fips140hash"
|
||||||
"crypto/internal/fips140only"
|
"crypto/internal/fips140only"
|
||||||
"errors"
|
"errors"
|
||||||
"hash"
|
"hash"
|
||||||
@ -34,6 +35,7 @@ import (
|
|||||||
// Using a higher iteration count will increase the cost of an exhaustive
|
// Using a higher iteration count will increase the cost of an exhaustive
|
||||||
// search but will also make derivation proportionally slower.
|
// search but will also make derivation proportionally slower.
|
||||||
func Key[Hash hash.Hash](h func() Hash, password string, salt []byte, iter, keyLength int) ([]byte, error) {
|
func Key[Hash hash.Hash](h func() Hash, password string, salt []byte, iter, keyLength int) ([]byte, error) {
|
||||||
|
fh := fips140hash.UnwrapNew(h)
|
||||||
if fips140only.Enabled {
|
if fips140only.Enabled {
|
||||||
if keyLength < 112/8 {
|
if keyLength < 112/8 {
|
||||||
return nil, errors.New("crypto/pbkdf2: use of keys shorter than 112 bits is not allowed in FIPS 140-only mode")
|
return nil, errors.New("crypto/pbkdf2: use of keys shorter than 112 bits is not allowed in FIPS 140-only mode")
|
||||||
@ -41,9 +43,9 @@ func Key[Hash hash.Hash](h func() Hash, password string, salt []byte, iter, keyL
|
|||||||
if len(salt) < 128/8 {
|
if len(salt) < 128/8 {
|
||||||
return nil, errors.New("crypto/pbkdf2: use of salts shorter than 128 bits is not allowed in FIPS 140-only mode")
|
return nil, errors.New("crypto/pbkdf2: use of salts shorter than 128 bits is not allowed in FIPS 140-only mode")
|
||||||
}
|
}
|
||||||
if !fips140only.ApprovedHash(h()) {
|
if !fips140only.ApprovedHash(fh()) {
|
||||||
return nil, errors.New("crypto/pbkdf2: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
return nil, errors.New("crypto/pbkdf2: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pbkdf2.Key(h, password, salt, iter, keyLength)
|
return pbkdf2.Key(fh, password, salt, iter, keyLength)
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"crypto"
|
"crypto"
|
||||||
"crypto/internal/boring"
|
"crypto/internal/boring"
|
||||||
"crypto/internal/fips140/rsa"
|
"crypto/internal/fips140/rsa"
|
||||||
|
"crypto/internal/fips140hash"
|
||||||
"crypto/internal/fips140only"
|
"crypto/internal/fips140only"
|
||||||
"errors"
|
"errors"
|
||||||
"hash"
|
"hash"
|
||||||
@ -64,21 +65,11 @@ func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, digest []byte,
|
|||||||
if err := checkPublicKeySize(&priv.PublicKey); err != nil {
|
if err := checkPublicKeySize(&priv.PublicKey); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := checkFIPS140OnlyPrivateKey(priv); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts != nil && opts.Hash != 0 {
|
if opts != nil && opts.Hash != 0 {
|
||||||
hash = opts.Hash
|
hash = opts.Hash
|
||||||
}
|
}
|
||||||
|
|
||||||
if fips140only.Enabled && !fips140only.ApprovedHash(hash.New()) {
|
|
||||||
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
if fips140only.Enabled && !fips140only.ApprovedRandomReader(rand) {
|
|
||||||
return nil, errors.New("crypto/rsa: only crypto/rand.Reader is allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
if boring.Enabled && rand == boring.RandReader {
|
if boring.Enabled && rand == boring.RandReader {
|
||||||
bkey, err := boringPrivateKey(priv)
|
bkey, err := boringPrivateKey(priv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -88,14 +79,25 @@ func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, digest []byte,
|
|||||||
}
|
}
|
||||||
boring.UnreachableExceptTests()
|
boring.UnreachableExceptTests()
|
||||||
|
|
||||||
|
h := fips140hash.Unwrap(hash.New())
|
||||||
|
|
||||||
|
if err := checkFIPS140OnlyPrivateKey(priv); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedHash(h) {
|
||||||
|
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedRandomReader(rand) {
|
||||||
|
return nil, errors.New("crypto/rsa: only crypto/rand.Reader is allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
|
||||||
k, err := fipsPrivateKey(priv)
|
k, err := fipsPrivateKey(priv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
h := hash.New()
|
|
||||||
|
|
||||||
saltLength := opts.saltLength()
|
saltLength := opts.saltLength()
|
||||||
if fips140only.Enabled && saltLength > hash.Size() {
|
if fips140only.Enabled && saltLength > h.Size() {
|
||||||
return nil, errors.New("crypto/rsa: use of PSS salt longer than the hash is not allowed in FIPS 140-only mode")
|
return nil, errors.New("crypto/rsa: use of PSS salt longer than the hash is not allowed in FIPS 140-only mode")
|
||||||
}
|
}
|
||||||
switch saltLength {
|
switch saltLength {
|
||||||
@ -105,7 +107,7 @@ func SignPSS(rand io.Reader, priv *PrivateKey, hash crypto.Hash, digest []byte,
|
|||||||
return nil, fipsError(err)
|
return nil, fipsError(err)
|
||||||
}
|
}
|
||||||
case PSSSaltLengthEqualsHash:
|
case PSSSaltLengthEqualsHash:
|
||||||
saltLength = hash.Size()
|
saltLength = h.Size()
|
||||||
default:
|
default:
|
||||||
// If we get here saltLength is either > 0 or < -1, in the
|
// If we get here saltLength is either > 0 or < -1, in the
|
||||||
// latter case we fail out.
|
// latter case we fail out.
|
||||||
@ -130,12 +132,6 @@ func VerifyPSS(pub *PublicKey, hash crypto.Hash, digest []byte, sig []byte, opts
|
|||||||
if err := checkPublicKeySize(pub); err != nil {
|
if err := checkPublicKeySize(pub); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := checkFIPS140OnlyPublicKey(pub); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if fips140only.Enabled && !fips140only.ApprovedHash(hash.New()) {
|
|
||||||
return errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
if boring.Enabled {
|
if boring.Enabled {
|
||||||
bkey, err := boringPublicKey(pub)
|
bkey, err := boringPublicKey(pub)
|
||||||
@ -148,22 +144,31 @@ func VerifyPSS(pub *PublicKey, hash crypto.Hash, digest []byte, sig []byte, opts
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h := fips140hash.Unwrap(hash.New())
|
||||||
|
|
||||||
|
if err := checkFIPS140OnlyPublicKey(pub); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedHash(h) {
|
||||||
|
return errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
|
||||||
k, err := fipsPublicKey(pub)
|
k, err := fipsPublicKey(pub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
saltLength := opts.saltLength()
|
saltLength := opts.saltLength()
|
||||||
if fips140only.Enabled && saltLength > hash.Size() {
|
if fips140only.Enabled && saltLength > h.Size() {
|
||||||
return errors.New("crypto/rsa: use of PSS salt longer than the hash is not allowed in FIPS 140-only mode")
|
return errors.New("crypto/rsa: use of PSS salt longer than the hash is not allowed in FIPS 140-only mode")
|
||||||
}
|
}
|
||||||
switch saltLength {
|
switch saltLength {
|
||||||
case PSSSaltLengthAuto:
|
case PSSSaltLengthAuto:
|
||||||
return fipsError(rsa.VerifyPSS(k, hash.New(), digest, sig))
|
return fipsError(rsa.VerifyPSS(k, h, digest, sig))
|
||||||
case PSSSaltLengthEqualsHash:
|
case PSSSaltLengthEqualsHash:
|
||||||
return fipsError(rsa.VerifyPSSWithSaltLength(k, hash.New(), digest, sig, hash.Size()))
|
return fipsError(rsa.VerifyPSSWithSaltLength(k, h, digest, sig, h.Size()))
|
||||||
default:
|
default:
|
||||||
return fipsError(rsa.VerifyPSSWithSaltLength(k, hash.New(), digest, sig, saltLength))
|
return fipsError(rsa.VerifyPSSWithSaltLength(k, h, digest, sig, saltLength))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,15 +194,6 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l
|
|||||||
if err := checkPublicKeySize(pub); err != nil {
|
if err := checkPublicKeySize(pub); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := checkFIPS140OnlyPublicKey(pub); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if fips140only.Enabled && !fips140only.ApprovedHash(hash) {
|
|
||||||
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
if fips140only.Enabled && !fips140only.ApprovedRandomReader(random) {
|
|
||||||
return nil, errors.New("crypto/rsa: only crypto/rand.Reader is allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
defer hash.Reset()
|
defer hash.Reset()
|
||||||
|
|
||||||
@ -215,6 +211,18 @@ func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, l
|
|||||||
}
|
}
|
||||||
boring.UnreachableExceptTests()
|
boring.UnreachableExceptTests()
|
||||||
|
|
||||||
|
hash = fips140hash.Unwrap(hash)
|
||||||
|
|
||||||
|
if err := checkFIPS140OnlyPublicKey(pub); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedHash(hash) {
|
||||||
|
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedRandomReader(random) {
|
||||||
|
return nil, errors.New("crypto/rsa: only crypto/rand.Reader is allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
|
||||||
k, err := fipsPublicKey(pub)
|
k, err := fipsPublicKey(pub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -241,14 +249,6 @@ func decryptOAEP(hash, mgfHash hash.Hash, priv *PrivateKey, ciphertext []byte, l
|
|||||||
if err := checkPublicKeySize(&priv.PublicKey); err != nil {
|
if err := checkPublicKeySize(&priv.PublicKey); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := checkFIPS140OnlyPrivateKey(priv); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if fips140only.Enabled {
|
|
||||||
if !fips140only.ApprovedHash(hash) || !fips140only.ApprovedHash(mgfHash) {
|
|
||||||
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if boring.Enabled {
|
if boring.Enabled {
|
||||||
k := priv.Size()
|
k := priv.Size()
|
||||||
@ -267,6 +267,18 @@ func decryptOAEP(hash, mgfHash hash.Hash, priv *PrivateKey, ciphertext []byte, l
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hash = fips140hash.Unwrap(hash)
|
||||||
|
mgfHash = fips140hash.Unwrap(mgfHash)
|
||||||
|
|
||||||
|
if err := checkFIPS140OnlyPrivateKey(priv); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if fips140only.Enabled {
|
||||||
|
if !fips140only.ApprovedHash(hash) || !fips140only.ApprovedHash(mgfHash) {
|
||||||
|
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
k, err := fipsPrivateKey(priv)
|
k, err := fipsPrivateKey(priv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -299,12 +311,6 @@ func SignPKCS1v15(random io.Reader, priv *PrivateKey, hash crypto.Hash, hashed [
|
|||||||
if err := checkPublicKeySize(&priv.PublicKey); err != nil {
|
if err := checkPublicKeySize(&priv.PublicKey); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := checkFIPS140OnlyPrivateKey(priv); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if fips140only.Enabled && !fips140only.ApprovedHash(hash.New()) {
|
|
||||||
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
if boring.Enabled {
|
if boring.Enabled {
|
||||||
bkey, err := boringPrivateKey(priv)
|
bkey, err := boringPrivateKey(priv)
|
||||||
@ -314,6 +320,13 @@ func SignPKCS1v15(random io.Reader, priv *PrivateKey, hash crypto.Hash, hashed [
|
|||||||
return boring.SignRSAPKCS1v15(bkey, hash, hashed)
|
return boring.SignRSAPKCS1v15(bkey, hash, hashed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := checkFIPS140OnlyPrivateKey(priv); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedHash(fips140hash.Unwrap(hash.New())) {
|
||||||
|
return nil, errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
|
||||||
k, err := fipsPrivateKey(priv)
|
k, err := fipsPrivateKey(priv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -330,15 +343,17 @@ func SignPKCS1v15(random io.Reader, priv *PrivateKey, hash crypto.Hash, hashed [
|
|||||||
// The inputs are not considered confidential, and may leak through timing side
|
// The inputs are not considered confidential, and may leak through timing side
|
||||||
// channels, or if an attacker has control of part of the inputs.
|
// channels, or if an attacker has control of part of the inputs.
|
||||||
func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
|
func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
|
||||||
|
var hashName string
|
||||||
|
if hash != crypto.Hash(0) {
|
||||||
|
if len(hashed) != hash.Size() {
|
||||||
|
return errors.New("crypto/rsa: input must be hashed message")
|
||||||
|
}
|
||||||
|
hashName = hash.String()
|
||||||
|
}
|
||||||
|
|
||||||
if err := checkPublicKeySize(pub); err != nil {
|
if err := checkPublicKeySize(pub); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := checkFIPS140OnlyPublicKey(pub); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if fips140only.Enabled && !fips140only.ApprovedHash(hash.New()) {
|
|
||||||
return errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
if boring.Enabled {
|
if boring.Enabled {
|
||||||
bkey, err := boringPublicKey(pub)
|
bkey, err := boringPublicKey(pub)
|
||||||
@ -351,17 +366,17 @@ func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := checkFIPS140OnlyPublicKey(pub); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if fips140only.Enabled && !fips140only.ApprovedHash(fips140hash.Unwrap(hash.New())) {
|
||||||
|
return errors.New("crypto/rsa: use of hash functions other than SHA-2 or SHA-3 is not allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
|
||||||
k, err := fipsPublicKey(pub)
|
k, err := fipsPublicKey(pub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var hashName string
|
|
||||||
if hash != crypto.Hash(0) {
|
|
||||||
if len(hashed) != hash.Size() {
|
|
||||||
return errors.New("crypto/rsa: input must be hashed message")
|
|
||||||
}
|
|
||||||
hashName = hash.String()
|
|
||||||
}
|
|
||||||
return fipsError(rsa.VerifyPKCS1v15(k, hashName, hashed, sig))
|
return fipsError(rsa.VerifyPKCS1v15(k, hashName, hashed, sig))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"crypto"
|
"crypto"
|
||||||
"crypto/internal/fips140/sha3"
|
"crypto/internal/fips140/sha3"
|
||||||
"hash"
|
"hash"
|
||||||
|
_ "unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -100,6 +101,11 @@ type SHA3 struct {
|
|||||||
s sha3.Digest
|
s sha3.Digest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:linkname fips140hash_sha3Unwrap crypto/internal/fips140hash.sha3Unwrap
|
||||||
|
func fips140hash_sha3Unwrap(sha3 *SHA3) *sha3.Digest {
|
||||||
|
return &sha3.s
|
||||||
|
}
|
||||||
|
|
||||||
// New224 creates a new SHA3-224 hash.
|
// New224 creates a new SHA3-224 hash.
|
||||||
func New224() *SHA3 {
|
func New224() *SHA3 {
|
||||||
return &SHA3{*sha3.New224()}
|
return &SHA3{*sha3.New224()}
|
||||||
|
@ -510,6 +510,8 @@ var depsRules = `
|
|||||||
< crypto/internal/fips140only
|
< crypto/internal/fips140only
|
||||||
< crypto
|
< crypto
|
||||||
< crypto/subtle
|
< crypto/subtle
|
||||||
|
< crypto/sha3
|
||||||
|
< crypto/internal/fips140hash
|
||||||
< crypto/cipher
|
< crypto/cipher
|
||||||
< crypto/internal/boring
|
< crypto/internal/boring
|
||||||
< crypto/boring
|
< crypto/boring
|
||||||
@ -520,7 +522,6 @@ var depsRules = `
|
|||||||
crypto/sha1,
|
crypto/sha1,
|
||||||
crypto/sha256,
|
crypto/sha256,
|
||||||
crypto/sha512,
|
crypto/sha512,
|
||||||
crypto/sha3,
|
|
||||||
crypto/hmac,
|
crypto/hmac,
|
||||||
crypto/hkdf,
|
crypto/hkdf,
|
||||||
crypto/pbkdf2,
|
crypto/pbkdf2,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user