grafana/k6 · error

failed to parse input as ECDH key

Error message

failed to parse input as ECDH key: %w

What it means

Returned by importECDHJWK, which first tries to parse the JWK as an ECDSA key; the underlying importECDSAJWK call failed (bad JSON, missing fields, unsupported curve, or undecodable coordinates) and the error is re-wrapped with the ECDH context. The wrapped cause tells which stage of EC parsing failed. Note the ECDH path only supports the NIST curves.

Solutions

  1. Inspect the wrapped error to find the actual parsing failure and fix that field
  2. Ensure the JWK uses kty 'EC' with crv P-256/P-384/P-521 and correctly base64url-encoded x/y
  3. For X25519 keys use raw import instead of jwk
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/js/modules/k6/webcrypto/jwk.go:272 when the library encounters an invalid state.

Common situations: See trigger scenarios.

Understand the failure class


AI-assisted analysis of grafana/k6@8d06114777 (2026-08-18). Data as JSON: /api/errors/50a4284814e41075. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/webcrypto/jwk.go:273

		return pk, PublicCryptoKeyType, nil
	}

	d, err := base64URLDecode(jwkKey.D)
	if err != nil {
		return nil, UnknownCryptoKeyType, fmt.Errorf("failed to decode D: %w", err)
	}

	return &ecdsa.PrivateKey{
		PublicKey: *pk,
		D:         new(big.Int).SetBytes(d),
	}, PrivateCryptoKeyType, nil
}

func importECDHJWK(_ EllipticCurveKind, jsonKeyData []byte) (any, CryptoKeyType, error) {
	// first we do try to parse the key as ECDSA key
	key, _, err := importECDSAJWK(EllipticCurveKindP256, jsonKeyData)
	if err != nil {
		return nil, UnknownCryptoKeyType, fmt.Errorf("failed to parse input as ECDH key: %w", err)
	}

	switch key := key.(type) {
	case *ecdsa.PrivateKey:
		ecdhKey, err := key.ECDH()
		if err != nil {
			return nil, UnknownCryptoKeyType, fmt.Errorf("failed to convert ECDSA key to ECDH key: %w", err)
		}

		return ecdhKey, PrivateCryptoKeyType, nil
	case *ecdsa.PublicKey:
		ecdhKey, err := key.ECDH()
		if err != nil {
			return nil, UnknownCryptoKeyType, fmt.Errorf("failed to convert ECDSA key to ECDH key: %w", err)
		}

		return ecdhKey, PublicCryptoKeyType, nil
	default:

View on GitHub (pinned to 8d06114777)