diff --git a/tiny-firmware/firmware/protect.c b/tiny-firmware/firmware/protect.c index 7a0df41..0a20f1a 100644 --- a/tiny-firmware/firmware/protect.c +++ b/tiny-firmware/firmware/protect.c @@ -267,6 +267,11 @@ ErrCode_t protectChangePinEx(ErrCode_t (*funcRequestPin)(PinMatrixRequestType, c strlcpy(pin_compare, pin, sizeof(pin_compare)); memset(pin, 0, sizeof(pin)); err = funcRequestPin(PinMatrixRequestType_PinMatrixRequestType_NewSecond, _("Please re-enter new PIN:"), pin); + if (err != ErrOk) { + memset(pin_compare, 0, sizeof(pin_compare)); + memset(pin, 0, sizeof(pin)); + return err; + } { char empty_pin[sizeof(pin)] = {0}; if (!memcmp(pin, empty_pin, sizeof(pin))) { diff --git a/tiny-firmware/tests/test_fsm.c b/tiny-firmware/tests/test_fsm.c index bd6ddda..5a915e8 100644 --- a/tiny-firmware/tests/test_fsm.c +++ b/tiny-firmware/tests/test_fsm.c @@ -416,14 +416,33 @@ START_TEST(test_msgChangePinSecondRejected) } END_TEST -START_TEST(test_msgChangePinShouldReturnCanceledAccordingToPinReader) +START_TEST(test_msgChangePinShouldReturnCanceledAccordingToPinReaderNewPin) { ChangePin msg = ChangePin_init_zero; storage_wipe(); - - // Pin mismatch - ck_assert_int_eq(msgChangePinImpl(&msg, &pin_reader_canceled), ErrPinCancelled); ck_assert_int_eq(storage_hasPin(), false); + ck_assert_int_eq(msgChangePinImpl(&msg, &pin_reader_new_canceled), ErrPinCancelled); + ck_assert_int_eq(storage_hasPin(), false); +} +END_TEST + +START_TEST(test_msgChangePinShouldReturnCanceledAccordingToPinReaderButPreserveAPreviuosOne) +{ + ErrCode_t (*pin_readers[2])(PinMatrixRequestType, const char*, char*) = { + &pin_reader_new_canceled, &pin_reader_confirm_canceled}; + for (int i = 0; i < sizeof(pin_readers) / sizeof(*pin_readers); ++i) { + ChangePin msg = ChangePin_init_zero; + storage_wipe(); + ck_assert_int_eq(storage_hasPin(), false); + + ck_assert_int_eq(msgChangePinImpl(&msg, &pin_reader_ok), ErrOk); + ck_assert_int_eq(storage_hasPin(), true); + ck_assert_str_eq(storage_getPin(), TEST_PIN1); + + ck_assert_int_eq(msgChangePinImpl(&msg, pin_readers[i]), ErrPinCancelled); + ck_assert_int_eq(storage_hasPin(), true); + ck_assert_str_eq(storage_getPin(), TEST_PIN1); + } } END_TEST @@ -543,7 +562,8 @@ TCase* add_fsm_tests(TCase* tc) tcase_add_test(tc, test_msgEntropyAckChgMixerNotInternal); tcase_add_test(tc, test_msgChangePinSuccess); tcase_add_test(tc, test_msgChangePinSecondRejected); - tcase_add_test(tc, test_msgChangePinShouldReturnCanceledAccordingToPinReader); + tcase_add_test(tc, test_msgChangePinShouldReturnCanceledAccordingToPinReaderNewPin); + tcase_add_test(tc, test_msgChangePinShouldReturnCanceledAccordingToPinReaderButPreserveAPreviuosOne); tcase_add_test(tc, test_msgChangePinEditSuccess); tcase_add_test(tc, test_msgChangePinRemoveSuccess); tcase_add_test(tc, test_isSha256DigestHex); diff --git a/tiny-firmware/tests/test_pin.c b/tiny-firmware/tests/test_pin.c index f7e012e..82fd36f 100644 --- a/tiny-firmware/tests/test_pin.c +++ b/tiny-firmware/tests/test_pin.c @@ -47,14 +47,31 @@ ErrCode_t pin_reader_wrong(PinMatrixRequestType pinReqType, const char* text, ch return ErrPinMismatch; } -ErrCode_t pin_reader_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out) +ErrCode_t pin_reader_new_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out) { (void)text; - (void)pin_out; switch (pinReqType) { case PinMatrixRequestType_PinMatrixRequestType_NewFirst: + return ErrPinCancelled; + case PinMatrixRequestType_PinMatrixRequestType_Current: + case PinMatrixRequestType_PinMatrixRequestType_NewSecond: + strcpy(pin_out, TEST_PIN1); + return ErrOk; + default: + return ErrInvalidArg; + } +} + +ErrCode_t pin_reader_confirm_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out) +{ + (void)text; + switch (pinReqType) { case PinMatrixRequestType_PinMatrixRequestType_NewSecond: return ErrPinCancelled; + case PinMatrixRequestType_PinMatrixRequestType_Current: + case PinMatrixRequestType_PinMatrixRequestType_NewFirst: + strcpy(pin_out, TEST_PIN1); + return ErrOk; default: return ErrInvalidArg; } diff --git a/tiny-firmware/tests/test_pin.h b/tiny-firmware/tests/test_pin.h index dabc10e..7dcc1d6 100644 --- a/tiny-firmware/tests/test_pin.h +++ b/tiny-firmware/tests/test_pin.h @@ -20,4 +20,6 @@ ErrCode_t pin_reader_alt(PinMatrixRequestType pinReqType, const char* text, char ErrCode_t pin_reader_wrong(PinMatrixRequestType pinReqType, const char* text, char* pin_out); -ErrCode_t pin_reader_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out); +ErrCode_t pin_reader_new_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out); + +ErrCode_t pin_reader_confirm_canceled(PinMatrixRequestType pinReqType, const char* text, char* pin_out);