diff options
Diffstat (limited to 'src/lib/libc/crypt/md5crypt.c')
-rw-r--r-- | src/lib/libc/crypt/md5crypt.c | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/lib/libc/crypt/md5crypt.c b/src/lib/libc/crypt/md5crypt.c new file mode 100644 index 0000000000..b32cd2115f --- /dev/null +++ b/src/lib/libc/crypt/md5crypt.c | |||
@@ -0,0 +1,149 @@ | |||
1 | /* $OpenBSD: md5crypt.c,v 1.15 2009/10/31 13:29:07 sobrado Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ---------------------------------------------------------------------------- | ||
5 | * "THE BEER-WARE LICENSE" (Revision 42): | ||
6 | * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you | ||
7 | * can do whatever you want with this stuff. If we meet some day, and you think | ||
8 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp | ||
9 | * ---------------------------------------------------------------------------- | ||
10 | * | ||
11 | * $FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp $ | ||
12 | * | ||
13 | */ | ||
14 | |||
15 | #include <unistd.h> | ||
16 | #include <stdio.h> | ||
17 | #include <string.h> | ||
18 | #include <md5.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ | ||
22 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | ||
23 | |||
24 | static void to64(char *, u_int32_t, int); | ||
25 | |||
26 | static void | ||
27 | to64(char *s, u_int32_t v, int n) | ||
28 | { | ||
29 | while (--n >= 0) { | ||
30 | *s++ = itoa64[v&0x3f]; | ||
31 | v >>= 6; | ||
32 | } | ||
33 | } | ||
34 | |||
35 | /* | ||
36 | * UNIX password | ||
37 | * | ||
38 | * Use MD5 for what it is best at... | ||
39 | */ | ||
40 | |||
41 | char *md5crypt(const char *pw, const char *salt); | ||
42 | |||
43 | char * | ||
44 | md5crypt(const char *pw, const char *salt) | ||
45 | { | ||
46 | /* | ||
47 | * This string is magic for this algorithm. Having | ||
48 | * it this way, we can get get better later on | ||
49 | */ | ||
50 | static unsigned char *magic = (unsigned char *)"$1$"; | ||
51 | |||
52 | static char passwd[120], *p; | ||
53 | static const unsigned char *sp,*ep; | ||
54 | unsigned char final[16]; | ||
55 | int sl,pl,i; | ||
56 | MD5_CTX ctx,ctx1; | ||
57 | u_int32_t l; | ||
58 | |||
59 | /* Refine the Salt first */ | ||
60 | sp = (const unsigned char *)salt; | ||
61 | |||
62 | /* If it starts with the magic string, then skip that */ | ||
63 | if(!strncmp((const char *)sp,(const char *)magic,strlen((const char *)magic))) | ||
64 | sp += strlen((const char *)magic); | ||
65 | |||
66 | /* It stops at the first '$', max 8 chars */ | ||
67 | for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) | ||
68 | continue; | ||
69 | |||
70 | /* get the length of the true salt */ | ||
71 | sl = ep - sp; | ||
72 | |||
73 | MD5Init(&ctx); | ||
74 | |||
75 | /* The password first, since that is what is most unknown */ | ||
76 | MD5Update(&ctx,(const unsigned char *)pw,strlen(pw)); | ||
77 | |||
78 | /* Then our magic string */ | ||
79 | MD5Update(&ctx,magic,strlen((const char *)magic)); | ||
80 | |||
81 | /* Then the raw salt */ | ||
82 | MD5Update(&ctx,sp,sl); | ||
83 | |||
84 | /* Then just as many characters of the MD5(pw,salt,pw) */ | ||
85 | MD5Init(&ctx1); | ||
86 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
87 | MD5Update(&ctx1,sp,sl); | ||
88 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
89 | MD5Final(final,&ctx1); | ||
90 | for(pl = strlen(pw); pl > 0; pl -= 16) | ||
91 | MD5Update(&ctx,final,pl>16 ? 16 : pl); | ||
92 | |||
93 | /* Don't leave anything around in vm they could use. */ | ||
94 | memset(final,0,sizeof final); | ||
95 | |||
96 | /* Then something really weird... */ | ||
97 | for (i = strlen(pw); i ; i >>= 1) | ||
98 | if(i&1) | ||
99 | MD5Update(&ctx, final, 1); | ||
100 | else | ||
101 | MD5Update(&ctx, (const unsigned char *)pw, 1); | ||
102 | |||
103 | /* Now make the output string */ | ||
104 | snprintf(passwd, sizeof(passwd), "%s%.*s$", (char *)magic, | ||
105 | sl, (const char *)sp); | ||
106 | |||
107 | MD5Final(final,&ctx); | ||
108 | |||
109 | /* | ||
110 | * and now, just to make sure things don't run too fast | ||
111 | * On a 60 MHz Pentium this takes 34 msec, so you would | ||
112 | * need 30 seconds to build a 1000 entry dictionary... | ||
113 | */ | ||
114 | for(i=0;i<1000;i++) { | ||
115 | MD5Init(&ctx1); | ||
116 | if(i & 1) | ||
117 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
118 | else | ||
119 | MD5Update(&ctx1,final,16); | ||
120 | |||
121 | if(i % 3) | ||
122 | MD5Update(&ctx1,sp,sl); | ||
123 | |||
124 | if(i % 7) | ||
125 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
126 | |||
127 | if(i & 1) | ||
128 | MD5Update(&ctx1,final,16); | ||
129 | else | ||
130 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
131 | MD5Final(final,&ctx1); | ||
132 | } | ||
133 | |||
134 | p = passwd + strlen(passwd); | ||
135 | |||
136 | l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4; | ||
137 | l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4; | ||
138 | l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4; | ||
139 | l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4; | ||
140 | l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4; | ||
141 | l = final[11] ; to64(p,l,2); p += 2; | ||
142 | *p = '\0'; | ||
143 | |||
144 | /* Don't leave anything around in vm they could use. */ | ||
145 | memset(final, 0, sizeof final); | ||
146 | |||
147 | return passwd; | ||
148 | } | ||
149 | |||