Updating lib/libcrypt

Change-Id: I4dc5ca7c86abc5295ffc57386a14dd1d790fd489
This commit is contained in:
2013-01-14 11:36:27 +01:00
parent e8235bc09a
commit f5435c74b7
9 changed files with 180 additions and 100 deletions
+57 -21
View File
@@ -1,4 +1,4 @@
/* $NetBSD: crypt.c,v 1.28 2009/05/01 00:28:17 perry Exp $ */
/* $NetBSD: crypt.c,v 1.33 2011/12/28 03:13:09 christos Exp $ */
/*
* Copyright (c) 1989, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)crypt.c 8.1.1.1 (Berkeley) 8/18/93";
#else
__RCSID("$NetBSD: crypt.c,v 1.28 2009/05/01 00:28:17 perry Exp $");
__RCSID("$NetBSD: crypt.c,v 1.33 2011/12/28 03:13:09 christos Exp $");
#endif
#endif /* not lint */
@@ -445,8 +445,6 @@ static const unsigned char itoa64[] = /* 0..63 => ascii-64 */
/* ===== Tables that are initialized at run time ==================== */
static unsigned char a64toi[128]; /* ascii-64 => 0..63 */
/* Initial key schedule permutation */
static C_block PC1ROT[64/CHUNKBITS][1<<CHUNKBITS];
@@ -469,13 +467,42 @@ static C_block CF6464[64/CHUNKBITS][1<<CHUNKBITS];
static C_block constdatablock; /* encryption constant */
static char cryptresult[1+4+4+11+1]; /* encrypted result */
/*
* We match the behavior of UFC-crypt on systems where "char" is signed by
* default (the majority), regardless of char's signedness on our system.
*/
static inline int
ascii_to_bin(char ch)
{
signed char sch = ch;
int retval;
if (sch >= 'a')
retval = sch - ('a' - 38);
else if (sch >= 'A')
retval = sch - ('A' - 12);
else
retval = sch - '.';
return retval & 0x3f;
}
/*
* When we choose to "support" invalid salts, nevertheless disallow those
* containing characters that would violate the passwd file format.
*/
static inline int
ascii_is_unsafe(char ch)
{
return !ch || ch == '\n' || ch == ':';
}
/*
* Return a pointer to static data consisting of the "setting"
* followed by an encryption produced by the "key" and "setting".
*/
char *
crypt(const char *key, const char *setting)
static char *
__crypt(const char *key, const char *setting)
{
char *encp;
int32_t i;
@@ -502,7 +529,7 @@ crypt(const char *key, const char *setting)
key++;
keyblock.b[i] = t;
}
if (des_setkey((char *)keyblock.b)) /* also initializes "a64toi" */
if (des_setkey((char *)keyblock.b))
return (NULL);
encp = &cryptresult[0];
@@ -529,11 +556,14 @@ crypt(const char *key, const char *setting)
/* get iteration count */
num_iter = 0;
for (i = 4; --i >= 0; ) {
if ((t = (unsigned char)setting[i]) == '\0')
t = '.';
encp[i] = t;
num_iter = (num_iter<<6) | a64toi[t];
int value = ascii_to_bin(setting[i]);
if (itoa64[value] != setting[i])
return NULL;
encp[i] = setting[i];
num_iter = (num_iter << 6) | value;
}
if (num_iter == 0)
return NULL;
setting += 4;
encp += 4;
salt_size = 4;
@@ -541,14 +571,17 @@ crypt(const char *key, const char *setting)
default:
num_iter = 25;
salt_size = 2;
if (ascii_is_unsafe(setting[0]) || ascii_is_unsafe(setting[1]))
return NULL;
}
salt = 0;
for (i = salt_size; --i >= 0; ) {
if ((t = (unsigned char)setting[i]) == '\0')
t = '.';
encp[i] = t;
salt = (salt<<6) | a64toi[t];
int value = ascii_to_bin(setting[i]);
if (salt_size > 2 && itoa64[value] != setting[i])
return NULL;
encp[i] = setting[i];
salt = (salt << 6) | value;
}
encp += salt_size;
if (des_cipher((char *)(void *)&constdatablock,
@@ -580,6 +613,15 @@ crypt(const char *key, const char *setting)
return (cryptresult);
}
char *
crypt(const char *key, const char *salt)
{
char *res = __crypt(key, salt);
if (res)
return res;
/* How do I handle errors ? Return "*0" or "*1" */
return __UNCONST(salt[0] == '*' && salt[1] == '0' ? "*1" : "*0");
}
/*
* The Key Schedule, filled in by des_setkey() or setkey().
@@ -750,12 +792,6 @@ init_des(void)
int tableno;
static unsigned char perm[64], tmp32[32]; /* "static" for speed */
/*
* table that converts chars "./0-9A-Za-z"to integers 0-63.
*/
for (i = 0; i < 64; i++)
a64toi[itoa64[i]] = i;
/*
* PC1ROT - bit reverse, then PC1, then Rotate, then PC2.
*/