diff -urp john-1.7.9.5/src/AFS_fmt.c john-1.7.9.5-align/src/AFS_fmt.c --- john-1.7.9.5/src/AFS_fmt.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/AFS_fmt.c 2012-07-15 09:32:01 +0000 @@ -36,7 +36,9 @@ static struct fmt_tests tests[] = { #define ALGORITHM_NAME DES_STD_ALGORITHM_NAME #define BINARY_SIZE (3 * ARCH_SIZE) +#define BINARY_ALIGN ARCH_SIZE #define SALT_SIZE 40 +#define SALT_ALIGN 1 #define MIN_KEYS_PER_CRYPT 0x80 #define MAX_KEYS_PER_CRYPT 0x100 @@ -438,7 +440,9 @@ struct fmt_main fmt_AFS = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, FMT_CASE | FMT_8_BIT, diff -urp john-1.7.9.5/src/BF_fmt.c john-1.7.9.5-align/src/BF_fmt.c --- john-1.7.9.5/src/BF_fmt.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/BF_fmt.c 2012-07-15 09:33:40 +0000 @@ -22,7 +22,9 @@ #define CIPHERTEXT_LENGTH 60 #define BINARY_SIZE 4 +#define BINARY_ALIGN 4 #define SALT_SIZE sizeof(BF_salt) +#define SALT_ALIGN 4 #define MIN_KEYS_PER_CRYPT BF_Nmin #define MAX_KEYS_PER_CRYPT BF_N @@ -278,7 +280,9 @@ struct fmt_main fmt_BF = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, #if BF_mt > 1 diff -urp john-1.7.9.5/src/BSDI_fmt.c john-1.7.9.5-align/src/BSDI_fmt.c --- john-1.7.9.5/src/BSDI_fmt.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/BSDI_fmt.c 2012-07-15 09:35:10 +0000 @@ -43,7 +43,9 @@ static struct fmt_tests tests[] = { #define ALGORITHM_NAME DES_BS_ALGORITHM_NAME #define BINARY_SIZE sizeof(ARCH_WORD_32) +#define BINARY_ALIGN sizeof(ARCH_WORD_32) #define SALT_SIZE (ARCH_SIZE * 2) +#define SALT_ALIGN ARCH_SIZE #define MIN_KEYS_PER_CRYPT DES_BS_DEPTH #define MAX_KEYS_PER_CRYPT DES_BS_DEPTH @@ -53,7 +55,9 @@ static struct fmt_tests tests[] = { #define ALGORITHM_NAME DES_STD_ALGORITHM_NAME #define BINARY_SIZE ARCH_SIZE +#define BINARY_ALIGN ARCH_SIZE #define SALT_SIZE (ARCH_SIZE * 2) +#define SALT_ALIGN ARCH_SIZE #define MIN_KEYS_PER_CRYPT 4 #define MAX_KEYS_PER_CRYPT 8 @@ -386,7 +390,9 @@ struct fmt_main fmt_BSDI = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, #if DES_BS && DES_bs_mt diff -urp john-1.7.9.5/src/DES_fmt.c john-1.7.9.5-align/src/DES_fmt.c --- john-1.7.9.5/src/DES_fmt.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/DES_fmt.c 2012-07-15 09:36:35 +0000 @@ -36,7 +36,9 @@ static struct fmt_tests tests[] = { #define ALGORITHM_NAME DES_BS_ALGORITHM_NAME #define BINARY_SIZE sizeof(ARCH_WORD_32) +#define BINARY_ALIGN sizeof(ARCH_WORD_32) #define SALT_SIZE ARCH_SIZE +#define SALT_ALIGN ARCH_SIZE #define MIN_KEYS_PER_CRYPT DES_BS_DEPTH #define MAX_KEYS_PER_CRYPT DES_BS_DEPTH @@ -46,7 +48,9 @@ static struct fmt_tests tests[] = { #define ALGORITHM_NAME DES_STD_ALGORITHM_NAME #define BINARY_SIZE ARCH_SIZE +#define BINARY_ALIGN ARCH_SIZE #define SALT_SIZE ARCH_SIZE +#define SALT_ALIGN ARCH_SIZE #define MIN_KEYS_PER_CRYPT 0x40 #if DES_128K @@ -345,7 +349,9 @@ struct fmt_main fmt_DES = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, #if DES_BS && DES_bs_mt diff -urp john-1.7.9.5/src/LM_fmt.c john-1.7.9.5-align/src/LM_fmt.c --- john-1.7.9.5/src/LM_fmt.c 2012-07-14 13:54:06 +0000 +++ john-1.7.9.5-align/src/LM_fmt.c 2012-07-15 09:37:34 +0000 @@ -39,7 +39,9 @@ static struct fmt_tests tests[] = { #define ALGORITHM_NAME DES_BS_ALGORITHM_NAME #define BINARY_SIZE (sizeof(ARCH_WORD_32) * 2) +#define BINARY_ALIGN sizeof(ARCH_WORD_32) #define SALT_SIZE 0 +#define SALT_ALIGN 1 #define MIN_KEYS_PER_CRYPT DES_BS_DEPTH #define MAX_KEYS_PER_CRYPT DES_BS_DEPTH @@ -196,7 +198,9 @@ struct fmt_main fmt_LM = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, #if DES_bs_mt diff -urp john-1.7.9.5/src/MD5_fmt.c john-1.7.9.5-align/src/MD5_fmt.c --- john-1.7.9.5/src/MD5_fmt.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/MD5_fmt.c 2012-07-15 09:39:00 +0000 @@ -21,7 +21,9 @@ #define CIPHERTEXT_LENGTH 22 #define BINARY_SIZE 4 +#define BINARY_ALIGN 4 #define SALT_SIZE 9 +#define SALT_ALIGN 1 #define MIN_KEYS_PER_CRYPT MD5_N #define MAX_KEYS_PER_CRYPT MD5_N @@ -233,7 +235,9 @@ struct fmt_main fmt_MD5 = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, #if MD5_std_mt diff -urp john-1.7.9.5/src/c3_fmt.c john-1.7.9.5-align/src/c3_fmt.c --- john-1.7.9.5/src/c3_fmt.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/c3_fmt.c 2012-07-15 09:39:28 +0000 @@ -37,7 +37,9 @@ #define PLAINTEXT_LENGTH 72 #define BINARY_SIZE 128 +#define BINARY_ALIGN 1 #define SALT_SIZE BINARY_SIZE +#define SALT_ALIGN 1 #define MIN_KEYS_PER_CRYPT 96 #define MAX_KEYS_PER_CRYPT 96 @@ -423,7 +425,9 @@ struct fmt_main fmt_crypt = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, FMT_CASE | FMT_8_BIT | FMT_OMP, diff -urp john-1.7.9.5/src/dummy.c john-1.7.9.5-align/src/dummy.c --- john-1.7.9.5/src/dummy.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/dummy.c 2012-07-15 09:42:38 +0000 @@ -24,7 +24,9 @@ typedef struct { } dummy_binary; #define BINARY_SIZE sizeof(dummy_binary) +#define BINARY_ALIGN sizeof(ARCH_WORD_32) #define SALT_SIZE 0 +#define SALT_ALIGN 1 #define MIN_KEYS_PER_CRYPT 1 #define MAX_KEYS_PER_CRYPT (0x4000 / (PLAINTEXT_LENGTH + 1)) @@ -265,7 +267,9 @@ struct fmt_main fmt_dummy = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, FMT_CASE | FMT_8_BIT, diff -urp john-1.7.9.5/src/formats.c john-1.7.9.5-align/src/formats.c --- john-1.7.9.5/src/formats.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/formats.c 2012-07-15 09:54:16 +0000 @@ -29,17 +29,14 @@ void fmt_init(struct fmt_main *format) } } -/* - * Test pointers returned by binary() and salt() for possible misalignment. - */ -static int is_misaligned(void *p, int size) +static int is_poweroftwo(size_t align) +{ + return align != 0 && (align & (align - 1)) == 0; +} + +static int is_aligned(void *p, size_t align) { - unsigned long mask = 0; - if (size >= ARCH_SIZE) - mask = ARCH_SIZE - 1; - else if (size >= 4) - mask = 3; - return (unsigned long)p & mask; + return ((size_t)p & (align - 1)) == 0; } static char *fmt_self_test_body(struct fmt_main *format, @@ -51,8 +48,15 @@ static char *fmt_self_test_body(struct f int ntests, done, index, max, size; void *binary, *salt; - if (format->params.plaintext_length > PLAINTEXT_BUFFER_SIZE - 3) - return "length"; + if (format->params.plaintext_length < 1 || + format->params.plaintext_length > PLAINTEXT_BUFFER_SIZE - 3) + return "plaintext_length"; + + if (!is_poweroftwo(format->params.binary_align)) + return "binary_align"; + + if (!is_poweroftwo(format->params.salt_align)) + return "salt_align"; if (format->methods.valid("*", format)) return "valid"; @@ -85,14 +89,14 @@ static char *fmt_self_test_body(struct f */ binary = format->methods.binary(ciphertext); if (!binary || - is_misaligned(binary, format->params.binary_size)) + !is_aligned(binary, format->params.binary_align)) return "binary"; memcpy(binary_copy, binary, format->params.binary_size); binary = binary_copy; salt = format->methods.salt(ciphertext); if (!salt || - is_misaligned(salt, format->params.salt_size)) + !is_aligned(salt, format->params.salt_align)) return "salt"; memcpy(salt_copy, salt, format->params.salt_size); salt = salt_copy; @@ -165,14 +169,21 @@ static char *fmt_self_test_body(struct f * minimum guaranteed alignment. We do this to test that binary_hash*(), * cmp_*(), and salt_hash() do accept such pointers. */ -static void *alloc_binary(size_t size, void **alloc) +static void *alloc_binary(void **alloc, size_t size, size_t align) { - *alloc = mem_alloc(size + 4); - if (size >= ARCH_SIZE) - return *alloc; - if (size >= 4) - return *alloc + 4; - return *alloc + 1; + size_t mask = align - 1; + char *p; + +/* Ensure minimum required alignment and leave room for "align" bytes more */ + p = *alloc = mem_alloc(size + mask + align); + p += mask; + p -= (size_t)p & mask; + +/* If the alignment is too great, reduce it to the minimum */ + if (!((size_t)p & align)) + p += align; + + return p; } char *fmt_self_test(struct fmt_main *format) @@ -181,8 +192,10 @@ char *fmt_self_test(struct fmt_main *for void *binary_alloc, *salt_alloc; void *binary_copy, *salt_copy; - binary_copy = alloc_binary(format->params.binary_size, &binary_alloc); - salt_copy = alloc_binary(format->params.salt_size, &salt_alloc); + binary_copy = alloc_binary(&binary_alloc, + format->params.binary_size, format->params.binary_align); + salt_copy = alloc_binary(&salt_alloc, + format->params.salt_size, format->params.salt_align); retval = fmt_self_test_body(format, binary_copy, salt_copy); diff -urp john-1.7.9.5/src/formats.h john-1.7.9.5-align/src/formats.h --- john-1.7.9.5/src/formats.h 2012-07-14 08:29:51 +0000 +++ john-1.7.9.5-align/src/formats.h 2012-07-15 09:00:07 +0000 @@ -63,11 +63,13 @@ struct fmt_params { /* Maximum length of a plaintext password */ int plaintext_length; -/* Size of binary ciphertext used for fast comparison, in bytes */ +/* Size and alignment of binary ciphertext, in bytes */ int binary_size; + int binary_align; -/* Size of internal salt representation, in bytes */ +/* Size and alignment of internal salt representation, in bytes */ int salt_size; + int salt_align; /* Number of plaintexts hashed by a single crypt_all() method call */ int min_keys_per_crypt; @@ -89,12 +91,6 @@ struct fmt_main; /* * Functions to implement a cracking algorithm. - * - * When passing binary ciphertexts or salts in internal representation, these - * should be ARCH_WORD aligned if their size is that of ARCH_WORD or larger, - * 4-byte aligned if they are smaller than ARCH_WORD but are at least 4 bytes, - * or not necessarily aligned otherwise. The functions may assume such - * alignment. */ struct fmt_methods { /* Initializes the algorithm's internal structures. diff -urp john-1.7.9.5/src/loader.c john-1.7.9.5-align/src/loader.c --- john-1.7.9.5/src/loader.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/loader.c 2012-07-15 09:24:38 +0000 @@ -91,8 +91,8 @@ void ldr_init_database(struct db_main *d { db->loaded = 0; - db->options = mem_alloc_copy(sizeof(struct db_options), - MEM_ALIGN_WORD, options); + db->options = mem_alloc_copy(options, + sizeof(struct db_options), MEM_ALIGN_WORD); db->salts = NULL; @@ -449,21 +449,6 @@ static struct list_main *ldr_init_words( return words; } -/* - * Use a smaller alignment for binary ciphertexts and salts that are smaller - * than ARCH_SIZE bytes. This saves some memory when dealing with 32-bit - * values on a 64-bit system. - */ -static void *alloc_copy_autoalign(size_t size, void *src) -{ - size_t align = MEM_ALIGN_NONE; - if (size >= ARCH_SIZE) - align = MEM_ALIGN_WORD; - else if (size >= 4) - align = 4; - return mem_alloc_copy(size, align, src); -} - static void ldr_load_pw_line(struct db_main *db, char *line) { static int skip_dupe_checking = 0; @@ -560,8 +545,9 @@ static void ldr_load_pw_line(struct db_m mem_alloc_tiny(salt_size, MEM_ALIGN_WORD); current_salt->next = last_salt; - current_salt->salt = alloc_copy_autoalign( - format->params.salt_size, salt); + current_salt->salt = mem_alloc_copy(salt, + format->params.salt_size, + format->params.salt_align); current_salt->index = fmt_dummy_hash; current_salt->bitmap = NULL; @@ -596,8 +582,9 @@ static void ldr_load_pw_line(struct db_m current_pw->binary = memcpy(¤t_pw->source, binary, format->params.binary_size); else - current_pw->binary = alloc_copy_autoalign( - format->params.binary_size, binary); + current_pw->binary = mem_alloc_copy(binary, + format->params.binary_size, + format->params.binary_align); if (format->methods.source == fmt_default_source) current_pw->source = str_alloc_copy(piece); diff -urp john-1.7.9.5/src/memory.c john-1.7.9.5-align/src/memory.c --- john-1.7.9.5/src/memory.c 2010-01-17 12:36:08 +0000 +++ john-1.7.9.5-align/src/memory.c 2012-07-15 10:35:53 +0000 @@ -1,6 +1,6 @@ /* * This file is part of John the Ripper password cracker, - * Copyright (c) 1996-98,2010 by Solar Designer + * Copyright (c) 1996-98,2010,2012 by Solar Designer */ #include @@ -30,32 +30,47 @@ void *mem_alloc(size_t size) void *mem_alloc_tiny(size_t size, size_t align) { - static unsigned long buffer, bufree = 0; - unsigned long start, end; + static char *buffer = NULL; + static size_t bufree = 0; + size_t mask; + char *p; #if ARCH_ALLOWS_UNALIGNED - if (mem_saving_level > 2) align = MEM_ALIGN_NONE; + if (mem_saving_level > 2) + align = MEM_ALIGN_NONE; #endif - start = buffer + --align; start &= ~align; - end = start + size; + mask = align - 1; - if (bufree >= end - buffer) { - bufree -= end - buffer; - buffer = end; - } else - if (size + align <= MEM_ALLOC_SIZE && bufree <= MEM_ALLOC_MAX_WASTE) { - buffer = (unsigned long)mem_alloc(MEM_ALLOC_SIZE); + do { + if (buffer) { + size_t need = + size + mask - (((size_t)buffer + mask) & mask); + if (bufree >= need) { + p = buffer; + p += mask; + p -= (size_t)p & mask; + bufree -= need; + buffer = p + size; + return p; + } + } + + if (size + mask > MEM_ALLOC_SIZE || + bufree > MEM_ALLOC_MAX_WASTE) + break; + + buffer = mem_alloc(MEM_ALLOC_SIZE); bufree = MEM_ALLOC_SIZE; - return mem_alloc_tiny(size, align + 1); - } else - start = ((unsigned long) - mem_alloc(size + align) + align) & ~align; + } while (1); - return (void *)start; + p = mem_alloc(size + mask); + p += mask; + p -= (size_t)p & mask; + return p; } -void *mem_alloc_copy(size_t size, size_t align, void *src) +void *mem_alloc_copy(void *src, size_t size, size_t align) { return memcpy(mem_alloc_tiny(size, align), src, size); } diff -urp john-1.7.9.5/src/memory.h john-1.7.9.5-align/src/memory.h --- john-1.7.9.5/src/memory.h 2011-02-25 10:09:20 +0000 +++ john-1.7.9.5-align/src/memory.h 2012-07-15 10:42:03 +0000 @@ -1,6 +1,6 @@ /* * This file is part of John the Ripper password cracker, - * Copyright (c) 1996-98,2003,2010,2011 by Solar Designer + * Copyright (c) 1996-98,2003,2010-2012 by Solar Designer */ /* @@ -75,7 +75,7 @@ extern void *mem_alloc_tiny(size_t size, /* * Uses mem_alloc_tiny() to allocate the memory, and copies src in there. */ -extern void *mem_alloc_copy(size_t size, size_t align, void *src); +extern void *mem_alloc_copy(void *src, size_t size, size_t align); /* * Similar to the above function, but for ASCIIZ strings. diff -urp john-1.7.9.5/src/trip_fmt.c john-1.7.9.5-align/src/trip_fmt.c --- john-1.7.9.5/src/trip_fmt.c 2012-07-14 13:36:44 +0000 +++ john-1.7.9.5-align/src/trip_fmt.c 2012-07-15 09:41:42 +0000 @@ -21,6 +21,7 @@ #define CIPHERTEXT_LENGTH 10 #define SALT_SIZE 0 +#define SALT_ALIGN 1 static struct fmt_tests tests[] = { {"Rk7VUsDT2U", "simpson"}, @@ -36,6 +37,7 @@ static struct fmt_tests tests[] = { #define ALGORITHM_NAME DES_BS_ALGORITHM_NAME #define BINARY_SIZE sizeof(ARCH_WORD_32) +#define BINARY_ALIGN sizeof(ARCH_WORD_32) #define TRIPCODE_SCALE 0x40 @@ -58,6 +60,7 @@ static int (*next_hash_func)(int index); #define ALGORITHM_NAME DES_STD_ALGORITHM_NAME #define BINARY_SIZE ARCH_SIZE +#define BINARY_ALIGN ARCH_SIZE #define MIN_KEYS_PER_CRYPT 0x40 #define MAX_KEYS_PER_CRYPT 0x1000 @@ -573,7 +576,9 @@ struct fmt_main fmt_trip = { BENCHMARK_LENGTH, PLAINTEXT_LENGTH, BINARY_SIZE, + BINARY_ALIGN, SALT_SIZE, + SALT_ALIGN, MIN_KEYS_PER_CRYPT, MAX_KEYS_PER_CRYPT, #if DES_BS && DES_bs_mt