[BACK]Return to md5crypt.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libcrypt

Annotation of src/lib/libcrypt/md5crypt.c, Revision 1.11

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

CVSweb <webmaster@jp.NetBSD.org>