19 |
#include <unistd.h> |
#include <unistd.h> |
20 |
#include <fcntl.h> |
#include <fcntl.h> |
21 |
|
|
22 |
#define FLAG_MD5 0x00000001 |
enum |
23 |
#define FLAG_DES 0x00000002 |
{ |
24 |
#define FLAG_SALT 0x00000004 |
FLAG_MD5 = 0x00000001, |
25 |
#define FLAG_PASS 0x00000008 |
FLAG_SALT = 0x00000002, |
26 |
#define FLAG_LENGTH 0x00000010 |
FLAG_PASS = 0x00000004, |
27 |
#define FLAG_BLOWFISH 0x00000020 |
FLAG_LENGTH = 0x00000008, |
28 |
#define FLAG_ROUNDS 0x00000040 |
FLAG_BLOWFISH = 0x00000010, |
29 |
#define FLAG_EXT 0x00000080 |
FLAG_ROUNDS = 0x00000020, |
30 |
#define FLAG_RAW 0x00000100 |
FLAG_RAW = 0x00000040, |
31 |
#define FLAG_SHA256 0x00000200 |
FLAG_SHA256 = 0x00000080, |
32 |
#define FLAG_SHA512 0x00000400 |
FLAG_SHA512 = 0x00000100 |
33 |
|
}; |
34 |
|
|
35 |
|
|
36 |
extern char *crypt(); |
extern char *crypt(); |
40 |
static const char *make_sha256_salt_para(const char *); |
static const char *make_sha256_salt_para(const char *); |
41 |
static const char *make_sha512_salt(unsigned int); |
static const char *make_sha512_salt(unsigned int); |
42 |
static const char *make_sha512_salt_para(const char *); |
static const char *make_sha512_salt_para(const char *); |
|
static const char *make_des_salt(void); |
|
|
static const char *make_ext_salt(unsigned int); |
|
|
static const char *make_ext_salt_para(unsigned int, const char *); |
|
43 |
static const char *make_md5_salt(unsigned int); |
static const char *make_md5_salt(unsigned int); |
44 |
static const char *make_md5_salt_para(const char *); |
static const char *make_md5_salt_para(const char *); |
45 |
static const char *make_bf_salt(unsigned int, unsigned int); |
static const char *make_blowfish_salt(unsigned int, unsigned int); |
46 |
static const char *make_bf_salt_para(unsigned int, const char *); |
static const char *make_blowfish_salt_para(unsigned int, const char *); |
|
static const char *int_to_base64(unsigned int); |
|
47 |
static const char *generate_random_salt(char *, unsigned int); |
static const char *generate_random_salt(char *, unsigned int); |
48 |
static const char *generate_poor_salt(char *, unsigned int); |
static const char *generate_poor_salt(char *, unsigned int); |
49 |
static void full_usage(void); |
static void full_usage(void); |
50 |
static void brief_usage(void); |
static void brief_usage(void); |
51 |
|
|
52 |
static const char saltChars[] = |
static const char saltChars[] = |
53 |
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
54 |
/* 0 .. 63, ascii - 64 */ |
/* 0 .. 63, ascii - 64 */ |
55 |
|
|
56 |
int |
int |
60 |
const char *saltpara = NULL; |
const char *saltpara = NULL; |
61 |
const char *salt = NULL; |
const char *salt = NULL; |
62 |
const char *ret = NULL; |
const char *ret = NULL; |
63 |
int c; |
unsigned int flag = 0; |
|
int flag = 0; |
|
64 |
int length = 0; /* Not Set */ |
int length = 0; /* Not Set */ |
65 |
int rounds = 0; /* Not set, since extended DES needs 25 and blowfish needs |
int rounds = 0; /* Not set, since extended DES needs 25 and blowfish needs |
66 |
** 4 by default, a side effect of this being the encryption |
** 4 by default, a side effect of this being the encryption |
68 |
** parameter. |
** parameter. |
69 |
*/ |
*/ |
70 |
|
|
71 |
while ((c = getopt(argc, argv, "56mdber:h?l:s:p:R:")) != -1) |
for (int c = 0; (c = getopt(argc, argv, "56mbr:h?l:s:p:R:")) != -1; ) |
72 |
{ |
{ |
73 |
switch (c) |
switch (c) |
74 |
{ |
{ |
81 |
case 'm': |
case 'm': |
82 |
flag |= FLAG_MD5; |
flag |= FLAG_MD5; |
83 |
break; |
break; |
|
case 'd': |
|
|
flag |= FLAG_DES; |
|
|
break; |
|
84 |
case 'b': |
case 'b': |
85 |
flag |= FLAG_BLOWFISH; |
flag |= FLAG_BLOWFISH; |
86 |
rounds = 4; |
rounds = 4; |
87 |
break; |
break; |
|
case 'e': |
|
|
flag |= FLAG_EXT; |
|
|
rounds = 25; |
|
|
break; |
|
88 |
case 'l': |
case 'l': |
89 |
flag |= FLAG_LENGTH; |
flag |= FLAG_LENGTH; |
90 |
if ((length = atoi(optarg)) < 0) |
if ((length = atoi(optarg)) < 0) |
121 |
} |
} |
122 |
} |
} |
123 |
|
|
124 |
if (flag & FLAG_DES) |
if (flag & FLAG_SHA256) |
|
{ |
|
|
if (flag & FLAG_SALT) |
|
|
{ |
|
|
if (strlen(saltpara) == 2) |
|
|
salt = saltpara; |
|
|
else |
|
|
{ |
|
|
printf("Invalid salt, please enter 2 alphanumeric characters\n"); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
else |
|
|
salt = make_des_salt(); |
|
|
} |
|
|
else if (flag & FLAG_SHA256) |
|
125 |
{ |
{ |
126 |
if (length == 0) |
if (length == 0) |
127 |
length = 16; |
length = 16; |
144 |
if (length == 0) |
if (length == 0) |
145 |
length = 22; |
length = 22; |
146 |
if (flag & FLAG_SALT) |
if (flag & FLAG_SALT) |
147 |
salt = make_bf_salt_para(rounds, saltpara); |
salt = make_blowfish_salt_para(rounds, saltpara); |
148 |
else |
else |
149 |
salt = make_bf_salt(rounds, length); |
salt = make_blowfish_salt(rounds, length); |
|
} |
|
|
else if (flag & FLAG_EXT) |
|
|
{ |
|
|
/* XXX - rounds needs to be done */ |
|
|
if (flag & FLAG_SALT) |
|
|
{ |
|
|
if (strlen(saltpara) == 4) |
|
|
{ |
|
|
salt = make_ext_salt_para(rounds, saltpara); |
|
|
} |
|
|
else |
|
|
{ |
|
|
printf("Invalid salt, please enter 4 alphanumeric characters\n"); |
|
|
exit(1); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
salt = make_ext_salt(rounds); |
|
|
} |
|
150 |
} |
} |
151 |
else if (flag & FLAG_RAW) |
else if (flag & FLAG_RAW) |
|
{ |
|
152 |
salt = saltpara; |
salt = saltpara; |
|
} |
|
153 |
else /* Default to MD5 */ |
else /* Default to MD5 */ |
154 |
{ |
{ |
155 |
if (length == 0) |
if (length == 0) |
177 |
} |
} |
178 |
|
|
179 |
static const char * |
static const char * |
|
make_des_salt(void) |
|
|
{ |
|
|
static char salt[3]; |
|
|
|
|
|
generate_random_salt(salt, 2); |
|
|
salt[2] = '\0'; |
|
|
return salt; |
|
|
} |
|
|
|
|
|
static const char * |
|
|
int_to_base64(unsigned int value) |
|
|
{ |
|
|
static char buf[5]; |
|
|
unsigned int i; |
|
|
|
|
|
for (i = 0; i < 4; ++i) |
|
|
{ |
|
|
buf[i] = saltChars[value & 63]; |
|
|
value >>= 6; /* Right shifting 6 places is the same as dividing by 64 */ |
|
|
} |
|
|
|
|
|
buf[i] = '\0'; |
|
|
|
|
|
return buf; |
|
|
} |
|
|
|
|
|
static const char * |
|
|
make_ext_salt(unsigned int rounds) |
|
|
{ |
|
|
static char salt[10]; |
|
|
|
|
|
snprintf(salt, sizeof(salt), "_%s", int_to_base64(rounds)); |
|
|
|
|
|
generate_random_salt(&salt[5], 4); |
|
|
salt[9] = '\0'; |
|
|
|
|
|
return salt; |
|
|
} |
|
|
|
|
|
static const char * |
|
|
make_ext_salt_para(unsigned int rounds, const char *saltpara) |
|
|
{ |
|
|
static char salt[10]; |
|
|
|
|
|
snprintf(salt, sizeof(salt), "_%s%s", int_to_base64(rounds), saltpara); |
|
|
return salt; |
|
|
} |
|
|
|
|
|
static const char * |
|
180 |
make_sha256_salt_para(const char *saltpara) |
make_sha256_salt_para(const char *saltpara) |
181 |
{ |
{ |
182 |
static char salt[21]; |
static char salt[21]; |
300 |
} |
} |
301 |
|
|
302 |
static const char * |
static const char * |
303 |
make_bf_salt_para(unsigned int rounds, const char *saltpara) |
make_blowfish_salt_para(unsigned int rounds, const char *saltpara) |
304 |
{ |
{ |
305 |
static char salt[31]; |
static char salt[31]; |
306 |
char tbuf[3]; |
char tbuf[3]; |
320 |
} |
} |
321 |
|
|
322 |
static const char * |
static const char * |
323 |
make_bf_salt(unsigned int rounds, unsigned int length) |
make_blowfish_salt(unsigned int rounds, unsigned int length) |
324 |
{ |
{ |
325 |
static char salt[31]; |
static char salt[31]; |
326 |
char tbuf[3]; |
char tbuf[3]; |
384 |
static void |
static void |
385 |
full_usage(void) |
full_usage(void) |
386 |
{ |
{ |
387 |
printf("mkpasswd [-5|-6|-m|-d|-b|-e] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n"); |
printf("mkpasswd [-5|-6|-b|-m] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n"); |
388 |
printf(" [-R rawsalt]\n"); |
printf(" [-R rawsalt]\n"); |
389 |
printf("-5 Generate a SHA-256 password\n"); |
printf("-5 Generate a SHA-256 password\n"); |
390 |
printf("-6 Generate a SHA-512 password\n"); |
printf("-6 Generate a SHA-512 password\n"); |
|
printf("-m Generate an MD5 password\n"); |
|
|
printf("-d Generate a DES password\n"); |
|
391 |
printf("-b Generate a Blowfish password\n"); |
printf("-b Generate a Blowfish password\n"); |
392 |
printf("-e Generate an Extended DES password\n"); |
printf("-m Generate an MD5 password\n"); |
393 |
printf("-l Specify a length for a random MD5 or Blowfish salt\n"); |
printf("-l Specify a length for a random MD5 or Blowfish salt\n"); |
394 |
printf("-r Specify a number of rounds for a Blowfish or Extended DES password\n"); |
printf("-r Specify a number of rounds for a Blowfish password;\n"); |
395 |
printf(" Blowfish: default 4, no more than 6 recommended\n"); |
printf(" default is 4, no more than 6 recommended\n"); |
396 |
printf(" Extended DES: default 25\n"); |
printf("-s Specify a salt, up to 16 alphanumeric characters for SHA/MD5,\n"); |
397 |
printf("-s Specify a salt, 2 alphanumeric characters for DES, up to 16 for SHA/MD5,\n"); |
printf(" and at least 22 for Blowfish\n"); |
|
printf(" up to 22 for Blowfish, and 4 for Extended DES\n"); |
|
398 |
printf("-R Specify a raw salt passed directly to crypt()\n"); |
printf("-R Specify a raw salt passed directly to crypt()\n"); |
399 |
printf("-p Specify a plaintext password to use\n"); |
printf("-p Specify a plaintext password to use\n"); |
400 |
printf("Example: mkpasswd -m -s 3dr -p test\n"); |
printf("Example: mkpasswd -6 -s 3dr -p test\n"); |
401 |
exit(0); |
exit(0); |
402 |
} |
} |
403 |
|
|
405 |
brief_usage(void) |
brief_usage(void) |
406 |
{ |
{ |
407 |
printf("mkpasswd - password hash generator\n"); |
printf("mkpasswd - password hash generator\n"); |
408 |
printf("Standard DES: mkpasswd [-d] [-s salt] [-p plaintext]\n"); |
printf(" MD5: mkpasswd [-m] [-l saltlength] [-s salt] [-p plaintext]\n"); |
409 |
printf("Extended DES: mkpasswd -e [-r rounds] [-s salt] [-p plaintext]\n"); |
printf(" SHA-256: mkpasswd -5 [-l saltlength] [-s salt] [-p plaintext]\n"); |
410 |
printf(" SHA-256: mkpasswd -5 [-l saltlength] [-s salt] [-p plaintext]\n"); |
printf(" SHA-512: mkpasswd -6 [-l saltlength] [-s salt] [-p plaintext]\n"); |
411 |
printf(" SHA-512: mkpasswd -6 [-l saltlength] [-s salt] [-p plaintext]\n"); |
printf("Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n"); |
|
printf(" MD5: mkpasswd -m [-l saltlength] [-s salt] [-p plaintext]\n"); |
|
|
printf(" Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n"); |
|
412 |
printf(" [-p plaintext]\n"); |
printf(" [-p plaintext]\n"); |
413 |
printf(" Raw: mkpasswd -R <rawsalt> [-p plaintext]\n"); |
printf(" Raw: mkpasswd -R <rawsalt> [-p plaintext]\n"); |
414 |
printf("Use -h for full usage\n"); |
printf("Use -h for full usage\n"); |
415 |
exit(0); |
exit(0); |
416 |
} |
} |