1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00

extract CHECK_INITIALIZED and CHECK_NOT_INITIALIZED macros

This commit is contained in:
Pavol Rusnak 2016-11-22 20:57:45 +01:00
parent 27a4e41707
commit 0ef70164a5
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

View File

@ -53,10 +53,23 @@
static uint8_t msg_resp[MSG_OUT_SIZE] __attribute__ ((aligned)); static uint8_t msg_resp[MSG_OUT_SIZE] __attribute__ ((aligned));
#define RESP_INIT(TYPE) TYPE *resp = (TYPE *) (void *) msg_resp; \ #define RESP_INIT(TYPE) \
TYPE *resp = (TYPE *) (void *) msg_resp; \
_Static_assert(sizeof(msg_resp) >= sizeof(TYPE), #TYPE " is too large"); \ _Static_assert(sizeof(msg_resp) >= sizeof(TYPE), #TYPE " is too large"); \
memset(resp, 0, sizeof(TYPE)); memset(resp, 0, sizeof(TYPE));
#define CHECK_INITIALIZED \
if (!storage_isInitialized()) { \
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized"); \
return; \
}
#define CHECK_NOT_INITIALIZED \
if (storage_isInitialized()) { \
fsm_sendFailure(FailureType_Failure_UnexpectedMessage, "Device is already initialized. Use Wipe first."); \
return; \
}
void fsm_sendSuccess(const char *text) void fsm_sendSuccess(const char *text)
{ {
RESP_INIT(Success); RESP_INIT(Success);
@ -285,10 +298,7 @@ void fsm_msgGetPublicKey(GetPublicKey *msg)
{ {
RESP_INIT(PublicKey); RESP_INIT(PublicKey);
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (!protectPin(true)) { if (!protectPin(true)) {
layoutHome(); layoutHome();
@ -345,10 +355,7 @@ void fsm_msgGetPublicKey(GetPublicKey *msg)
void fsm_msgLoadDevice(LoadDevice *msg) void fsm_msgLoadDevice(LoadDevice *msg)
{ {
if (storage_isInitialized()) { CHECK_NOT_INITIALIZED
fsm_sendFailure(FailureType_Failure_UnexpectedMessage, "Device is already initialized. Use Wipe first.");
return;
}
layoutDialogSwipe(&bmp_icon_question, "Cancel", "I take the risk", NULL, "Loading private seed", "is not recommended.", "Continue only if you", "know what you are", "doing!", NULL); layoutDialogSwipe(&bmp_icon_question, "Cancel", "I take the risk", NULL, "Loading private seed", "is not recommended.", "Continue only if you", "know what you are", "doing!", NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) { if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
@ -373,10 +380,7 @@ void fsm_msgLoadDevice(LoadDevice *msg)
void fsm_msgResetDevice(ResetDevice *msg) void fsm_msgResetDevice(ResetDevice *msg)
{ {
if (storage_isInitialized()) { CHECK_NOT_INITIALIZED
fsm_sendFailure(FailureType_Failure_UnexpectedMessage, "Device is already initialized. Use Wipe first.");
return;
}
reset_init( reset_init(
msg->has_display_random && msg->display_random, msg->has_display_random && msg->display_random,
@ -391,10 +395,7 @@ void fsm_msgResetDevice(ResetDevice *msg)
void fsm_msgSignTx(SignTx *msg) void fsm_msgSignTx(SignTx *msg)
{ {
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (msg->inputs_count < 1) { if (msg->inputs_count < 1) {
fsm_sendFailure(FailureType_Failure_Other, "Transaction must have at least one input"); fsm_sendFailure(FailureType_Failure_Other, "Transaction must have at least one input");
@ -441,10 +442,7 @@ void fsm_msgCancel(Cancel *msg)
void fsm_msgEthereumSignTx(EthereumSignTx *msg) void fsm_msgEthereumSignTx(EthereumSignTx *msg)
{ {
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (!protectPin(true)) { if (!protectPin(true)) {
layoutHome(); layoutHome();
@ -464,10 +462,8 @@ void fsm_msgEthereumTxAck(EthereumTxAck *msg)
void fsm_msgCipherKeyValue(CipherKeyValue *msg) void fsm_msgCipherKeyValue(CipherKeyValue *msg)
{ {
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (!msg->has_key) { if (!msg->has_key) {
fsm_sendFailure(FailureType_Failure_SyntaxError, "No key provided"); fsm_sendFailure(FailureType_Failure_SyntaxError, "No key provided");
return; return;
@ -593,10 +589,7 @@ void fsm_msgGetAddress(GetAddress *msg)
{ {
RESP_INIT(Address); RESP_INIT(Address);
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (!protectPin(true)) { if (!protectPin(true)) {
layoutHome(); layoutHome();
@ -658,10 +651,7 @@ void fsm_msgEthereumGetAddress(EthereumGetAddress *msg)
{ {
RESP_INIT(EthereumAddress); RESP_INIT(EthereumAddress);
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (!protectPin(true)) { if (!protectPin(true)) {
layoutHome(); layoutHome();
@ -708,10 +698,7 @@ void fsm_msgSignMessage(SignMessage *msg)
{ {
RESP_INIT(MessageSignature); RESP_INIT(MessageSignature);
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
layoutSignMessage(msg->message.bytes, msg->message.size); layoutSignMessage(msg->message.bytes, msg->message.size);
if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) { if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
@ -786,10 +773,7 @@ void fsm_msgSignIdentity(SignIdentity *msg)
{ {
RESP_INIT(SignedIdentity); RESP_INIT(SignedIdentity);
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
layoutSignIdentity(&(msg->identity), msg->has_challenge_visual ? msg->challenge_visual : 0); layoutSignIdentity(&(msg->identity), msg->has_challenge_visual ? msg->challenge_visual : 0);
if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) { if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
@ -868,10 +852,7 @@ void fsm_msgGetECDHSessionKey(GetECDHSessionKey *msg)
{ {
RESP_INIT(ECDHSessionKey); RESP_INIT(ECDHSessionKey);
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
layoutDecryptIdentity(&msg->identity); layoutDecryptIdentity(&msg->identity);
if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) { if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
@ -921,10 +902,8 @@ void fsm_msgGetECDHSessionKey(GetECDHSessionKey *msg)
/* ECIES disabled /* ECIES disabled
void fsm_msgEncryptMessage(EncryptMessage *msg) void fsm_msgEncryptMessage(EncryptMessage *msg)
{ {
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (!msg->has_pubkey) { if (!msg->has_pubkey) {
fsm_sendFailure(FailureType_Failure_SyntaxError, "No public key provided"); fsm_sendFailure(FailureType_Failure_SyntaxError, "No public key provided");
return; return;
@ -975,10 +954,8 @@ void fsm_msgEncryptMessage(EncryptMessage *msg)
void fsm_msgDecryptMessage(DecryptMessage *msg) void fsm_msgDecryptMessage(DecryptMessage *msg)
{ {
if (!storage_isInitialized()) { CHECK_INITIALIZED
fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
return;
}
if (!msg->has_nonce) { if (!msg->has_nonce) {
fsm_sendFailure(FailureType_Failure_SyntaxError, "No nonce provided"); fsm_sendFailure(FailureType_Failure_SyntaxError, "No nonce provided");
return; return;
@ -1042,10 +1019,8 @@ void fsm_msgEstimateTxSize(EstimateTxSize *msg)
void fsm_msgRecoveryDevice(RecoveryDevice *msg) void fsm_msgRecoveryDevice(RecoveryDevice *msg)
{ {
if (storage_isInitialized()) { CHECK_NOT_INITIALIZED
fsm_sendFailure(FailureType_Failure_UnexpectedMessage, "Device is already initialized. Use Wipe first.");
return;
}
recovery_init( recovery_init(
msg->has_word_count ? msg->word_count : 12, msg->has_word_count ? msg->word_count : 12,
msg->has_passphrase_protection && msg->passphrase_protection, msg->has_passphrase_protection && msg->passphrase_protection,