{"record":{"id":"adafef97dcbd3bac","repo":"golang/go","slug":"mldsa-zero-private-key","errorCode":null,"errorMessage":"mldsa: zero private key","messagePattern":"mldsa: zero private key","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/crypto/mldsa/mldsa_fips140v1.26.go","lineNumber":107,"sourceCode":"// Bytes returns the private key seed.\nfunc (sk *PrivateKey) Bytes() []byte {\n\treturn sk.k.Bytes()\n}\n\nvar errInvalidSignerOpts = errors.New(\"mldsa: invalid SignerOpts\")\n\n// Sign returns a signature of the given message using this private key.\n//\n// If opts is nil or opts.HashFunc returns zero, the message is signed directly.\n// If opts.HashFunc returns [crypto.MLDSAMu], the provided message must be a\n// [pre-hashed μ message representative]. opts can be of type *[Options] if a\n// context string is desired along with a directly-signed message. The io.Reader\n// argument is ignored.\n//\n// [pre-hashed μ message representative]: https://www.rfc-editor.org/rfc/rfc9881.html#externalmu\nfunc (sk *PrivateKey) Sign(_ io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) {\n\tif sk.k == (mldsa.PrivateKey{}) {\n\t\treturn nil, errors.New(\"mldsa: zero private key\")\n\t}\n\tif opts == nil {\n\t\topts = &Options{}\n\t}\n\tswitch opts.HashFunc() {\n\tcase 0:\n\t\tvar context string\n\t\tif opts, ok := opts.(*Options); ok && opts != nil {\n\t\t\tcontext = opts.Context\n\t\t}\n\t\treturn mldsa.Sign(&sk.k, message, context)\n\tcase crypto.MLDSAMu:\n\t\treturn mldsa.SignExternalMu(&sk.k, message)\n\tdefault:\n\t\treturn nil, errInvalidSignerOpts\n\t}\n}\n","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/crypto/mldsa/mldsa_fips140v1.26.go#L89-L125","documentation":"Returned by PrivateKey.Sign when the underlying mldsa.PrivateKey field sk.k is the zero value, meaning the PrivateKey struct was never populated by key generation or decoding. This guards against signing with an uninitialized key, which would otherwise produce invalid or deterministic garbage. The check sk.k == (mldsa.PrivateKey{}) detects the all-zero internal state.","triggerScenarios":"Declaring var sk mldsa.PrivateKey (or new(mldsa.PrivateKey)) and calling Sign without running it through GenerateKey or UnmarshalBinary. Loading a key from bytes via a code path that silently failed and left sk.k zero.","commonSituations":"Deserializing a key from disk/network where the decode error was ignored, leaving the zero-value key. Copying a PrivateKey by value into a new variable and forgetting to populate it. Test scaffolding that instantiates the struct directly.","solutions":["Generate the key first: sk, err := mldsa.GenerateKey(rand.Reader); then call sk.Sign(...).","Decode from stored bytes using the documented Unmarshal/Decode functions and check the returned error before signing.","Add a nil/zero-value assertion immediately after key construction in your loading code."],"exampleFix":"// before\nvar sk mldsa.PrivateKey\nsig, err := sk.Sign(nil, msg, nil) // \"mldsa: zero private key\"\n\n// after\nsk, err := mldsa.GenerateKey(rand.Reader)\nif err != nil { return err }\nsig, err := sk.Sign(nil, msg, nil)","handlingStrategy":"validation","validationCode":"// Ensure the key is non-zero before signing.\nif sk == nil || len(sk.Bytes()) == 0 {\n    return errors.New(\"private key not initialized\")\n}\nsig, err := sk.Sign(nil, msg, nil)","typeGuard":"func (sk *PrivateKey) isInitialized() bool {\n    return sk != nil && len(sk.Bytes()) > 0\n}","tryCatchPattern":"sig, err := sk.Sign(nil, msg, opts)\nif err != nil && strings.Contains(err.Error(), \"zero private key\") {\n    // re-initialize key from source, then retry once\n}\nreturn sig, err","preventionTips":["Always check the error from GenerateKey / key-decode functions.","Centralize key construction in one factory that returns a fully populated key or an error.","Assert key bytes are non-empty in tests after loading."],"tags":["cryptography","go","post-quantum","mldsa","key-management","uninitialized-state"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}