Make emulation build cleanly with MSVC

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos
2026-07-29 19:08:14 +02:00
parent 48974a6e24
commit 6f8fd64754
9 changed files with 46 additions and 25 deletions
+2 -2
View File
@@ -264,7 +264,7 @@ void sm_update_iv(void) {
int sm_verify(void) {
uint8_t input[USB_BUFFER_SIZE];
memset(input, 0, sizeof(input));
uint16_t input_len = 0;
size_t input_len = 0;
int r = 0;
bool add_header = (CLA(apdu) & 0xC) == 0xC;
size_t data_len = (size_t)(apdu.nc / sm_blocksize) * sm_blocksize;
@@ -294,7 +294,7 @@ int sm_verify(void) {
}
bool some_added = false;
const uint8_t *mac = NULL;
uint16_t mac_len = 0;
size_t mac_len = 0;
uint8_t *p = NULL;
tlv_item_t item;
tlv_ctx_t ctxi;
+8 -7
View File
@@ -122,12 +122,13 @@ void file_process_fci(const file_t *pe, int fmd) {
memcpy(res_APDU + res_APDU_size, "\x8A\x01\x05", 3); //life-cycle (5 -> activated)
res_APDU_size += 3;
byte_array_t metadata = meta_find(pe->fid);
if (metadata.len > 0 && metadata.data != NULL) {
if (metadata.len > 0 && metadata.len <= UINT8_MAX && metadata.data != NULL) {
uint16_t metadata_len = (uint16_t)metadata.len;
res_APDU[res_APDU_size++] = 0xA5;
res_APDU[res_APDU_size++] = 0x81;
res_APDU[res_APDU_size++] = (uint8_t)metadata.len;
memcpy(res_APDU + res_APDU_size, metadata.data, metadata.len);
res_APDU_size += metadata.len;
res_APDU[res_APDU_size++] = (uint8_t)metadata_len;
memcpy(res_APDU + res_APDU_size, metadata.data, metadata_len);
res_APDU_size += metadata_len;
}
res_APDU[1] = (uint8_t)res_APDU_size - 2;
if (fmd) {
@@ -517,7 +518,7 @@ static int meta_delete_internal(uint16_t fid, bool commit) {
}
uint16_t cfid = get_uint16_be(tag_data);
if (cfid == fid) {
uint16_t new_len = ctxi.len - 1 - tag_len - tlv_format_len(tag_len, NULL);
size_t new_len = ctxi.len - 1 - tag_len - tlv_format_len(tag_len, NULL);
if (new_len == 0) {
flash_clear_file(ef);
}
@@ -564,7 +565,7 @@ int meta_add(uint16_t fid, const_byte_array_t data) {
if (!ef) {
return PICOKEYS_ERR_FILE_NOT_FOUND;
}
uint16_t ef_size = file_get_size(ef);
uint32_t ef_size = file_get_size(ef);
uint8_t *fdata = (uint8_t *) calloc(1, ef_size);
memcpy(fdata, file_get_data(ef), ef_size);
uint8_t *p = NULL;
@@ -680,7 +681,7 @@ int flash_clear_file(file_t *file) {
if (flash_program_block(payload_addr + offset, CONST_BYTE_ARRAY(zeros, chunk)) != PICOKEYS_OK) {
return PICOKEYS_EXEC_ERROR;
}
offset += chunk;
offset += (uint32_t)chunk;
}
if (next_addr > 0) {
flash_program_uintptr(next_addr + sizeof(uintptr_t), prev_addr);
+1 -1
View File
@@ -155,7 +155,7 @@ static int copy_file_range(const file_t *source, uint32_t source_offset, uintptr
uint8_t buffer[256];
while (len > 0) {
size_t chunk = MIN(sizeof(buffer), len);
uint32_t chunk = MIN((uint32_t)sizeof(buffer), len);
int r = file_read_at(source, source_offset, BYTE_ARRAY(buffer, chunk));
if (r != PICOKEYS_OK) {
return r;
+4 -4
View File
@@ -230,14 +230,14 @@ int file_object_manifest_build(const file_object_manifest_t *manifest, const_byt
}
uint32_t descriptor_bytes = (uint32_t)object_count * FILE_OBJECT_DESCRIPTOR_SIZE;
uint32_t total_size = FILE_OBJECT_MANIFEST_HEADER_SIZE + descriptor_bytes + extensions_size + FILE_OBJECT_AUTH_TAG_SIZE;
uint16_t extension_start = FILE_OBJECT_MANIFEST_HEADER_SIZE + descriptor_bytes;
uint16_t extension_end = extension_start + extensions_size;
if (manifest->generation == 0 || manifest->previous_generation >= manifest->generation) {
return PICOKEYS_WRONG_DATA;
}
if (total_size > UINT16_MAX || output->capacity - output->len < total_size) {
return PICOKEYS_WRONG_LENGTH;
}
uint16_t extension_start = (uint16_t)(FILE_OBJECT_MANIFEST_HEADER_SIZE + descriptor_bytes);
uint16_t extension_end = (uint16_t)(extension_start + extensions_size);
if (!file_object_descriptors_valid(manifest->objects, object_count, extension_start, extension_end)) {
return PICOKEYS_WRONG_DATA;
}
@@ -313,8 +313,8 @@ int file_object_manifest_parse(const_byte_array_t data, const file_object_authen
return r;
}
uint16_t extension_start = FILE_OBJECT_MANIFEST_HEADER_SIZE + descriptor_bytes;
uint16_t extension_end = extension_start + extensions_size;
uint16_t extension_start = (uint16_t)(FILE_OBJECT_MANIFEST_HEADER_SIZE + descriptor_bytes);
uint16_t extension_end = (uint16_t)(extension_start + extensions_size);
r = file_object_extensions_validate(CONST_BYTE_ARRAY(data.data + extension_start, extensions_size), extension_supported, extension_ctx);
if (r != PICOKEYS_OK) {
return r;
+1 -1
View File
@@ -136,7 +136,7 @@ static int file_object_txn_auth_record(const file_object_authenticator_t *auth,
uint8_t buffer[FILE_OBJECT_TXN_READ_CHUNK_SIZE];
uint32_t offset = 0;
while (r == PICOKEYS_OK && offset < payload_size) {
size_t chunk = MIN(sizeof(buffer), payload_size - offset);
uint32_t chunk = MIN((uint32_t)sizeof(buffer), payload_size - offset);
r = file_read_at(file, FILE_OBJECT_TXN_RECORD_HEADER_SIZE + offset, BYTE_ARRAY(buffer, chunk));
if (r == PICOKEYS_OK) {
r = auth->update(auth->ctx, CONST_BYTE_ARRAY(buffer, chunk));
+1 -1
View File
@@ -120,7 +120,7 @@ static int encrypt_internal_keydev(file_t *ef_devcert_key, const uint8_t pkey[DE
}
static int decrypt_internal_keydev(file_t *ef_devcert_key, uint8_t pkey[DEVCERT_KEY_PLAIN_SIZE], bool *legacy) {
uint16_t record_len = file_get_size(ef_devcert_key);
uint32_t record_len = file_get_size(ef_devcert_key);
const uint8_t *record = file_get_data(ef_devcert_key);
uint8_t kbase[32] = { 0 };
derive_kbase(kbase);
+5 -1
View File
@@ -180,10 +180,14 @@ void tud_vendor_rx_cb(uint8_t itf, const uint8_t *buffer, uint16_t bufsize) {
}
static int driver_write_ccid(uint8_t itf, const_byte_array_t buffer) {
if (buffer.len > UINT16_MAX) {
return 0;
}
uint16_t buffer_len = (uint16_t)buffer.len;
if (buffer.len > 0 && buffer.data[0] != 0x81) {
DEBUG_PAYLOAD(buffer.data, buffer.len);
}
uint32_t written = tud_vendor_n_write(itf, buffer.data, buffer.len);
uint32_t written = tud_vendor_n_write(itf, buffer.data, buffer_len);
if (written > 0) {
tud_vendor_n_write_flush(itf);
+17 -5
View File
@@ -220,7 +220,11 @@ bool tud_hid_n_report(uint8_t itf, uint8_t report_id, const uint8_t *buffer, uin
#endif
uint16_t driver_write_emul(uint8_t itf, const_byte_array_t buffer) {
uint16_t size = htons((uint16_t)buffer.len);
if (buffer.len > UINT16_MAX) {
return 0;
}
uint16_t buffer_len = (uint16_t)buffer.len;
uint16_t size = htons(buffer_len);
socket_t sock = get_sock_itf(itf);
if (sock == INVALID_SOCKET) {
return 0;
@@ -238,13 +242,13 @@ uint16_t driver_write_emul(uint8_t itf, const_byte_array_t buffer) {
}
} while (ret <= 0);
do {
ret = send(sock, (const char *)buffer.data, buffer.len, 0);
ret = send(sock, (const char *)buffer.data, (int)buffer_len, 0);
if (ret == SOCKET_ERROR) {
msleep(10);
}
} while (ret <= 0);
emul_tx_size = (uint16_t)buffer.len;
return (uint16_t)buffer.len;
emul_tx_size = buffer_len;
return buffer_len;
}
void driver_exec_finished_cont_emul(uint8_t itf, uint16_t size_next, uint16_t offset) {
@@ -268,7 +272,11 @@ uint16_t emul_read(uint8_t itf) {
socklen_t client_socklen = sizeof client_sockaddr;
int timeout;
#ifdef _MSC_VER
WSAPOLLFD pfd;
#else
struct pollfd pfd;
#endif
pfd.fd = hid_server_sock;
pfd.events = POLLIN;
@@ -276,7 +284,11 @@ uint16_t emul_read(uint8_t itf) {
timeout = (0 * 1000 + 1000 / 1000);
#ifdef _MSC_VER
if (WSAPoll(&pfd, 1, timeout) == SOCKET_ERROR) {
#else
if (poll(&pfd, 1, timeout) == -1) {
#endif
return 0;
}
@@ -286,7 +298,7 @@ uint16_t emul_read(uint8_t itf) {
}
hid_client_sock = accept(hid_server_sock, (struct sockaddr *) &client_sockaddr, &client_socklen);
if (hid_client_sock != INVALID_SOCKET) {
printf("hid_client connected! %d\n", hid_client_sock);
printf("hid_client connected! %llu\n", (unsigned long long)hid_client_sock);
}
}
/*if (send_buffer_size > 0) {
+7 -3
View File
@@ -256,18 +256,22 @@ void tud_hid_report_complete_cb(uint8_t instance, uint8_t const *report, uint16_
}
int driver_write_hid(uint8_t itf, const_byte_array_t buffer) {
if ((!buffer.data && buffer.len > 0) || buffer.len > UINT16_MAX) {
return 0;
}
uint16_t buffer_len = (uint16_t)buffer.len;
if (last_write_result[itf] == WRITE_PENDING) {
return 0;
}
bool r = tud_hid_n_report(itf, 0, buffer.data, buffer.len);
bool r = tud_hid_n_report(itf, 0, buffer.data, buffer_len);
last_write_result[itf] = r ? WRITE_PENDING : WRITE_FAILED;
if (last_write_result[itf] == WRITE_FAILED) {
return 0;
}
#ifdef ENABLE_EMULATION
tud_hid_report_complete_cb(ITF_HID_CTAP, buffer.data, buffer.len);
tud_hid_report_complete_cb(ITF_HID_CTAP, buffer.data, buffer_len);
#endif
return MIN(64u, buffer.len);
return buffer_len > 64 ? 64 : buffer_len;
}
int (*hid_set_report_cb)(uint8_t, uint8_t, hid_report_type_t, uint8_t const *, uint16_t) = NULL;