{"record":{"id":"0a25d42333a526df","repo":"ory/hydra","slug":"jwksx-s-does-not-support-arbitrary-key-length","errorCode":null,"errorMessage":"jwksx: \"%s\" does not support arbitrary key length","messagePattern":"jwksx: \"(.+?)\" does not support arbitrary key length","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/jwksx/generator.go","lineNumber":65,"sourceCode":"\treturn []string{\n\t\tstring(jose.HS256), string(jose.HS384), string(jose.HS512),\n\t\tstring(jose.ES256), string(jose.ES384), string(jose.ES512), string(jose.EdDSA),\n\t\tstring(jose.RS256), string(jose.RS384), string(jose.RS512), string(jose.PS256), string(jose.PS384), string(jose.PS512),\n\t}\n}\n\n// generate generates keypair for corresponding SignatureAlgorithm.\nfunc generate(alg jose.SignatureAlgorithm, bits int) (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, errors.Errorf(`jwksx: \"%s\" does not support arbitrary key length`, alg)\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, errors.Errorf(`jwksx: key size must be at least 2048 bit for algorithm \"%s\"`, alg)\n\t\t}\n\tcase jose.HS256:\n\t\tif bits == 0 {\n\t\t\tbits = 256\n\t\t}\n\t\tif bits < 256 {\n\t\t\treturn nil, errors.Errorf(`jwksx: key size must be at least 256 bit for algorithm \"%s\"`, alg)\n\t\t}\n\tcase jose.HS384:\n\t\tif bits == 0 {\n\t\t\tbits = 384","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/jwksx/generator.go#L47-L83","documentation":"generate() validates the requested bit size for elliptic-curve and EdDSA algorithms (ES256/ES384/ES512/EdDSA). These algorithms have fixed key sizes (256, 384, 521, 256 bits respectively), so any non-zero bits value that differs from the fixed size is rejected with this error. Passing bits=0 uses the algorithm's default size and is allowed.","triggerScenarios":"Calling GenerateSigningKeys(id, alg, bits) with alg one of ES256, ES384, ES512, EdDSA and bits set to any value other than 0 or the algorithm's exact fixed size (256/384/521/256). E.g. GenerateSigningKeys(id, \"ES256\", 512).","commonSituations":"Reusing a generic key-generation helper that passes a user-supplied bits parameter (e.g. 2048 or 4096 from config) for all algorithms; assuming ES512 uses 512 bits when it actually uses 521 (P-521); copy-pasting RSA-style sizes into EC algorithm config.","solutions":["Pass bits=0 to use the algorithm's default key size","Pass the exact fixed size: 256 for ES256/EdDSA, 384 for ES384, 521 (not 512) for ES512","Switch to an RSA (RS*/PS*) or HMAC (HS*) algorithm if an arbitrary key length is genuinely required"],"exampleFix":"// before\njwks, err := jwksx.GenerateSigningKeys(\"kid\", \"ES256\", 2048)\n// after\njwks, err := jwksx.GenerateSigningKeys(\"kid\", \"ES256\", 0) // or 256","handlingStrategy":"validation","validationCode":"func validBitsForAlg(alg string, bits int) error {\n\tfixed := map[string]int{\"ES256\": 256, \"ES384\": 384, \"ES512\": 521, \"EdDSA\": 256}\n\tif want, ok := fixed[alg]; ok && bits != 0 && bits != want {\n\t\treturn fmt.Errorf(\"%s requires exactly %d bits (or 0 for default), got %d\", alg, want, bits)\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"jwks, err := jwksx.GenerateSigningKeys(id, alg, bits)\nif err != nil {\n\tif strings.Contains(err.Error(), \"does not support arbitrary key length\") {\n\t\t// retry with default size\n\t\tjwks, err = jwksx.GenerateSigningKeys(id, alg, 0)\n\t}\n\treturn err\n}","preventionTips":["Pass bits=0 for EC/EdDSA algorithms and let the library pick the size","Remember ES512 uses 521 bits (P-521), not 512","Do not feed RSA-style sizes (2048/4096) into EC algorithm configs","Validate algorithm+bits pairs together in config parsing"],"tags":["jwks","go","crypto","validation"],"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"}