Switching to new style.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2023-02-15 00:10:35 +01:00
parent e7495d11f2
commit cd6e280f4f
33 changed files with 1067 additions and 642 deletions

View File

@@ -27,20 +27,18 @@
extern void scan_all();
extern char __StackLimit;
int heapLeft()
{
int heapLeft() {
#ifndef ENABLE_EMULATION
char *p = malloc(256); // try to avoid undue fragmentation
int left = &__StackLimit - p;
free(p);
#else
int left = 1024*1024;
int left = 1024 * 1024;
#endif
return left;
}
int cmd_initialize()
{
int cmd_initialize() {
if (apdu.nc > 0) {
uint8_t mkek[MKEK_SIZE];
int ret_mkek = load_mkek(mkek); //Try loading MKEK with previous session
@@ -54,25 +52,28 @@ int cmd_initialize()
if (tag == 0x80) { //options
file_t *tf = search_by_fid(EF_DEVOPS, NULL, SPECIFY_EF);
flash_write_data_to_file(tf, tag_data, tag_len);
} else if (tag == 0x81) { //user pin
}
else if (tag == 0x81) { //user pin
if (file_pin1 && file_pin1->data) {
uint8_t dhash[33];
dhash[0] = tag_len;
double_hash_pin(tag_data, tag_len, dhash+1);
double_hash_pin(tag_data, tag_len, dhash + 1);
flash_write_data_to_file(file_pin1, dhash, sizeof(dhash));
hash_multi(tag_data, tag_len, session_pin);
has_session_pin = true;
}
} else if (tag == 0x82) { //sopin pin
}
else if (tag == 0x82) { //sopin pin
if (file_sopin && file_sopin->data) {
uint8_t dhash[33];
dhash[0] = tag_len;
double_hash_pin(tag_data, tag_len, dhash+1);
double_hash_pin(tag_data, tag_len, dhash + 1);
flash_write_data_to_file(file_sopin, dhash, sizeof(dhash));
hash_multi(tag_data, tag_len, session_sopin);
has_session_sopin = true;
}
} else if (tag == 0x91) { //retries user pin
}
else if (tag == 0x91) { //retries user pin
file_t *tf = search_by_fid(0x1082, NULL, SPECIFY_EF);
if (tf && tf->data) {
flash_write_data_to_file(tf, tag_data, tag_len);
@@ -80,7 +81,8 @@ int cmd_initialize()
if (file_retries_pin1 && file_retries_pin1->data) {
flash_write_data_to_file(file_retries_pin1, tag_data, tag_len);
}
} else if (tag == 0x92) {
}
else if (tag == 0x92) {
dkeks = tag_data;
file_t *tf = file_new(EF_DKEK);
if (!tf) {
@@ -88,7 +90,8 @@ int cmd_initialize()
return SW_MEMORY_FAILURE();
}
flash_write_data_to_file(tf, NULL, 0);
} else if (tag == 0x93) {
}
else if (tag == 0x93) {
file_t *ef_puk = search_by_fid(EF_PUKAUT, NULL, SPECIFY_EF);
if (!ef_puk) {
release_mkek(mkek);
@@ -101,14 +104,15 @@ int cmd_initialize()
pk_status[2] = tag_data[1];
flash_write_data_to_file(ef_puk, pk_status, sizeof(pk_status));
for (int i = 0; i < puks; i++) {
file_t *tf = file_new(EF_PUK+i);
file_t *tf = file_new(EF_PUK + i);
if (!tf) {
release_mkek(mkek);
return SW_MEMORY_FAILURE();
}
flash_write_data_to_file(tf, NULL, 0);
}
} else if (tag == 0x97) {
}
else if (tag == 0x97) {
kds = tag_data;
/*
for (int i = 0; i < MIN(*kds,MAX_KEY_DOMAINS); i++) {
@@ -139,7 +143,8 @@ int cmd_initialize()
if (flash_write_data_to_file(tf_kd, (const uint8_t *) &d, sizeof(d)) != CCID_OK) {
return SW_EXEC_ERROR();
}
} else {
}
else {
int r = save_dkek_key(0, random_bytes_get(32));
if (r != CCID_OK) {
return SW_EXEC_ERROR();
@@ -149,16 +154,17 @@ int cmd_initialize()
return SW_EXEC_ERROR();
}
}
} else {
}
else {
uint16_t d = 0x0000;
if (flash_write_data_to_file(tf_kd, (const uint8_t *) &d, sizeof(d)) != CCID_OK) {
return SW_EXEC_ERROR();
}
}
if (kds) {
uint8_t t[MAX_KEY_DOMAINS*2], k = MIN(*kds, MAX_KEY_DOMAINS);
memset(t, 0xff, 2*k);
if (flash_write_data_to_file(tf_kd, t, 2*k) != CCID_OK) {
uint8_t t[MAX_KEY_DOMAINS * 2], k = MIN(*kds, MAX_KEY_DOMAINS);
memset(t, 0xff, 2 * k);
if (flash_write_data_to_file(tf_kd, t, 2 * k) != CCID_OK) {
return SW_EXEC_ERROR();
}
}
@@ -215,7 +221,8 @@ int cmd_initialize()
return SW_EXEC_ERROR();
}
low_flash_available();
} else { //free memory bytes request
}
else { //free memory bytes request
int heap_left = heapLeft();
res_APDU[0] = ((heap_left >> 24) & 0xff);
res_APDU[1] = ((heap_left >> 16) & 0xff);