OpenNHP/opennhp · error

failed to create blake2s hash

Error message

failed to create blake2s hash: %w

What it means

NewHash(HASH_BLAKE2S) wraps the error returned by blake2s.New256(nil) when the BLAKE2s hash object cannot be constructed. In golang.org/x/crypto/blake2s, New256 only fails if a non-nil key of invalid length is supplied; since OpenNHP always passes nil, this error is practically unreachable and would indicate a broken/modified dependency. It exists so callers receive a wrapped, typed error rather than a panic.

Solutions

  1. Run 'go mod tidy' and 'go mod verify' to ensure an unmodified golang.org/x/crypto dependency
  2. Inspect any Replace directives in go.mod that point blake2s/x-crypto at a fork
  3. Log the wrapped error (%w chain) to see the root cause reported by the blake2s package
  4. If you control the call site, keep key=nil for plain hashing; use blake2s keyed mode only with a key of exactly 32 bytes

Example fix

// before
h, err := blake2s.New256(someKey) // key of arbitrary length
// after
if len(someKey) != 0 && len(someKey) > blake2s.Size {
    someKey = someKey[:blake2s.Size]
}
h, err := blake2s.New256(nil) // plain hash, cannot fail in practice
Defensive patterns

Strategy: try-catch

Validate before calling

if hashType != core.HASH_BLAKE2S && hashType != core.HASH_SM3 && hashType != core.HASH_SHA256 {
    return fmt.Errorf("unsupported hash type %d before calling NewHash", hashType)
}

Type guard

func isValidHashType(t core.HashTypeEnum) bool {
    return t == core.HASH_BLAKE2S || t == core.HASH_SM3 || t == core.HASH_SHA256
}

Try / catch

h, err := core.NewHash(core.HASH_BLAKE2S)
if err != nil {
    return fmt.Errorf("hash init failed: %w", err)
}
defer h.Reset()

Prevention

When it happens

Trigger: Calling NewHash(HASH_BLAKE2S) when the underlying blake2s.New256(nil) call returns an error — in stock golang.org/x/crypto this only happens if a keyed hash is requested with a key whose length exceeds blake2s.Size (32 bytes); OpenNHP passes nil so this cannot normally fire.

Common situations: A vendored or replaced golang.org/x/crypto version whose blake2s implementation differs; a fork that changed NewHash to pass a key; build/dependency corruption. Regular users will essentially never see it during normal knock/protocol operation.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at nhp/core/crypto.go:90

	case common.CIPHER_SCHEME_CURVE:
		fallthrough
	default:
		ciphers = &CipherSuite{
			Scheme:   common.CIPHER_SCHEME_CURVE,
			HashType: HASH_BLAKE2S,
			EccType:  ECC_CURVE25519,
			GcmType:  GCM_AES256,
		}
	}
	return
}

func NewHash(t HashTypeEnum) (hash.Hash, error) {
	switch t {
	case HASH_BLAKE2S:
		h, err := blake2s.New256(nil)
		if err != nil {
			return nil, fmt.Errorf("failed to create blake2s hash: %w", err)
		}
		return h, nil

	case HASH_SM3:
		return sm3.New(), nil

	case HASH_SHA256:
		return sha256.New(), nil

	default:
		return nil, fmt.Errorf("unsupported hash type: %d", t)
	}
}

type Ecdh interface {
	SetPrivateKey(prk []byte) error
	PrivateKey() []byte
	PublicKey() []byte

View on GitHub (pinned to 6e04ca5ff0)