1d8e82636SMark Murray /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 35e53a4f9SPedro F. Giffuni * 4d8e82636SMark Murray * Copyright (c) 2003 Poul-Henning Kamp 5d8e82636SMark Murray * All rights reserved. 6d8e82636SMark Murray * 7d8e82636SMark Murray * Redistribution and use in source and binary forms, with or without 8d8e82636SMark Murray * modification, are permitted provided that the following conditions 9d8e82636SMark Murray * are met: 10d8e82636SMark Murray * 1. Redistributions of source code must retain the above copyright 11d8e82636SMark Murray * notice, this list of conditions and the following disclaimer. 12d8e82636SMark Murray * 2. Redistributions in binary form must reproduce the above copyright 13d8e82636SMark Murray * notice, this list of conditions and the following disclaimer in the 14d8e82636SMark Murray * documentation and/or other materials provided with the distribution. 15d8e82636SMark Murray * 16d8e82636SMark Murray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17d8e82636SMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18d8e82636SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19d8e82636SMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20d8e82636SMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21d8e82636SMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22d8e82636SMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23d8e82636SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24d8e82636SMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25d8e82636SMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26d8e82636SMark Murray * SUCH DAMAGE. 27e9a56ad5SMark Murray */ 28e9a56ad5SMark Murray 29d8e82636SMark Murray #include <sys/types.h> 30d8e82636SMark Murray 31d8e82636SMark Murray #include <err.h> 32d8e82636SMark Murray #include <md5.h> 33e9a56ad5SMark Murray #include <stdio.h> 34e9a56ad5SMark Murray #include <string.h> 35*a2c0d202SRobert Clausecker #include <strings.h> 36d8e82636SMark Murray #include <unistd.h> 37d8e82636SMark Murray 38e9a56ad5SMark Murray #include "crypt.h" 39e9a56ad5SMark Murray 40e9a56ad5SMark Murray /* 41e9a56ad5SMark Murray * UNIX password 42e9a56ad5SMark Murray */ 43e9a56ad5SMark Murray 445f521d7bSEd Schouten int 455f521d7bSEd Schouten crypt_md5(const char *pw, const char *salt, char *buffer) 46e9a56ad5SMark Murray { 47f2ac424aSMark Murray MD5_CTX ctx,ctx1; 48f2ac424aSMark Murray unsigned long l; 49bbf177c2SMark Murray int sl, pl; 50bbf177c2SMark Murray u_int i; 51f2ac424aSMark Murray u_char final[MD5_SIZE]; 525f521d7bSEd Schouten const char *ep; 53d8e82636SMark Murray static const char *magic = "$1$"; 54e9a56ad5SMark Murray 555f521d7bSEd Schouten /* If the salt starts with the magic string, skip that. */ 565f521d7bSEd Schouten if (!strncmp(salt, magic, strlen(magic))) 575f521d7bSEd Schouten salt += strlen(magic); 58e9a56ad5SMark Murray 59e9a56ad5SMark Murray /* It stops at the first '$', max 8 chars */ 605f521d7bSEd Schouten for (ep = salt; *ep && *ep != '$' && ep < salt + 8; ep++) 61e9a56ad5SMark Murray continue; 62e9a56ad5SMark Murray 63e9a56ad5SMark Murray /* get the length of the true salt */ 645f521d7bSEd Schouten sl = ep - salt; 65e9a56ad5SMark Murray 66e9a56ad5SMark Murray MD5Init(&ctx); 67e9a56ad5SMark Murray 68e9a56ad5SMark Murray /* The password first, since that is what is most unknown */ 69f2ac424aSMark Murray MD5Update(&ctx, (const u_char *)pw, strlen(pw)); 70e9a56ad5SMark Murray 71e9a56ad5SMark Murray /* Then our magic string */ 72f2ac424aSMark Murray MD5Update(&ctx, (const u_char *)magic, strlen(magic)); 73e9a56ad5SMark Murray 74e9a56ad5SMark Murray /* Then the raw salt */ 755f521d7bSEd Schouten MD5Update(&ctx, (const u_char *)salt, (u_int)sl); 76e9a56ad5SMark Murray 77e9a56ad5SMark Murray /* Then just as many characters of the MD5(pw,salt,pw) */ 78e9a56ad5SMark Murray MD5Init(&ctx1); 79f2ac424aSMark Murray MD5Update(&ctx1, (const u_char *)pw, strlen(pw)); 805f521d7bSEd Schouten MD5Update(&ctx1, (const u_char *)salt, (u_int)sl); 81f2ac424aSMark Murray MD5Update(&ctx1, (const u_char *)pw, strlen(pw)); 82e9a56ad5SMark Murray MD5Final(final, &ctx1); 83bbf177c2SMark Murray for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE) 84f2ac424aSMark Murray MD5Update(&ctx, (const u_char *)final, 85bbf177c2SMark Murray (u_int)(pl > MD5_SIZE ? MD5_SIZE : pl)); 86e9a56ad5SMark Murray 87e9a56ad5SMark Murray /* Don't leave anything around in vm they could use. */ 88*a2c0d202SRobert Clausecker explicit_bzero(final, sizeof(final)); 89e9a56ad5SMark Murray 90e9a56ad5SMark Murray /* Then something really weird... */ 91e9a56ad5SMark Murray for (i = strlen(pw); i; i >>= 1) 92e9a56ad5SMark Murray if(i & 1) 93f2ac424aSMark Murray MD5Update(&ctx, (const u_char *)final, 1); 94e9a56ad5SMark Murray else 95f2ac424aSMark Murray MD5Update(&ctx, (const u_char *)pw, 1); 96e9a56ad5SMark Murray 97e9a56ad5SMark Murray /* Now make the output string */ 985f521d7bSEd Schouten buffer = stpcpy(buffer, magic); 995f521d7bSEd Schouten buffer = stpncpy(buffer, salt, (u_int)sl); 1005f521d7bSEd Schouten *buffer++ = '$'; 101e9a56ad5SMark Murray 102e9a56ad5SMark Murray MD5Final(final, &ctx); 103e9a56ad5SMark Murray 104e9a56ad5SMark Murray /* 105e9a56ad5SMark Murray * and now, just to make sure things don't run too fast 106e9a56ad5SMark Murray * On a 60 Mhz Pentium this takes 34 msec, so you would 107e9a56ad5SMark Murray * need 30 seconds to build a 1000 entry dictionary... 108e9a56ad5SMark Murray */ 109e9a56ad5SMark Murray for(i = 0; i < 1000; i++) { 110e9a56ad5SMark Murray MD5Init(&ctx1); 111e9a56ad5SMark Murray if(i & 1) 112f2ac424aSMark Murray MD5Update(&ctx1, (const u_char *)pw, strlen(pw)); 113e9a56ad5SMark Murray else 114f2ac424aSMark Murray MD5Update(&ctx1, (const u_char *)final, MD5_SIZE); 115e9a56ad5SMark Murray 116e9a56ad5SMark Murray if(i % 3) 1175f521d7bSEd Schouten MD5Update(&ctx1, (const u_char *)salt, (u_int)sl); 118e9a56ad5SMark Murray 119e9a56ad5SMark Murray if(i % 7) 120f2ac424aSMark Murray MD5Update(&ctx1, (const u_char *)pw, strlen(pw)); 121e9a56ad5SMark Murray 122e9a56ad5SMark Murray if(i & 1) 123f2ac424aSMark Murray MD5Update(&ctx1, (const u_char *)final, MD5_SIZE); 124e9a56ad5SMark Murray else 125f2ac424aSMark Murray MD5Update(&ctx1, (const u_char *)pw, strlen(pw)); 126e9a56ad5SMark Murray MD5Final(final, &ctx1); 127e9a56ad5SMark Murray } 128e9a56ad5SMark Murray 129e9a56ad5SMark Murray l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; 1305f521d7bSEd Schouten _crypt_to64(buffer, l, 4); buffer += 4; 131e9a56ad5SMark Murray l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; 1325f521d7bSEd Schouten _crypt_to64(buffer, l, 4); buffer += 4; 133e9a56ad5SMark Murray l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; 1345f521d7bSEd Schouten _crypt_to64(buffer, l, 4); buffer += 4; 135e9a56ad5SMark Murray l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; 1365f521d7bSEd Schouten _crypt_to64(buffer, l, 4); buffer += 4; 137e9a56ad5SMark Murray l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; 1385f521d7bSEd Schouten _crypt_to64(buffer, l, 4); buffer += 4; 139e9a56ad5SMark Murray l = final[11]; 1405f521d7bSEd Schouten _crypt_to64(buffer, l, 2); buffer += 2; 1415f521d7bSEd Schouten *buffer = '\0'; 142e9a56ad5SMark Murray 143e9a56ad5SMark Murray /* Don't leave anything around in vm they could use. */ 144*a2c0d202SRobert Clausecker explicit_bzero(final, sizeof(final)); 145e9a56ad5SMark Murray 1465f521d7bSEd Schouten return (0); 147e9a56ad5SMark Murray } 148