/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2003 Poul-Henning Kamp4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/types.h>2930#include <err.h>31#include <md5.h>32#include <stdio.h>33#include <string.h>34#include <strings.h>35#include <unistd.h>3637#include "crypt.h"3839/*40* UNIX password41*/4243int44crypt_md5(const char *pw, const char *salt, char *buffer)45{46MD5_CTX ctx,ctx1;47unsigned long l;48int sl, pl;49u_int i;50u_char final[MD5_SIZE];51const char *ep;52static const char *magic = "$1$";5354/* If the salt starts with the magic string, skip that. */55if (!strncmp(salt, magic, strlen(magic)))56salt += strlen(magic);5758/* It stops at the first '$', max 8 chars */59for (ep = salt; *ep && *ep != '$' && ep < salt + 8; ep++)60continue;6162/* get the length of the true salt */63sl = ep - salt;6465MD5Init(&ctx);6667/* The password first, since that is what is most unknown */68MD5Update(&ctx, (const u_char *)pw, strlen(pw));6970/* Then our magic string */71MD5Update(&ctx, (const u_char *)magic, strlen(magic));7273/* Then the raw salt */74MD5Update(&ctx, (const u_char *)salt, (u_int)sl);7576/* Then just as many characters of the MD5(pw,salt,pw) */77MD5Init(&ctx1);78MD5Update(&ctx1, (const u_char *)pw, strlen(pw));79MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);80MD5Update(&ctx1, (const u_char *)pw, strlen(pw));81MD5Final(final, &ctx1);82for(pl = (int)strlen(pw); pl > 0; pl -= MD5_SIZE)83MD5Update(&ctx, (const u_char *)final,84(u_int)(pl > MD5_SIZE ? MD5_SIZE : pl));8586/* Don't leave anything around in vm they could use. */87explicit_bzero(final, sizeof(final));8889/* Then something really weird... */90for (i = strlen(pw); i; i >>= 1)91if(i & 1)92MD5Update(&ctx, (const u_char *)final, 1);93else94MD5Update(&ctx, (const u_char *)pw, 1);9596/* Now make the output string */97buffer = stpcpy(buffer, magic);98buffer = stpncpy(buffer, salt, (u_int)sl);99*buffer++ = '$';100101MD5Final(final, &ctx);102103/*104* and now, just to make sure things don't run too fast105* On a 60 Mhz Pentium this takes 34 msec, so you would106* need 30 seconds to build a 1000 entry dictionary...107*/108for(i = 0; i < 1000; i++) {109MD5Init(&ctx1);110if(i & 1)111MD5Update(&ctx1, (const u_char *)pw, strlen(pw));112else113MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);114115if(i % 3)116MD5Update(&ctx1, (const u_char *)salt, (u_int)sl);117118if(i % 7)119MD5Update(&ctx1, (const u_char *)pw, strlen(pw));120121if(i & 1)122MD5Update(&ctx1, (const u_char *)final, MD5_SIZE);123else124MD5Update(&ctx1, (const u_char *)pw, strlen(pw));125MD5Final(final, &ctx1);126}127128l = (final[ 0]<<16) | (final[ 6]<<8) | final[12];129_crypt_to64(buffer, l, 4); buffer += 4;130l = (final[ 1]<<16) | (final[ 7]<<8) | final[13];131_crypt_to64(buffer, l, 4); buffer += 4;132l = (final[ 2]<<16) | (final[ 8]<<8) | final[14];133_crypt_to64(buffer, l, 4); buffer += 4;134l = (final[ 3]<<16) | (final[ 9]<<8) | final[15];135_crypt_to64(buffer, l, 4); buffer += 4;136l = (final[ 4]<<16) | (final[10]<<8) | final[ 5];137_crypt_to64(buffer, l, 4); buffer += 4;138l = final[11];139_crypt_to64(buffer, l, 2); buffer += 2;140*buffer = '\0';141142/* Don't leave anything around in vm they could use. */143explicit_bzero(final, sizeof(final));144145return (0);146}147148149