1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*
* passlib/src/md5crypt.c - md5-crypt & apr-md5-crypt encryption routines
*
* this file is a direct copy of FreeBSD's md5crypt.c,
* taken from http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/lib/libcrypt/crypt.c?rev=1.2
* the following changes were made from the original:
* - mild refactoring of frontend functions
* - changed to use C99 types
*
* ============================================================================
*
* "THE BEER-WARE LICENSE" (Revision 42):
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
*
*/
#include <string.h>
#include <stdint.h>
#include <openssl/md5.h>
#include "h64.h"
#include "md5crypt.h"
#define to64 h64_encode_from_long
int md5_crypt(char *outbuf,
char *passwd,
char *salt,
char *magic)
{
/* NOTE: 'magic' should be '$1$' for md5 crypt,
* and '$apr1$' for apache md5 crypt
*
* NOTE: outbuf must have at least 23 bytes alloc'd
* it will be filled with the encoded checksum ONLY
*
* returns 1 on error, 0 on success
*/
MD5_CTX ctx;
uint8_t tmpbuf[MD5_DIGEST_LENGTH];
char *p;
int i;
unsigned long l;
int pwdlen = strlen(passwd);
int saltlen = strlen(salt);
/* Prepare tmp digest MD5(pw,salt,pw) */
MD5_Init(&ctx);
MD5_Update(&ctx, passwd, pwdlen);
MD5_Update(&ctx, salt, saltlen);
MD5_Update(&ctx, passwd, pwdlen);
MD5_Final(tmpbuf, &ctx);
/* start real digest */
MD5_Init(&ctx);
/* The password first, since that is what is most unknown */
MD5_Update(&ctx, passwd, pwdlen);
/* Then our magic string */
MD5_Update(&ctx, magic, strlen(magic));
/* Then the raw salt */
MD5_Update(&ctx, salt, saltlen);
/* then tmp digest above, repeated to match pwd size */
for(i = pwdlen; i > 0; i -= MD5_DIGEST_LENGTH)
MD5_Update(&ctx, tmpbuf, i>MD5_DIGEST_LENGTH ? MD5_DIGEST_LENGTH : i);
/* Then something really weird... */
/* NOTE: original code has a more complex alg
which was probably meant to be stronger,
but had some typos, so it's effectively this...
*/
tmpbuf[0] = '\0';
for (i = pwdlen; i ; i >>= 1)
if(i&1)
MD5_Update(&ctx, tmpbuf, 1);
else
MD5_Update(&ctx, passwd, 1);
/* Now make the output string */
MD5_Final(tmpbuf,&ctx);
/*
* and now, just to make sure things don't run too fast
* On a 60 Mhz Pentium this takes 34 msec, so you would
* need 30 seconds to build a 1000 entry dictionary...
*/
for(i=0;i<1000;i++) {
MD5_Init(&ctx);
if(i & 1)
MD5_Update(&ctx,passwd,pwdlen);
else
MD5_Update(&ctx,tmpbuf, MD5_DIGEST_LENGTH);
if(i % 3)
MD5_Update(&ctx,salt,saltlen);
if(i % 7)
MD5_Update(&ctx,passwd,pwdlen);
if(i & 1)
MD5_Update(&ctx,tmpbuf, MD5_DIGEST_LENGTH);
else
MD5_Update(&ctx,passwd,pwdlen);
MD5_Final(tmpbuf,&ctx);
}
p = outbuf;
l = (tmpbuf[ 0]<<16) | (tmpbuf[ 6]<<8) | tmpbuf[12]; to64(p,l,4); p += 4;
l = (tmpbuf[ 1]<<16) | (tmpbuf[ 7]<<8) | tmpbuf[13]; to64(p,l,4); p += 4;
l = (tmpbuf[ 2]<<16) | (tmpbuf[ 8]<<8) | tmpbuf[14]; to64(p,l,4); p += 4;
l = (tmpbuf[ 3]<<16) | (tmpbuf[ 9]<<8) | tmpbuf[15]; to64(p,l,4); p += 4;
l = (tmpbuf[ 4]<<16) | (tmpbuf[10]<<8) | tmpbuf[ 5]; to64(p,l,4); p += 4;
l = tmpbuf[11] ; to64(p,l,2); p += 2;
*p = '\0';
/* Don't leave anything around in vm they could use. */
memset(tmpbuf, 0, sizeof(tmpbuf));
memset(&ctx, 0, sizeof(ctx));
return 0;
}
|