[PATCH 1/2] libcrypt: make crypt() more modular
William Pitcock
nenolod at dereferenced.org
Wed Nov 16 11:27:48 UTC 2011
By using a function table, we can more cleanly support new crypt
implementations, such as SHA256 ($5$) and SHA512 ($6$).
Signed-off-by: William Pitcock <nenolod at dereferenced.org>
---
libcrypt/crypt.c | 34 ++++++++++++++++++++++++++++------
1 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/libcrypt/crypt.c b/libcrypt/crypt.c
index 8b361d3..28cbc70 100644
--- a/libcrypt/crypt.c
+++ b/libcrypt/crypt.c
@@ -8,14 +8,36 @@
#define __FORCE_GLIBC
#include <crypt.h>
#include <unistd.h>
+#include <string.h>
+#include <syscall.h>
#include "libcrypt.h"
+#define ARRAY_SIZE(v) (sizeof(v) / sizeof((v)[0]))
+
+typedef char *(*crypt_impl_f)(const unsigned char *pw, const unsigned char *salt);
+
+static const struct {
+ const char *salt_pfx;
+ const crypt_impl_f crypt_impl;
+} crypt_impl_tab[] = {
+ { "$1$", __md5_crypt },
+ { NULL, __des_crypt },
+};
+
char *crypt(const char *key, const char *salt)
{
- /* First, check if we are supposed to be using the MD5 replacement
- * instead of DES... */
- if (salt[0]=='$' && salt[1]=='1' && salt[2]=='$')
- return __md5_crypt((unsigned char*)key, (unsigned char*)salt);
- else
- return __des_crypt((unsigned char*)key, (unsigned char*)salt);
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(crypt_impl_tab); i++)
+ {
+ if (crypt_impl_tab[i].salt_pfx &&
+ strncmp(crypt_impl_tab[i].salt_pfx, salt, strlen(crypt_impl_tab[i].salt_pfx)))
+ continue;
+
+ return crypt_impl_tab[i].crypt_impl((unsigned char *) key, (unsigned char *) salt);
+ }
+
+ /* this should never happen, but just incase... */
+ __set_errno(ENOSYS);
+ return NULL;
}
--
1.7.7.3
More information about the uClibc
mailing list