{"record":{"id":"dde8b6e96d558dea","repo":"golang/go","slug":"crypto-rsa-generatemultiprimekey-nprimes-must-be","errorCode":null,"errorMessage":"crypto/rsa: GenerateMultiPrimeKey: nprimes must be >= 2","messagePattern":"crypto/rsa: GenerateMultiPrimeKey: nprimes must be >= 2","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/rsa/rsa.go","lineNumber":449,"sourceCode":"// two is not recommended for the above security, compatibility, and performance\n// reasons. Use [GenerateKey] instead.\n//\n// [On the Security of Multi-prime RSA]: http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf\nfunc GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (*PrivateKey, error) {\n\tif nprimes == 2 {\n\t\treturn GenerateKey(random, bits)\n\t}\n\tif fips140only.Enforced() {\n\t\treturn nil, errors.New(\"crypto/rsa: multi-prime RSA is not allowed in FIPS 140-only mode\")\n\t}\n\n\trandom = rand.CustomReader(random)\n\n\tpriv := new(PrivateKey)\n\tpriv.E = 65537\n\n\tif nprimes < 2 {\n\t\treturn nil, errors.New(\"crypto/rsa: GenerateMultiPrimeKey: nprimes must be >= 2\")\n\t}\n\n\tif bits < 64 {\n\t\tprimeLimit := float64(uint64(1) << uint(bits/nprimes))\n\t\t// pi approximates the number of primes less than primeLimit\n\t\tpi := primeLimit / (math.Log(primeLimit) - 1)\n\t\t// Generated primes start with 11 (in binary) so we can only\n\t\t// use a quarter of them.\n\t\tpi /= 4\n\t\t// Use a factor of two to ensure that key generation terminates\n\t\t// in a reasonable amount of time.\n\t\tpi /= 2\n\t\tif pi <= float64(nprimes) {\n\t\t\treturn nil, errors.New(\"crypto/rsa: too few primes of given length to generate an RSA key\")\n\t\t}\n\t}\n\n\tprimes := make([]*big.Int, nprimes)","sourceCodeStart":431,"sourceCodeEnd":467,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/rsa/rsa.go#L431-L467","documentation":"Returned by GenerateMultiPrimeKey when nprimes < 2. The function explicitly redirects nprimes==2 to GenerateKey, so this error only fires for nprimes 0 or 1 — values that cannot form an RSA modulus. It is a programmer-error guard, not a security or compliance check (it fires even outside FIPS mode).","triggerScenarios":"Call rsa.GenerateMultiPrimeKey(rand.Reader, 1, 2048) or GenerateMultiPrimeKey(rand.Reader, 0, 2048); compute nprimes from user input without a lower-bound check.","commonSituations":"Off-by-one when computing prime count from a configuration value; copy-paste from a tutorial that used nprimes=1 as a placeholder.","solutions":["Pass nprimes == 2 (which routes to GenerateKey) — there is no good reason to call GenerateMultiPrimeKey with any other value today.","Validate the caller's input: if nprimes < 2 { return ErrInvalidPrimeCount } before reaching GenerateMultiPrimeKey.","Prefer rsa.GenerateKey outright and drop GenerateMultiPrimeKey usage."],"exampleFix":"// before\npriv, err := rsa.GenerateMultiPrimeKey(rand.Reader, 1, 2048) // err: nprimes must be >= 2\n\n// after\npriv, err := rsa.GenerateKey(rand.Reader, 2048)","handlingStrategy":"validation","validationCode":"if nprimes < 2 {\n    return errors.New(\"nprimes must be >= 2\")\n}\nif nprimes == 2 {\n    return rsa.GenerateKey(rand.Reader, bits)\n}\nreturn rsa.GenerateMultiPrimeKey(rand.Reader, nprimes, bits)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to rsa.GenerateKey and never expose nprimes to callers.","Bounds-check configuration-derived prime counts.","Treat any nprimes != 2 as a code smell."],"tags":["rsa","multi-prime","key-generation","api-misuse","validation","crypto"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}