OpenNHP/opennhp · error

unknown mode

Error message

unknown mode: %d

What it means

NewDataKeyPairECCMode converts a core.EccTypeEnum into a DataKeyPairECCMode. Only ECC_CURVE25519 and ECC_SM2 are accepted; any other enum value returns this error. Guards against invalid or zero-valued enum inputs when constructing data key pairs.

Solutions

  1. Pass core.ECC_CURVE25519 or core.ECC_SM2 explicitly
  2. Check that the EccTypeEnum field is initialized, not the zero value
  3. Update mapping code if a new EccTypeEnum was introduced upstream

Example fix

// before
var ecc core.EccTypeEnum // zero value -> unknown mode: 0
mode, err := NewDataKeyPairECCMode(ecc)
// after
mode, err := NewDataKeyPairECCMode(core.ECC_CURVE25519)
Defensive patterns

Strategy: type-guard

Validate before calling

if eccMode != core.ECC_CURVE25519 && eccMode != core.ECC_SM2 {
	return fmt.Errorf("unsupported EccTypeEnum: %d", eccMode)
}

Type guard

func validEccType(e core.EccTypeEnum) bool { return e == core.ECC_CURVE25519 || e == core.ECC_SM2 }

Try / catch

mode, err := NewDataKeyPairECCMode(eccMode)
if err != nil {
	mode, err = NewDataKeyPairECCMode(core.ECC_CURVE25519)
}

Prevention

When it happens

Trigger: Calling NewDataKeyPairECCMode with an EccTypeEnum other than core.ECC_CURVE25519 or core.ECC_SM2 (e.g. 0 / uninitialized) (nhp/core/ztdo/noise.go:225).

Common situations: Uninitialized EccTypeEnum fields in config structs (zero value), deserialized enum values from other systems, future enum members added upstream but not mapped here.

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/4f6048608cf4de07. Report an issue: GitHub.

Appendix: source

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

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)
}

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

// MessagePattern defines a set of tokens which are used during symmetric key agreement
type MessagePattern int

const (
	MessagePatternS MessagePattern = iota
	MessagePatternE
	MessagePatternRS

View on GitHub (pinned to 6e04ca5ff0)