1 /**
2 * Author......: See docs/credits.txt
3 * License.....: MIT
4 */
5
6 #include "common.h"
7 #include "types.h"
8 #include "convert.h"
9 #include "shared.h"
10 #include "memory.h"
11 #include "ext_lzma.h"
12 #include <errno.h>
13
14 #if defined (__CYGWIN__)
15 #include <sys/cygwin.h>
16 #endif
17
18 static const char *const PA_000 = "OK";
19 static const char *const PA_001 = "Ignored due to comment";
20 static const char *const PA_002 = "Ignored due to zero length";
21 static const char *const PA_003 = "Line-length exception";
22 static const char *const PA_004 = "Hash-length exception";
23 static const char *const PA_005 = "Hash-value exception";
24 static const char *const PA_006 = "Salt-length exception";
25 static const char *const PA_007 = "Salt-value exception";
26 static const char *const PA_008 = "Salt-iteration count exception";
27 static const char *const PA_009 = "Separator unmatched";
28 static const char *const PA_010 = "Signature unmatched";
29 static const char *const PA_011 = "Invalid hccapx file size";
30 static const char *const PA_012 = "Invalid hccapx eapol size";
31 static const char *const PA_013 = "Invalid psafe2 filesize";
32 static const char *const PA_014 = "Invalid psafe3 filesize";
33 static const char *const PA_015 = "Invalid truecrypt filesize";
34 static const char *const PA_016 = "Invalid veracrypt filesize";
35 static const char *const PA_017 = "Invalid SIP directive, only MD5 is supported";
36 static const char *const PA_018 = "Hash-file exception";
37 static const char *const PA_019 = "Hash-encoding exception";
38 static const char *const PA_020 = "Salt-encoding exception";
39 static const char *const PA_021 = "Invalid LUKS filesize";
40 static const char *const PA_022 = "Invalid LUKS identifier";
41 static const char *const PA_023 = "Invalid LUKS version";
42 static const char *const PA_024 = "Invalid or unsupported LUKS cipher type";
43 static const char *const PA_025 = "Invalid or unsupported LUKS cipher mode";
44 static const char *const PA_026 = "Invalid or unsupported LUKS hash type";
45 static const char *const PA_027 = "Invalid LUKS key size";
46 static const char *const PA_028 = "Disabled LUKS key detected";
47 static const char *const PA_029 = "Invalid LUKS key AF stripes count";
48 static const char *const PA_030 = "Invalid combination of LUKS hash type and cipher type";
49 static const char *const PA_031 = "Invalid hccapx signature";
50 static const char *const PA_032 = "Invalid hccapx version";
51 static const char *const PA_033 = "Invalid hccapx message pair";
52 static const char *const PA_034 = "Token encoding exception";
53 static const char *const PA_035 = "Token length exception";
54 static const char *const PA_036 = "Insufficient entropy exception";
55 static const char *const PA_037 = "Hash contains unsupported compression type for current mode";
56 static const char *const PA_038 = "Invalid key size";
57 static const char *const PA_039 = "Invalid block size";
58 static const char *const PA_040 = "Invalid or unsupported cipher";
59 static const char *const PA_041 = "Invalid filesize";
60 static const char *const PA_042 = "IV length exception";
61 static const char *const PA_043 = "CT length exception";
62 static const char *const PA_255 = "Unknown error";
63
64 static const char *const OPTI_STR_OPTIMIZED_KERNEL = "Optimized-Kernel";
65 static const char *const OPTI_STR_ZERO_BYTE = "Zero-Byte";
66 static const char *const OPTI_STR_PRECOMPUTE_INIT = "Precompute-Init";
67 static const char *const OPTI_STR_MEET_IN_MIDDLE = "Meet-In-The-Middle";
68 static const char *const OPTI_STR_EARLY_SKIP = "Early-Skip";
69 static const char *const OPTI_STR_NOT_SALTED = "Not-Salted";
70 static const char *const OPTI_STR_NOT_ITERATED = "Not-Iterated";
71 static const char *const OPTI_STR_PREPENDED_SALT = "Prepended-Salt";
72 static const char *const OPTI_STR_APPENDED_SALT = "Appended-Salt";
73 static const char *const OPTI_STR_SINGLE_HASH = "Single-Hash";
74 static const char *const OPTI_STR_SINGLE_SALT = "Single-Salt";
75 static const char *const OPTI_STR_BRUTE_FORCE = "Brute-Force";
76 static const char *const OPTI_STR_RAW_HASH = "Raw-Hash";
77 static const char *const OPTI_STR_SLOW_HASH_SIMD_INIT = "Slow-Hash-SIMD-INIT";
78 static const char *const OPTI_STR_SLOW_HASH_SIMD_LOOP = "Slow-Hash-SIMD-LOOP";
79 static const char *const OPTI_STR_SLOW_HASH_SIMD_COMP = "Slow-Hash-SIMD-COMP";
80 static const char *const OPTI_STR_USES_BITS_8 = "Uses-8-Bit";
81 static const char *const OPTI_STR_USES_BITS_16 = "Uses-16-Bit";
82 static const char *const OPTI_STR_USES_BITS_32 = "Uses-32-Bit";
83 static const char *const OPTI_STR_USES_BITS_64 = "Uses-64-Bit";
84
85 static const char *const HASH_CATEGORY_UNDEFINED_STR = "Undefined";
86 static const char *const HASH_CATEGORY_RAW_HASH_STR = "Raw Hash";
87 static const char *const HASH_CATEGORY_RAW_HASH_SALTED_STR = "Raw Hash salted and/or iterated";
88 static const char *const HASH_CATEGORY_RAW_HASH_AUTHENTICATED_STR = "Raw Hash authenticated";
89 static const char *const HASH_CATEGORY_RAW_CIPHER_KPA_STR = "Raw Cipher, Known-plaintext attack";
90 static const char *const HASH_CATEGORY_GENERIC_KDF_STR = "Generic KDF";
91 static const char *const HASH_CATEGORY_NETWORK_PROTOCOL_STR = "Network Protocol";
92 static const char *const HASH_CATEGORY_FORUM_SOFTWARE_STR = "Forums, CMS, E-Commerce";
93 static const char *const HASH_CATEGORY_DATABASE_SERVER_STR = "Database Server";
94 static const char *const HASH_CATEGORY_NETWORK_SERVER_STR = "FTP, HTTP, SMTP, LDAP Server";
95 static const char *const HASH_CATEGORY_RAW_CHECKSUM_STR = "Raw Checksum";
96 static const char *const HASH_CATEGORY_OS_STR = "Operating System";
97 static const char *const HASH_CATEGORY_EAS_STR = "Enterprise Application Software (EAS)";
98 static const char *const HASH_CATEGORY_ARCHIVE_STR = "Archive";
99 static const char *const HASH_CATEGORY_FDE_STR = "Full-Disk Encryption (FDE)";
100 static const char *const HASH_CATEGORY_FBE_STR = "File-Based Encryption (FBE)";
101 static const char *const HASH_CATEGORY_DOCUMENTS_STR = "Document";
102 static const char *const HASH_CATEGORY_PASSWORD_MANAGER_STR = "Password Manager";
103 static const char *const HASH_CATEGORY_OTP_STR = "One-Time Password";
104 static const char *const HASH_CATEGORY_PLAIN_STR = "Plaintext";
105 static const char *const HASH_CATEGORY_FRAMEWORK_STR = "Framework";
106 static const char *const HASH_CATEGORY_PRIVATE_KEY_STR = "Private Key";
107 static const char *const HASH_CATEGORY_IMS_STR = "Instant Messaging Service";
108 static const char *const HASH_CATEGORY_CRYPTOCURRENCY_WALLET_STR = "Cryptocurrency Wallet";
109
sort_by_string_sized(const void * p1,const void * p2)110 int sort_by_string_sized (const void *p1, const void *p2)
111 {
112 string_sized_t *s1 = (string_sized_t *) p1;
113 string_sized_t *s2 = (string_sized_t *) p2;
114
115 const int d = s1->len - s2->len;
116
117 if (d != 0) return d;
118
119 return memcmp (s1->buf, s2->buf, s1->len);
120 }
121
sort_by_stringptr(const void * p1,const void * p2)122 int sort_by_stringptr (const void *p1, const void *p2)
123 {
124 const char* const *s1 = (const char* const *) p1;
125 const char* const *s2 = (const char* const *) p2;
126
127 return strcmp (*s1, *s2);
128 }
129
get_msb32(const u32 v)130 static inline int get_msb32 (const u32 v)
131 {
132 int i;
133
134 for (i = 32; i > 0; i--) if ((v >> (i - 1)) & 1) break;
135
136 return i;
137 }
138
get_msb64(const u64 v)139 static inline int get_msb64 (const u64 v)
140 {
141 int i;
142
143 for (i = 64; i > 0; i--) if ((v >> (i - 1)) & 1) break;
144
145 return i;
146 }
147
overflow_check_u32_add(const u32 a,const u32 b)148 bool overflow_check_u32_add (const u32 a, const u32 b)
149 {
150 const int a_msb = get_msb32 (a);
151 const int b_msb = get_msb32 (b);
152
153 return ((a_msb < 32) && (b_msb < 32));
154 }
155
overflow_check_u32_mul(const u32 a,const u32 b)156 bool overflow_check_u32_mul (const u32 a, const u32 b)
157 {
158 const int a_msb = get_msb32 (a);
159 const int b_msb = get_msb32 (b);
160
161 return ((a_msb + b_msb) < 32);
162 }
163
overflow_check_u64_add(const u64 a,const u64 b)164 bool overflow_check_u64_add (const u64 a, const u64 b)
165 {
166 const int a_msb = get_msb64 (a);
167 const int b_msb = get_msb64 (b);
168
169 return ((a_msb < 64) && (b_msb < 64));
170 }
171
overflow_check_u64_mul(const u64 a,const u64 b)172 bool overflow_check_u64_mul (const u64 a, const u64 b)
173 {
174 const int a_msb = get_msb64 (a);
175 const int b_msb = get_msb64 (b);
176
177 return ((a_msb + b_msb) < 64);
178 }
179
is_power_of_2(const u32 v)180 bool is_power_of_2 (const u32 v)
181 {
182 return (v && !(v & (v - 1)));
183 }
184
mydivc32(const u32 dividend,const u32 divisor)185 u32 mydivc32 (const u32 dividend, const u32 divisor)
186 {
187 u32 quotient = dividend / divisor;
188
189 if (dividend % divisor) quotient++;
190
191 return quotient;
192 }
193
mydivc64(const u64 dividend,const u64 divisor)194 u64 mydivc64 (const u64 dividend, const u64 divisor)
195 {
196 u64 quotient = dividend / divisor;
197
198 if (dividend % divisor) quotient++;
199
200 return quotient;
201 }
202
filename_from_filepath(char * filepath)203 char *filename_from_filepath (char *filepath)
204 {
205 char *ptr = NULL;
206
207 if ((ptr = strrchr (filepath, '/')) != NULL)
208 {
209 ptr++;
210 }
211 else if ((ptr = strrchr (filepath, '\\')) != NULL)
212 {
213 ptr++;
214 }
215 else
216 {
217 ptr = filepath;
218 }
219
220 return ptr;
221 }
222
naive_replace(char * s,const char key_char,const char replace_char)223 void naive_replace (char *s, const char key_char, const char replace_char)
224 {
225 const size_t len = strlen (s);
226
227 for (size_t in = 0; in < len; in++)
228 {
229 const char c = s[in];
230
231 if (c == key_char)
232 {
233 s[in] = replace_char;
234 }
235 }
236 }
237
naive_escape(char * s,size_t s_max,const char key_char,const char escape_char)238 void naive_escape (char *s, size_t s_max, const char key_char, const char escape_char)
239 {
240 char s_escaped[1024] = { 0 };
241
242 size_t s_escaped_max = sizeof (s_escaped);
243
244 const size_t len = strlen (s);
245
246 for (size_t in = 0, out = 0; in < len; in++, out++)
247 {
248 const char c = s[in];
249
250 if (c == key_char)
251 {
252 s_escaped[out] = escape_char;
253
254 out++;
255 }
256
257 if (out == s_escaped_max - 2) break;
258
259 s_escaped[out] = c;
260 }
261
262 strncpy (s, s_escaped, s_max - 1);
263 }
264
hc_asprintf(char ** strp,const char * fmt,...)265 int hc_asprintf (char **strp, const char *fmt, ...)
266 {
267 va_list args;
268 va_start (args, fmt);
269 int rc = vasprintf (strp, fmt, args);
270 va_end (args);
271 return rc;
272 }
273
274 #if defined (_WIN)
275 #define __WINDOWS__
276 #endif
277 #include "sort_r.h"
278 #if defined (_WIN)
279 #undef __WINDOWS__
280 #endif
281
hc_qsort_r(void * base,size_t nmemb,size_t size,int (* compar)(const void *,const void *,void *),void * arg)282 void hc_qsort_r (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg)
283 {
284 sort_r (base, nmemb, size, compar, arg);
285 }
286
hc_bsearch_r(const void * key,const void * base,size_t nmemb,size_t size,int (* compar)(const void *,const void *,void *),void * arg)287 void *hc_bsearch_r (const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg)
288 {
289 for (size_t l = 0, r = nmemb; r; r >>= 1)
290 {
291 const size_t m = r >> 1;
292
293 const size_t c = l + m;
294
295 const char *next = (const char *) base + (c * size);
296
297 const int cmp = (*compar) (key, next, arg);
298
299 if (cmp > 0)
300 {
301 l += m + 1;
302
303 r--;
304 }
305
306 if (cmp == 0) return ((void *) next);
307 }
308
309 return (NULL);
310 }
311
hc_path_is_file(const char * path)312 bool hc_path_is_file (const char *path)
313 {
314 struct stat s;
315
316 memset (&s, 0, sizeof (s));
317
318 if (stat (path, &s) == -1) return false;
319
320 if (S_ISREG (s.st_mode)) return true;
321
322 return false;
323 }
324
hc_path_is_directory(const char * path)325 bool hc_path_is_directory (const char *path)
326 {
327 struct stat s;
328
329 memset (&s, 0, sizeof (s));
330
331 if (stat (path, &s) == -1) return false;
332
333 if (S_ISDIR (s.st_mode)) return true;
334
335 return false;
336 }
337
hc_path_is_empty(const char * path)338 bool hc_path_is_empty (const char *path)
339 {
340 struct stat s;
341
342 memset (&s, 0, sizeof (s));
343
344 if (stat (path, &s) == -1) return false;
345
346 if (s.st_size == 0) return true;
347
348 return false;
349 }
350
hc_path_exist(const char * path)351 bool hc_path_exist (const char *path)
352 {
353 if (access (path, F_OK) == -1) return false;
354
355 return true;
356 }
357
hc_path_read(const char * path)358 bool hc_path_read (const char *path)
359 {
360 if (access (path, R_OK) == -1) return false;
361
362 return true;
363 }
364
hc_path_write(const char * path)365 bool hc_path_write (const char *path)
366 {
367 if (access (path, W_OK) == -1) return false;
368
369 return true;
370 }
371
hc_path_create(const char * path)372 bool hc_path_create (const char *path)
373 {
374 if (hc_path_exist (path) == true) return false;
375
376 #ifdef O_CLOEXEC
377 const int fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRUSR | S_IWUSR);
378 #else
379 const int fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
380 #endif
381
382 if (fd == -1) return false;
383
384 close (fd);
385
386 unlink (path);
387
388 return true;
389 }
390
hc_path_has_bom(const char * path)391 bool hc_path_has_bom (const char *path)
392 {
393 u8 buf[8] = { 0 };
394
395 HCFILE fp;
396
397 if (hc_fopen_raw (&fp, path, "rb") == false) return false;
398
399 const size_t nread = hc_fread (buf, 1, sizeof (buf), &fp);
400
401 hc_fclose (&fp);
402
403 if (nread < 1) return false;
404
405 const int bom_size = hc_string_bom_size (buf);
406
407 const bool has_bom = bom_size > 0;
408
409 return has_bom;
410 }
411
hc_string_bom_size(const u8 * s)412 int hc_string_bom_size (const u8 *s)
413 {
414 /* signatures from https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding */
415
416 // utf-8
417
418 if ((s[0] == 0xef)
419 && (s[1] == 0xbb)
420 && (s[2] == 0xbf)) return 3;
421
422 // utf-16
423
424 if ((s[0] == 0xfe)
425 && (s[1] == 0xff)) return 2;
426
427 if ((s[0] == 0xff)
428 && (s[1] == 0xfe)) return 2;
429
430 // utf-32
431
432 if ((s[0] == 0x00)
433 && (s[1] == 0x00)
434 && (s[2] == 0xfe)
435 && (s[3] == 0xff)) return 4;
436
437 if ((s[0] == 0xff)
438 && (s[1] == 0xfe)
439 && (s[2] == 0x00)
440 && (s[3] == 0x00)) return 4;
441
442 // utf-7
443
444 if ((s[0] == 0x2b)
445 && (s[1] == 0x2f)
446 && (s[2] == 0x76)
447 && (s[3] == 0x38)) return 4;
448
449 if ((s[0] == 0x2b)
450 && (s[1] == 0x2f)
451 && (s[2] == 0x76)
452 && (s[3] == 0x39)) return 4;
453
454 if ((s[0] == 0x2b)
455 && (s[1] == 0x2f)
456 && (s[2] == 0x76)
457 && (s[3] == 0x2b)) return 4;
458
459 if ((s[0] == 0x2b)
460 && (s[1] == 0x2f)
461 && (s[2] == 0x76)
462 && (s[3] == 0x2f)) return 4;
463
464 if ((s[0] == 0x2b)
465 && (s[1] == 0x2f)
466 && (s[2] == 0x76)
467 && (s[3] == 0x38)
468 && (s[4] == 0x2d)) return 5;
469
470 // utf-1
471
472 if ((s[0] == 0xf7)
473 && (s[1] == 0x64)
474 && (s[2] == 0x4c)) return 3;
475
476 // utf-ebcdic
477
478 if ((s[0] == 0xdd)
479 && (s[1] == 0x73)
480 && (s[2] == 0x66)
481 && (s[3] == 0x73)) return 4;
482
483 // scsu
484
485 if ((s[0] == 0x0e)
486 && (s[1] == 0xfe)
487 && (s[2] == 0xff)) return 3;
488
489 // bocu-1
490
491 if ((s[0] == 0xfb)
492 && (s[1] == 0xee)
493 && (s[2] == 0x28)) return 3;
494
495 // gb-18030
496
497 if ((s[0] == 0x84)
498 && (s[1] == 0x31)
499 && (s[2] == 0x95)
500 && (s[3] == 0x33)) return 4;
501
502 return 0;
503 }
504
hc_string_is_digit(const char * s)505 bool hc_string_is_digit (const char *s)
506 {
507 if (s == NULL) return false;
508
509 const size_t len = strlen (s);
510
511 if (len == 0) return false;
512
513 for (size_t i = 0; i < len; i++)
514 {
515 const int c = (const int) s[i];
516
517 if (isdigit (c) == 0) return false;
518 }
519
520 return true;
521 }
522
setup_environment_variables(const folder_config_t * folder_config)523 void setup_environment_variables (const folder_config_t *folder_config)
524 {
525 char *compute = getenv ("COMPUTE");
526
527 if (compute)
528 {
529 char *display;
530
531 hc_asprintf (&display, "DISPLAY=%s", compute);
532
533 putenv (display);
534
535 hcfree (display);
536 }
537 else
538 {
539 if (getenv ("DISPLAY") == NULL)
540 putenv ((char *) "DISPLAY=:0");
541 }
542
543 #if defined (DEBUG)
544 if (getenv ("OCL_CODE_CACHE_ENABLE") == NULL)
545 putenv ((char *) "OCL_CODE_CACHE_ENABLE=0");
546
547 if (getenv ("CUDA_CACHE_DISABLE") == NULL)
548 putenv ((char *) "CUDA_CACHE_DISABLE=1");
549
550 if (getenv ("POCL_KERNEL_CACHE") == NULL)
551 putenv ((char *) "POCL_KERNEL_CACHE=0");
552 #endif
553
554 if (getenv ("TMPDIR") == NULL)
555 {
556 char *tmpdir = NULL;
557
558 hc_asprintf (&tmpdir, "TMPDIR=%s", folder_config->profile_dir);
559
560 putenv (tmpdir);
561
562 // we can't free tmpdir at this point!
563 }
564
565 /*
566 if (getenv ("CL_CONFIG_USE_VECTORIZER") == NULL)
567 putenv ((char *) "CL_CONFIG_USE_VECTORIZER=False");
568 */
569
570 #if defined (__CYGWIN__)
571 cygwin_internal (CW_SYNC_WINENV);
572 #endif
573 }
574
setup_umask()575 void setup_umask ()
576 {
577 umask (077);
578 }
579
setup_seeding(const bool rp_gen_seed_chgd,const u32 rp_gen_seed)580 void setup_seeding (const bool rp_gen_seed_chgd, const u32 rp_gen_seed)
581 {
582 if (rp_gen_seed_chgd == true)
583 {
584 srand (rp_gen_seed);
585 }
586 else
587 {
588 const time_t ts = time (NULL); // don't tell me that this is an insecure seed
589
590 srand ((unsigned int) ts);
591 }
592 }
593
get_random_num(const u32 min,const u32 max)594 u32 get_random_num (const u32 min, const u32 max)
595 {
596 if (min == max) return (min);
597
598 const u32 low = max - min;
599
600 if (low == 0) return (0);
601
602 #if defined (_WIN)
603
604 return (((u32) rand () % (max - min)) + min);
605
606 #else
607
608 return (((u32) random () % (max - min)) + min);
609
610 #endif
611 }
612
hc_string_trim_leading(char * s)613 void hc_string_trim_leading (char *s)
614 {
615 int skip = 0;
616
617 const int len = (int) strlen (s);
618
619 for (int i = 0; i < len; i++)
620 {
621 const int c = (const int) s[i];
622
623 if (isspace (c) == 0) break;
624
625 skip++;
626 }
627
628 if (skip == 0) return;
629
630 const int new_len = len - skip;
631
632 memmove (s, s + skip, new_len);
633
634 s[new_len] = 0;
635 }
636
hc_string_trim_trailing(char * s)637 void hc_string_trim_trailing (char *s)
638 {
639 int skip = 0;
640
641 const int len = (int) strlen (s);
642
643 for (int i = len - 1; i >= 0; i--)
644 {
645 const int c = (const int) s[i];
646
647 if (isspace (c) == 0) break;
648
649 skip++;
650 }
651
652 if (skip == 0) return;
653
654 const size_t new_len = len - skip;
655
656 s[new_len] = 0;
657 }
658
hc_get_processor_count()659 int hc_get_processor_count ()
660 {
661 int cnt = 0;
662
663 #if defined (_WIN)
664
665 SYSTEM_INFO info;
666
667 GetSystemInfo (&info);
668
669 cnt = (int) info.dwNumberOfProcessors;
670
671 #else
672
673 cnt = (int) sysconf (_SC_NPROCESSORS_ONLN);
674
675 #endif
676
677 return cnt;
678 }
679
hc_same_files(char * file1,char * file2)680 bool hc_same_files (char *file1, char *file2)
681 {
682 if ((file1 != NULL) && (file2 != NULL))
683 {
684 struct stat tmpstat_file1;
685 struct stat tmpstat_file2;
686
687 memset (&tmpstat_file1, 0, sizeof (tmpstat_file1));
688 memset (&tmpstat_file2, 0, sizeof (tmpstat_file2));
689
690 int do_check = 0;
691
692 HCFILE fp;
693
694 if (hc_fopen (&fp, file1, "r") == true)
695 {
696 if (hc_fstat (&fp, &tmpstat_file1))
697 {
698 hc_fclose (&fp);
699
700 return false;
701 }
702
703 hc_fclose (&fp);
704
705 do_check++;
706 }
707
708 if (hc_fopen (&fp, file2, "r") == true)
709 {
710 if (hc_fstat (&fp, &tmpstat_file2))
711 {
712 hc_fclose (&fp);
713
714 return false;
715 }
716
717 hc_fclose (&fp);
718
719 do_check++;
720 }
721
722 if (do_check == 2)
723 {
724 tmpstat_file1.st_mode = 0;
725 tmpstat_file1.st_nlink = 0;
726 tmpstat_file1.st_uid = 0;
727 tmpstat_file1.st_gid = 0;
728 tmpstat_file1.st_rdev = 0;
729 tmpstat_file1.st_atime = 0;
730
731 #if defined (STAT_NANOSECONDS_ACCESS_TIME)
732 tmpstat_file1.STAT_NANOSECONDS_ACCESS_TIME = 0;
733 #endif
734
735 #if defined (_POSIX)
736 tmpstat_file1.st_blksize = 0;
737 tmpstat_file1.st_blocks = 0;
738 #endif
739
740 tmpstat_file2.st_mode = 0;
741 tmpstat_file2.st_nlink = 0;
742 tmpstat_file2.st_uid = 0;
743 tmpstat_file2.st_gid = 0;
744 tmpstat_file2.st_rdev = 0;
745 tmpstat_file2.st_atime = 0;
746
747 #if defined (STAT_NANOSECONDS_ACCESS_TIME)
748 tmpstat_file2.STAT_NANOSECONDS_ACCESS_TIME = 0;
749 #endif
750
751 #if defined (_POSIX)
752 tmpstat_file2.st_blksize = 0;
753 tmpstat_file2.st_blocks = 0;
754 #endif
755
756 if (memcmp (&tmpstat_file1, &tmpstat_file2, sizeof (struct stat)) == 0)
757 {
758 return true;
759 }
760 }
761 }
762
763 return false;
764 }
765
hc_strtoul(const char * nptr,char ** endptr,int base)766 u32 hc_strtoul (const char *nptr, char **endptr, int base)
767 {
768 return (u32) strtoul (nptr, endptr, base);
769 }
770
hc_strtoull(const char * nptr,char ** endptr,int base)771 u64 hc_strtoull (const char *nptr, char **endptr, int base)
772 {
773 return (u64) strtoull (nptr, endptr, base);
774 }
775
power_of_two_ceil_32(const u32 v)776 u32 power_of_two_ceil_32 (const u32 v)
777 {
778 u32 r = v;
779
780 r--;
781
782 r |= r >> 1;
783 r |= r >> 2;
784 r |= r >> 4;
785 r |= r >> 8;
786 r |= r >> 16;
787
788 r++;
789
790 return r;
791 }
792
power_of_two_floor_32(const u32 v)793 u32 power_of_two_floor_32 (const u32 v)
794 {
795 u32 r = power_of_two_ceil_32 (v);
796
797 if (r > v)
798 {
799 r >>= 1;
800 }
801
802 return r;
803 }
804
round_up_multiple_32(const u32 v,const u32 m)805 u32 round_up_multiple_32 (const u32 v, const u32 m)
806 {
807 if (m == 0) return v;
808
809 const u32 r = v % m;
810
811 if (r == 0) return v;
812
813 return v + m - r;
814 }
815
round_up_multiple_64(const u64 v,const u64 m)816 u64 round_up_multiple_64 (const u64 v, const u64 m)
817 {
818 if (m == 0) return v;
819
820 const u64 r = v % m;
821
822 if (r == 0) return v;
823
824 return v + m - r;
825 }
826
827 // difference to original strncat is no returncode and u8* instead of char*
828
hc_strncat(u8 * dst,const u8 * src,const size_t n)829 void hc_strncat (u8 *dst, const u8 *src, const size_t n)
830 {
831 const size_t dst_len = strlen ((char *) dst);
832
833 const u8 *src_ptr = src;
834
835 u8 *dst_ptr = dst + dst_len;
836
837 for (size_t i = 0; i < n && *src_ptr != 0; i++)
838 {
839 *dst_ptr++ = *src_ptr++;
840 }
841
842 *dst_ptr = 0;
843 }
844
count_char(const u8 * buf,const int len,const u8 c)845 int count_char (const u8 *buf, const int len, const u8 c)
846 {
847 int r = 0;
848
849 for (int i = 0; i < len; i++)
850 {
851 if (buf[i] == c) r++;
852 }
853
854 return r;
855 }
856
get_entropy(const u8 * buf,const int len)857 float get_entropy (const u8 *buf, const int len)
858 {
859 float entropy = 0.0;
860
861 for (int c = 0; c < 256; c++)
862 {
863 const int r = count_char (buf, len, (const u8) c);
864
865 if (r == 0) continue;
866
867 float w = (float) r / len;
868
869 entropy += -w * log2f (w);
870 }
871
872 return entropy;
873 }
874
select_read_timeout(int sockfd,const int sec)875 int select_read_timeout (int sockfd, const int sec)
876 {
877 struct timeval tv;
878
879 tv.tv_sec = sec;
880 tv.tv_usec = 0;
881
882 fd_set fds;
883
884 FD_ZERO (&fds);
885 FD_SET (sockfd, &fds);
886
887 return select (sockfd + 1, &fds, NULL, NULL, &tv);
888 }
889
select_write_timeout(int sockfd,const int sec)890 int select_write_timeout (int sockfd, const int sec)
891 {
892 struct timeval tv;
893
894 tv.tv_sec = sec;
895 tv.tv_usec = 0;
896
897 fd_set fds;
898
899 FD_ZERO (&fds);
900 FD_SET (sockfd, &fds);
901
902 return select (sockfd + 1, NULL, &fds, NULL, &tv);
903 }
904
905 #if defined (_WIN)
906
select_read_timeout_console(const int sec)907 int select_read_timeout_console (const int sec)
908 {
909 const HANDLE hStdIn = GetStdHandle (STD_INPUT_HANDLE);
910
911 const DWORD rc = WaitForSingleObject (hStdIn, sec * 1000);
912
913 if (rc == WAIT_OBJECT_0)
914 {
915 DWORD dwRead;
916
917 INPUT_RECORD inRecords;
918
919 inRecords.EventType = 0;
920
921 PeekConsoleInput (hStdIn, &inRecords, 1, &dwRead);
922
923 if (inRecords.EventType == 0)
924 {
925 // those are good ones
926
927 return 1;
928 }
929 else
930 {
931 // but we don't want that stuff like windows focus etc. in our stream
932
933 ReadConsoleInput (hStdIn, &inRecords, 1, &dwRead);
934 }
935
936 return select_read_timeout_console (sec);
937 }
938 else if (rc == WAIT_TIMEOUT)
939 {
940 return 0;
941 }
942
943 return -1;
944 }
945
946 #else
947
select_read_timeout_console(const int sec)948 int select_read_timeout_console (const int sec)
949 {
950 return select_read_timeout (fileno (stdin), sec);
951 }
952
953 #endif
954
strhashcategory(const u32 hash_category)955 const char *strhashcategory (const u32 hash_category)
956 {
957 switch (hash_category)
958 {
959 case HASH_CATEGORY_UNDEFINED: return HASH_CATEGORY_UNDEFINED_STR;
960 case HASH_CATEGORY_RAW_HASH: return HASH_CATEGORY_RAW_HASH_STR;
961 case HASH_CATEGORY_RAW_HASH_SALTED: return HASH_CATEGORY_RAW_HASH_SALTED_STR;
962 case HASH_CATEGORY_RAW_HASH_AUTHENTICATED: return HASH_CATEGORY_RAW_HASH_AUTHENTICATED_STR;
963 case HASH_CATEGORY_RAW_CIPHER_KPA: return HASH_CATEGORY_RAW_CIPHER_KPA_STR;
964 case HASH_CATEGORY_GENERIC_KDF: return HASH_CATEGORY_GENERIC_KDF_STR;
965 case HASH_CATEGORY_NETWORK_PROTOCOL: return HASH_CATEGORY_NETWORK_PROTOCOL_STR;
966 case HASH_CATEGORY_FORUM_SOFTWARE: return HASH_CATEGORY_FORUM_SOFTWARE_STR;
967 case HASH_CATEGORY_DATABASE_SERVER: return HASH_CATEGORY_DATABASE_SERVER_STR;
968 case HASH_CATEGORY_NETWORK_SERVER: return HASH_CATEGORY_NETWORK_SERVER_STR;
969 case HASH_CATEGORY_RAW_CHECKSUM: return HASH_CATEGORY_RAW_CHECKSUM_STR;
970 case HASH_CATEGORY_OS: return HASH_CATEGORY_OS_STR;
971 case HASH_CATEGORY_EAS: return HASH_CATEGORY_EAS_STR;
972 case HASH_CATEGORY_ARCHIVE: return HASH_CATEGORY_ARCHIVE_STR;
973 case HASH_CATEGORY_FDE: return HASH_CATEGORY_FDE_STR;
974 case HASH_CATEGORY_FBE: return HASH_CATEGORY_FBE_STR;
975 case HASH_CATEGORY_DOCUMENTS: return HASH_CATEGORY_DOCUMENTS_STR;
976 case HASH_CATEGORY_PASSWORD_MANAGER: return HASH_CATEGORY_PASSWORD_MANAGER_STR;
977 case HASH_CATEGORY_OTP: return HASH_CATEGORY_OTP_STR;
978 case HASH_CATEGORY_PLAIN: return HASH_CATEGORY_PLAIN_STR;
979 case HASH_CATEGORY_FRAMEWORK: return HASH_CATEGORY_FRAMEWORK_STR;
980 case HASH_CATEGORY_PRIVATE_KEY: return HASH_CATEGORY_PRIVATE_KEY_STR;
981 case HASH_CATEGORY_IMS: return HASH_CATEGORY_IMS_STR;
982 case HASH_CATEGORY_CRYPTOCURRENCY_WALLET: return HASH_CATEGORY_CRYPTOCURRENCY_WALLET_STR;
983 }
984
985 return NULL;
986 }
987
stroptitype(const u32 opti_type)988 const char *stroptitype (const u32 opti_type)
989 {
990 switch (opti_type)
991 {
992 case OPTI_TYPE_OPTIMIZED_KERNEL: return OPTI_STR_OPTIMIZED_KERNEL;
993 case OPTI_TYPE_ZERO_BYTE: return OPTI_STR_ZERO_BYTE;
994 case OPTI_TYPE_PRECOMPUTE_INIT: return OPTI_STR_PRECOMPUTE_INIT;
995 case OPTI_TYPE_MEET_IN_MIDDLE: return OPTI_STR_MEET_IN_MIDDLE;
996 case OPTI_TYPE_EARLY_SKIP: return OPTI_STR_EARLY_SKIP;
997 case OPTI_TYPE_NOT_SALTED: return OPTI_STR_NOT_SALTED;
998 case OPTI_TYPE_NOT_ITERATED: return OPTI_STR_NOT_ITERATED;
999 case OPTI_TYPE_PREPENDED_SALT: return OPTI_STR_PREPENDED_SALT;
1000 case OPTI_TYPE_APPENDED_SALT: return OPTI_STR_APPENDED_SALT;
1001 case OPTI_TYPE_SINGLE_HASH: return OPTI_STR_SINGLE_HASH;
1002 case OPTI_TYPE_SINGLE_SALT: return OPTI_STR_SINGLE_SALT;
1003 case OPTI_TYPE_BRUTE_FORCE: return OPTI_STR_BRUTE_FORCE;
1004 case OPTI_TYPE_RAW_HASH: return OPTI_STR_RAW_HASH;
1005 case OPTI_TYPE_SLOW_HASH_SIMD_INIT: return OPTI_STR_SLOW_HASH_SIMD_INIT;
1006 case OPTI_TYPE_SLOW_HASH_SIMD_LOOP: return OPTI_STR_SLOW_HASH_SIMD_LOOP;
1007 case OPTI_TYPE_SLOW_HASH_SIMD_COMP: return OPTI_STR_SLOW_HASH_SIMD_COMP;
1008 case OPTI_TYPE_USES_BITS_8: return OPTI_STR_USES_BITS_8;
1009 case OPTI_TYPE_USES_BITS_16: return OPTI_STR_USES_BITS_16;
1010 case OPTI_TYPE_USES_BITS_32: return OPTI_STR_USES_BITS_32;
1011 case OPTI_TYPE_USES_BITS_64: return OPTI_STR_USES_BITS_64;
1012 }
1013
1014 return NULL;
1015 }
1016
strparser(const u32 parser_status)1017 const char *strparser (const u32 parser_status)
1018 {
1019 switch (parser_status)
1020 {
1021 case PARSER_OK: return PA_000;
1022 case PARSER_COMMENT: return PA_001;
1023 case PARSER_GLOBAL_ZERO: return PA_002;
1024 case PARSER_GLOBAL_LENGTH: return PA_003;
1025 case PARSER_HASH_LENGTH: return PA_004;
1026 case PARSER_HASH_VALUE: return PA_005;
1027 case PARSER_SALT_LENGTH: return PA_006;
1028 case PARSER_SALT_VALUE: return PA_007;
1029 case PARSER_SALT_ITERATION: return PA_008;
1030 case PARSER_SEPARATOR_UNMATCHED: return PA_009;
1031 case PARSER_SIGNATURE_UNMATCHED: return PA_010;
1032 case PARSER_HCCAPX_FILE_SIZE: return PA_011;
1033 case PARSER_HCCAPX_EAPOL_LEN: return PA_012;
1034 case PARSER_PSAFE2_FILE_SIZE: return PA_013;
1035 case PARSER_PSAFE3_FILE_SIZE: return PA_014;
1036 case PARSER_TC_FILE_SIZE: return PA_015;
1037 case PARSER_VC_FILE_SIZE: return PA_016;
1038 case PARSER_SIP_AUTH_DIRECTIVE: return PA_017;
1039 case PARSER_HASH_FILE: return PA_018;
1040 case PARSER_HASH_ENCODING: return PA_019;
1041 case PARSER_SALT_ENCODING: return PA_020;
1042 case PARSER_LUKS_FILE_SIZE: return PA_021;
1043 case PARSER_LUKS_MAGIC: return PA_022;
1044 case PARSER_LUKS_VERSION: return PA_023;
1045 case PARSER_LUKS_CIPHER_TYPE: return PA_024;
1046 case PARSER_LUKS_CIPHER_MODE: return PA_025;
1047 case PARSER_LUKS_HASH_TYPE: return PA_026;
1048 case PARSER_LUKS_KEY_SIZE: return PA_027;
1049 case PARSER_LUKS_KEY_DISABLED: return PA_028;
1050 case PARSER_LUKS_KEY_STRIPES: return PA_029;
1051 case PARSER_LUKS_HASH_CIPHER: return PA_030;
1052 case PARSER_HCCAPX_SIGNATURE: return PA_031;
1053 case PARSER_HCCAPX_VERSION: return PA_032;
1054 case PARSER_HCCAPX_MESSAGE_PAIR: return PA_033;
1055 case PARSER_TOKEN_ENCODING: return PA_034;
1056 case PARSER_TOKEN_LENGTH: return PA_035;
1057 case PARSER_INSUFFICIENT_ENTROPY: return PA_036;
1058 case PARSER_PKZIP_CT_UNMATCHED: return PA_037;
1059 case PARSER_KEY_SIZE: return PA_038;
1060 case PARSER_BLOCK_SIZE: return PA_039;
1061 case PARSER_CIPHER: return PA_040;
1062 case PARSER_FILE_SIZE: return PA_041;
1063 case PARSER_IV_LENGTH: return PA_042;
1064 case PARSER_CT_LENGTH: return PA_043;
1065 }
1066
1067 return PA_255;
1068 }
1069
rounds_count_length(const char * input_buf,const int input_len)1070 static int rounds_count_length (const char *input_buf, const int input_len)
1071 {
1072 if (input_len >= 9) // 9 is minimum because of "rounds=X$"
1073 {
1074 static const char *const rounds = "rounds=";
1075
1076 if (memcmp (input_buf, rounds, 7) == 0)
1077 {
1078 const char *next_pos = strchr (input_buf + 8, '$');
1079
1080 if (next_pos == NULL) return -1;
1081
1082 const int rounds_len = next_pos - input_buf;
1083
1084 return rounds_len;
1085 }
1086 }
1087
1088 return -1;
1089 }
1090
hc_strchr_next(const u8 * input_buf,const int input_len,const u8 separator)1091 const u8 *hc_strchr_next (const u8 *input_buf, const int input_len, const u8 separator)
1092 {
1093 for (int i = 0; i < input_len; i++)
1094 {
1095 if (input_buf[i] == separator) return &input_buf[i];
1096 }
1097
1098 return NULL;
1099 }
1100
hc_strchr_last(const u8 * input_buf,const int input_len,const u8 separator)1101 const u8 *hc_strchr_last (const u8 *input_buf, const int input_len, const u8 separator)
1102 {
1103 for (int i = input_len - 1; i >= 0; i--)
1104 {
1105 if (input_buf[i] == separator) return &input_buf[i];
1106 }
1107
1108 return NULL;
1109 }
1110
input_tokenizer(const u8 * input_buf,const int input_len,token_t * token)1111 int input_tokenizer (const u8 *input_buf, const int input_len, token_t *token)
1112 {
1113 int len_left = input_len;
1114
1115 token->buf[0] = input_buf;
1116
1117 int token_idx;
1118
1119 for (token_idx = 0; token_idx < token->token_cnt - 1; token_idx++)
1120 {
1121 if (token->attr[token_idx] & TOKEN_ATTR_FIXED_LENGTH)
1122 {
1123 int len = token->len[token_idx];
1124
1125 if (len_left < len) return (PARSER_TOKEN_LENGTH);
1126
1127 token->buf[token_idx + 1] = token->buf[token_idx] + len;
1128
1129 len_left -= len;
1130 }
1131 else
1132 {
1133 if (token->attr[token_idx] & TOKEN_ATTR_OPTIONAL_ROUNDS)
1134 {
1135 const int len = rounds_count_length ((const char *) token->buf[token_idx], len_left);
1136
1137 token->opt_buf = token->buf[token_idx];
1138
1139 token->opt_len = len; // we want an eventual -1 in here, it's used later for verification
1140
1141 if (len > 0)
1142 {
1143 token->buf[token_idx] += len + 1; // +1 = separator
1144
1145 len_left -= len + 1; // +1 = separator
1146 }
1147 }
1148
1149 const u8 *next_pos = NULL;
1150
1151 if (token->attr[token_idx] & TOKEN_ATTR_SEPARATOR_FARTHEST)
1152 {
1153 next_pos = hc_strchr_last (token->buf[token_idx], len_left, token->sep[token_idx]);
1154 }
1155 else
1156 {
1157 next_pos = hc_strchr_next (token->buf[token_idx], len_left, token->sep[token_idx]);
1158 }
1159
1160 if (next_pos == NULL) return (PARSER_SEPARATOR_UNMATCHED);
1161
1162 const int len = next_pos - token->buf[token_idx];
1163
1164 token->len[token_idx] = len;
1165
1166 token->buf[token_idx + 1] = next_pos + 1; // +1 = separator
1167
1168 len_left -= len + 1; // +1 = separator
1169 }
1170 }
1171
1172 if (token->attr[token_idx] & TOKEN_ATTR_FIXED_LENGTH)
1173 {
1174 int len = token->len[token_idx];
1175
1176 if (len_left != len) return (PARSER_TOKEN_LENGTH);
1177 }
1178 else
1179 {
1180 token->len[token_idx] = len_left;
1181 }
1182
1183 // verify data
1184
1185 for (token_idx = 0; token_idx < token->token_cnt; token_idx++)
1186 {
1187 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_SIGNATURE)
1188 {
1189 bool matched = false;
1190
1191 for (int signature_idx = 0; signature_idx < token->signatures_cnt; signature_idx++)
1192 {
1193 if (strncmp ((char *) token->buf[token_idx], token->signatures_buf[signature_idx], token->len[token_idx]) == 0) matched = true;
1194 }
1195
1196 if (matched == false) return (PARSER_SIGNATURE_UNMATCHED);
1197 }
1198
1199 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_LENGTH)
1200 {
1201 if (token->len[token_idx] < token->len_min[token_idx]) return (PARSER_TOKEN_LENGTH);
1202 if (token->len[token_idx] > token->len_max[token_idx]) return (PARSER_TOKEN_LENGTH);
1203 }
1204
1205 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_DIGIT)
1206 {
1207 if (is_valid_digit_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING);
1208 }
1209
1210 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_FLOAT)
1211 {
1212 if (is_valid_float_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING);
1213 }
1214
1215 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_HEX)
1216 {
1217 if (is_valid_hex_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING);
1218 }
1219
1220 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_BASE64A)
1221 {
1222 if (is_valid_base64a_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING);
1223 }
1224
1225 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_BASE64B)
1226 {
1227 if (is_valid_base64b_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING);
1228 }
1229
1230 if (token->attr[token_idx] & TOKEN_ATTR_VERIFY_BASE64C)
1231 {
1232 if (is_valid_base64c_string (token->buf[token_idx], token->len[token_idx]) == false) return (PARSER_TOKEN_ENCODING);
1233 }
1234 }
1235
1236 return PARSER_OK;
1237 }
1238
generic_salt_decode(MAYBE_UNUSED const hashconfig_t * hashconfig,const u8 * in_buf,const int in_len,u8 * out_buf,int * out_len)1239 bool generic_salt_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, const u8 *in_buf, const int in_len, u8 *out_buf, int *out_len)
1240 {
1241 u32 tmp_u32[(64 * 2) + 1] = { 0 };
1242
1243 u8 *tmp_u8 = (u8 *) tmp_u32;
1244
1245 if (in_len > 512) return false; // 512 = 2 * 256 -- (2 * because of hex), 256 because of maximum salt length in salt_t
1246
1247 int tmp_len = 0;
1248
1249 if (hashconfig->opts_type & OPTS_TYPE_ST_HEX)
1250 {
1251 if (in_len < (int) (hashconfig->salt_min * 2)) return false;
1252 if (in_len > (int) (hashconfig->salt_max * 2)) return false;
1253
1254 if (in_len & 1) return false;
1255
1256 for (int i = 0, j = 0; j < in_len; i += 1, j += 2)
1257 {
1258 u8 p0 = in_buf[j + 0];
1259 u8 p1 = in_buf[j + 1];
1260
1261 tmp_u8[i] = hex_convert (p1) << 0;
1262 tmp_u8[i] |= hex_convert (p0) << 4;
1263 }
1264
1265 tmp_len = in_len / 2;
1266 }
1267 else if (hashconfig->opts_type & OPTS_TYPE_ST_BASE64)
1268 {
1269 if (in_len < (int) (((hashconfig->salt_min * 8) / 6) + 0)) return false;
1270 if (in_len > (int) (((hashconfig->salt_max * 8) / 6) + 3)) return false;
1271
1272 tmp_len = base64_decode (base64_to_int, in_buf, in_len, tmp_u8);
1273 }
1274 else
1275 {
1276 if (in_len < (int) hashconfig->salt_min) return false;
1277 if (in_len > (int) hashconfig->salt_max) return false;
1278
1279 memcpy (tmp_u8, in_buf, in_len);
1280
1281 tmp_len = in_len;
1282 }
1283
1284 if (hashconfig->opts_type & OPTS_TYPE_ST_UTF16LE)
1285 {
1286 if (tmp_len >= 128) return false;
1287
1288 for (int i = 64 - 1; i >= 1; i -= 2)
1289 {
1290 const u32 v = tmp_u32[i / 2];
1291
1292 tmp_u32[i - 0] = ((v >> 8) & 0x00FF0000) | ((v >> 16) & 0x000000FF);
1293 tmp_u32[i - 1] = ((v << 8) & 0x00FF0000) | ((v >> 0) & 0x000000FF);
1294 }
1295
1296 tmp_len = tmp_len * 2;
1297 }
1298
1299 if (hashconfig->opts_type & OPTS_TYPE_ST_LOWER)
1300 {
1301 lowercase (tmp_u8, tmp_len);
1302 }
1303
1304 if (hashconfig->opts_type & OPTS_TYPE_ST_UPPER)
1305 {
1306 uppercase (tmp_u8, tmp_len);
1307 }
1308
1309 int tmp2_len = tmp_len;
1310
1311 if (hashconfig->opts_type & OPTS_TYPE_ST_ADD80)
1312 {
1313 if (tmp2_len >= 256) return false;
1314
1315 tmp_u8[tmp2_len++] = 0x80;
1316 }
1317
1318 if (hashconfig->opts_type & OPTS_TYPE_ST_ADD01)
1319 {
1320 if (tmp2_len >= 256) return false;
1321
1322 tmp_u8[tmp2_len++] = 0x01;
1323 }
1324
1325 memcpy (out_buf, tmp_u8, tmp2_len);
1326
1327 *out_len = tmp_len;
1328
1329 return true;
1330 }
1331
generic_salt_encode(MAYBE_UNUSED const hashconfig_t * hashconfig,const u8 * in_buf,const int in_len,u8 * out_buf)1332 int generic_salt_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, const u8 *in_buf, const int in_len, u8 *out_buf)
1333 {
1334 u32 tmp_u32[(64 * 2) + 1] = { 0 };
1335
1336 u8 *tmp_u8 = (u8 *) tmp_u32;
1337
1338 memcpy (tmp_u8, in_buf, in_len);
1339
1340 int tmp_len = in_len;
1341
1342 if (hashconfig->opts_type & OPTS_TYPE_ST_UTF16LE)
1343 {
1344 for (int i = 0, j = 0; j < in_len; i += 1, j += 2)
1345 {
1346 const u8 p = tmp_u8[j];
1347
1348 tmp_u8[i] = p;
1349 }
1350
1351 tmp_len = tmp_len / 2;
1352 }
1353
1354 if (hashconfig->opts_type & OPTS_TYPE_ST_HEX)
1355 {
1356 for (int i = 0, j = 0; i < in_len; i += 1, j += 2)
1357 {
1358 u8_to_hex (in_buf[i], tmp_u8 + j);
1359 }
1360
1361 tmp_len = in_len * 2;
1362 }
1363 else if (hashconfig->opts_type & OPTS_TYPE_ST_BASE64)
1364 {
1365 tmp_len = base64_encode (int_to_base64, in_buf, in_len, tmp_u8);
1366 }
1367
1368 memcpy (out_buf, tmp_u8, tmp_len);
1369
1370 return tmp_len;
1371 }
1372