]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/auth/basic/NCSA/crypt_md5.cc
2 * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * Shamelessly stolen from linux-pam, and adopted to work with
11 * OpenSSL md5 implementation and any magic string
13 * Origin2: md5_crypt.c,v 1.1.1.1 2000/01/03 17:34:46 gafton Exp
15 * ----------------------------------------------------------------------------
16 * "THE BEER-WARE LICENSE" (Revision 42):
17 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
18 * can do whatever you want with this stuff. If we meet some day, and you think
19 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
20 * ----------------------------------------------------------------------------
22 * Origin: Id: crypt.c,v 1.3 1995/05/30 05:42:22 rgrimes Exp
27 #include "auth/basic/NCSA/crypt_md5.h"
32 static unsigned char itoa64
[] = /* 0 ... 63 => ascii - 64 */
33 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
35 static void md5to64(char *s
, unsigned long v
, int n
)
38 *s
= itoa64
[v
& 0x3f];
47 * Use MD5 for what it is best at...
49 * If salt begins with $ then it is assumed to be on the form
51 * If not the normal UNIX magic $1$ is used.
54 char *crypt_md5(const char *pw
, const char *salt
)
56 const char *magic
= "$1$";
58 static char passwd
[120], *p
;
59 static const char *sp
, *ep
;
60 unsigned char final
[16];
62 SquidMD5_CTX ctx
, ctx1
;
68 while (*salt
&& *salt
!= '$')
72 magiclen
= salt
- magic
;
79 /* Refine the Salt first */
82 /* It stops at the first '$', max 8 chars */
83 for (ep
= sp
; *ep
&& *ep
!= '$' && ep
< (sp
+ 8); ++ep
)
86 /* get the length of the true salt */
91 /* The password first, since that is what is most unknown */
92 SquidMD5Update(&ctx
, (unsigned const char *) pw
, strlen(pw
));
94 /* Then our magic string */
95 SquidMD5Update(&ctx
, (unsigned const char *) magic
, magiclen
);
97 /* Then the raw salt */
98 SquidMD5Update(&ctx
, (unsigned const char *) sp
, sl
);
100 /* Then just as many characters of the MD5(pw,salt,pw) */
102 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
103 SquidMD5Update(&ctx1
, (unsigned const char *) sp
, sl
);
104 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
105 SquidMD5Final(final
, &ctx1
);
106 for (pl
= strlen(pw
); pl
> 0; pl
-= 16)
107 SquidMD5Update(&ctx
, (unsigned const char *) final
, pl
> 16 ? 16 : pl
);
109 /* Don't leave anything around in vm they could use. */
110 memset(final
, 0, sizeof final
);
112 /* Then something really weird... */
113 for (j
= 0, i
= strlen(pw
); i
; i
>>= 1)
115 SquidMD5Update(&ctx
, (unsigned const char *) final
+ j
, 1);
117 SquidMD5Update(&ctx
, (unsigned const char *) pw
+ j
, 1);
119 /* Now make the output string */
120 memset(passwd
, 0, sizeof(passwd
));
121 strncat(passwd
, magic
, magiclen
);
122 strncat(passwd
, sp
, sl
);
125 SquidMD5Final(final
, &ctx
);
128 * and now, just to make sure things don't run too fast
129 * On a 60 Mhz Pentium this takes 34 msec, so you would
130 * need 30 seconds to build a 1000 entry dictionary...
132 for (i
= 0; i
< 1000; ++i
) {
135 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
137 SquidMD5Update(&ctx1
, (unsigned const char *) final
, 16);
140 SquidMD5Update(&ctx1
, (unsigned const char *) sp
, sl
);
143 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
146 SquidMD5Update(&ctx1
, (unsigned const char *) final
, 16);
148 SquidMD5Update(&ctx1
, (unsigned const char *) pw
, strlen(pw
));
149 SquidMD5Final(final
, &ctx1
);
152 p
= passwd
+ strlen(passwd
);
154 l
= (final
[0] << 16) | (final
[6] << 8) | final
[12];
157 l
= (final
[1] << 16) | (final
[7] << 8) | final
[13];
160 l
= (final
[2] << 16) | (final
[8] << 8) | final
[14];
163 l
= (final
[3] << 16) | (final
[9] << 8) | final
[15];
166 l
= (final
[4] << 16) | (final
[10] << 8) | final
[5];
174 /* Don't leave anything around in vm they could use. */
175 memset(final
, 0, sizeof final
);
180 /* Created by Ramon de Carvalho <ramondecarvalho@yahoo.com.br>
181 Refined by Rodrigo Rubira Branco <rodrigo@kernelhacking.com>
183 char *md5sum(const char *s
)
185 static unsigned char digest
[16];
193 SquidMD5Update(&ctx
,(const unsigned char *)s
,strlen(s
));
194 SquidMD5Final(digest
,&ctx
);
196 for (idx
=0; idx
<16; ++idx
)
197 snprintf(&sum
[idx
*2],(33-(idx
*2)),"%02x",digest
[idx
]);
201 /* Don't leave anything around in vm they could use. */
202 memset(digest
, 0, sizeof digest
);