Skip to content

Commit

Permalink
Extend --enable-weak-hashes configure option
Browse files Browse the repository at this point in the history
Extend --enable-weak-hashes configure option to accept optional "glibc"
parameter.  When specified, it enables only those of weak hashes that
are supported by historic versions of the GNU libc.

Closes: besser82#7
  • Loading branch information
ldv-alt committed Jun 29, 2018
1 parent 09d53d0 commit 06ea2f8
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 21 deletions.
20 changes: 14 additions & 6 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,13 @@ check_PROGRAMS = \

if ENABLE_WEAK_HASHES
libcrypt_la_SOURCES += \
crypt-md5.c crypt-des.c crypt-nthash.c crypt-pbkdf1-sha1.c \
crypt-sunmd5.c \
alg-des.c alg-hmac-sha1.c alg-md4.c alg-md5.c alg-sha1.c
crypt-des.c crypt-md5.c alg-des.c alg-md5.c

if ENABLE_WEAK_NON_GLIBC_HASHES
libcrypt_la_SOURCES += \
crypt-nthash.c crypt-pbkdf1-sha1.c crypt-sunmd5.c \
alg-hmac-sha1.c alg-md4.c alg-sha1.c
endif

nodist_libcrypt_la_SOURCES = \
alg-des-tables.c
Expand All @@ -143,11 +147,15 @@ alg-des-tables.c: gen-des-tables
$(AM_V_at)mv -f alg-des-tables.c.T alg-des-tables.c

check_PROGRAMS += \
test-alg-des test-alg-hmac-sha1 test-alg-md4 \
test-alg-md5 test-alg-sha1 \
test-crypt-badsalt test-crypt-des test-crypt-md5 \
test-alg-des test-alg-md5 test-crypt-des test-crypt-md5 \
test-crypt-badsalt

if ENABLE_WEAK_NON_GLIBC_HASHES
check_PROGRAMS += \
test-alg-hmac-sha1 test-alg-md4 test-alg-sha1 \
test-crypt-nthash test-crypt-sunmd5 test-crypt-pbkdf1-sha1
endif
endif

if ENABLE_OBSOLETE_API
libcrypt_la_SOURCES += crypt-des-obsolete.c
Expand Down
29 changes: 22 additions & 7 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,28 @@ AC_ARG_ENABLE([obsolete-api],

AC_ARG_ENABLE([weak-hashes],
AS_HELP_STRING(
[--disable-weak-hashes],
[do not include any support for weak hashes (DES, MD5).
Implies --disable-obsolete-api, and breaks binary compatibility
with glibc's libcrypt.]
[--enable-weak-hashes[=ARG]],
[When set to "yes", all of the hashing methods documented in crypt(5)
are supported for authenticating existing password hashes, including
several that are too weak to use for new passwords.
When set to "glibc", only the weak hashes supported by historic
versions of GNU libc are supported: traditional DES and FreeBSD-style
MD5, but not bigcrypt-DES, BSDi extended DES, SHA1, SUNMD5, or NTHASH.
When set to "no", only bcrypt, SHA512, and SHA256 are supported.
This mode implies --disable-obsolete-api and breaks binary
compatibility with glibc's libcrypt.
[default=yes]]
),
[case "${enableval}" in
yes) enable_weak_hashes=1 ;;
no) enable_weak_hashes=0 ;;
yes) enable_weak_hashes=1; enable_weak_non_glibc_hashes=1 ;;
glibc) enable_weak_hashes=1; enable_weak_non_glibc_hashes=0 ;;
no) enable_weak_hashes=0; enable_weak_non_glibc_hashes=0 ;;
*) AC_MSG_ERROR([bad value ${enableval} for --enable-weak-hashes]) ;;
esac],
[enable_weak_hashes=1]
[enable_weak_hashes=1; enable_weak_non_glibc_hashes=1]
)

# If weak hashes are disabled, then the obsolete APIs won't work anyway
Expand Down Expand Up @@ -293,6 +304,10 @@ AC_DEFINE_UNQUOTED([ENABLE_WEAK_HASHES], [$enable_weak_hashes],
If this is 0, ENABLE_OBSOLETE_API must also be 0.])
AM_CONDITIONAL([ENABLE_WEAK_HASHES], [test $enable_weak_hashes = 1])

