Debian Codegraph @

utils/eurephia_saltdecode.c

raw
1 /* saltdecode.c -- Decode a given password salt into a readable format
2 *
3 * GPLv2 only - Copyright (C) 2009 - 2012
4 * David Sommerseth <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; version 2
9 * of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
21
22 /**
23 * @file eurephia_saltdecode.c
24 * @author David Sommerseth <[email protected]>
25 * @date 2009-03-29
26 *
27 * @brief Simple utility which decodes a eurephia SHA512 hash salt
28 *
29 */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <sys/param.h>
35
36 #include <eurephia_nullsafe.h>
37 #include <get_console_input.h>
38
39 #define ROUNDS_MIN 1000 /**< Minimum hashing rounds to acknowledge */
40 #define ROUNDS_MAX 999999999 /**< Maximum hashing rounds to acknowledge */
41
42
43 /**
44 * Generates a phase2 salt value from a password.
45 *
46 * @param pwd Input hash
47 *
48 * @return Returns an int value containing the phase2 salt information
49 */
50 static unsigned int get_salt_p2(const char *pwd) {
51 int n = 0;
52 long int saltinfo_p2 = 0, t = 0;
53
54 for( n = 0; n < strlen_nullsafe(pwd); n++ ) {
55 t += pwd[n];
56 }
57
58 for( n = 0; n < 4; n++ ) {
59 saltinfo_p2 <<= 8;
60 saltinfo_p2 += (strlen_nullsafe(pwd) ^ (t % 0xff));
61 }
62 return saltinfo_p2;
63 }
64
65
66 /**
67 * This function will unpack the salt information and "unscramble" it with a given password
68 *
69 * @param insalt Input eurephia SHA512 salt string
70 * @param pwd Users password
71 *
72 * @return Returns the decoded salt information, containing hashing rounds and salt length.
73 */
74 unsigned int unpack_saltinfo(const char *insalt, const char *pwd) {
75 unsigned int in_salt_prefix = 0;
76
77 assert(insalt != NULL && pwd != NULL);
78
79 if( sscanf(insalt, "%08x", &in_salt_prefix) > -1 ) {
80 long int regen_p2 = in_salt_prefix ^ get_salt_p2(pwd);
81 return regen_p2 ^ 0xAAAAAAAA;
82 } else {
83 return -1;
84 }
85 }
86
87
88 /**
89 * saltdecode main function
90 *
91 * @param argc argument counter
92 * @param argv argument table
93 *
94 * @return Returns 0 on success, otherwise 1.
95 */
96 int main(int argc, char **argv) {
97 char key[258];
98 unsigned int saltinfo = 0;
99 size_t rounds = 0, salt_len = 0;
100
101 if( argc != 2 ) {
102 fprintf(stderr, "Usage: %s <password salt>\n", argv[0]);
103 return 1;
104 }
105
106 memset(&key, 0, 258);
107 printf("\nYou will need to give the correct password for this salt.\n");
108 printf("If you give the wrong password, the decoded information will be wrong.\n\n");
109 get_console_input(key, 256, "Password:", 1);
110 saltinfo = unpack_saltinfo(argv[1], key);
111 memset(&key, 0, 258);
112
113 salt_len = saltinfo & 0x000000ff;
114 rounds = MAX(ROUNDS_MIN, MIN(((saltinfo & 0xffffff00) >> 8), ROUNDS_MAX));
115
116 printf("\nSalt length: %ld\nHash rounds: %ld\n\n",
117 (unsigned long int) salt_len,
118 (unsigned long int) rounds);
119
120 return 0;
121 }