This commit is contained in:
iceman1001 2024-03-25 23:33:44 +01:00
commit 140cc1d957

View file

@ -35,6 +35,13 @@
#include "hitag2.h" #include "hitag2.h"
#include "dolphin_macro.h" #include "dolphin_macro.h"
#define AEND "\x1b[0m"
#define _RED_(s) "\x1b[31m" s AEND
#define _GREEN_(s) "\x1b[32m" s AEND
#define _YELLOW_(s) "\x1b[33m" s AEND
#define _CYAN_(s) "\x1b[36m" s AEND
#if defined(__MINGW64__) #if defined(__MINGW64__)
#define timersub(a, b, result) \ #define timersub(a, b, result) \
do { \ do { \
@ -282,9 +289,13 @@ int main(int argc, char **argv) {
free(platforms_selected); free(platforms_selected);
free(devices_selected); free(devices_selected);
if (device_types_selected == 0) device_types_selected = CL_DEVICE_TYPE_GPU; if (device_types_selected == 0) {
else if (device_types_selected == 1) device_types_selected = CL_DEVICE_TYPE_CPU; device_types_selected = CL_DEVICE_TYPE_GPU;
else device_types_selected = CL_DEVICE_TYPE_ALL; } else if (device_types_selected == 1) {
device_types_selected = CL_DEVICE_TYPE_CPU;
} else {
device_types_selected = CL_DEVICE_TYPE_ALL;
}
if (show) { if (show) {
plat_sel[0] = 0xff; plat_sel[0] = 0xff;
@ -293,17 +304,23 @@ int main(int argc, char **argv) {
} }
if (verbose) { if (verbose) {
if (plat_sel[0] == 0xff) printf("Platforms selected : ALL\n"); if (plat_sel[0] == 0xff) {
else { printf("Platforms selected : ALL\n");
} else {
printf("Platforms selected : %u", plat_sel[0]); printf("Platforms selected : %u", plat_sel[0]);
for (unsigned int i = 1; i < plat_cnt; i++) printf(", %u", plat_sel[i]); for (unsigned int i = 1; i < plat_cnt; i++) {
printf(", %u", plat_sel[i]);
}
printf("\n"); printf("\n");
} }
if (dev_sel[0] == 0xff) printf("Devices selected : ALL\n"); if (dev_sel[0] == 0xff) {
else { printf("Devices selected : ALL\n");
} else {
printf("Devices selected : %u", dev_sel[0]); printf("Devices selected : %u", dev_sel[0]);
for (unsigned int i = 1; i < dev_cnt; i++) printf(", %u", dev_sel[i]); for (unsigned int i = 1; i < dev_cnt; i++) {
printf(", %u", dev_sel[i]);
}
printf("\n"); printf("\n");
} }
@ -312,7 +329,8 @@ int main(int argc, char **argv) {
printf("Profile selected : %u\n", profile_selected); printf("Profile selected : %u\n", profile_selected);
} }
if (!show) { if (show == false) {
if ((argc - optind) < 5) { if ((argc - optind) < 5) {
#if DEBUGME > 0 #if DEBUGME > 0
printf("Error: invalid extra arguments\n"); printf("Error: invalid extra arguments\n");
@ -324,41 +342,57 @@ int main(int argc, char **argv) {
switch (e) { switch (e) {
case 0: // UID case 0: // UID
if (!strncmp(argv[optind], "0x", 2) || !strncmp(argv[optind], "0X", 2)) { if (!strncmp(argv[optind], "0x", 2) || !strncmp(argv[optind], "0X", 2)) {
if (strlen(argv[optind]) != 2 + 8) { printf("Error: invalid UID length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 2 + 8) {
printf("Error: invalid UID length\n"); usage(argv[0]);
}
uid = (uint32_t) rev32(hexreversetoulong(argv[optind] + 2)); uid = (uint32_t) rev32(hexreversetoulong(argv[optind] + 2));
} else { } else {
if (strlen(argv[optind]) != 8) { printf("Error: invalid UID length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 8) {
printf("Error: invalid UID length\n"); usage(argv[0]);
}
uid = (uint32_t) rev32(hexreversetoulong(argv[optind])); uid = (uint32_t) rev32(hexreversetoulong(argv[optind]));
} }
break; break;
case 1: // nR1 case 1: // nR1
if (!strncmp(argv[optind], "0x", 2) || !strncmp(argv[optind], "0X", 2)) { if (!strncmp(argv[optind], "0x", 2) || !strncmp(argv[optind], "0X", 2)) {
if (strlen(argv[optind]) != 2 + 8) { printf("Error: invalid nR1 length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 2 + 8) {
printf("Error: invalid nR1 length\n"); usage(argv[0]);
}
nR1 = (uint32_t) rev32(hexreversetoulong(argv[optind] + 2)); nR1 = (uint32_t) rev32(hexreversetoulong(argv[optind] + 2));
} else { } else {
if (strlen(argv[optind]) != 8) { printf("Error: invalid nR1 length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 8) {
printf("Error: invalid nR1 length\n"); usage(argv[0]);
}
nR1 = (uint32_t) rev32(hexreversetoulong(argv[optind])); nR1 = (uint32_t) rev32(hexreversetoulong(argv[optind]));
} }
break; break;
case 2: // aR1 case 2: // aR1
if (strlen(argv[optind]) != 8) { printf("Error: invalid aR1 length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 8) {
printf("Error: invalid aR1 length\n"); usage(argv[0]);
}
aR1 = (uint32_t) strtoul(argv[optind], NULL, 16); aR1 = (uint32_t) strtoul(argv[optind], NULL, 16);
break; break;
case 3: // nR2 case 3: // nR2
if (!strncmp(argv[optind], "0x", 2) || !strncmp(argv[optind], "0X", 2)) { if (!strncmp(argv[optind], "0x", 2) || !strncmp(argv[optind], "0X", 2)) {
if (strlen(argv[optind]) != 2 + 8) { printf("Error: invalid nR2 length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 2 + 8) {
printf("Error: invalid nR2 length\n"); usage(argv[0]);
}
nR2 = (uint32_t) rev32(hexreversetoulong(argv[optind] + 2)); nR2 = (uint32_t) rev32(hexreversetoulong(argv[optind] + 2));
} else { } else {
if (strlen(argv[optind]) != 8) { printf("Error: invalid nR2 length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 8) {
printf("Error: invalid nR2 length\n"); usage(argv[0]);
}
nR2 = (uint32_t) rev32(hexreversetoulong(argv[optind])); nR2 = (uint32_t) rev32(hexreversetoulong(argv[optind]));
} }
break; break;
case 4: // aR2 case 4: // aR2
if (strlen(argv[optind]) != 8) { printf("Error: invalid aR2 length\n"); usage(argv[0]); } if (strlen(argv[optind]) != 8) {
printf("Error: invalid aR2 length\n"); usage(argv[0]);
}
aR2 = (uint32_t) strtoul(argv[optind], NULL, 16); aR2 = (uint32_t) strtoul(argv[optind], NULL, 16);
break; break;
@ -379,8 +413,10 @@ int main(int argc, char **argv) {
uint32_t checks[4] = { uid, aR2, nR1, nR2 }; uint32_t checks[4] = { uid, aR2, nR1, nR2 };
if (!show) { if (show == false) {
if (verbose) printf("uid: %u, aR2: %u, nR1: %u, nR2: %u\n", checks[0], checks[1], checks[2], checks[3]); if (verbose) {
printf("uid: %u, aR2: %u, nR1: %u, nR2: %u\n", checks[0], checks[1], checks[2], checks[3]);
}
uint32_t target = ~aR1; uint32_t target = ~aR1;
// bitslice inverse target bits // bitslice inverse target bits
@ -419,7 +455,7 @@ int main(int argc, char **argv) {
exit(3); exit(3);
} }
if (!show) { if (show == false) {
// load OpenCL kernel source // load OpenCL kernel source
struct stat st; struct stat st;
const char *opencl_kernel = "ht2crack5opencl_kernel.cl"; const char *opencl_kernel = "ht2crack5opencl_kernel.cl";
@ -463,17 +499,23 @@ int main(int argc, char **argv) {
size_t selected_devices_cnt = 0; size_t selected_devices_cnt = 0;
compute_platform_ctx_t *cd_ctx = NULL; compute_platform_ctx_t *cd_ctx = NULL;
if (show) verbose = true; if (show) {
verbose = true;
}
// now discover and set up compute device(s) // now discover and set up compute device(s)
if ((err = discoverDevices(profile_selected, device_types_selected, &ocl_platform_cnt, &selected_platforms_cnt, &selected_devices_cnt, &cd_ctx, plat_sel, plat_cnt, dev_sel, dev_cnt, verbose, show)) != 0) { if ((err = discoverDevices(profile_selected, device_types_selected, &ocl_platform_cnt, &selected_platforms_cnt, &selected_devices_cnt, &cd_ctx, plat_sel, plat_cnt, dev_sel, dev_cnt, verbose, show)) != 0) {
printf("Error: discoverDevices() failed\n"); printf("Error: discoverDevices() failed\n");
if (err < -5) free(cd_ctx); if (err < -5) {
free(cd_ctx);
}
MEMORY_FREE_ALL MEMORY_FREE_ALL
exit(2); exit(2);
} }
if (verbose) printf("\n"); if (verbose) {
printf("\n");
}
// new selection engine, need to support multi-gpu system (with the same platform) // new selection engine, need to support multi-gpu system (with the same platform)
if (show) { if (show) {
@ -504,10 +546,14 @@ int main(int argc, char **argv) {
printf("Selected %zu OpenCL Device(s)\n\n", selected_devices_cnt); printf("Selected %zu OpenCL Device(s)\n\n", selected_devices_cnt);
for (w = 0; w < ocl_platform_cnt; w++) { for (w = 0; w < ocl_platform_cnt; w++) {
if (!cd_ctx[w].selected) continue; if (!cd_ctx[w].selected) {
continue;
}
for (q = 0; q < cd_ctx[w].device_cnt; q++) { for (q = 0; q < cd_ctx[w].device_cnt; q++) {
if (!cd_ctx[w].device[q].selected) continue; if (!cd_ctx[w].device[q].selected) {
continue;
}
if (cd_ctx[w].is_apple && !strncmp(cd_ctx[w].device[q].vendor, "Intel", 5)) { if (cd_ctx[w].is_apple && !strncmp(cd_ctx[w].device[q].vendor, "Intel", 5)) {
// disable hitag2 with apple platform and not apple device vendor (< Apple M1) // disable hitag2 with apple platform and not apple device vendor (< Apple M1)
@ -749,7 +795,9 @@ int main(int argc, char **argv) {
blen += 21; blen += 21;
} }
if (verbose) printf("[%zu] Building OpenCL program with options (len %zu): %s\n", z, blen, build_options); if (verbose) {
printf("[%zu] Building OpenCL program with options (len %zu): %s\n", z, blen, build_options);
}
err = clBuildProgram(ctx.programs[z], 1, &ctx.device_ids[z], build_options, NULL, NULL); err = clBuildProgram(ctx.programs[z], 1, &ctx.device_ids[z], build_options, NULL, NULL);
@ -868,7 +916,9 @@ int main(int argc, char **argv) {
// dow't allow gws < lws // dow't allow gws < lws
if (ctx.global_ws[z] < ctx.local_ws[z]) ctx.local_ws[z] = ctx.global_ws[z]; if (ctx.global_ws[z] < ctx.local_ws[z]) ctx.local_ws[z] = ctx.global_ws[z];
if (opencl_profiling) printf("[%zu] global_ws %zu, local_ws %zu\n", g, ctx.global_ws[z], ctx.local_ws[z]); if (opencl_profiling) {
printf("[%zu] global_ws %zu, local_ws %zu\n", g, ctx.global_ws[z], ctx.local_ws[z]);
}
if (!ctx.force_hitag2_opencl) { if (!ctx.force_hitag2_opencl) {
if (!(matches[z] = (uint64_t *) calloc((uint32_t)(ctx.global_ws[z] * WGS_MATCHES_FACTOR), sizeof(uint64_t)))) { if (!(matches[z] = (uint64_t *) calloc((uint32_t)(ctx.global_ws[z] * WGS_MATCHES_FACTOR), sizeof(uint64_t)))) {
@ -1128,8 +1178,15 @@ int main(int argc, char **argv) {
printf("Attack 5 - opencl - start (Max Slices %u, %s order", max_step, wu_queue_strdesc(ctx.queue_ctx.queue_type)); printf("Attack 5 - opencl - start (Max Slices %u, %s order", max_step, wu_queue_strdesc(ctx.queue_ctx.queue_type));
if (!verbose) printf(")\n\n"); if (!verbose) {
else printf(", Profile %u, Async Threads %s, HiTag2 key verify on device %s)\n\n", profile, (ctx.thread_sched_type == THREAD_TYPE_ASYNC) ? "yes" : "no", (force_hitag2_opencl) ? "yes" : "no"); printf(")\n\n");
} else {
printf(", Profile %u, Async Threads %s, HiTag2 key verify on device %s)\n\n"
, profile
, (ctx.thread_sched_type == THREAD_TYPE_ASYNC) ? "yes" : "no"
, (force_hitag2_opencl) ? "yes" : "no"
);
}
if (gettimeofday(&cpu_t_start, NULL) == -1) { if (gettimeofday(&cpu_t_start, NULL) == -1) {
printf("Error: gettimeofday(start) failed (%d): %s\n", errno, strerror(errno)); printf("Error: gettimeofday(start) failed (%d): %s\n", errno, strerror(errno));
@ -1147,17 +1204,27 @@ int main(int argc, char **argv) {
// if found, show the key here // if found, show the key here
for (size_t y = 0; y < thread_count; y++) { for (size_t y = 0; y < thread_count; y++) {
if (t_arg[y].r) { if (t_arg[y].r) {
if (verbose) printf("\n");
if (thread_count > 1) printf("[%zu] ", y); if (verbose) {
printf("\n");
}
if (thread_count > 1) {
printf("[%zu] ", y);
}
printf("\nKey found @ slice %zu/%zu [ \x1b[32m"
, t_arg[y].slice
, t_arg[y].max_slices
);
printf("\nKey found @ slice %zu/%zu: [ ", t_arg[y].slice, t_arg[y].max_slices);
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
printf("%02X", (uint8_t)(t_arg[y].key & 0xff)); printf("%02X", (uint8_t)(t_arg[y].key & 0xff));
t_arg[y].key = t_arg[y].key >> 8; t_arg[y].key = t_arg[y].key >> 8;
} }
printf(" ]\n"); printf(AEND " ]\n");
fflush(stdout); fflush(stdout);
break; break;
} }
@ -1172,14 +1239,21 @@ int main(int argc, char **argv) {
} }
} }
if (!found) { if (found == false) {
printf("\nError. %s\n", (error) ? "something went wrong :(" : "Key not found :|"); if (error) {
printf("\nSomething went wrong ( " _RED_("fail") " )\n");
} else {
printf("\nExhausted keyspace ( " _RED_("fail") " )\n");
}
} }
printf("\nAttack 5 - opencl - end"); printf("\nAttack 5 - opencl - end");
if (show_overall_time) printf(" in %ld.%2ld second(s).\n\n", (long int)cpu_t_result.tv_sec, (long int)cpu_t_result.tv_usec); if (show_overall_time) {
else printf("\n"); printf(" in " _YELLOW_("%ld.%2ld") " second(s).\n\n", (long int)cpu_t_result.tv_sec, (long int)cpu_t_result.tv_usec);
} else {
printf("\n");
}
fflush(stdout); fflush(stdout);
@ -1188,14 +1262,16 @@ int main(int argc, char **argv) {
fflush(stdout); fflush(stdout);
#endif #endif
if (!error && th_ctx.type != THREAD_TYPE_SEQ) thread_stop(&th_ctx); if (error == false && th_ctx.type != THREAD_TYPE_SEQ) {
thread_stop(&th_ctx);
}
#if DEBUGME > 1 #if DEBUGME > 1
printf("destroy threads\n"); printf("destroy threads\n");
fflush(stdout); fflush(stdout);
#endif #endif
if (!error) { if (error == false) {
if ((ret = thread_destroy(&th_ctx)) != 0) { if ((ret = thread_destroy(&th_ctx)) != 0) {
#if DEBUGME > 0 #if DEBUGME > 0
printf("Warning: thread_destroy() failed (%d): %s\n", ret, thread_strerror(ret)); printf("Warning: thread_destroy() failed (%d): %s\n", ret, thread_strerror(ret));