-
Notifications
You must be signed in to change notification settings - Fork 0
Add PIN prefix anti-phishing verification #134
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
Open
wksantiago
wants to merge
5
commits into
main
Choose a base branch
from
PIN-prefix-anti-phishing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3dfd6f9
Add PIN prefix anti-phishing derivation
wksantiago 26b2df7
fix: address security review findings
wksantiago 86342e0
Simplify pin_prefix validation and use secure_memzero in mock
wksantiago 6517e3b
Apply clang-format to fix CI formatting check
wksantiago 8c193f6
Add NULL checks and secure cleanup on error paths
wksantiago File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // SPDX-FileCopyrightText: © 2026 PrivKey LLC | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| #include "pin_prefix.h" | ||
| #include "bip39_wordlist.h" | ||
| #include "crypto_asm.h" | ||
| #include <mbedtls/md.h> | ||
| #include <stdbool.h> | ||
| #include <string.h> | ||
|
|
||
| _Static_assert(sizeof(BIP39_WORDLIST) / sizeof(BIP39_WORDLIST[0]) == BIP39_WORD_COUNT, | ||
| "BIP39_WORDLIST size mismatch"); | ||
|
|
||
| #define HMAC_OUTPUT_SIZE 32 | ||
| #define ANTI_PHISHING_CONTEXT "anti-phishing" | ||
| #define CONTEXT_LEN 13 | ||
| #define SECRET_LEN_MAX 1024 | ||
|
|
||
| int pin_prefix_derive_words(const pin_prefix_t *prefix, const uint8_t *device_secret, | ||
| size_t secret_len, uint16_t *word1_index, uint16_t *word2_index) { | ||
| if (!prefix || !device_secret || !word1_index || !word2_index) | ||
| return -1; | ||
| if (prefix->len < PIN_PREFIX_MIN_LEN || prefix->len > PIN_PREFIX_MAX_LEN) | ||
| return -1; | ||
| if (secret_len == 0 || secret_len > SECRET_LEN_MAX) | ||
| return -1; | ||
|
|
||
| uint8_t input[CONTEXT_LEN + PIN_PREFIX_MAX_LEN]; | ||
| memcpy(input, ANTI_PHISHING_CONTEXT, CONTEXT_LEN); | ||
| memcpy(input + CONTEXT_LEN, prefix->digits, prefix->len); | ||
| size_t input_len = CONTEXT_LEN + prefix->len; | ||
|
|
||
| uint8_t hmac_out[HMAC_OUTPUT_SIZE]; | ||
| const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); | ||
| if (!md_info) { | ||
| secure_memzero(input, sizeof(input)); | ||
| secure_memzero(hmac_out, sizeof(hmac_out)); | ||
| return -1; | ||
| } | ||
| int ret = mbedtls_md_hmac(md_info, device_secret, secret_len, input, input_len, hmac_out); | ||
|
|
||
| secure_memzero(input, sizeof(input)); | ||
|
|
||
| if (ret != 0) { | ||
| secure_memzero(hmac_out, sizeof(hmac_out)); | ||
| return -1; | ||
| } | ||
|
|
||
| *word1_index = ((uint16_t)hmac_out[0] << 3 | hmac_out[1] >> 5) & 0x7FF; | ||
| *word2_index = ((uint16_t)(hmac_out[1] & 0x1F) << 6 | hmac_out[2] >> 2) & 0x7FF; | ||
|
|
||
| secure_memzero(hmac_out, sizeof(hmac_out)); | ||
| return 0; | ||
| } | ||
|
|
||
| const char *bip39_get_word(uint16_t index) { | ||
| if (index >= BIP39_WORD_COUNT) { | ||
| return NULL; | ||
| } | ||
| return BIP39_WORDLIST[index]; | ||
| } | ||
|
|
||
| int pin_prefix_get_words(const pin_prefix_t *prefix, const uint8_t *device_secret, | ||
| size_t secret_len, char *word1, size_t word1_size, char *word2, | ||
| size_t word2_size) { | ||
| if (!word1 || !word2 || word1_size == 0 || word2_size == 0) | ||
| return -1; | ||
|
|
||
| uint16_t idx1, idx2; | ||
| if (pin_prefix_derive_words(prefix, device_secret, secret_len, &idx1, &idx2) != 0) { | ||
| return -1; | ||
| } | ||
|
|
||
| const char *w1 = bip39_get_word(idx1); | ||
| const char *w2 = bip39_get_word(idx2); | ||
| if (!w1 || !w2) { | ||
| return -1; | ||
| } | ||
|
|
||
| size_t len1 = strlen(w1); | ||
| size_t len2 = strlen(w2); | ||
| if (len1 >= word1_size || len2 >= word2_size) { | ||
| return -1; | ||
| } | ||
|
|
||
| memcpy(word1, w1, len1 + 1); | ||
| memcpy(word2, w2, len2 + 1); | ||
| return 0; | ||
| } | ||
|
|
||
| int pin_prefix_set_digit(pin_prefix_t *prefix, uint8_t digit) { | ||
| if (!prefix || digit > 9 || prefix->len >= PIN_PREFIX_MAX_LEN) | ||
| return -1; | ||
| prefix->digits[prefix->len++] = digit; | ||
| return 0; | ||
| } | ||
|
|
||
| void pin_prefix_clear(pin_prefix_t *prefix) { | ||
| if (prefix) { | ||
| secure_memzero(prefix, sizeof(*prefix)); | ||
| } | ||
| } | ||
|
|
||
| bool pin_prefix_is_ready(const pin_prefix_t *prefix) { | ||
| return prefix && prefix->len >= PIN_PREFIX_MIN_LEN; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // SPDX-FileCopyrightText: © 2026 PrivKey LLC | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
|
||
| #ifndef PIN_PREFIX_H | ||
| #define PIN_PREFIX_H | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
|
|
||
| #define PIN_PREFIX_MAX_LEN 4 | ||
| #define PIN_PREFIX_MIN_LEN 2 | ||
| #define PIN_PREFIX_WORD_COUNT 2 | ||
| #define BIP39_WORD_COUNT 2048 | ||
| #define BIP39_MAX_WORD_LEN 8 | ||
|
|
||
| typedef struct { | ||
| uint8_t digits[PIN_PREFIX_MAX_LEN]; | ||
| uint8_t len; | ||
| } pin_prefix_t; | ||
|
|
||
| int pin_prefix_derive_words(const pin_prefix_t *prefix, const uint8_t *device_secret, | ||
| size_t secret_len, uint16_t *word1_index, uint16_t *word2_index); | ||
|
|
||
| const char *bip39_get_word(uint16_t index); | ||
|
|
||
| int pin_prefix_get_words(const pin_prefix_t *prefix, const uint8_t *device_secret, | ||
| size_t secret_len, char *word1, size_t word1_size, char *word2, | ||
| size_t word2_size); | ||
|
|
||
| int pin_prefix_set_digit(pin_prefix_t *prefix, uint8_t digit); | ||
|
|
||
| void pin_prefix_clear(pin_prefix_t *prefix); | ||
|
|
||
| bool pin_prefix_is_ready(const pin_prefix_t *prefix); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.