From c3f2c015c4dd6ce730af378adb8a56959af8d1ff Mon Sep 17 00:00:00 2001 From: unkernet Date: Sun, 27 Aug 2017 19:39:34 +0300 Subject: [PATCH 1/7] Add ability to build on OS X --- .gitignore | 4 ++++ Makefile | 7 ++----- crypto1_bs_crack.c | 7 ++++--- libnfc_crypto1_crack.c | 8 ++++++-- solve_bs.c | 36 ++++++++++++++++++++++++++++-------- solve_piwi.c | 7 +++---- solve_piwi_bs.c | 4 ---- 7 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57b6ba9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +craptev1-v1.1.tar.xz +craptev1-v1.1/* +crapto1-v3.3.tar.xz +crapto1-v3.3/* diff --git a/Makefile b/Makefile index 2e74701..87f634b 100755 --- a/Makefile +++ b/Makefile @@ -15,11 +15,8 @@ CRAPTEV1 = craptev1-v1.1/craptev1.c -I craptev1-v1.1/ CRAPTO1 = crapto1-v3.3/crapto1.c crapto1-v3.3/crypto1.c -I crapto1-v3.3/ CRYPTO1_BS = crypto1_bs.c crypto1_bs_crack.c -solve.so: - $(CC) $(CFLAGS) craptev1-v1.1/solve.c -fPIC -shared -o solve.so - -solve_bs: solve.so - $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} ./solve.so -o $@ -lpthread +solve_bs: + $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} -o $@ -lpthread solve_piwi_bs: $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} -o $@ -lpthread diff --git a/crypto1_bs_crack.c b/crypto1_bs_crack.c index 9cf5ee2..53e6f75 100644 --- a/crypto1_bs_crack.c +++ b/crypto1_bs_crack.c @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include #include "crypto1_bs_crack.h" inline uint64_t crack_states_bitsliced(uint32_t **task){ @@ -134,9 +134,10 @@ inline uint64_t crack_states_bitsliced(uint32_t **task){ } #ifdef EXACT_COUNT - bucket_states_tested += bucket_size[block_idx]; + // Fix a "1000000% bug". Looks like here is a problem with OS X gcc + bucket_states_tested += bucket_size[block_idx] > MAX_BITSLICES ? MAX_BITSLICES : bucket_size[block_idx]; #ifdef ONLINE_COUNT - __atomic_fetch_add(&total_states_tested, bucket_size[block_idx], __ATOMIC_RELAXED); + __atomic_fetch_add(&total_states_tested, bucket_size[block_idx] > MAX_BITSLICES ? MAX_BITSLICES : bucket_size[block_idx], __ATOMIC_RELAXED); #endif #else #ifdef ONLINE_COUNT diff --git a/libnfc_crypto1_crack.c b/libnfc_crypto1_crack.c index e1857a9..5f1e283 100755 --- a/libnfc_crypto1_crack.c +++ b/libnfc_crypto1_crack.c @@ -288,7 +288,7 @@ POSSIBILITY OF SUCH DAMAGES. #include #include #include -#include +#include #include #include @@ -676,7 +676,11 @@ int main (int argc, const char * argv[]) { return 1; } - thread_count = get_nprocs_conf(); +#ifndef __WIN32 + thread_count = sysconf(_SC_NPROCESSORS_CONF); +#else + thread_count = 1; +#endif // append some zeroes to the end of the space to make sure threads don't go off into the wild size_t j = 0; for(j = 0; space[j]; j+=5){ diff --git a/solve_bs.c b/solve_bs.c index e244c7f..ccfcd00 100644 --- a/solve_bs.c +++ b/solve_bs.c @@ -1,12 +1,8 @@ #include -#include #include #include #include #include -#ifndef __WIN32 -#include -#endif #include "craptev1.h" #include "crypto1_bs.h" #include "crypto1_bs_crack.h" @@ -17,12 +13,34 @@ #define llu PRIu64 #define lu PRIu32 -// linked from .so / .c files by bla -extern uint64_t *readnonces(char* fname); - uint32_t **space; size_t thread_count = 1; +uint64_t *readnonces(char* fname) { + int i, j, r; + FILE *f = fopen(fname, "r"); + uint64_t *nonces = malloc(sizeof (uint64_t) << 24); + uint32_t byte; + char parities; + + for(i = 0; !feof(f); ++i) { + for(j = nonces[i] = 0; j < 4; ++j) { + r = fscanf(f, "%02x%c ", &byte, &parities); + if(r != 2) { + fprintf(stderr, "Input parse error pos:%ld\n", ftell(f)); + fflush(stderr); + abort(); + } + parities = (parities == '!') ^ parity(byte); + nonces[i] |= byte << 8 * j; + nonces[i] |= ((uint64_t)parities) << (32 + j * 8); + } + } + nonces[i] = -1; + fclose(f); + return nonces; +} + void* crack_states_thread(void* x){ const size_t thread_id = (size_t)x; int j; @@ -51,7 +69,9 @@ int main(int argc, char* argv[]){ total_states = craptev1_sizeof_space(space); #ifndef __WIN32 - thread_count = get_nprocs_conf(); + thread_count = sysconf(_SC_NPROCESSORS_CONF); +#else + thread_count = 1; #endif // append some zeroes to the end of the space to make sure threads don't go off into the wild size_t j = 0; diff --git a/solve_piwi.c b/solve_piwi.c index 3945a93..acc1bc5 100644 --- a/solve_piwi.c +++ b/solve_piwi.c @@ -3,9 +3,6 @@ #include #include #include -#ifndef __WIN32 -#include -#endif #include "craptev1.h" #include #define __STDC_FORMAT_MACROS @@ -74,7 +71,9 @@ int main(int argc, char* argv[]){ total_states = craptev1_sizeof_space(space); #ifndef __WIN32 - thread_count = get_nprocs_conf(); + thread_count = sysconf(_SC_NPROCESSORS_CONF); +#else + thread_count = 1; #endif // append some zeroes to the end of the space to make sure threads don't go off into the wild size_t j = 0; diff --git a/solve_piwi_bs.c b/solve_piwi_bs.c index 6c762a8..f20ef27 100644 --- a/solve_piwi_bs.c +++ b/solve_piwi_bs.c @@ -1,12 +1,8 @@ #include -#include #include #include #include #include -#ifndef __WIN32 -#include -#endif #include "craptev1.h" #include "crypto1_bs.h" #include "crypto1_bs_crack.h" From ee432c793272260cd57ef8218745b36e7b7ae617 Mon Sep 17 00:00:00 2001 From: unkernet Date: Sun, 27 Aug 2017 20:25:54 +0300 Subject: [PATCH 2/7] Add progress display to solve_bs and solve_piwi_bs --- solve_bs.c | 19 +++++++++++++++++-- solve_piwi_bs.c | 19 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/solve_bs.c b/solve_bs.c index ccfcd00..04b9fe1 100644 --- a/solve_bs.c +++ b/solve_bs.c @@ -12,6 +12,7 @@ #define lli PRIi64 #define llu PRIu64 #define lu PRIu32 +#define VT100_cleareol "\r\33[2K" uint32_t **space; size_t thread_count = 1; @@ -51,20 +52,27 @@ void* crack_states_thread(void* x){ break; } else if(keys_found){ break; - } else { - printf("Cracking... %6.02f%%\n", (total_states_tested/(total_states/100.0))); } } return NULL; } +void notify_status_offline(int sig){ + printf(VT100_cleareol "Cracking... %6.02f%%", (100.0*total_states_tested/(total_states))); + alarm(1); + fflush(stdout); + signal(SIGALRM, notify_status_offline); +} + int main(int argc, char* argv[]){ if(argc != 3){ printf("Usage: %s \n", argv[0]); return -1; } + printf("Reading nonces...\n"); uint64_t *nonces = readnonces(argv[1]); uint32_t uid = strtoul(argv[2], NULL, 16); + printf("Deriving search space...\n"); space = craptev1_get_space(nonces, 95, uid); total_states = craptev1_sizeof_space(space); @@ -108,12 +116,19 @@ int main(int argc, char* argv[]){ keys_found = 0; printf("Starting %zu threads to test %"llu" states\n", thread_count, total_states); + + signal(SIGALRM, notify_status_offline); + alarm(1); + for(i = 0; i < thread_count; i++){ pthread_create(&threads[i], NULL, crack_states_thread, (void*) i); } for(i = 0; i < thread_count; i++){ pthread_join(threads[i], 0); } + + alarm(0); + printf("Tested %"llu" states\n", total_states_tested); craptev1_destroy_space(space); diff --git a/solve_piwi_bs.c b/solve_piwi_bs.c index f20ef27..9f16ce4 100644 --- a/solve_piwi_bs.c +++ b/solve_piwi_bs.c @@ -12,6 +12,7 @@ #define lli PRIi64 #define llu PRIu64 #define lu PRIu32 +#define VT100_cleareol "\r\33[2K" uint64_t split(uint8_t p){ return (((p & 0x8) >>3 )| ((p & 0x4) >> 2) << 8 | ((p & 0x2) >> 1) << 16 | (p & 0x1) << 24 ); @@ -54,19 +55,26 @@ void* crack_states_thread(void* x){ break; } else if(keys_found){ break; - } else { - printf("Cracking... %6.02f%%\n", (100.0*total_states_tested/(total_states))); } } return NULL; } +void notify_status_offline(int sig){ + printf(VT100_cleareol "Cracking... %6.02f%%", (100.0*total_states_tested/(total_states))); + alarm(1); + fflush(stdout); + signal(SIGALRM, notify_status_offline); +} + int main(int argc, char* argv[]){ if(argc != 2){ printf("Usage: %s \n", argv[0]); return -1; } + printf("Reading nonces...\n"); uint64_t *nonces = readnonces(argv[1]); + printf("Deriving search space...\n"); space = craptev1_get_space(nonces, 95, uid); total_states = craptev1_sizeof_space(space); @@ -111,12 +119,19 @@ int main(int argc, char* argv[]){ keys_found = 0; printf("Starting %u threads to test %"llu" states\n", thread_count, total_states); + + signal(SIGALRM, notify_status_offline); + alarm(1); + for(i = 0; i < thread_count; i++){ pthread_create(&threads[i], NULL, crack_states_thread, (void*) i); } for(i = 0; i < thread_count; i++){ pthread_join(threads[i], 0); } + + alarm(0); + printf("Tested %"llu" states\n", total_states_tested); craptev1_destroy_space(space); From a343a950ca61fcddf452f7552104a9a000d091aa Mon Sep 17 00:00:00 2001 From: unkernet Date: Sun, 27 Aug 2017 20:46:54 +0300 Subject: [PATCH 3/7] Some fixes for Linux --- crypto1_bs_crack.c | 3 +++ solve_bs.c | 1 + solve_piwi_bs.c | 1 + 3 files changed, 5 insertions(+) diff --git a/crypto1_bs_crack.c b/crypto1_bs_crack.c index 53e6f75..e4c9ee7 100644 --- a/crypto1_bs_crack.c +++ b/crypto1_bs_crack.c @@ -23,6 +23,9 @@ THE SOFTWARE. */ #include +#ifndef __APPLE__ +#include +#endif #include "crypto1_bs_crack.h" inline uint64_t crack_states_bitsliced(uint32_t **task){ diff --git a/solve_bs.c b/solve_bs.c index 04b9fe1..7ea0a45 100644 --- a/solve_bs.c +++ b/solve_bs.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "craptev1.h" #include "crypto1_bs.h" diff --git a/solve_piwi_bs.c b/solve_piwi_bs.c index 9f16ce4..ac56717 100644 --- a/solve_piwi_bs.c +++ b/solve_piwi_bs.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include "craptev1.h" #include "crypto1_bs.h" From f028c5e4e3d1d1aa47c8cb48973348839d356ba1 Mon Sep 17 00:00:00 2001 From: unkernet Date: Sun, 27 Aug 2017 23:28:16 +0300 Subject: [PATCH 4/7] Rewrite readnonces --- solve_bs.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/solve_bs.c b/solve_bs.c index 7ea0a45..741202e 100644 --- a/solve_bs.c +++ b/solve_bs.c @@ -19,24 +19,28 @@ uint32_t **space; size_t thread_count = 1; uint64_t *readnonces(char* fname) { - int i, j, r; + int i, j; FILE *f = fopen(fname, "r"); + if (f == NULL) { + fprintf(stderr, "Cannot open file.\n"); + exit(EXIT_FAILURE); + } uint64_t *nonces = malloc(sizeof (uint64_t) << 24); - uint32_t byte; - char parities; + uint32_t nt; + char par; - for(i = 0; !feof(f); ++i) { - for(j = nonces[i] = 0; j < 4; ++j) { - r = fscanf(f, "%02x%c ", &byte, &parities); - if(r != 2) { - fprintf(stderr, "Input parse error pos:%ld\n", ftell(f)); + i = 0; + while(!feof(f)){ + nonces[i] = 0; + for(j = 0; j < 32; j += 8) { + if(2 != fscanf(f, "%02x%c ", &nt, &par)) { + fprintf(stderr, "Input format error at line:%d\n", i); fflush(stderr); - abort(); + exit(EXIT_FAILURE); } - parities = (parities == '!') ^ parity(byte); - nonces[i] |= byte << 8 * j; - nonces[i] |= ((uint64_t)parities) << (32 + j * 8); + nonces[i] |= nt << j | (uint64_t)((par == '!') ^ parity(nt)) << (32 + j); } + i++; } nonces[i] = -1; fclose(f); From e6b23bd0e26d8e0b82a0cfac4e7167f93c1d3438 Mon Sep 17 00:00:00 2001 From: unkernet Date: Sun, 27 Aug 2017 23:32:34 +0300 Subject: [PATCH 5/7] Some small fixes --- libnfc_crypto1_crack.c | 1 + solve_bs.c | 9 ++++++--- solve_piwi_bs.c | 7 ++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/libnfc_crypto1_crack.c b/libnfc_crypto1_crack.c index 5f1e283..1b41542 100755 --- a/libnfc_crypto1_crack.c +++ b/libnfc_crypto1_crack.c @@ -517,6 +517,7 @@ void * update_predictions_thread(void* p){ if(space){ total_states = craptev1_sizeof_space(space); } + sleep(1); // We don't need to check this more often than once per second } return NULL; } diff --git a/solve_bs.c b/solve_bs.c index 741202e..bf14d9a 100644 --- a/solve_bs.c +++ b/solve_bs.c @@ -8,6 +8,7 @@ #include "crypto1_bs.h" #include "crypto1_bs_crack.h" #include +#include #define __STDC_FORMAT_MACROS #define llx PRIx64 #define lli PRIi64 @@ -16,7 +17,7 @@ #define VT100_cleareol "\r\33[2K" uint32_t **space; -size_t thread_count = 1; +uint8_t thread_count = 1; uint64_t *readnonces(char* fname) { int i, j; @@ -120,7 +121,7 @@ int main(int argc, char* argv[]){ total_states_tested = 0; keys_found = 0; - printf("Starting %zu threads to test %"llu" states\n", thread_count, total_states); + printf("Starting %u threads to test %"llu" (~2^%0.2f) states\n", thread_count, total_states, log(total_states) / log(2)); signal(SIGALRM, notify_status_offline); alarm(1); @@ -134,7 +135,9 @@ int main(int argc, char* argv[]){ alarm(0); - printf("Tested %"llu" states\n", total_states_tested); + printf("\nTested %"llu" states\n", total_states_tested); + + if(!keys_found) fprintf(stderr, "No solution found :(\n"); craptev1_destroy_space(space); return 0; diff --git a/solve_piwi_bs.c b/solve_piwi_bs.c index ac56717..1d149e7 100644 --- a/solve_piwi_bs.c +++ b/solve_piwi_bs.c @@ -8,6 +8,7 @@ #include "crypto1_bs.h" #include "crypto1_bs_crack.h" #include +#include #define __STDC_FORMAT_MACROS #define llx PRIx64 #define lli PRIi64 @@ -23,6 +24,10 @@ uint32_t uid; uint64_t *readnonces(char* fname){ int i; FILE *f = fopen(fname, "rb"); + if (f == NULL) { + fprintf(stderr, "Cannot open file.\n"); + exit(EXIT_FAILURE); + } uint64_t *nonces = malloc(sizeof (uint64_t) << 24); if(fread(&uid, 1, 4, f)){ uid = rev32(uid); @@ -119,7 +124,7 @@ int main(int argc, char* argv[]){ total_states_tested = 0; keys_found = 0; - printf("Starting %u threads to test %"llu" states\n", thread_count, total_states); + printf("Starting %u threads to test %"llu" (~2^%0.2f) states\n", thread_count, total_states, log(total_states) / log(2)); signal(SIGALRM, notify_status_offline); alarm(1); From a9410a1b30a18ea7257e071de39fa674f55d960b Mon Sep 17 00:00:00 2001 From: unkernet Date: Mon, 28 Aug 2017 00:25:25 +0300 Subject: [PATCH 6/7] Fix Makefile --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 87f634b..758e411 100755 --- a/Makefile +++ b/Makefile @@ -16,10 +16,10 @@ CRAPTO1 = crapto1-v3.3/crapto1.c crapto1-v3.3/crypto1.c -I crapto1-v3.3/ CRYPTO1_BS = crypto1_bs.c crypto1_bs_crack.c solve_bs: - $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} -o $@ -lpthread + $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} -o $@ -lpthread -lm solve_piwi_bs: - $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} -o $@ -lpthread + $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} -o $@ -lpthread -lm solve_piwi: $(CC) $(CFLAGS) $@.c $(CRYPTO1_BS) $(CRAPTO1) ${CRAPTEV1} -o $@ -lpthread From db69999219a50c582f9bd12926faa38093c06a33 Mon Sep 17 00:00:00 2001 From: unkernet Date: Mon, 28 Aug 2017 02:14:36 +0300 Subject: [PATCH 7/7] Fix wrong bucket_size bug --- crypto1_bs_crack.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crypto1_bs_crack.c b/crypto1_bs_crack.c index e4c9ee7..90aba63 100644 --- a/crypto1_bs_crack.c +++ b/crypto1_bs_crack.c @@ -136,11 +136,13 @@ inline uint64_t crack_states_bitsliced(uint32_t **task){ } } -#ifdef EXACT_COUNT // Fix a "1000000% bug". Looks like here is a problem with OS X gcc - bucket_states_tested += bucket_size[block_idx] > MAX_BITSLICES ? MAX_BITSLICES : bucket_size[block_idx]; + size_t current_bucket_size = bucket_size[block_idx] > MAX_BITSLICES ? MAX_BITSLICES : bucket_size[block_idx]; + +#ifdef EXACT_COUNT + bucket_states_tested += current_bucket_size; #ifdef ONLINE_COUNT - __atomic_fetch_add(&total_states_tested, bucket_size[block_idx] > MAX_BITSLICES ? MAX_BITSLICES : bucket_size[block_idx], __ATOMIC_RELAXED); + __atomic_fetch_add(&total_states_tested, current_bucket_size, __ATOMIC_RELAXED); #endif #else #ifdef ONLINE_COUNT