{"record":{"id":"b83bb7a1df2e2fe4","repo":"golang/go","slug":"crypto-rsa-unsupported-hash-function","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/boring/rsa.go","lineNumber":149,"sourceCode":"\tif withKey(func(key *C.GO_RSA) C.int {\n\t\treturn C._goboringcrypto_EVP_PKEY_set1_RSA(pkey, key)\n\t}) == 0 {\n\t\treturn pkey, ctx, fail(\"EVP_PKEY_set1_RSA\")\n\t}\n\tctx = C._goboringcrypto_EVP_PKEY_CTX_new(pkey, nil)\n\tif ctx == nil {\n\t\treturn pkey, ctx, fail(\"EVP_PKEY_CTX_new\")\n\t}\n\tif init(ctx) == 0 {\n\t\treturn pkey, ctx, fail(\"EVP_PKEY_operation_init\")\n\t}\n\tif C._goboringcrypto_EVP_PKEY_CTX_set_rsa_padding(ctx, padding) == 0 {\n\t\treturn pkey, ctx, fail(\"EVP_PKEY_CTX_set_rsa_padding\")\n\t}\n\tif padding == C.GO_RSA_PKCS1_OAEP_PADDING {\n\t\tmd := hashToMD(h)\n\t\tif md == nil {\n\t\t\treturn pkey, ctx, errors.New(\"crypto/rsa: unsupported hash function\")\n\t\t}\n\t\tmgfMD := hashToMD(mgfHash)\n\t\tif mgfMD == nil {\n\t\t\treturn pkey, ctx, errors.New(\"crypto/rsa: unsupported hash function\")\n\t\t}\n\t\tif C._goboringcrypto_EVP_PKEY_CTX_set_rsa_oaep_md(ctx, md) == 0 {\n\t\t\treturn pkey, ctx, fail(\"EVP_PKEY_set_rsa_oaep_md\")\n\t\t}\n\t\tif C._goboringcrypto_EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, mgfMD) == 0 {\n\t\t\treturn pkey, ctx, fail(\"EVP_PKEY_set_rsa_mgf1_md\")\n\t\t}\n\t\t// ctx takes ownership of label, so malloc a copy for BoringCrypto to free.\n\t\tclabel := (*C.uint8_t)(C._goboringcrypto_OPENSSL_malloc(C.size_t(len(label))))\n\t\tif clabel == nil {\n\t\t\treturn pkey, ctx, fail(\"OPENSSL_malloc\")\n\t\t}\n\t\tcopy((*[1 << 30]byte)(unsafe.Pointer(clabel))[:len(label)], label)\n\t\tif C._goboringcrypto_EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, clabel, C.size_t(len(label))) == 0 {","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/boring/rsa.go#L131-L167","documentation":"Raised in setupRSA (OAEP path) when hashToMD(h) returns nil — the hash.Hash concrete type is not one of the boring wrappers (sha1/sha224/sha256/sha384/sha512Hash). BoringCrypto can only drive OAEP with a hash it recognises, so an unrecognised hash implementation is rejected before the EVP context is configured.","triggerScenarios":"Calling RSA OAEP encrypt/decrypt through the boring backend with an h hash.Hash whose concrete type is not one of the five supported boring sha wrappers. This is an internal API; externally it surfaces when crypto/rsa OAEP is used with a hash the boring shim does not map.","commonSituations":"Using a third-party hash (BLAKE2, SHA-3, etc.) with RSA-OAEP under a boringcrypto Go build; passing a standard crypto/sha256 instance where the boring wrapper is expected internally.","solutions":["Use one of SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512 for OAEP under boringcrypto.","If a non-SHA-2 hash is required, use a non-boring Go build or a pure-Go RSA implementation.","Confirm the hash instance originates from the boring shim's expected constructors."],"exampleFix":"// before (boringcrypto build)\nerr := rsa.EncryptOAEP(blake2b.New256, rand, pub, msg, label) // hashToMD -> nil\n\n// after\nerr := rsa.EncryptOAEP(sha256.New, rand, pub, msg, label)","handlingStrategy":"validation","validationCode":"func isBoringOAEPHash(h hash.Hash) bool {\n    switch h.(type) {\n    case *sha1Hash, *sha224Hash, *sha256Hash, *sha384Hash, *sha512Hash:\n        return true\n    }\n    return false\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use SHA-1/224/256/384/512 for OAEP under boringcrypto.","Avoid third-party hash types with OAEP on a FIPS build.","For SHA-3 OAEP, switch to a non-boring build."],"tags":["crypto","rsa","oaep","boringcrypto","fips","hash"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}