File: hash.c

package info (click to toggle)
cpu 1.4.3-12
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 2,612 kB
  • sloc: sh: 7,535; ansic: 7,392; makefile: 117
file content (426 lines) | stat: -rw-r--r-- 9,427 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
     This file is part of CPU
     (C) 2003 Blake Matheny (and other contributing authors)

     CPU is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     CPU is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with CPU; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * hasing routines
 * @author Blake Matheny
 * @file hash.c
 **/
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#include "util/hash.h"
#ifdef HAVE_CRACK_H
#include <crack.h>
#endif
#ifndef crypt
extern char *crypt(const char *key, const char *salt);
#endif

char salt[] = "$1$........";
char csalt[] = "........";
const char rstring[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR"
		       "STUVWXYZ~!@#$%^&*()_+{}|:\"<>?`-=[];',./";
const double rlen = 93.00;

const char * hashes[] = {
  "sha1",
  "ssha1",
  "md5",
  "smd5",
  "crypt",
  "md5crypt",
  "clear",
  NULL
};

char *
CPU_getpass ( const char * prompt )
{
  struct termios old, new;
  char * tmp_pass = NULL;
  int i = 0;

  fprintf(stdout, "%s", prompt);
  if ( tcgetattr (fileno (stdin), &old) != 0)
    return NULL;
  new = old;
  new.c_lflag &= ~ECHO;
  if (tcsetattr (fileno (stdin), TCSAFLUSH, &new) != 0)
    return NULL;
  tmp_pass = (char*)malloc(sizeof(char)*PASSWORD_SIZE);
  if ( tmp_pass == NULL )
    return NULL;
  memset(tmp_pass, 0, sizeof(char)*PASSWORD_SIZE);
  if ( fgets(tmp_pass, PASSWORD_SIZE, stdin) == NULL )
    return NULL;
  (void) tcsetattr (fileno (stdin), TCSAFLUSH, &old);
  for ( i = 0; i < (int)strlen(tmp_pass); ++i ) {
    if ( tmp_pass[i] == '\n' ) {
      tmp_pass[i] = '\0';
      break;
    }
  }
  printf("\n");
  return tmp_pass;
}

char *
genPass(int len)
{
  int n = 0;
  int j = 0;
  struct timeval tv;
  char * pass = NULL;

  if ( len < 1 )
    return NULL;

  pass = (char*)malloc(sizeof(char)*len);
  if ( pass == NULL )
    return NULL;
  bzero(pass, (sizeof(char)*len));

  while ( n < len )
    {
      gettimeofday(&tv, NULL);
      srand((unsigned int)tv.tv_usec);
      j = (int)(rlen*rand()/(RAND_MAX+1.0));
      pass[n] = rstring[j];
      n++;
    }
  return pass;
}

hash_t
getHashType(char * hashname)
{
  int i = 0;
  char * temph = NULL;
  hash_t hasht = H_UNKNOWN;

  temph = ctolower(hashname);

  for ( i = 0; hashes[i] != NULL; i++ )
    {
      if ( strcmp(temph, hashes[i]) == 0 )
	{
	  hasht = i;
	  break;
	}
    }
  return hasht;
}

char *
getHash(hash_t hasht, char * password, const char * prefix,
	const char * suffix)
{
  char * temp = NULL;
  char * passphrase = NULL;
  size_t plen = 0;
  char md5salt[32];

  if ( password == NULL )
    return NULL;

#ifdef HAVE_LIBCRACK
{
  char * msg = NULL;
  char * newpass = password;
  char * dict = NULL;
  dict = cfg_get_str("GLOBAL", "CRACKLIB_DICTIONARY");
  if ( dict != NULL )
  {
    if ( password[0] != '*' )
      while ( (msg = (char*)FascistCheck(newpass, dict)) != NULL )
      {
	fprintf(stdout, "Bad password: %s\n", msg);
	if (cfg_get_int("GLOBAL", "ALLOW_BADPASS")) {
		fprintf(stdout, "But allowing anyway since allow-badpass is enabled\n");
		break;
	}
	newpass = NULL;
	msg = NULL;
	while ( (newpass = CPU_getpass("Enter a new password: ")) == NULL )
	{}
      }
  }
}
#endif

  switch(hasht)
    {
      case H_SHA1:
	temp = sha1_hash(password);
	break;
      case H_SSHA1:
	temp = ssha1_hash(password);
	break;
      case H_MD5:
	temp = md5_hash(password);
	break;
      case H_SMD5:
	temp = smd5_hash(password);
	break;
      case H_CRYPT:
#ifdef HAVE_LIBCRYPT
	temp = crypt(password, cgetSalt());
#else
	fprintf(stderr, "Your c library is missing 'crypt'\n");
#endif
	break;
      case H_MD5CRYPT:
#ifdef HAVE_LIBCRYPT
	snprintf(md5salt, sizeof(md5salt),"$1$%s", cgetSalt());
	temp = crypt(password, md5salt);
#else
	fprintf(stderr, "Your c library is missing 'crypt'\n");
#endif
	break;
      case H_CLEAR:
	temp = password;
	break;
      default:
	fprintf(stderr, "getHash: Unknown hash type.\n");
	return NULL;
    }
  if ( temp == NULL )
    return NULL;

  plen = strlen(temp)+1;
  if ( prefix != NULL )
    plen += strlen(prefix);
  if ( suffix != NULL )
    plen += strlen(suffix);

  passphrase = (char*)malloc(sizeof(char)*plen);
  if ( passphrase == NULL )
    return NULL;
  bzero(passphrase, plen*sizeof(char));

  if ( prefix != NULL && suffix != NULL )
    {
      snprintf(passphrase, plen, "%s%s%s", prefix, temp, suffix);
    }
  else if ( prefix != NULL )
    {
      snprintf(passphrase, plen, "%s%s", prefix, temp);
    }
  else if ( suffix != NULL )
    {
      snprintf(passphrase, plen, "%s%s", temp, suffix);
    }
  else
    {
      snprintf(passphrase, plen, "%s", temp);
    }
  return passphrase;
}

char *
sha1_hash(char * password)
{
  unsigned char sha1digest[SHA1_DIGEST_BYTES];
  int b64_len = 0;
  char * b64digest = NULL;

  if ( password == NULL )
    return NULL;

  sha_buffer(password, strlen(password), sha1digest);

  b64_len = CEILING(SHA1_DIGEST_BYTES)*4+1;
  b64digest = (char*)malloc(sizeof(char)*b64_len);
  if ( b64digest == NULL )
    return NULL;
  bzero(b64digest, b64_len);

  if (base64_encode(sha1digest, sizeof(sha1digest), b64digest, b64_len) < 0)
    return NULL;

  return b64digest;
}

char *
ssha1_hash(char * password)
{
  unsigned char sha1digest[SHA1_DIGEST_BYTES];
  int b64_len = 0;
  int slen = 0;
  char * b64digest = NULL;
  char * temp = NULL;
  char * salt = getSalt();

  if ( password == NULL )
    return NULL;

  slen = strlen(password)+SALT_LEN;
  temp = (char*)malloc(slen*sizeof(char));
  if ( temp == NULL )
    return NULL;
  bzero(temp, (slen*sizeof(char)));
  snprintf(temp, slen, "%s%s", password, salt);

  sha_buffer(temp, strlen(temp), sha1digest);

  b64_len = CEILING(SHA1_DIGEST_BYTES+SALT_LEN)*4+1;
  b64digest = (char*)malloc(sizeof(char)*b64_len);
  if ( b64digest == NULL )
    return NULL;
  bzero(b64digest, b64_len);

  Free(temp);
  temp = (char *)malloc((sizeof(sha1digest)+SALT_LEN)*sizeof(char));
  if ( temp == NULL )
    return NULL;
  bzero(temp, ((sizeof(sha1digest)+SALT_LEN)*sizeof(char)));
  snprintf(temp, (sizeof(sha1digest)+SALT_LEN), "%s%s", sha1digest, salt);

  if (base64_encode(temp, sizeof(sha1digest)+SALT_LEN, b64digest, b64_len) < 0)
    return NULL;

  return b64digest;
}

char *
md5_hash(char * password)
{
  unsigned char md5digest[MD5_DIGEST_BYTES];
  int b64_len = 0;
  char * b64digest = NULL;

  if ( password == NULL )
    return NULL;

  md5_buffer(password, strlen(password), md5digest);

  b64_len = CEILING(MD5_DIGEST_BYTES)*4+1;
  b64digest = (char*)malloc(sizeof(char)*b64_len);
  if ( b64digest == NULL )
    return NULL;
  bzero(b64digest, b64_len);

  if (base64_encode(md5digest, sizeof(md5digest), b64digest, b64_len) < 0)
    return NULL;

  return b64digest;
}

char *
smd5_hash(char * password)
{
  unsigned char md5digest[MD5_DIGEST_BYTES];
  int b64_len = 0;
  int slen = 0;
  char * b64digest = NULL;
  char * temp = NULL;
  char * salt = getSalt();

  if ( password == NULL )
    return NULL;

  slen = strlen(password)+SALT_LEN;
  temp = (char*)malloc(slen*sizeof(char));
  if ( temp == NULL )
    return NULL;
  bzero(temp, (slen*sizeof(char)));
  snprintf(temp, slen, "%s%s", password, salt);

  md5_buffer(temp, strlen(temp), md5digest);

  b64_len = CEILING(MD5_DIGEST_BYTES+SALT_LEN)*4+1;
  b64digest = (char*)malloc(sizeof(char)*b64_len);
  if ( b64digest == NULL )
    return NULL;
  bzero(b64digest, b64_len);

  Free(temp);
  temp = (char *)malloc((sizeof(md5digest)+SALT_LEN)*sizeof(char));
  if ( temp == NULL )
    return NULL;
  bzero(temp, ((sizeof(md5digest)+SALT_LEN)*sizeof(char)));
  snprintf(temp, (sizeof(md5digest)+SALT_LEN), "%s%s", md5digest, salt);

  if (base64_encode(temp, sizeof(md5digest)+SALT_LEN, b64digest, b64_len) < 0)
    return NULL;

  return b64digest;
}

int
cRandom(int minr, int maxr)
{
  int j = 0;
  int t = 0;
  struct timeval tv;
  if ( gettimeofday(&tv, NULL) < 0 )
    return -1;

  srand((unsigned int)tv.tv_usec);

  maxr = ABS(maxr);
  minr = ABS(minr);

  if ( minr > maxr )
    {
      t = maxr;
      maxr = minr;
      minr = t;
    }
  j = minr+(int)((double)((maxr-minr)+1) * rand()/(RAND_MAX+1.0));
  return j;
}

char *
getSalt ()
{
  unsigned long seed[2];
  const char *const seedchars =
    "./0123456789ABCDEFGHIJKLMNOPQRST" "UVWXYZabcdefghijklmnopqrstuvwxyz";
  int i;

  seed[0] = time (NULL);
  seed[1] = getpid () ^ (seed[0] >> 14 & 0x30000);

  for (i = 3; i < 8; i++)
    salt[i] = seedchars[(seed[i / 5] >> (i % 5) * 6) & 0x3f];
  return salt;
}

char *
cgetSalt ()
{
  unsigned long seed[2];
  const char *const seedchars =
    "./0123456789ABCDEFGHIJKLMNOPQRST" "UVWXYZabcdefghijklmnopqrstuvwxyz";
  int i;

  seed[0] = time (NULL);
  seed[1] = getpid () ^ (seed[0] >> 14 & 0x30000);

  for (i = 0; i < 8; i++)
    csalt[i] = seedchars[(seed[i / 5] >> (i % 5) * 6) & 0x3f];
  return csalt;
}