{"record":{"id":"562d03377c556dd5","repo":"golang/go","slug":"invalid-p256-point-encoding-562d03","errorCode":null,"errorMessage":"invalid P256 point encoding","messagePattern":"invalid P256 point encoding","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/internal/fips140/nistec/p256_asm.go","lineNumber":133,"sourceCode":"\n\t\t// y² = x³ - 3x + b\n\t\tp256Polynomial(&r.y, &r.x)\n\t\tif !p256Sqrt(&r.y, &r.y) {\n\t\t\treturn nil, errors.New(\"invalid P256 compressed point encoding\")\n\t\t}\n\n\t\t// Select the positive or negative root, as indicated by the least\n\t\t// significant bit, based on the encoding type byte.\n\t\tyy := new(p256Element)\n\t\tp256FromMont(yy, &r.y)\n\t\tcond := int(yy[0]&1) ^ int(b[0]&1)\n\t\tp256NegCond(&r.y, cond)\n\n\t\tr.z = p256One\n\t\treturn p.Set(&r), nil\n\n\tdefault:\n\t\treturn nil, errors.New(\"invalid P256 point encoding\")\n\t}\n}\n\n// p256Polynomial sets y2 to x³ - 3x + b, and returns y2.\nfunc p256Polynomial(y2, x *p256Element) *p256Element {\n\tx3 := new(p256Element)\n\tp256Sqr(x3, x, 1)\n\tp256Mul(x3, x3, x)\n\n\tthreeX := new(p256Element)\n\tp256Add(threeX, x, x)\n\tp256Add(threeX, threeX, x)\n\tp256NegCond(threeX, 1)\n\n\tp256B := &p256Element{0xd89cdf6229c4bddf, 0xacf005cd78843090,\n\t\t0xe5a220abf7212ed6, 0xdc30061d04874834}\n\n\tp256Add(x3, x3, threeX)","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/internal/fips140/nistec/p256_asm.go#L115-L151","documentation":"The default case of P256Point.SetBytes: the input byte slice does not match any recognized P-256 point encoding. Valid formats are: 1-byte infinity (0x00), 65-byte uncompressed (prefix 0x04), or 33-byte compressed (prefix 0x02 or 0x03). Any other combination of length and prefix byte falls through to this error.","triggerScenarios":"Calling SetBytes with an empty slice, a slice of the wrong length, a slice with an unrecognized prefix byte, or a slice whose length matches a valid format but whose prefix byte is wrong (e.g., 65 bytes starting with 0x02).","commonSituations":"Passing raw 64-byte x||y coordinates without the 0x04 prefix; passing a DER-encoded SubjectPublicKeyInfo instead of raw point bytes; truncated network data; using uncompressed coordinates with a compressed-point prefix by mistake.","solutions":["Ensure the input matches one of: [0x00] (infinity), 0x04 + 32-byte x + 32-byte y (uncompressed), or 0x02/0x03 + 32-byte x (compressed)","If working with raw x,y coordinates, prepend the 0x04 prefix yourself","Use crypto/x509.ParsePKIXPublicKey or ecdsa.Unmarshal for correct parsing of higher-level formats"],"exampleFix":"// before\npoint, err := nistec.NewP256Point().SetBytes(rawXY) // raw 64-byte x||y, no prefix\n\n// after\nenc := make([]byte, 1, 65)\nenc[0] = 0x04\nenc = append(enc, rawXY...) // now 65 bytes: 0x04 || x || y\npoint, err := nistec.NewP256Point().SetBytes(enc)","handlingStrategy":"validation","validationCode":"func classifyP256Encoding(b []byte) error {\n    switch {\n    case len(b) == 1 && b[0] == 0:\n        return nil // infinity\n    case len(b) == 65 && b[0] == 0x04:\n        return nil // uncompressed\n    case len(b) == 33 && (b[0] == 0x02 || b[0] == 0x03):\n        return nil // compressed\n    default:\n        return fmt.Errorf(\"unrecognized P-256 encoding: len=%d prefix=0x%02x\", len(b), b[0])\n    }\n}\n\nif err := classifyP256Encoding(b); err != nil { return err }\n_, err := point.SetBytes(b)","typeGuard":null,"tryCatchPattern":"_, err := point.SetBytes(b)\nif err != nil {\n    return fmt.Errorf(\"not a valid P-256 point encoding: %w\", err)\n}","preventionTips":["Ensure raw x||y coordinates are prefixed with 0x04 before calling SetBytes","Do not pass DER/ASN.1 or PEM-encoded data directly to SetBytes — decode first","Check the first byte and length match a known format before parsing"],"tags":["crypto","fips140","p256","elliptic-curve","point-encoding","input-validation"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}