{"record":{"id":"b9303f2869a24b15","repo":"golang/go","slug":"unsupported-public-key-type","errorCode":null,"errorMessage":"unsupported public key type","messagePattern":"unsupported public key type","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/hpke/pq.go","lineNumber":437,"sourceCode":"// NewMLKEMPublicKey returns a KEMPublicKey implementing\n//\n//   - ML-KEM-768\n//   - ML-KEM-1024\n//\n// from draft-ietf-hpke-pq, depending on the type of pub\n// (*[mlkem.EncapsulationKey768] or *[mlkem.EncapsulationKey1024]).\n//\n// This function is meant for applications that already have an instantiated\n// crypto/mlkem public key. Otherwise, applications should use the\n// [KEM.NewPublicKey] method of e.g. [MLKEM768].\nfunc NewMLKEMPublicKey(pub crypto.Encapsulator) (PublicKey, error) {\n\tswitch pub.(type) {\n\tcase *mlkem.EncapsulationKey768:\n\t\treturn &mlkemPublicKey{mlkem768, pub}, nil\n\tcase *mlkem.EncapsulationKey1024:\n\t\treturn &mlkemPublicKey{mlkem1024, pub}, nil\n\tdefault:\n\t\treturn nil, errors.New(\"unsupported public key type\")\n\t}\n}\n\nfunc (kem *mlkemKEM) NewPublicKey(data []byte) (PublicKey, error) {\n\tpq, err := kem.newPublicKey(data)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn NewMLKEMPublicKey(pq)\n}\n\nfunc (pk *mlkemPublicKey) KEM() KEM {\n\treturn pk.kem\n}\n\nfunc (pk *mlkemPublicKey) Bytes() []byte {\n\treturn pk.pq.Bytes()\n}","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/hpke/pq.go#L419-L455","documentation":"Raised by NewMLKEMPublicKey when the supplied crypto.Encapsulator is neither *mlkem.EncapsulationKey768 nor *mlkem.EncapsulationKey1024. These are the only two ML-KEM parameter sets exposed as standalone HPKE public keys.","triggerScenarios":"Calling NewMLKEMPublicKey(pub) where pub is an Encapsulator of an unrecognized concrete type — e.g. a mock/test double, a custom KEM implementation, or a future ML-KEM variant not yet wrapped here.","commonSituations":"Unit tests passing a fake Encapsulator; integrating an alternate PQ KEM library whose type does not match the mlkem wrappers; protocol code that accepts a generic crypto.Encapsulator and forwards it here.","solutions":["Construct the public key via KEM.NewPublicKey([]byte) (e.g. MLKEM768.NewPublicKey) which yields the correct wrapped type.","Ensure pub is produced by mlkem.NewEncapsulationKey768 or mlkem.NewEncapsulationKey1024.","Type-assert pub to one of the two supported concrete types before calling NewMLKEMPublicKey."],"exampleFix":"// before\npub, err := hpke.NewMLKEMPublicKey(customEncapsulator) // unsupported\n\n// after\nek, _ := mlkem.NewEncapsulationKey768(rawKeyBytes)\npub, err := hpke.NewMLKEMPublicKey(ek)","handlingStrategy":"type-guard","validationCode":"func isValidMLKEMPub(pub crypto.Encapsulator) bool {\n    switch pub.(type) {\n    case *mlkem.EncapsulationKey768, *mlkem.EncapsulationKey1024:\n        return true\n    }\n    return false\n}","typeGuard":"func mlkemPubKind(pub crypto.Encapsulator) string {\n    switch pub.(type) {\n    case *mlkem.EncapsulationKey768:\n        return \"768\"\n    case *mlkem.EncapsulationKey1024:\n        return \"1024\"\n    }\n    return \"unsupported\"\n}","tryCatchPattern":null,"preventionTips":["Prefer KEM.NewPublicKey([]byte) over NewMLKEMPublicKey for raw bytes.","Build encapsulators only via mlkem.NewEncapsulationKey768/1024.","Reject generic Encapsulator inputs at your API boundary."],"tags":["hpke","post-quantum","mlkem","kem","validation"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:17:08.281Z"}