mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-28 17:11:23 +02:00
Harmonizing coding style.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
90
src/apdu.c
90
src/apdu.c
@@ -24,11 +24,13 @@ uint8_t *rdata_gr = NULL;
|
|||||||
uint16_t rdata_bk = 0x0;
|
uint16_t rdata_bk = 0x0;
|
||||||
extern uint32_t timeout;
|
extern uint32_t timeout;
|
||||||
|
|
||||||
int process_apdu() {
|
int process_apdu()
|
||||||
|
{
|
||||||
led_set_blink(BLINK_PROCESSING);
|
led_set_blink(BLINK_PROCESSING);
|
||||||
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
|
||||||
if (current_app && current_app->unload)
|
if (current_app && current_app->unload) {
|
||||||
current_app->unload();
|
current_app->unload();
|
||||||
|
}
|
||||||
for (int a = 0; a < num_apps; a++) {
|
for (int a = 0; a < num_apps; a++) {
|
||||||
if ((current_app = apps[a].select_aid(&apps[a], apdu.data, apdu.nc))) {
|
if ((current_app = apps[a].select_aid(&apps[a], apdu.data, apdu.nc))) {
|
||||||
return set_res_sw(0x90, 0x00);
|
return set_res_sw(0x90, 0x00);
|
||||||
@@ -36,52 +38,55 @@ int process_apdu() {
|
|||||||
}
|
}
|
||||||
return set_res_sw(0x6a, 0x82);
|
return set_res_sw(0x6a, 0x82);
|
||||||
}
|
}
|
||||||
if (current_app && current_app->process_apdu)
|
if (current_app && current_app->process_apdu) {
|
||||||
return current_app->process_apdu();
|
return current_app->process_apdu();
|
||||||
|
}
|
||||||
return set_res_sw(0x6D, 0x00);
|
return set_res_sw(0x6D, 0x00);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t apdu_process(uint8_t itf, const uint8_t *buffer, size_t buffer_size) {
|
size_t apdu_process(uint8_t itf, const uint8_t *buffer, size_t buffer_size)
|
||||||
|
{
|
||||||
apdu.header = (uint8_t *) buffer;
|
apdu.header = (uint8_t *) buffer;
|
||||||
apdu.nc = apdu.ne = 0;
|
apdu.nc = apdu.ne = 0;
|
||||||
if (buffer_size == 4) {
|
if (buffer_size == 4) {
|
||||||
apdu.nc = apdu.ne = 0;
|
apdu.nc = apdu.ne = 0;
|
||||||
if (apdu.ne == 0)
|
if (apdu.ne == 0) {
|
||||||
apdu.ne = 256;
|
apdu.ne = 256;
|
||||||
}
|
}
|
||||||
else if (buffer_size == 5) {
|
} else if (buffer_size == 5) {
|
||||||
apdu.nc = 0;
|
apdu.nc = 0;
|
||||||
apdu.ne = apdu.header[4];
|
apdu.ne = apdu.header[4];
|
||||||
if (apdu.ne == 0)
|
if (apdu.ne == 0) {
|
||||||
apdu.ne = 256;
|
apdu.ne = 256;
|
||||||
}
|
}
|
||||||
else if (apdu.header[4] == 0x0 && buffer_size >= 7) {
|
} else if (apdu.header[4] == 0x0 && buffer_size >= 7) {
|
||||||
if (buffer_size == 7) {
|
if (buffer_size == 7) {
|
||||||
apdu.ne = (apdu.header[5] << 8) | apdu.header[6];
|
apdu.ne = (apdu.header[5] << 8) | apdu.header[6];
|
||||||
if (apdu.ne == 0)
|
if (apdu.ne == 0) {
|
||||||
apdu.ne = 65536;
|
apdu.ne = 65536;
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
apdu.ne = 0;
|
apdu.ne = 0;
|
||||||
apdu.nc = (apdu.header[5] << 8) | apdu.header[6];
|
apdu.nc = (apdu.header[5] << 8) | apdu.header[6];
|
||||||
apdu.data = apdu.header+7;
|
apdu.data = apdu.header+7;
|
||||||
if (apdu.nc+7+2 == buffer_size) {
|
if (apdu.nc+7+2 == buffer_size) {
|
||||||
apdu.ne = (apdu.header[buffer_size-2] << 8) | apdu.header[buffer_size-1];
|
apdu.ne = (apdu.header[buffer_size-2] << 8) | apdu.header[buffer_size-1];
|
||||||
if (apdu.ne == 0)
|
if (apdu.ne == 0) {
|
||||||
apdu.ne = 65536;
|
apdu.ne = 65536;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
apdu.nc = apdu.header[4];
|
apdu.nc = apdu.header[4];
|
||||||
apdu.data = apdu.header+5;
|
apdu.data = apdu.header+5;
|
||||||
apdu.ne = 0;
|
apdu.ne = 0;
|
||||||
if (apdu.nc+5+1 == buffer_size) {
|
if (apdu.nc+5+1 == buffer_size) {
|
||||||
apdu.ne = apdu.header[buffer_size-1];
|
apdu.ne = apdu.header[buffer_size-1];
|
||||||
if (apdu.ne == 0)
|
if (apdu.ne == 0) {
|
||||||
apdu.ne = 256;
|
apdu.ne = 256;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//printf("apdu.nc %ld, apdu.ne %ld\r\n",apdu.nc,apdu.ne);
|
//printf("apdu.nc %ld, apdu.ne %ld\r\n",apdu.nc,apdu.ne);
|
||||||
if (apdu.header[1] == 0xc0) {
|
if (apdu.header[1] == 0xc0) {
|
||||||
//printf("apdu.ne %u, apdu.rlen %d, bk %x\r\n",apdu.ne,apdu.rlen,rdata_bk);
|
//printf("apdu.ne %u, apdu.rlen %d, bk %x\r\n",apdu.ne,apdu.rlen,rdata_bk);
|
||||||
@@ -89,47 +94,52 @@ size_t apdu_process(uint8_t itf, const uint8_t *buffer, size_t buffer_size) {
|
|||||||
*(uint16_t *) rdata_gr = rdata_bk;
|
*(uint16_t *) rdata_gr = rdata_bk;
|
||||||
if (apdu.rlen <= apdu.ne) {
|
if (apdu.rlen <= apdu.ne) {
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
driver_exec_finished_cont_hid(apdu.rlen+2, rdata_gr-usb_get_tx(itf));
|
driver_exec_finished_cont_hid(apdu.rlen+2, rdata_gr-usb_get_tx(itf));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
driver_exec_finished_cont_ccid(apdu.rlen+2, rdata_gr-usb_get_tx(itf));
|
driver_exec_finished_cont_ccid(apdu.rlen+2, rdata_gr-usb_get_tx(itf));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
if (itf == ITF_EMUL)
|
if (itf == ITF_EMUL) {
|
||||||
driver_exec_finished_cont_emul(apdu.rlen+2, rdata_gr-usb_get_tx(itf));
|
driver_exec_finished_cont_emul(apdu.rlen+2, rdata_gr-usb_get_tx(itf));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
//Prepare next RAPDU
|
//Prepare next RAPDU
|
||||||
apdu.sw = 0;
|
apdu.sw = 0;
|
||||||
apdu.rlen = 0;
|
apdu.rlen = 0;
|
||||||
usb_prepare_response(itf);
|
usb_prepare_response(itf);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
rdata_gr += apdu.ne;
|
rdata_gr += apdu.ne;
|
||||||
rdata_bk = *rdata_gr;
|
rdata_bk = *rdata_gr;
|
||||||
rdata_gr[0] = 0x61;
|
rdata_gr[0] = 0x61;
|
||||||
if (apdu.rlen - apdu.ne >= 256)
|
if (apdu.rlen - apdu.ne >= 256) {
|
||||||
rdata_gr[1] = 0;
|
rdata_gr[1] = 0;
|
||||||
else
|
} else {
|
||||||
rdata_gr[1] = apdu.rlen - apdu.ne;
|
rdata_gr[1] = apdu.rlen - apdu.ne;
|
||||||
|
}
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
driver_exec_finished_cont_hid(apdu.ne+2, rdata_gr-apdu.ne-usb_get_tx(itf));
|
driver_exec_finished_cont_hid(apdu.ne+2, rdata_gr-apdu.ne-usb_get_tx(itf));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
driver_exec_finished_cont_ccid(apdu.ne+2, rdata_gr-apdu.ne-usb_get_tx(itf));
|
driver_exec_finished_cont_ccid(apdu.ne+2, rdata_gr-apdu.ne-usb_get_tx(itf));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
if (itf == ITF_EMUL)
|
if (itf == ITF_EMUL) {
|
||||||
driver_exec_finished_cont_emul(apdu.ne+2, rdata_gr-apdu.ne-usb_get_tx(itf));
|
driver_exec_finished_cont_emul(apdu.ne+2, rdata_gr-apdu.ne-usb_get_tx(itf));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
apdu.rlen -= apdu.ne;
|
apdu.rlen -= apdu.ne;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
apdu.sw = 0;
|
apdu.sw = 0;
|
||||||
apdu.rlen = 0;
|
apdu.rlen = 0;
|
||||||
apdu.rdata = usb_prepare_response(itf);
|
apdu.rdata = usb_prepare_response(itf);
|
||||||
@@ -139,15 +149,18 @@ size_t apdu_process(uint8_t itf, const uint8_t *buffer, size_t buffer_size) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
if (sw1 != 0x90)
|
if (sw1 != 0x90) {
|
||||||
res_APDU_size = 0;
|
res_APDU_size = 0;
|
||||||
|
}
|
||||||
return make_uint16_t(sw1, sw2);
|
return make_uint16_t(sw1, sw2);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
void apdu_thread() {
|
void apdu_thread()
|
||||||
|
{
|
||||||
card_init_core1();
|
card_init_core1();
|
||||||
while (1) {
|
while (1) {
|
||||||
uint32_t m = 0;
|
uint32_t m = 0;
|
||||||
@@ -156,8 +169,7 @@ void apdu_thread() {
|
|||||||
if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE) {
|
if (m == EV_VERIFY_CMD_AVAILABLE || m == EV_MODIFY_CMD_AVAILABLE) {
|
||||||
set_res_sw(0x6f, 0x00);
|
set_res_sw(0x6f, 0x00);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
} else if (m == EV_EXIT) {
|
||||||
else if (m == EV_EXIT) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,30 +190,32 @@ void apdu_thread() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void apdu_finish() {
|
void apdu_finish()
|
||||||
|
{
|
||||||
apdu.rdata[apdu.rlen] = apdu.sw >> 8;
|
apdu.rdata[apdu.rlen] = apdu.sw >> 8;
|
||||||
apdu.rdata[apdu.rlen+1] = apdu.sw & 0xff;
|
apdu.rdata[apdu.rlen+1] = apdu.sw & 0xff;
|
||||||
timeout_stop();
|
timeout_stop();
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
if ((apdu.rlen + 2 + 10) % 64 == 0)
|
if ((apdu.rlen + 2 + 10) % 64 == 0) { // FIX for strange behaviour with PSCS and multiple of 64
|
||||||
{ // FIX for strange behaviour with PSCS and multiple of 64
|
|
||||||
apdu.ne = apdu.rlen - 2;
|
apdu.ne = apdu.rlen - 2;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t apdu_next() {
|
size_t apdu_next()
|
||||||
|
{
|
||||||
if (apdu.sw != 0) {
|
if (apdu.sw != 0) {
|
||||||
if (apdu.rlen <= apdu.ne)
|
if (apdu.rlen <= apdu.ne) {
|
||||||
return apdu.rlen + 2;
|
return apdu.rlen + 2;
|
||||||
else {
|
} else {
|
||||||
rdata_gr = apdu.rdata+apdu.ne;
|
rdata_gr = apdu.rdata+apdu.ne;
|
||||||
rdata_bk = *(uint16_t *) rdata_gr;
|
rdata_bk = *(uint16_t *) rdata_gr;
|
||||||
rdata_gr[0] = 0x61;
|
rdata_gr[0] = 0x61;
|
||||||
if (apdu.rlen - apdu.ne >= 256)
|
if (apdu.rlen - apdu.ne >= 256) {
|
||||||
rdata_gr[1] = 0;
|
rdata_gr[1] = 0;
|
||||||
else
|
} else {
|
||||||
rdata_gr[1] = apdu.rlen - apdu.ne;
|
rdata_gr[1] = apdu.rlen - apdu.ne;
|
||||||
|
}
|
||||||
apdu.rlen -= apdu.ne;
|
apdu.rlen -= apdu.ne;
|
||||||
}
|
}
|
||||||
return apdu.ne + 2;
|
return apdu.ne + 2;
|
||||||
|
|||||||
@@ -34,8 +34,7 @@ typedef struct app {
|
|||||||
|
|
||||||
extern int register_app(app_t *(*)(app_t *, const uint8_t *, uint8_t));
|
extern int register_app(app_t *(*)(app_t *, const uint8_t *, uint8_t));
|
||||||
|
|
||||||
typedef struct cmd
|
typedef struct cmd {
|
||||||
{
|
|
||||||
uint8_t ins;
|
uint8_t ins;
|
||||||
int (*cmd_handler)();
|
int (*cmd_handler)();
|
||||||
} cmd_t;
|
} cmd_t;
|
||||||
|
|||||||
60
src/asn1.c
60
src/asn1.c
@@ -17,27 +17,29 @@
|
|||||||
|
|
||||||
#include "asn1.h"
|
#include "asn1.h"
|
||||||
|
|
||||||
size_t asn1_len_tag(uint16_t tag, size_t len) {
|
size_t asn1_len_tag(uint16_t tag, size_t len)
|
||||||
|
{
|
||||||
size_t ret = 1+format_tlv_len(len, NULL)+len;
|
size_t ret = 1+format_tlv_len(len, NULL)+len;
|
||||||
if (tag > 0x00ff)
|
if (tag > 0x00ff) {
|
||||||
return ret+1;
|
return ret+1;
|
||||||
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int format_tlv_len(size_t len, uint8_t *out) {
|
int format_tlv_len(size_t len, uint8_t *out)
|
||||||
|
{
|
||||||
if (len < 128) {
|
if (len < 128) {
|
||||||
if (out)
|
if (out) {
|
||||||
*out = len;
|
*out = len;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
else if (len < 256) {
|
return 1;
|
||||||
|
} else if (len < 256) {
|
||||||
if (out) {
|
if (out) {
|
||||||
*out++ = 0x81;
|
*out++ = 0x81;
|
||||||
*out++ = len;
|
*out++ = len;
|
||||||
}
|
}
|
||||||
return 2;
|
return 2;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (out) {
|
if (out) {
|
||||||
*out++ = 0x82;
|
*out++ = 0x82;
|
||||||
*out++ = (len >> 8) & 0xff;
|
*out++ = (len >> 8) & 0xff;
|
||||||
@@ -48,13 +50,22 @@ int format_tlv_len(size_t len, uint8_t *out) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int walk_tlv(const uint8_t *cdata, size_t cdata_len, uint8_t **p, uint16_t *tag, size_t *tag_len, uint8_t **data) {
|
int walk_tlv(const uint8_t *cdata,
|
||||||
if (!p)
|
size_t cdata_len,
|
||||||
|
uint8_t **p,
|
||||||
|
uint16_t *tag,
|
||||||
|
size_t *tag_len,
|
||||||
|
uint8_t **data)
|
||||||
|
{
|
||||||
|
if (!p) {
|
||||||
return 0;
|
return 0;
|
||||||
if (!*p)
|
}
|
||||||
|
if (!*p) {
|
||||||
*p = (uint8_t *) cdata;
|
*p = (uint8_t *) cdata;
|
||||||
if (*p-cdata >= cdata_len)
|
}
|
||||||
|
if (*p-cdata >= cdata_len) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
uint16_t tg = 0x0;
|
uint16_t tg = 0x0;
|
||||||
size_t tgl = 0;
|
size_t tgl = 0;
|
||||||
tg = *(*p)++;
|
tg = *(*p)++;
|
||||||
@@ -66,31 +77,40 @@ int walk_tlv(const uint8_t *cdata, size_t cdata_len, uint8_t **p, uint16_t *tag,
|
|||||||
if (tgl == 0x82) {
|
if (tgl == 0x82) {
|
||||||
tgl = *(*p)++ << 8;
|
tgl = *(*p)++ << 8;
|
||||||
tgl |= *(*p)++;
|
tgl |= *(*p)++;
|
||||||
}
|
} else if (tgl == 0x81) {
|
||||||
else if (tgl == 0x81) {
|
|
||||||
tgl = *(*p)++;
|
tgl = *(*p)++;
|
||||||
}
|
}
|
||||||
if (tag)
|
if (tag) {
|
||||||
*tag = tg;
|
*tag = tg;
|
||||||
if (tag_len)
|
}
|
||||||
|
if (tag_len) {
|
||||||
*tag_len = tgl;
|
*tag_len = tgl;
|
||||||
if (data)
|
}
|
||||||
|
if (data) {
|
||||||
*data = *p;
|
*data = *p;
|
||||||
|
}
|
||||||
*p = *p+tgl;
|
*p = *p+tgl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool asn1_find_tag(const uint8_t *data, size_t data_len, uint16_t itag, size_t *tag_len, uint8_t **tag_data) {
|
bool asn1_find_tag(const uint8_t *data,
|
||||||
|
size_t data_len,
|
||||||
|
uint16_t itag,
|
||||||
|
size_t *tag_len,
|
||||||
|
uint8_t **tag_data)
|
||||||
|
{
|
||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *p = NULL;
|
uint8_t *p = NULL;
|
||||||
uint8_t *tdata = NULL;
|
uint8_t *tdata = NULL;
|
||||||
size_t tlen = 0;
|
size_t tlen = 0;
|
||||||
while (walk_tlv(data, data_len, &p, &tag, &tlen, &tdata)) {
|
while (walk_tlv(data, data_len, &p, &tag, &tlen, &tdata)) {
|
||||||
if (itag == tag) {
|
if (itag == tag) {
|
||||||
if (tag_data != NULL)
|
if (tag_data != NULL) {
|
||||||
*tag_data = tdata;
|
*tag_data = tdata;
|
||||||
if (tag_len != NULL)
|
}
|
||||||
|
if (tag_len != NULL) {
|
||||||
*tag_len = tlen;
|
*tag_len = tlen;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/asn1.h
13
src/asn1.h
@@ -26,9 +26,18 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern int walk_tlv(const uint8_t *cdata, size_t cdata_len, uint8_t **p, uint16_t *tag, size_t *tag_len, uint8_t **data);
|
extern int walk_tlv(const uint8_t *cdata,
|
||||||
|
size_t cdata_len,
|
||||||
|
uint8_t **p,
|
||||||
|
uint16_t *tag,
|
||||||
|
size_t *tag_len,
|
||||||
|
uint8_t **data);
|
||||||
extern int format_tlv_len(size_t len, uint8_t *out);
|
extern int format_tlv_len(size_t len, uint8_t *out);
|
||||||
extern bool asn1_find_tag(const uint8_t *data, size_t data_len, uint16_t itag, size_t *tag_len, uint8_t **tag_data);
|
extern bool asn1_find_tag(const uint8_t *data,
|
||||||
|
size_t data_len,
|
||||||
|
uint16_t itag,
|
||||||
|
size_t *tag_len,
|
||||||
|
uint8_t **tag_data);
|
||||||
extern size_t asn1_len_tag(uint16_t tag, size_t len);
|
extern size_t asn1_len_tag(uint16_t tag, size_t len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -24,15 +24,18 @@
|
|||||||
#include "crypto_utils.h"
|
#include "crypto_utils.h"
|
||||||
#include "hsm.h"
|
#include "hsm.h"
|
||||||
|
|
||||||
void double_hash_pin(const uint8_t *pin, size_t len, uint8_t output[32]) {
|
void double_hash_pin(const uint8_t *pin, size_t len, uint8_t output[32])
|
||||||
|
{
|
||||||
uint8_t o1[32];
|
uint8_t o1[32];
|
||||||
hash_multi(pin, len, o1);
|
hash_multi(pin, len, o1);
|
||||||
for (int i = 0; i < sizeof(o1); i++)
|
for (int i = 0; i < sizeof(o1); i++) {
|
||||||
o1[i] ^= pin[i%len];
|
o1[i] ^= pin[i%len];
|
||||||
|
}
|
||||||
hash_multi(o1, sizeof(o1), output);
|
hash_multi(o1, sizeof(o1), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hash_multi(const uint8_t *input, size_t len, uint8_t output[32]) {
|
void hash_multi(const uint8_t *input, size_t len, uint8_t output[32])
|
||||||
|
{
|
||||||
mbedtls_sha256_context ctx;
|
mbedtls_sha256_context ctx;
|
||||||
mbedtls_sha256_init(&ctx);
|
mbedtls_sha256_init(&ctx);
|
||||||
int iters = 256;
|
int iters = 256;
|
||||||
@@ -46,18 +49,19 @@ void hash_multi(const uint8_t *input, size_t len, uint8_t output[32]) {
|
|||||||
mbedtls_sha256_update(&ctx, unique_id.id, sizeof(unique_id.id));
|
mbedtls_sha256_update(&ctx, unique_id.id, sizeof(unique_id.id));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (iters > len)
|
while (iters > len) {
|
||||||
{
|
|
||||||
mbedtls_sha256_update(&ctx, input, len);
|
mbedtls_sha256_update(&ctx, input, len);
|
||||||
iters -= len;
|
iters -= len;
|
||||||
}
|
}
|
||||||
if (iters > 0) // remaining iterations
|
if (iters > 0) { // remaining iterations
|
||||||
mbedtls_sha256_update(&ctx, input, iters);
|
mbedtls_sha256_update(&ctx, input, iters);
|
||||||
|
}
|
||||||
mbedtls_sha256_finish(&ctx, output);
|
mbedtls_sha256_finish(&ctx, output);
|
||||||
mbedtls_sha256_free(&ctx);
|
mbedtls_sha256_free(&ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void hash256(const uint8_t *input, size_t len, uint8_t output[32]) {
|
void hash256(const uint8_t *input, size_t len, uint8_t output[32])
|
||||||
|
{
|
||||||
mbedtls_sha256_context ctx;
|
mbedtls_sha256_context ctx;
|
||||||
mbedtls_sha256_init(&ctx);
|
mbedtls_sha256_init(&ctx);
|
||||||
|
|
||||||
@@ -68,47 +72,68 @@ void hash256(const uint8_t *input, size_t len, uint8_t output[32]) {
|
|||||||
mbedtls_sha256_free(&ctx);
|
mbedtls_sha256_free(&ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void generic_hash(mbedtls_md_type_t md, const uint8_t *input, size_t len, uint8_t *output) {
|
void generic_hash(mbedtls_md_type_t md, const uint8_t *input, size_t len, uint8_t *output)
|
||||||
|
{
|
||||||
mbedtls_md(mbedtls_md_info_from_type(md), input, len, output);
|
mbedtls_md(mbedtls_md_info_from_type(md), input, len, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
int aes_encrypt(const uint8_t *key, const uint8_t *iv, int key_size, int mode, uint8_t *data, int len) {
|
int aes_encrypt(const uint8_t *key,
|
||||||
|
const uint8_t *iv,
|
||||||
|
int key_size,
|
||||||
|
int mode,
|
||||||
|
uint8_t *data,
|
||||||
|
int len)
|
||||||
|
{
|
||||||
mbedtls_aes_context aes;
|
mbedtls_aes_context aes;
|
||||||
mbedtls_aes_init(&aes);
|
mbedtls_aes_init(&aes);
|
||||||
uint8_t tmp_iv[IV_SIZE];
|
uint8_t tmp_iv[IV_SIZE];
|
||||||
size_t iv_offset = 0;
|
size_t iv_offset = 0;
|
||||||
memset(tmp_iv, 0, IV_SIZE);
|
memset(tmp_iv, 0, IV_SIZE);
|
||||||
if (iv)
|
if (iv) {
|
||||||
memcpy(tmp_iv, iv, IV_SIZE);
|
memcpy(tmp_iv, iv, IV_SIZE);
|
||||||
|
}
|
||||||
int r = mbedtls_aes_setkey_enc(&aes, key, key_size);
|
int r = mbedtls_aes_setkey_enc(&aes, key, key_size);
|
||||||
if (r != 0)
|
if (r != 0) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
if (mode == HSM_AES_MODE_CBC)
|
}
|
||||||
|
if (mode == HSM_AES_MODE_CBC) {
|
||||||
return mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, len, tmp_iv, data, data);
|
return mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, len, tmp_iv, data, data);
|
||||||
|
}
|
||||||
return mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, len, &iv_offset, tmp_iv, data, data);
|
return mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_ENCRYPT, len, &iv_offset, tmp_iv, data, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int aes_decrypt(const uint8_t *key, const uint8_t *iv, int key_size, int mode, uint8_t *data, int len) {
|
int aes_decrypt(const uint8_t *key,
|
||||||
|
const uint8_t *iv,
|
||||||
|
int key_size,
|
||||||
|
int mode,
|
||||||
|
uint8_t *data,
|
||||||
|
int len)
|
||||||
|
{
|
||||||
mbedtls_aes_context aes;
|
mbedtls_aes_context aes;
|
||||||
mbedtls_aes_init(&aes);
|
mbedtls_aes_init(&aes);
|
||||||
uint8_t tmp_iv[IV_SIZE];
|
uint8_t tmp_iv[IV_SIZE];
|
||||||
size_t iv_offset = 0;
|
size_t iv_offset = 0;
|
||||||
memset(tmp_iv, 0, IV_SIZE);
|
memset(tmp_iv, 0, IV_SIZE);
|
||||||
if (iv)
|
if (iv) {
|
||||||
memcpy(tmp_iv, iv, IV_SIZE);
|
memcpy(tmp_iv, iv, IV_SIZE);
|
||||||
|
}
|
||||||
int r = mbedtls_aes_setkey_dec(&aes, key, key_size);
|
int r = mbedtls_aes_setkey_dec(&aes, key, key_size);
|
||||||
if (r != 0)
|
if (r != 0) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
if (mode == HSM_AES_MODE_CBC)
|
}
|
||||||
|
if (mode == HSM_AES_MODE_CBC) {
|
||||||
return mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, len, tmp_iv, data, data);
|
return mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, len, tmp_iv, data, data);
|
||||||
|
}
|
||||||
r = mbedtls_aes_setkey_enc(&aes, key, key_size); //CFB requires set_enc instead set_dec
|
r = mbedtls_aes_setkey_enc(&aes, key, key_size); //CFB requires set_enc instead set_dec
|
||||||
return mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_DECRYPT, len, &iv_offset, tmp_iv, data, data);
|
return mbedtls_aes_crypt_cfb128(&aes, MBEDTLS_AES_DECRYPT, len, &iv_offset, tmp_iv, data, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len) {
|
int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len)
|
||||||
|
{
|
||||||
return aes_encrypt(key, iv, 256, HSM_AES_MODE_CFB, data, len);
|
return aes_encrypt(key, iv, 256, HSM_AES_MODE_CFB, data, len);
|
||||||
}
|
}
|
||||||
int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len) {
|
int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len)
|
||||||
|
{
|
||||||
return aes_decrypt(key, iv, 256, HSM_AES_MODE_CFB, data, len);
|
return aes_decrypt(key, iv, 256, HSM_AES_MODE_CFB, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,22 +147,45 @@ struct ec_curve_mbed_id {
|
|||||||
mbedtls_ecp_group_id id;
|
mbedtls_ecp_group_id id;
|
||||||
};
|
};
|
||||||
struct ec_curve_mbed_id ec_curves_mbed[] = {
|
struct ec_curve_mbed_id ec_curves_mbed[] = {
|
||||||
{ { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 24}, MBEDTLS_ECP_DP_SECP192R1 },
|
{ { (unsigned char *)
|
||||||
{ { (unsigned char *) "\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 32}, MBEDTLS_ECP_DP_SECP256R1 },
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||||
{ { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF", 48}, MBEDTLS_ECP_DP_SECP384R1 },
|
24 }, MBEDTLS_ECP_DP_SECP192R1 },
|
||||||
{ { (unsigned char *) "\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 66}, MBEDTLS_ECP_DP_SECP521R1 },
|
{ { (unsigned char *)
|
||||||
{ { (unsigned char *) "\xA9\xFB\x57\xDB\xA1\xEE\xA9\xBC\x3E\x66\x0A\x90\x9D\x83\x8D\x72\x6E\x3B\xF6\x23\xD5\x26\x20\x28\x20\x13\x48\x1D\x1F\x6E\x53\x77", 32}, MBEDTLS_ECP_DP_BP256R1 },
|
"\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||||
{ { (unsigned char *) "\x8C\xB9\x1E\x82\xA3\x38\x6D\x28\x0F\x5D\x6F\x7E\x50\xE6\x41\xDF\x15\x2F\x71\x09\xED\x54\x56\xB4\x12\xB1\xDA\x19\x7F\xB7\x11\x23\xAC\xD3\xA7\x29\x90\x1D\x1A\x71\x87\x47\x00\x13\x31\x07\xEC\x53", 48}, MBEDTLS_ECP_DP_BP384R1 },
|
32 }, MBEDTLS_ECP_DP_SECP256R1 },
|
||||||
{ { (unsigned char *) "\xAA\xDD\x9D\xB8\xDB\xE9\xC4\x8B\x3F\xD4\xE6\xAE\x33\xC9\xFC\x07\xCB\x30\x8D\xB3\xB3\xC9\xD2\x0E\xD6\x63\x9C\xCA\x70\x33\x08\x71\x7D\x4D\x9B\x00\x9B\xC6\x68\x42\xAE\xCD\xA1\x2A\xE6\xA3\x80\xE6\x28\x81\xFF\x2F\x2D\x82\xC6\x85\x28\xAA\x60\x56\x58\x3A\x48\xF3", 64}, MBEDTLS_ECP_DP_BP512R1 },
|
{ { (unsigned char *)
|
||||||
{ { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xEE\x37", 24}, MBEDTLS_ECP_DP_SECP192K1 },
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF",
|
||||||
{ { (unsigned char *) "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFC\x2F", 32}, MBEDTLS_ECP_DP_SECP256K1 },
|
48 }, MBEDTLS_ECP_DP_SECP384R1 },
|
||||||
{ { (unsigned char *) "\x7f\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xed", 32}, MBEDTLS_ECP_DP_CURVE25519 },
|
{ { (unsigned char *)
|
||||||
{ { (unsigned char *) "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff", 56}, MBEDTLS_ECP_DP_CURVE448 },
|
"\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||||
|
66 }, MBEDTLS_ECP_DP_SECP521R1 },
|
||||||
|
{ { (unsigned char *)
|
||||||
|
"\xA9\xFB\x57\xDB\xA1\xEE\xA9\xBC\x3E\x66\x0A\x90\x9D\x83\x8D\x72\x6E\x3B\xF6\x23\xD5\x26\x20\x28\x20\x13\x48\x1D\x1F\x6E\x53\x77",
|
||||||
|
32 }, MBEDTLS_ECP_DP_BP256R1 },
|
||||||
|
{ { (unsigned char *)
|
||||||
|
"\x8C\xB9\x1E\x82\xA3\x38\x6D\x28\x0F\x5D\x6F\x7E\x50\xE6\x41\xDF\x15\x2F\x71\x09\xED\x54\x56\xB4\x12\xB1\xDA\x19\x7F\xB7\x11\x23\xAC\xD3\xA7\x29\x90\x1D\x1A\x71\x87\x47\x00\x13\x31\x07\xEC\x53",
|
||||||
|
48 }, MBEDTLS_ECP_DP_BP384R1 },
|
||||||
|
{ { (unsigned char *)
|
||||||
|
"\xAA\xDD\x9D\xB8\xDB\xE9\xC4\x8B\x3F\xD4\xE6\xAE\x33\xC9\xFC\x07\xCB\x30\x8D\xB3\xB3\xC9\xD2\x0E\xD6\x63\x9C\xCA\x70\x33\x08\x71\x7D\x4D\x9B\x00\x9B\xC6\x68\x42\xAE\xCD\xA1\x2A\xE6\xA3\x80\xE6\x28\x81\xFF\x2F\x2D\x82\xC6\x85\x28\xAA\x60\x56\x58\x3A\x48\xF3",
|
||||||
|
64 }, MBEDTLS_ECP_DP_BP512R1 },
|
||||||
|
{ { (unsigned char *)
|
||||||
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xEE\x37",
|
||||||
|
24 }, MBEDTLS_ECP_DP_SECP192K1 },
|
||||||
|
{ { (unsigned char *)
|
||||||
|
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFC\x2F",
|
||||||
|
32 }, MBEDTLS_ECP_DP_SECP256K1 },
|
||||||
|
{ { (unsigned char *)
|
||||||
|
"\x7f\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xed",
|
||||||
|
32 }, MBEDTLS_ECP_DP_CURVE25519 },
|
||||||
|
{ { (unsigned char *)
|
||||||
|
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
|
||||||
|
56 }, MBEDTLS_ECP_DP_CURVE448 },
|
||||||
|
|
||||||
{ { NULL, 0 }, MBEDTLS_ECP_DP_NONE }
|
{ { NULL, 0 }, MBEDTLS_ECP_DP_NONE }
|
||||||
};
|
};
|
||||||
|
|
||||||
mbedtls_ecp_group_id ec_get_curve_from_prime(const uint8_t *prime, size_t prime_len) {
|
mbedtls_ecp_group_id ec_get_curve_from_prime(const uint8_t *prime, size_t prime_len)
|
||||||
|
{
|
||||||
for (struct ec_curve_mbed_id *ec = ec_curves_mbed; ec->id != MBEDTLS_ECP_DP_NONE; ec++) {
|
for (struct ec_curve_mbed_id *ec = ec_curves_mbed; ec->id != MBEDTLS_ECP_DP_NONE; ec++) {
|
||||||
if (prime_len == ec->curve.len && memcmp(prime, ec->curve.value, prime_len) == 0) {
|
if (prime_len == ec->curve.len && memcmp(prime, ec->curve.value, prime_len) == 0) {
|
||||||
return ec->id;
|
return ec->id;
|
||||||
|
|||||||
@@ -41,8 +41,18 @@ extern void double_hash_pin(const uint8_t *pin, size_t len, uint8_t output[32]);
|
|||||||
extern void hash_multi(const uint8_t *input, size_t len, uint8_t output[32]);
|
extern void hash_multi(const uint8_t *input, size_t len, uint8_t output[32]);
|
||||||
extern void hash256(const uint8_t *input, size_t len, uint8_t output[32]);
|
extern void hash256(const uint8_t *input, size_t len, uint8_t output[32]);
|
||||||
extern void generic_hash(mbedtls_md_type_t md, const uint8_t *input, size_t len, uint8_t *output);
|
extern void generic_hash(mbedtls_md_type_t md, const uint8_t *input, size_t len, uint8_t *output);
|
||||||
extern int aes_encrypt(const uint8_t *key, const uint8_t *iv, int key_size, int mode, uint8_t *data, int len);
|
extern int aes_encrypt(const uint8_t *key,
|
||||||
extern int aes_decrypt(const uint8_t *key, const uint8_t *iv, int key_size, int mode, uint8_t *data, int len);
|
const uint8_t *iv,
|
||||||
|
int key_size,
|
||||||
|
int mode,
|
||||||
|
uint8_t *data,
|
||||||
|
int len);
|
||||||
|
extern int aes_decrypt(const uint8_t *key,
|
||||||
|
const uint8_t *iv,
|
||||||
|
int key_size,
|
||||||
|
int mode,
|
||||||
|
uint8_t *data,
|
||||||
|
int len);
|
||||||
extern int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len);
|
extern int aes_encrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len);
|
||||||
extern int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len);
|
extern int aes_decrypt_cfb_256(const uint8_t *key, const uint8_t *iv, uint8_t *data, int len);
|
||||||
extern mbedtls_ecp_group_id ec_get_curve_from_prime(const uint8_t *prime, size_t prime_len);
|
extern mbedtls_ecp_group_id ec_get_curve_from_prime(const uint8_t *prime, size_t prime_len);
|
||||||
|
|||||||
116
src/eac.c
116
src/eac.c
@@ -32,16 +32,25 @@ static uint8_t sm_iv[16];
|
|||||||
size_t sm_session_pin_len = 0;
|
size_t sm_session_pin_len = 0;
|
||||||
uint8_t sm_session_pin[16];
|
uint8_t sm_session_pin[16];
|
||||||
|
|
||||||
bool is_secured_apdu() {
|
bool is_secured_apdu()
|
||||||
return (CLA(apdu) & 0xC);
|
{
|
||||||
|
return CLA(apdu) & 0xC;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sm_derive_key(const uint8_t *input, size_t input_len, uint8_t counter, const uint8_t *nonce, size_t nonce_len, uint8_t *out) {
|
void sm_derive_key(const uint8_t *input,
|
||||||
|
size_t input_len,
|
||||||
|
uint8_t counter,
|
||||||
|
const uint8_t *nonce,
|
||||||
|
size_t nonce_len,
|
||||||
|
uint8_t *out)
|
||||||
|
{
|
||||||
uint8_t *b = (uint8_t *) calloc(1, input_len+nonce_len+4);
|
uint8_t *b = (uint8_t *) calloc(1, input_len+nonce_len+4);
|
||||||
if (input)
|
if (input) {
|
||||||
memcpy(b, input, input_len);
|
memcpy(b, input, input_len);
|
||||||
if (nonce)
|
}
|
||||||
|
if (nonce) {
|
||||||
memcpy(b+input_len, nonce, nonce_len);
|
memcpy(b+input_len, nonce, nonce_len);
|
||||||
|
}
|
||||||
b[input_len+nonce_len+3] = counter;
|
b[input_len+nonce_len+3] = counter;
|
||||||
uint8_t digest[20];
|
uint8_t digest[20];
|
||||||
generic_hash(MBEDTLS_MD_SHA1, b, input_len+nonce_len+4, digest);
|
generic_hash(MBEDTLS_MD_SHA1, b, input_len+nonce_len+4, digest);
|
||||||
@@ -49,7 +58,8 @@ void sm_derive_key(const uint8_t *input, size_t input_len, uint8_t counter, cons
|
|||||||
free(b);
|
free(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sm_derive_all_keys(const uint8_t *derived, size_t derived_len) {
|
void sm_derive_all_keys(const uint8_t *derived, size_t derived_len)
|
||||||
|
{
|
||||||
memcpy(nonce, random_bytes_get(8), 8);
|
memcpy(nonce, random_bytes_get(8), 8);
|
||||||
sm_derive_key(derived, derived_len, 1, nonce, sizeof(nonce), sm_kenc);
|
sm_derive_key(derived, derived_len, 1, nonce, sizeof(nonce), sm_kenc);
|
||||||
sm_derive_key(derived, derived_len, 2, nonce, sizeof(nonce), sm_kmac);
|
sm_derive_key(derived, derived_len, 2, nonce, sizeof(nonce), sm_kmac);
|
||||||
@@ -60,36 +70,50 @@ void sm_derive_all_keys(const uint8_t *derived, size_t derived_len) {
|
|||||||
sm_session_pin_len = 0;
|
sm_session_pin_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sm_set_protocol(MSE_protocol proto) {
|
void sm_set_protocol(MSE_protocol proto)
|
||||||
|
{
|
||||||
sm_protocol = proto;
|
sm_protocol = proto;
|
||||||
if (proto == MSE_AES)
|
if (proto == MSE_AES) {
|
||||||
sm_blocksize = 16;
|
sm_blocksize = 16;
|
||||||
else if (proto == MSE_3DES)
|
} else if (proto == MSE_3DES) {
|
||||||
sm_blocksize = 8;
|
sm_blocksize = 8;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
MSE_protocol sm_get_protocol() {
|
MSE_protocol sm_get_protocol()
|
||||||
|
{
|
||||||
return sm_protocol;
|
return sm_protocol;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *sm_get_nonce() {
|
uint8_t *sm_get_nonce()
|
||||||
|
{
|
||||||
return nonce;
|
return nonce;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm_sign(uint8_t *in, size_t in_len, uint8_t *out) {
|
int sm_sign(uint8_t *in, size_t in_len, uint8_t *out)
|
||||||
return mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB), sm_kmac, 128, in, in_len, out);
|
{
|
||||||
|
return mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB),
|
||||||
|
sm_kmac,
|
||||||
|
128,
|
||||||
|
in,
|
||||||
|
in_len,
|
||||||
|
out);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm_unwrap() {
|
int sm_unwrap()
|
||||||
|
{
|
||||||
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
|
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
|
||||||
if (sm_indicator == 0)
|
if (sm_indicator == 0) {
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
|
}
|
||||||
int r = sm_verify();
|
int r = sm_verify();
|
||||||
if (r != CCID_OK)
|
if (r != CCID_OK) {
|
||||||
return r;
|
return r;
|
||||||
|
}
|
||||||
int le = sm_get_le();
|
int le = sm_get_le();
|
||||||
if (le >= 0)
|
if (le >= 0) {
|
||||||
apdu.ne = le;
|
apdu.ne = le;
|
||||||
|
}
|
||||||
uint8_t *body = NULL;
|
uint8_t *body = NULL;
|
||||||
size_t body_size = 0;
|
size_t body_size = 0;
|
||||||
bool is87 = false;
|
bool is87 = false;
|
||||||
@@ -121,10 +145,12 @@ int sm_unwrap() {
|
|||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm_wrap() {
|
int sm_wrap()
|
||||||
|
{
|
||||||
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
|
uint8_t sm_indicator = (CLA(apdu) >> 2) & 0x3;
|
||||||
if (sm_indicator == 0)
|
if (sm_indicator == 0) {
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
|
}
|
||||||
uint8_t input[1024];
|
uint8_t input[1024];
|
||||||
size_t input_len = 0;
|
size_t input_len = 0;
|
||||||
memset(input, 0, sizeof(input));
|
memset(input, 0, sizeof(input));
|
||||||
@@ -133,8 +159,9 @@ int sm_wrap() {
|
|||||||
mbedtls_mpi_add_int(&ssc, &sm_mSSC, 1);
|
mbedtls_mpi_add_int(&ssc, &sm_mSSC, 1);
|
||||||
mbedtls_mpi_copy(&sm_mSSC, &ssc);
|
mbedtls_mpi_copy(&sm_mSSC, &ssc);
|
||||||
int r = mbedtls_mpi_write_binary(&ssc, input, sm_blocksize);
|
int r = mbedtls_mpi_write_binary(&ssc, input, sm_blocksize);
|
||||||
if (r != 0)
|
if (r != 0) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
|
}
|
||||||
input_len += sm_blocksize;
|
input_len += sm_blocksize;
|
||||||
mbedtls_mpi_free(&ssc);
|
mbedtls_mpi_free(&ssc);
|
||||||
if (res_APDU_size > 0) {
|
if (res_APDU_size > 0) {
|
||||||
@@ -151,14 +178,12 @@ int sm_wrap() {
|
|||||||
memmove(res_APDU+2, res_APDU, res_APDU_size);
|
memmove(res_APDU+2, res_APDU, res_APDU_size);
|
||||||
res_APDU[1] = res_APDU_size;
|
res_APDU[1] = res_APDU_size;
|
||||||
res_APDU_size += 2;
|
res_APDU_size += 2;
|
||||||
}
|
} else if (res_APDU_size < 256) {
|
||||||
else if (res_APDU_size < 256) {
|
|
||||||
memmove(res_APDU+3, res_APDU, res_APDU_size);
|
memmove(res_APDU+3, res_APDU, res_APDU_size);
|
||||||
res_APDU[1] = 0x81;
|
res_APDU[1] = 0x81;
|
||||||
res_APDU[2] = res_APDU_size;
|
res_APDU[2] = res_APDU_size;
|
||||||
res_APDU_size += 3;
|
res_APDU_size += 3;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
memmove(res_APDU+4, res_APDU, res_APDU_size);
|
memmove(res_APDU+4, res_APDU, res_APDU_size);
|
||||||
res_APDU[1] = 0x82;
|
res_APDU[1] = 0x82;
|
||||||
res_APDU[2] = res_APDU_size >> 8;
|
res_APDU[2] = res_APDU_size >> 8;
|
||||||
@@ -179,27 +204,31 @@ int sm_wrap() {
|
|||||||
res_APDU[res_APDU_size++] = 0x8E;
|
res_APDU[res_APDU_size++] = 0x8E;
|
||||||
res_APDU[res_APDU_size++] = 8;
|
res_APDU[res_APDU_size++] = 8;
|
||||||
res_APDU_size += 8;
|
res_APDU_size += 8;
|
||||||
if (apdu.ne > 0)
|
if (apdu.ne > 0) {
|
||||||
apdu.ne = res_APDU_size;
|
apdu.ne = res_APDU_size;
|
||||||
|
}
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm_get_le() {
|
int sm_get_le()
|
||||||
|
{
|
||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
size_t tag_len = 0;
|
size_t tag_len = 0;
|
||||||
while (walk_tlv(apdu.data, apdu.nc, &p, &tag, &tag_len, &tag_data)) {
|
while (walk_tlv(apdu.data, apdu.nc, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag == 0x97) {
|
if (tag == 0x97) {
|
||||||
uint32_t le = 0;
|
uint32_t le = 0;
|
||||||
for (int t = 1; t <= tag_len; t++)
|
for (int t = 1; t <= tag_len; t++) {
|
||||||
le |= (*tag_data++) << (tag_len-t);
|
le |= (*tag_data++) << (tag_len-t);
|
||||||
|
}
|
||||||
return le;
|
return le;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sm_update_iv() {
|
void sm_update_iv()
|
||||||
|
{
|
||||||
uint8_t tmp_iv[16], sc_counter[16];
|
uint8_t tmp_iv[16], sc_counter[16];
|
||||||
memset(tmp_iv, 0, sizeof(tmp_iv)); //IV is always 0 for encryption of IV based on counter
|
memset(tmp_iv, 0, sizeof(tmp_iv)); //IV is always 0 for encryption of IV based on counter
|
||||||
mbedtls_mpi_write_binary(&sm_mSSC, sc_counter, sizeof(sc_counter));
|
mbedtls_mpi_write_binary(&sm_mSSC, sc_counter, sizeof(sc_counter));
|
||||||
@@ -207,16 +236,19 @@ void sm_update_iv() {
|
|||||||
memcpy(sm_iv, sc_counter, sizeof(sc_counter));
|
memcpy(sm_iv, sc_counter, sizeof(sc_counter));
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm_verify() {
|
int sm_verify()
|
||||||
|
{
|
||||||
uint8_t input[1024];
|
uint8_t input[1024];
|
||||||
memset(input, 0, sizeof(input));
|
memset(input, 0, sizeof(input));
|
||||||
int input_len = 0, r = 0;
|
int input_len = 0, r = 0;
|
||||||
bool add_header = (CLA(apdu) & 0xC) == 0xC;
|
bool add_header = (CLA(apdu) & 0xC) == 0xC;
|
||||||
int data_len = (int) (apdu.nc/sm_blocksize)*sm_blocksize;
|
int data_len = (int) (apdu.nc/sm_blocksize)*sm_blocksize;
|
||||||
if (data_len % sm_blocksize)
|
if (data_len % sm_blocksize) {
|
||||||
data_len += sm_blocksize;
|
data_len += sm_blocksize;
|
||||||
if (data_len+(add_header ? sm_blocksize : 0) > 1024)
|
}
|
||||||
|
if (data_len+(add_header ? sm_blocksize : 0) > 1024) {
|
||||||
return CCID_WRONG_LENGTH;
|
return CCID_WRONG_LENGTH;
|
||||||
|
}
|
||||||
mbedtls_mpi ssc;
|
mbedtls_mpi ssc;
|
||||||
mbedtls_mpi_init(&ssc);
|
mbedtls_mpi_init(&ssc);
|
||||||
mbedtls_mpi_add_int(&ssc, &sm_mSSC, 1);
|
mbedtls_mpi_add_int(&ssc, &sm_mSSC, 1);
|
||||||
@@ -224,8 +256,9 @@ int sm_verify() {
|
|||||||
r = mbedtls_mpi_write_binary(&ssc, input, sm_blocksize);
|
r = mbedtls_mpi_write_binary(&ssc, input, sm_blocksize);
|
||||||
input_len += sm_blocksize;
|
input_len += sm_blocksize;
|
||||||
mbedtls_mpi_free(&ssc);
|
mbedtls_mpi_free(&ssc);
|
||||||
if (r != 0)
|
if (r != 0) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
|
}
|
||||||
if (add_header) {
|
if (add_header) {
|
||||||
input[input_len++] = CLA(apdu);
|
input[input_len++] = CLA(apdu);
|
||||||
input[input_len++] = INS(apdu);
|
input[input_len++] = INS(apdu);
|
||||||
@@ -254,25 +287,32 @@ int sm_verify() {
|
|||||||
mac_len = tag_len;
|
mac_len = tag_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!mac)
|
if (!mac) {
|
||||||
return CCID_WRONG_DATA;
|
return CCID_WRONG_DATA;
|
||||||
|
}
|
||||||
if (some_added) {
|
if (some_added) {
|
||||||
input[input_len++] = 0x80;
|
input[input_len++] = 0x80;
|
||||||
input_len += (sm_blocksize - (input_len%sm_blocksize));
|
input_len += (sm_blocksize - (input_len%sm_blocksize));
|
||||||
}
|
}
|
||||||
uint8_t signature[16];
|
uint8_t signature[16];
|
||||||
r = sm_sign(input, input_len, signature);
|
r = sm_sign(input, input_len, signature);
|
||||||
if (r != 0)
|
if (r != 0) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
if (memcmp(signature, mac, mac_len) == 0)
|
}
|
||||||
|
if (memcmp(signature, mac, mac_len) == 0) {
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
|
}
|
||||||
return CCID_VERIFICATION_FAILED;
|
return CCID_VERIFICATION_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sm_remove_padding(const uint8_t *data, size_t data_len) {
|
int sm_remove_padding(const uint8_t *data, size_t data_len)
|
||||||
|
{
|
||||||
int i = data_len-1;
|
int i = data_len-1;
|
||||||
for (; i >= 0 && data[i] == 0; i--);
|
for (; i >= 0 && data[i] == 0; i--) {
|
||||||
if (i < 0 || data[i] != 0x80)
|
;
|
||||||
|
}
|
||||||
|
if (i < 0 || data[i] != 0x80) {
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
220
src/fs/file.c
220
src/fs/file.c
@@ -27,7 +27,10 @@ extern const uintptr_t start_data_pool;
|
|||||||
extern const uintptr_t end_rom_pool;
|
extern const uintptr_t end_rom_pool;
|
||||||
extern const uintptr_t start_rom_pool;
|
extern const uintptr_t start_rom_pool;
|
||||||
extern int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len);
|
extern int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len);
|
||||||
extern int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t len, uint16_t offset);
|
extern int flash_write_data_to_file_offset(file_t *file,
|
||||||
|
const uint8_t *data,
|
||||||
|
uint16_t len,
|
||||||
|
uint16_t offset);
|
||||||
extern int flash_program_halfword(uintptr_t addr, uint16_t data);
|
extern int flash_program_halfword(uintptr_t addr, uint16_t data);
|
||||||
extern int flash_program_word(uintptr_t addr, uint32_t data);
|
extern int flash_program_word(uintptr_t addr, uint32_t data);
|
||||||
extern int flash_program_uintptr(uintptr_t addr, uintptr_t data);
|
extern int flash_program_uintptr(uintptr_t addr, uintptr_t data);
|
||||||
@@ -39,7 +42,8 @@ extern uint8_t *flash_read(uintptr_t addr);
|
|||||||
extern void low_flash_available();
|
extern void low_flash_available();
|
||||||
|
|
||||||
//puts FCI in the RAPDU
|
//puts FCI in the RAPDU
|
||||||
void process_fci(const file_t *pe, int fmd) {
|
void process_fci(const file_t *pe, int fmd)
|
||||||
|
{
|
||||||
res_APDU_size = 0;
|
res_APDU_size = 0;
|
||||||
if (fmd) {
|
if (fmd) {
|
||||||
res_APDU[res_APDU_size++] = 0x6f;
|
res_APDU[res_APDU_size++] = 0x6f;
|
||||||
@@ -56,14 +60,12 @@ void process_fci(const file_t *pe, int fmd) {
|
|||||||
uint16_t len = ((int (*)(const file_t *, int))(pe->data))(pe, 0);
|
uint16_t len = ((int (*)(const file_t *, int))(pe->data))(pe, 0);
|
||||||
res_APDU[res_APDU_size++] = (len >> 8) & 0xff;
|
res_APDU[res_APDU_size++] = (len >> 8) & 0xff;
|
||||||
res_APDU[res_APDU_size++] = len & 0xff;
|
res_APDU[res_APDU_size++] = len & 0xff;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
uint16_t v = file_get_size(pe);
|
uint16_t v = file_get_size(pe);
|
||||||
res_APDU[res_APDU_size++] = v >> 8;
|
res_APDU[res_APDU_size++] = v >> 8;
|
||||||
res_APDU[res_APDU_size++] = v & 0xff;
|
res_APDU[res_APDU_size++] = v & 0xff;
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
memset(res_APDU+res_APDU_size, 0, 2);
|
memset(res_APDU+res_APDU_size, 0, 2);
|
||||||
res_APDU_size += 2;
|
res_APDU_size += 2;
|
||||||
}
|
}
|
||||||
@@ -71,12 +73,13 @@ void process_fci(const file_t *pe, int fmd) {
|
|||||||
res_APDU[res_APDU_size++] = 0x82;
|
res_APDU[res_APDU_size++] = 0x82;
|
||||||
res_APDU[res_APDU_size++] = 1;
|
res_APDU[res_APDU_size++] = 1;
|
||||||
res_APDU[res_APDU_size] = 0;
|
res_APDU[res_APDU_size] = 0;
|
||||||
if (pe->type == FILE_TYPE_INTERNAL_EF)
|
if (pe->type == FILE_TYPE_INTERNAL_EF) {
|
||||||
res_APDU[res_APDU_size++] |= 0x08;
|
res_APDU[res_APDU_size++] |= 0x08;
|
||||||
else if (pe->type == FILE_TYPE_WORKING_EF)
|
} else if (pe->type == FILE_TYPE_WORKING_EF) {
|
||||||
res_APDU[res_APDU_size++] |= pe->ef_structure & 0x7;
|
res_APDU[res_APDU_size++] |= pe->ef_structure & 0x7;
|
||||||
else if (pe->type == FILE_TYPE_DF)
|
} else if (pe->type == FILE_TYPE_DF) {
|
||||||
res_APDU[res_APDU_size++] |= 0x38;
|
res_APDU[res_APDU_size++] |= 0x38;
|
||||||
|
}
|
||||||
|
|
||||||
res_APDU[res_APDU_size++] = 0x83;
|
res_APDU[res_APDU_size++] = 0x83;
|
||||||
res_APDU[res_APDU_size++] = 2;
|
res_APDU[res_APDU_size++] = 2;
|
||||||
@@ -100,9 +103,10 @@ void process_fci(const file_t *pe, int fmd) {
|
|||||||
res_APDU_size += meta_size;
|
res_APDU_size += meta_size;
|
||||||
}
|
}
|
||||||
res_APDU[1] = res_APDU_size-2;
|
res_APDU[1] = res_APDU_size-2;
|
||||||
if (fmd)
|
if (fmd) {
|
||||||
res_APDU[3] = res_APDU_size-4;
|
res_APDU[3] = res_APDU_size-4;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#define MAX_DYNAMIC_FILES 128
|
#define MAX_DYNAMIC_FILES 128
|
||||||
uint16_t dynamic_files = 0;
|
uint16_t dynamic_files = 0;
|
||||||
@@ -110,19 +114,24 @@ file_t dynamic_file[MAX_DYNAMIC_FILES];
|
|||||||
|
|
||||||
bool card_terminated = false;
|
bool card_terminated = false;
|
||||||
|
|
||||||
bool is_parent(const file_t *child, const file_t *parent) {
|
bool is_parent(const file_t *child, const file_t *parent)
|
||||||
if (child == parent)
|
{
|
||||||
|
if (child == parent) {
|
||||||
return true;
|
return true;
|
||||||
if (child == MF)
|
}
|
||||||
|
if (child == MF) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
return is_parent(&file_entries[child->parent], parent);
|
return is_parent(&file_entries[child->parent], parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_t *get_parent(file_t *f) {
|
file_t *get_parent(file_t *f)
|
||||||
|
{
|
||||||
return &file_entries[f->parent];
|
return &file_entries[f->parent];
|
||||||
}
|
}
|
||||||
|
|
||||||
file_t *search_by_name(uint8_t *name, uint16_t namelen) {
|
file_t *search_by_name(uint8_t *name, uint16_t namelen)
|
||||||
|
{
|
||||||
for (file_t *p = file_entries; p != file_last; p++) {
|
for (file_t *p = file_entries; p != file_last; p++) {
|
||||||
if (p->name && *p->name == apdu.nc && memcmp(p->name+1, name, namelen) == 0) {
|
if (p->name && *p->name == apdu.nc && memcmp(p->name+1, name, namelen) == 0) {
|
||||||
return p;
|
return p;
|
||||||
@@ -131,29 +140,37 @@ file_t *search_by_name(uint8_t *name, uint16_t namelen) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_t *search_by_fid(const uint16_t fid, const file_t *parent, const uint8_t sp) {
|
file_t *search_by_fid(const uint16_t fid, const file_t *parent, const uint8_t sp)
|
||||||
|
{
|
||||||
|
|
||||||
for (file_t *p = file_entries; p != file_last; p++) {
|
for (file_t *p = file_entries; p != file_last; p++) {
|
||||||
if (p->fid != 0x0000 && p->fid == fid) {
|
if (p->fid != 0x0000 && p->fid == fid) {
|
||||||
if (!parent || (parent && is_parent(p, parent))) {
|
if (!parent || (parent && is_parent(p, parent))) {
|
||||||
if (!sp || sp == SPECIFY_ANY || (((sp & SPECIFY_EF) && (p->type & FILE_TYPE_INTERNAL_EF)) || ((sp & SPECIFY_DF) && p->type == FILE_TYPE_DF)))
|
if (!sp || sp == SPECIFY_ANY ||
|
||||||
|
(((sp & SPECIFY_EF) && (p->type & FILE_TYPE_INTERNAL_EF)) ||
|
||||||
|
((sp & SPECIFY_DF) && p->type == FILE_TYPE_DF))) {
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t make_path_buf(const file_t *pe, uint8_t *buf, uint8_t buflen, const file_t *top) {
|
uint8_t make_path_buf(const file_t *pe, uint8_t *buf, uint8_t buflen, const file_t *top)
|
||||||
if (!buflen)
|
{
|
||||||
|
if (!buflen) {
|
||||||
return 0;
|
return 0;
|
||||||
if (pe == top) //MF or relative DF
|
}
|
||||||
|
if (pe == top) { //MF or relative DF
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
put_uint16_t(pe->fid, buf);
|
put_uint16_t(pe->fid, buf);
|
||||||
return make_path_buf(&file_entries[pe->parent], buf+2, buflen-2, top)+2;
|
return make_path_buf(&file_entries[pe->parent], buf+2, buflen-2, top)+2;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t make_path(const file_t *pe, const file_t *top, uint8_t *path) {
|
uint8_t make_path(const file_t *pe, const file_t *top, uint8_t *path)
|
||||||
|
{
|
||||||
uint8_t buf[MAX_DEPTH*2], *p = path;
|
uint8_t buf[MAX_DEPTH*2], *p = path;
|
||||||
put_uint16_t(pe->fid, buf);
|
put_uint16_t(pe->fid, buf);
|
||||||
uint8_t depth = make_path_buf(&file_entries[pe->parent], buf+2, sizeof(buf)-2, top)+2;
|
uint8_t depth = make_path_buf(&file_entries[pe->parent], buf+2, sizeof(buf)-2, top)+2;
|
||||||
@@ -164,16 +181,18 @@ uint8_t make_path(const file_t *pe, const file_t *top, uint8_t *path) {
|
|||||||
return depth;
|
return depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_t *search_by_path(const uint8_t *pe_path, uint8_t pathlen, const file_t *parent) {
|
file_t *search_by_path(const uint8_t *pe_path, uint8_t pathlen, const file_t *parent)
|
||||||
|
{
|
||||||
uint8_t path[MAX_DEPTH*2];
|
uint8_t path[MAX_DEPTH*2];
|
||||||
if (pathlen > sizeof(path)) {
|
if (pathlen > sizeof(path)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
for (file_t *p = file_entries; p != file_last; p++) {
|
for (file_t *p = file_entries; p != file_last; p++) {
|
||||||
uint8_t depth = make_path(p, parent, path);
|
uint8_t depth = make_path(p, parent, path);
|
||||||
if (pathlen == depth && memcmp(path, pe_path, depth) == 0)
|
if (pathlen == depth && memcmp(path, pe_path, depth) == 0) {
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,65 +201,73 @@ file_t *currentDF = NULL;
|
|||||||
const file_t *selected_applet = NULL;
|
const file_t *selected_applet = NULL;
|
||||||
bool isUserAuthenticated = false;
|
bool isUserAuthenticated = false;
|
||||||
|
|
||||||
bool authenticate_action(const file_t *ef, uint8_t op) {
|
bool authenticate_action(const file_t *ef, uint8_t op)
|
||||||
|
{
|
||||||
uint8_t acl = ef->acl[op];
|
uint8_t acl = ef->acl[op];
|
||||||
if (acl == 0x0)
|
if (acl == 0x0) {
|
||||||
return true;
|
return true;
|
||||||
else if (acl == 0xff)
|
} else if (acl == 0xff) {
|
||||||
return false;
|
return false;
|
||||||
else if (acl == 0x90 || (acl & 0x9F) == 0x10) {
|
} else if (acl == 0x90 || (acl & 0x9F) == 0x10) {
|
||||||
// PIN required.
|
// PIN required.
|
||||||
if (isUserAuthenticated) {
|
if (isUserAuthenticated) {
|
||||||
return true;
|
return true;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize_flash(bool hard) {
|
void initialize_flash(bool hard)
|
||||||
|
{
|
||||||
if (hard) {
|
if (hard) {
|
||||||
const uint8_t empty[8] = { 0 };
|
const uint8_t empty[8] = { 0 };
|
||||||
flash_program_block(end_data_pool, empty, sizeof(empty));
|
flash_program_block(end_data_pool, empty, sizeof(empty));
|
||||||
low_flash_available();
|
low_flash_available();
|
||||||
}
|
}
|
||||||
for (file_t *f = file_entries; f != file_last; f++) {
|
for (file_t *f = file_entries; f != file_last; f++) {
|
||||||
if ((f->type & FILE_DATA_FLASH) == FILE_DATA_FLASH)
|
if ((f->type & FILE_DATA_FLASH) == FILE_DATA_FLASH) {
|
||||||
f->data = NULL;
|
f->data = NULL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
dynamic_files = 0;
|
dynamic_files = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void scan_region(bool persistent) {
|
void scan_region(bool persistent)
|
||||||
|
{
|
||||||
uintptr_t endp = end_data_pool, startp = start_data_pool;
|
uintptr_t endp = end_data_pool, startp = start_data_pool;
|
||||||
if (persistent) {
|
if (persistent) {
|
||||||
endp = end_rom_pool;
|
endp = end_rom_pool;
|
||||||
startp = start_rom_pool;
|
startp = start_rom_pool;
|
||||||
}
|
}
|
||||||
for (uintptr_t base = flash_read_uintptr(endp); base >= startp; base = flash_read_uintptr(base)) {
|
for (uintptr_t base = flash_read_uintptr(endp); base >= startp;
|
||||||
if (base == 0x0) //all is empty
|
base = flash_read_uintptr(base)) {
|
||||||
|
if (base == 0x0) { //all is empty
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t fid = flash_read_uint16(base+sizeof(uintptr_t)+sizeof(uintptr_t));
|
uint16_t fid = flash_read_uint16(base+sizeof(uintptr_t)+sizeof(uintptr_t));
|
||||||
printf("[%x] scan fid %x, len %d\r\n",(unsigned int)base,fid,flash_read_uint16(base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t)));
|
printf("[%x] scan fid %x, len %d\r\n", (unsigned int) base, fid,
|
||||||
|
flash_read_uint16(base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t)));
|
||||||
file_t *file = (file_t *) search_by_fid(fid, NULL, SPECIFY_EF);
|
file_t *file = (file_t *) search_by_fid(fid, NULL, SPECIFY_EF);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
file = file_new(fid);
|
file = file_new(fid);
|
||||||
}
|
}
|
||||||
if (file)
|
if (file) {
|
||||||
file->data = (uint8_t *) (base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t));
|
file->data = (uint8_t *) (base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t));
|
||||||
|
}
|
||||||
if (flash_read_uintptr(base) == 0x0) {
|
if (flash_read_uintptr(base) == 0x0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void wait_flash_finish();
|
void wait_flash_finish();
|
||||||
void scan_flash() {
|
void scan_flash()
|
||||||
initialize_flash(false); //soft initialization
|
|
||||||
if (*(uintptr_t *)flash_read(end_rom_pool) == 0xffffffff && *(uintptr_t *)flash_read(end_rom_pool+sizeof(uintptr_t)) == 0xffffffff)
|
|
||||||
{
|
{
|
||||||
|
initialize_flash(false); //soft initialization
|
||||||
|
if (*(uintptr_t *) flash_read(end_rom_pool) == 0xffffffff &&
|
||||||
|
*(uintptr_t *) flash_read(end_rom_pool+sizeof(uintptr_t)) == 0xffffffff) {
|
||||||
printf("First initialization (or corrupted!)\r\n");
|
printf("First initialization (or corrupted!)\r\n");
|
||||||
uint8_t empty[sizeof(uintptr_t)*2+sizeof(uint32_t)];
|
uint8_t empty[sizeof(uintptr_t)*2+sizeof(uint32_t)];
|
||||||
memset(empty, 0, sizeof(empty));
|
memset(empty, 0, sizeof(empty));
|
||||||
@@ -254,43 +281,55 @@ void scan_flash() {
|
|||||||
scan_region(false);
|
scan_region(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *file_read(const uint8_t *addr) {
|
uint8_t *file_read(const uint8_t *addr)
|
||||||
|
{
|
||||||
return flash_read((uintptr_t) addr);
|
return flash_read((uintptr_t) addr);
|
||||||
}
|
}
|
||||||
uint16_t file_read_uint16(const uint8_t *addr) {
|
uint16_t file_read_uint16(const uint8_t *addr)
|
||||||
|
{
|
||||||
return flash_read_uint16((uintptr_t) addr);
|
return flash_read_uint16((uintptr_t) addr);
|
||||||
}
|
}
|
||||||
uint8_t file_read_uint8(const uint8_t *addr) {
|
uint8_t file_read_uint8(const uint8_t *addr)
|
||||||
|
{
|
||||||
return flash_read_uint8((uintptr_t) addr);
|
return flash_read_uint8((uintptr_t) addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *file_get_data(const file_t *tf) {
|
uint8_t *file_get_data(const file_t *tf)
|
||||||
if (!tf || !tf->data)
|
{
|
||||||
|
if (!tf || !tf->data) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
return file_read(tf->data+sizeof(uint16_t));
|
return file_read(tf->data+sizeof(uint16_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t file_get_size(const file_t *tf) {
|
uint16_t file_get_size(const file_t *tf)
|
||||||
if (!tf || !tf->data)
|
{
|
||||||
|
if (!tf || !tf->data) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
return file_read_uint16(tf->data);
|
return file_read_uint16(tf->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
file_t *search_dynamic_file(uint16_t fid) {
|
file_t *search_dynamic_file(uint16_t fid)
|
||||||
|
{
|
||||||
for (int i = 0; i < dynamic_files; i++) {
|
for (int i = 0; i < dynamic_files; i++) {
|
||||||
if (dynamic_file[i].fid == fid)
|
if (dynamic_file[i].fid == fid) {
|
||||||
return &dynamic_file[i];
|
return &dynamic_file[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delete_dynamic_file(file_t *f) {
|
int delete_dynamic_file(file_t *f)
|
||||||
if (f == NULL)
|
{
|
||||||
|
if (f == NULL) {
|
||||||
return CCID_ERR_FILE_NOT_FOUND;
|
return CCID_ERR_FILE_NOT_FOUND;
|
||||||
|
}
|
||||||
for (int i = 0; i < dynamic_files; i++) {
|
for (int i = 0; i < dynamic_files; i++) {
|
||||||
if (dynamic_file[i].fid == f->fid) {
|
if (dynamic_file[i].fid == f->fid) {
|
||||||
for (int j = i+1; j < dynamic_files; j++)
|
for (int j = i+1; j < dynamic_files; j++) {
|
||||||
memcpy(&dynamic_file[j-1], &dynamic_file[j], sizeof(file_t));
|
memcpy(&dynamic_file[j-1], &dynamic_file[j], sizeof(file_t));
|
||||||
|
}
|
||||||
dynamic_files--;
|
dynamic_files--;
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
@@ -298,12 +337,15 @@ int delete_dynamic_file(file_t *f) {
|
|||||||
return CCID_ERR_FILE_NOT_FOUND;
|
return CCID_ERR_FILE_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_t *file_new(uint16_t fid) {
|
file_t *file_new(uint16_t fid)
|
||||||
|
{
|
||||||
file_t *f;
|
file_t *f;
|
||||||
if ((f = search_dynamic_file(fid)) || (f = search_by_fid(fid, NULL, SPECIFY_EF)))
|
if ((f = search_dynamic_file(fid)) || (f = search_by_fid(fid, NULL, SPECIFY_EF))) {
|
||||||
return f;
|
return f;
|
||||||
if (dynamic_files == MAX_DYNAMIC_FILES)
|
}
|
||||||
|
if (dynamic_files == MAX_DYNAMIC_FILES) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
f = &dynamic_file[dynamic_files];
|
f = &dynamic_file[dynamic_files];
|
||||||
dynamic_files++;
|
dynamic_files++;
|
||||||
file_t file = {
|
file_t file = {
|
||||||
@@ -319,43 +361,50 @@ file_t *file_new(uint16_t fid) {
|
|||||||
//memset((uint8_t *)f->acl, 0x90, sizeof(f->acl));
|
//memset((uint8_t *)f->acl, 0x90, sizeof(f->acl));
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
int meta_find(uint16_t fid, uint8_t **out) {
|
int meta_find(uint16_t fid, uint8_t **out)
|
||||||
|
{
|
||||||
file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF);
|
file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF);
|
||||||
if (!ef)
|
if (!ef) {
|
||||||
return CCID_ERR_FILE_NOT_FOUND;
|
return CCID_ERR_FILE_NOT_FOUND;
|
||||||
|
}
|
||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL, *data = file_get_data(ef);
|
uint8_t *tag_data = NULL, *p = NULL, *data = file_get_data(ef);
|
||||||
size_t tag_len = 0, data_len = file_get_size(ef);
|
size_t tag_len = 0, data_len = file_get_size(ef);
|
||||||
while (walk_tlv(data, data_len, &p, &tag, &tag_len, &tag_data)) {
|
while (walk_tlv(data, data_len, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag_len < 2)
|
if (tag_len < 2) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
uint16_t cfid = (tag_data[0] << 8 | tag_data[1]);
|
uint16_t cfid = (tag_data[0] << 8 | tag_data[1]);
|
||||||
if (cfid == fid) {
|
if (cfid == fid) {
|
||||||
if (out)
|
if (out) {
|
||||||
*out = tag_data+2;
|
*out = tag_data+2;
|
||||||
|
}
|
||||||
return tag_len-2;
|
return tag_len-2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int meta_delete(uint16_t fid) {
|
int meta_delete(uint16_t fid)
|
||||||
|
{
|
||||||
file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF);
|
file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF);
|
||||||
if (!ef)
|
if (!ef) {
|
||||||
return CCID_ERR_FILE_NOT_FOUND;
|
return CCID_ERR_FILE_NOT_FOUND;
|
||||||
|
}
|
||||||
uint16_t tag = 0x0;
|
uint16_t tag = 0x0;
|
||||||
uint8_t *tag_data = NULL, *p = NULL, *data = file_get_data(ef);
|
uint8_t *tag_data = NULL, *p = NULL, *data = file_get_data(ef);
|
||||||
size_t tag_len = 0, data_len = file_get_size(ef);
|
size_t tag_len = 0, data_len = file_get_size(ef);
|
||||||
uint8_t *fdata = NULL;
|
uint8_t *fdata = NULL;
|
||||||
while (walk_tlv(data, data_len, &p, &tag, &tag_len, &tag_data)) {
|
while (walk_tlv(data, data_len, &p, &tag, &tag_len, &tag_data)) {
|
||||||
uint8_t *tpos = p-tag_len-format_tlv_len(tag_len, NULL)-1;
|
uint8_t *tpos = p-tag_len-format_tlv_len(tag_len, NULL)-1;
|
||||||
if (tag_len < 2)
|
if (tag_len < 2) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
uint16_t cfid = (tag_data[0] << 8 | tag_data[1]);
|
uint16_t cfid = (tag_data[0] << 8 | tag_data[1]);
|
||||||
if (cfid == fid) {
|
if (cfid == fid) {
|
||||||
size_t new_len = data_len-1-tag_len-format_tlv_len(tag_len, NULL);
|
size_t new_len = data_len-1-tag_len-format_tlv_len(tag_len, NULL);
|
||||||
if (new_len == 0)
|
if (new_len == 0) {
|
||||||
flash_clear_file(ef);
|
flash_clear_file(ef);
|
||||||
else {
|
} else {
|
||||||
fdata = (uint8_t *) calloc(1, new_len);
|
fdata = (uint8_t *) calloc(1, new_len);
|
||||||
if (tpos > data) {
|
if (tpos > data) {
|
||||||
memcpy(fdata, data, tpos-data);
|
memcpy(fdata, data, tpos-data);
|
||||||
@@ -365,20 +414,23 @@ int meta_delete(uint16_t fid) {
|
|||||||
}
|
}
|
||||||
int r = flash_write_data_to_file(ef, fdata, new_len);
|
int r = flash_write_data_to_file(ef, fdata, new_len);
|
||||||
free(fdata);
|
free(fdata);
|
||||||
if (r != CCID_OK)
|
if (r != CCID_OK) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
low_flash_available();
|
low_flash_available();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
int meta_add(uint16_t fid, const uint8_t *data, uint16_t len)
|
||||||
|
{
|
||||||
int r;
|
int r;
|
||||||
file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF);
|
file_t *ef = search_by_fid(EF_META, NULL, SPECIFY_EF);
|
||||||
if (!ef)
|
if (!ef) {
|
||||||
return CCID_ERR_FILE_NOT_FOUND;
|
return CCID_ERR_FILE_NOT_FOUND;
|
||||||
|
}
|
||||||
uint16_t ef_size = file_get_size(ef);
|
uint16_t ef_size = file_get_size(ef);
|
||||||
uint8_t *fdata = (uint8_t *) calloc(1, ef_size);
|
uint8_t *fdata = (uint8_t *) calloc(1, ef_size);
|
||||||
memcpy(fdata, file_get_data(ef), ef_size);
|
memcpy(fdata, file_get_data(ef), ef_size);
|
||||||
@@ -386,19 +438,20 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
uint8_t *tag_data = NULL, *p = NULL;
|
uint8_t *tag_data = NULL, *p = NULL;
|
||||||
size_t tag_len = 0;
|
size_t tag_len = 0;
|
||||||
while (walk_tlv(fdata, ef_size, &p, &tag, &tag_len, &tag_data)) {
|
while (walk_tlv(fdata, ef_size, &p, &tag, &tag_len, &tag_data)) {
|
||||||
if (tag_len < 2)
|
if (tag_len < 2) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
uint16_t cfid = (tag_data[0] << 8 | tag_data[1]);
|
uint16_t cfid = (tag_data[0] << 8 | tag_data[1]);
|
||||||
if (cfid == fid) {
|
if (cfid == fid) {
|
||||||
if (tag_len-2 == len) { //an update
|
if (tag_len-2 == len) { //an update
|
||||||
memcpy(p-tag_len+2, data, len);
|
memcpy(p-tag_len+2, data, len);
|
||||||
r = flash_write_data_to_file(ef, fdata, ef_size);
|
r = flash_write_data_to_file(ef, fdata, ef_size);
|
||||||
free(fdata);
|
free(fdata);
|
||||||
if (r != CCID_OK)
|
if (r != CCID_OK) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
return CCID_OK;
|
|
||||||
}
|
}
|
||||||
else { //needs reallocation
|
return CCID_OK;
|
||||||
|
} else { //needs reallocation
|
||||||
uint8_t *tpos = p-asn1_len_tag(tag, tag_len);
|
uint8_t *tpos = p-asn1_len_tag(tag, tag_len);
|
||||||
memmove(tpos, p, fdata+ef_size-p);
|
memmove(tpos, p, fdata+ef_size-p);
|
||||||
tpos += fdata+ef_size-p;
|
tpos += fdata+ef_size-p;
|
||||||
@@ -406,9 +459,9 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
ef_size += len - (tag_len-2);
|
ef_size += len - (tag_len-2);
|
||||||
if (len > tag_len-2) {
|
if (len > tag_len-2) {
|
||||||
uint8_t *fdata_new = (uint8_t *) realloc(fdata, ef_size);
|
uint8_t *fdata_new = (uint8_t *) realloc(fdata, ef_size);
|
||||||
if (fdata_new != NULL)
|
if (fdata_new != NULL) {
|
||||||
fdata = fdata_new;
|
fdata = fdata_new;
|
||||||
else {
|
} else {
|
||||||
free(fdata);
|
free(fdata);
|
||||||
return CCID_ERR_MEMORY_FATAL;
|
return CCID_ERR_MEMORY_FATAL;
|
||||||
}
|
}
|
||||||
@@ -421,8 +474,9 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
memcpy(f, data, len);
|
memcpy(f, data, len);
|
||||||
r = flash_write_data_to_file(ef, fdata, ef_size);
|
r = flash_write_data_to_file(ef, fdata, ef_size);
|
||||||
free(fdata);
|
free(fdata);
|
||||||
if (r != CCID_OK)
|
if (r != CCID_OK) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
|
}
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -436,23 +490,29 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) {
|
|||||||
memcpy(f, data, len);
|
memcpy(f, data, len);
|
||||||
r = flash_write_data_to_file(ef, fdata, ef_size+asn1_len_tag(fid & 0x1f, len+2));
|
r = flash_write_data_to_file(ef, fdata, ef_size+asn1_len_tag(fid & 0x1f, len+2));
|
||||||
free(fdata);
|
free(fdata);
|
||||||
if (r != CCID_OK)
|
if (r != CCID_OK) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
|
}
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool file_has_data(file_t *f) {
|
bool file_has_data(file_t *f)
|
||||||
return (f != NULL && f->data != NULL && file_get_size(f) > 0);
|
{
|
||||||
|
return f != NULL && f->data != NULL && file_get_size(f) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int delete_file(file_t *ef) {
|
int delete_file(file_t *ef)
|
||||||
if (ef == NULL)
|
{
|
||||||
|
if (ef == NULL) {
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
|
}
|
||||||
meta_delete(ef->fid);
|
meta_delete(ef->fid);
|
||||||
if (flash_clear_file(ef) != CCID_OK)
|
if (flash_clear_file(ef) != CCID_OK) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
if (delete_dynamic_file(ef) != CCID_OK)
|
}
|
||||||
|
if (delete_dynamic_file(ef) != CCID_OK) {
|
||||||
return CCID_EXEC_ERROR;
|
return CCID_EXEC_ERROR;
|
||||||
|
}
|
||||||
low_flash_available();
|
low_flash_available();
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,8 +68,7 @@
|
|||||||
|
|
||||||
#define MAX_DEPTH 4
|
#define MAX_DEPTH 4
|
||||||
|
|
||||||
typedef struct file
|
typedef struct file {
|
||||||
{
|
|
||||||
const uint16_t fid;
|
const uint16_t fid;
|
||||||
const uint8_t parent; //entry number in the whole table!!
|
const uint8_t parent; //entry number in the whole table!!
|
||||||
const uint8_t *name;
|
const uint8_t *name;
|
||||||
@@ -126,4 +125,3 @@ extern int meta_add(uint16_t fid, const uint8_t *data, uint16_t len);
|
|||||||
extern int delete_file(file_t *ef);
|
extern int delete_file(file_t *ef);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -45,9 +45,11 @@
|
|||||||
//To avoid possible future allocations, data region starts at the end of flash and goes upwards to the center region
|
//To avoid possible future allocations, data region starts at the end of flash and goes upwards to the center region
|
||||||
|
|
||||||
const uintptr_t start_data_pool = (XIP_BASE + FLASH_TARGET_OFFSET);
|
const uintptr_t start_data_pool = (XIP_BASE + FLASH_TARGET_OFFSET);
|
||||||
const uintptr_t end_data_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-FLASH_PERMANENT_REGION-FLASH_DATA_HEADER_SIZE-4; //This is a fixed value. DO NOT CHANGE
|
const uintptr_t end_data_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-
|
||||||
|
FLASH_PERMANENT_REGION-FLASH_DATA_HEADER_SIZE-4; //This is a fixed value. DO NOT CHANGE
|
||||||
const uintptr_t end_rom_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-4; //This is a fixed value. DO NOT CHANGE
|
const uintptr_t end_rom_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-4; //This is a fixed value. DO NOT CHANGE
|
||||||
const uintptr_t start_rom_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-FLASH_PERMANENT_REGION; //This is a fixed value. DO NOT CHANGE
|
const uintptr_t start_rom_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE-
|
||||||
|
FLASH_PERMANENT_REGION; //This is a fixed value. DO NOT CHANGE
|
||||||
|
|
||||||
extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len);
|
extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len);
|
||||||
extern int flash_program_halfword(uintptr_t addr, uint16_t data);
|
extern int flash_program_halfword(uintptr_t addr, uint16_t data);
|
||||||
@@ -58,9 +60,11 @@ extern uint8_t *flash_read(uintptr_t addr);
|
|||||||
|
|
||||||
extern void low_flash_available();
|
extern void low_flash_available();
|
||||||
|
|
||||||
uintptr_t allocate_free_addr(uint16_t size, bool persistent) {
|
uintptr_t allocate_free_addr(uint16_t size, bool persistent)
|
||||||
if (size > FLASH_SECTOR_SIZE)
|
{
|
||||||
|
if (size > FLASH_SECTOR_SIZE) {
|
||||||
return 0x0; //ERROR
|
return 0x0; //ERROR
|
||||||
|
}
|
||||||
size_t real_size = size+sizeof(uint16_t)+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //len+len size+next address+fid+prev_addr size
|
size_t real_size = size+sizeof(uint16_t)+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //len+len size+next address+fid+prev_addr size
|
||||||
uintptr_t next_base = 0x0, endp = end_data_pool, startp = start_data_pool;
|
uintptr_t next_base = 0x0, endp = end_data_pool, startp = start_data_pool;
|
||||||
if (persistent) {
|
if (persistent) {
|
||||||
@@ -75,14 +79,12 @@ uintptr_t allocate_free_addr(uint16_t size, bool persistent) {
|
|||||||
//printf("fid %x\r\n",flash_read_uint16(next_base+sizeof(uintptr_t)));
|
//printf("fid %x\r\n",flash_read_uint16(next_base+sizeof(uintptr_t)));
|
||||||
if (next_base == 0x0) { //we are at the end
|
if (next_base == 0x0) { //we are at the end
|
||||||
//now we check if we fit in the current sector
|
//now we check if we fit in the current sector
|
||||||
if (addr_alg <= potential_addr) //it fits in the current sector
|
if (addr_alg <= potential_addr) { //it fits in the current sector
|
||||||
{
|
|
||||||
flash_program_uintptr(potential_addr, 0x0);
|
flash_program_uintptr(potential_addr, 0x0);
|
||||||
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
||||||
flash_program_uintptr(base, potential_addr);
|
flash_program_uintptr(base, potential_addr);
|
||||||
return potential_addr;
|
return potential_addr;
|
||||||
}
|
} else if (addr_alg-FLASH_SECTOR_SIZE >= startp) { //check whether it fits in the next sector, so we take addr_aligned as the base
|
||||||
else if (addr_alg-FLASH_SECTOR_SIZE >= startp) { //check whether it fits in the next sector, so we take addr_aligned as the base
|
|
||||||
potential_addr = addr_alg-real_size;
|
potential_addr = addr_alg-real_size;
|
||||||
flash_program_uintptr(potential_addr, 0x0);
|
flash_program_uintptr(potential_addr, 0x0);
|
||||||
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
||||||
@@ -92,7 +94,13 @@ uintptr_t allocate_free_addr(uint16_t size, bool persistent) {
|
|||||||
return 0x0;
|
return 0x0;
|
||||||
}
|
}
|
||||||
//we check if |base-(next_addr+size_next_addr)| > |base-potential_addr| only if fid != 1xxx (not size blocked)
|
//we check if |base-(next_addr+size_next_addr)| > |base-potential_addr| only if fid != 1xxx (not size blocked)
|
||||||
else if (addr_alg <= potential_addr && base-(next_base+flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t))+2*sizeof(uint16_t)+2*sizeof(uintptr_t)) > base-potential_addr && (flash_read_uint16(next_base+2*sizeof(uintptr_t)) & 0x1000) != 0x1000) {
|
else if (addr_alg <= potential_addr &&
|
||||||
|
base-
|
||||||
|
(next_base+
|
||||||
|
flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t))+
|
||||||
|
2*
|
||||||
|
sizeof(uint16_t)+2*sizeof(uintptr_t)) > base-potential_addr &&
|
||||||
|
(flash_read_uint16(next_base+2*sizeof(uintptr_t)) & 0x1000) != 0x1000) {
|
||||||
flash_program_uintptr(potential_addr, next_base);
|
flash_program_uintptr(potential_addr, next_base);
|
||||||
flash_program_uintptr(next_base+sizeof(uintptr_t), potential_addr);
|
flash_program_uintptr(next_base+sizeof(uintptr_t), potential_addr);
|
||||||
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
||||||
@@ -103,17 +111,21 @@ uintptr_t allocate_free_addr(uint16_t size, bool persistent) {
|
|||||||
return 0x0; //probably never reached
|
return 0x0; //probably never reached
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_clear_file(file_t *file) {
|
int flash_clear_file(file_t *file)
|
||||||
if (file == NULL)
|
{
|
||||||
|
if (file == NULL) {
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
uintptr_t base_addr = (uintptr_t)(file->data-sizeof(uintptr_t)-sizeof(uint16_t)-sizeof(uintptr_t));
|
}
|
||||||
|
uintptr_t base_addr =
|
||||||
|
(uintptr_t) (file->data-sizeof(uintptr_t)-sizeof(uint16_t)-sizeof(uintptr_t));
|
||||||
uintptr_t prev_addr = flash_read_uintptr(base_addr+sizeof(uintptr_t));
|
uintptr_t prev_addr = flash_read_uintptr(base_addr+sizeof(uintptr_t));
|
||||||
uintptr_t next_addr = flash_read_uintptr(base_addr);
|
uintptr_t next_addr = flash_read_uintptr(base_addr);
|
||||||
//printf("nc %lx->%lx %lx->%lx\r\n",prev_addr,flash_read_uintptr(prev_addr),base_addr,next_addr);
|
//printf("nc %lx->%lx %lx->%lx\r\n",prev_addr,flash_read_uintptr(prev_addr),base_addr,next_addr);
|
||||||
flash_program_uintptr(prev_addr, next_addr);
|
flash_program_uintptr(prev_addr, next_addr);
|
||||||
flash_program_halfword((uintptr_t) file->data, 0);
|
flash_program_halfword((uintptr_t) file->data, 0);
|
||||||
if (next_addr > 0)
|
if (next_addr > 0) {
|
||||||
flash_program_uintptr(next_addr+sizeof(uintptr_t), prev_addr);
|
flash_program_uintptr(next_addr+sizeof(uintptr_t), prev_addr);
|
||||||
|
}
|
||||||
flash_program_uintptr(base_addr, 0);
|
flash_program_uintptr(base_addr, 0);
|
||||||
flash_program_uintptr(base_addr+sizeof(uintptr_t), 0);
|
flash_program_uintptr(base_addr+sizeof(uintptr_t), 0);
|
||||||
file->data = NULL;
|
file->data = NULL;
|
||||||
@@ -121,21 +133,25 @@ int flash_clear_file(file_t *file) {
|
|||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t len, uint16_t offset) {
|
int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t len,
|
||||||
if (!file)
|
uint16_t offset)
|
||||||
|
{
|
||||||
|
if (!file) {
|
||||||
return CCID_ERR_NULL_PARAM;
|
return CCID_ERR_NULL_PARAM;
|
||||||
|
}
|
||||||
uint16_t size_file_flash = file->data ? flash_read_uint16((uintptr_t) file->data) : 0;
|
uint16_t size_file_flash = file->data ? flash_read_uint16((uintptr_t) file->data) : 0;
|
||||||
uint8_t *old_data = NULL;
|
uint8_t *old_data = NULL;
|
||||||
if (offset+len > FLASH_SECTOR_SIZE || offset > size_file_flash)
|
if (offset+len > FLASH_SECTOR_SIZE || offset > size_file_flash) {
|
||||||
return CCID_ERR_NO_MEMORY;
|
return CCID_ERR_NO_MEMORY;
|
||||||
|
}
|
||||||
if (file->data) { //already in flash
|
if (file->data) { //already in flash
|
||||||
if (offset+len <= size_file_flash) { //it fits, no need to move it
|
if (offset+len <= size_file_flash) { //it fits, no need to move it
|
||||||
flash_program_halfword((uintptr_t) file->data, offset+len);
|
flash_program_halfword((uintptr_t) file->data, offset+len);
|
||||||
if (data)
|
if (data) {
|
||||||
flash_program_block((uintptr_t) file->data+sizeof(uint16_t)+offset, data, len);
|
flash_program_block((uintptr_t) file->data+sizeof(uint16_t)+offset, data, len);
|
||||||
return CCID_OK;
|
|
||||||
}
|
}
|
||||||
else { //we clear the old file
|
return CCID_OK;
|
||||||
|
} else { //we clear the old file
|
||||||
flash_clear_file(file);
|
flash_clear_file(file);
|
||||||
if (offset > 0) {
|
if (offset > 0) {
|
||||||
old_data = (uint8_t *) calloc(1, offset+len);
|
old_data = (uint8_t *) calloc(1, offset+len);
|
||||||
@@ -149,17 +165,21 @@ int flash_write_data_to_file_offset(file_t *file, const uint8_t *data, uint16_t
|
|||||||
|
|
||||||
uintptr_t new_addr = allocate_free_addr(len, (file->type & FILE_PERSISTENT) == FILE_PERSISTENT);
|
uintptr_t new_addr = allocate_free_addr(len, (file->type & FILE_PERSISTENT) == FILE_PERSISTENT);
|
||||||
//printf("na %x\r\n",new_addr);
|
//printf("na %x\r\n",new_addr);
|
||||||
if (new_addr == 0x0)
|
if (new_addr == 0x0) {
|
||||||
return CCID_ERR_NO_MEMORY;
|
return CCID_ERR_NO_MEMORY;
|
||||||
|
}
|
||||||
file->data = (uint8_t *) new_addr+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //next addr+fid+prev addr
|
file->data = (uint8_t *) new_addr+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //next addr+fid+prev addr
|
||||||
flash_program_halfword(new_addr+sizeof(uintptr_t)+sizeof(uintptr_t), file->fid);
|
flash_program_halfword(new_addr+sizeof(uintptr_t)+sizeof(uintptr_t), file->fid);
|
||||||
flash_program_halfword((uintptr_t) file->data, len);
|
flash_program_halfword((uintptr_t) file->data, len);
|
||||||
if (data)
|
if (data) {
|
||||||
flash_program_block((uintptr_t) file->data+sizeof(uint16_t), data, len);
|
flash_program_block((uintptr_t) file->data+sizeof(uint16_t), data, len);
|
||||||
if (old_data)
|
}
|
||||||
|
if (old_data) {
|
||||||
free(old_data);
|
free(old_data);
|
||||||
|
}
|
||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) {
|
int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len)
|
||||||
|
{
|
||||||
return flash_write_data_to_file_offset(file, data, len, 0);
|
return flash_write_data_to_file_offset(file, data, len, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,8 @@ bool flash_available = false;
|
|||||||
|
|
||||||
|
|
||||||
//this function has to be called from the core 0
|
//this function has to be called from the core 0
|
||||||
void do_flash() {
|
void do_flash()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
if (mutex_try_enter(&mtx_flash, NULL) == true) {
|
if (mutex_try_enter(&mtx_flash, NULL) == true) {
|
||||||
#endif
|
#endif
|
||||||
@@ -81,26 +82,38 @@ void do_flash() {
|
|||||||
if (flash_pages[r].ready == true) {
|
if (flash_pages[r].ready == true) {
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
||||||
while (multicore_lockout_start_timeout_us(1000) == false);
|
while (multicore_lockout_start_timeout_us(1000) == false) {
|
||||||
|
;
|
||||||
|
}
|
||||||
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
||||||
uint32_t ints = save_and_disable_interrupts();
|
uint32_t ints = save_and_disable_interrupts();
|
||||||
flash_range_erase(flash_pages[r].address-XIP_BASE, FLASH_SECTOR_SIZE);
|
flash_range_erase(flash_pages[r].address-XIP_BASE, FLASH_SECTOR_SIZE);
|
||||||
flash_range_program(flash_pages[r].address-XIP_BASE, flash_pages[r].page, FLASH_SECTOR_SIZE);
|
flash_range_program(flash_pages[r].address-XIP_BASE,
|
||||||
|
flash_pages[r].page,
|
||||||
|
FLASH_SECTOR_SIZE);
|
||||||
restore_interrupts(ints);
|
restore_interrupts(ints);
|
||||||
while (multicore_lockout_end_timeout_us(1000) == false);
|
while (multicore_lockout_end_timeout_us(1000) == false) {
|
||||||
|
;
|
||||||
|
}
|
||||||
//printf("WRITEN %X !\r\n",flash_pages[r].address);
|
//printf("WRITEN %X !\r\n",flash_pages[r].address);
|
||||||
#else
|
#else
|
||||||
memcpy(map + flash_pages[r].address, flash_pages[r].page, FLASH_SECTOR_SIZE);
|
memcpy(map + flash_pages[r].address, flash_pages[r].page, FLASH_SECTOR_SIZE);
|
||||||
#endif
|
#endif
|
||||||
flash_pages[r].ready = false;
|
flash_pages[r].ready = false;
|
||||||
ready_pages--;
|
ready_pages--;
|
||||||
}
|
} else if (flash_pages[r].erase == true) {
|
||||||
else if (flash_pages[r].erase == true) {
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
while (multicore_lockout_start_timeout_us(1000) == false);
|
while (multicore_lockout_start_timeout_us(1000) == false) {
|
||||||
|
;
|
||||||
|
}
|
||||||
//printf("WRITTING\r\n");
|
//printf("WRITTING\r\n");
|
||||||
flash_range_erase(flash_pages[r].address-XIP_BASE, flash_pages[r].page_size ? ((int)(flash_pages[r].page_size/FLASH_SECTOR_SIZE))*FLASH_SECTOR_SIZE : FLASH_SECTOR_SIZE);
|
flash_range_erase(flash_pages[r].address-XIP_BASE,
|
||||||
while (multicore_lockout_end_timeout_us(1000) == false);
|
flash_pages[r].page_size ? ((int) (flash_pages[r].page_size/
|
||||||
|
FLASH_SECTOR_SIZE))*
|
||||||
|
FLASH_SECTOR_SIZE : FLASH_SECTOR_SIZE);
|
||||||
|
while (multicore_lockout_end_timeout_us(1000) == false) {
|
||||||
|
;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
memset(map + flash_pages[r].address, 0, FLASH_SECTOR_SIZE);
|
memset(map + flash_pages[r].address, 0, FLASH_SECTOR_SIZE);
|
||||||
#endif
|
#endif
|
||||||
@@ -124,7 +137,8 @@ void do_flash() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//this function has to be called from the core 0
|
//this function has to be called from the core 0
|
||||||
void low_flash_init() {
|
void low_flash_init()
|
||||||
|
{
|
||||||
memset(flash_pages, 0, sizeof(page_flash_t)*TOTAL_FLASH_PAGES);
|
memset(flash_pages, 0, sizeof(page_flash_t)*TOTAL_FLASH_PAGES);
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mutex_init(&mtx_flash);
|
mutex_init(&mtx_flash);
|
||||||
@@ -137,7 +151,8 @@ void low_flash_init() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void low_flash_init_core1() {
|
void low_flash_init_core1()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mutex_enter_blocking(&mtx_flash);
|
mutex_enter_blocking(&mtx_flash);
|
||||||
multicore_lockout_victim_init();
|
multicore_lockout_victim_init();
|
||||||
@@ -148,7 +163,8 @@ void low_flash_init_core1() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void wait_flash_finish() {
|
void wait_flash_finish()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
sem_acquire_blocking(&sem_wait); //blocks until released
|
sem_acquire_blocking(&sem_wait); //blocks until released
|
||||||
//wake up
|
//wake up
|
||||||
@@ -156,7 +172,8 @@ void wait_flash_finish() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void low_flash_available() {
|
void low_flash_available()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mutex_enter_blocking(&mtx_flash);
|
mutex_enter_blocking(&mtx_flash);
|
||||||
#endif
|
#endif
|
||||||
@@ -166,20 +183,22 @@ void low_flash_available() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
page_flash_t *find_free_page(uintptr_t addr) {
|
page_flash_t *find_free_page(uintptr_t addr)
|
||||||
|
{
|
||||||
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
||||||
page_flash_t *p = NULL;
|
page_flash_t *p = NULL;
|
||||||
for (int r = 0; r < TOTAL_FLASH_PAGES; r++)
|
for (int r = 0; r < TOTAL_FLASH_PAGES; r++) {
|
||||||
{
|
if ((!flash_pages[r].ready && !flash_pages[r].erase) ||
|
||||||
if ((!flash_pages[r].ready && !flash_pages[r].erase) || flash_pages[r].address == addr_alg) //first available
|
flash_pages[r].address == addr_alg) { //first available
|
||||||
{
|
|
||||||
p = &flash_pages[r];
|
p = &flash_pages[r];
|
||||||
if (!flash_pages[r].ready && !flash_pages[r].erase)
|
if (!flash_pages[r].ready && !flash_pages[r].erase) {
|
||||||
{
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
memcpy(p->page, (uint8_t *) addr_alg, FLASH_SECTOR_SIZE);
|
memcpy(p->page, (uint8_t *) addr_alg, FLASH_SECTOR_SIZE);
|
||||||
#else
|
#else
|
||||||
memcpy(p->page, (addr >= start_data_pool && addr <= end_rom_pool) ? (uint8_t *)(map+addr_alg) : (uint8_t *)addr_alg, FLASH_SECTOR_SIZE);
|
memcpy(p->page,
|
||||||
|
(addr >= start_data_pool &&
|
||||||
|
addr <= end_rom_pool) ? (uint8_t *) (map+addr_alg) : (uint8_t *) addr_alg,
|
||||||
|
FLASH_SECTOR_SIZE);
|
||||||
#endif
|
#endif
|
||||||
ready_pages++;
|
ready_pages++;
|
||||||
p->address = addr_alg;
|
p->address = addr_alg;
|
||||||
@@ -191,11 +210,13 @@ page_flash_t *find_free_page(uintptr_t addr) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) {
|
int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len)
|
||||||
|
{
|
||||||
page_flash_t *p = NULL;
|
page_flash_t *p = NULL;
|
||||||
|
|
||||||
if (!data || len == 0)
|
if (!data || len == 0) {
|
||||||
return CCID_ERR_NULL_PARAM;
|
return CCID_ERR_NULL_PARAM;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mutex_enter_blocking(&mtx_flash);
|
mutex_enter_blocking(&mtx_flash);
|
||||||
@@ -207,8 +228,7 @@ int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) {
|
|||||||
printf("ERROR: ALL FLASH PAGES CACHED\r\n");
|
printf("ERROR: ALL FLASH PAGES CACHED\r\n");
|
||||||
return CCID_ERR_NO_MEMORY;
|
return CCID_ERR_NO_MEMORY;
|
||||||
}
|
}
|
||||||
if (!(p = find_free_page(addr)))
|
if (!(p = find_free_page(addr))) {
|
||||||
{
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mutex_exit(&mtx_flash);
|
mutex_exit(&mtx_flash);
|
||||||
#endif
|
#endif
|
||||||
@@ -223,19 +243,23 @@ int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) {
|
|||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_program_halfword (uintptr_t addr, uint16_t data) {
|
int flash_program_halfword(uintptr_t addr, uint16_t data)
|
||||||
|
{
|
||||||
return flash_program_block(addr, (const uint8_t *) &data, sizeof(uint16_t));
|
return flash_program_block(addr, (const uint8_t *) &data, sizeof(uint16_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_program_word (uintptr_t addr, uint32_t data) {
|
int flash_program_word(uintptr_t addr, uint32_t data)
|
||||||
|
{
|
||||||
return flash_program_block(addr, (const uint8_t *) &data, sizeof(uint32_t));
|
return flash_program_block(addr, (const uint8_t *) &data, sizeof(uint32_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_program_uintptr (uintptr_t addr, uintptr_t data) {
|
int flash_program_uintptr(uintptr_t addr, uintptr_t data)
|
||||||
|
{
|
||||||
return flash_program_block(addr, (const uint8_t *) &data, sizeof(uintptr_t));
|
return flash_program_block(addr, (const uint8_t *) &data, sizeof(uintptr_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *flash_read(uintptr_t addr) {
|
uint8_t *flash_read(uintptr_t addr)
|
||||||
|
{
|
||||||
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mutex_enter_blocking(&mtx_flash);
|
mutex_enter_blocking(&mtx_flash);
|
||||||
@@ -255,13 +279,15 @@ uint8_t *flash_read(uintptr_t addr) {
|
|||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
mutex_exit(&mtx_flash);
|
mutex_exit(&mtx_flash);
|
||||||
#else
|
#else
|
||||||
if (addr >= start_data_pool && addr <= end_rom_pool)
|
if (addr >= start_data_pool && addr <= end_rom_pool) {
|
||||||
v += (uintptr_t) map;
|
v += (uintptr_t) map;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
uintptr_t flash_read_uintptr(uintptr_t addr) {
|
uintptr_t flash_read_uintptr(uintptr_t addr)
|
||||||
|
{
|
||||||
uint8_t *p = flash_read(addr);
|
uint8_t *p = flash_read(addr);
|
||||||
uintptr_t v = 0x0;
|
uintptr_t v = 0x0;
|
||||||
for (int i = 0; i < sizeof(uintptr_t); i++) {
|
for (int i = 0; i < sizeof(uintptr_t); i++) {
|
||||||
@@ -269,7 +295,8 @@ uintptr_t flash_read_uintptr(uintptr_t addr) {
|
|||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
uint16_t flash_read_uint16(uintptr_t addr) {
|
uint16_t flash_read_uint16(uintptr_t addr)
|
||||||
|
{
|
||||||
uint8_t *p = flash_read(addr);
|
uint8_t *p = flash_read(addr);
|
||||||
uint16_t v = 0x0;
|
uint16_t v = 0x0;
|
||||||
for (int i = 0; i < sizeof(uint16_t); i++) {
|
for (int i = 0; i < sizeof(uint16_t); i++) {
|
||||||
@@ -277,11 +304,13 @@ uint16_t flash_read_uint16(uintptr_t addr) {
|
|||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
uint8_t flash_read_uint8(uintptr_t addr) {
|
uint8_t flash_read_uint8(uintptr_t addr)
|
||||||
|
{
|
||||||
return *flash_read(addr);
|
return *flash_read(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int flash_erase_page (uintptr_t addr, size_t page_size) {
|
int flash_erase_page(uintptr_t addr, size_t page_size)
|
||||||
|
{
|
||||||
page_flash_t *p = NULL;
|
page_flash_t *p = NULL;
|
||||||
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
@@ -316,8 +345,9 @@ bool flash_check_blank(const uint8_t *p_start, size_t size)
|
|||||||
const uint8_t *p;
|
const uint8_t *p;
|
||||||
|
|
||||||
for (p = p_start; p < p_start + size; p++) {
|
for (p = p_start; p < p_start + size; p++) {
|
||||||
if (*p != 0xff)
|
if (*p != 0xff) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,13 +39,16 @@ extern bool wait_button();
|
|||||||
|
|
||||||
extern void low_flash_init_core1();
|
extern void low_flash_init_core1();
|
||||||
|
|
||||||
static inline const uint16_t make_uint16_t(uint8_t b1, uint8_t b2) {
|
static inline const uint16_t make_uint16_t(uint8_t b1, uint8_t b2)
|
||||||
|
{
|
||||||
return (b1 << 8) | b2;
|
return (b1 << 8) | b2;
|
||||||
}
|
}
|
||||||
static inline const uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) {
|
static inline const uint16_t get_uint16_t(const uint8_t *b, uint16_t offset)
|
||||||
|
{
|
||||||
return make_uint16_t(b[offset], b[offset+1]);
|
return make_uint16_t(b[offset], b[offset+1]);
|
||||||
}
|
}
|
||||||
static inline void put_uint16_t(uint16_t n, uint8_t *b) {
|
static inline void put_uint16_t(uint16_t n, uint8_t *b)
|
||||||
|
{
|
||||||
*b++ = (n >> 8) & 0xff;
|
*b++ = (n >> 8) & 0xff;
|
||||||
*b = n & 0xff;
|
*b = n & 0xff;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,4 +24,3 @@
|
|||||||
#define HSM_SDK_VERSION_MINOR (HSM_SDK_VERSION & 0xff)
|
#define HSM_SDK_VERSION_MINOR (HSM_SDK_VERSION & 0xff)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
41
src/main.c
41
src/main.c
@@ -62,7 +62,8 @@ app_t *current_app = NULL;
|
|||||||
|
|
||||||
const uint8_t *ccid_atr = NULL;
|
const uint8_t *ccid_atr = NULL;
|
||||||
|
|
||||||
int register_app(app_t * (*select_aid)(app_t *, const uint8_t *, uint8_t)) {
|
int register_app(app_t *(*select_aid)(app_t *, const uint8_t *, uint8_t))
|
||||||
|
{
|
||||||
if (num_apps < sizeof(apps)/sizeof(app_t)) {
|
if (num_apps < sizeof(apps)/sizeof(app_t)) {
|
||||||
apps[num_apps].select_aid = select_aid;
|
apps[num_apps].select_aid = select_aid;
|
||||||
num_apps++;
|
num_apps++;
|
||||||
@@ -73,16 +74,19 @@ int register_app(app_t * (*select_aid)(app_t *, const uint8_t *, uint8_t)) {
|
|||||||
|
|
||||||
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
|
static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
|
||||||
|
|
||||||
void led_set_blink(uint32_t mode) {
|
void led_set_blink(uint32_t mode)
|
||||||
|
{
|
||||||
blink_interval_ms = mode;
|
blink_interval_ms = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t timeout = 0;
|
uint32_t timeout = 0;
|
||||||
void timeout_stop() {
|
void timeout_stop()
|
||||||
|
{
|
||||||
timeout = 0;
|
timeout = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void timeout_start() {
|
void timeout_start()
|
||||||
|
{
|
||||||
timeout = board_millis();
|
timeout = board_millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,7 +94,8 @@ void execute_tasks();
|
|||||||
|
|
||||||
static bool req_button_pending = false;
|
static bool req_button_pending = false;
|
||||||
|
|
||||||
bool is_req_button_pending() {
|
bool is_req_button_pending()
|
||||||
|
{
|
||||||
return req_button_pending;
|
return req_button_pending;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,14 +103,16 @@ uint32_t button_timeout = 15000;
|
|||||||
bool cancel_button = false;
|
bool cancel_button = false;
|
||||||
|
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
uint32_t board_millis() {
|
uint32_t board_millis()
|
||||||
|
{
|
||||||
struct timeval start;
|
struct timeval start;
|
||||||
gettimeofday(&start, NULL);
|
gettimeofday(&start, NULL);
|
||||||
return (start.tv_sec * 1000 + start.tv_usec/1000);
|
return start.tv_sec * 1000 + start.tv_usec/1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
bool wait_button() {
|
bool wait_button()
|
||||||
|
{
|
||||||
uint32_t start_button = board_millis();
|
uint32_t start_button = board_millis();
|
||||||
bool timeout = false;
|
bool timeout = false;
|
||||||
cancel_button = false;
|
cancel_button = false;
|
||||||
@@ -137,7 +144,8 @@ bool wait_button() {
|
|||||||
|
|
||||||
struct apdu apdu;
|
struct apdu apdu;
|
||||||
|
|
||||||
void led_blinking_task() {
|
void led_blinking_task()
|
||||||
|
{
|
||||||
#ifdef PICO_DEFAULT_LED_PIN
|
#ifdef PICO_DEFAULT_LED_PIN
|
||||||
static uint32_t start_ms = 0;
|
static uint32_t start_ms = 0;
|
||||||
static uint8_t led_state = false;
|
static uint8_t led_state = false;
|
||||||
@@ -150,8 +158,9 @@ void led_blinking_task() {
|
|||||||
|
|
||||||
|
|
||||||
// Blink every interval ms
|
// Blink every interval ms
|
||||||
if (board_millis() - start_ms < interval)
|
if (board_millis() - start_ms < interval) {
|
||||||
return; // not enough time
|
return; // not enough time
|
||||||
|
}
|
||||||
start_ms += interval;
|
start_ms += interval;
|
||||||
|
|
||||||
gpio_put(led_color, led_state);
|
gpio_put(led_color, led_state);
|
||||||
@@ -159,7 +168,8 @@ void led_blinking_task() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void led_off_all() {
|
void led_off_all()
|
||||||
|
{
|
||||||
#ifdef PIMORONI_TINY2040
|
#ifdef PIMORONI_TINY2040
|
||||||
gpio_put(TINY2040_LED_R_PIN, 1);
|
gpio_put(TINY2040_LED_R_PIN, 1);
|
||||||
gpio_put(TINY2040_LED_G_PIN, 1);
|
gpio_put(TINY2040_LED_G_PIN, 1);
|
||||||
@@ -171,7 +181,8 @@ void led_off_all() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_rtc() {
|
void init_rtc()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
rtc_init();
|
rtc_init();
|
||||||
datetime_t dt = {
|
datetime_t dt = {
|
||||||
@@ -190,7 +201,8 @@ void init_rtc() {
|
|||||||
extern void neug_task();
|
extern void neug_task();
|
||||||
extern void usb_task();
|
extern void usb_task();
|
||||||
|
|
||||||
void execute_tasks() {
|
void execute_tasks()
|
||||||
|
{
|
||||||
usb_task();
|
usb_task();
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
tud_task(); // tinyusb device task
|
tud_task(); // tinyusb device task
|
||||||
@@ -198,7 +210,8 @@ void execute_tasks() {
|
|||||||
led_blinking_task();
|
led_blinking_task();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void)
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
usb_init();
|
usb_init();
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,8 @@ mbedtls_ctr_drbg_context ctr_drbg;
|
|||||||
extern uint32_t board_millis();
|
extern uint32_t board_millis();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void adc_start() {
|
void adc_start()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
adc_init();
|
adc_init();
|
||||||
adc_gpio_init(27);
|
adc_gpio_init(27);
|
||||||
@@ -44,10 +45,12 @@ void adc_start() {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void adc_stop() {
|
void adc_stop()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
uint32_t adc_read() {
|
uint32_t adc_read()
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -55,19 +58,25 @@ uint32_t adc_read() {
|
|||||||
static uint64_t random_word = 0xcbf29ce484222325;
|
static uint64_t random_word = 0xcbf29ce484222325;
|
||||||
static uint8_t ep_round = 0;
|
static uint8_t ep_round = 0;
|
||||||
|
|
||||||
static void ep_init() {
|
static void ep_init()
|
||||||
|
{
|
||||||
random_word = 0xcbf29ce484222325;
|
random_word = 0xcbf29ce484222325;
|
||||||
ep_round = 0;
|
ep_round = 0;
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
mbedtls_entropy_context entropy;
|
mbedtls_entropy_context entropy;
|
||||||
mbedtls_entropy_init(&entropy);
|
mbedtls_entropy_init(&entropy);
|
||||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||||
mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
|
mbedtls_ctr_drbg_seed(&ctr_drbg,
|
||||||
|
mbedtls_entropy_func,
|
||||||
|
&entropy,
|
||||||
|
(const unsigned char *) "RANDOM_GEN",
|
||||||
|
10);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Here, we assume a little endian architecture. */
|
/* Here, we assume a little endian architecture. */
|
||||||
static int ep_process () {
|
static int ep_process()
|
||||||
|
{
|
||||||
if (ep_round == 0) {
|
if (ep_round == 0) {
|
||||||
ep_init();
|
ep_init();
|
||||||
}
|
}
|
||||||
@@ -75,8 +84,7 @@ static int ep_process () {
|
|||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
for (int n = 0; n < 64; n++) {
|
for (int n = 0; n < 64; n++) {
|
||||||
uint8_t bit1, bit2;
|
uint8_t bit1, bit2;
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
bit1 = rosc_hw->randombit&0xff;
|
bit1 = rosc_hw->randombit&0xff;
|
||||||
//sleep_ms(1);
|
//sleep_ms(1);
|
||||||
bit2 = rosc_hw->randombit&0xff;
|
bit2 = rosc_hw->randombit&0xff;
|
||||||
@@ -95,7 +103,8 @@ static int ep_process () {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const uint32_t *ep_output() {
|
static const uint32_t *ep_output()
|
||||||
|
{
|
||||||
return (uint32_t *) &random_word;
|
return (uint32_t *) &random_word;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +116,8 @@ struct rng_rb {
|
|||||||
unsigned int empty : 1;
|
unsigned int empty : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void rb_init(struct rng_rb *rb, uint32_t *p, uint8_t size) {
|
static void rb_init(struct rng_rb *rb, uint32_t *p, uint8_t size)
|
||||||
|
{
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
#endif
|
#endif
|
||||||
rb->buf = p;
|
rb->buf = p;
|
||||||
@@ -117,22 +127,28 @@ static void rb_init(struct rng_rb *rb, uint32_t *p, uint8_t size) {
|
|||||||
rb->empty = 1;
|
rb->empty = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rb_add(struct rng_rb *rb, uint32_t v) {
|
static void rb_add(struct rng_rb *rb, uint32_t v)
|
||||||
|
{
|
||||||
rb->buf[rb->tail++] = v;
|
rb->buf[rb->tail++] = v;
|
||||||
if (rb->tail == rb->size)
|
if (rb->tail == rb->size) {
|
||||||
rb->tail = 0;
|
rb->tail = 0;
|
||||||
if (rb->tail == rb->head)
|
}
|
||||||
|
if (rb->tail == rb->head) {
|
||||||
rb->full = 1;
|
rb->full = 1;
|
||||||
|
}
|
||||||
rb->empty = 0;
|
rb->empty = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t rb_del(struct rng_rb *rb) {
|
static uint32_t rb_del(struct rng_rb *rb)
|
||||||
|
{
|
||||||
uint32_t v = rb->buf[rb->head++];
|
uint32_t v = rb->buf[rb->head++];
|
||||||
|
|
||||||
if (rb->head == rb->size)
|
if (rb->head == rb->size) {
|
||||||
rb->head = 0;
|
rb->head = 0;
|
||||||
if (rb->head == rb->tail)
|
}
|
||||||
|
if (rb->head == rb->tail) {
|
||||||
rb->empty = 1;
|
rb->empty = 1;
|
||||||
|
}
|
||||||
rb->full = 0;
|
rb->full = 0;
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
@@ -140,7 +156,8 @@ static uint32_t rb_del(struct rng_rb *rb) {
|
|||||||
|
|
||||||
static struct rng_rb the_ring_buffer;
|
static struct rng_rb the_ring_buffer;
|
||||||
|
|
||||||
void *neug_task() {
|
void *neug_task()
|
||||||
|
{
|
||||||
struct rng_rb *rb = &the_ring_buffer;
|
struct rng_rb *rb = &the_ring_buffer;
|
||||||
|
|
||||||
int n;
|
int n;
|
||||||
@@ -152,14 +169,16 @@ void *neug_task() {
|
|||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
rb_add(rb, *vp++);
|
rb_add(rb, *vp++);
|
||||||
if (rb->full)
|
if (rb->full) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void neug_init(uint32_t *buf, uint8_t size) {
|
void neug_init(uint32_t *buf, uint8_t size)
|
||||||
|
{
|
||||||
struct rng_rb *rb = &the_ring_buffer;
|
struct rng_rb *rb = &the_ring_buffer;
|
||||||
|
|
||||||
rb_init(rb, buf, size);
|
rb_init(rb, buf, size);
|
||||||
@@ -169,40 +188,45 @@ void neug_init(uint32_t *buf, uint8_t size) {
|
|||||||
ep_init();
|
ep_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void neug_flush(void) {
|
void neug_flush(void)
|
||||||
|
{
|
||||||
struct rng_rb *rb = &the_ring_buffer;
|
struct rng_rb *rb = &the_ring_buffer;
|
||||||
|
|
||||||
while (!rb->empty)
|
while (!rb->empty) {
|
||||||
rb_del(rb);
|
rb_del(rb);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t neug_get() {
|
uint32_t neug_get()
|
||||||
|
{
|
||||||
struct rng_rb *rb = &the_ring_buffer;
|
struct rng_rb *rb = &the_ring_buffer;
|
||||||
uint32_t v;
|
uint32_t v;
|
||||||
|
|
||||||
while (rb->empty)
|
while (rb->empty) {
|
||||||
neug_task();
|
neug_task();
|
||||||
|
}
|
||||||
v = rb_del(rb);
|
v = rb_del(rb);
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void neug_wait_full() {
|
void neug_wait_full()
|
||||||
|
{
|
||||||
struct rng_rb *rb = &the_ring_buffer;
|
struct rng_rb *rb = &the_ring_buffer;
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
uint core = get_core_num();
|
uint core = get_core_num();
|
||||||
#endif
|
#endif
|
||||||
while (!rb->full) {
|
while (!rb->full) {
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
if (core == 1)
|
if (core == 1) {
|
||||||
sleep_ms(1);
|
sleep_ms(1);
|
||||||
else
|
} else
|
||||||
#endif
|
#endif
|
||||||
neug_task();
|
neug_task();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void neug_fini(void) {
|
void neug_fini(void)
|
||||||
|
{
|
||||||
neug_get();
|
neug_get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,16 +24,19 @@
|
|||||||
#define RANDOM_BYTES_LENGTH 32
|
#define RANDOM_BYTES_LENGTH 32
|
||||||
static uint32_t random_word[RANDOM_BYTES_LENGTH/sizeof(uint32_t)];
|
static uint32_t random_word[RANDOM_BYTES_LENGTH/sizeof(uint32_t)];
|
||||||
|
|
||||||
void random_init(void) {
|
void random_init(void)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
neug_init(random_word, RANDOM_BYTES_LENGTH/sizeof(uint32_t));
|
neug_init(random_word, RANDOM_BYTES_LENGTH/sizeof(uint32_t));
|
||||||
|
|
||||||
for (i = 0; i < NEUG_PRE_LOOP; i++)
|
for (i = 0; i < NEUG_PRE_LOOP; i++) {
|
||||||
neug_get();
|
neug_get();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void random_fini(void) {
|
void random_fini(void)
|
||||||
|
{
|
||||||
neug_fini();
|
neug_fini();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,9 +45,11 @@ void random_fini(void) {
|
|||||||
*/
|
*/
|
||||||
void random_bytes_free(const uint8_t *p);
|
void random_bytes_free(const uint8_t *p);
|
||||||
#define MAX_RANDOM_BUFFER 1024
|
#define MAX_RANDOM_BUFFER 1024
|
||||||
const uint8_t * random_bytes_get(size_t len) {
|
const uint8_t *random_bytes_get(size_t len)
|
||||||
if (len > MAX_RANDOM_BUFFER)
|
{
|
||||||
|
if (len > MAX_RANDOM_BUFFER) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
static uint32_t return_word[MAX_RANDOM_BUFFER/sizeof(uint32_t)];
|
static uint32_t return_word[MAX_RANDOM_BUFFER/sizeof(uint32_t)];
|
||||||
for (int ix = 0; ix < len; ix += RANDOM_BYTES_LENGTH) {
|
for (int ix = 0; ix < len; ix += RANDOM_BYTES_LENGTH) {
|
||||||
neug_wait_full();
|
neug_wait_full();
|
||||||
@@ -57,7 +62,8 @@ const uint8_t * random_bytes_get(size_t len) {
|
|||||||
/*
|
/*
|
||||||
* Free pointer to random 32-byte
|
* Free pointer to random 32-byte
|
||||||
*/
|
*/
|
||||||
void random_bytes_free(const uint8_t *p) {
|
void random_bytes_free(const uint8_t *p)
|
||||||
|
{
|
||||||
(void) p;
|
(void) p;
|
||||||
memset(random_word, 0, RANDOM_BYTES_LENGTH);
|
memset(random_word, 0, RANDOM_BYTES_LENGTH);
|
||||||
neug_flush();
|
neug_flush();
|
||||||
@@ -66,7 +72,8 @@ void random_bytes_free(const uint8_t *p) {
|
|||||||
/*
|
/*
|
||||||
* Return 4-byte salt
|
* Return 4-byte salt
|
||||||
*/
|
*/
|
||||||
void random_get_salt(uint8_t *p) {
|
void random_get_salt(uint8_t *p)
|
||||||
|
{
|
||||||
uint32_t rnd;
|
uint32_t rnd;
|
||||||
|
|
||||||
rnd = neug_get();
|
rnd = neug_get();
|
||||||
@@ -79,7 +86,8 @@ void random_get_salt(uint8_t *p) {
|
|||||||
/*
|
/*
|
||||||
* Random byte iterator
|
* Random byte iterator
|
||||||
*/
|
*/
|
||||||
int random_gen(void *arg, unsigned char *out, size_t out_len) {
|
int random_gen(void *arg, unsigned char *out, size_t out_len)
|
||||||
|
{
|
||||||
uint8_t *index_p = (uint8_t *) arg;
|
uint8_t *index_p = (uint8_t *) arg;
|
||||||
uint8_t index = index_p ? *index_p : 0;
|
uint8_t index = index_p ? *index_p : 0;
|
||||||
size_t n;
|
size_t n;
|
||||||
@@ -88,8 +96,9 @@ int random_gen(void *arg, unsigned char *out, size_t out_len) {
|
|||||||
neug_wait_full();
|
neug_wait_full();
|
||||||
|
|
||||||
n = RANDOM_BYTES_LENGTH - index;
|
n = RANDOM_BYTES_LENGTH - index;
|
||||||
if (n > out_len)
|
if (n > out_len) {
|
||||||
n = out_len;
|
n = out_len;
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(out, ((unsigned char *) random_word) + index, n);
|
memcpy(out, ((unsigned char *) random_word) + index, n);
|
||||||
out += n;
|
out += n;
|
||||||
@@ -102,8 +111,9 @@ int random_gen(void *arg, unsigned char *out, size_t out_len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index_p)
|
if (index_p) {
|
||||||
*index_p = index;
|
*index_p = index;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,20 +102,24 @@ struct ccid_header {
|
|||||||
uint8_t ccid_status = 1;
|
uint8_t ccid_status = 1;
|
||||||
static uint8_t itf_num;
|
static uint8_t itf_num;
|
||||||
|
|
||||||
void ccid_write_offset(uint16_t size, uint16_t offset) {
|
void ccid_write_offset(uint16_t size, uint16_t offset)
|
||||||
if (*usb_get_tx(ITF_CCID)+offset != 0x81)
|
{
|
||||||
|
if (*usb_get_tx(ITF_CCID)+offset != 0x81) {
|
||||||
DEBUG_PAYLOAD(usb_get_tx(ITF_CCID)+offset, size+10);
|
DEBUG_PAYLOAD(usb_get_tx(ITF_CCID)+offset, size+10);
|
||||||
|
}
|
||||||
usb_write_offset(ITF_CCID, size+10, offset);
|
usb_write_offset(ITF_CCID, size+10, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ccid_write(uint16_t size) {
|
void ccid_write(uint16_t size)
|
||||||
|
{
|
||||||
ccid_write_offset(size, 0);
|
ccid_write_offset(size, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ccid_header *ccid_response;
|
struct ccid_header *ccid_response;
|
||||||
struct ccid_header *ccid_header;
|
struct ccid_header *ccid_header;
|
||||||
|
|
||||||
int driver_init_ccid() {
|
int driver_init_ccid()
|
||||||
|
{
|
||||||
ccid_header = (struct ccid_header *) usb_get_rx(ITF_CCID);
|
ccid_header = (struct ccid_header *) usb_get_rx(ITF_CCID);
|
||||||
// apdu.header = &ccid_header->apdu;
|
// apdu.header = &ccid_header->apdu;
|
||||||
|
|
||||||
@@ -126,38 +130,45 @@ int driver_init_ccid() {
|
|||||||
return CCID_OK;
|
return CCID_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tud_vendor_rx_cb(uint8_t itf) {
|
void tud_vendor_rx_cb(uint8_t itf)
|
||||||
|
{
|
||||||
(void) itf;
|
(void) itf;
|
||||||
|
|
||||||
uint32_t len = tud_vendor_available();
|
uint32_t len = tud_vendor_available();
|
||||||
usb_rx(ITF_CCID, NULL, len);
|
usb_rx(ITF_CCID, NULL, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes) {
|
void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes)
|
||||||
|
{
|
||||||
printf("written %ld\n", sent_bytes);
|
printf("written %ld\n", sent_bytes);
|
||||||
usb_write_flush(ITF_CCID);
|
usb_write_flush(ITF_CCID);
|
||||||
}
|
}
|
||||||
|
|
||||||
int driver_write_ccid(const uint8_t *buffer, size_t buffer_size) {
|
int driver_write_ccid(const uint8_t *buffer, size_t buffer_size)
|
||||||
|
{
|
||||||
return tud_vendor_write(buffer, buffer_size);
|
return tud_vendor_write(buffer, buffer_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t driver_read_ccid(uint8_t *buffer, size_t buffer_size) {
|
size_t driver_read_ccid(uint8_t *buffer, size_t buffer_size)
|
||||||
|
{
|
||||||
return tud_vendor_read(buffer, buffer_size);
|
return tud_vendor_read(buffer, buffer_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int driver_process_usb_nopacket_ccid() {
|
int driver_process_usb_nopacket_ccid()
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
int driver_process_usb_packet_ccid(uint16_t rx_read)
|
||||||
|
{
|
||||||
if (rx_read >= 10) {
|
if (rx_read >= 10) {
|
||||||
driver_init_ccid();
|
driver_init_ccid();
|
||||||
//printf("%d %d %x\r\n",tccid->dwLength,rx_read-10,tccid->bMessageType);
|
//printf("%d %d %x\r\n",tccid->dwLength,rx_read-10,tccid->bMessageType);
|
||||||
if (ccid_header->dwLength <= rx_read-10) {
|
if (ccid_header->dwLength <= rx_read-10) {
|
||||||
size_t apdu_sent = 0;
|
size_t apdu_sent = 0;
|
||||||
if (ccid_header->bMessageType != 0x65)
|
if (ccid_header->bMessageType != 0x65) {
|
||||||
DEBUG_PAYLOAD(usb_get_rx(ITF_CCID), usb_read_available(ITF_CCID));
|
DEBUG_PAYLOAD(usb_get_rx(ITF_CCID), usb_read_available(ITF_CCID));
|
||||||
|
}
|
||||||
if (ccid_header->bMessageType == 0x65) {
|
if (ccid_header->bMessageType == 0x65) {
|
||||||
ccid_response->bMessageType = CCID_SLOT_STATUS_RET;
|
ccid_response->bMessageType = CCID_SLOT_STATUS_RET;
|
||||||
ccid_response->dwLength = 0;
|
ccid_response->dwLength = 0;
|
||||||
@@ -166,8 +177,7 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
|||||||
ccid_response->abRFU0 = ccid_status;
|
ccid_response->abRFU0 = ccid_status;
|
||||||
ccid_response->abRFU1 = 0;
|
ccid_response->abRFU1 = 0;
|
||||||
ccid_write(0);
|
ccid_write(0);
|
||||||
}
|
} else if (ccid_header->bMessageType == 0x62) {
|
||||||
else if (ccid_header->bMessageType == 0x62) {
|
|
||||||
size_t size_atr = (ccid_atr ? ccid_atr[0] : 0);
|
size_t size_atr = (ccid_atr ? ccid_atr[0] : 0);
|
||||||
ccid_response->bMessageType = 0x80;
|
ccid_response->bMessageType = 0x80;
|
||||||
ccid_response->dwLength = size_atr;
|
ccid_response->dwLength = size_atr;
|
||||||
@@ -180,10 +190,10 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
|||||||
card_start(apdu_thread);
|
card_start(apdu_thread);
|
||||||
ccid_status = 0;
|
ccid_status = 0;
|
||||||
ccid_write(size_atr);
|
ccid_write(size_atr);
|
||||||
}
|
} else if (ccid_header->bMessageType == 0x63) {
|
||||||
else if (ccid_header->bMessageType == 0x63) {
|
if (ccid_status == 0) {
|
||||||
if (ccid_status == 0)
|
|
||||||
card_exit(0);
|
card_exit(0);
|
||||||
|
}
|
||||||
ccid_status = 1;
|
ccid_status = 1;
|
||||||
ccid_response->bMessageType = CCID_SLOT_STATUS_RET;
|
ccid_response->bMessageType = CCID_SLOT_STATUS_RET;
|
||||||
ccid_response->dwLength = 0;
|
ccid_response->dwLength = 0;
|
||||||
@@ -192,8 +202,7 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
|||||||
ccid_response->abRFU0 = ccid_status;
|
ccid_response->abRFU0 = ccid_status;
|
||||||
ccid_response->abRFU1 = 0;
|
ccid_response->abRFU1 = 0;
|
||||||
ccid_write(0);
|
ccid_write(0);
|
||||||
}
|
} else if (ccid_header->bMessageType == 0x6F) {
|
||||||
else if (ccid_header->bMessageType == 0x6F) {
|
|
||||||
apdu_sent = apdu_process(ITF_CCID, &ccid_header->apdu, ccid_header->dwLength);
|
apdu_sent = apdu_process(ITF_CCID, &ccid_header->apdu, ccid_header->dwLength);
|
||||||
}
|
}
|
||||||
usb_clear_rx(ITF_CCID);
|
usb_clear_rx(ITF_CCID);
|
||||||
@@ -213,11 +222,13 @@ int driver_process_usb_packet_ccid(uint16_t rx_read) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool driver_mounted_ccid() {
|
bool driver_mounted_ccid()
|
||||||
|
{
|
||||||
return tud_vendor_mounted();
|
return tud_vendor_mounted();
|
||||||
}
|
}
|
||||||
|
|
||||||
void driver_exec_timeout_ccid() {
|
void driver_exec_timeout_ccid()
|
||||||
|
{
|
||||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||||
ccid_response->dwLength = 0;
|
ccid_response->dwLength = 0;
|
||||||
ccid_response->bSlot = 0;
|
ccid_response->bSlot = 0;
|
||||||
@@ -227,7 +238,8 @@ void driver_exec_timeout_ccid() {
|
|||||||
ccid_write(0);
|
ccid_write(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void driver_exec_finished_ccid(size_t size_next) {
|
void driver_exec_finished_ccid(size_t size_next)
|
||||||
|
{
|
||||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||||
ccid_response->dwLength = size_next;
|
ccid_response->dwLength = size_next;
|
||||||
ccid_response->bSlot = 0;
|
ccid_response->bSlot = 0;
|
||||||
@@ -237,7 +249,8 @@ void driver_exec_finished_ccid(size_t size_next) {
|
|||||||
ccid_write(size_next);
|
ccid_write(size_next);
|
||||||
}
|
}
|
||||||
|
|
||||||
void driver_exec_finished_cont_ccid(size_t size_next, size_t offset) {
|
void driver_exec_finished_cont_ccid(size_t size_next, size_t offset)
|
||||||
|
{
|
||||||
|
|
||||||
ccid_response = (struct ccid_header *) (usb_get_tx(ITF_CCID)+offset-10);
|
ccid_response = (struct ccid_header *) (usb_get_tx(ITF_CCID)+offset-10);
|
||||||
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
ccid_response->bMessageType = CCID_DATA_BLOCK_RET;
|
||||||
@@ -249,7 +262,8 @@ void driver_exec_finished_cont_ccid(size_t size_next, size_t offset) {
|
|||||||
ccid_write_offset(size_next, offset-10);
|
ccid_write_offset(size_next, offset-10);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *driver_prepare_response_ccid() {
|
uint8_t *driver_prepare_response_ccid()
|
||||||
|
{
|
||||||
ccid_response = (struct ccid_header *) usb_get_tx(ITF_CCID);
|
ccid_response = (struct ccid_header *) usb_get_tx(ITF_CCID);
|
||||||
apdu.rdata = &ccid_response->apdu;
|
apdu.rdata = &ccid_response->apdu;
|
||||||
return &ccid_response->apdu;
|
return &ccid_response->apdu;
|
||||||
@@ -258,23 +272,28 @@ uint8_t *driver_prepare_response_ccid() {
|
|||||||
|
|
||||||
#define MAX_USB_POWER 1
|
#define MAX_USB_POWER 1
|
||||||
|
|
||||||
static void ccid_init_cb(void) {
|
static void ccid_init_cb(void)
|
||||||
|
{
|
||||||
TU_LOG1("-------- CCID INIT\r\n");
|
TU_LOG1("-------- CCID INIT\r\n");
|
||||||
vendord_init();
|
vendord_init();
|
||||||
|
|
||||||
//ccid_notify_slot_change(c);
|
//ccid_notify_slot_change(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ccid_reset_cb(uint8_t rhport) {
|
static void ccid_reset_cb(uint8_t rhport)
|
||||||
|
{
|
||||||
TU_LOG1("-------- CCID RESET\r\n");
|
TU_LOG1("-------- CCID RESET\r\n");
|
||||||
itf_num = 0;
|
itf_num = 0;
|
||||||
vendord_reset(rhport);
|
vendord_reset(rhport);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
|
static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
|
||||||
|
{
|
||||||
uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t)*max_len);
|
uint8_t *itf_vendor = (uint8_t *) malloc(sizeof(uint8_t)*max_len);
|
||||||
//TU_LOG1("-------- CCID OPEN\r\n");
|
//TU_LOG1("-------- CCID OPEN\r\n");
|
||||||
TU_VERIFY(itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0, 0);
|
TU_VERIFY(
|
||||||
|
itf_desc->bInterfaceClass == TUSB_CLASS_SMART_CARD && itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0,
|
||||||
|
0);
|
||||||
|
|
||||||
//vendord_open expects a CLASS_VENDOR interface class
|
//vendord_open expects a CLASS_VENDOR interface class
|
||||||
memcpy(itf_vendor, itf_desc, sizeof(uint8_t)*max_len);
|
memcpy(itf_vendor, itf_desc, sizeof(uint8_t)*max_len);
|
||||||
@@ -282,7 +301,8 @@ static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc,
|
|||||||
vendord_open(rhport, (tusb_desc_interface_t *) itf_vendor, max_len);
|
vendord_open(rhport, (tusb_desc_interface_t *) itf_vendor, max_len);
|
||||||
free(itf_vendor);
|
free(itf_vendor);
|
||||||
|
|
||||||
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 2*sizeof(tusb_desc_endpoint_t);
|
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||||
|
2*sizeof(tusb_desc_endpoint_t);
|
||||||
TU_VERIFY(max_len >= drv_len, 0);
|
TU_VERIFY(max_len >= drv_len, 0);
|
||||||
|
|
||||||
itf_num = itf_desc->bInterfaceNumber;
|
itf_num = itf_desc->bInterfaceNumber;
|
||||||
@@ -290,14 +310,22 @@ static uint16_t ccid_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Support for parameterized reset via vendor interface control request
|
// Support for parameterized reset via vendor interface control request
|
||||||
static bool ccid_control_xfer_cb(uint8_t __unused rhport, uint8_t stage, tusb_control_request_t const * request) {
|
static bool ccid_control_xfer_cb(uint8_t __unused rhport,
|
||||||
|
uint8_t stage,
|
||||||
|
tusb_control_request_t const *request)
|
||||||
|
{
|
||||||
// nothing to do with DATA & ACK stage
|
// nothing to do with DATA & ACK stage
|
||||||
TU_LOG2("-------- CCID CTRL XFER\r\n");
|
TU_LOG2("-------- CCID CTRL XFER\r\n");
|
||||||
if (stage != CONTROL_STAGE_SETUP) return true;
|
if (stage != CONTROL_STAGE_SETUP) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (request->wIndex == itf_num)
|
if (request->wIndex == itf_num) {
|
||||||
{
|
TU_LOG2("-------- bmRequestType %x, bRequest %x, wValue %x, wLength %x\r\n",
|
||||||
TU_LOG2("-------- bmRequestType %x, bRequest %x, wValue %x, wLength %x\r\n",request->bmRequestType,request->bRequest, request->wValue, request->wLength);
|
request->bmRequestType,
|
||||||
|
request->bRequest,
|
||||||
|
request->wValue,
|
||||||
|
request->wLength);
|
||||||
/*
|
/*
|
||||||
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL
|
#if PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_RESET_TO_BOOTSEL
|
||||||
if (request->bRequest == RESET_REQUEST_BOOTSEL) {
|
if (request->bRequest == RESET_REQUEST_BOOTSEL) {
|
||||||
@@ -327,7 +355,11 @@ static bool ccid_control_xfer_cb(uint8_t __unused rhport, uint8_t stage, tusb_co
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ccid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
|
static bool ccid_xfer_cb(uint8_t rhport,
|
||||||
|
uint8_t ep_addr,
|
||||||
|
xfer_result_t result,
|
||||||
|
uint32_t xferred_bytes)
|
||||||
|
{
|
||||||
//printf("------ CALLED XFER_CB\r\n");
|
//printf("------ CALLED XFER_CB\r\n");
|
||||||
return vendord_xfer_cb(rhport, ep_addr, result, xferred_bytes);
|
return vendord_xfer_cb(rhport, ep_addr, result, xferred_bytes);
|
||||||
//return true;
|
//return true;
|
||||||
@@ -346,7 +378,8 @@ static const usbd_class_driver_t ccid_driver = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Implement callback to add our custom driver
|
// Implement callback to add our custom driver
|
||||||
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count) {
|
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count)
|
||||||
|
{
|
||||||
*driver_count = 1;
|
*driver_count = 1;
|
||||||
return &ccid_driver;
|
return &ccid_driver;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -322,4 +322,3 @@ extern bool usb_write_available();
|
|||||||
extern uint32_t usb_write_flush();
|
extern uint32_t usb_write_flush();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -38,8 +38,7 @@ int msleep(long msec)
|
|||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
if (msec < 0)
|
if (msec < 0) {
|
||||||
{
|
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -54,7 +53,8 @@ int msleep(long msec)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int emul_init(char *host, uint16_t port) {
|
int emul_init(char *host, uint16_t port)
|
||||||
|
{
|
||||||
struct sockaddr_in serv_addr;
|
struct sockaddr_in serv_addr;
|
||||||
fprintf(stderr, "\n Starting emulation envionrment\n");
|
fprintf(stderr, "\n Starting emulation envionrment\n");
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||||
@@ -81,29 +81,34 @@ int emul_init(char *host, uint16_t port) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *driver_prepare_response_emul() {
|
uint8_t *driver_prepare_response_emul()
|
||||||
|
{
|
||||||
apdu.rdata = usb_get_tx(ITF_EMUL);
|
apdu.rdata = usb_get_tx(ITF_EMUL);
|
||||||
return apdu.rdata;
|
return apdu.rdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
int driver_write_emul(const uint8_t *buffer, size_t buffer_size) {
|
int driver_write_emul(const uint8_t *buffer, size_t buffer_size)
|
||||||
|
{
|
||||||
uint16_t size = htons(buffer_size);
|
uint16_t size = htons(buffer_size);
|
||||||
//DEBUG_PAYLOAD(buffer,buffer_size);
|
//DEBUG_PAYLOAD(buffer,buffer_size);
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
do {
|
do {
|
||||||
ret = send(sock, &size, sizeof(size), 0);
|
ret = send(sock, &size, sizeof(size), 0);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
msleep(10);
|
msleep(10);
|
||||||
|
}
|
||||||
} while (ret <= 0);
|
} while (ret <= 0);
|
||||||
do {
|
do {
|
||||||
ret = send(sock, buffer, (uint16_t) buffer_size, 0);
|
ret = send(sock, buffer, (uint16_t) buffer_size, 0);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
msleep(10);
|
msleep(10);
|
||||||
|
}
|
||||||
} while (ret <= 0);
|
} while (ret <= 0);
|
||||||
return buffer_size;
|
return buffer_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t emul_write_offset(uint16_t size, uint16_t offset) {
|
uint32_t emul_write_offset(uint16_t size, uint16_t offset)
|
||||||
|
{
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
//DEBUG_PAYLOAD(usb_get_tx(ITF_EMUL)+offset, size);
|
//DEBUG_PAYLOAD(usb_get_tx(ITF_EMUL)+offset, size);
|
||||||
return usb_write_offset(ITF_EMUL, size, offset);
|
return usb_write_offset(ITF_EMUL, size, offset);
|
||||||
@@ -111,29 +116,33 @@ uint32_t emul_write_offset(uint16_t size, uint16_t offset) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t emul_write(uint16_t size) {
|
uint32_t emul_write(uint16_t size)
|
||||||
|
{
|
||||||
return emul_write_offset(size, 0);
|
return emul_write_offset(size, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void driver_exec_finished_cont_emul(size_t size_next, size_t offset) {
|
void driver_exec_finished_cont_emul(size_t size_next, size_t offset)
|
||||||
|
{
|
||||||
emul_write_offset(size_next, offset);
|
emul_write_offset(size_next, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
int driver_process_usb_packet_emul(uint16_t len) {
|
int driver_process_usb_packet_emul(uint16_t len)
|
||||||
|
{
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
uint8_t *data = usb_get_rx(ITF_EMUL), *rdata = usb_get_tx(ITF_EMUL);
|
uint8_t *data = usb_get_rx(ITF_EMUL), *rdata = usb_get_tx(ITF_EMUL);
|
||||||
if (len == 1) {
|
if (len == 1) {
|
||||||
uint8_t c = data[0];
|
uint8_t c = data[0];
|
||||||
if (c == 4) {
|
if (c == 4) {
|
||||||
if (ccid_atr)
|
if (ccid_atr) {
|
||||||
memcpy(rdata, ccid_atr+1, ccid_atr[0]);
|
memcpy(rdata, ccid_atr+1, ccid_atr[0]);
|
||||||
|
}
|
||||||
emul_write(ccid_atr ? ccid_atr[0] : 0);
|
emul_write(ccid_atr ? ccid_atr[0] : 0);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
DEBUG_PAYLOAD(data, len);
|
DEBUG_PAYLOAD(data, len);
|
||||||
if (apdu_process(ITF_EMUL, data, len) > 0)
|
if (apdu_process(ITF_EMUL, data, len) > 0) {
|
||||||
process_apdu();
|
process_apdu();
|
||||||
|
}
|
||||||
apdu_finish();
|
apdu_finish();
|
||||||
size_t ret = apdu_next();
|
size_t ret = apdu_next();
|
||||||
DEBUG_PAYLOAD(rdata, ret);
|
DEBUG_PAYLOAD(rdata, ret);
|
||||||
@@ -144,7 +153,8 @@ int driver_process_usb_packet_emul(uint16_t len) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t emul_read() {
|
uint16_t emul_read()
|
||||||
|
{
|
||||||
uint16_t len = 0;
|
uint16_t len = 0;
|
||||||
fd_set input;
|
fd_set input;
|
||||||
FD_ZERO(&input);
|
FD_ZERO(&input);
|
||||||
@@ -165,8 +175,9 @@ uint16_t emul_read() {
|
|||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
while (true) {
|
while (true) {
|
||||||
valread = recv(sock, usb_get_rx(ITF_EMUL), len, 0);
|
valread = recv(sock, usb_get_rx(ITF_EMUL), len, 0);
|
||||||
if (valread > 0)
|
if (valread > 0) {
|
||||||
return valread;
|
return valread;
|
||||||
|
}
|
||||||
msleep(10);
|
msleep(10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,13 +40,15 @@ void tud_mount_cb()
|
|||||||
mounted = true;
|
mounted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool driver_mounted_hid() {
|
bool driver_mounted_hid()
|
||||||
|
{
|
||||||
return mounted;
|
return mounted;
|
||||||
}
|
}
|
||||||
|
|
||||||
CTAPHID_FRAME *ctap_req = NULL, *ctap_resp = NULL;
|
CTAPHID_FRAME *ctap_req = NULL, *ctap_resp = NULL;
|
||||||
|
|
||||||
int driver_init_hid() {
|
int driver_init_hid()
|
||||||
|
{
|
||||||
tud_init(BOARD_TUD_RHPORT);
|
tud_init(BOARD_TUD_RHPORT);
|
||||||
ctap_req = (CTAPHID_FRAME *) usb_get_rx(ITF_HID);
|
ctap_req = (CTAPHID_FRAME *) usb_get_rx(ITF_HID);
|
||||||
apdu.header = ctap_req->init.data;
|
apdu.header = ctap_req->init.data;
|
||||||
@@ -66,7 +68,11 @@ int driver_init_hid() {
|
|||||||
// Invoked when received GET_REPORT control request
|
// Invoked when received GET_REPORT control request
|
||||||
// Application must fill buffer report's content and return its length.
|
// Application must fill buffer report's content and return its length.
|
||||||
// Return zero will cause the stack to STALL request
|
// Return zero will cause the stack to STALL request
|
||||||
uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
|
uint16_t tud_hid_get_report_cb(uint8_t itf,
|
||||||
|
uint8_t report_id,
|
||||||
|
hid_report_type_t report_type,
|
||||||
|
uint8_t *buffer,
|
||||||
|
uint16_t reqlen)
|
||||||
{
|
{
|
||||||
// TODO not Implemented
|
// TODO not Implemented
|
||||||
(void) itf;
|
(void) itf;
|
||||||
@@ -82,13 +88,16 @@ uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t
|
|||||||
return reqlen;
|
return reqlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t hid_write_offset(uint16_t size, uint16_t offset) {
|
uint32_t hid_write_offset(uint16_t size, uint16_t offset)
|
||||||
if (*usb_get_tx(ITF_HID) != 0x81)
|
{
|
||||||
|
if (*usb_get_tx(ITF_HID) != 0x81) {
|
||||||
DEBUG_PAYLOAD(usb_get_tx(ITF_HID)+offset, size);
|
DEBUG_PAYLOAD(usb_get_tx(ITF_HID)+offset, size);
|
||||||
|
}
|
||||||
return usb_write_offset(ITF_HID, size, offset);
|
return usb_write_offset(ITF_HID, size, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t hid_write(uint16_t size) {
|
uint32_t hid_write(uint16_t size)
|
||||||
|
{
|
||||||
return hid_write_offset(size, 0);
|
return hid_write_offset(size, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,14 +110,17 @@ static const uint8_t conv_table[128][2] = { HID_ASCII_TO_KEYCODE };
|
|||||||
static uint8_t keyboard_w = 0;
|
static uint8_t keyboard_w = 0;
|
||||||
static bool sent_key = false;
|
static bool sent_key = false;
|
||||||
|
|
||||||
void add_keyboard_buffer(const uint8_t *data, size_t data_len) {
|
void add_keyboard_buffer(const uint8_t *data, size_t data_len)
|
||||||
|
{
|
||||||
keyboard_buffer_len = MIN(sizeof(keyboard_buffer), data_len);
|
keyboard_buffer_len = MIN(sizeof(keyboard_buffer), data_len);
|
||||||
memcpy(keyboard_buffer, data, keyboard_buffer_len);
|
memcpy(keyboard_buffer, data, keyboard_buffer_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void send_hid_report(uint8_t report_id) {
|
static void send_hid_report(uint8_t report_id)
|
||||||
if (!tud_hid_ready())
|
{
|
||||||
|
if (!tud_hid_ready()) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (report_id) {
|
switch (report_id) {
|
||||||
case REPORT_ID_KEYBOARD: {
|
case REPORT_ID_KEYBOARD: {
|
||||||
@@ -117,20 +129,22 @@ static void send_hid_report(uint8_t report_id) {
|
|||||||
uint8_t keycode[6] = { 0 };
|
uint8_t keycode[6] = { 0 };
|
||||||
uint8_t modifier = 0;
|
uint8_t modifier = 0;
|
||||||
uint8_t chr = keyboard_buffer[keyboard_w];
|
uint8_t chr = keyboard_buffer[keyboard_w];
|
||||||
if (conv_table[chr][0])
|
if (conv_table[chr][0]) {
|
||||||
modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
|
modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
|
||||||
|
}
|
||||||
keycode[0] = conv_table[chr][1];
|
keycode[0] = conv_table[chr][1];
|
||||||
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, modifier, keycode) == true)
|
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, modifier,
|
||||||
|
keycode) == true) {
|
||||||
sent_key = true;
|
sent_key = true;
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, 0, NULL) == true) {
|
if (tud_hid_n_keyboard_report(ITF_KEYBOARD, REPORT_ID_KEYBOARD, 0,
|
||||||
|
NULL) == true) {
|
||||||
keyboard_w++;
|
keyboard_w++;
|
||||||
sent_key = false;
|
sent_key = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (keyboard_w == keyboard_buffer_len && keyboard_buffer_len > 0) {
|
||||||
else if (keyboard_w == keyboard_buffer_len && keyboard_buffer_len > 0) {
|
|
||||||
keyboard_w = keyboard_buffer_len = 0;
|
keyboard_w = keyboard_buffer_len = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,25 +154,27 @@ static void send_hid_report(uint8_t report_id) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void hid_task(void) {
|
void hid_task(void)
|
||||||
|
{
|
||||||
// Poll every 10ms
|
// Poll every 10ms
|
||||||
const uint32_t interval_ms = 10;
|
const uint32_t interval_ms = 10;
|
||||||
static uint32_t start_ms = 0;
|
static uint32_t start_ms = 0;
|
||||||
|
|
||||||
if (board_millis() - start_ms < interval_ms)
|
if (board_millis() - start_ms < interval_ms) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
start_ms += interval_ms;
|
start_ms += interval_ms;
|
||||||
|
|
||||||
// Remote wakeup
|
// Remote wakeup
|
||||||
if (tud_suspended() && keyboard_buffer_len > 0) {
|
if (tud_suspended() && keyboard_buffer_len > 0) {
|
||||||
tud_remote_wakeup();
|
tud_remote_wakeup();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
send_hid_report(REPORT_ID_KEYBOARD);
|
send_hid_report(REPORT_ID_KEYBOARD);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint16_t*/ uint8_t len) {
|
void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, /*uint16_t*/ uint8_t len)
|
||||||
|
{
|
||||||
if (send_buffer_size > 0 && instance == ITF_HID) {
|
if (send_buffer_size > 0 && instance == ITF_HID) {
|
||||||
uint8_t seq = report[4] & TYPE_MASK ? 0 : report[4] + 1;
|
uint8_t seq = report[4] & TYPE_MASK ? 0 : report[4] + 1;
|
||||||
if (last_write_result == true) {
|
if (last_write_result == true) {
|
||||||
@@ -172,33 +188,43 @@ void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, /*uint1
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int driver_write_hid(const uint8_t *buffer, size_t buffer_size) {
|
int driver_write_hid(const uint8_t *buffer, size_t buffer_size)
|
||||||
|
{
|
||||||
last_write_result = tud_hid_n_report(ITF_HID, 0, buffer, buffer_size);
|
last_write_result = tud_hid_n_report(ITF_HID, 0, buffer, buffer_size);
|
||||||
printf("result %d\n", last_write_result);
|
printf("result %d\n", last_write_result);
|
||||||
if (last_write_result == false)
|
if (last_write_result == false) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
return MIN(64, buffer_size);
|
return MIN(64, buffer_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t driver_read_hid(uint8_t *buffer, size_t buffer_size) {
|
size_t driver_read_hid(uint8_t *buffer, size_t buffer_size)
|
||||||
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoked when received SET_REPORT control request or
|
// Invoked when received SET_REPORT control request or
|
||||||
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
|
// received data on OUT endpoint ( Report ID = 0, Type = 0 )
|
||||||
void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
|
void tud_hid_set_report_cb(uint8_t itf,
|
||||||
|
uint8_t report_id,
|
||||||
|
hid_report_type_t report_type,
|
||||||
|
uint8_t const *buffer,
|
||||||
|
uint16_t bufsize)
|
||||||
|
{
|
||||||
// This example doesn't use multiple report and report ID
|
// This example doesn't use multiple report and report ID
|
||||||
(void) itf;
|
(void) itf;
|
||||||
(void) report_id;
|
(void) report_id;
|
||||||
(void) report_type;
|
(void) report_type;
|
||||||
printf("set_report %d %d %d\n", itf, report_id, report_type);
|
printf("set_report %d %d %d\n", itf, report_id, report_type);
|
||||||
if (itf == ITF_KEYBOARD)
|
if (itf == ITF_KEYBOARD) {
|
||||||
DEBUG_PAYLOAD(buffer, bufsize);
|
DEBUG_PAYLOAD(buffer, bufsize);
|
||||||
|
}
|
||||||
usb_rx(itf, buffer, bufsize);
|
usb_rx(itf, buffer, bufsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t last_cmd_time = 0, last_packet_time = 0;
|
uint32_t last_cmd_time = 0, last_packet_time = 0;
|
||||||
int ctap_error(uint8_t error) {
|
int ctap_error(uint8_t error)
|
||||||
|
{
|
||||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||||
memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||||
ctap_resp->cid = ctap_req->cid;
|
ctap_resp->cid = ctap_req->cid;
|
||||||
@@ -220,7 +246,8 @@ uint8_t thread_type = 0; //1 is APDU, 2 is CBOR
|
|||||||
extern void cbor_thread();
|
extern void cbor_thread();
|
||||||
extern bool cancel_button;
|
extern bool cancel_button;
|
||||||
|
|
||||||
int driver_process_usb_nopacket_hid() {
|
int driver_process_usb_nopacket_hid()
|
||||||
|
{
|
||||||
if (last_packet_time > 0 && last_packet_time+500 < board_millis()) {
|
if (last_packet_time > 0 && last_packet_time+500 < board_millis()) {
|
||||||
ctap_error(CTAP1_ERR_MSG_TIMEOUT);
|
ctap_error(CTAP1_ERR_MSG_TIMEOUT);
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
@@ -231,32 +258,38 @@ int driver_process_usb_nopacket_hid() {
|
|||||||
|
|
||||||
extern const uint8_t fido_aid[];
|
extern const uint8_t fido_aid[];
|
||||||
|
|
||||||
int driver_process_usb_packet_hid(uint16_t read) {
|
int driver_process_usb_packet_hid(uint16_t read)
|
||||||
|
{
|
||||||
int apdu_sent = 0;
|
int apdu_sent = 0;
|
||||||
if (read >= 5) {
|
if (read >= 5) {
|
||||||
driver_init_hid();
|
driver_init_hid();
|
||||||
last_packet_time = board_millis();
|
last_packet_time = board_millis();
|
||||||
DEBUG_PAYLOAD(usb_get_rx(ITF_HID), 64);
|
DEBUG_PAYLOAD(usb_get_rx(ITF_HID), 64);
|
||||||
memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||||
if (ctap_req->cid == 0x0 || (ctap_req->cid == CID_BROADCAST && ctap_req->init.cmd != CTAPHID_INIT))
|
if (ctap_req->cid == 0x0 ||
|
||||||
|
(ctap_req->cid == CID_BROADCAST && ctap_req->init.cmd != CTAPHID_INIT)) {
|
||||||
return ctap_error(CTAP1_ERR_INVALID_CHANNEL);
|
return ctap_error(CTAP1_ERR_INVALID_CHANNEL);
|
||||||
if (board_millis() < lock && ctap_req->cid != last_req.cid && last_cmd_time+100 > board_millis())
|
}
|
||||||
|
if (board_millis() < lock && ctap_req->cid != last_req.cid &&
|
||||||
|
last_cmd_time+100 > board_millis()) {
|
||||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||||
if (FRAME_TYPE(ctap_req) == TYPE_INIT)
|
}
|
||||||
{
|
if (FRAME_TYPE(ctap_req) == TYPE_INIT) {
|
||||||
if (MSG_LEN(ctap_req) > CTAP_MAX_PACKET_SIZE)
|
if (MSG_LEN(ctap_req) > CTAP_MAX_PACKET_SIZE) {
|
||||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||||
if (msg_packet.len > 0 && last_cmd_time+100 > board_millis() && ctap_req->init.cmd != CTAPHID_INIT) {
|
}
|
||||||
if (last_req.cid != ctap_req->cid) //We are in a transaction
|
if (msg_packet.len > 0 && last_cmd_time+100 > board_millis() &&
|
||||||
|
ctap_req->init.cmd != CTAPHID_INIT) {
|
||||||
|
if (last_req.cid != ctap_req->cid) { //We are in a transaction
|
||||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||||
else
|
} else {
|
||||||
return ctap_error(CTAP1_ERR_INVALID_SEQ);
|
return ctap_error(CTAP1_ERR_INVALID_SEQ);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
printf("command %x\n", FRAME_CMD(ctap_req));
|
printf("command %x\n", FRAME_CMD(ctap_req));
|
||||||
printf("len %d\n", MSG_LEN(ctap_req));
|
printf("len %d\n", MSG_LEN(ctap_req));
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
if (MSG_LEN(ctap_req) > 64 - 7)
|
if (MSG_LEN(ctap_req) > 64 - 7) {
|
||||||
{
|
|
||||||
msg_packet.len = MSG_LEN(ctap_req);
|
msg_packet.len = MSG_LEN(ctap_req);
|
||||||
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->init.data, 64-7);
|
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->init.data, 64-7);
|
||||||
msg_packet.current_len += 64 - 7;
|
msg_packet.current_len += 64 - 7;
|
||||||
@@ -265,20 +298,22 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
last_cmd = ctap_req->init.cmd;
|
last_cmd = ctap_req->init.cmd;
|
||||||
last_seq = 0;
|
last_seq = 0;
|
||||||
last_cmd_time = board_millis();
|
last_cmd_time = board_millis();
|
||||||
}
|
} else {
|
||||||
else {
|
if (msg_packet.len == 0) { //Received a cont with a prior init pkt
|
||||||
if (msg_packet.len == 0) //Received a cont with a prior init pkt
|
|
||||||
return 0;
|
return 0;
|
||||||
if (last_seq != ctap_req->cont.seq)
|
}
|
||||||
|
if (last_seq != ctap_req->cont.seq) {
|
||||||
return ctap_error(CTAP1_ERR_INVALID_SEQ);
|
return ctap_error(CTAP1_ERR_INVALID_SEQ);
|
||||||
|
}
|
||||||
if (last_req.cid == ctap_req->cid) {
|
if (last_req.cid == ctap_req->cid) {
|
||||||
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->cont.data, MIN(64 - 5, msg_packet.len - msg_packet.current_len));
|
memcpy(msg_packet.data + msg_packet.current_len, ctap_req->cont.data,
|
||||||
|
MIN(64 - 5, msg_packet.len - msg_packet.current_len));
|
||||||
msg_packet.current_len += MIN(64 - 5, msg_packet.len - msg_packet.current_len);
|
msg_packet.current_len += MIN(64 - 5, msg_packet.len - msg_packet.current_len);
|
||||||
memcpy(&last_req, ctap_req, sizeof(CTAPHID_FRAME));
|
memcpy(&last_req, ctap_req, sizeof(CTAPHID_FRAME));
|
||||||
last_seq++;
|
last_seq++;
|
||||||
}
|
} else if (last_cmd_time+100 > board_millis()) {
|
||||||
else if (last_cmd_time+100 > board_millis())
|
|
||||||
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
return ctap_error(CTAP1_ERR_CHANNEL_BUSY);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,8 +337,7 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
hid_write(64);
|
hid_write(64);
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
}
|
} else if (ctap_req->init.cmd == CTAPHID_WINK) {
|
||||||
else if (ctap_req->init.cmd == CTAPHID_WINK) {
|
|
||||||
if (MSG_LEN(ctap_req) != 0) {
|
if (MSG_LEN(ctap_req) != 0) {
|
||||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||||
}
|
}
|
||||||
@@ -313,14 +347,14 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
hid_write(64);
|
hid_write(64);
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
}
|
} else if ((last_cmd == CTAPHID_PING || last_cmd == CTAPHID_SYNC) &&
|
||||||
else if ((last_cmd == CTAPHID_PING || last_cmd == CTAPHID_SYNC) && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
(msg_packet.len == 0 ||
|
||||||
|
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||||
memcpy(ctap_resp->init.data, msg_packet.data, msg_packet.len);
|
memcpy(ctap_resp->init.data, msg_packet.data, msg_packet.len);
|
||||||
driver_exec_finished_hid(msg_packet.len);
|
driver_exec_finished_hid(msg_packet.len);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
memcpy(ctap_resp->init.data, ctap_req->init.data, MSG_LEN(ctap_req));
|
memcpy(ctap_resp->init.data, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||||
ctap_resp->cid = ctap_req->cid;
|
ctap_resp->cid = ctap_req->cid;
|
||||||
ctap_resp->init.cmd = last_cmd;
|
ctap_resp->init.cmd = last_cmd;
|
||||||
@@ -330,12 +364,13 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
}
|
}
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
}
|
} else if (ctap_req->init.cmd == CTAPHID_LOCK) {
|
||||||
else if (ctap_req->init.cmd == CTAPHID_LOCK) {
|
if (MSG_LEN(ctap_req) != 1) {
|
||||||
if (MSG_LEN(ctap_req) != 1)
|
|
||||||
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
return ctap_error(CTAP1_ERR_INVALID_LEN);
|
||||||
if (ctap_req->init.data[0] > 10)
|
}
|
||||||
|
if (ctap_req->init.data[0] > 10) {
|
||||||
return ctap_error(CTAP1_ERR_INVALID_PARAMETER);
|
return ctap_error(CTAP1_ERR_INVALID_PARAMETER);
|
||||||
|
}
|
||||||
lock = board_millis() + ctap_req->init.data[0] * 1000;
|
lock = board_millis() + ctap_req->init.data[0] * 1000;
|
||||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||||
memset(ctap_resp, 0, 64);
|
memset(ctap_resp, 0, 64);
|
||||||
@@ -344,10 +379,12 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
hid_write(64);
|
hid_write(64);
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
}
|
} else if (last_cmd == CTAPHID_MSG &&
|
||||||
else if (last_cmd == CTAPHID_MSG && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
(msg_packet.len == 0 ||
|
||||||
|
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||||
|
|
||||||
if (current_app == NULL || memcmp(current_app->aid, fido_aid+1, MIN(current_app->aid[0], fido_aid[0])) != 0) {
|
if (current_app == NULL ||
|
||||||
|
memcmp(current_app->aid, fido_aid+1, MIN(current_app->aid[0], fido_aid[0])) != 0) {
|
||||||
for (int a = 0; a < num_apps; a++) {
|
for (int a = 0; a < num_apps; a++) {
|
||||||
if ((current_app = apps[a].select_aid(&apps[a], fido_aid+1, fido_aid[0]))) {
|
if ((current_app = apps[a].select_aid(&apps[a], fido_aid+1, fido_aid[0]))) {
|
||||||
break;
|
break;
|
||||||
@@ -358,38 +395,42 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
card_start(apdu_thread);
|
card_start(apdu_thread);
|
||||||
thread_type = 1;
|
thread_type = 1;
|
||||||
|
|
||||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0)
|
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||||
apdu_sent = apdu_process(ITF_HID, msg_packet.data, msg_packet.len);
|
apdu_sent = apdu_process(ITF_HID, msg_packet.data, msg_packet.len);
|
||||||
else
|
} else {
|
||||||
apdu_sent = apdu_process(ITF_HID, ctap_req->init.data, MSG_LEN(ctap_req));
|
apdu_sent = apdu_process(ITF_HID, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||||
|
}
|
||||||
DEBUG_PAYLOAD(apdu.data, (int) apdu.nc);
|
DEBUG_PAYLOAD(apdu.data, (int) apdu.nc);
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
}
|
} else if ((last_cmd == CTAPHID_CBOR ||
|
||||||
else if ((last_cmd == CTAPHID_CBOR || (last_cmd >= CTAPHID_VENDOR_FIRST && last_cmd <= CTAPHID_VENDOR_LAST)) && (msg_packet.len == 0 || (msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
(last_cmd >= CTAPHID_VENDOR_FIRST && last_cmd <= CTAPHID_VENDOR_LAST)) &&
|
||||||
|
(msg_packet.len == 0 ||
|
||||||
|
(msg_packet.len == msg_packet.current_len && msg_packet.len > 0))) {
|
||||||
|
|
||||||
//if (thread_type != 2)
|
//if (thread_type != 2)
|
||||||
card_start(cbor_thread);
|
card_start(cbor_thread);
|
||||||
thread_type = 2;
|
thread_type = 2;
|
||||||
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0)
|
if (msg_packet.current_len == msg_packet.len && msg_packet.len > 0) {
|
||||||
apdu_sent = cbor_process(last_cmd, msg_packet.data, msg_packet.len);
|
apdu_sent = cbor_process(last_cmd, msg_packet.data, msg_packet.len);
|
||||||
else
|
} else {
|
||||||
apdu_sent = cbor_process(last_cmd, ctap_req->init.data, MSG_LEN(ctap_req));
|
apdu_sent = cbor_process(last_cmd, ctap_req->init.data, MSG_LEN(ctap_req));
|
||||||
|
}
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
if (apdu_sent < 0)
|
if (apdu_sent < 0) {
|
||||||
return ctap_error(-apdu_sent);
|
return ctap_error(-apdu_sent);
|
||||||
}
|
}
|
||||||
else if (ctap_req->init.cmd == CTAPHID_CANCEL) {
|
} else if (ctap_req->init.cmd == CTAPHID_CANCEL) {
|
||||||
ctap_error(0x2D);
|
ctap_error(0x2D);
|
||||||
msg_packet.len = msg_packet.current_len = 0;
|
msg_packet.len = msg_packet.current_len = 0;
|
||||||
last_packet_time = 0;
|
last_packet_time = 0;
|
||||||
cancel_button = true;
|
cancel_button = true;
|
||||||
}
|
} else {
|
||||||
else {
|
if (msg_packet.len == 0) {
|
||||||
if (msg_packet.len == 0)
|
|
||||||
return ctap_error(CTAP1_ERR_INVALID_CMD);
|
return ctap_error(CTAP1_ERR_INVALID_CMD);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// echo back anything we received from host
|
// echo back anything we received from host
|
||||||
//tud_hid_report(0, buffer, bufsize);
|
//tud_hid_report(0, buffer, bufsize);
|
||||||
//printf("END\n");
|
//printf("END\n");
|
||||||
@@ -398,7 +439,8 @@ int driver_process_usb_packet_hid(uint16_t read) {
|
|||||||
return apdu_sent;
|
return apdu_sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_keepalive() {
|
void send_keepalive()
|
||||||
|
{
|
||||||
CTAPHID_FRAME *resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + 4096);
|
CTAPHID_FRAME *resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + 4096);
|
||||||
//memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
//memset(ctap_resp, 0, sizeof(CTAPHID_FRAME));
|
||||||
resp->cid = ctap_req->cid;
|
resp->cid = ctap_req->cid;
|
||||||
@@ -409,12 +451,15 @@ void send_keepalive() {
|
|||||||
hid_write_offset(64, 4096);
|
hid_write_offset(64, 4096);
|
||||||
}
|
}
|
||||||
|
|
||||||
void driver_exec_timeout_hid() {
|
void driver_exec_timeout_hid()
|
||||||
if (thread_type == 2)
|
{
|
||||||
|
if (thread_type == 2) {
|
||||||
send_keepalive();
|
send_keepalive();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t *driver_prepare_response_hid() {
|
uint8_t *driver_prepare_response_hid()
|
||||||
|
{
|
||||||
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
ctap_resp = (CTAPHID_FRAME *) usb_get_tx(ITF_HID);
|
||||||
apdu.rdata = ctap_resp->init.data;
|
apdu.rdata = ctap_resp->init.data;
|
||||||
send_buffer_size = 0;
|
send_buffer_size = 0;
|
||||||
@@ -422,17 +467,20 @@ uint8_t *driver_prepare_response_hid() {
|
|||||||
return ctap_resp->init.data;
|
return ctap_resp->init.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void driver_exec_finished_hid(size_t size_next) {
|
void driver_exec_finished_hid(size_t size_next)
|
||||||
|
{
|
||||||
if (size_next > 0) {
|
if (size_next > 0) {
|
||||||
if (thread_type == 2 && apdu.sw != 0)
|
if (thread_type == 2 && apdu.sw != 0) {
|
||||||
ctap_error(apdu.sw & 0xff);
|
ctap_error(apdu.sw & 0xff);
|
||||||
else
|
} else {
|
||||||
driver_exec_finished_cont_hid(size_next, 7);
|
driver_exec_finished_cont_hid(size_next, 7);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
apdu.sw = 0;
|
apdu.sw = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void driver_exec_finished_cont_hid(size_t size_next, size_t offset) {
|
void driver_exec_finished_cont_hid(size_t size_next, size_t offset)
|
||||||
|
{
|
||||||
offset -= 7;
|
offset -= 7;
|
||||||
ctap_resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + offset);
|
ctap_resp = (CTAPHID_FRAME *) (usb_get_tx(ITF_HID) + offset);
|
||||||
ctap_resp->cid = ctap_req->cid;
|
ctap_resp->cid = ctap_req->cid;
|
||||||
|
|||||||
@@ -55,7 +55,8 @@
|
|||||||
#error CFG_TUSB_MCU must be defined
|
#error CFG_TUSB_MCU must be defined
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
|
#if CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || \
|
||||||
|
CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \
|
||||||
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56
|
CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56
|
||||||
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
|
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
|
||||||
#else
|
#else
|
||||||
@@ -125,4 +126,3 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* _TUSB_CONFIG_H_ */
|
#endif /* _TUSB_CONFIG_H_ */
|
||||||
|
|
||||||
|
|||||||
140
src/usb/usb.c
140
src/usb/usb.c
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* This file is part of the Pico HSM SDK distribution (https://github.com/polhenarejos/pico-hsm-sdk).
|
* This file is part of the Pico HSM SDK distribution (https://github.com/polhenarejos/pico-hsm-sdk).
|
||||||
* Copyright (c) 2022 Pol Henarejos.
|
* Copyright (c) 2022 Pol Henarejos.
|
||||||
@@ -40,70 +39,83 @@ static uint16_t w_len[ITF_TOTAL] = {0}, tx_r_offset[ITF_TOTAL] = {0};
|
|||||||
static uint32_t timeout_counter[ITF_TOTAL] = { 0 };
|
static uint32_t timeout_counter[ITF_TOTAL] = { 0 };
|
||||||
uint8_t card_locked_itf = ITF_TOTAL; // no locked
|
uint8_t card_locked_itf = ITF_TOTAL; // no locked
|
||||||
|
|
||||||
void usb_set_timeout_counter(uint8_t itf, uint32_t v) {
|
void usb_set_timeout_counter(uint8_t itf, uint32_t v)
|
||||||
|
{
|
||||||
timeout_counter[itf] = v;
|
timeout_counter[itf] = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset) {
|
uint32_t usb_write_offset(uint8_t itf, uint16_t len, uint16_t offset)
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
uint8_t pkt_max = 64;
|
uint8_t pkt_max = 64;
|
||||||
#endif
|
#endif
|
||||||
int w = 0;
|
int w = 0;
|
||||||
if (len > sizeof(tx_buffer[itf]))
|
if (len > sizeof(tx_buffer[itf])) {
|
||||||
len = sizeof(tx_buffer[itf]);
|
len = sizeof(tx_buffer[itf]);
|
||||||
|
}
|
||||||
w_len[itf] = len;
|
w_len[itf] = len;
|
||||||
tx_r_offset[itf] = offset;
|
tx_r_offset[itf] = offset;
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
w = driver_write_hid(tx_buffer[itf]+offset, MIN(len, pkt_max));
|
w = driver_write_hid(tx_buffer[itf]+offset, MIN(len, pkt_max));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
w = driver_write_ccid(tx_buffer[itf]+offset, MIN(len, pkt_max));
|
w = driver_write_ccid(tx_buffer[itf]+offset, MIN(len, pkt_max));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
if (itf == ITF_EMUL)
|
if (itf == ITF_EMUL) {
|
||||||
w = driver_write_emul(tx_buffer[itf]+offset, len);
|
w = driver_write_emul(tx_buffer[itf]+offset, len);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
w_len[itf] -= w;
|
w_len[itf] -= w;
|
||||||
tx_r_offset[itf] += w;
|
tx_r_offset[itf] += w;
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len) {
|
size_t usb_rx(uint8_t itf, const uint8_t *buffer, size_t len)
|
||||||
|
{
|
||||||
uint16_t size = MIN(sizeof(rx_buffer[itf]) - w_offset[itf], len);
|
uint16_t size = MIN(sizeof(rx_buffer[itf]) - w_offset[itf], len);
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
size = driver_read_hid(rx_buffer[itf] + w_offset[itf], size);
|
size = driver_read_hid(rx_buffer[itf] + w_offset[itf], size);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
size = driver_read_ccid(rx_buffer[itf] + w_offset[itf], size);
|
size = driver_read_ccid(rx_buffer[itf] + w_offset[itf], size);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
else
|
#endif
|
||||||
|
} else {
|
||||||
memcpy(rx_buffer[itf] + w_offset[itf], buffer, size);
|
memcpy(rx_buffer[itf] + w_offset[itf], buffer, size);
|
||||||
|
}
|
||||||
w_offset[itf] += size;
|
w_offset[itf] += size;
|
||||||
}
|
}
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t usb_write_flush(uint8_t itf) {
|
uint32_t usb_write_flush(uint8_t itf)
|
||||||
|
{
|
||||||
int w = 0;
|
int w = 0;
|
||||||
if (w_len[itf] > 0) {
|
if (w_len[itf] > 0) {
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
w = driver_write_hid(tx_buffer[itf]+tx_r_offset[itf], MIN(w_len[itf], 64));
|
w = driver_write_hid(tx_buffer[itf]+tx_r_offset[itf], MIN(w_len[itf], 64));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
w = driver_write_ccid(tx_buffer[itf]+tx_r_offset[itf], MIN(w_len[itf], 64));
|
w = driver_write_ccid(tx_buffer[itf]+tx_r_offset[itf], MIN(w_len[itf], 64));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
if (itf == ITF_EMUL)
|
if (itf == ITF_EMUL) {
|
||||||
w = driver_write_emul(tx_buffer[itf]+tx_r_offset[itf], w_len[itf]);
|
w = driver_write_emul(tx_buffer[itf]+tx_r_offset[itf], w_len[itf]);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
tx_r_offset[itf] += w;
|
tx_r_offset[itf] += w;
|
||||||
w_len[itf] -= w;
|
w_len[itf] -= w;
|
||||||
@@ -111,27 +123,33 @@ uint32_t usb_write_flush(uint8_t itf) {
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t usb_write(uint8_t itf, uint16_t len) {
|
uint32_t usb_write(uint8_t itf, uint16_t len)
|
||||||
|
{
|
||||||
return usb_write_offset(itf, len, 0);
|
return usb_write_offset(itf, len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t usb_read_available(uint8_t itf) {
|
uint16_t usb_read_available(uint8_t itf)
|
||||||
|
{
|
||||||
return w_offset[itf] - r_offset[itf];
|
return w_offset[itf] - r_offset[itf];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t usb_write_available(uint8_t itf) {
|
uint16_t usb_write_available(uint8_t itf)
|
||||||
|
{
|
||||||
return w_len[itf] > 0;
|
return w_len[itf] > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *usb_get_rx(uint8_t itf) {
|
uint8_t *usb_get_rx(uint8_t itf)
|
||||||
|
{
|
||||||
return rx_buffer[itf];
|
return rx_buffer[itf];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *usb_get_tx(uint8_t itf) {
|
uint8_t *usb_get_tx(uint8_t itf)
|
||||||
|
{
|
||||||
return tx_buffer[itf];
|
return tx_buffer[itf];
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_clear_rx(uint8_t itf) {
|
void usb_clear_rx(uint8_t itf)
|
||||||
|
{
|
||||||
w_offset[itf] = r_offset[itf] = 0;
|
w_offset[itf] = r_offset[itf] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +167,8 @@ queue_t usb_to_card_q;
|
|||||||
queue_t card_to_usb_q;
|
queue_t card_to_usb_q;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void usb_init() {
|
void usb_init()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
queue_init(&card_to_usb_q, sizeof(uint32_t), 64);
|
queue_init(&card_to_usb_q, sizeof(uint32_t), 64);
|
||||||
queue_init(&usb_to_card_q, sizeof(uint32_t), 64);
|
queue_init(&usb_to_card_q, sizeof(uint32_t), 64);
|
||||||
@@ -159,7 +178,8 @@ void usb_init() {
|
|||||||
extern int driver_process_usb_nopacket();
|
extern int driver_process_usb_nopacket();
|
||||||
extern uint32_t timeout;
|
extern uint32_t timeout;
|
||||||
|
|
||||||
static int usb_event_handle(uint8_t itf) {
|
static int usb_event_handle(uint8_t itf)
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
uint16_t rx_read = usb_read_available(itf);
|
uint16_t rx_read = usb_read_available(itf);
|
||||||
#else
|
#else
|
||||||
@@ -167,16 +187,19 @@ static int usb_event_handle(uint8_t itf) {
|
|||||||
#endif
|
#endif
|
||||||
int proc_packet = 0;
|
int proc_packet = 0;
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
proc_packet = driver_process_usb_packet_hid(rx_read);
|
proc_packet = driver_process_usb_packet_hid(rx_read);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
proc_packet = driver_process_usb_packet_ccid(rx_read);
|
proc_packet = driver_process_usb_packet_ccid(rx_read);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
if (itf == ITF_EMUL)
|
if (itf == ITF_EMUL) {
|
||||||
proc_packet = driver_process_usb_packet_emul(rx_read);
|
proc_packet = driver_process_usb_packet_emul(rx_read);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
if (proc_packet > 0) {
|
if (proc_packet > 0) {
|
||||||
card_locked_itf = itf;
|
card_locked_itf = itf;
|
||||||
@@ -185,22 +208,24 @@ static int usb_event_handle(uint8_t itf) {
|
|||||||
queue_add_blocking(&usb_to_card_q, &flag);
|
queue_add_blocking(&usb_to_card_q, &flag);
|
||||||
#endif
|
#endif
|
||||||
timeout_start();
|
timeout_start();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
driver_process_usb_nopacket_hid();
|
driver_process_usb_nopacket_hid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
driver_process_usb_nopacket_ccid();
|
driver_process_usb_nopacket_ccid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void low_flash_init();
|
extern void low_flash_init();
|
||||||
void card_init_core1() {
|
void card_init_core1()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
low_flash_init_core1();
|
low_flash_init_core1();
|
||||||
#endif
|
#endif
|
||||||
@@ -208,24 +233,28 @@ void card_init_core1() {
|
|||||||
|
|
||||||
size_t finished_data_size = 0;
|
size_t finished_data_size = 0;
|
||||||
|
|
||||||
void card_start(void (*func)(void)) {
|
void card_start(void (*func)(void))
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
uint32_t m = 0;
|
uint32_t m = 0;
|
||||||
while (queue_is_empty(&usb_to_card_q) == false) {
|
while (queue_is_empty(&usb_to_card_q) == false) {
|
||||||
if (queue_try_remove(&usb_to_card_q, &m) == false)
|
if (queue_try_remove(&usb_to_card_q, &m) == false) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while (queue_is_empty(&card_to_usb_q) == false) {
|
while (queue_is_empty(&card_to_usb_q) == false) {
|
||||||
if (queue_try_remove(&card_to_usb_q, &m) == false)
|
if (queue_try_remove(&card_to_usb_q, &m) == false) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
multicore_reset_core1();
|
multicore_reset_core1();
|
||||||
multicore_launch_core1(func);
|
multicore_launch_core1(func);
|
||||||
led_set_blink(BLINK_MOUNTED);
|
led_set_blink(BLINK_MOUNTED);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void card_exit() {
|
void card_exit()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
uint32_t flag = EV_EXIT;
|
uint32_t flag = EV_EXIT;
|
||||||
queue_try_add(&usb_to_card_q, &flag);
|
queue_try_add(&usb_to_card_q, &flag);
|
||||||
@@ -234,7 +263,8 @@ void card_exit() {
|
|||||||
card_locked_itf = ITF_TOTAL;
|
card_locked_itf = ITF_TOTAL;
|
||||||
}
|
}
|
||||||
extern void hid_task();
|
extern void hid_task();
|
||||||
void usb_task() {
|
void usb_task()
|
||||||
|
{
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
bool mounted = false;
|
bool mounted = false;
|
||||||
#else
|
#else
|
||||||
@@ -242,12 +272,14 @@ void usb_task() {
|
|||||||
#endif
|
#endif
|
||||||
for (uint8_t itf = 0; itf < ITF_TOTAL; itf++) {
|
for (uint8_t itf = 0; itf < ITF_TOTAL; itf++) {
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
mounted = driver_mounted_hid();
|
mounted = driver_mounted_hid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
mounted = driver_mounted_ccid();
|
mounted = driver_mounted_ccid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (mounted == true) {
|
if (mounted == true) {
|
||||||
@@ -265,31 +297,33 @@ void usb_task() {
|
|||||||
if (m == EV_EXEC_FINISHED) {
|
if (m == EV_EXEC_FINISHED) {
|
||||||
timeout_stop();
|
timeout_stop();
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
driver_exec_finished_hid(finished_data_size);
|
driver_exec_finished_hid(finished_data_size);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
driver_exec_finished_ccid(finished_data_size);
|
driver_exec_finished_ccid(finished_data_size);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
led_set_blink(BLINK_MOUNTED);
|
led_set_blink(BLINK_MOUNTED);
|
||||||
card_locked_itf = ITF_TOTAL;
|
card_locked_itf = ITF_TOTAL;
|
||||||
}
|
} else if (m == EV_PRESS_BUTTON) {
|
||||||
else if (m == EV_PRESS_BUTTON) {
|
|
||||||
uint32_t flag = wait_button() ? EV_BUTTON_TIMEOUT : EV_BUTTON_PRESSED;
|
uint32_t flag = wait_button() ? EV_BUTTON_TIMEOUT : EV_BUTTON_PRESSED;
|
||||||
queue_try_add(&usb_to_card_q, &flag);
|
queue_try_add(&usb_to_card_q, &flag);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
if (timeout + timeout_counter[itf] < board_millis()) {
|
if (timeout + timeout_counter[itf] < board_millis()) {
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
driver_exec_timeout_hid();
|
driver_exec_timeout_hid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
driver_exec_timeout_ccid();
|
driver_exec_timeout_ccid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
timeout = board_millis();
|
timeout = board_millis();
|
||||||
}
|
}
|
||||||
@@ -305,18 +339,22 @@ void usb_task() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8_t *usb_prepare_response(uint8_t itf) {
|
uint8_t *usb_prepare_response(uint8_t itf)
|
||||||
|
{
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
return driver_prepare_response_hid();
|
return driver_prepare_response_hid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
if (itf == ITF_CCID)
|
if (itf == ITF_CCID) {
|
||||||
return driver_prepare_response_ccid();
|
return driver_prepare_response_ccid();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef ENABLE_EMULATION
|
#ifdef ENABLE_EMULATION
|
||||||
if (itf == ITF_EMUL)
|
if (itf == ITF_EMUL) {
|
||||||
return driver_prepare_response_emul();
|
return driver_prepare_response_emul();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,8 +56,7 @@ enum {
|
|||||||
ITF_TOTAL
|
ITF_TOTAL
|
||||||
};
|
};
|
||||||
|
|
||||||
enum
|
enum {
|
||||||
{
|
|
||||||
REPORT_ID_KEYBOARD = 1,
|
REPORT_ID_KEYBOARD = 1,
|
||||||
REPORT_ID_COUNT
|
REPORT_ID_COUNT
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ tusb_desc_configuration_t const desc_config =
|
|||||||
.bDescriptorType = TUSB_DESC_CONFIGURATION,
|
.bDescriptorType = TUSB_DESC_CONFIGURATION,
|
||||||
.wTotalLength = (sizeof(tusb_desc_configuration_t)
|
.wTotalLength = (sizeof(tusb_desc_configuration_t)
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
+ sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 2*sizeof(tusb_desc_endpoint_t)
|
+ sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) +
|
||||||
|
2*sizeof(tusb_desc_endpoint_t)
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
||||||
@@ -153,7 +154,9 @@ tusb_desc_endpoint_t const desc_ep2 =
|
|||||||
|
|
||||||
static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t)
|
static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t)
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
+ sizeof(tusb_desc_interface_t) + sizeof(struct ccid_class_descriptor) + 2*sizeof(tusb_desc_endpoint_t)
|
+ sizeof(tusb_desc_interface_t) +
|
||||||
|
sizeof(struct ccid_class_descriptor) + 2*
|
||||||
|
sizeof(tusb_desc_endpoint_t)
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
+ TUD_HID_INOUT_DESC_LEN + TUD_HID_DESC_LEN
|
||||||
@@ -163,8 +166,7 @@ static uint8_t desc_config_extended[sizeof(tusb_desc_configuration_t)
|
|||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
#define HID_USAGE_PAGE_FIDO 0xF1D0
|
#define HID_USAGE_PAGE_FIDO 0xF1D0
|
||||||
|
|
||||||
enum
|
enum {
|
||||||
{
|
|
||||||
HID_USAGE_FIDO_U2FHID = 0x01,// U2FHID usage for top-level collection
|
HID_USAGE_FIDO_U2FHID = 0x01,// U2FHID usage for top-level collection
|
||||||
HID_USAGE_FIDO_DATA_IN = 0x20,// Raw IN data report
|
HID_USAGE_FIDO_DATA_IN = 0x20,// Raw IN data report
|
||||||
HID_USAGE_FIDO_DATA_OUT = 0x21 // Raw OUT data report
|
HID_USAGE_FIDO_DATA_OUT = 0x21 // Raw OUT data report
|
||||||
@@ -203,20 +205,29 @@ uint8_t const desc_hid_report_kb[] =
|
|||||||
#define EPNUM_HID 0x03
|
#define EPNUM_HID 0x03
|
||||||
|
|
||||||
static uint8_t desc_hid[] = {
|
static uint8_t desc_hid[] = {
|
||||||
TUD_HID_INOUT_DESCRIPTOR(ITF_HID, ITF_HID+5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, 0x80 | EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
|
TUD_HID_INOUT_DESCRIPTOR(ITF_HID,
|
||||||
|
ITF_HID+5,
|
||||||
|
HID_ITF_PROTOCOL_NONE,
|
||||||
|
sizeof(desc_hid_report),
|
||||||
|
EPNUM_HID,
|
||||||
|
0x80 | EPNUM_HID,
|
||||||
|
CFG_TUD_HID_EP_BUFSIZE,
|
||||||
|
10)
|
||||||
};
|
};
|
||||||
|
|
||||||
static uint8_t desc_hid_kb[] = {
|
static uint8_t desc_hid_kb[] = {
|
||||||
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD+5, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report_kb), 0x80 | (EPNUM_HID+1), 16, 5)
|
TUD_HID_DESCRIPTOR(ITF_KEYBOARD, ITF_KEYBOARD+5, HID_ITF_PROTOCOL_NONE,
|
||||||
|
sizeof(desc_hid_report_kb), 0x80 | (EPNUM_HID+1), 16, 5)
|
||||||
};
|
};
|
||||||
|
|
||||||
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf)
|
uint8_t const *tud_hid_descriptor_report_cb(uint8_t itf)
|
||||||
{
|
{
|
||||||
printf("report_cb %d\n", itf);
|
printf("report_cb %d\n", itf);
|
||||||
if (itf == ITF_HID)
|
if (itf == ITF_HID) {
|
||||||
return desc_hid_report;
|
return desc_hid_report;
|
||||||
else if (itf == ITF_KEYBOARD)
|
} else if (itf == ITF_KEYBOARD) {
|
||||||
return desc_hid_report_kb;
|
return desc_hid_report_kb;
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -226,17 +237,19 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
|
|||||||
(void) index; // for multiple configurations
|
(void) index; // for multiple configurations
|
||||||
|
|
||||||
static uint8_t initd = 0;
|
static uint8_t initd = 0;
|
||||||
if (initd == 0)
|
if (initd == 0) {
|
||||||
{
|
|
||||||
uint8_t *p = desc_config_extended;
|
uint8_t *p = desc_config_extended;
|
||||||
memcpy(p, &desc_config, sizeof(tusb_desc_configuration_t)); p += sizeof(tusb_desc_configuration_t);
|
memcpy(p, &desc_config, sizeof(tusb_desc_configuration_t));
|
||||||
|
p += sizeof(tusb_desc_configuration_t);
|
||||||
#ifdef USB_ITF_HID
|
#ifdef USB_ITF_HID
|
||||||
memcpy(p, &desc_hid, sizeof(desc_hid)); p += sizeof(desc_hid);
|
memcpy(p, &desc_hid, sizeof(desc_hid)); p += sizeof(desc_hid);
|
||||||
memcpy(p, &desc_hid_kb, sizeof(desc_hid_kb)); p += sizeof(desc_hid_kb);
|
memcpy(p, &desc_hid_kb, sizeof(desc_hid_kb)); p += sizeof(desc_hid_kb);
|
||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_CCID
|
#ifdef USB_ITF_CCID
|
||||||
memcpy(p, &desc_interface, sizeof(tusb_desc_interface_t)); p += sizeof(tusb_desc_interface_t);
|
memcpy(p, &desc_interface, sizeof(tusb_desc_interface_t));
|
||||||
memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor)); p += sizeof(struct ccid_class_descriptor);
|
p += sizeof(tusb_desc_interface_t);
|
||||||
|
memcpy(p, &desc_ccid, sizeof(struct ccid_class_descriptor));
|
||||||
|
p += sizeof(struct ccid_class_descriptor);
|
||||||
memcpy(p, &desc_ep1, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
memcpy(p, &desc_ep1, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
||||||
memcpy(p, &desc_ep2, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
memcpy(p, &desc_ep2, sizeof(tusb_desc_endpoint_t)); p += sizeof(tusb_desc_endpoint_t);
|
||||||
#endif
|
#endif
|
||||||
@@ -292,13 +305,13 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
|||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||||
chr_count = 1;
|
chr_count = 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
|
||||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||||
|
|
||||||
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) )
|
if (!(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0]))) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
const char *str = string_desc_arr[index];
|
const char *str = string_desc_arr[index];
|
||||||
char unique_id_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
|
char unique_id_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1];
|
||||||
@@ -310,8 +323,9 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
|
|||||||
}
|
}
|
||||||
|
|
||||||
chr_count = strlen(str);
|
chr_count = strlen(str);
|
||||||
if ( chr_count > 31 )
|
if (chr_count > 31) {
|
||||||
chr_count = 31;
|
chr_count = 31;
|
||||||
|
}
|
||||||
|
|
||||||
// Convert ASCII string into UTF-16
|
// Convert ASCII string into UTF-16
|
||||||
for (uint8_t i = 0; i < chr_count; i++) {
|
for (uint8_t i = 0; i < chr_count; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user