AC_DEFINE_UNQUOTED([ENABLE_WEAK_NON_GLIBC_HASHES], [$enable_weak_non_glibc_hashes],
[Define as 1 if weak hashes not supported by glibc should be included, 0 otherwise.])
AM_CONDITIONAL([ENABLE_WEAK_NON_GLIBC_HASHES], [test $enable_weak_non_glibc_hashes = 1])

# The Makefile needs to know which versions of the library we are building.
AM_CONDITIONAL([ENABLE_STATIC], [test $enable_static = yes])
AM_CONDITIONAL([ENABLE_SHARED], [test $enable_shared = yes])
Expand Down
9 changes: 8 additions & 1 deletion crypt-des.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ des_gen_hash (struct des_ctx *ctx, uint32_t count, uint8_t *output,
}

/* The original UNIX DES-based password hash, no extensions. */
static void
#if ENABLE_WEAK_NON_GLIBC_HASHES
static
#endif
void
crypt_des_trd_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size)
Expand Down Expand Up @@ -192,6 +195,7 @@ crypt_des_trd_rn (const char *phrase, const char *setting,
des_gen_hash (ctx, 25, cp, pkbuf);
}

#if ENABLE_WEAK_NON_GLIBC_HASHES
/* This algorithm is algorithm 0 (default) shipped with the C2 secure
implementation of Digital UNIX.
Expand Down Expand Up @@ -342,6 +346,7 @@ crypt_des_xbsd_rn (const char *phrase, const char *setting,
des_set_salt (ctx, salt);
des_gen_hash (ctx, count, cp, pkbuf);
}
#endif /* ENABLE_WEAK_NON_GLIBC_HASHES */

void
gensalt_des_trd_rn (unsigned long count,
Expand All @@ -365,6 +370,7 @@ gensalt_des_trd_rn (unsigned long count,
output[2] = '\0';
}

#if ENABLE_WEAK_NON_GLIBC_HASHES
void
gensalt_des_xbsd_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
Expand Down Expand Up @@ -406,3 +412,4 @@ gensalt_des_xbsd_rn (unsigned long count,

output[9] = '\0';
}
#endif /* ENABLE_WEAK_NON_GLIBC_HASHES */
4 changes: 4 additions & 0 deletions crypt-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ void _xcrypt_secure_memset (void *s, size_t len)
#if ENABLE_WEAK_HASHES
#define comp_maskl _crypt_comp_maskl
#define comp_maskr _crypt_comp_maskr
#if ENABLE_WEAK_NON_GLIBC_HASHES
#define crypt_des_trd_or_big_rn _crypt_crypt_des_trd_or_big_rn
#else
#define crypt_des_trd_rn _crypt_crypt_des_trd_rn
#endif
#define crypt_des_xbsd_rn _crypt_crypt_des_xbsd_rn
#define crypt_md5_rn _crypt_crypt_md5_rn
#define crypt_nthash_rn _crypt_crypt_nthash_rn
Expand Down
20 changes: 14 additions & 6 deletions crypt-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ extern void gensalt_sha_rn (char tag, size_t maxsalt, unsigned long defcount,
/* Individual hash functions */

#if ENABLE_WEAK_HASHES
extern void crypt_md5_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size);
#if ENABLE_WEAK_NON_GLIBC_HASHES
extern void crypt_des_trd_or_big_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size);
extern void crypt_des_xbsd_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size);
extern void crypt_md5_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size);
extern void crypt_nthash_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size);
Expand All @@ -57,6 +58,11 @@ extern void crypt_sha1_rn (const char *phrase, const char *setting,
extern void crypt_sunmd5_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size);
#else
extern void crypt_des_trd_rn (const char *phrase, const char *setting,
uint8_t *output, size_t o_size,
void *scratch, size_t s_size);
#endif
#endif

