"Fossies" - the Fresh Open Source Software Archive

Member "john-1.9.0/src/DES_fmt.c" (7 Apr 2019, 7699 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 "DES_fmt.c" 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,2010-2012,2017 by Solar Designer
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted.
    7  *
    8  * There's ABSOLUTELY NO WARRANTY, express or implied.
    9  */
   10 
   11 #include <stdint.h>
   12 #include <string.h>
   13 
   14 #include "arch.h"
   15 #include "DES_std.h"
   16 #include "common.h"
   17 #include "formats.h"
   18 
   19 #define FORMAT_LABEL            "descrypt"
   20 #define FORMAT_NAME         "traditional crypt(3)"
   21 
   22 #define BENCHMARK_COMMENT       ""
   23 #define BENCHMARK_LENGTH        7
   24 
   25 #define PLAINTEXT_LENGTH        8
   26 #define CIPHERTEXT_LENGTH_1     13
   27 #define CIPHERTEXT_LENGTH_2     24
   28 
   29 static struct fmt_tests tests[] = {
   30     {"CCNf8Sbh3HDfQ", "U*U*U*U*"},
   31     {"CCX.K.MFy4Ois", "U*U***U"},
   32     {"CC4rMpbg9AMZ.", "U*U***U*"},
   33     {"XXxzOu6maQKqQ", "*U*U*U*U"},
   34     {"SDbsugeBiC58A", ""},
   35     {NULL}
   36 };
   37 
   38 #if DES_BS
   39 
   40 #include "DES_bs.h"
   41 
   42 #define ALGORITHM_NAME          DES_BS_ALGORITHM_NAME
   43 
   44 #define BINARY_SIZE         sizeof(uint32_t)
   45 #define BINARY_ALIGN            sizeof(uint32_t)
   46 #define SALT_SIZE           ARCH_SIZE
   47 #define SALT_ALIGN          ARCH_SIZE
   48 
   49 #define MIN_KEYS_PER_CRYPT      DES_BS_DEPTH
   50 #define MAX_KEYS_PER_CRYPT      DES_BS_DEPTH
   51 
   52 #else
   53 
   54 #define ALGORITHM_NAME          DES_STD_ALGORITHM_NAME
   55 
   56 #define BINARY_SIZE         ARCH_SIZE
   57 #define BINARY_ALIGN            ARCH_SIZE
   58 #define SALT_SIZE           ARCH_SIZE
   59 #define SALT_ALIGN          ARCH_SIZE
   60 
   61 #define MIN_KEYS_PER_CRYPT      0x40
   62 #if DES_128K
   63 #define MAX_KEYS_PER_CRYPT      0x100
   64 #else
   65 #define MAX_KEYS_PER_CRYPT      0x80
   66 #endif
   67 
   68 static struct {
   69     union {
   70         double dummy;
   71         struct {
   72             DES_KS KS;
   73             DES_binary binary;
   74         } data;
   75     } aligned;
   76     char key[PLAINTEXT_LENGTH];
   77 } buffer[MAX_KEYS_PER_CRYPT];
   78 
   79 #endif
   80 
   81 #if DES_BS && DES_bs_mt
   82 struct fmt_main fmt_DES;
   83 #endif
   84 
   85 static void init(struct fmt_main *self)
   86 {
   87 #if DES_BS
   88     DES_bs_init(0, DES_bs_cpt);
   89 #if DES_bs_mt
   90     fmt_DES.params.min_keys_per_crypt = DES_bs_min_kpc;
   91     fmt_DES.params.max_keys_per_crypt = DES_bs_max_kpc;
   92 #endif
   93 #else
   94     DES_std_init();
   95 #endif
   96 }
   97 
   98 static int valid(char *ciphertext, struct fmt_main *self)
   99 {
  100     char *pos;
  101 
  102     if (!ciphertext[0] || !ciphertext[1]) return 0;
  103 
  104     for (pos = &ciphertext[2]; atoi64[ARCH_INDEX(*pos)] != 0x7F; pos++);
  105     if (*pos && *pos != ',') return 0;
  106 
  107     if (atoi64[ARCH_INDEX(*(pos - 1))] & 3) return 0;
  108 
  109     switch (pos - ciphertext) {
  110     case CIPHERTEXT_LENGTH_1:
  111         return 1;
  112 
  113     case CIPHERTEXT_LENGTH_2:
  114         if (atoi64[ARCH_INDEX(ciphertext[12])] & 3) return 0;
  115         return 2;
  116 
  117     default:
  118         return 0;
  119     }
  120 }
  121 
  122 static char *split(char *ciphertext, int index, struct fmt_main *self)
  123 {
  124     static char out[14];
  125 
  126     if (index) {
  127         memcpy(out, &ciphertext[2], 2);
  128         memcpy(&out[2], &ciphertext[13], 11);
  129     } else
  130         memcpy(out, ciphertext, 13);
  131 
  132     out[13] = 0;
  133     return out;
  134 }
  135 
  136 static void *salt(char *ciphertext)
  137 {
  138     static ARCH_WORD out;
  139 
  140 #if DES_BS
  141     out = DES_raw_get_salt(ciphertext);
  142 #else
  143     out = DES_std_get_salt(ciphertext);
  144 #endif
  145 
  146     return &out;
  147 }
  148 
  149 #if DES_BS
  150 
  151 static int binary_hash_0(void *binary)
  152 {
  153     return *(uint32_t *)binary & PH_MASK_0;
  154 }
  155 
  156 static int binary_hash_1(void *binary)
  157 {
  158     return *(uint32_t *)binary & PH_MASK_1;
  159 }
  160 
  161 static int binary_hash_2(void *binary)
  162 {
  163     return *(uint32_t *)binary & PH_MASK_2;
  164 }
  165 
  166 static int binary_hash_3(void *binary)
  167 {
  168     return *(uint32_t *)binary & PH_MASK_3;
  169 }
  170 
  171 static int binary_hash_4(void *binary)
  172 {
  173     return *(uint32_t *)binary & PH_MASK_4;
  174 }
  175 
  176 static int binary_hash_5(void *binary)
  177 {
  178     return *(uint32_t *)binary & PH_MASK_5;
  179 }
  180 
  181 static int binary_hash_6(void *binary)
  182 {
  183     return *(uint32_t *)binary & PH_MASK_6;
  184 }
  185 
  186 #define get_hash_0 DES_bs_get_hash_0
  187 #define get_hash_1 DES_bs_get_hash_1
  188 #define get_hash_2 DES_bs_get_hash_2
  189 #define get_hash_3 DES_bs_get_hash_3
  190 #define get_hash_4 DES_bs_get_hash_4
  191 #define get_hash_5 DES_bs_get_hash_5
  192 #define get_hash_6 DES_bs_get_hash_6
  193 
  194 static int salt_hash(void *salt)
  195 {
  196     return *(ARCH_WORD *)salt & (SALT_HASH_SIZE - 1);
  197 }
  198 
  199 static void set_salt(void *salt)
  200 {
  201     DES_bs_set_salt(*(ARCH_WORD *)salt);
  202 }
  203 
  204 static int crypt_all(int *pcount, struct db_salt *salt)
  205 {
  206     int count = *pcount;
  207     DES_bs_crypt_25(count);
  208     return count;
  209 }
  210 
  211 static int cmp_one(void *binary, int index)
  212 {
  213     return DES_bs_cmp_one((uint32_t *)binary, 32, index);
  214 }
  215 
  216 static int cmp_exact(char *source, int index)
  217 {
  218     return DES_bs_cmp_one(DES_bs_get_binary(source), 64, index);
  219 }
  220 
  221 #else
  222 
  223 static int binary_hash_0(void *binary)
  224 {
  225     return DES_STD_HASH_0(*(ARCH_WORD *)binary);
  226 }
  227 
  228 static int binary_hash_1(void *binary)
  229 {
  230     return DES_STD_HASH_1(*(ARCH_WORD *)binary);
  231 }
  232 
  233 #define binary_hash_2 NULL
  234 #define binary_hash_3 NULL
  235 #define binary_hash_4 NULL
  236 #define binary_hash_5 NULL
  237 #define binary_hash_6 NULL
  238 
  239 static int get_hash_0(int index)
  240 {
  241     ARCH_WORD binary;
  242 
  243     binary = buffer[index].aligned.data.binary[0];
  244     return DES_STD_HASH_0(binary);
  245 }
  246 
  247 static int get_hash_1(int index)
  248 {
  249     ARCH_WORD binary;
  250 
  251     binary = buffer[index].aligned.data.binary[0];
  252     return DES_STD_HASH_1(binary);
  253 }
  254 
  255 #define get_hash_2 NULL
  256 #define get_hash_3 NULL
  257 #define get_hash_4 NULL
  258 #define get_hash_5 NULL
  259 #define get_hash_6 NULL
  260 
  261 static int salt_hash(void *salt)
  262 {
  263     return DES_STD_HASH_1(*(ARCH_WORD *)salt) & (SALT_HASH_SIZE - 1);
  264 }
  265 
  266 static void set_salt(void *salt)
  267 {
  268     DES_std_set_salt(*(ARCH_WORD *)salt);
  269 }
  270 
  271 static int crypt_all(int *pcount, struct db_salt *salt)
  272 {
  273     int count = *pcount;
  274     int index;
  275 
  276     for (index = 0; index < count; index++)
  277         DES_std_crypt(buffer[index].aligned.data.KS,
  278             buffer[index].aligned.data.binary);
  279 
  280     return count;
  281 }
  282 
  283 static int cmp_all(void *binary, int count)
  284 {
  285     int index;
  286 
  287     for (index = 0; index < count; index++)
  288     if (*(unsigned ARCH_WORD *)binary ==
  289         (buffer[index].aligned.data.binary[0] & DES_BINARY_MASK))
  290         return 1;
  291 
  292     return 0;
  293 }
  294 
  295 static int cmp_one(void *binary, int index)
  296 {
  297     return *(unsigned ARCH_WORD *)binary ==
  298         (buffer[index].aligned.data.binary[0] & DES_BINARY_MASK);
  299 }
  300 
  301 static int cmp_exact(char *source, int index)
  302 {
  303     ARCH_WORD *binary;
  304     int word;
  305 
  306     binary = DES_std_get_binary(source);
  307 
  308     for (word = 0; word < 16 / DES_SIZE; word++)
  309     if ((unsigned ARCH_WORD)binary[word] !=
  310         (buffer[index].aligned.data.binary[word] & DES_BINARY_MASK))
  311         return 0;
  312 
  313     return 1;
  314 }
  315 
  316 #endif
  317 
  318 #if !DES_BS
  319 static void set_key(char *key, int index)
  320 {
  321     DES_std_set_key(key);
  322     memcpy(buffer[index].aligned.data.KS, DES_KS_current, sizeof(DES_KS));
  323     memcpy(buffer[index].key, key, PLAINTEXT_LENGTH);
  324 }
  325 #endif
  326 
  327 static char *get_key(int index)
  328 {
  329     static char out[PLAINTEXT_LENGTH + 1];
  330 #if DES_BS
  331     unsigned char *src;
  332     char *dst;
  333 
  334     init_t();
  335 
  336     src = DES_bs_all.pxkeys[index];
  337     dst = out;
  338     while (dst < &out[PLAINTEXT_LENGTH] && (*dst = *src)) {
  339         src += sizeof(DES_bs_vector) * 8;
  340         dst++;
  341     }
  342     *dst = 0;
  343 #else
  344     memcpy(out, buffer[index].key, PLAINTEXT_LENGTH);
  345     out[PLAINTEXT_LENGTH] = 0;
  346 #endif
  347 
  348     return out;
  349 }
  350 
  351 struct fmt_main fmt_DES = {
  352     {
  353         FORMAT_LABEL,
  354         FORMAT_NAME,
  355         ALGORITHM_NAME,
  356         BENCHMARK_COMMENT,
  357         BENCHMARK_LENGTH,
  358         PLAINTEXT_LENGTH,
  359         BINARY_SIZE,
  360         BINARY_ALIGN,
  361         SALT_SIZE,
  362         SALT_ALIGN,
  363         MIN_KEYS_PER_CRYPT,
  364         MAX_KEYS_PER_CRYPT,
  365 #if DES_BS && DES_bs_mt
  366         FMT_OMP |
  367 #endif
  368 #if DES_BS
  369         FMT_CASE | FMT_BS,
  370 #else
  371         FMT_CASE,
  372 #endif
  373         tests
  374     }, {
  375         init,
  376         fmt_default_done,
  377         fmt_default_reset,
  378         fmt_default_prepare,
  379         valid,
  380         split,
  381         (void *(*)(char *))
  382 #if DES_BS
  383             DES_bs_get_binary,
  384 #else
  385             DES_std_get_binary,
  386 #endif
  387         salt,
  388         fmt_default_source,
  389         {
  390             binary_hash_0,
  391             binary_hash_1,
  392             binary_hash_2,
  393             binary_hash_3,
  394             binary_hash_4,
  395             binary_hash_5,
  396             binary_hash_6
  397         },
  398         salt_hash,
  399         set_salt,
  400 #if DES_BS
  401         DES_bs_set_key,
  402 #else
  403         set_key,
  404 #endif
  405         get_key,
  406         fmt_default_clear_keys,
  407         crypt_all,
  408         {
  409             get_hash_0,
  410             get_hash_1,
  411             get_hash_2,
  412             get_hash_3,
  413             get_hash_4,
  414             get_hash_5,
  415             get_hash_6
  416         },
  417 #if DES_BS
  418         (int (*)(void *, int))DES_bs_cmp_all,
  419 #else
  420         cmp_all,
  421 #endif
  422         cmp_one,
  423         cmp_exact
  424     }
  425 };