Some code cleanups and led blink modifications.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
202
hsm2040.c
202
hsm2040.c
@@ -366,9 +366,10 @@ usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
BLINK_NOT_MOUNTED = 250,
|
BLINK_NOT_MOUNTED = (250 << 16) | 250,
|
||||||
BLINK_MOUNTED = 1000,
|
BLINK_MOUNTED = (250 << 16) | 250,
|
||||||
BLINK_SUSPENDED = 2500,
|
BLINK_SUSPENDED = (500 << 16) | 1000,
|
||||||
|
BLINK_PROCESSING = (50 << 16) | 50,
|
||||||
|
|
||||||
BLINK_RED = 18,
|
BLINK_RED = 18,
|
||||||
BLINK_GREEN = 19,
|
BLINK_GREEN = 19,
|
||||||
@@ -478,6 +479,7 @@ static enum ccid_state ccid_power_on (struct ccid *c)
|
|||||||
|
|
||||||
DEBUG_INFO("ON\r\n");
|
DEBUG_INFO("ON\r\n");
|
||||||
c->tx_busy = 1;
|
c->tx_busy = 1;
|
||||||
|
blink_interval_ms = BLINK_MOUNTED;
|
||||||
return CCID_STATE_WAIT;
|
return CCID_STATE_WAIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -524,6 +526,7 @@ static enum ccid_state ccid_power_off (struct ccid *c)
|
|||||||
ccid_send_status (c);
|
ccid_send_status (c);
|
||||||
DEBUG_INFO ("OFF\r\n");
|
DEBUG_INFO ("OFF\r\n");
|
||||||
c->tx_busy = 1;
|
c->tx_busy = 1;
|
||||||
|
blink_interval_ms = BLINK_SUSPENDED;
|
||||||
return CCID_STATE_START;
|
return CCID_STATE_START;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -927,81 +930,6 @@ TU_LOG3("---- CCID STATE %d,msg_type %x,start %d\r\n",c->ccid_state,c->ccid_head
|
|||||||
}
|
}
|
||||||
else if (c->ccid_header.msg_type == CCID_SET_PARAMS || c->ccid_header.msg_type == CCID_GET_PARAMS || c->ccid_header.msg_type == CCID_RESET_PARAMS)
|
else if (c->ccid_header.msg_type == CCID_SET_PARAMS || c->ccid_header.msg_type == CCID_GET_PARAMS || c->ccid_header.msg_type == CCID_RESET_PARAMS)
|
||||||
ccid_send_params(c);
|
ccid_send_params(c);
|
||||||
else if (c->ccid_header.msg_type == CCID_SECURE)
|
|
||||||
{
|
|
||||||
if (c->p != c->a->cmd_apdu_data)
|
|
||||||
{
|
|
||||||
/* SECURE received in the middle of command chaining */
|
|
||||||
ccid_reset (c);
|
|
||||||
ccid_error (c, CCID_OFFSET_DATA_LEN);
|
|
||||||
return next_state;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (c->p[10-10] == 0x00) /* PIN verification */
|
|
||||||
{
|
|
||||||
c->a->cmd_apdu_head[0] = c->p[25-10];
|
|
||||||
c->a->cmd_apdu_head[1] = c->p[26-10];
|
|
||||||
c->a->cmd_apdu_head[2] = c->p[27-10];
|
|
||||||
c->a->cmd_apdu_head[3] = c->p[28-10];
|
|
||||||
/**/
|
|
||||||
c->a->cmd_apdu_data[0] = 0; /* bConfirmPIN */
|
|
||||||
c->a->cmd_apdu_data[1] = c->p[17-10]; /* bEntryValidationCondition */
|
|
||||||
c->a->cmd_apdu_data[2] = c->p[18-10]; /* bNumberMessage */
|
|
||||||
c->a->cmd_apdu_data[3] = c->p[19-10]; /* wLangId L */
|
|
||||||
c->a->cmd_apdu_data[4] = c->p[20-10]; /* wLangId H */
|
|
||||||
c->a->cmd_apdu_data[5] = c->p[21-10]; /* bMsgIndex */
|
|
||||||
|
|
||||||
c->a->cmd_apdu_data_len = 6;
|
|
||||||
c->a->expected_res_size = 0;
|
|
||||||
|
|
||||||
c->a->sw = 0x9000;
|
|
||||||
c->a->res_apdu_data_len = 0;
|
|
||||||
c->a->res_apdu_data = &c->p[5];
|
|
||||||
|
|
||||||
c->state = APDU_STATE_COMMAND_RECEIVED;
|
|
||||||
uint32_t flag = EV_VERIFY_CMD_AVAILABLE;
|
|
||||||
queue_try_add(&c->card_comm, &flag);
|
|
||||||
next_state = CCID_STATE_EXECUTE;
|
|
||||||
}
|
|
||||||
else if (c->p[10-10] == 0x01) /* PIN Modification */
|
|
||||||
{
|
|
||||||
uint8_t num_msgs = c->p[21-10];
|
|
||||||
|
|
||||||
if (num_msgs == 0x00)
|
|
||||||
num_msgs = 1;
|
|
||||||
else if (num_msgs == 0xff)
|
|
||||||
num_msgs = 3;
|
|
||||||
c->a->cmd_apdu_head[0] = c->p[27 + num_msgs-10];
|
|
||||||
c->a->cmd_apdu_head[1] = c->p[28 + num_msgs-10];
|
|
||||||
c->a->cmd_apdu_head[2] = c->p[29 + num_msgs-10];
|
|
||||||
c->a->cmd_apdu_head[3] = c->p[30 + num_msgs-10];
|
|
||||||
/**/
|
|
||||||
c->a->cmd_apdu_data[0] = c->p[19-10]; /* bConfirmPIN */
|
|
||||||
c->a->cmd_apdu_data[1] = c->p[20-10]; /* bEntryValidationCondition */
|
|
||||||
c->a->cmd_apdu_data[2] = c->p[21-10]; /* bNumberMessage */
|
|
||||||
c->a->cmd_apdu_data[3] = c->p[22-10]; /* wLangId L */
|
|
||||||
c->a->cmd_apdu_data[4] = c->p[23-10]; /* wLangId H */
|
|
||||||
c->a->cmd_apdu_data[5] = c->p[24-10]; /* bMsgIndex, bMsgIndex1 */
|
|
||||||
if (num_msgs >= 2)
|
|
||||||
c->a->cmd_apdu_data[6] = c->p[25-10]; /* bMsgIndex2 */
|
|
||||||
if (num_msgs == 3)
|
|
||||||
c->a->cmd_apdu_data[7] = c->p[26-10]; /* bMsgIndex3 */
|
|
||||||
|
|
||||||
c->a->cmd_apdu_data_len = 5 + num_msgs;
|
|
||||||
c->a->expected_res_size = 0;
|
|
||||||
|
|
||||||
c->a->sw = 0x9000;
|
|
||||||
c->a->res_apdu_data_len = 0;
|
|
||||||
c->a->res_apdu_data = &ccid_buffer[5];
|
|
||||||
|
|
||||||
c->state = APDU_STATE_COMMAND_RECEIVED;
|
|
||||||
uint32_t flag = EV_MODIFY_CMD_AVAILABLE;
|
|
||||||
queue_try_add(&c->card_comm, &flag);
|
|
||||||
next_state = CCID_STATE_EXECUTE;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
ccid_error (c, CCID_MSG_DATA_OFFSET);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
DEBUG_INFO ("ERR03\r\n");
|
DEBUG_INFO ("ERR03\r\n");
|
||||||
@@ -1442,6 +1370,7 @@ void prepare_ccid()
|
|||||||
}
|
}
|
||||||
|
|
||||||
int process_apdu() {
|
int process_apdu() {
|
||||||
|
blink_interval_ms = BLINK_PROCESSING;
|
||||||
if (!current_app) {
|
if (!current_app) {
|
||||||
if (INS(apdu) == 0xA4 && P1(apdu) == 0x04 && (P2(apdu) == 0x00 || P2(apdu) == 0x4)) { //select by AID
|
if (INS(apdu) == 0xA4 && P1(apdu) == 0x04 && (P2(apdu) == 0x00 || P2(apdu) == 0x4)) { //select by AID
|
||||||
for (int a = 0; a < num_apps; a++) {
|
for (int a = 0; a < num_apps; a++) {
|
||||||
@@ -1457,7 +1386,6 @@ int process_apdu() {
|
|||||||
return set_res_sw (0x6D, 0x00);
|
return set_res_sw (0x6D, 0x00);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint16_t set_res_sw (uint8_t sw1, uint8_t sw2)
|
uint16_t set_res_sw (uint8_t sw1, uint8_t sw2)
|
||||||
{
|
{
|
||||||
apdu.sw = (sw1 << 8) | sw2;
|
apdu.sw = (sw1 << 8) | sw2;
|
||||||
@@ -1479,94 +1407,14 @@ void card_thread()
|
|||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
#if defined(PINPAD_SUPPORT)
|
|
||||||
int len, pw_len, newpw_len;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
uint32_t m;
|
uint32_t m;
|
||||||
queue_remove_blocking(card_comm, &m);
|
queue_remove_blocking(card_comm, &m);
|
||||||
|
|
||||||
if (m == EV_VERIFY_CMD_AVAILABLE)
|
if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE)
|
||||||
{
|
|
||||||
#if defined(PINPAD_SUPPORT)
|
|
||||||
if (INS (apdu) != INS_VERIFY)
|
|
||||||
{
|
|
||||||
GPG_CONDITION_NOT_SATISFIED ();
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
pw_len = get_pinpad_input (PIN_INPUT_CURRENT);
|
|
||||||
if (pw_len < 0)
|
|
||||||
{
|
{
|
||||||
set_res_sw (0x6f, 0x00);
|
set_res_sw (0x6f, 0x00);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
memcpy (apdu.cmd_apdu_data, pin_input_buffer, pw_len);
|
|
||||||
apdu.cmd_apdu_data_len = pw_len;
|
|
||||||
#else
|
|
||||||
set_res_sw (0x6f, 0x00);
|
|
||||||
goto done;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else if (m == EV_MODIFY_CMD_AVAILABLE)
|
|
||||||
{
|
|
||||||
#if defined(PINPAD_SUPPORT)
|
|
||||||
uint8_t bConfirmPIN = apdu.cmd_apdu_data[0];
|
|
||||||
uint8_t *p = apdu.cmd_apdu_data;
|
|
||||||
|
|
||||||
if (INS (apdu) != INS_CHANGE_REFERENCE_DATA
|
|
||||||
&& INS (apdu) != INS_RESET_RETRY_COUNTER
|
|
||||||
&& INS (apdu) != INS_PUT_DATA)
|
|
||||||
{
|
|
||||||
GPG_CONDITION_NOT_SATISFIED ();
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((bConfirmPIN & 2)) /* Require old PIN */
|
|
||||||
{
|
|
||||||
pw_len = get_pinpad_input (PIN_INPUT_CURRENT);
|
|
||||||
if (pw_len < 0)
|
|
||||||
{
|
|
||||||
set_res_sw (0x6f, 0x00);
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
memcpy (p, pin_input_buffer, pw_len);
|
|
||||||
p += pw_len;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
pw_len = 0;
|
|
||||||
|
|
||||||
newpw_len = get_pinpad_input (PIN_INPUT_NEW);
|
|
||||||
if (newpw_len < 0)
|
|
||||||
{
|
|
||||||
set_res_sw (0x6f, 0x00);
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
memcpy (p, pin_input_buffer, newpw_len);
|
|
||||||
|
|
||||||
if ((bConfirmPIN & 1)) /* New PIN twice */
|
|
||||||
{
|
|
||||||
len = get_pinpad_input (PIN_INPUT_CONFIRM);
|
|
||||||
if (len < 0)
|
|
||||||
{
|
|
||||||
set_res_sw (0x6f, 0x00);
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len != newpw_len || memcmp (p, pin_input_buffer, len) != 0)
|
|
||||||
{
|
|
||||||
GPG_SECURITY_FAILURE ();
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apdu.cmd_apdu_data_len = pw_len + newpw_len;
|
|
||||||
#else
|
|
||||||
set_res_sw (0x6f, 0x00);
|
|
||||||
goto done;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
else if (m == EV_EXIT) {
|
else if (m == EV_EXIT) {
|
||||||
if (current_app && current_app->unload)
|
if (current_app && current_app->unload)
|
||||||
current_app->unload();
|
current_app->unload();
|
||||||
@@ -1677,6 +1525,7 @@ void ccid_task(void)
|
|||||||
{
|
{
|
||||||
DEBUG_INFO ("ERR05\r\n");
|
DEBUG_INFO ("ERR05\r\n");
|
||||||
}
|
}
|
||||||
|
blink_interval_ms = BLINK_MOUNTED;
|
||||||
}
|
}
|
||||||
else if (m == EV_TX_FINISHED)
|
else if (m == EV_TX_FINISHED)
|
||||||
{
|
{
|
||||||
@@ -1709,43 +1558,23 @@ void ccid_task(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoked when cdc when line state changed e.g connected/disconnected
|
|
||||||
void tud_vendor_line_state_cb(uint8_t itf, bool dtr, bool rts)
|
|
||||||
{
|
|
||||||
(void) itf;
|
|
||||||
|
|
||||||
// connected
|
|
||||||
if ( dtr && rts )
|
|
||||||
{
|
|
||||||
// print initial message when connected
|
|
||||||
tud_vendor_write_str("\r\nTinyUSB WebUSB device example\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Invoked when CDC interface received data from host
|
|
||||||
void tud_vendor_rx_cb(uint8_t itf)
|
|
||||||
{
|
|
||||||
(void) itf;
|
|
||||||
//TU_LOG3("!!!!!!! RX_CB\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void tud_mount_cb()
|
void tud_mount_cb()
|
||||||
{
|
{
|
||||||
//TU_LOG3("!!!!!!! MOUNTED\r\n");
|
|
||||||
|
|
||||||
ccid_prepare_receive (&ccid);
|
ccid_prepare_receive (&ccid);
|
||||||
}
|
}
|
||||||
|
|
||||||
void led_blinking_task(void)
|
void led_blinking_task()
|
||||||
{
|
{
|
||||||
static uint32_t start_ms = 0;
|
static uint32_t start_ms = 0;
|
||||||
static uint8_t led_state = false;
|
static uint8_t led_state = false;
|
||||||
static uint8_t led_color = BLINK_RED;
|
static uint8_t led_color = BLINK_RED;
|
||||||
|
uint32_t interval = !led_state ? blink_interval_ms & 0xffff : blink_interval_ms >> 16;
|
||||||
|
|
||||||
|
|
||||||
// Blink every interval ms
|
// Blink every interval ms
|
||||||
if ( board_millis() - start_ms < blink_interval_ms)
|
if (board_millis() - start_ms < interval)
|
||||||
return; // not enough time
|
return; // not enough time
|
||||||
start_ms += blink_interval_ms;
|
start_ms += interval;
|
||||||
|
|
||||||
gpio_put(led_color, led_state);
|
gpio_put(led_color, led_state);
|
||||||
led_state ^= 1; // toggle
|
led_state ^= 1; // toggle
|
||||||
@@ -1758,9 +1587,6 @@ void led_off_all()
|
|||||||
gpio_put(20, 1);
|
gpio_put(20, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define RANDOM_BYTES_LENGTH 32
|
|
||||||
#include "hardware/flash.h"
|
|
||||||
#include "hardware/sync.h"
|
|
||||||
extern void neug_task();
|
extern void neug_task();
|
||||||
|
|
||||||
pico_unique_board_id_t unique_id;
|
pico_unique_board_id_t unique_id;
|
||||||
|
|||||||
Reference in New Issue
Block a user