extern void crypt_sha256_rn (const char *phrase, const char *setting,
Expand All @@ -73,12 +79,13 @@ extern void crypt_bcrypt_rn (const char *phrase, const char *setting,
extern void gensalt_des_trd_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
uint8_t *output, size_t o_size);
extern void gensalt_des_xbsd_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
uint8_t *output, size_t o_size);
extern void gensalt_md5_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
uint8_t *output, size_t o_size);
#if ENABLE_WEAK_NON_GLIBC_HASHES
extern void gensalt_des_xbsd_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
uint8_t *output, size_t o_size);
extern void gensalt_nthash_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
uint8_t *output, size_t o_size);
Expand All @@ -89,6 +96,7 @@ extern void gensalt_sunmd5_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
uint8_t *output, size_t o_size);
#endif
#endif

extern void gensalt_sha256_rn (unsigned long count,
const uint8_t *rbytes, size_t nrbytes,
Expand Down
15 changes: 15 additions & 0 deletions crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@ static const struct hashfn tagged_hashes[] =
/* legacy hashes */
#if ENABLE_WEAK_HASHES
{ "$1$", crypt_md5_rn, gensalt_md5_rn },
#if ENABLE_WEAK_NON_GLIBC_HASHES
{ "$3$", crypt_nthash_rn, gensalt_nthash_rn },
{ "$md5", crypt_sunmd5_rn, gensalt_sunmd5_rn },
{ "$sha1", crypt_sha1_rn, gensalt_sha1_rn },
#endif
#endif
{ "$5$", crypt_sha256_rn, gensalt_sha256_rn },
{ "$6$", crypt_sha512_rn, gensalt_sha512_rn },
{ 0, 0, 0 }
};

#if ENABLE_WEAK_HASHES
#if ENABLE_WEAK_NON_GLIBC_HASHES
/* BSD-style extended DES */
static const struct hashfn bsdi_extended_hash =
{
Expand All @@ -109,6 +112,16 @@ static const struct hashfn traditional_hash =
"", crypt_des_trd_or_big_rn, gensalt_des_trd_rn
};

#else

/* Traditional DES */
static const struct hashfn traditional_hash =
{
"", crypt_des_trd_rn, gensalt_des_trd_rn
};

#endif

static int
is_des_salt_char (char c)
{
Expand All @@ -131,8 +144,10 @@ get_hashfn (const char *setting)
return 0;
}
#if ENABLE_WEAK_HASHES
#if ENABLE_WEAK_NON_GLIBC_HASHES
else if (setting[0] == '_')
return &bsdi_extended_hash;
#endif
else if (setting[0] == '\0' ||
(is_des_salt_char (setting[0]) && is_des_salt_char (setting[1])))
return &traditional_hash;
Expand Down
2 changes: 2 additions & 0 deletions test-crypt-des.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ static const struct
{ "XX", "XXxzOu6maQKqQ", "*U*U*U*U" },
{ "SD", "SDbsugeBiC58A", "" },

#if ENABLE_WEAK_NON_GLIBC_HASHES
/* BSDI-extended-DES, ditto */
{ "_J9..CCCC", "_J9..CCCCXBrJUJV154M", "U*U*U*U*" },
{ "_J9..CCCC", "_J9..CCCCXUhOBTXzaiE", "U*U***U" },
Expand Down Expand Up @@ -63,6 +64,7 @@ static const struct
"6M..............", "6MvZdspyAL4QEId8ugLUEeDs",
"\xf3\xf4\xe5\xf0\xe8\xe1\xee\xe9\xe5" /* stephanie */
},
#endif
};

#define ntests ARRAY_SIZE (tests)
Expand Down
4 changes: 3 additions & 1 deletion test-gensalt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ static const struct testcase testcases[] =
{
#if ENABLE_WEAK_HASHES
{ "", 2 }, // DES
{ "_", 9 }, // BSDi extended DES
{ "$1$", 11 }, // MD5
#if ENABLE_WEAK_NON_GLIBC_HASHES
{ "_", 9 }, // BSDi extended DES
{ "$3$", 29 }, // NTHASH
{ "$md5", 27 }, // SUNMD5
{ "$sha1", 34 }, // PBKDF with SHA1
#endif
#endif
{ "$5$", 19 }, // SHA-2-256
{ "$6$", 19 }, // SHA-2-512
Expand Down

0 comments on commit 06ea2f8

Please sign in to comment.