Skip to content

Commit a094e6a

Browse files
committed
support attach to pin
1 parent 7761485 commit a094e6a

File tree

126 files changed

+7107
-2000
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+7107
-2000
lines changed

common/protob/messages-common.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ message ButtonRequest {
7575
ButtonRequest_Warning = 18;
7676
ButtonRequest_PassphraseEntry = 19;
7777
ButtonRequest_PinEntry = 20;
78+
ButtonRequest_AttachPin = 8000;
7879
}
7980
}
8081

@@ -119,6 +120,7 @@ message PinMatrixAck {
119120
*/
120121
message PassphraseRequest {
121122
optional bool _on_device = 1 [deprecated=true]; // <2.3.0
123+
optional bool exists_attach_pin_user = 8000;
122124
}
123125

124126
/**
@@ -129,6 +131,7 @@ message PassphraseAck {
129131
optional string passphrase = 1;
130132
optional bytes _state = 2 [deprecated=true]; // <2.3.0
131133
optional bool on_device = 3; // user wants to enter passphrase on the device
134+
optional bool on_device_attach_pin = 8000;
132135
}
133136

134137
/**

common/protob/messages-management.proto

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ import "messages.proto";
1313
* Type of the mnemonic backup given/received by the device during reset/recovery.
1414
*/
1515
enum BackupType {
16-
Bip39 = 0; // also called "Single Backup", see BIP-0039
17-
Slip39_Basic = 1; // also called "Shamir Backup", see SLIP-0039
18-
Slip39_Advanced = 2; // also called "Super Shamir" or "Shamir with Groups", see SLIP-0039#two-level-scheme
16+
Bip39 = 0; // also called "Single Backup", see BIP-0039
17+
Slip39_Basic = 1; // also called "Shamir Backup", see SLIP-0039
18+
Slip39_Advanced = 2; // also called "Super Shamir" or "Shamir with Groups", see SLIP-0039#two-level-scheme
19+
Slip39_Single_Extendable = 3; // extendable single-share Shamir backup
20+
Slip39_Basic_Extendable = 4; // extendable multi-share Shamir backup
21+
Slip39_Advanced_Extendable = 5; // extendable multi-share Shamir backup with groups
1922
}
2023

2124
/**
@@ -36,6 +39,8 @@ message Initialize {
3639
optional bytes session_id = 1; // assumed device session id; Trezor clears caches if it is different or empty
3740
optional bool _skip_passphrase = 2 [deprecated=true]; // removed as part of passphrase redesign
3841
optional bool derive_cardano = 3; // whether to derive Cardano Icarus root keys in this session
42+
optional string passphrase_state = 8000;
43+
optional bool is_contains_attach = 8001;
3944
}
4045

4146
/**
@@ -126,6 +131,7 @@ message Features {
126131
Capability_Shamir = 15 [(bitcoin_only) = true];
127132
Capability_ShamirGroups = 16 [(bitcoin_only) = true];
128133
Capability_PassphraseEntry = 17 [(bitcoin_only) = true]; // the device is capable of passphrase entry directly on the device
134+
Capability_AttachToPin = 18 [(bitcoin_only) = true]; // OneKey: Attach passphrase to PIN feature
129135
}
130136
optional BackupType backup_type = 31; // type of device backup (BIP-39 / SLIP-39 basic / SLIP-39 advanced)
131137
optional bool sd_card_present = 32; // is SD card present
@@ -186,6 +192,8 @@ message Features {
186192
optional OneKeySEState onekey_se02_state = 622;
187193
optional OneKeySEState onekey_se03_state = 623;
188194
optional OneKeySEState onekey_se04_state = 624;
195+
optional bool attach_to_pin_user = 625;
196+
optional bool unlocked_attach_pin = 626;
189197
}
190198

191199
/**
@@ -805,5 +813,44 @@ message UnlockPath {
805813
* @next GetAddress
806814
*/
807815
message UnlockedPathRequest {
808-
optional bytes mac = 1; // authentication code for future UnlockPath calls
816+
optional bytes mac = 1;
809817
}
818+
819+
/**
820+
* Request: Get current passphrase state
821+
* @start
822+
* @next PassphraseState
823+
*/
824+
message GetPassphraseState {
825+
optional string passphrase_state = 1;
826+
optional bool _only_main_pin = 2;
827+
optional bool allow_create_attach_pin = 3;
828+
}
829+
830+
/**
831+
* Response: Current passphrase state
832+
* @end
833+
*/
834+
message PassphraseState {
835+
optional string passphrase_state = 1;
836+
optional bytes session_id = 2;
837+
optional bool unlocked_attach_pin = 3;
838+
}
839+
840+
/**
841+
* Request: Unlock device
842+
* @start
843+
* @next UnLockDeviceResponse
844+
*/
845+
message UnLockDevice {
846+
}
847+
848+
/**
849+
* Response: Device unlock status
850+
* @end
851+
*/
852+
message UnLockDeviceResponse {
853+
optional bool unlocked = 1;
854+
optional bool unlocked_attach_pin = 2;
855+
optional bool passphrase_protection = 3;
856+
}

common/protob/messages-tron.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ message TronSignTx {
9999
optional uint64 balance = 3;
100100
optional string receiver_address = 4;
101101
optional bool lock = 5;
102+
optional uint64 lock_period = 6;
102103
}
103104
message TronUnDelegateResourceContract {
104105
optional TronResourceCode resource = 2;

common/protob/messages.proto

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ enum MessageType {
7777
option (has_bitcoin_only_values) = true;
7878
option allow_alias = true;
7979
// Management
80-
MessageType_Initialize = 0 [(bitcoin_only) = true, (wire_in) = true, (wire_tiny) = true];
80+
MessageType_Initialize = 0 [(bitcoin_only) = true, (wire_in) = true];
8181
MessageType_Ping = 1 [(bitcoin_only) = true, (wire_in) = true];
8282
MessageType_Success = 2 [(bitcoin_only) = true, (wire_out) = true, (wire_debug_out) = true];
8383
MessageType_Failure = 3 [(bitcoin_only) = true, (wire_out) = true, (wire_debug_out) = true];
@@ -121,6 +121,7 @@ enum MessageType {
121121
MessageType_UnlockPath = 93 [(bitcoin_only) = true, (wire_in) = true];
122122
MessageType_UnlockedPathRequest = 94 [(bitcoin_only) = true, (wire_out) = true];
123123

124+
124125
MessageType_SetU2FCounter = 63 [(wire_in) = true];
125126
MessageType_GetNextU2FCounter = 80 [(wire_in) = true];
126127
MessageType_NextU2FCounter = 81 [(wire_out) = true];
@@ -605,4 +606,8 @@ enum MessageType {
605606
MessageType_OnekeyGetFeatures = 10025 [(bitcoin_only) = true,(wire_in) = true];
606607
MessageType_OnekeyFeatures = 10026 [(bitcoin_only) = true,(wire_out) = true];
607608
MessageType_WriteSEPrivateKey = 10027 [(wire_in) = true, (wire_bootloader) = true];
608-
}
609+
MessageType_GetPassphraseState = 10028 [(wire_in) = true];
610+
MessageType_PassphraseState = 10029 [(wire_out) = true];
611+
MessageType_UnLockDevice = 10030 [(bitcoin_only) = true, (wire_in) = true];
612+
MessageType_UnLockDeviceResponse = 10031 [(bitcoin_only) = true, (wire_out) = true];
613+
}

core/embed/extmod/modtrezorconfig/modtrezorconfig.c

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,58 +110,74 @@ STATIC mp_obj_t mod_trezorconfig_is_initialized(void) {
110110
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_is_initialized_obj,
111111
mod_trezorconfig_is_initialized);
112112

113-
/// def unlock(pin: str, ext_salt: bytes | None) -> bool:
113+
/// def unlock(pin: str, ext_salt: bytes | None, pin_use_type: int = 0)
114+
/// -> tuple[bool, int]:
114115
/// """
115116
/// Attempts to unlock the storage with the given PIN and external salt.
116117
/// Returns True on success, False on failure.
117118
/// """
118-
STATIC mp_obj_t mod_trezorconfig_unlock(mp_obj_t pin, mp_obj_t ext_salt) {
119+
STATIC mp_obj_t mod_trezorconfig_unlock(size_t n_args, const mp_obj_t *args) {
119120
mp_buffer_info_t pin_b = {0};
120-
mp_get_buffer_raise(pin, &pin_b, MP_BUFFER_READ);
121+
mp_get_buffer_raise(args[0], &pin_b, MP_BUFFER_READ);
121122

122123
mp_buffer_info_t ext_salt_b = {0};
123124
ext_salt_b.buf = NULL;
124-
if (ext_salt != mp_const_none) {
125-
mp_get_buffer_raise(ext_salt, &ext_salt_b, MP_BUFFER_READ);
125+
if (n_args > 1 && args[1] != mp_const_none) {
126+
mp_get_buffer_raise(args[1], &ext_salt_b, MP_BUFFER_READ);
126127
if (ext_salt_b.len != EXTERNAL_SALT_SIZE)
127128
mp_raise_msg(&mp_type_ValueError, "Invalid length of external salt.");
128129
}
129130

131+
pin_type_t pin_use_type = PIN_TYPE_USER;
132+
133+
if (n_args > 2) {
134+
pin_use_type = mp_obj_get_int(args[2]);
135+
}
136+
130137
// display_clear();
131138
// display_loader_ex(0, false, 0, 0xFFFF, 0x0000, NULL, 0, 0);
132139
secbool ret = secfalse;
133140

134141
// verify se pin first when not in emulator
135-
ret = se_verifyPin(pin_b.buf);
142+
ret = se_verifyPin(pin_b.buf, pin_use_type);
136143
if (ret != sectrue) {
137144
if (!pin_state.pin_unlocked_initialized) {
138145
pin_state.pin_unlocked = false;
139146
pin_state.pin_unlocked_initialized = true;
140147
}
141-
return mp_const_false;
148+
mp_obj_t tuple[2] = {mp_const_false, mp_obj_new_int(0)};
149+
return mp_obj_new_tuple(2, tuple);
142150
}
143151

152+
pin_result_t pin_type = se_get_pin_result_type();
153+
144154
// fpsensor_data_init();
145155
fpsensor_data_init_start();
146156
pin_state.pin_unlocked = true;
147157
pin_state.pin_unlocked_initialized = true;
148158
pin_state.fp_unlocked = true;
149159
pin_state.fp_unlocked_initialized = true;
150-
return mp_const_true;
160+
161+
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));
162+
tuple->items[0] = mp_const_true;
163+
tuple->items[1] = mp_obj_new_int(pin_type);
164+
return MP_OBJ_FROM_PTR(tuple);
151165
}
152-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_unlock_obj,
153-
mod_trezorconfig_unlock);
166+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorconfig_unlock_obj, 2, 3,
167+
mod_trezorconfig_unlock);
154168

155-
/// def check_pin(pin: str, ext_salt: bytes | None) -> bool:
169+
/// def check_pin(pin: str, ext_salt: bytes | None, pin_use_type: int = 0) ->
170+
/// bool:
156171
/// """
157172
/// Check the given PIN with the given external salt.
158173
/// Returns True on success, False on failure.
159174
/// """
160-
STATIC mp_obj_t mod_trezorconfig_check_pin(mp_obj_t pin, mp_obj_t ext_salt) {
161-
return mod_trezorconfig_unlock(pin, ext_salt);
175+
STATIC mp_obj_t mod_trezorconfig_check_pin(size_t n_args,
176+
const mp_obj_t *args) {
177+
return mod_trezorconfig_unlock(n_args, args);
162178
}
163-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_check_pin_obj,
164-
mod_trezorconfig_check_pin);
179+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorconfig_check_pin_obj, 2, 3,
180+
mod_trezorconfig_check_pin);
165181

