{"record":{"id":"332c7a6b8868cab4","repo":"golang/go","slug":"ecdsa-curve-not-supported-by-parserawprivatekey","errorCode":null,"errorMessage":"ecdsa: curve not supported by ParseRawPrivateKey","messagePattern":"ecdsa: curve not supported by ParseRawPrivateKey","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/ecdsa/ecdsa.go","lineNumber":257,"sourceCode":"//\n// ParseRawPrivateKey accepts the same format as [ecdh.Curve.NewPrivateKey] does\n// for NIST curves, but returns a [PrivateKey] instead of an [ecdh.PrivateKey].\n//\n// Note that private keys are more commonly encoded in ASN.1 or PKCS#8 format,\n// which can be parsed with [crypto/x509.ParseECPrivateKey] or\n// [crypto/x509.ParsePKCS8PrivateKey] (and [encoding/pem]).\nfunc ParseRawPrivateKey(curve elliptic.Curve, data []byte) (*PrivateKey, error) {\n\tswitch curve {\n\tcase elliptic.P224():\n\t\treturn parseRawPrivateKey(ecdsa.P224(), nistec.NewP224Point, curve, data)\n\tcase elliptic.P256():\n\t\treturn parseRawPrivateKey(ecdsa.P256(), nistec.NewP256Point, curve, data)\n\tcase elliptic.P384():\n\t\treturn parseRawPrivateKey(ecdsa.P384(), nistec.NewP384Point, curve, data)\n\tcase elliptic.P521():\n\t\treturn parseRawPrivateKey(ecdsa.P521(), nistec.NewP521Point, curve, data)\n\tdefault:\n\t\treturn nil, errors.New(\"ecdsa: curve not supported by ParseRawPrivateKey\")\n\t}\n}\n\nfunc parseRawPrivateKey[P ecdsa.Point[P]](c *ecdsa.Curve[P], newPoint func() P, curve elliptic.Curve, data []byte) (*PrivateKey, error) {\n\tq, err := newPoint().ScalarBaseMult(data)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tk, err := ecdsa.NewPrivateKey(c, data, q.Bytes())\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn privateKeyFromFIPS(curve, k)\n}\n\n// Bytes encodes the private key as a fixed-length big-endian integer according\n// to SEC 1, Version 2.0, Section 2.3.6 (sometimes referred to as the raw\n// format). It returns an error if the private key is invalid.","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/ecdsa/ecdsa.go#L239-L275","documentation":"Thrown by ParseRawPrivateKey when the provided curve does not match P224, P256, P384, or P521 in the switch statement. Only standard NIST curves are supported for parsing raw (unencoded) ECDSA private key scalars.","triggerScenarios":"Calling ParseRawPrivateKey with a custom elliptic.Curve, or passing curve.Params() instead of the singleton curve object. The switch compares against the singleton returns of elliptic.P224()/P256()/P384()/P521().","commonSituations":"Using a third-party curve implementation; mistakenly passing curve.Params() which is a different type than the curve singleton; working with non-NIST curves like SEC curves outside the standard set.","solutions":["Pass the canonical curve singleton (e.g., elliptic.P256()) not curve.Params().","For non-NIST curves, parse the scalar manually and construct the PrivateKey by computing D*G via ScalarBaseMult.","Validate the curve matches one of the four supported singletons before calling ParseRawPrivateKey."],"exampleFix":"// before\npriv, err := ecdsa.ParseRawPrivateKey(curve.Params(), scalarBytes)\n\n// after\npriv, err := ecdsa.ParseRawPrivateKey(elliptic.P256(), scalarBytes)","handlingStrategy":"validation","validationCode":"func isSupportedCurve(c elliptic.Curve) bool {\n    switch c {\n    case elliptic.P224(), elliptic.P256(), elliptic.P384(), elliptic.P521():\n        return true\n    }\n    return false\n}\n// call before: ParseRawPrivateKey(curve, data)","typeGuard":null,"tryCatchPattern":"priv, err := ecdsa.ParseRawPrivateKey(curve, data)\nif err != nil {\n    return fmt.Errorf(\"cannot parse private key on curve %s: %w\", curve.Params().Name, err)\n}","preventionTips":["Pass elliptic.P256() not elliptic.P256().Params() — they are different interface values.","For non-NIST curves, parse the scalar and call ScalarBaseMult manually."],"tags":["crypto","ecdsa","private-key","curve-mismatch","nist-curves","input-validation"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}