Home | History | Annotate | Line # | Download | only in libcrypt
md5crypt.c revision 1.10
      1  1.10  drochner /*	$NetBSD: md5crypt.c,v 1.10 2011/11/29 13:18:52 drochner Exp $	*/
      2   1.1        ad 
      3   1.1        ad /*
      4   1.1        ad  * ----------------------------------------------------------------------------
      5   1.1        ad  * "THE BEER-WARE LICENSE" (Revision 42):
      6   1.1        ad  * <phk (at) login.dknet.dk> wrote this file.  As long as you retain this notice you
      7   1.1        ad  * can do whatever you want with this stuff. If we meet some day, and you think
      8   1.1        ad  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
      9   1.1        ad  * ----------------------------------------------------------------------------
     10   1.1        ad  *
     11   1.1        ad  * from FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp
     12   1.1        ad  * via OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp
     13   1.1        ad  *
     14   1.1        ad  */
     15   1.1        ad 
     16   1.1        ad #include <sys/cdefs.h>
     17   1.1        ad #if !defined(lint)
     18  1.10  drochner __RCSID("$NetBSD: md5crypt.c,v 1.10 2011/11/29 13:18:52 drochner Exp $");
     19   1.1        ad #endif /* not lint */
     20   1.1        ad 
     21   1.5   thorpej /*
     22   1.5   thorpej  * NOTE: We are also built for inclusion in libcrypto; when built for that
     23   1.5   thorpej  * environment, use the libcrypto versions of the MD5 routines, so save
     24   1.5   thorpej  * having to pull two versions into the same program.
     25   1.5   thorpej  */
     26   1.5   thorpej 
     27   1.1        ad #include <unistd.h>
     28   1.1        ad #include <stdio.h>
     29   1.1        ad #include <string.h>
     30   1.5   thorpej #ifdef libcrypto
     31   1.5   thorpej #include <openssl/md5.h>
     32   1.5   thorpej #else
     33   1.1        ad #include <md5.h>
     34   1.5   thorpej #endif
     35   1.1        ad 
     36   1.8       sjg #include "crypt.h"
     37   1.8       sjg 
     38   1.1        ad #define MD5_MAGIC	"$1$"
     39   1.1        ad #define MD5_MAGIC_LEN	3
     40   1.1        ad 
     41   1.5   thorpej #ifdef libcrypto
     42   1.5   thorpej #define	INIT(x)			MD5_Init((x))
     43   1.5   thorpej #define	UPDATE(x, b, l)		MD5_Update((x), (b), (l))
     44   1.5   thorpej #define	FINAL(v, x)		MD5_Final((v), (x))
     45   1.5   thorpej #else
     46   1.5   thorpej #define	INIT(x)			MD5Init((x))
     47   1.5   thorpej #define	UPDATE(x, b, l)		MD5Update((x), (b), (l))
     48   1.5   thorpej #define	FINAL(v, x)		MD5Final((v), (x))
     49   1.5   thorpej #endif
     50   1.1        ad 
     51   1.1        ad 
     52   1.1        ad /*
     53   1.1        ad  * MD5 password encryption.
     54   1.1        ad  */
     55   1.1        ad char *
     56   1.1        ad __md5crypt(const char *pw, const char *salt)
     57   1.1        ad {
     58   1.1        ad 	static char passwd[120], *p;
     59   1.1        ad 	const char *sp, *ep;
     60   1.1        ad 	unsigned char final[16];
     61   1.1        ad 	unsigned int i, sl, pwl;
     62   1.1        ad 	MD5_CTX	ctx, ctx1;
     63   1.1        ad 	u_int32_t l;
     64   1.1        ad 	int pl;
     65   1.1        ad 
     66   1.1        ad 	pwl = strlen(pw);
     67   1.1        ad 
     68   1.1        ad 	/* Refine the salt first */
     69   1.1        ad 	sp = salt;
     70   1.1        ad 
     71   1.1        ad 	/* If it starts with the magic string, then skip that */
     72   1.2        ad 	if (strncmp(sp, MD5_MAGIC, MD5_MAGIC_LEN) == 0)
     73   1.1        ad 		sp += MD5_MAGIC_LEN;
     74   1.1        ad 
     75   1.1        ad 	/* It stops at the first '$', max 8 chars */
     76   1.2        ad 	for (ep = sp; *ep != '\0' && *ep != '$' && ep < (sp + 8); ep++)
     77   1.1        ad 		continue;
     78   1.1        ad 
     79   1.1        ad 	/* get the length of the true salt */
     80   1.1        ad 	sl = ep - sp;
     81   1.1        ad 
     82   1.5   thorpej 	INIT(&ctx);
     83   1.1        ad 
     84   1.1        ad 	/* The password first, since that is what is most unknown */
     85   1.5   thorpej 	UPDATE(&ctx, (const unsigned char *)pw, pwl);
     86   1.1        ad 
     87   1.1        ad 	/* Then our magic string */
     88   1.5   thorpej 	UPDATE(&ctx, (const unsigned char *)MD5_MAGIC, MD5_MAGIC_LEN);
     89   1.1        ad 
     90   1.1        ad 	/* Then the raw salt */
     91   1.5   thorpej 	UPDATE(&ctx, (const unsigned char *)sp, sl);
     92   1.1        ad 
     93   1.1        ad 	/* Then just as many characters of the MD5(pw,salt,pw) */
     94   1.5   thorpej 	INIT(&ctx1);
     95   1.5   thorpej 	UPDATE(&ctx1, (const unsigned char *)pw, pwl);
     96   1.5   thorpej 	UPDATE(&ctx1, (const unsigned char *)sp, sl);
     97   1.5   thorpej 	UPDATE(&ctx1, (const unsigned char *)pw, pwl);
     98   1.5   thorpej 	FINAL(final, &ctx1);
     99   1.1        ad 
    100   1.2        ad 	for (pl = pwl; pl > 0; pl -= 16)
    101   1.5   thorpej 		UPDATE(&ctx, final, (unsigned int)(pl > 16 ? 16 : pl));
    102   1.1        ad 
    103   1.1        ad 	/* Don't leave anything around in vm they could use. */
    104   1.1        ad 	memset(final, 0, sizeof(final));
    105   1.1        ad 
    106   1.1        ad 	/* Then something really weird... */
    107   1.1        ad 	for (i = pwl; i != 0; i >>= 1)
    108   1.2        ad 		if ((i & 1) != 0)
    109   1.5   thorpej 		    UPDATE(&ctx, final, 1);
    110   1.1        ad 		else
    111   1.5   thorpej 		    UPDATE(&ctx, (const unsigned char *)pw, 1);
    112   1.1        ad 
    113   1.1        ad 	/* Now make the output string */
    114   1.1        ad 	memcpy(passwd, MD5_MAGIC, MD5_MAGIC_LEN);
    115   1.4        ad 	strlcpy(passwd + MD5_MAGIC_LEN, sp, sl + 1);
    116   1.6    itojun 	strlcat(passwd, "$", sizeof(passwd));
    117   1.1        ad 
    118   1.5   thorpej 	FINAL(final, &ctx);
    119   1.1        ad 
    120  1.10  drochner 	/* Don't leave anything around in vm they could use. */
    121  1.10  drochner 	memset(&ctx, 0, sizeof(ctx));
    122  1.10  drochner 
    123   1.1        ad 	/*
    124   1.1        ad 	 * And now, just to make sure things don't run too fast. On a 60 MHz
    125   1.1        ad 	 * Pentium this takes 34 msec, so you would need 30 seconds to build
    126   1.1        ad 	 * a 1000 entry dictionary...
    127   1.1        ad 	 */
    128   1.2        ad 	for (i = 0; i < 1000; i++) {
    129   1.5   thorpej 		INIT(&ctx1);
    130   1.1        ad 
    131   1.2        ad 		if ((i & 1) != 0)
    132   1.5   thorpej 			UPDATE(&ctx1, (const unsigned char *)pw, pwl);
    133   1.1        ad 		else
    134   1.5   thorpej 			UPDATE(&ctx1, final, 16);
    135   1.1        ad 
    136   1.2        ad 		if ((i % 3) != 0)
    137   1.5   thorpej 			UPDATE(&ctx1, (const unsigned char *)sp, sl);
    138   1.1        ad 
    139   1.2        ad 		if ((i % 7) != 0)
    140   1.5   thorpej 			UPDATE(&ctx1, (const unsigned char *)pw, pwl);
    141   1.1        ad 
    142   1.2        ad 		if ((i & 1) != 0)
    143   1.5   thorpej 			UPDATE(&ctx1, final, 16);
    144   1.1        ad 		else
    145   1.5   thorpej 			UPDATE(&ctx1, (const unsigned char *)pw, pwl);
    146   1.1        ad 
    147   1.5   thorpej 		FINAL(final, &ctx1);
    148   1.1        ad 	}
    149   1.1        ad 
    150  1.10  drochner 	/* Don't leave anything around in vm they could use. */
    151  1.10  drochner 	memset(&ctx1, 0, sizeof(ctx1));
    152  1.10  drochner 
    153   1.1        ad 	p = passwd + sl + MD5_MAGIC_LEN + 1;
    154   1.1        ad 
    155   1.8       sjg 	l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; __crypt_to64(p,l,4); p += 4;
    156   1.8       sjg 	l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; __crypt_to64(p,l,4); p += 4;
    157   1.8       sjg 	l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; __crypt_to64(p,l,4); p += 4;
    158   1.8       sjg 	l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; __crypt_to64(p,l,4); p += 4;
    159   1.8       sjg 	l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; __crypt_to64(p,l,4); p += 4;
    160   1.8       sjg 	l =		       final[11]		; __crypt_to64(p,l,2); p += 2;
    161   1.1        ad 	*p = '\0';
    162   1.1        ad 
    163   1.1        ad 	/* Don't leave anything around in vm they could use. */
    164   1.1        ad 	memset(final, 0, sizeof(final));
    165   1.1        ad 	return (passwd);
    166   1.1        ad }
    167