{"record":{"id":"8e67b22b472b8266","repo":"ory/hydra","slug":"invalid-key-size-for-rsa-key-2048-or-more-is-requ","errorCode":null,"errorMessage":"invalid key size for RSA key, 2048 or more is required","messagePattern":"invalid key size for RSA key, 2048 or more is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/josex/generate.go","lineNumber":50,"sourceCode":"// NewSigningKey generates a keypair for corresponding SignatureAlgorithm.\nfunc NewSigningKey(alg jose.SignatureAlgorithm, bits int) (crypto.PublicKey, crypto.PrivateKey, error) {\n\tswitch alg {\n\tcase jose.ES256, jose.ES384, jose.ES512, jose.EdDSA:\n\t\tkeylen := map[jose.SignatureAlgorithm]int{\n\t\t\tjose.ES256: 256,\n\t\t\tjose.ES384: 384,\n\t\t\tjose.ES512: 521, // sic!\n\t\t\tjose.EdDSA: 256,\n\t\t}\n\t\tif bits != 0 && bits != keylen[alg] {\n\t\t\treturn nil, nil, errors.New(\"invalid elliptic curve key size, this algorithm does not support arbitrary size\")\n\t\t}\n\tcase jose.RS256, jose.RS384, jose.RS512, jose.PS256, jose.PS384, jose.PS512:\n\t\tif bits == 0 {\n\t\t\tbits = 2048\n\t\t}\n\t\tif bits < 2048 {\n\t\t\treturn nil, nil, errors.New(\"invalid key size for RSA key, 2048 or more is required\")\n\t\t}\n\t}\n\tswitch alg {\n\tcase jose.ES256:\n\t\tkey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)\n\t\tif err != nil {\n\t\t\treturn nil, nil, err\n\t\t}\n\t\treturn key.Public(), key, err\n\tcase jose.ES384:\n\t\tkey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)\n\t\tif err != nil {\n\t\t\treturn nil, nil, err\n\t\t}\n\t\treturn key.Public(), key, err\n\tcase jose.ES512:\n\t\tkey, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)\n\t\tif err != nil {","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/josex/generate.go#L32-L68","documentation":"josex.NewSigningKey enforces a minimum RSA modulus of 2048 bits for the RS256/RS384/RS512 and PS256/PS384/PS512 signature algorithms. If bits is non-zero and less than 2048, key generation is refused because shorter RSA keys are considered insecure. bits == 0 defaults to 2048.","triggerScenarios":"Calling josex.NewSigningKey(jose.RS256, 1024), NewSigningKey(jose.PS256, 512), or any RSA-family signature algorithm with 0 < bits < 2048.","commonSituations":"Legacy configs specifying 1024-bit RSA keys from older security policies; copying example code that used 1024 or 1536; a shared 'keyBits' setting meant for EC (256/384) being applied to RSA algorithms; environment-driven key-size configuration not validated on startup.","solutions":["Pass bits >= 2048 (2048 is the common default; 3072/4096 for longer-term keys)","Pass bits = 0 to accept the library default of 2048","Update legacy configuration that still specifies 1024-bit RSA","If you intended an elliptic-curve key, switch the algorithm to ES256/ES384/ES512 or EdDSA, where sizes are 256/384/521"],"exampleFix":"// before\nkey, _, err := josex.NewSigningKey(jose.RS256, 1024)\n// after\nkey, _, err := josex.NewSigningKey(jose.RS256, 2048)\n// or rely on the default:\nkey, _, err := josex.NewSigningKey(jose.RS256, 0)","handlingStrategy":"validation","validationCode":"func validBitsForSigning(alg jose.SignatureAlgorithm, bits int) error {\n\tswitch alg {\n\tcase jose.RS256, jose.RS384, jose.RS512, jose.PS256, jose.PS384, jose.PS512:\n\t\tif bits != 0 && bits < 2048 {\n\t\t\treturn fmt.Errorf(\"%s requires bits>=2048 (got %d)\", alg, bits)\n\t\t}\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"pub, priv, err := josex.NewSigningKey(jose.RS256, bits)\nif err != nil {\n\tif strings.Contains(err.Error(), \"2048 or more is required\") {\n\t\tbits = 2048\n\t\tpub, priv, err = josex.NewSigningKey(jose.RS256, bits)\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"generating RSA signing key: %w\", err)\n\t}\n}","preventionTips":["Default to 2048 (or 3072/4096) for any RSA config value; never accept values below 2048","Pass bits=0 to use the library's 2048 default","Migrate any legacy 1024-bit RSA settings at config-load time, not at key-generation time","Do not share one key-size setting between EC and RSA algorithms"],"tags":["go","jose","crypto","rsa","key-generation"],"backgroundTag":"invalid-key-size","analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}