[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.8

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

CVSweb <webmaster@jp.NetBSD.org>