1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #if ENABLE_USE_BB_CRYPT
15 * DES and MD5 crypt implementations are taken from uclibc.
16 * They were modified to not use static buffers.
19 /* Used by pw_encrypt_XXX.c */
20 static const uint8_t ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
22 to64(char *s, unsigned v, int n)
25 *s++ = ascii64[v & 0x3f];
31 #include "pw_encrypt_des.c"
32 #include "pw_encrypt_md5.c"
33 #if ENABLE_USE_BB_CRYPT_SHA
34 #include "pw_encrypt_sha.c"
37 /* Other advanced crypt ids (TODO?): */
38 /* $2$ or $2a$: Blowfish */
40 static struct const_des_ctx *des_cctx;
41 static struct des_ctx *des_ctx;
43 /* my_crypt returns malloc'ed data */
44 static char *my_crypt(const char *key, const char *salt)
47 if (salt[0] == '$' && salt[1] && salt[2] == '$') {
49 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
50 #if ENABLE_USE_BB_CRYPT_SHA
51 if (salt[1] == '5' || salt[1] == '6')
52 return sha_crypt((char*)key, (char*)salt);
57 des_cctx = const_des_init();
58 des_ctx = des_init(des_ctx, des_cctx);
59 return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
62 /* So far nobody wants to have it public */
63 static void my_crypt_cleanup(void)
71 char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
75 encrypted = my_crypt(clear, salt);
83 #else /* if !ENABLE_USE_BB_CRYPT */
85 char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
87 return xstrdup(crypt(clear, salt));