{"record":{"id":"50040827dafef0a3","repo":"gchq/CyberChef","slug":"invalid-public-key","errorCode":null,"errorMessage":"Invalid Public Key","messagePattern":"Invalid Public Key","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/lib/SM2.mjs","lineNumber":62,"sourceCode":"\n        this.format = format;\n    }\n\n    /**\n     * Set the public key coordinates for the SM2 class\n     *\n     * @param {string} publicKeyX\n     * @param {string} publicKeyY\n     */\n    setPublicKey(publicKeyX, publicKeyY) {\n        /*\n        * TODO: This needs some additional length validation; and checking for errors in the decoding process\n        * TODO: Can probably support other public key encoding methods here as well in the future\n        */\n        this.publicKey = this.ecParams.curve.decodePointHex(\"04\" + publicKeyX + publicKeyY);\n\n        if (this.publicKey.isInfinity()) {\n            throw new OperationError(\"Invalid Public Key\");\n        }\n    }\n\n    /**\n     * Set the private key value for the SM2 class\n     *\n     * @param {string} privateKey\n     */\n    setPrivateKey(privateKeyHex) {\n        this.privateKey = new r.BigInteger(privateKeyHex, 16);\n    }\n\n    /**\n     * Main encryption function; takes user input, processes encryption and returns the result in hex (with the components arranged as configured by the user args)\n     *\n     * @param {*} input\n     * @returns {string}\n     */","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/lib/SM2.mjs#L44-L80","documentation":"SM2's setPublicKey() builds an uncompressed point '04'+X+Y, decodes it via the elliptic curve, then at SM2.mjs:62 rejects the result if it is the point at infinity. The decode may succeed on syntactically valid hex that is mathematically not on the curve or evaluates to the identity element, so this is a semantic validity check, not just a parse check.","triggerScenarios":"setPublicKey(publicKeyX, publicKeyY) is called with X/Y hex coordinates that decode to the identity element (point at infinity). Causes: wrong curve parameters loaded, X/Y swapped, coordinates that do not satisfy the SM2 curve equation y^2 = x^3 + ax + b, or a malformed/truncated hex string that decodes to a degenerate point.","commonSituations":"Public key copied from a different curve (e.g. secp256k1 vs SM2); hex endianness mismatch (big-endian vs little-endian); X and Y swapped during manual transcription; truncated key where one coordinate is all zeros.","solutions":["Confirm the X and Y coordinates are correct, in the right order, and at the expected length (32 bytes / 64 hex chars each for SM2 P-256).","Verify the SM2 curve parameters object (ecParams) is the standard SM2 curve, not a secp256k1/P-256 stand-in.","Check endianness matches the decoder's expectation (big-endian hex).","Reject zero/all-zero coordinates in caller code before invoking setPublicKey."],"exampleFix":"// before\nsm2.setPublicKey(coordB, coordA); // X/Y swapped\n// after\nsm2.setPublicKey(coordA, coordB);","handlingStrategy":"validation","validationCode":"// Validate X/Y hex before calling setPublicKey.\nfunction validateSm2PubKeyHex(x, y) {\n  const re = /^[0-9a-fA-F]{64}$/;\n  if (!re.test(x) || !re.test(y))\n    throw new TypeError(\"SM2 public key coordinates must be 64 hex chars each\");\n  if (/^0+$/.test(x) || /^0+$/.test(y))\n    throw new TypeError(\"SM2 public key coordinates must be non-zero\");\n}","typeGuard":"function looksLikeSm2PublicKeyCoords(x, y) {\n  return typeof x === \"string\" && typeof y === \"string\" &&\n    /^[0-9a-fA-F]{64}$/.test(x) && /^[0-9a-fA-F]{64}$/.test(y) &&\n    !/^0+$/.test(x) && !/^0+$/.test(y);\n}","tryCatchPattern":"import OperationError from \"../errors/OperationError.mjs\";\ntry {\n  validateSm2PubKeyHex(x, y);\n  sm2.setPublicKey(x, y);\n} catch (e) {\n  if (e instanceof OperationError && /Invalid Public Key/.test(e.message)) {\n    // coordinates decoded but point is on-curve degenerate; re-check curve params\n  } else throw e;\n}","preventionTips":["Always pass X before Y, in big-endian hex, 32 bytes each.","Verify ecParams is the SM2 curve (a, b, p, Gx, Gy match GM/T 0003).","Reject zero or all-zero coordinates at the caller before invoking setPublicKey."],"tags":["sm2","ecc","public-key","validation","argument-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}