"Fossies" - the Fresh Open Source Software Archive

Member "john-1.9.0/src/formats.h" (21 Mar 2019, 10849 Bytes) of package /linux/privat/john-1.9.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "formats.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.8.0_vs_1.9.0.

    1 /*
    2  * This file is part of John the Ripper password cracker,
    3  * Copyright (c) 1996-2001,2005,2010-2013,2015 by Solar Designer
    4  */
    5 
    6 /*
    7  * Supported ciphertext formats management.
    8  */
    9 
   10 #ifndef _JOHN_FORMATS_H
   11 #define _JOHN_FORMATS_H
   12 
   13 #include "params.h"
   14 
   15 /*
   16  * Some format methods accept pointers to these, yet we can't just include
   17  * loader.h here because that would be a circular dependency.
   18  */
   19 struct db_main;
   20 struct db_salt;
   21 
   22 /*
   23  * Format property flags.
   24  */
   25 /* Uses case-sensitive passwords */
   26 #define FMT_CASE            0x00000001
   27 /* Supports 8-bit characters in passwords (does not ignore the 8th bit) */
   28 #define FMT_8_BIT           0x00000002
   29 /* Uses a bitslice implementation */
   30 #define FMT_BS              0x00010000
   31 /* The split() method unifies the case of characters in hash encodings */
   32 #define FMT_SPLIT_UNIFIES_CASE      0x00020000
   33 #ifdef _OPENMP
   34 /* Parallelized with OpenMP */
   35 #define FMT_OMP             0x01000000
   36 /* Poor OpenMP scalability */
   37 #define FMT_OMP_BAD         0x02000000
   38 #else
   39 #define FMT_OMP             0
   40 #define FMT_OMP_BAD         0
   41 #endif
   42 /* We've already warned the user about hashes of this type being present */
   43 #define FMT_WARNED          0x80000000
   44 
   45 /*
   46  * A password to test the methods for correct operation.
   47  */
   48 struct fmt_tests {
   49     char *ciphertext, *plaintext;
   50     char *fields[10];
   51 };
   52 
   53 /*
   54  * Parameters of a hash function and its cracking algorithm.
   55  */
   56 struct fmt_params {
   57 /* Label to refer to this format (any alphabetical characters in it must be
   58  * lowercase). */
   59     const char *label;
   60 
   61 /* Ciphertext format name */
   62     const char *format_name;
   63 
   64 /* Cracking algorithm name */
   65     const char *algorithm_name;
   66 
   67 /* Comment about the benchmark (can be empty) */
   68     const char *benchmark_comment;
   69 
   70 /* Benchmark for short/long passwords instead of for one/many salts */
   71     int benchmark_length;
   72 
   73 /* Maximum length of a plaintext password */
   74     int plaintext_length;
   75 
   76 /* Size and alignment of binary ciphertext, in bytes */
   77     int binary_size;
   78     int binary_align;
   79 
   80 /* Size and alignment of internal salt representation, in bytes */
   81     int salt_size;
   82     int salt_align;
   83 
   84 /* Number of plaintexts hashed by a single crypt_all() method call */
   85     int min_keys_per_crypt;
   86     int max_keys_per_crypt;
   87 
   88 /* Properties of this format */
   89     unsigned int flags;
   90 
   91 /* Some passwords to test the methods for correct operation (or NULL for no
   92  * self test, and no benchmark), terminated with a NULL ciphertext. */
   93     struct fmt_tests *tests;
   94 };
   95 
   96 /*
   97  * fmt_main is declared for real further down this file, but we refer to it in
   98  * function prototypes in fmt_methods.
   99  */
  100 struct fmt_main;
  101 
  102 /*
  103  * Functions to implement a cracking algorithm.
  104  */
  105 struct fmt_methods {
  106 /* Initializes the algorithm's internal structures.
  107  * prepare(), valid(), and split() are the only methods that are allowed to be
  108  * called before a call to init().
  109  * Note that initializing an algorithm might de-initialize some others (if a
  110  * shared underlying resource is used). */
  111     void (*init)(struct fmt_main *self);
  112 
  113 /* De-initializes this format, which must have been previously initialized */
  114     void (*done)(void);
  115 
  116 /* Called whenever the set of password hashes being cracked changes, such as
  117  * after self-test, but before actual cracking starts.  When called before a
  118  * self-test or benchmark rather than before actual cracking, db may be NULL.
  119  * Normally, this is a no-op since a format implementation shouldn't mess with
  120  * the database unnecessarily.  However, when there is a good reason to do so
  121  * this may e.g. transfer the salts and hashes onto a GPU card. */
  122     void (*reset)(struct db_main *db);
  123 
  124 /* Extracts the ciphertext string out of the input file fields.  Normally, this
  125  * will simply return field[1], but in some special cases it may use another
  126  * field (e.g., when the hash type is commonly used with PWDUMP rather than
  127  * /etc/passwd format files) or/and it may also extract and include the
  128  * username, etc. */
  129     char *(*prepare)(char *fields[10], struct fmt_main *self);
  130 
  131 /* Checks if an ASCII ciphertext is valid for this format.  Returns zero for
  132  * invalid ciphertexts, or the number of parts the ciphertext should be split
  133  * into (up to 9, will usually be 1). */
  134     int (*valid)(char *ciphertext, struct fmt_main *self);
  135 
  136 /* Splits a ciphertext into several pieces and returns the piece with given
  137  * index, starting from 0 (will usually return the ciphertext unchanged).
  138  * For hex-encoded hashes which are compared by the target system/application
  139  * irrespective of the case of characters (upper/lower/mixed) used in their
  140  * encoding, split() must unify the case (e.g., convert to all-lowercase)
  141  * and FMT_SPLIT_UNIFIES_CASE must be set. */
  142     char *(*split)(char *ciphertext, int index, struct fmt_main *self);
  143 
  144 /* Converts an ASCII ciphertext to binary, possibly using the salt */
  145     void *(*binary)(char *ciphertext);
  146 
  147 /* Converts an ASCII salt to its internal representation */
  148     void *(*salt)(char *ciphertext);
  149 
  150 /* Reconstructs the ASCII ciphertext from its binary (saltless only).
  151  * Alternatively, in the simplest case simply returns "source" as-is. */
  152     char *(*source)(char *source, void *binary);
  153 
  154 /* These functions calculate a hash out of a binary ciphertext. To be used
  155  * for hash table initialization. One of them should be selected depending
  156  * on the hash table size. */
  157     int (*binary_hash[PASSWORD_HASH_SIZES])(void *binary);
  158 
  159 /* Calculates a hash out of a salt (given in internal representation). To be
  160  * used by the password file loader. */
  161     int (*salt_hash)(void *salt);
  162 
  163 /* Sets a salt for the crypt_all() method */
  164     void (*set_salt)(void *salt);
  165 
  166 /* Sets a plaintext, with index from 0 to fmt_params.max_keys_per_crypt - 1.
  167  * The string is NUL-terminated, but set_key() may over-read it until up to
  168  * PLAINTEXT_BUFFER_SIZE total read (thus, the caller's buffer must be at least
  169  * this large).  Empty string may be passed as fmt_null_key. */
  170     void (*set_key)(char *key, int index);
  171 
  172 /* Returns a plaintext previously set with and potentially altered by
  173  * set_key() (e.g., converted to all-uppercase and truncated at 7 for LM
  174  * hashes).  The plaintext may also have been generated or altered by
  175  * crypt_all().  Depending on crypt_all() implementation, the index used here
  176  * does not have to match an index previously used with set_key(), although
  177  * for most formats it does.  See the description of crypt_all() below. */
  178     char *(*get_key)(int index);
  179 
  180 /* Allow the previously set keys to be dropped if that would help improve
  181  * performance and/or reduce the impact of certain hardware faults. After
  182  * a call to clear_keys() the keys are undefined. */
  183     void (*clear_keys)(void);
  184 
  185 /* Computes the ciphertexts for given salt and plaintexts.
  186  * For implementation reasons, this may happen to always compute at least
  187  * min_keys_per_crypt ciphertexts even if the requested count is lower,
  188  * although it is preferable for implementations to obey the count whenever
  189  * practical and also for callers not to call crypt_all() with fewer than
  190  * min_keys_per_crypt keys whenever practical.
  191  * Returns the last output index for which there might be a match (against the
  192  * supplied salt's hashes) plus 1.  A return value of zero indicates no match.
  193  * Note that output indices don't have to match input indices (although they
  194  * may and usually do).  The indices passed to get_key(), get_hash[](),
  195  * cmp_one(), and cmp_exact() must be in the 0 to crypt_all() return value
  196  * minus 1 range, although for infrequent status reporting get_key() may also
  197  * be called on indices previously supplied to set_key() as well as on indices
  198  * up to the updated *count minus 1 even if they're beyond this range.
  199  * The count passed to cmp_all() must be equal to crypt_all()'s return value.
  200  * If an implementation does not use the salt parameter or if salt is NULL
  201  * (as it may be during self-test and benchmark), the return value must always
  202  * match *count the way it is after the crypt_all() call.
  203  * The count is passed by reference and must be updated by crypt_all() if it
  204  * computes other than the requested count (such as if it generates additional
  205  * candidate passwords on its own).  The updated count is used for c/s rate
  206  * calculation.  The return value is thus in the 0 to updated *count range. */
  207     int (*crypt_all)(int *count, struct db_salt *salt);
  208 
  209 /* These functions calculate a hash out of a ciphertext that has just been
  210  * generated with the crypt_all() method. To be used while cracking. */
  211     int (*get_hash[PASSWORD_HASH_SIZES])(int index);
  212 
  213 /* Compares a given ciphertext against all the crypt_all() method outputs and
  214  * returns zero if no matches detected. A non-zero return value means that
  215  * there might be matches, and more checks are needed. */
  216     int (*cmp_all)(void *binary, int count);
  217 
  218 /* Same as the above, except the comparison is done against only one of the
  219  * crypt_all() method outputs. */
  220     int (*cmp_one)(void *binary, int index);
  221 
  222 /* Compares an ASCII ciphertext against a particular crypt_all() output */
  223     int (*cmp_exact)(char *source, int index);
  224 };
  225 
  226 /*
  227  * Private fields for formats management.
  228  */
  229 struct fmt_private {
  230     int initialized;
  231 };
  232 
  233 /*
  234  * A structure to keep a list of supported ciphertext formats.
  235  */
  236 struct fmt_main {
  237     struct fmt_params params;
  238     struct fmt_methods methods;
  239     struct fmt_private private;
  240     struct fmt_main *next;
  241 };
  242 
  243 /*
  244  * Empty key that is safe to pass to the set_key() method, given that it may
  245  * over-read the empty string for up to PLAINTEXT_BUFFER_SIZE.
  246  */
  247 extern char fmt_null_key[PLAINTEXT_BUFFER_SIZE];
  248 
  249 /*
  250  * Linked list of registered formats.
  251  */
  252 extern struct fmt_main *fmt_list;
  253 
  254 /*
  255  * Format registration function.
  256  */
  257 extern void fmt_register(struct fmt_main *format);
  258 
  259 /*
  260  * Initializes the format's internal structures unless already initialized.
  261  */
  262 extern void fmt_init(struct fmt_main *format);
  263 
  264 /*
  265  * De-initializes this format if it was previously initialized.
  266  */
  267 extern void fmt_done(struct fmt_main *format);
  268 
  269 /*
  270  * Tests the format's methods for correct operation. Returns NULL on
  271  * success, method name on error.
  272  */
  273 extern char *fmt_self_test(struct fmt_main *format);
  274 
  275 /*
  276  * Default methods.
  277  */
  278 extern void fmt_default_init(struct fmt_main *self);
  279 extern void fmt_default_done(void);
  280 extern void fmt_default_reset(struct db_main *db);
  281 extern char *fmt_default_prepare(char *fields[10], struct fmt_main *self);
  282 extern char *fmt_default_split(char *ciphertext, int index,
  283     struct fmt_main *self);
  284 extern void *fmt_default_binary(char *ciphertext);
  285 extern void *fmt_default_salt(char *ciphertext);
  286 extern char *fmt_default_source(char *source, void *binary);
  287 extern int fmt_default_binary_hash(void *binary);
  288 extern int fmt_default_salt_hash(void *salt);
  289 extern void fmt_default_set_salt(void *salt);
  290 extern void fmt_default_clear_keys(void);
  291 extern int fmt_default_get_hash(int index);
  292 
  293 /*
  294  * Dummy hash function to use for salts with no hash table.
  295  */
  296 #define fmt_dummy_hash fmt_default_get_hash
  297 
  298 #endif