ecdsa.com
WebCrypto crypto.subtle.importKeyWebCrypto

Error message

DataError: Data provided to an operation does not meet requirements

The DOMException name DataError is what to match on; the message text varies by runtime. Node prints "Invalid keyData" (or a specific reason like "Named curve mismatch"); browsers typically show the generic sentence above.

WebCrypto crypto.subtle.importKey — browsers & Node

What it means

importKey (or unwrapKey) rejected the key bytes: they do not parse as the format you declared, or their contents contradict the algorithm parameters (wrong curve, mismatched JWK fields). It is the WebCrypto equivalent of "these bytes are not the key you say they are" — thrown before any key object is created.

Why it happens

How to fix it

  1. 1.

    Convert PEM → ArrayBuffer correctly

    Strip the armor lines, base64-decode the body, and pick the format by the PEM label: PUBLIC KEY → spki, PRIVATE KEY → pkcs8.

    js
    function pemToArrayBuffer(pem) {
      const b64 = pem.replace(/-----[^-]+-----/g, "").replace(/\s+/g, "");
      return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)).buffer;
    }
    
    const key = await crypto.subtle.importKey(
      "spki", pemToArrayBuffer(publicKeyPem),
      { name: "ECDSA", namedCurve: "P-256" },
      false, ["verify"],
    );
  2. 2.

    Prefer JWK when you have JSON key material

    JWKS endpoints already serve WebCrypto-ready objects — no binary conversion, and crv travels with the key.

    js
    const key = await crypto.subtle.importKey(
      "jwk", jwk,
      { name: "ECDSA", namedCurve: jwk.crv },
      false, ["verify"],
    );
  3. 3.

    Confirm what the bytes really are

    Paste the key into the certificate decoder or signature verifier below — both parse SPKI/PKCS#8/JWK locally and report the actual curve and key type, which usually names the mismatch immediately.

Related errors

← Browse the full signature error database