grafana/k6 · error

failed to convert ECDSA key to ECDH key

Error message

failed to convert ECDSA key to ECDH key: %w

What it means

Returned by importECDHJWK when the JWK parsed successfully as an ECDSA private key but Go's ecdsa.PrivateKey.ECDH() refused to convert it to an ECDH key — typically because the key's curve is not one of the ECDH-capable NIST curves (X25519 mismatch) or the private scalar is invalid (e.g. zero or out of range).

Solutions

  1. Use a private key on P-256, P-384 or P-521 for ECDH import
  2. Regenerate the key pair — an invalid scalar indicates corrupted key material
Defensive patterns

Strategy: fallback

When it happens

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

Common situations: See trigger scenarios.


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

Appendix: source

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

	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:
		return nil, UnknownCryptoKeyType, errors.New("input isn't a valid ECDH key")
	}
}

type rsaJWK struct {
	Kty string `json:"kty"`          // Key Type
	N   string `json:"n"`            // Modulus

View on GitHub (pinned to 8d06114777)