{"record":{"id":"278cd2bcabbbf188","repo":"golang/go","slug":"crypto-rsa-missing-public-modulus","errorCode":null,"errorMessage":"crypto/rsa: missing public modulus","messagePattern":"crypto/rsa: missing public modulus","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/internal/fips140/rsa/rsa.go","lineNumber":329,"sourceCode":"\t}\n\n\t// Check that d > 2^(nlen/2).\n\t//\n\t// See section 3 of https://crypto.stanford.edu/~dabo/papers/RSA-survey.pdf\n\t// for more details about attacks on small d values.\n\t//\n\t// Likewise, the leakage of the magnitude of d is not adaptive.\n\tif priv.d.BitLenVarTime() <= N.BitLen()/2 {\n\t\treturn errors.New(\"crypto/rsa: d too small\")\n\t}\n\n\treturn nil\n}\n\nfunc checkPublicKey(pub *PublicKey) (fipsApproved bool, err error) {\n\tfipsApproved = true\n\tif pub.N == nil {\n\t\treturn false, errors.New(\"crypto/rsa: missing public modulus\")\n\t}\n\tif pub.N.Nat().IsOdd() == 0 {\n\t\treturn false, errors.New(\"crypto/rsa: public modulus is even\")\n\t}\n\t// FIPS 186-5, Section 5.1: \"This standard specifies the use of a modulus\n\t// whose bit length is an even integer and greater than or equal to 2048\n\t// bits.\"\n\tif pub.N.BitLen() < 2048 {\n\t\tfipsApproved = false\n\t}\n\tif pub.N.BitLen()%2 == 1 {\n\t\tfipsApproved = false\n\t}\n\tif pub.E < 2 {\n\t\treturn false, errors.New(\"crypto/rsa: public exponent too small or negative\")\n\t}\n\t// e needs to be coprime with p-1 and q-1, since it must be invertible\n\t// modulo λ(pq). Since p and q are prime, this means e needs to be odd.","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/fips140/rsa/rsa.go#L311-L347","documentation":"Thrown by checkPublicKey when pub.N is nil — the public modulus is entirely missing. N is the RSA modulus (product of the two primes) and every RSA operation needs it; a nil N means the PublicKey was not initialized, so the key is unusable.","triggerScenarios":"checkPublicKey runs (during encryption, decryption, signing, verification, or key validation) and pub.N == nil. Reached on any RSA operation that validates the public key.","commonSituations":"A zero-value rsa.PublicKey used before population. A parse error that returned a partial key without setting N. A nil pointer or missing field in imported key JSON/ASN.1.","solutions":["Ensure the PublicKey is constructed from parsed material (e.g. via x509.ParsePKIXPublicKey) and N is set.","Guard usage with a nil check on pub.N before any RSA operation.","Regenerate the key pair and use the public key from rsa.GenerateKey directly."],"exampleFix":"// before\nvar pub rsa.PublicKey // zero value, N is nil\nenc, err := rsa.EncryptOAEP(h, rand, &pub, msg, nil)\n\n// after\npub, ok := parsed.(*rsa.PublicKey)\nif !ok || pub.N == nil {\n    return errors.New(\"missing RSA public modulus\")\n}\nenc, err := rsa.EncryptOAEP(h, rand, pub, msg, nil)","handlingStrategy":"type-guard","validationCode":"if pub == nil || pub.N == nil {\n    return errors.New(\"RSA public key missing modulus\")\n}","typeGuard":"func hasModulus(pub *rsa.PublicKey) bool { return pub != nil && pub.N != nil }","tryCatchPattern":"if err := op(pub); err != nil {\n    if strings.Contains(err.Error(), \"missing public modulus\") {\n        // re-parse or regenerate the public key\n    }\n    return err\n}","preventionTips":["Always populate N when constructing a PublicKey; prefer parsing via x509.","Check pub.N != nil before any RSA operation.","Treat a zero-value PublicKey as a programming error."],"tags":["crypto","rsa","public-key","input-validation","go"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}