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 * ----------------------------------------------------------------------------
14 // Modularized by Brandon Gillespie
17 #if defined(LIBC_SCCS) && !defined(lint)
18 static char rcsid[] = "$Header: /home/ncvs/src/lib/libcrypt/crypt.c,v 1.4 1996/07/12 18:56:01 jkh Exp $";
19 #endif /* LIBC_SCCS and not lint */
29 crypt_md5(pw, pl, sp, sl, passwd, token)
30 register const unsigned char *pw;
31 const unsigned int pl;
32 register const unsigned char *sp;
33 const unsigned int sl;
38 unsigned char final[_MD5_SIZE];
45 /* The password first, since that is what is most unknown */
46 MD5Update(&ctx,pw,pl);
48 /* Then our magic string */
49 MD5Update(&ctx,(unsigned char *)token,strlen(token));
51 /* Then the raw salt */
52 MD5Update(&ctx,sp,sl);
54 /* Then just as many characters of the MD5(pw,salt,pw) */
56 MD5Update(&ctx1,pw,pl);
57 MD5Update(&ctx1,sp,sl);
58 MD5Update(&ctx1,pw,pl);
59 MD5Final(final,&ctx1);
60 for(i = pl; i > 0; i -= 16)
61 MD5Update(&ctx,final,i>16 ? 16 : i);
63 /* Don't leave anything around in vm they could use. */
64 memset(final,0,sizeof final);
66 /* Then something really weird... */
67 for (j=0,i = pl; i ; i >>= 1)
69 MD5Update(&ctx, final+j, 1);
71 MD5Update(&ctx, pw+j, 1);
73 /* Now make the output string */
74 strcpy(passwd, token);
75 strncat(passwd, (char *)sp, (int)sl);
81 * and now, just to make sure things don't run too fast
82 * On a 60 Mhz Pentium this takes 34 msec, so you would
83 * need 30 seconds to build a 1000 entry dictionary...
88 MD5Update(&ctx1,pw,pl);
90 MD5Update(&ctx1,final,_MD5_SIZE);
93 MD5Update(&ctx1,sp,sl);
96 MD5Update(&ctx1,pw,pl);
99 MD5Update(&ctx1,final,_MD5_SIZE);
101 MD5Update(&ctx1,pw,pl);
102 MD5Final(final,&ctx1);
105 p = passwd + strlen(passwd);
107 l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
108 l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
109 l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
110 l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
111 l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
112 l = final[11] ; to64(p,l,2); p += 2;
115 /* Don't leave anything around in vm they could use. */
116 memset(final,0,sizeof final);