{"record":{"id":"e0d45b4e15352697","repo":"golang/go","slug":"crypto-rsa-unsupported-hash-function-e0d45b","errorCode":null,"errorMessage":"crypto/rsa: unsupported hash function","messagePattern":"crypto/rsa: unsupported hash function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/internal/fips140/rsa/pkcs1v15.go","lineNumber":73,"sourceCode":"}\n\nfunc signPKCS1v15(priv *PrivateKey, hash string, hashed []byte) ([]byte, error) {\n\tem, err := pkcs1v15ConstructEM(&priv.pub, hash, hashed)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn decrypt(priv, em, withCheck)\n}\n\nfunc pkcs1v15ConstructEM(pub *PublicKey, hash string, hashed []byte) ([]byte, error) {\n\t// Special case: \"\" is used to indicate that the data is signed directly.\n\tvar prefix []byte\n\tif hash != \"\" {\n\t\tvar ok bool\n\t\tprefix, ok = hashPrefixes[hash]\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"crypto/rsa: unsupported hash function\")\n\t\t}\n\t\tif len(hashed) != hashSize(hash) {\n\t\t\treturn nil, errors.New(\"crypto/rsa: hashed message length does not match hash function\")\n\t\t}\n\t}\n\n\t// EM = 0x00 || 0x01 || PS || 0x00 || T\n\tk := pub.Size()\n\tif k < len(prefix)+len(hashed)+2+8+1 {\n\t\treturn nil, ErrMessageTooLong\n\t}\n\tem := make([]byte, k)\n\tem[1] = 1\n\tfor i := 2; i < k-len(prefix)-len(hashed)-1; i++ {\n\t\tem[i] = 0xff\n\t}\n\tcopy(em[k-len(prefix)-len(hashed):], prefix)\n\tcopy(em[k-len(hashed):], hashed)","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/fips140/rsa/pkcs1v15.go#L55-L91","documentation":"pkcs1v15ConstructEM looks up the hash function name in the hashPrefixes map to get the DER-encoded AlgorithmIdentifier prefix for PKCS#1 v1.5 signature padding. Supported names are: MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256, SHA3-224, SHA3-256, SHA3-384, SHA3-512, MD5+SHA1 (TLS special case), and RIPEMD-160. An unrecognized name yields this error.","triggerScenarios":"Calling SignPKCS1v15 or VerifyPKCS1v15 (which internally call pkcs1v15ConstructEM) with a hash crypto.Hash whose String() method returns a name not in the hashPrefixes map — for example, hash.Hash(0), BLAKE2b, or an entirely custom hash.","commonSituations":"Using a non-standard hash function (BLAKE2, BLAKE3) that is not in the FIPS-approved set; passing crypto.Hash(0) (no hash, direct signing) but with an empty prefix mismatch; using a hash constant from a different library that doesn't match Go's crypto.Hash enum.","solutions":["Use one of the supported hash functions: crypto.SHA256, crypto.SHA384, crypto.SHA512, etc.","Pass hash.Hash.String() correctly — ensure you're passing the hash name, not the hash value","For direct signing without a hash prefix, pass hash 0 and empty hashed message as specified by the API"],"exampleFix":"// before\nsig, err := rsa.SignPKCS1v15(rand, key, crypto.BLAKE2b_256, hashed) // not supported\n\n// after\nsig, err := rsa.SignPKCS1v15(rand, key, crypto.SHA256, hashedSHA256)","handlingStrategy":"validation","validationCode":"var supportedPKCS1v15Hashes = map[crypto.Hash]bool{\n    crypto.MD5: true, crypto.SHA1: true, crypto.SHA224: true,\n    crypto.SHA256: true, crypto.SHA384: true, crypto.SHA512: true,\n    crypto.SHA512_224: true, crypto.SHA512_256: true,\n    crypto.SHA3_224: true, crypto.SHA3_256: true,\n    crypto.SHA3_384: true, crypto.SHA3_512: true,\n}\n\nfunc isSupportedPKCS1v15Hash(h crypto.Hash) bool {\n    return supportedPKCS1v15Hashes[h]\n}\n\nif !isSupportedPKCS1v15Hash(hashAlg) {\n    return errors.New(\"unsupported hash for PKCS#1 v1.5\")\n}","typeGuard":null,"tryCatchPattern":"sig, err := rsa.SignPKCS1v15(rand, key, hashAlg, hashed)\nif err != nil {\n    return fmt.Errorf(\"PKCS#1 v1.5 signing failed: %w\", err)\n}","preventionTips":["Stick to SHA-256, SHA-384, or SHA-512 for new RSA signatures","Avoid BLAKE2/BLAKE3 with PKCS#1 v1.5 — they are not in the supported set","Pass the crypto.Hash enum constant, not a string literal"],"tags":["crypto","fips140","rsa","pkcs1v15","hash-function","input-validation"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}