{"record":{"id":"bd9c626e025e23d9","repo":"slackhq/nebula","slug":"got-a-key-of-d-bytes-expected-d","errorCode":null,"errorMessage":"got a key of %d bytes, expected %d","messagePattern":"got a key of (.+?) bytes, expected (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkclient/pkclient.go","lineNumber":84,"sourceCode":"\tswitch t := e.(type) {\n\tcase *ecdsa.PublicKey:\n\t\treturn ecKeyToArray(e.(*ecdsa.PublicKey)), nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"unknown public key type: %T\", t)\n\t}\n}\n\nfunc (c *PKClient) Test() error {\n\tpub, err := c.GetPubKey()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get public key: %w\", err)\n\t}\n\tout, err := c.DeriveNoise(pub) //do an ECDH with ourselves as a quick test\n\tif err != nil {\n\t\treturn err\n\t}\n\tif len(out) != NoiseKeySize {\n\t\treturn fmt.Errorf(\"got a key of %d bytes, expected %d\", len(out), NoiseKeySize)\n\t}\n\treturn nil\n}\n","sourceCodeStart":66,"sourceCodeEnd":88,"githubUrl":"https://github.com/slackhq/nebula/blob/dd8f660c0ac37903ec4080ca4d3c861ba9342ceb/pkclient/pkclient.go#L66-L88","documentation":"Test() performed a self-ECDH (DeriveNoise with the node's own public key) and the resulting shared secret length did not equal NoiseKeySize (32 bytes). The library throws this because a derive operation returning a different-size secret means the key or HSM cannot produce valid Noise material.","triggerScenarios":"Calling Test() when session.DeriveNoise succeeds but returns byte length != NoiseKeySize — e.g. the HSM derives raw X coordinate padded differently, or the key is not a P-256 EC key.","commonSituations":"HSMs that return the derived secret with leading-zero padding stripped; non-P-256 curves (P-384/P-521) producing 48/66-byte secrets; non-EC keys that still expose a derive mechanism.","solutions":["Provision/verify the key is EC P-256 (prime256v1) specifically","Check the HSM vendor's derive output format; left-pad to 32 bytes if a firmware quirk strips leading zeros","Update the HSM firmware/vendor PKCS#11 library","Confirm CKA_EC_PARAMS is the prime256v1 OID","Test the same key with a minimal PKCS#11 derive script to see raw length"],"exampleFix":"// before\npkcs11-tool --keypairgen --key-type EC:secp384r1 --usage-derive\n// after\npkcs11-tool --keypairgen --key-type EC:prime256v1 --usage-derive","handlingStrategy":"validation","validationCode":"// ensure curve is P-256 before deriving\nparams, _ := session.GetAttributeValue(key, []*pkcs11.Attribute{\n    pkcs11.NewAttribute(pkcs11.CKA_EC_PARAMS, nil),\n})\n// prime256v1 OID encoding: 06 08 2A 86 48 CE 3D 03 01 07\nif !bytes.Equal(params[0].Value, []byte{0x06,0x08,0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07}) {\n    return errors.New(\"HSM EC key is not P-256\")\n}","typeGuard":null,"tryCatchPattern":"if err := client.Test(); err != nil {\n    if strings.Contains(err.Error(), \"got a key of\") {\n        return fmt.Errorf(\"HSM derive output incompatible; use P-256 key/vendor fix: %w\", err)\n    }\n    return err\n}","preventionTips":["Provision only P-256 (prime256v1) derive keys","Test a fresh key with the HSM before production","Check vendor docs for derive output padding quirks","Keep vendor PKCS#11 middleware updated"],"tags":["pkcs11","hsm","ecdh","noise-protocol"],"backgroundTag":"ecdh-derived-key-size-mismatch","analyzedSha":"dd8f660c0ac37903ec4080ca4d3c861ba9342ceb","analyzedAt":"2026-09-03T11:13:55.444Z","contentChangedAt":"2026-09-03T11:13:55.444Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}