summaryrefslogtreecommitdiff
path: root/yhy_test.c
blob: 65cfa4a3c1090483d1da73175400af9be4f874e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#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);
}