golang/go · error

crypto/rsa: requested hash function unavailable:

Error message

crypto/rsa: requested hash function unavailable: 

What it means

Returned inside RSA PSS signing when the requested hash is not available — hash.Available() returns false. This happens when the hash package's constructor is not linked into the binary (e.g., the hash is in a package not imported anywhere) so hash.New would panic. The guard prevents that panic with an explicit, descriptive error naming the hash.

Source

Thrown at src/crypto/rsa/fips.go:86

	}

	if opts != nil && opts.Hash != 0 {
		hash = opts.Hash
	}

	if boring.Enabled && rand.IsDefaultReader(random) && priv.N.BitLen() >= 1024 {
		bkey, err := boringPrivateKey(priv)
		if err != nil {
			return nil, err
		}
		return boring.SignRSAPSS(bkey, hash, digest, opts.saltLength())
	}
	if priv.N.BitLen() >= 1024 {
		boring.UnreachableExceptTests()
	}

	if !hash.Available() {
		return nil, errors.New("crypto/rsa: requested hash function unavailable: " + hash.String())
	}
	h := fips140hash.Unwrap(hash.New())

	if err := checkFIPS140OnlyPrivateKey(priv); err != nil {
		return nil, err
	}
	if fips140only.Enforced() && !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.Enforced() && !fips140only.ApprovedRandomReader(random) {
		return nil, errors.New("crypto/rsa: only crypto/rand.Reader is allowed in FIPS 140-only mode")
	}

	k, err := fipsPrivateKey(priv)
	if err != nil {
		return nil, err
	}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Ensure the hash implementation is imported: add a blank import like _ "crypto/sha512" if needed.
  2. Confirm hash.Available() is true before signing.
  3. Switch to a hash whose package is already imported in the build.

Example fix

// before
import "crypto/rsa"
// sha512 package never imported -> hash.Available() false
sig, err := rsa.SignPSS(rand.Reader, priv, crypto.SHA512, digest, opts)

// after
import (
  "crypto/rsa"
  _ "crypto/sha512"
)
sig, err := rsa.SignPSS(rand.Reader, priv, crypto.SHA512, digest, opts)
Defensive patterns

Strategy: validation

Validate before calling

if !hash.Available() {
    return nil, fmt.Errorf("hash %s not linked into binary", hash)
}
return rsa.SignPSS(rand.Reader, priv, hash, digest, opts)

Type guard

func hashLinked(h crypto.Hash) bool { return h.Available() }

Try / catch

sig, err := rsa.SignPSS(rand.Reader, priv, hash, digest, opts)
if err != nil && strings.Contains(err.Error(), "hash function unavailable") {
    // import the hash package or switch to crypto.SHA256
}
return sig, err

Prevention

When it happens

Trigger: Referencing a crypto.Hash constant (e.g., crypto.SHA512) whose implementing package is not imported anywhere in the build, causing hash.Available() to be false. Using a hash identifier the linker dead-stripped.

Common situations: Binary-stripped builds where only some hash implementations are retained. Code paths that select a hash via a constant without importing the hash package. Cross-compiling with reduced crypto subsets.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/bf43d19f83aafdd4. Report an issue: GitHub.