OpenNHP/opennhp · error

unknown mode

Error message

unknown mode: %s

What it means

NewDataKeyPairECCModeWithName maps an ECC curve name string to a DataKeyPairECCMode. Only "CURVE25519" and "SM2" are accepted; anything else returns this error. It enforces the closed set of elliptic curves supported for data key pairs in the ztdo package.

Solutions

  1. Pass exactly "CURVE25519" or "SM2" (uppercase, exact match)
  2. Normalize/uppercase the config value before calling, or map aliases to the accepted names
  3. Align peer negotiation to only offer supported curves

Example fix

// before
mode, err := NewDataKeyPairECCModeWithName(cfg.Curve) // "curve25519"
// after
mode, err := NewDataKeyPairECCModeWithName(strings.ToUpper(cfg.Curve)) // "CURVE25519"
Defensive patterns

Strategy: validation

Validate before calling

if name != "CURVE25519" && name != "SM2" {
	return fmt.Errorf("unsupported ECC curve %q", name)
}

Try / catch

mode, err := NewDataKeyPairECCModeWithName(name)
if err != nil {
	mode, err = NewDataKeyPairECCModeWithName("CURVE25519") // default curve
}

Prevention

When it happens

Trigger: Calling NewDataKeyPairECCModeWithName with a name other than exactly "CURVE25519" or "SM2" — e.g. "curve25519", "P-256", "ed25519" (nhp/core/ztdo/noise.go:214).

Common situations: Config files using lowercase curve names or NIST curve names; protocol negotiation tables listing curves this build doesn't support; typos like "Curve25519".

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of OpenNHP/opennhp@6e04ca5ff0 (2026-09-07). Data as JSON: /api/errors/de10cb0acddf47c0. Report an issue: GitHub.

Appendix: source

Thrown at nhp/core/ztdo/noise.go:214

func (d DataKeyPairECCMode) ToHashType() core.HashTypeEnum {
	switch d {
	case CURVE25519:
		return core.HASH_SHA256
	case SM2:
		return core.HASH_SM3
	default:
		return core.HASH_SHA256
	}
}

func NewDataKeyPairECCModeWithName(mode string) (DataKeyPairECCMode, error) {
	switch mode {
	case "CURVE25519":
		return CURVE25519, nil
	case "SM2":
		return SM2, nil
	default:
		return 0, fmt.Errorf("unknown mode: %s", mode)
	}
}

func NewDataKeyPairECCMode(eccMode core.EccTypeEnum) (DataKeyPairECCMode, error) {
	switch eccMode {
	case core.ECC_CURVE25519:
		return CURVE25519, nil
	case core.ECC_SM2:
		return SM2, nil
	default:
		return 0, fmt.Errorf("unknown mode: %d", eccMode)
	}
}

func (d DataKeyPairECCMode) ECDHFromKey(prk []byte) core.Ecdh {
	return core.ECDHFromKey(d.ToEccType(), prk)
}

View on GitHub (pinned to 6e04ca5ff0)