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 source tree.
12 /* static const uint8_t ascii64[] ALIGN1 =
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 */)
33 int x = getpid() + monotonic_us();
35 /* x = (x*1664525 + 1013904223) % 2^32 generator is lame
36 * (low-order bit is not "random", etc...),
37 * but for our purposes it is good enough */
38 x = x*1664525 + 1013904223;
39 /* BTW, Park and Miller's "minimal standard generator" is
40 * x = x*16807 % ((2^31)-1)
41 * It has no problem with visibly alternating lowest bit
42 * but is also weak in cryptographic sense + needs div,
43 * which needs more code (and slower) on many CPUs */
51 char* FAST_FUNC crypt_make_pw_salt(char salt[MAX_PW_SALT_LEN], const char *algo)
54 char *salt_ptr = salt;
56 /* Standard chpasswd uses uppercase algos ("MD5", not "md5").
57 * Need to be case-insensitive in the code below.
59 if ((algo[0]|0x20) != 'd') { /* not des */
60 len = 8/2; /* so far assuming md5 */
64 #if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
65 if ((algo[0]|0x20) == 's') { /* sha */
66 salt[1] = '5' + (strcasecmp(algo, "sha512") == 0);
71 crypt_make_salt(salt_ptr, len);
75 #if ENABLE_USE_BB_CRYPT
78 to64(char *s, unsigned v, int n)
81 /* *s++ = ascii64[v & 0x3f]; */
89 * DES and MD5 crypt implementations are taken from uclibc.
90 * They were modified to not use static buffers.
93 #include "pw_encrypt_des.c"
94 #include "pw_encrypt_md5.c"
95 #if ENABLE_USE_BB_CRYPT_SHA
96 #include "pw_encrypt_sha.c"
99 /* Other advanced crypt ids (TODO?): */
100 /* $2$ or $2a$: Blowfish */
102 static struct const_des_ctx *des_cctx;
103 static struct des_ctx *des_ctx;
105 /* my_crypt returns malloc'ed data */
106 static char *my_crypt(const char *key, const char *salt)
109 if (salt[0] == '$' && salt[1] && salt[2] == '$') {
111 return md5_crypt(xzalloc(MD5_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
112 #if ENABLE_USE_BB_CRYPT_SHA
113 if (salt[1] == '5' || salt[1] == '6')
114 return sha_crypt((char*)key, (char*)salt);
119 des_cctx = const_des_init();
120 des_ctx = des_init(des_ctx, des_cctx);
121 return des_crypt(des_ctx, xzalloc(DES_OUT_BUFSIZE), (unsigned char*)key, (unsigned char*)salt);
124 /* So far nobody wants to have it public */
125 static void my_crypt_cleanup(void)
133 char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
137 encrypted = my_crypt(clear, salt);
145 #else /* if !ENABLE_USE_BB_CRYPT */
147 char* FAST_FUNC pw_encrypt(const char *clear, const char *salt, int cleanup)
151 s = crypt(clear, salt);
153 * glibc used to return "" on malformed salts (for example, ""),
154 * but since 2.17 it returns NULL.
156 return xstrdup(s ? s : "");