hf mf fchk - output style
[RRG-proxmark3.git] / tools / hitag2crack / crack2 / ht2crack2gentest.c
blobcc8d1e94ee730ae4d1e8817cada7fd94e5eb3042
1 /*
2 * ht2crack2gentests.c
3 * this uses the RFIDler hitag2 PRNG code to generate test cases to test the tables
4 */
6 #include "ht2crackutils.h"
8 static int makerandom(char *hex, unsigned int len, int fd) {
9 unsigned char raw[32];
10 int i;
12 if (!hex) {
13 printf("makerandom: hex is NULL\n");
14 exit(1);
17 if (!len || (len > 32)) {
18 printf("makerandom: len must be between 1 and 32 inclusive\n");
19 exit(1);
22 if (read(fd, raw, len) != len) {
23 printf("makerandom: cannot read random bytes\n");
24 exit(1);
27 for (i = 0; i < len; i++) {
28 sprintf(hex + (2 * i), "%02X", raw[i]);
31 return 1;
35 int main(int argc, char *argv[]) {
36 Hitag_State hstate;
37 char key[32];
38 char uid[32];
39 char nR[32];
40 char filename[256];
41 int i, j;
42 int numtests;
43 int urandomfd;
45 if (argc < 2) {
46 printf("%s number\n", argv[0]);
47 exit(1);
50 numtests = atoi(argv[1]);
51 if (numtests <= 0) {
52 printf("need positive number of tests\n");
53 exit(1);
56 urandomfd = open("/dev/urandom", O_RDONLY);
57 if (urandomfd <= 0) {
58 printf("cannot open /dev/urandom\n");
59 exit(1);
63 for (i = 0; i < numtests; i++) {
65 makerandom(key, 6, urandomfd);
66 makerandom(uid, 4, urandomfd);
67 makerandom(nR, 4, urandomfd);
68 sprintf(filename, "keystream.key-%s.uid-%s.nR-%s", key, uid, nR);
70 FILE *fp = fopen(filename, "w");
71 if (!fp) {
72 printf("cannot open file '%s' for writing\n", filename);
73 exit(1);
76 hstate.shiftreg = 0;
77 hstate.lfsr = 0;
79 hitag2_init(&hstate, rev64(hexreversetoulonglong(key)), rev32(hexreversetoulong(uid)), rev32(hexreversetoulong(nR)));
81 hitag2_nstep(&hstate, 64);
83 for (j = 0; j < 64; j++) {
84 fprintf(fp, "%08X\n", hitag2_nstep(&hstate, 32));
87 fclose(fp);
89 return 0;