1994-08-09 17:07:27 +00:00
|
|
|
/*
|
1994-11-07 21:07:09 +00:00
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
1994-08-09 17:07:27 +00:00
|
|
|
*/
|
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
/*
|
|
|
|
* It has since been changed by Brandon Gillespie, the end result is not
|
|
|
|
* too clean, but it is clear and modular; there is no need for crypt()
|
|
|
|
* to be optimized (and actually a desire for the opposite) so I am not
|
|
|
|
* overly concerned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assumptions made with the format of passwords:
|
|
|
|
*
|
|
|
|
* + Any password beginning with a dollar-sign is assumed to be in
|
|
|
|
* the Modular Crypt Format (MCF), namely: $tag$salt$hash. Any
|
|
|
|
* algorithms added will also use this format. Other MCF assumptions:
|
|
|
|
* + The algorithm tag (field 1) will be less than five characters
|
|
|
|
* long (yay, arbitrary limits). Anything longer is ignored.
|
|
|
|
* New algorithm names are not allowed to be fully numeric as
|
|
|
|
* anything fully numeric is mapped from other OS's not following
|
|
|
|
* our standard, and from older versions of this standard (such as
|
|
|
|
* $1$ for MD5 passwords, rather than $MD5$).
|
|
|
|
* + The salt can be up to 16 characters in length (more arbitrary
|
|
|
|
* limits).
|
|
|
|
* + An invalid or unrecognized algorithm tag will default to use the
|
|
|
|
* 'best' encryption method--whatever that may be at the time.
|
|
|
|
* + If the MCF is not specified, use the 'best' method, unless DES
|
|
|
|
* is installed--then use DES.
|
|
|
|
* + Any password beginning with an underscore '_' is assumed to be
|
|
|
|
* the Extended DES Format, which has its own salt requirements,
|
|
|
|
* and is not the same as the MCF.
|
|
|
|
* + Salt must be limited to the same ascii64 character set the hash
|
|
|
|
* is encoded in (namely "./0-9A-Za-z").
|
|
|
|
*/
|
1994-08-09 17:07:27 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
#include <stdlib.h>
|
1996-07-12 18:57:58 +00:00
|
|
|
#include <string.h>
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#define _CRYPT_C_
|
|
|
|
|
|
|
|
#include "crypt.h"
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
1994-11-07 21:07:09 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
1994-11-07 21:07:09 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
/*
|
|
|
|
* commonly used througout all algorithms
|
|
|
|
*/
|
1996-10-14 08:34:02 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
static unsigned char ascii64[] = /* 0 ... 63 => ascii - 64 */
|
|
|
|
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
void
|
1994-11-07 21:07:09 +00:00
|
|
|
to64(s, v, n)
|
|
|
|
char *s;
|
|
|
|
unsigned long v;
|
|
|
|
int n;
|
|
|
|
{
|
|
|
|
while (--n >= 0) {
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
*s++ = ascii64[v&0x3f];
|
1994-11-07 21:07:09 +00:00
|
|
|
v >>= 6;
|
|
|
|
}
|
|
|
|
}
|
1994-08-09 17:07:27 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
static char * hash_word(password, salt, output)
|
|
|
|
const char *password;
|
|
|
|
const char *salt;
|
|
|
|
char *output;
|
|
|
|
{
|
|
|
|
unsigned char spbuf[_CRYPT_MAX_SALT_LEN+1],
|
|
|
|
pwbuf[_CRYPT_OUTPUT_SIZE+1],
|
|
|
|
* ep, * sp, * pw;
|
|
|
|
unsigned int sl, pl,
|
|
|
|
tag = _CRYPT_DEFAULT_VERSION,
|
|
|
|
mcf = FALSE;
|
|
|
|
|
|
|
|
memset(spbuf, 0, _CRYPT_MAX_SALT_LEN+1);
|
|
|
|
memset(pwbuf, 0, _CRYPT_MAX_SALT_LEN+1);
|
|
|
|
strncpy((char *) spbuf, (unsigned char *) salt, _CRYPT_MAX_SALT_LEN);
|
|
|
|
strncpy((char *) pwbuf, (unsigned char *) password, _CRYPT_OUTPUT_SIZE);
|
|
|
|
sp = &spbuf[0];
|
|
|
|
pw = &pwbuf[0];
|
|
|
|
pl = strlen((char *) pw);
|
|
|
|
|
|
|
|
/* figure out what type of crypt is wanted */
|
|
|
|
if (sp && sp[0] == '$') {
|
|
|
|
mcf = TRUE;
|
|
|
|
sp++;
|
|
|
|
if (strncasecmp((char *) sp, "MD5$", 4)==0) {
|
|
|
|
tag = _MD5_CRYPT;
|
|
|
|
sp += 4;
|
|
|
|
} else if (strncasecmp((char *) sp, "1$", 2)==0) {
|
|
|
|
tag = _MD5_CRYPT_OLD;
|
|
|
|
sp += 2;
|
|
|
|
} else if (strncasecmp((char *) sp, "SHA1$", 5)==0) {
|
|
|
|
tag = _SHS_CRYPT;
|
|
|
|
sp += 5;
|
|
|
|
} else {
|
|
|
|
tag = _CRYPT_DEFAULT_VERSION;
|
|
|
|
while (*sp && *sp != '$')
|
|
|
|
sp++;
|
|
|
|
if (*sp == '$')
|
|
|
|
sp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Refine the salt. Go to the end, it stops at the first '$' or NULL */
|
|
|
|
for (ep=sp; *ep && *ep != '$'; ep++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* we have to do this so we dont overflow _PASSWORD_LEN */
|
|
|
|
if ((ep - sp) > 16) {
|
|
|
|
sl = 16;
|
|
|
|
sp[16] = (char) NULL;
|
|
|
|
} else {
|
|
|
|
sl = ep - sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (tag) {
|
|
|
|
case _MD5_CRYPT_OLD:
|
|
|
|
return crypt_md5(pw, pl, sp, sl, output, "$1$");
|
|
|
|
case _MD5_CRYPT:
|
|
|
|
return crypt_md5(pw, pl, sp, sl, output, "$MD5$");
|
|
|
|
#ifdef DES_CRYPT
|
|
|
|
case _DES_CRYPT:
|
|
|
|
return crypt_des(pw, pl, sp, sl, output, "");
|
|
|
|
#endif
|
|
|
|
/* dropping a DES password through will likely cause problems,
|
|
|
|
but at least crypt() will return as it says it will (we cannot
|
|
|
|
return an error condition) */
|
|
|
|
case _SHS_CRYPT:
|
|
|
|
default:
|
|
|
|
return crypt_shs(pw, pl, sp, sl, output, "$SHA1$");
|
|
|
|
}
|
|
|
|
}
|
1994-08-09 17:07:27 +00:00
|
|
|
|
|
|
|
char *
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
crypt(password, salt)
|
|
|
|
const char *password;
|
|
|
|
const char *salt;
|
1994-08-09 17:07:27 +00:00
|
|
|
{
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
static char output[_CRYPT_OUTPUT_SIZE];
|
|
|
|
|
|
|
|
return hash_word(password, salt, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
malloc_crypt(password, salt)
|
|
|
|
const char *password;
|
|
|
|
const char *salt;
|
|
|
|
{
|
|
|
|
char * output;
|
|
|
|
|
|
|
|
output = (char *) malloc(sizeof(char) * _CRYPT_OUTPUT_SIZE);
|
|
|
|
return hash_word(password, salt, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
match_crypted(possible, crypted)
|
|
|
|
const char * possible,
|
|
|
|
* crypted;
|
|
|
|
{
|
|
|
|
char * pc;
|
|
|
|
int match;
|
1994-08-09 17:07:27 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
pc = malloc_crypt(possible, crypted);
|
1995-05-30 05:51:47 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
match = !strcmp(pc, crypted);
|
1994-11-07 21:07:09 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
free(pc);
|
1994-08-09 17:07:27 +00:00
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
return match;
|
1994-08-09 17:07:27 +00:00
|
|
|
}
|
|
|
|
|
Rewrite of crypt library to be more modular, and addition of the
Secure Hashing Algorithm - 1 (SHA-1), along with the further
refinement of what $x$salt$hash means. With this new crypt the
following are all acceptable:
$1$
$MD5$
$SHA1$
Note: $2$ is used by OpenBSD's Blowfish, which I considered adding
as $BF$, but there is no actual need for it with SHA-1. However,
somebody wishing to add OpenBSD password support could easilly add
it in now.
There is also a malloc_crypt() available in the library now, which
behaves exactly the same as crypt(), but it uses a malloced buffer
instead of a static buffer. However, this is not standard so will
likely not be used much (at all).
Also, for those interested I did a brief speed test Pentium 166/MMX,
which shows the DES crypt to do approximately 2640 crypts a CPU second,
MD5 to do about 62 crypts a CPU second and SHA1 to do about 18 crypts
a CPU second.
Reviewed by: Mark Murray
1999-01-21 13:50:09 +00:00
|
|
|
#undef _CRYPT_C_
|