166182
/// def lock() -> None:
167183
/// """
@@ -578,6 +594,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorconfig_wipe_obj,
578594
mod_trezorconfig_wipe);
579595

580596
#ifndef TREZOR_EMULATOR
597+
/// def se_import_mnemonic(mnemonic: bytes) -> bool:
598+
/// """
599+
/// Import mnemonic to SE.
600+
/// """
581601
STATIC mp_obj_t mod_trezorconfig_se_import_mnemonic(mp_obj_t mnemonic) {
582602
mp_buffer_info_t mnemo = {0};
583603
mp_get_buffer_raise(mnemonic, &mnemo, MP_BUFFER_READ);
@@ -592,6 +612,35 @@ STATIC mp_obj_t mod_trezorconfig_se_import_mnemonic(mp_obj_t mnemonic) {
592612
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorconfig_se_import_mnemonic_obj,
593613
mod_trezorconfig_se_import_mnemonic);
594614

615+
/// def se_import_slip39(mnemonic: bytes, backup_type: int, identifier: int |
616+
/// None, iteration_exponent: int | None) -> bool:
617+
/// """
618+
/// Import slip39 to SE.
619+
/// """
620+
STATIC mp_obj_t mod_trezorconfig_se_import_slip39(size_t n_args,
621+
const mp_obj_t *args) {
622+
mp_buffer_info_t master_secret_info = {0};
623+
mp_get_buffer_raise(args[0], &master_secret_info, MP_BUFFER_READ);
624+
625+
uint8_t backup_type = trezor_obj_get_uint8(args[1]);
626+
uint16_t identifier = 0;
627+
if (args[2] != mp_const_none) {
628+
identifier = trezor_obj_get_uint(args[2]);
629+
}
630+
uint8_t iteration_exponent = trezor_obj_get_uint8(args[3]);
631+
632+
if (sectrue != se_import_slip39(master_secret_info.buf,
633+
master_secret_info.len, backup_type,
634+
identifier, iteration_exponent)) {
635+
return mp_const_false;
636+
}
637+
return mp_const_true;
638+
}
639+
640+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
641+
mod_trezorconfig_se_import_slip39_obj, 4, 4,
642+
mod_trezorconfig_se_import_slip39);
643+
595644
/// def se_export_mnemonic() -> bytes:
596645
/// """
597646
/// Export mnemonic from SE.
@@ -777,6 +826,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorconfig_globals_table[] = {
777826
#ifndef TREZOR_EMULATOR
778827
{MP_ROM_QSTR(MP_QSTR_se_import_mnemonic),
779828
MP_ROM_PTR(&mod_trezorconfig_se_import_mnemonic_obj)},
829+
{MP_ROM_QSTR(MP_QSTR_se_import_slip39),
830+
MP_ROM_PTR(&mod_trezorconfig_se_import_slip39_obj)},
780831
{MP_ROM_QSTR(MP_QSTR_se_export_mnemonic),
781832
MP_ROM_PTR(&mod_trezorconfig_se_export_mnemonic_obj)},
782833
{MP_ROM_QSTR(MP_QSTR_get_serial),

core/embed/extmod/modtrezorcrypto/modtrezorcrypto-bip39.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip39_complete_word(mp_obj_t prefix) {
3535
if (pfx.len == 0) {
3636
return mp_const_none;
3737
}
38-
const char *word = mnemonic_complete_word(pfx.buf, pfx.len);
38+
const char *word = mnemonic_complete_word(pfx.buf, pfx.len, false);
3939
if (word) {
4040
return mp_obj_new_str(word, strlen(word));
4141
} else {
@@ -54,7 +54,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_complete_word_obj,
5454
STATIC mp_obj_t mod_trezorcrypto_bip39_word_completion_mask(mp_obj_t prefix) {
5555
mp_buffer_info_t pfx = {0};
5656
mp_get_buffer_raise(prefix, &pfx, MP_BUFFER_READ);
57-
return mp_obj_new_int(mnemonic_word_completion_mask(pfx.buf, pfx.len));
57+
return mp_obj_new_int(mnemonic_word_completion_mask(pfx.buf, pfx.len, false));
5858
}
5959
STATIC MP_DEFINE_CONST_FUN_OBJ_1(
6060
mod_trezorcrypto_bip39_word_completion_mask_obj,

0 commit comments

Comments
 (0)