1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
20 static int passwd_compare_sha1(const char *passwd
, const char *entry
)
24 unsigned char sha1
[20], pwdsha1
[20];
31 if ((p
= strchr(passwd
+ 3, '$'))) {
32 SHA1Update(&d
.ctx
, (void *)passwd
+ 3, p
- (passwd
+ 3));
35 p
= passwd
+ 3; /* Assume no salt */
38 SHA1Update(&d
.ctx
, (void *)entry
, strlen(entry
));
39 SHA1Final(d
.sha1
, &d
.ctx
);
41 memset(d
.pwdsha1
, 0, 20);
42 unbase64(d
.pwdsha1
, 20, p
);
44 rv
= !memcmp(d
.sha1
, d
.pwdsha1
, 20);
46 memset(&d
, 0, sizeof d
);
50 static int passwd_compare_md5(const char *passwd
, const char *entry
)
52 const char *crypted
= crypt_md5(entry
, passwd
+ 3);
53 int len
= strlen(crypted
);
55 return !strncmp(crypted
, passwd
, len
) &&
56 (passwd
[len
] == '\0' || passwd
[len
] == '$');
59 static int passwd_compare_sha256(const char *passwd
, const char *entry
)
61 const char *crypted
= sha256_crypt(entry
, passwd
+ 3);
62 int len
= strlen(crypted
);
64 return !strncmp(crypted
, passwd
, len
) &&
65 (passwd
[len
] == '\0' || passwd
[len
] == '$');
68 static int passwd_compare_sha512(const char *passwd
, const char *entry
)
70 const char *crypted
= sha512_crypt(entry
, passwd
+ 3);
71 int len
= strlen(crypted
);
73 return !strncmp(crypted
, passwd
, len
) &&
74 (passwd
[len
] == '\0' || passwd
[len
] == '$');
77 int passwd_compare(const char *passwd
, const char *entry
)
79 if (passwd
[0] != '$' || !passwd
[1] || passwd
[2] != '$') {
80 /* Plaintext passwd, yuck! */
81 return !strcmp(entry
, passwd
);
85 return passwd_compare_md5(passwd
, entry
);
87 return passwd_compare_sha1(passwd
, entry
);
89 return passwd_compare_sha256(passwd
, entry
);
91 return passwd_compare_sha512(passwd
, entry
);
93 return 0; /* Unknown encryption algorithm -> false */