Skip to content

Commit

Permalink
[clean] Attempt to reduce scattering and unclearness
Browse files Browse the repository at this point in the history
  • Loading branch information
lpascal-ledger committed Feb 19, 2024
1 parent 5529284 commit 9293b49
Show file tree
Hide file tree
Showing 16 changed files with 80 additions and 72 deletions.
3 changes: 2 additions & 1 deletion src/apdu_handlers/dump_metadatas.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "dump_metadatas.h"
#include "error.h"
#include "globals.h"
#include "io.h"
#include "sw.h"
#include "password_ui_flows.h"


int dump_metadatas() {
if (app_state.user_approval == false) {
app_state.bytes_transferred = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/apdu_handlers/get_app_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include <math.h>
#include <stdlib.h>

#include "../password_ui_flows.h"
#include "error.h"
#include "get_app_config.h"
#include "sw.h"
#include "types.h"
#include "globals.h"
#include "password_ui_flows.h"
#include "types.h"

int get_app_config(uint8_t p1, uint8_t p2, __attribute__((unused)) const buf_t* input) {
if (p1 != 0 || p2 != 0) {
Expand Down
5 changes: 3 additions & 2 deletions src/apdu_handlers/load_metadatas.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <io.h>

#include "load_metadatas.h"
#include "error.h"
#include "globals.h"
#include "sw.h"
#include "load_metadatas.h"
#include "metadata.h"
#include "password_ui_flows.h"


int load_metadatas(uint8_t p1, uint8_t p2, const buf_t *input) {
if ((p1 != 0 && p1 != P1_LAST_CHUNK) || p2 != 0) {
return io_send_sw(SW_WRONG_P1P2);
Expand Down
28 changes: 13 additions & 15 deletions src/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,35 @@
#include <io.h>
#include <os.h>
#include <os_io_seproxyhal.h>
#include <lib_standard_app/offsets.h>
#include <stdbool.h>
#include <string.h>

#include <ctr_drbg.h>
#include <hid_mapping.h>
#include <password_generation.h>
#include <usbd_hid_impl.h>

#include "glyphs.h"

#include "ctr_drbg.h"
#include "hid_mapping.h"
#include "password_generation.h"
#include "usbd_hid_impl.h"
#include "sw.h"

#include "password_ui_flows.h"
#include "password_typing.h"
#include "dispatcher.h"
#include "error.h"
#include "globals.h"
#include "metadata.h"
#include "dispatcher.h"
#include "password_ui_flows.h"
#include "password_typing.h"


const internalStorage_t N_storage_real;
app_state_t app_state;
volatile unsigned int G_led_status;

void app_init() {
init_storage();
memset(&app_state, 0, sizeof(app_state));
}

void app_main() {
int input_len = 0;

app_init();
init_storage();
memset(&app_state, 0, sizeof(app_state));

ui_idle();

app_state.io.output_len = 0;
Expand Down
9 changes: 5 additions & 4 deletions src/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@
********************************************************************************/

#include <io.h>
#include <lib_standard_app/offsets.h>
#include <stdint.h>

#include "dispatcher.h"
#include "types.h"
#include "globals.h"
#include "sw.h"
#include "apdu_handlers/dump_metadatas.h"
#include "apdu_handlers/load_metadatas.h"
#include "apdu_handlers/get_app_config.h"
#include "dispatcher.h"
#include "error.h"
#include "globals.h"
#include "tests/tests.h"
#include "types.h"

int dispatch() {
if (G_io_apdu_buffer[OFFSET_CLA] != CLA) {
Expand Down
18 changes: 12 additions & 6 deletions src/error.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#include "error.h"


static const message_pair_t ERR_MESSAGES[5] = {
{"", ""}, // OK
{"Write Error", "Database is full"}, // ERR_NO_MORE_SPACE_AVAILABLE
{"Write Error",
"Database should be repaired, please contact Ledger Support"}, // ERR_CORRUPTED_METADATA
{"Erase Error", "Database already empty"}, // ERR_NO_METADATA
{"Write Error", "Entry is too big"} // ERR_METADATA_ENTRY_TOO_BIG
// OK
{},
// ERR_NO_MORE_SPACE_AVAILABLE
{"Write Error", "Database is full"},
// ERR_CORRUPTED_METADATA
{"Write Error", "Database should be repaired, please contact Ledger Support"},
// ERR_NO_METADATA
{"Erase Error", "Database already empty"},
// ERR_METADATA_ENTRY_TOO_BIG
{"Write Error", "Entry is too big"}
};


message_pair_t get_error(const error_type_t error) {
return ERR_MESSAGES[error];
}
19 changes: 19 additions & 0 deletions src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@

#include "types.h"

#define SW_OK 0x9000
#define SW_CONDITIONS_OF_USE_NOT_SATISFIED 0x6985
#define SW_WRONG_P1P2 0x6A86
#define SW_WRONG_DATA_LENGTH 0x6A87
#define SW_INS_NOT_SUPPORTED 0x6D00
#define SW_CLA_NOT_SUPPORTED 0x6E00
#define SW_APPNAME_TOO_LONG 0xB000
#define SW_METADATAS_PARSING_ERROR 0x6F10


typedef enum error_type_e {
OK = 0,
ERR_NO_MORE_SPACE_AVAILABLE = 1,
ERR_CORRUPTED_METADATA = 2,
ERR_NO_METADATA = 3,
ERR_METADATA_ENTRY_TOO_BIG = 4
} error_type_t;


message_pair_t get_error(const error_type_t error);
11 changes: 2 additions & 9 deletions src/globals.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#pragma once

#include "os.h"
#include "os_io_seproxyhal.h"
#include <os.h>
#include <os_io_seproxyhal.h>
#include "types.h"

extern const internalStorage_t N_storage_real;
extern app_state_t app_state;
extern volatile unsigned int G_led_status;

#define OFFSET_CLA 0
#define OFFSET_INS 1
#define OFFSET_P1 2
#define OFFSET_P2 3
#define OFFSET_LC 4
#define OFFSET_CDATA 5

#define CLA 0xE0
#define N_storage (*(volatile internalStorage_t*) PIC(&N_storage_real))

Expand Down
1 change: 1 addition & 0 deletions src/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "metadata.h"
#include "globals.h"


error_type_t write_metadata(uint8_t *data, uint8_t dataSize) {
if (dataSize > MAX_METANAME) {
dataSize = MAX_METANAME;
Expand Down
1 change: 1 addition & 0 deletions src/metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdint.h>

#include "error.h"
#include "types.h"

#define METADATA_PTR(offset) (&N_storage.metadatas[offset])
Expand Down
1 change: 1 addition & 0 deletions src/password_typing.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "password_typing.h"
#include "globals.h"


#define REPORT_SIZE 8
static const uint8_t EMPTY_REPORT[REPORT_SIZE] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static const uint8_t SPACE_REPORT[REPORT_SIZE] = {0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00};
Expand Down
6 changes: 4 additions & 2 deletions src/password_typing.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include "stdint.h"
#include "password_generation.h"
#include <stdint.h>

#include <password_generation.h>


bool type_password(uint8_t *data,
uint32_t dataSize,
Expand Down
20 changes: 10 additions & 10 deletions src/password_ui_flows.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#include <io.h>
#include <usbd_hid_impl.h>
#include <string.h>
#include <lib_stusb_impl/usbd_hid_impl.h>
#include <stdbool.h>
#include <string.h>

#include <hid_mapping.h>

#include "dispatcher.h"
#include "error.h"
#include "globals.h"
#include "metadata.h"
#include "password_ui_flows.h"
#include "password.h"
#include "globals.h"
#include "password_typing.h"
#include "metadata.h"
#include "dispatcher.h"
#include "sw.h"

#define LINE_1_SIZE 16
char line_buffer_1[LINE_1_SIZE];
Expand Down Expand Up @@ -89,7 +88,7 @@ static void select_password_and_apply_cb();
static void type_password_cb(const size_t offset);
static void show_password_cb(const size_t offset);
static void reset_password_cb(const size_t offset);
static void ui_error(message_pair_t err);
static void ui_error(error_type_t error);

// clang-format off
UX_STEP_INIT(
Expand Down Expand Up @@ -197,7 +196,7 @@ static void reset_password_cb(const size_t offset) {
PRINTF("reset_password_cb\n");
error_type_t err = delete_password_at_offset(offset);
if (err != OK) {
ui_error(get_error(err));
ui_error(err);
return;
}
ui_idle();
Expand Down Expand Up @@ -329,7 +328,7 @@ static void create_password_entry() {
error_type_t err =
create_new_password(G_keyboard_ctx.words_buffer, strlen(G_keyboard_ctx.words_buffer));
if (err != OK) {
ui_error(get_error(err));
ui_error(err);
return;
}
ui_idle();
Expand Down Expand Up @@ -374,7 +373,8 @@ bnnn_paging,

UX_FLOW(err_corrupted_memory_flow, &err_corrupted_memory_step, &generic_cancel_step);

static void ui_error(message_pair_t err) {
static void ui_error(error_type_t error) {
message_pair_t err = get_error(error);
strlcpy(line_buffer_1, (char*) PIC(err.first), strlen(err.first) + 1);
strlcpy(line_buffer_2, (char*) PIC(err.second), strlen(err.second) + 1);
ux_flow_init(0, err_corrupted_memory_flow, NULL);
Expand Down
10 changes: 0 additions & 10 deletions src/sw.h

This file was deleted.

6 changes: 4 additions & 2 deletions src/tests/tests.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <io.h>
#include "tests.h"
#include "sw.h"

#include "error.h"
#include "password_typing.h"
#include "tests.h"


/* Takes a metadata as an input (charset + seed) and returns a 20 char password*/
int test_generate_password(const buf_t *input) {
Expand Down
8 changes: 0 additions & 8 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,3 @@ typedef struct message_pair_s {
const char* first;
const char* second;
} message_pair_t;

typedef enum error_type_e {
OK = 0,
ERR_NO_MORE_SPACE_AVAILABLE = 1,
ERR_CORRUPTED_METADATA = 2,
ERR_NO_METADATA = 3,
ERR_METADATA_ENTRY_TOO_BIG = 4
} error_type_t;

0 comments on commit 9293b49

Please sign in to comment.