2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.5.2.1 2001/05/24 12:20:02 markm Exp $
10 * $DragonFly: src/lib/libcrypt/crypt-md5.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
12 * $FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.5.2.1 2001/05/24 12:20:02 markm Exp $
31 static char *magic = "$1$"; /*
32 * This string is magic for
33 * this algorithm. Having
34 * it this way, we can get
37 static char passwd[120], *p;
38 static const char *sp,*ep;
39 unsigned char final[MD5_SIZE];
44 /* Refine the Salt first */
47 /* If it starts with the magic string, then skip that */
48 if(!strncmp(sp,magic,strlen(magic)))
51 /* It stops at the first '$', max 8 chars */
52 for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
55 /* get the length of the true salt */
60 /* The password first, since that is what is most unknown */
61 MD5Update(&ctx,pw,strlen(pw));
63 /* Then our magic string */
64 MD5Update(&ctx,magic,strlen(magic));
66 /* Then the raw salt */
67 MD5Update(&ctx,sp,sl);
69 /* Then just as many characters of the MD5(pw,salt,pw) */
71 MD5Update(&ctx1,pw,strlen(pw));
72 MD5Update(&ctx1,sp,sl);
73 MD5Update(&ctx1,pw,strlen(pw));
74 MD5Final(final,&ctx1);
75 for(pl = strlen(pw); pl > 0; pl -= MD5_SIZE)
76 MD5Update(&ctx,final,pl>MD5_SIZE ? MD5_SIZE : pl);
78 /* Don't leave anything around in vm they could use. */
79 memset(final,0,sizeof final);
81 /* Then something really weird... */
82 for (i = strlen(pw); i ; i >>= 1)
84 MD5Update(&ctx, final, 1);
86 MD5Update(&ctx, pw, 1);
88 /* Now make the output string */
90 strncat(passwd,sp,sl);
96 * and now, just to make sure things don't run too fast
97 * On a 60 Mhz Pentium this takes 34 msec, so you would
98 * need 30 seconds to build a 1000 entry dictionary...
100 for(i=0;i<1000;i++) {
103 MD5Update(&ctx1,pw,strlen(pw));
105 MD5Update(&ctx1,final,MD5_SIZE);
108 MD5Update(&ctx1,sp,sl);
111 MD5Update(&ctx1,pw,strlen(pw));
114 MD5Update(&ctx1,final,MD5_SIZE);
116 MD5Update(&ctx1,pw,strlen(pw));
117 MD5Final(final,&ctx1);
120 p = passwd + strlen(passwd);
122 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];
123 _crypt_to64(p,l,4); p += 4;
124 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];
125 _crypt_to64(p,l,4); p += 4;
126 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];
127 _crypt_to64(p,l,4); p += 4;
128 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];
129 _crypt_to64(p,l,4); p += 4;
130 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];
131 _crypt_to64(p,l,4); p += 4;
133 _crypt_to64(p,l,2); p += 2;
136 /* Don't leave anything around in vm they could use. */
137 memset(final,0,sizeof final);