From 7553d25995bf871fcd8b1b245c59f7d46979f0bb Mon Sep 17 00:00:00 2001 From: Sayantan Datta Date: Sun, 11 Jan 2015 12:20:04 +0530 Subject: [PATCH] work-in-progress:mask mode internal candidate gen --- src/mask.c | 18 ++++++-- src/mask_device.c | 40 ------------------ src/mask_ext.c | 69 +++++++++++++++++++++++++++++++ src/{mask_device.h => mask_ext.h} | 13 ++++-- 4 files changed, 94 insertions(+), 46 deletions(-) delete mode 100644 src/mask_device.c create mode 100644 src/mask_ext.c rename src/{mask_device.h => mask_ext.h} (51%) diff --git a/src/mask.c b/src/mask.c index 1c72ab0c7d..3648e3b490 100644 --- a/src/mask.c +++ b/src/mask.c @@ -31,7 +31,7 @@ #include "unicode.h" #include "encoding_data.h" #include "memdbg.h" -#include "mask_device.h" +#include "mask_ext.h" static parsed_ctx parsed_mask; static cpu_mask_context cpu_mask_ctx, rec_ctx; @@ -1022,6 +1022,11 @@ static void save_restore(cpu_mask_context *cpu_mask_ctx, int range_idx, int ch) static void truncate_mask(cpu_mask_context *cpu_mask_ctx, int range_idx) { int i; + if (range_idx < mask_max_skip_loc && mask_max_skip_loc != -1) { + fprintf(stderr, "Format internal ranges cannot be truncated!\n"); + fprintf(stderr, "Use a bigger key length or non-gpu format.\n"); + error(); + } cpu_mask_ctx->ranges[range_idx].next = MAX_NUM_MASK_PLHDR; @@ -1690,13 +1695,18 @@ void mask_init(struct db_main *db, char *unprocessed_mask) #endif init_cpu_mask(mask, &parsed_mask, &cpu_mask_ctx, db); - calc_combination(5); + mask_calc_combination(&cpu_mask_ctx); + + fprintf(stderr, "MASK_FMT_INT_PLHDRs:"); + for (i = 0; i < MASK_FMT_INT_PLHDR && mask_skip_ranges; i++) + fprintf(stderr, "%d ", mask_skip_ranges[i]); + fprintf(stderr, "\n"); /* * Warning: NULL to be replaced by an array containing information * regarding GPU portion of mask. */ - skip_position(&cpu_mask_ctx, NULL); + skip_position(&cpu_mask_ctx, mask_skip_ranges); /* If running hybrid (stacked), we let the parent mode distribute */ if (options.node_count && !(options.flags & FLG_MASK_STACKED)) @@ -1725,6 +1735,8 @@ void mask_done() { MEM_FREE(template_key); MEM_FREE(template_key_offsets); + if (!mask_skip_ranges) + MEM_FREE(mask_skip_ranges); if (!(options.flags & FLG_MASK_STACKED)) { if (parsed_mask.parse_ok && diff --git a/src/mask_device.c b/src/mask_device.c deleted file mode 100644 index d3453a07ad..0000000000 --- a/src/mask_device.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of John the Ripper password cracker, - * Copyright (c) 2014 by Sayantan Datta - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted. - * - * There's ABSOLUTELY NO WARRANTY, express or implied. - */ -#include "mask.h" -#include "mask_device.h" - -static void combination_util(int data[], int start, int end, int index, int r); - -void calc_combination(int n) { - int data[n], i; - - /* Fix the maximum number of ranges that can be calculated on GPU to 3 */ - for (i = 1; i <= 3; i++) - combination_util(data, 0, n - 1, 0, i); - -} - -static void combination_util(int data[], int start, int end, int index, - int r) { - int i; - - if (index == r) { - for (i = 0; i < r; i++) - fprintf(stderr, "%d", data[i]); - fprintf(stderr, "\n"); - - return; - } - - for (i = start; i <= end && end - i + 1 >= r - index; i++) { - data[index] = i; - combination_util(data, i + 1, end, index + 1, r); - } -} diff --git a/src/mask_ext.c b/src/mask_ext.c new file mode 100644 index 0000000000..0b59217cc3 --- /dev/null +++ b/src/mask_ext.c @@ -0,0 +1,69 @@ +/* + * This file is part of John the Ripper password cracker, + * Copyright (c) 2014 by Sayantan Datta + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + */ + +#include "mask_ext.h" +#include "memory.h" + +int *mask_skip_ranges = NULL; +int mask_max_skip_loc = -1; +int mask_int_cand_target = 0; + +static void combination_util(int *, int, int, int, int, + cpu_mask_context *, int *); + +void mask_calc_combination(cpu_mask_context *ptr) { + int *data, i, n; + int delta_to_target = 0x7fffffff; + if (!mask_int_cand_target) return; + + n = ptr->count; + data = (int*) malloc(n * sizeof(int)); + mask_skip_ranges = (int*) malloc (MASK_FMT_INT_PLHDR * sizeof(int)); + + for (i = 0; i < MASK_FMT_INT_PLHDR; i++) + mask_skip_ranges[i] = -1; + + /* Fix the maximum number of ranges that can be calculated on GPU to 3 */ + for (i = 1; i <= MASK_FMT_INT_PLHDR; i++) + combination_util(data, 0, n - 1, 0, i, ptr, + &delta_to_target); + + MEM_FREE(data); +} + +static void combination_util(int *data, int start, int end, int index, + int r, cpu_mask_context *ptr, int *delta) { + int i; + + if (index == r) { + int tmp = 1; + for (i = 0; i < r; i++) + tmp *= ptr->ranges[data[i]].count; + + tmp -= mask_int_cand_target; + tmp = tmp < 0 ? -tmp : tmp; + + if (tmp < *delta) { + for (i = 0; i < r; i++) + mask_skip_ranges[i] = data[i]; + + mask_max_skip_loc = mask_skip_ranges[i-1]; + *delta = tmp; + } + + return; + } + + for (i = start; i <= end && end - i + 1 >= r - index; i++) { + data[index] = i; + combination_util(data, i + 1, end, index + 1, + r, ptr, delta); + } +} diff --git a/src/mask_device.h b/src/mask_ext.h similarity index 51% rename from src/mask_device.h rename to src/mask_ext.h index 60624b12c9..17dd8119a7 100644 --- a/src/mask_device.h +++ b/src/mask_ext.h @@ -8,9 +8,16 @@ * There's ABSOLUTELY NO WARRANTY, express or implied. */ -#ifndef _JOHN_MASK_DEVICE_H -#define _JOHN_MASK_DEVICE_H +#ifndef _JOHN_MASK_EXT_H +#define _JOHN_MASK_EXT_H -extern void calc_combination(int n); +#include "mask.h" + +#define MASK_FMT_INT_PLHDR 3 + +extern void mask_calc_combination(cpu_mask_context *); +extern int *mask_skip_ranges; +extern int mask_max_skip_loc; +extern int mask_int_cand_target; #endif \ No newline at end of file