Conversation
WalkthroughAdds a PIN-prefix anti‑phishing feature: BIP39 wordlist, pin-prefix API (derive/get words, manage digits), HMAC‑SHA256 derivation implementation, unit tests, and build/test CMake changes. Changes
Sequence DiagramsequenceDiagram
participant Caller
participant PinMod as pin_prefix module
participant HMAC as HMAC-SHA256
participant WL as BIP39 wordlist
Caller->>PinMod: pin_prefix_derive_words(prefix, secret)
PinMod->>HMAC: HMAC-SHA256(context || digits, key=secret)
HMAC-->>PinMod: 32-byte digest
PinMod->>PinMod: extract two 11-bit indices
PinMod-->>Caller: word1_index, word2_index
Caller->>PinMod: pin_prefix_get_words(prefix, secret)
PinMod->>PinMod: pin_prefix_derive_words(...)
PinMod->>WL: bip39_get_word(index1)
WL-->>PinMod: word1_string
PinMod->>WL: bip39_get_word(index2)
WL-->>PinMod: word2_string
PinMod-->>Caller: word1, word2
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related issues
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 5
🤖 Fix all issues with AI agents
In `@main/bip39_wordlist.h`:
- Around line 1-8: The file fails clang-format; reformat main/bip39_wordlist.h
to match project style by running the repository's clang-format configuration
(or apply the same formatting rules) so the BIP39_WORDLIST array and header
guards follow the project's indentation and spacing conventions; update spacing
around preprocessor lines (`#ifndef`, `#define`, `#endif`), the static const
declaration, and the array entries (BIP39_WORDLIST) so the file passes CI
clang-format checks.
In `@main/pin_prefix.c`:
- Around line 19-49: In pin_prefix_derive_words, check the return value of
mbedtls_md_info_from_type(MBEDTLS_MD_SHA256) before calling mbedtls_md_hmac; if
it returns NULL, securely zero input and hmac_out (using secure_memzero) and
return an error (e.g., -1) to avoid dereferencing NULL; then use the non-NULL
md_info when calling mbedtls_md_hmac and proceed as before. Ensure you reference
mbedtls_md_info_from_type, mbedtls_md_hmac, input, hmac_out, and secure_memzero
in the fix so the cleanup always runs on the error path.
In `@test/native/mocks/mbedtls/md.h`:
- Around line 147-163: The early return on ilen > 1024 leaves sensitive stacks
(k_ipad, k_opad, and tk) uncleared; before returning -1 insert explicit
zeroization (e.g. memset(k_ipad, 0, sizeof k_ipad); memset(k_opad, 0, sizeof
k_opad); memset(tk, 0, sizeof tk);) so key material is wiped from the stack,
taking care to zero tk even if keylen was not reduced to 32.
In `@test/native/test_pin_prefix.c`:
- Around line 1-20: Reformat test/native/test_pin_prefix.c to satisfy
clang-format: run the repository's clang-format configuration against the file
(or apply your editor's format command) so spacing, include ordering,
indentation, and macro alignment conform; ensure includes ("crypto_asm.h",
"pin_prefix.h", and the inlined "pin_prefix.c"), macro definitions
TEST/PASS/FAIL, and the TEST_SECRET array are reformatted consistently with the
project's style. Commit the reformatted file so CI passes.
- Around line 8-11: The test currently includes the implementation file
"pin_prefix.c" directly, which breaks build boundaries and can cause duplicate
symbols; remove the '#include "pin_prefix.c"' line from
test/native/test_pin_prefix.c and instead add pin_prefix.c to the test's build
target in the CMakeLists (e.g., add it to the test executable's sources via
target_sources or add_executable) so the implementation is compiled and linked
normally alongside the test; keep the existing includes for pin_prefix.h and
crypto_asm.h unchanged.
🧹 Nitpick comments (4)
main/bip39_wordlist.h (1)
7-7: Avoid multiple wordlist copies across translation units.
Keeping a 2048-entrystaticarray in a header can emit a full copy per TU that includes it. Consider declaring itexternhere and moving the definition to a single.cfile to avoid flash bloat on embedded targets.♻️ Suggested header change
-static const char *const BIP39_WORDLIST[2048] = { +extern const char *const BIP39_WORDLIST[2048];main/pin_prefix.h (1)
11-16: Clarify buffer sizing for BIP39 words.
BIP39_MAX_WORD_LENdoesn’t indicate whether the NUL terminator is included. Callers who allocate exactly 8 bytes will failpin_prefix_get_words. Consider an explicit buffer-size constant (or a brief comment) to make the expectation unambiguous.♻️ Suggested API clarification
-#define BIP39_MAX_WORD_LEN 8 +#define BIP39_MAX_WORD_LEN 8 +#define BIP39_MAX_WORD_BUFSIZE (BIP39_MAX_WORD_LEN + 1)main/pin_prefix.c (2)
14-17: MakeCONTEXT_LENderive from the string literal.Avoid manual sync risk if the context string changes.
♻️ Proposed refactor
-#define CONTEXT_LEN 13 +#define CONTEXT_LEN (sizeof(ANTI_PHISHING_CONTEXT) - 1)
58-84: Clear outputs on early failures to avoid stale words.This prevents callers from accidentally reusing old word buffers after a failed derivation.
♻️ Proposed tweak
if (!word1 || !word2 || word1_size == 0 || word2_size == 0) return -1; + + word1[0] = '\0'; + word2[0] = '\0';
Summary
Test plan
Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.