Skip to content

Commit 3657f6e

Browse files
authored
feat: support attach to pin (#357)
* support attach to pin
1 parent 564500f commit 3657f6e

Some content is hidden

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

48 files changed

+2527
-242
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: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ message Initialize {
3939
optional bytes session_id = 1; // assumed device session id; Trezor clears caches if it is different or empty
4040
optional bool _skip_passphrase = 2 [deprecated=true]; // removed as part of passphrase redesign
4141
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;
4244
}
4345

4446
/**
@@ -129,6 +131,7 @@ message Features {
129131
Capability_Shamir = 15 [(bitcoin_only) = true];
130132
Capability_ShamirGroups = 16 [(bitcoin_only) = true];
131133
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
132135
}
133136
optional BackupType backup_type = 31; // type of device backup (BIP-39 / SLIP-39 basic / SLIP-39 advanced)
134137
optional bool sd_card_present = 32; // is SD card present
@@ -189,6 +192,8 @@ message Features {
189192
optional OneKeySEState onekey_se02_state = 622;
190193
optional OneKeySEState onekey_se03_state = 623;
191194
optional OneKeySEState onekey_se04_state = 624;
195+
optional bool attach_to_pin_user = 625;
196+
optional bool unlocked_attach_pin = 626;
192197
}
193198

194199
/**
@@ -808,5 +813,44 @@ message UnlockPath {
808813
* @next GetAddress
809814
*/
810815
message UnlockedPathRequest {
811-
optional bytes mac = 1; // authentication code for future UnlockPath calls
816+
optional bytes mac = 1;
812817
}
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.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,(wire_tiny) = 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: 31 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
/// """

0 commit comments

Comments
 (0)