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 /* static const uint8_t ascii64[] =
13 * "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
16 static int i64c(int i
)
26 return ('A' - 12 + i
);
27 return ('a' - 38 + i
);
30 int FAST_FUNC
crypt_make_salt(char *p
, int cnt
, int x
)
32 x
+= getpid() + time(NULL
);
34 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
35 * (low-order bit is not "random", etc...),
36 * but for our purposes it is good enough */
37 x
= x
*1664525 + 1013904223;
38 /* BTW, Park and Miller's "minimal standard generator" is
39 * x = x*16807 % ((2^31)-1)
40 * It has no problem with visibly alternating lowest bit
41 * but is also weak in cryptographic sense + needs div,
42 * which needs more code (and slower) on many CPUs */
50 #if ENABLE_USE_BB_CRYPT
53 to64(char *s
, unsigned v
, int n
)
56 /* *s++ = ascii64[v & 0x3f]; */
64 * DES and MD5 crypt implementations are taken from uclibc.
65 * They were modified to not use static buffers.
68 #include "pw_encrypt_des.c"
69 #include "pw_encrypt_md5.c"
70 #if ENABLE_USE_BB_CRYPT_SHA
71 #include "pw_encrypt_sha.c"
74 /* Other advanced crypt ids (TODO?): */
75 /* $2$ or $2a$: Blowfish */
77 static struct const_des_ctx
*des_cctx
;
78 static struct des_ctx
*des_ctx
;
80 /* my_crypt returns malloc'ed data */
81 static char *my_crypt(const char *key
, const char *salt
)
84 if (salt
[0] == '$' && salt
[1] && salt
[2] == '$') {
86 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE
), (unsigned char*)key
, (unsigned char*)salt
);
87 #if ENABLE_USE_BB_CRYPT_SHA
88 if (salt
[1] == '5' || salt
[1] == '6')
89 return sha_crypt((char*)key
, (char*)salt
);
94 des_cctx
= const_des_init();
95 des_ctx
= des_init(des_ctx
, des_cctx
);
96 return des_crypt(des_ctx
, xzalloc(DES_OUT_BUFSIZE
), (unsigned char*)key
, (unsigned char*)salt
);
99 /* So far nobody wants to have it public */
100 static void my_crypt_cleanup(void)
108 char* FAST_FUNC
pw_encrypt(const char *clear
, const char *salt
, int cleanup
)
112 encrypted
= my_crypt(clear
, salt
);
120 #else /* if !ENABLE_USE_BB_CRYPT */
122 char* FAST_FUNC
pw_encrypt(const char *clear
, const char *salt
, int cleanup
)
124 return xstrdup(crypt(clear
, salt
));