{"record":{"id":"7b298c4c0379219e","repo":"ory/hydra","slug":"invalid-elliptic-curve-key-size-use-one-of-256-3","errorCode":null,"errorMessage":"invalid elliptic curve key size, use one of 256, 384, or 521","messagePattern":"invalid elliptic curve key size, use one of 256, 384, or 521","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/josex/generate.go","lineNumber":111,"sourceCode":"\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\tkey, err := rsa.GenerateKey(rand.Reader, bits)\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.ECDH_ES, jose.ECDH_ES_A128KW, jose.ECDH_ES_A192KW, jose.ECDH_ES_A256KW:\n\t\tvar crv elliptic.Curve\n\t\tswitch bits {\n\t\tcase 0, 256:\n\t\t\tcrv = elliptic.P256()\n\t\tcase 384:\n\t\t\tcrv = elliptic.P384()\n\t\tcase 521:\n\t\t\tcrv = elliptic.P521()\n\t\tdefault:\n\t\t\treturn nil, nil, errors.New(\"invalid elliptic curve key size, use one of 256, 384, or 521\")\n\t\t}\n\t\tkey, err := ecdsa.GenerateKey(crv, 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\tdefault:\n\t\treturn nil, nil, fmt.Errorf(\"unknown algorithm %s for encryption key\", alg)\n\t}\n}\n","sourceCodeStart":93,"sourceCodeEnd":122,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/josex/generate.go#L93-L122","documentation":"josex.NewEncryptionKey generates ECDH keys for the ECDH_ES / ECDH_ES_A*KW key algorithms on one of three NIST curves selected by the bits parameter: 256 (P-256, also the default when bits == 0), 384 (P-384), or 521 (P-521). Any other non-zero value falls through to this error because elliptic curves cannot be generated at arbitrary sizes.","triggerScenarios":"Calling josex.NewEncryptionKey(jose.ECDH_ES, 512) (expecting 512 like ES512), NewEncryptionKey(jose.ECDH_ES_A128KW, 128), NewEncryptionKey(jose.ECDH_ES_A256KW, 528), or any ECDH_* algorithm with bits not in {0, 256, 384, 521}.","commonSituations":"Assuming the AES key-wrap size (A128KW/A192KW/A256KW: 128/192/256) is the curve size and passing 128 or 192; confusing ES512's 512 with the required 521; a generic 'security level' config (e.g. 192) fed into every key-generation call.","solutions":["Use bits = 256, 384, or 521 for ECDH_ES* algorithms (256 is the default; note 521, not 512)","Pass bits = 0 to get the default P-256 key","Do not derive bits from the AES key-wrap name (A128KW still uses a P-256/P-384/P-521 curve, not 128)","Map your security-level setting to the nearest supported curve size before calling"],"exampleFix":"// before\npub, priv, err := josex.NewEncryptionKey(jose.ECDH_ES, 512)\n// after\npub, priv, err := josex.NewEncryptionKey(jose.ECDH_ES, 521) // P-521\n// or default P-256:\npub, priv, err := josex.NewEncryptionKey(jose.ECDH_ES, 0)","handlingStrategy":"validation","validationCode":"func validBitsForECDH(bits int) error {\n\tswitch bits {\n\tcase 0, 256, 384, 521:\n\t\treturn nil\n\tdefault:\n\t\treturn fmt.Errorf(\"ECDH key size must be 0 (default P-256), 256, 384, or 521 (got %d)\", bits)\n\t}\n}","typeGuard":null,"tryCatchPattern":"pub, priv, err := josex.NewEncryptionKey(jose.ECDH_ES, bits)\nif err != nil {\n\tif strings.Contains(err.Error(), \"use one of 256, 384, or 521\") {\n\t\tbits = 0 // default P-256\n\t\tpub, priv, err = josex.NewEncryptionKey(jose.ECDH_ES, bits)\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"generating ECDH encryption key: %w\", err)\n\t}\n}","preventionTips":["Use only 256, 384, 521 (or 0) for ECDH_ES* key generation — 512 is not valid even though ES512 exists (it means P-521)","Do not derive the curve size from the AES key-wrap name (A128KW ≠ 128-bit curve)","Pass bits=0 to accept the default P-256","Centralize key-size selection in one helper that maps algorithms to allowed sizes"],"tags":["go","jose","crypto","ecdh","jwe","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"}