Skip to content

Conversation

@justin-layerv
Copy link
Contributor

Summary

  • Add error handling to NewHash() and AeadFromKey() functions that were silently ignoring errors
  • Update all callers (10 files) to properly handle returned errors
  • Prevents potential panics from nil cipher objects

Problem

Cryptographic function errors were silently ignored in 8+ locations:

  • NewHash() ignored blake2s.New256() errors
  • AeadFromKey() ignored cipher and GCM creation errors
  • CBC functions ignored cipher creation errors

This could result in nil cipher objects being returned, causing panics or undefined behavior.

Changes

  • NewHash() now returns (hash.Hash, error)
  • AeadFromKey() now returns (cipher.AEAD, error)
  • CBCEncryption/Decryption properly handle cipher errors
  • Internal helpers (HMAC, symmetric agreement) panic on errors (programming bugs with valid params)
  • External APIs (nhpdevice C bindings) return proper error codes

Files Modified

File Change
nhp/core/crypto.go Core function signatures updated
nhp/core/responder.go Error handling in packet parsing
nhp/core/initiator.go Error handling in message assembly
nhp/core/kdf.go HMAC helper error handling
nhp/core/ztdo/noise.go Symmetric agreement error handling
nhp/core/ztdo/ztdo.go Signature helper error handling
nhp/core/main/nhpdevice.go C binding error returns
nhp/core/benchmark/ecc_rsa_test.go Test updates
nhp/test/ecdsa_test.go Test updates
nhp/test/packet_test.go Test updates

Test Plan

  • Verify build succeeds: go build github.com/OpenNHP/opennhp/nhp/core/...
  • Verify tests pass: go test ./core/... (benchmark and ztdo tests pass)
  • Verify no regressions in crypto operations

@justin-layerv justin-layerv self-assigned this Jan 4, 2026
@justin-layerv justin-layerv force-pushed the security/crypto-error-handling branch from 8524d23 to 7facc7d Compare January 4, 2026 17:07
Previously, cryptographic function errors were silently ignored in
multiple locations, potentially returning nil cipher objects that
could cause panics or undefined behavior:

- NewHash() silently ignored blake2s.New256() errors
- AeadFromKey() ignored cipher and GCM creation errors
- CBC functions ignored cipher creation errors

Changes:
- NewHash() now returns (hash.Hash, error)
- AeadFromKey() now returns (cipher.AEAD, error)
- CBCEncryption/Decryption properly handle cipher errors
- All callers updated to check returned errors
- Internal helpers use panic for programming bugs (invalid params)
- External APIs return errors for proper handling

Files modified:
- nhp/core/crypto.go (core changes)
- nhp/core/responder.go (caller updates)
- nhp/core/initiator.go (caller updates)
- nhp/core/kdf.go (HMAC helpers)
- nhp/core/ztdo/noise.go (symmetric agreement)
- nhp/core/ztdo/ztdo.go (signature helpers)
- nhp/core/main/nhpdevice.go (C bindings)
- nhp/core/benchmark/ecc_rsa_test.go (tests)
- nhp/test/ecdsa_test.go (tests)
- nhp/test/packet_test.go (tests)
@justin-layerv justin-layerv force-pushed the security/crypto-error-handling branch from 7facc7d to 327a2bb Compare January 4, 2026 17:09
- Keep proper error handling from PR (returning errors instead of log+nil)
- Fix typo: "invloved" -> "involved" in noise.go comment
- Keep descriptive error messages for CBC encryption/decryption
@craftleon craftleon merged commit 4547009 into main Jan 7, 2026
11 checks passed
@craftleon craftleon deleted the security/crypto-error-handling branch January 7, 2026 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants