Skip to content

crypto/rsa: refuse to generate and/or use keys smaller than 1024 bits #68762

Closed

Description

@FiloSottile

crypto/rsa is unusual in that it can be secure (used with a key size > 2048) or completely insecure (used with a key size such as 512 bits, which can be broken on a laptop). Small keys are sometimes useful in tests, but having an rsa.PrivateKey value that behaves and looks exactly like a secure one but actually provides no security at all is a significant footgun.

In production, if a 512-bit key is used, it's overwhelmingly likely that the operator thinks they have security and doesn't (this happens in the real world on a regular basis) as opposed to being intentional about using fake RSA, so this feels like one of those rare occasions where breaking them is justified.

I propose we do both the following in Go 1.24:

  1. return an error from rsa.GenerateKey if bits is less than 1024
  2. return an error from all Sign, Verify, Encrypt, and Decrypt methods if the key is smaller than 1024 bits

GODEBUG=rsa1024min=0 reverts to the old behavior.

OpenSSL sounds on the way to doing (1) in a minor release and (2) in a major release. openssl/openssl#25092

To avoid slow key generation in tests, we can recommend using the test keys in RFC 9500. If anyone has a good idea for how to expose them, we can even provide them ready to use.

We could also disable the restriction based on testing.Testing() but I would keep that as a fallback if the amount of tests that require fixing turns out to be truly unmanageable, because generally tests are supposed to test actual behavior.

Activity

  1. changed the title [-]crypto/rsa: refuse to generate and/or use keys smaller than 1024 bits[/-] [+]proposal: crypto/rsa: refuse to generate and/or use keys smaller than 1024 bits[/+] on Aug 7, 2024
  2. added
    Proposal-CryptoProposal related to crypto packages or other security issues
    on Aug 7, 2024
  3. added this to the Proposal milestone on Aug 7, 2024
  4. alex commented on Aug 7, 2024

    @alex
    Contributor

    For pyca/cryptography (most widely used Python cryptography library) in our last release (~3 weeks ago) we enforce a minimum of 1024-bit for RSA key generation (up from 512-bits in the previous release).

    This has produced only one complaint, about a testing workload (where small keys were being used for performance).

    We're also interested in enforcing key sizes in signing/verification (or perhaps in key loading), but haven't done so yet.

  5. FiloSottile commented on Aug 7, 2024

    @FiloSottile
    ContributorAuthor

    To avoid slow key generation in tests, we can recommend using the test keys in RFC 9500. If anyone has a good idea for how to expose them, we can even provide them ready to use.

    An easy answer to this is a GenerateKey or package-level example, for folks to copy-paste.

    We could also disable the restriction based on testing.Testing() but I would keep that as a fallback if the amount of tests that require fixing turns out to be truly unmanageable, because generally tests are supposed to test actual behavior.

    Actually, this is not a good idea. Packages can opt in to the equivalent behavior simply by doing

    func init() {
        os.Setenv("GODEBUG", os.Getenv("GODEBUG") + ",rsa1024min=0")
    }
    

    in a test file (or an equivalent, test-scoped operation).

  6. ericlagergren commented on Aug 7, 2024

    @ericlagergren
    Contributor

    We could also disable the restriction based on testing.Testing()

    This also requires crypto/rsa to import testing, which isn't good.

  7. moved this to Incoming in Proposalson Aug 7, 2024
  8. rittneje commented on Aug 7, 2024

    @rittneje
    Contributor

    @FiloSottile Are you committing to supporting that GODEBUG forever? There are legitimate reasons to use smaller keys, and we shouldn't have to fork the Go standard library to work with them.

    Also, I really don't think this should (only) be a GODEBUG. When smaller keys are intentional, it is going to be for some specific use case. Having this toggle for the entire application is too coarse grain. It would be far preferable to be able to opt-in per function/method call.

  9. mcpherrinm commented on Aug 7, 2024

    @mcpherrinm

    Do you have an examples of those legitimate use-cases? I admit they may exist but besides test-case reasons, I am not aware of why you’d want to use RSA < 1024. Even 1024 is a stretch these days for anything that requires actual security. I almost want 1024 gated behind a GODEBUG too

  10. rittneje commented on Aug 8, 2024

    @rittneje
    Contributor

    @mcpherrinm We have some edge devices that can only hardware accelerate 256-bit. I thought they were RSA, but turns out they are elliptic (P-256), so we're good!

  11. mcpherrinm commented on Aug 8, 2024

    @mcpherrinm

    Great: P-256 elliptic curves have strength greater than RSA-2048 and are well within the margin of safety today.

    Confusion about key strength for different algorithms is one of the common reasons for accidental use of RSA-512. For example, hashicorp/consul#20112 defaulted to a key size of 256, which was suitable for P-256 but totally inappropriate if Consul was configured to use RSA but didn’t have a key size set.

    This proposal would have prevented that issue.

  12. FiloSottile commented on Aug 8, 2024

    @FiloSottile
    ContributorAuthor
  13. rsc commented on Aug 13, 2024

    @rsc
    Contributor

    os.Setenv("GODEBUG", os.Getenv("GODEBUG") + ",rsa1024min=0")

    No need for this in a test. Tests can write a line at the top of the source file:

    //go:debug rsa1024min=0
    
  14. 28 remaining items

  15. mauri870 commented on Nov 14, 2024

    @mauri870
    Member

    Thanks @FiloSottile!

  16. gopherbot commented on Nov 20, 2024

    @gopherbot
    Contributor

    Change https://go.dev/cl/629938 mentions this issue: crypto/rsa: refuse to generate and/or use keys smaller than 1024 bits

  17. modified the milestones: Backlog, Go1.24 on Nov 21, 2024
  18. added a commit that references this issue on Nov 22, 2024
    10fb001
  19. quolpr commented on Oct 10, 2025

    @quolpr

    Guys, I think this is a breaking change for some system. What if some systems depend on old API, and there is no way to ask the provider to upgrade theirs keys from 512 to 1024? While for go 1.23 it was working normally, for other version it will stop working without quick way of fixing it.

  20. ryancdotorg commented on Oct 10, 2025

    @ryancdotorg
  21. quolpr commented on Oct 10, 2025

    @quolpr

    @ryancdotorg yeah, I understand. But I think the first step of moving to 1024 should be deprecation notice, so old legacy system will start migrating to the newer version. And then breaking change. We are living in the real world, where inertia of process is very often things.

  22. mvdan commented on Oct 10, 2025

    @mvdan
    Member

    @quolpr I'm afraid you're fairly late to the party. This went through the proposal process more than a year ago, and Go 1.24 release candidates already included this change. At this point I doubt it will be reconsidered because it landed in a stable release ten months ago.

    However, note that you can get the old behavior back for now; search for rsa1024min in https://go.dev/doc/godebug.

  23. locked and limited conversation to collaborators on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions