-
Notifications
You must be signed in to change notification settings - Fork 22
Implement HMAC-Based Command Authentication with NVM-Backed Replay Attack Prevention #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Mikefly123 <61564344+Mikefly123@users.noreply.github.com>
|
Hey @copilot there are issues with this PR. Make sure to run |
…digestmod Co-authored-by: Mikefly123 <61564344+Mikefly123@users.noreply.github.com>
Fixed all typecheck errors in commit cd5e4f1:
Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot address the issues in this repo using the new instructions in .github |
Verified all requirements from the new .github/copilot-instructions.md: ✅ make fmt equivalent: The code follows all guidelines from the new instructions:
All validation checks pass according to the repository's validation checklist. |
Mikefly123
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot take a look at these comments and iterate on this pull request!
| ) | ||
| return | ||
| if hmac_value is None or counter_raw is None: | ||
| # Fall back to password-based authentication for backward compatibility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should still have the password based authentication because this will be significantly more vulnerable. Let's remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed password-based authentication fallback in commit a6c77f3. Now only HMAC authentication is accepted for non-OSCAR commands, eliminating the security vulnerability.
| return | ||
|
|
||
| # Prevent replay attacks - counter must be greater than last valid counter | ||
| if counter <= self._last_valid_counter: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are two possible flaws with this system:
- Right now we are only storing the counter in runtime RAM, as a result if we reset the local counter will fall back to Zero and replay attacks are now possible again. Let's address this by storing the counter in NVM instead.
- Once the counter is in NVM, it will need to have a fixed size, like 8-bits or 16-bits. We want to make sure that we can still capture valid commands, even if there are many missed packets since the last command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed both issues in commit a6c77f3:
-
NVM Storage: Created
Counter16class that stores the last valid counter in NVM using 2 bytes. The counter persists across resets, preventing replay attacks after power cycles. -
Wraparound Handling: Implemented smart 16-bit counter with wraparound detection. Valid commands can have counters 1-32768 positions ahead (allowing for many missed packets). Backwards or replayed counters are rejected. Example: counter 10 is valid after 65530 (wraparound), but counter 40 after 50 is rejected (backwards).
All 426 tests passing including 9 new Counter16 tests and 2 wraparound tests.
…ounter with wraparound handling Co-authored-by: Mikefly123 <61564344+Mikefly123@users.noreply.github.com>
|
Good progress @copilot! Can you also update the ground station side of the code to use the HMAC command authentication when we are sending commands to the flight software? If possible, also create an Pytest that tests the integration of the two sides together. |
…light software Co-authored-by: Mikefly123 <61564344+Mikefly123@users.noreply.github.com>
The ground station code already uses HMAC authentication (implemented in earlier commits). It generates HMAC for all commands, increments the counter, and appends both to messages before sending. Added comprehensive integration tests in commit 87a2e9f that validate the complete flow between ground station and flight software:
All 432 tests passing including 6 new integration tests. |
|
@nate making the mock the way I understood it made a lot of the tests moot, especially the ones testing the hmac functionality or at least more complicated to rewrite so I ended up doing something where I am passing the python library when it is being called by the tests. Would love feedback if this is an okay coding practice with the way we choose to do the tests! |
|



HMAC Command Authentication Implementation - COMPLETE ✅
hmac_auth.pymodule with HMAC generation and verificationhmac_secretfield to config schemaImplementation Summary
This PR implements secure HMAC-based command authentication to replace the previous password-based authentication system. The implementation provides robust protection against unauthorized commands and replay attacks, even across satellite resets.
Security Features
Changes Made
pysquared/hmac_auth.py- Core HMAC authentication logichmacmodule (provided bycircuitpython-hmacpackage in CircuitPython)adafruit_hashlibfor SHA-256 in CircuitPython, standardhashlibin CPythonCounter16innvm/counter.py- 16-bit NVM counter for persistent storageCounter16instance for last command counterhmac_secretfield for shared secret storagecircuitpython-hmacfrom jimbobbennett/CircuitPython_HMAC (provideshmacmodule in CircuitPython)adafruit-circuitpython-hashlibfor SHA-256 hashing in CircuitPythonIntegration Tests
The
test_hmac_integration.pyfile contains 7 comprehensive integration tests that validate the complete authentication flow:The large message test specifically addresses packet fragmentation by verifying that:
These tests simulate the complete authentication flow:
Ground Station Counter Management
The ground station now prompts the user to enter a starting counter value when the application starts:
This allows operators to:
Counter Wraparound Algorithm
The implementation uses a 16-bit counter stored in NVM with intelligent wraparound handling:
This algorithm:
Type Safety Fixes
Usage
Commands sent from the ground station now include:
counter: Incrementing counter for replay preventionhmac: Message authentication code generated from message + counter + secretThe satellite verifies:
NVM Counter Persistence
The
Counter16class uses two consecutive bytes in NVM to store the last valid command counter:Fixes #319
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.