mirror of
https://github.com/LedgerHQ/openpgp-card-app
synced 2024-11-09 07:10:30 +00:00
Minor code optimization to ease readability
- Remove useless parameter for 'ui_info' - Remove useless functions 'gpg_init_ux' and 'io_seproxyhal_display' - Add constant definition - Unique function 'app_reset' instead of duplicate code
This commit is contained in:
parent
3847b0636d
commit
d97b0ea3b4
@ -24,7 +24,7 @@
|
||||
/* -- A Kind of Magic -- */
|
||||
/* ----------------------*/
|
||||
|
||||
const unsigned char C_MAGIC[8] = {'G', 'P', 'G', 'C', 'A', 'R', 'D', '3'};
|
||||
const unsigned char C_MAGIC[MAGIC_LENGTH] = {'G', 'P', 'G', 'C', 'A', 'R', 'D', '3'};
|
||||
/* ----------------------*/
|
||||
/* --ECC OID -- */
|
||||
/* ----------------------*/
|
||||
@ -374,9 +374,9 @@ const unsigned char C_sha256_PW2[] = {
|
||||
void gpg_init() {
|
||||
explicit_bzero(&G_gpg_vstate, sizeof(gpg_v_state_t));
|
||||
// first init ?
|
||||
if (memcmp((void *) (N_gpg_pstate->magic), (void *) C_MAGIC, sizeof(C_MAGIC)) != 0) {
|
||||
if (memcmp((void *) (N_gpg_pstate->magic), (void *) C_MAGIC, MAGIC_LENGTH) != 0) {
|
||||
gpg_install(STATE_ACTIVATE);
|
||||
nvm_write((void *) (N_gpg_pstate->magic), (void *) C_MAGIC, sizeof(C_MAGIC));
|
||||
nvm_write((void *) (N_gpg_pstate->magic), (void *) C_MAGIC, MAGIC_LENGTH);
|
||||
explicit_bzero(&G_gpg_vstate, sizeof(gpg_v_state_t));
|
||||
}
|
||||
|
||||
@ -389,10 +389,6 @@ void gpg_init() {
|
||||
// seed conf
|
||||
G_gpg_vstate.seed_mode = 1;
|
||||
// ux conf
|
||||
gpg_init_ux();
|
||||
}
|
||||
|
||||
void gpg_init_ux() {
|
||||
G_gpg_vstate.ux_type = -1;
|
||||
G_gpg_vstate.ux_key = -1;
|
||||
}
|
||||
|
@ -41,6 +41,7 @@
|
||||
#define GPG_MIN_PW1_LENGTH 6
|
||||
#define GPG_MIN_PW3_LENGTH 8
|
||||
|
||||
#define MAGIC_LENGTH 8
|
||||
#define AID_LENGTH 16
|
||||
#define HISTO_LENGTH 15
|
||||
#define HISTO_OFFSET_STATE 12 // 3rd byte from last (buffer size is 15)
|
||||
@ -136,7 +137,7 @@ typedef struct gpg_key_slot_s {
|
||||
|
||||
struct gpg_nv_state_s {
|
||||
/* magic */
|
||||
unsigned char magic[8];
|
||||
unsigned char magic[MAGIC_LENGTH];
|
||||
|
||||
/* pin mode */
|
||||
unsigned char config_pin[1];
|
||||
|
25
src/gpg_ux.c
25
src/gpg_ux.c
@ -16,6 +16,8 @@
|
||||
* limitations under the License.
|
||||
*****************************************************************************/
|
||||
|
||||
#include "gpg_vars.h"
|
||||
#include "gpg_ux.h"
|
||||
#include "usbd_ccid_if.h"
|
||||
|
||||
/**
|
||||
@ -26,3 +28,26 @@ void ui_CCID_reset(void) {
|
||||
io_usb_ccid_set_card_inserted(0);
|
||||
io_usb_ccid_set_card_inserted(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exit app
|
||||
*
|
||||
*/
|
||||
void app_quit(void) {
|
||||
// exit app here
|
||||
os_sched_exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset app
|
||||
*
|
||||
*/
|
||||
void app_reset(void) {
|
||||
unsigned char magic[MAGIC_LENGTH];
|
||||
|
||||
explicit_bzero(magic, MAGIC_LENGTH);
|
||||
nvm_write((void*) (N_gpg_pstate->magic), magic, MAGIC_LENGTH);
|
||||
gpg_init();
|
||||
ui_CCID_reset();
|
||||
ui_init();
|
||||
}
|
||||
|
@ -34,6 +34,8 @@
|
||||
#define LABEL_Ed25519 "Ed25519"
|
||||
|
||||
void ui_CCID_reset(void);
|
||||
void app_quit(void);
|
||||
void app_reset(void);
|
||||
void ui_init(void);
|
||||
void ui_menu_pinconfirm_display(unsigned int value);
|
||||
void ui_menu_pinentry_display(unsigned int value);
|
||||
|
@ -47,10 +47,10 @@ unsigned int ui_pinentry_action_button(unsigned int button_mask, unsigned int bu
|
||||
* @param[in] menu_display next page display callback
|
||||
*
|
||||
*/
|
||||
void ui_info(const char *msg1, const char *msg2, const void *menu_display, unsigned int value) {
|
||||
void ui_info(const char *msg1, const char *msg2, const void *menu_display) {
|
||||
explicit_bzero(&G_gpg_vstate.ui_dogsays[0], sizeof(ux_menu_entry_t));
|
||||
G_gpg_vstate.ui_dogsays[0].callback = menu_display;
|
||||
G_gpg_vstate.ui_dogsays[0].userid = value;
|
||||
G_gpg_vstate.ui_dogsays[0].userid = 0;
|
||||
G_gpg_vstate.ui_dogsays[0].line1 = msg1;
|
||||
G_gpg_vstate.ui_dogsays[0].line2 = msg2;
|
||||
|
||||
@ -471,7 +471,7 @@ static void validate_pin() {
|
||||
sizeof(G_gpg_vstate.menu),
|
||||
" %d tries remaining",
|
||||
pin->counter);
|
||||
ui_info(WRONG_PIN, G_gpg_vstate.menu, ui_menu_main_display, 0);
|
||||
ui_info(WRONG_PIN, G_gpg_vstate.menu, ui_menu_main_display);
|
||||
} else {
|
||||
ui_menu_main_display(0);
|
||||
}
|
||||
@ -496,7 +496,7 @@ static void validate_pin() {
|
||||
sizeof(G_gpg_vstate.menu),
|
||||
" %d tries remaining",
|
||||
pin->counter);
|
||||
ui_info(WRONG_PIN, G_gpg_vstate.menu, ui_menu_main_display, 0);
|
||||
ui_info(WRONG_PIN, G_gpg_vstate.menu, ui_menu_main_display);
|
||||
return;
|
||||
}
|
||||
offset = 1 + G_gpg_vstate.work.io_buffer[0];
|
||||
@ -508,7 +508,7 @@ static void validate_pin() {
|
||||
gpg_io_discard(1);
|
||||
gpg_io_insert_u16(SW_CONDITIONS_NOT_SATISFIED);
|
||||
gpg_io_do(IO_RETURN_AFTER_TX);
|
||||
ui_info(PIN_DIFFERS, EMPTY, ui_menu_main_display, 0);
|
||||
ui_info(PIN_DIFFERS, EMPTY, ui_menu_main_display);
|
||||
} else {
|
||||
sw = gpg_pin_set(gpg_pin_get_pin(G_gpg_vstate.io_p2),
|
||||
G_gpg_vstate.work.io_buffer + offset + 1,
|
||||
@ -749,7 +749,7 @@ void ui_menu_tmpl_set_action(unsigned int value) {
|
||||
break;
|
||||
}
|
||||
if (attributes.value[0] == 0) {
|
||||
ui_info(INVALID_SELECTION, TEMPLATE_TYPE, ui_menu_template_display, 0);
|
||||
ui_info(INVALID_SELECTION, TEMPLATE_TYPE, ui_menu_template_display);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -770,9 +770,9 @@ void ui_menu_tmpl_set_action(unsigned int value) {
|
||||
if (dest != NULL) {
|
||||
nvm_write(dest, NULL, sizeof(gpg_key_t));
|
||||
nvm_write(&dest->attributes, &attributes, sizeof(attributes));
|
||||
ui_info(OK, EMPTY, ui_menu_template_display, 0);
|
||||
ui_info(OK, EMPTY, ui_menu_template_display);
|
||||
} else {
|
||||
ui_info(INVALID_SELECTION, TEMPLATE_KEY, ui_menu_template_display, 0);
|
||||
ui_info(INVALID_SELECTION, TEMPLATE_KEY, ui_menu_template_display);
|
||||
}
|
||||
}
|
||||
|
||||
@ -981,7 +981,7 @@ void ui_menu_pinmode_action(unsigned int value) {
|
||||
case 128:
|
||||
if (G_gpg_vstate.pinmode != N_gpg_pstate->config_pin[0]) {
|
||||
if (G_gpg_vstate.pinmode == PIN_MODE_TRUST) {
|
||||
ui_info(DEFAULT_MODE, NOT_ALLOWED, ui_menu_pinmode_display, 0);
|
||||
ui_info(DEFAULT_MODE, NOT_ALLOWED, ui_menu_pinmode_display);
|
||||
return;
|
||||
}
|
||||
// set new mode
|
||||
@ -999,7 +999,7 @@ void ui_menu_pinmode_action(unsigned int value) {
|
||||
break;
|
||||
}
|
||||
if ((gpg_pin_is_verified(PIN_ID_PW1) == 0) && (gpg_pin_is_verified(PIN_ID_PW2) == 0)) {
|
||||
ui_info(PIN_USER, NOT_VERIFIED, ui_menu_pinmode_display, 0);
|
||||
ui_info(PIN_USER, NOT_VERIFIED, ui_menu_pinmode_display);
|
||||
return;
|
||||
}
|
||||
G_gpg_vstate.pinmode = value;
|
||||
@ -1012,7 +1012,7 @@ void ui_menu_pinmode_action(unsigned int value) {
|
||||
break;
|
||||
}
|
||||
if (!gpg_pin_is_verified(PIN_ID_PW3)) {
|
||||
ui_info(PIN_ADMIN, NOT_VERIFIED, ui_menu_pinmode_display, 0);
|
||||
ui_info(PIN_ADMIN, NOT_VERIFIED, ui_menu_pinmode_display);
|
||||
return;
|
||||
}
|
||||
// Confirm request
|
||||
@ -1024,7 +1024,7 @@ void ui_menu_pinmode_action(unsigned int value) {
|
||||
break;
|
||||
default:
|
||||
value = 0;
|
||||
ui_info(INVALID_SELECTION, EMPTY, ui_menu_pinmode_display, 0);
|
||||
ui_info(INVALID_SELECTION, EMPTY, ui_menu_pinmode_display);
|
||||
break;
|
||||
}
|
||||
ui_menu_pinmode_display(value);
|
||||
@ -1107,7 +1107,7 @@ void ui_menu_uifmode_action(unsigned int value) {
|
||||
uif = &G_gpg_vstate.kslot->aut.UIF[0];
|
||||
break;
|
||||
default:
|
||||
ui_info(INVALID_SELECTION, EMPTY, ui_menu_uifmode_display, 0);
|
||||
ui_info(INVALID_SELECTION, EMPTY, ui_menu_uifmode_display);
|
||||
return;
|
||||
}
|
||||
if (uif[0] == 0) {
|
||||
@ -1117,7 +1117,7 @@ void ui_menu_uifmode_action(unsigned int value) {
|
||||
new_uif = 0;
|
||||
nvm_write(&uif[0], &new_uif, 1);
|
||||
} else /*if (uif[0] == 2 )*/ {
|
||||
ui_info(UIF_LOCKED, EMPTY, ui_menu_uifmode_display, 0);
|
||||
ui_info(UIF_LOCKED, EMPTY, ui_menu_uifmode_display);
|
||||
return;
|
||||
}
|
||||
ui_menu_uifmode_display(value);
|
||||
@ -1139,15 +1139,8 @@ const ux_menu_entry_t ui_menu_reset[] = {
|
||||
*/
|
||||
void ui_menu_reset_action(unsigned int value) {
|
||||
UNUSED(value);
|
||||
unsigned char magic[4];
|
||||
magic[0] = 0;
|
||||
magic[1] = 0;
|
||||
magic[2] = 0;
|
||||
magic[3] = 0;
|
||||
nvm_write((void *) (N_gpg_pstate->magic), magic, 4);
|
||||
gpg_init();
|
||||
ui_CCID_reset();
|
||||
ui_menu_main_display(0);
|
||||
|
||||
app_reset();
|
||||
}
|
||||
|
||||
/* ------------------------------- SETTINGS UX ------------------------------- */
|
||||
@ -1181,7 +1174,7 @@ const ux_menu_entry_t ui_menu_main[] = {
|
||||
{NULL, NULL, 0, NULL, "", "", 0, 0},
|
||||
{ui_menu_settings, NULL, 0, NULL, "Settings", NULL, 0, 0},
|
||||
{ui_menu_info, NULL, 0, NULL, "About", NULL, 0, 0},
|
||||
{NULL, (void *) os_sched_exit, 0, &C_icon_dashboard, "Quit app", NULL, 50, 29},
|
||||
{NULL, (void *) app_quit, 0, &C_icon_dashboard, "Quit app", NULL, 50, 29},
|
||||
UX_MENU_END};
|
||||
|
||||
/**
|
||||
@ -1240,8 +1233,4 @@ void ui_init(void) {
|
||||
UX_CALLBACK_SET_INTERVAL(1000);
|
||||
}
|
||||
|
||||
void io_seproxyhal_display(const bagl_element_t *element) {
|
||||
io_seproxyhal_display_default((bagl_element_t *) element);
|
||||
}
|
||||
|
||||
#endif // defined(HAVE_BAGL) && defined(TARGET_NANOS)
|
||||
|
@ -1193,15 +1193,8 @@ void ui_menu_reset_display(unsigned int value) {
|
||||
*/
|
||||
void ui_menu_reset_action(unsigned int value) {
|
||||
UNUSED(value);
|
||||
unsigned char magic[4];
|
||||
magic[0] = 0;
|
||||
magic[1] = 0;
|
||||
magic[2] = 0;
|
||||
magic[3] = 0;
|
||||
nvm_write((void *) (N_gpg_pstate->magic), magic, 4);
|
||||
gpg_init();
|
||||
ui_CCID_reset();
|
||||
ui_menu_main_display(0);
|
||||
|
||||
app_reset();
|
||||
}
|
||||
|
||||
/* ------------------------------- SETTINGS UX ------------------------------- */
|
||||
@ -1421,7 +1414,7 @@ UX_STEP_CB(ux_menu_main_3_step, pb, ui_menu_settings_display(0), {&C_icon_coggle
|
||||
|
||||
UX_STEP_CB(ux_menu_main_4_step, pb, ui_menu_info_display(0), {&C_icon_certificate, "About"});
|
||||
|
||||
UX_STEP_CB(ux_menu_main_5_step, pb, os_sched_exit(0), {&C_icon_dashboard_x, "Quit app"});
|
||||
UX_STEP_CB(ux_menu_main_5_step, pb, app_quit(), {&C_icon_dashboard_x, "Quit app"});
|
||||
|
||||
UX_FLOW(ux_flow_main,
|
||||
&ux_menu_main_1_step,
|
||||
@ -1479,8 +1472,4 @@ void ui_init(void) {
|
||||
ui_menu_main_display(0);
|
||||
}
|
||||
|
||||
void io_seproxyhal_display(const bagl_element_t *element) {
|
||||
io_seproxyhal_display_default((bagl_element_t *) element);
|
||||
}
|
||||
|
||||
#endif // defined(HAVE_BAGL) && (defined(TARGET_NANOX) || defined(TARGET_NANOS2))
|
||||
|
@ -89,11 +89,6 @@ static void ui_setting_header(const char* title,
|
||||
// ----------------------- HOME PAGE -------------------------
|
||||
// -----------------------------------------------------------
|
||||
|
||||
void app_quit(void) {
|
||||
// exit app here
|
||||
os_sched_exit(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* home page definition
|
||||
*
|
||||
@ -840,15 +835,12 @@ static bool reset_nav_cb(uint8_t page, nbgl_pageContent_t* content) {
|
||||
*/
|
||||
static void reset_ctrl_cb(int token, uint8_t index) {
|
||||
UNUSED(index);
|
||||
unsigned char magic[4] = {0, 0, 0, 0};
|
||||
|
||||
if (token != TOKEN_RESET) {
|
||||
return;
|
||||
}
|
||||
nvm_write((void*) (N_gpg_pstate->magic), magic, sizeof(magic));
|
||||
gpg_init();
|
||||
ui_CCID_reset();
|
||||
ui_init();
|
||||
|
||||
app_reset();
|
||||
}
|
||||
|
||||
/* ------------------------------- SETTINGS UX ------------------------------- */
|
||||
|
Loading…
Reference in New Issue
Block a user