OSDN Git Service

libcrypt: add support for SHA256-CRYPT password hashing
[uclinux-h8/uClibc.git] / libcrypt / crypt.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * crypt() for uClibc
4  * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  */
7
8 #define __FORCE_GLIBC
9 #include <crypt.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <errno.h>
13 #include "libcrypt.h"
14
15 typedef char *(*crypt_impl_f)(const unsigned char *pw, const unsigned char *salt);
16
17 static const struct {
18         const char *salt_pfx;
19         const crypt_impl_f crypt_impl;
20 } crypt_impl_tab[] = {
21         { "$1$",        __md5_crypt },
22 #ifdef __UCLIBC_HAS_SHA256_CRYPT_IMPL__
23         { "$5$",        __sha256_crypt },
24 #endif
25 #ifdef __UCLIBC_HAS_SHA512_CRYPT_IMPL__
26         { "$6$",        __sha512_crypt },
27 #endif
28         { NULL,         __des_crypt },
29 };
30
31 char *crypt(const char *key, const char *salt)
32 {
33         const unsigned char *ukey = (const unsigned char *)key;
34         const unsigned char *usalt = (const unsigned char *)salt;
35         size_t i;
36
37         for (i = 0; i < ARRAY_SIZE(crypt_impl_tab); i++) {
38                 if (crypt_impl_tab[i].salt_pfx != NULL &&
39                     strncmp(crypt_impl_tab[i].salt_pfx, salt, strlen(crypt_impl_tab[i].salt_pfx)))
40                         continue;
41
42                 return crypt_impl_tab[i].crypt_impl(ukey, usalt);
43         }
44
45         /* no crypt implementation was found, set errno to ENOSYS and return NULL */
46         __set_errno(ENOSYS);
47         return NULL;
48 }