From 120cfc148f0cadc23819fb20444b8de24e1c625a Mon Sep 17 00:00:00 2001 From: Jochen Hoenicke Date: Sun, 12 Jun 2016 23:39:28 +0200 Subject: [PATCH] new message SetU2FCounter --- firmware/fsm.c | 6 ++++++ firmware/fsm.h | 1 + firmware/messages.c | 1 + firmware/protob/messages.pb.c | 7 ++++++- firmware/protob/messages.pb.h | 11 +++++++++++ firmware/storage.c | 7 +++++++ firmware/storage.h | 1 + vendor/trezor-common | 2 +- 8 files changed, 34 insertions(+), 2 deletions(-) diff --git a/firmware/fsm.c b/firmware/fsm.c index b7c6e5025a..2ca2f514ec 100644 --- a/firmware/fsm.c +++ b/firmware/fsm.c @@ -924,6 +924,12 @@ void fsm_msgWordAck(WordAck *msg) recovery_word(msg->word); } +void fsm_msgSetU2FCounter(SetU2FCounter *msg) +{ + storage_setU2FCounter(msg->u2f_counter); + fsm_sendSuccess("U2F counter set"); +} + #if DEBUG_LINK void fsm_msgDebugLinkGetState(DebugLinkGetState *msg) diff --git a/firmware/fsm.h b/firmware/fsm.h index 5b6eef971e..a106b39a96 100644 --- a/firmware/fsm.h +++ b/firmware/fsm.h @@ -57,6 +57,7 @@ void fsm_msgDecryptMessage(DecryptMessage *msg); void fsm_msgEstimateTxSize(EstimateTxSize *msg); void fsm_msgRecoveryDevice(RecoveryDevice *msg); void fsm_msgWordAck(WordAck *msg); +void fsm_msgSetU2FCounter(SetU2FCounter *msg); // debug message functions #if DEBUG_LINK diff --git a/firmware/messages.c b/firmware/messages.c index 57c311151d..a12911a84c 100644 --- a/firmware/messages.c +++ b/firmware/messages.c @@ -71,6 +71,7 @@ static const struct MessagesMap_t MessagesMap[] = { {'n', 'i', MessageType_MessageType_EstimateTxSize, EstimateTxSize_fields, (void (*)(void *))fsm_msgEstimateTxSize}, {'n', 'i', MessageType_MessageType_RecoveryDevice, RecoveryDevice_fields, (void (*)(void *))fsm_msgRecoveryDevice}, {'n', 'i', MessageType_MessageType_WordAck, WordAck_fields, (void (*)(void *))fsm_msgWordAck}, + {'n', 'i', MessageType_MessageType_SetU2FCounter, SetU2FCounter_fields, (void (*)(void *))fsm_msgSetU2FCounter}, // out messages {'n', 'o', MessageType_MessageType_Success, Success_fields, 0}, {'n', 'o', MessageType_MessageType_Failure, Failure_fields, 0}, diff --git a/firmware/protob/messages.pb.c b/firmware/protob/messages.pb.c index 4abfcc07f7..c72e3073c3 100644 --- a/firmware/protob/messages.pb.c +++ b/firmware/protob/messages.pb.c @@ -380,6 +380,11 @@ const pb_field_t ECDHSessionKey_fields[2] = { PB_LAST_FIELD }; +const pb_field_t SetU2FCounter_fields[2] = { + PB_FIELD2( 1, UINT32 , OPTIONAL, STATIC , FIRST, SetU2FCounter, u2f_counter, u2f_counter, 0), + PB_LAST_FIELD +}; + const pb_field_t FirmwareErase_fields[1] = { PB_LAST_FIELD }; @@ -456,7 +461,7 @@ const pb_field_t DebugLinkFlashErase_fields[2] = { * numbers or field sizes that are larger than what can fit in 8 or 16 bit * field descriptors. */ -STATIC_ASSERT((pb_membersize(Features, coins[0]) < 65536 && pb_membersize(PublicKey, node) < 65536 && pb_membersize(GetAddress, multisig) < 65536 && pb_membersize(LoadDevice, node) < 65536 && pb_membersize(SimpleSignTx, inputs[0]) < 65536 && pb_membersize(SimpleSignTx, outputs[0]) < 65536 && pb_membersize(SimpleSignTx, transactions[0]) < 65536 && pb_membersize(TxRequest, details) < 65536 && pb_membersize(TxRequest, serialized) < 65536 && pb_membersize(TxAck, tx) < 65536 && pb_membersize(SignIdentity, identity) < 65536 && pb_membersize(GetECDHSessionKey, identity) < 65536 && pb_membersize(DebugLinkState, node) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_Initialize_GetFeatures_Features_ClearSession_ApplySettings_ChangePin_Ping_Success_Failure_ButtonRequest_ButtonAck_PinMatrixRequest_PinMatrixAck_Cancel_PassphraseRequest_PassphraseAck_GetEntropy_Entropy_GetPublicKey_PublicKey_GetAddress_EthereumGetAddress_Address_EthereumAddress_WipeDevice_LoadDevice_ResetDevice_EntropyRequest_EntropyAck_RecoveryDevice_WordRequest_WordAck_SignMessage_VerifyMessage_MessageSignature_EncryptMessage_EncryptedMessage_DecryptMessage_DecryptedMessage_CipherKeyValue_CipheredKeyValue_EstimateTxSize_TxSize_SignTx_SimpleSignTx_TxRequest_TxAck_EthereumSignTx_EthereumTxRequest_EthereumTxAck_SignIdentity_SignedIdentity_GetECDHSessionKey_ECDHSessionKey_FirmwareErase_FirmwareUpload_DebugLinkDecision_DebugLinkGetState_DebugLinkState_DebugLinkStop_DebugLinkLog_DebugLinkMemoryRead_DebugLinkMemory_DebugLinkMemoryWrite_DebugLinkFlashErase) +STATIC_ASSERT((pb_membersize(Features, coins[0]) < 65536 && pb_membersize(PublicKey, node) < 65536 && pb_membersize(GetAddress, multisig) < 65536 && pb_membersize(LoadDevice, node) < 65536 && pb_membersize(SimpleSignTx, inputs[0]) < 65536 && pb_membersize(SimpleSignTx, outputs[0]) < 65536 && pb_membersize(SimpleSignTx, transactions[0]) < 65536 && pb_membersize(TxRequest, details) < 65536 && pb_membersize(TxRequest, serialized) < 65536 && pb_membersize(TxAck, tx) < 65536 && pb_membersize(SignIdentity, identity) < 65536 && pb_membersize(GetECDHSessionKey, identity) < 65536 && pb_membersize(DebugLinkState, node) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_Initialize_GetFeatures_Features_ClearSession_ApplySettings_ChangePin_Ping_Success_Failure_ButtonRequest_ButtonAck_PinMatrixRequest_PinMatrixAck_Cancel_PassphraseRequest_PassphraseAck_GetEntropy_Entropy_GetPublicKey_PublicKey_GetAddress_EthereumGetAddress_Address_EthereumAddress_WipeDevice_LoadDevice_ResetDevice_EntropyRequest_EntropyAck_RecoveryDevice_WordRequest_WordAck_SignMessage_VerifyMessage_MessageSignature_EncryptMessage_EncryptedMessage_DecryptMessage_DecryptedMessage_CipherKeyValue_CipheredKeyValue_EstimateTxSize_TxSize_SignTx_SimpleSignTx_TxRequest_TxAck_EthereumSignTx_EthereumTxRequest_EthereumTxAck_SignIdentity_SignedIdentity_GetECDHSessionKey_ECDHSessionKey_SetU2FCounter_FirmwareErase_FirmwareUpload_DebugLinkDecision_DebugLinkGetState_DebugLinkState_DebugLinkStop_DebugLinkLog_DebugLinkMemoryRead_DebugLinkMemory_DebugLinkMemoryWrite_DebugLinkFlashErase) #endif #if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT) diff --git a/firmware/protob/messages.pb.h b/firmware/protob/messages.pb.h index d0f6eb68c5..2b814ba8fe 100644 --- a/firmware/protob/messages.pb.h +++ b/firmware/protob/messages.pb.h @@ -68,6 +68,7 @@ typedef enum _MessageType { MessageType_MessageType_EthereumTxAck = 60, MessageType_MessageType_GetECDHSessionKey = 61, MessageType_MessageType_ECDHSessionKey = 62, + MessageType_MessageType_SetU2FCounter = 63, MessageType_MessageType_DebugLinkDecision = 100, MessageType_MessageType_DebugLinkGetState = 101, MessageType_MessageType_DebugLinkState = 102, @@ -686,6 +687,11 @@ typedef struct _ResetDevice { char label[33]; } ResetDevice; +typedef struct _SetU2FCounter { + bool has_u2f_counter; + uint32_t u2f_counter; +} SetU2FCounter; + typedef struct { size_t size; uint8_t bytes[256]; @@ -881,6 +887,7 @@ extern const uint32_t SimpleSignTx_lock_time_default; #define SignedIdentity_init_default {false, "", false, {0, {0}}, false, {0, {0}}} #define GetECDHSessionKey_init_default {false, IdentityType_init_default, false, {0, {0}}, false, ""} #define ECDHSessionKey_init_default {false, {0, {0}}} +#define SetU2FCounter_init_default {false, 0} #define FirmwareErase_init_default {0} #define FirmwareUpload_init_default {{0, {0}}} #define DebugLinkDecision_init_default {0} @@ -946,6 +953,7 @@ extern const uint32_t SimpleSignTx_lock_time_default; #define SignedIdentity_init_zero {false, "", false, {0, {0}}, false, {0, {0}}} #define GetECDHSessionKey_init_zero {false, IdentityType_init_zero, false, {0, {0}}, false, ""} #define ECDHSessionKey_init_zero {false, {0, {0}}} +#define SetU2FCounter_init_zero {false, 0} #define FirmwareErase_init_zero {0} #define FirmwareUpload_init_zero {{0, {0}}} #define DebugLinkDecision_init_zero {0} @@ -1093,6 +1101,7 @@ extern const uint32_t SimpleSignTx_lock_time_default; #define ResetDevice_pin_protection_tag 4 #define ResetDevice_language_tag 5 #define ResetDevice_label_tag 6 +#define SetU2FCounter_u2f_counter_tag 1 #define SignIdentity_identity_tag 1 #define SignIdentity_challenge_hidden_tag 2 #define SignIdentity_challenge_visual_tag 3 @@ -1181,6 +1190,7 @@ extern const pb_field_t SignIdentity_fields[5]; extern const pb_field_t SignedIdentity_fields[4]; extern const pb_field_t GetECDHSessionKey_fields[4]; extern const pb_field_t ECDHSessionKey_fields[2]; +extern const pb_field_t SetU2FCounter_fields[2]; extern const pb_field_t FirmwareErase_fields[1]; extern const pb_field_t FirmwareUpload_fields[2]; extern const pb_field_t DebugLinkDecision_fields[2]; @@ -1248,6 +1258,7 @@ extern const pb_field_t DebugLinkFlashErase_fields[2]; #define SignedIdentity_size 140 #define GetECDHSessionKey_size (107 + IdentityType_size) #define ECDHSessionKey_size 67 +#define SetU2FCounter_size 6 #define FirmwareErase_size 0 #define FirmwareUpload_size 2 #define DebugLinkDecision_size 2 diff --git a/firmware/storage.c b/firmware/storage.c index f1a21196aa..519443ac38 100644 --- a/firmware/storage.c +++ b/firmware/storage.c @@ -559,3 +559,10 @@ uint32_t storage_nextU2FCounter(void) storage_check_flash_errors(); return storage.u2f_counter + storage_u2f_offset; } + +void storage_setU2FCounter(uint32_t u2fcounter) +{ + storage.has_u2f_counter = true; + storage.u2f_counter = u2fcounter - storage_u2f_offset; + storage_commit(); +} diff --git a/firmware/storage.h b/firmware/storage.h index ed5a674ab3..b3680a2bb4 100644 --- a/firmware/storage.h +++ b/firmware/storage.h @@ -62,6 +62,7 @@ bool storage_increasePinFails(uint32_t *pinfailptr); uint32_t *storage_getPinFailsPtr(void); uint32_t storage_nextU2FCounter(void); +void storage_setU2FCounter(uint32_t u2fcounter); bool storage_isInitialized(void); diff --git a/vendor/trezor-common b/vendor/trezor-common index 70d8ac8f85..203d95b913 160000 --- a/vendor/trezor-common +++ b/vendor/trezor-common @@ -1 +1 @@ -Subproject commit 70d8ac8f85f16a0a16019749402f26dcc635d841 +Subproject commit 203d95b913903debbaab3333ea2416a7c37e0b95