From f0cb3e813a51034dd0d8051aa7165b926e0d8411 Mon Sep 17 00:00:00 2001 From: cbdev Date: Sun, 18 Jun 2023 13:07:25 +0200 Subject: Initial basic library --- yhy_test.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 yhy_test.c (limited to 'yhy_test.c') diff --git a/yhy_test.c b/yhy_test.c new file mode 100644 index 0000000..65cfa4a --- /dev/null +++ b/yhy_test.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include "libyhy.h" + +#define ATQA_UIDSIZE_BITS(x) ((x & 0x00C0) >> 6) +#define UID_CASCADE_TAG 0x88 +#define SAK_UID_INCOMPLETE(x) (x & 0x04) + +int main(int argc, char** argv){ + size_t n; + uint8_t test_block = 4; + int fd = yhy_open("/dev/ttyUSB0"); + + printf("YHY Test: %d\n", fd); + yhy_sync_led(fd, 3); + //yhy_sync_beep(fd, 0x64); + + printf("Reading version\n"); + char version[512]; + yhy_sync_read_version(fd, version, 512); + printf("Version %s\n\n", version); + + size_t bytes; + uint16_t atqa = 0; + uint8_t sak = 0; + uint8_t serial[10] = ""; + + //iterate cards in field + printf("Scanning field\n\n"); + + for(yhy_sync_request(fd, 1, &atqa); atqa; yhy_sync_request(fd, 0, &atqa)){ + printf("ATQA %04X\n", atqa); + //ATQA and SAK should not be used for identifying PICCs. + //However, we don't have lower-level access to the APDUs + printf("ATQA UID size: %d\n\n", ATQA_UIDSIZE_BITS(atqa)); + + if(ATQA_UIDSIZE_BITS(atqa) == 1){ + printf("Based on ATQA, running UL select\n"); + bytes = yhy_sync_ulselect(fd, serial, sizeof(serial)); + printf("Received %lu bytes\n", bytes); + } + else{ + printf("Performing Anticollision (Cascade Level 1)\n"); + bytes = yhy_sync_anticoll(fd, serial, sizeof(serial)); + printf("UID %lu bytes, %02X %02X %02X %02X\n", bytes, + serial[0], + serial[1], + serial[2], + serial[3]); + + if(serial[0] == UID_CASCADE_TAG){ + printf("UID has CT prefix, UID is yet incomplete\n"); + } + + printf("\nSelecting (Cascade Level 1)\n"); + yhy_sync_select(fd, serial, 4, &sak); + printf("SAK CL1 %02X\n", sak); + if(SAK_UID_INCOMPLETE(sak)){ + printf("SAK CL1 indicates incomplete UID\n"); + } + } + + printf("\nHalting card\n"); + yhy_sync_hlta(fd); + } + + printf("\nAll PICCs enumerated\n"); + + /* + printf("Authenticating for block %d\n", test_block); + uint8_t key[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + yhy_sync_auth(fd, 1, test_block, key); + printf("\n"); + + printf("Reading block %d\n", test_block); + uint8_t data[16] = {0}; + yhy_sync_read(fd, test_block, data, 16); + printf("Block %d data: ", test_block); + for(n = 0; n < 16; n++){ + printf("%02x ", data[n]); + } + printf("\n\n"); + + printf("Incrementing all bytes in block %d\n", test_block); + for(n = 0; n < 16; n++){ + data[n]++; + } + + printf("Writing block %d\n", test_block); + yhy_sync_write(fd, test_block, data); + printf("\n"); + */ + + close(fd); +} -- cgit v1.2.3