OSDN Git Service

ARC: Make vfork weak in libc
[uclinux-h8/uClibc.git] / libcrypt / md5.c
index 02025e5..1af11ed 100644 (file)
@@ -28,8 +28,8 @@
  * edited for clarity and style only.
  *
  * ----------------------------------------------------------------------------
- * The md5_crypt() function was taken from freeBSD's libcrypt and contains 
- * this license: 
+ * The md5_crypt() function was taken from freeBSD's libcrypt and contains
+ * this license:
  *    "THE BEER-WARE LICENSE" (Revision 42):
  *     <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
  *     can do whatever you want with this stuff. If we meet some day, and you think
  * $FreeBSD: src/lib/libcrypt/crypt.c,v 1.7.2.1 1999/08/29 14:56:33 peter Exp $
  *
  * ----------------------------------------------------------------------------
- * On April 19th, 2001 md5_crypt() was modified to make it reentrant 
+ * On April 19th, 2001 md5_crypt() was modified to make it reentrant
  * by Erik Andersen <andersen@uclibc.org>
  *
+ *
  * June 28, 2001             Manuel Novoa III
  *
  * "Un-inlined" code using loops and static const tables in order to
  *               in each loop (glacial)
  * On i386, sizes are roughly (-Os -fno-builtin):
  *     0: 3k     1: 2.5k     2: 2.2k     3: 2k
+ *
+ *
+ * Since SuSv3 does not require crypt_r, modified again August 7, 2002
+ * by Erik Andersen to remove reentrance stuff...
  */
 
 /*
@@ -74,7 +79,8 @@
 #include <stdio.h>
 #include <crypt.h>
 #include <sys/cdefs.h>
-       
+#include "libcrypt.h"
+
 /* MD5 context. */
 struct MD5Context {
   u_int32_t state[4];  /* state (ABCD) */
@@ -86,17 +92,16 @@ static void   __md5_Init (struct MD5Context *);
 static void   __md5_Update (struct MD5Context *, const unsigned char *, unsigned int);
 static void   __md5_Pad (struct MD5Context *);
 static void   __md5_Final (unsigned char [16], struct MD5Context *);
-//static char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data);
 static void __md5_Transform __P((u_int32_t [4], const unsigned char [64]));
 
 
-static const char __md5__magic[] = "$1$";      /* This string is magic for this algorithm.  Having 
-                                                  it this way, we can get better later on */
+#define MD5_MAGIC_STR "$1$"
+#define MD5_MAGIC_LEN (sizeof(MD5_MAGIC_STR) - 1)
+static const unsigned char __md5__magic[] = MD5_MAGIC_STR;
 static const unsigned char __md5_itoa64[] =            /* 0 ... 63 => ascii - 64 */
        "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
 
-
 #ifdef i386
 #define __md5_Encode memcpy
 #define __md5_Decode memcpy
@@ -108,10 +113,7 @@ static const unsigned char __md5_itoa64[] =                /* 0 ... 63 => ascii - 64 */
  */
 
 static void
-__md5_Encode (output, input, len)
-       unsigned char *output;
-       u_int32_t *input;
-       unsigned int len;
+__md5_Encode (unsigned char *output, u_int32_t *input, unsigned int len)
 {
        unsigned int i, j;
 
@@ -129,10 +131,7 @@ __md5_Encode (output, input, len)
  */
 
 static void
-__md5_Decode (output, input, len)
-       u_int32_t *output;
-       const unsigned char *input;
-       unsigned int len;
+__md5_Decode (u_int32_t *output, const unsigned char *input, unsigned int len)
 {
        unsigned int i, j;
 
@@ -189,7 +188,7 @@ static void __md5_Init (struct MD5Context *context)
        context->state[3] = 0x10325476;
 }
 
-/* 
+/*
  * MD5 block update operation. Continues an MD5 message-digest
  * operation, processing another message block, and updating the
  * context.
@@ -197,10 +196,10 @@ static void __md5_Init (struct MD5Context *context)
 
 static void __md5_Update ( struct MD5Context *context, const unsigned char *input, unsigned int inputLen)
 {
-       unsigned int i, index, partLen;
+       unsigned int i, idx, partLen;
 
        /* Compute number of bytes mod 64 */
-       index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+       idx = (unsigned int)((context->count[0] >> 3) & 0x3F);
 
        /* Update number of bits */
        if ((context->count[0] += ((u_int32_t)inputLen << 3))
@@ -208,24 +207,24 @@ static void __md5_Update ( struct MD5Context *context, const unsigned char *inpu
                context->count[1]++;
        context->count[1] += ((u_int32_t)inputLen >> 29);
 
-       partLen = 64 - index;
+       partLen = 64 - idx;
 
        /* Transform as many times as possible. */
        if (inputLen >= partLen) {
-               memcpy((void *)&context->buffer[index], (const void *)input,
+               memcpy((void *)&context->buffer[idx], (const void *)input,
                    partLen);
                __md5_Transform (context->state, context->buffer);
 
                for (i = partLen; i + 63 < inputLen; i += 64)
                        __md5_Transform (context->state, &input[i]);
 
-               index = 0;
+               idx = 0;
        }
        else
                i = 0;
 
        /* Buffer remaining input */
-       memcpy ((void *)&context->buffer[index], (const void *)&input[i],
+       memcpy ((void *)&context->buffer[idx], (const void *)&input[i],
            inputLen-i);
 }
 
@@ -236,7 +235,7 @@ static void __md5_Update ( struct MD5Context *context, const unsigned char *inpu
 static void __md5_Pad ( struct MD5Context *context)
 {
        unsigned char bits[8];
-       unsigned int index, padLen;
+       unsigned int idx, padLen;
        unsigned char PADDING[64];
 
        memset(PADDING, 0, sizeof(PADDING));
@@ -246,8 +245,8 @@ static void __md5_Pad ( struct MD5Context *context)
        __md5_Encode (bits, context->count, 8);
 
        /* Pad out to 56 mod 64. */
-       index = (unsigned int)((context->count[0] >> 3) & 0x3f);
-       padLen = (index < 56) ? (56 - index) : (120 - index);
+       idx = (unsigned int)((context->count[0] >> 3) & 0x3f);
+       padLen = (idx < 56) ? (56 - idx) : (120 - idx);
        __md5_Update (context, PADDING, padLen);
 
        /* Append length (before padding) */
@@ -273,13 +272,9 @@ static void __md5_Final ( unsigned char digest[16], struct MD5Context *context)
 
 /* MD5 basic transformation. Transforms state based on block. */
 
-static void
-__md5_Transform (state, block)
-       u_int32_t state[4];
-       const unsigned char block[64];
+static void __md5_Transform (u_int32_t state[4], const unsigned char block[64])
 {
        u_int32_t a, b, c, d, x[16];
-
 #if MD5_SIZE_OVER_SPEED > 1
        u_int32_t temp;
        const char *ps;
@@ -331,7 +326,7 @@ __md5_Transform (state, block)
 
        __md5_Decode (x, block, 64);
 
-       a = state[0]; b = state[1]; c = state[2]; d = state[3]; 
+       a = state[0]; b = state[1]; c = state[2]; d = state[3];
 
 #if MD5_SIZE_OVER_SPEED > 2
        pc = C; pp = P; ps = S - 4;
@@ -533,17 +528,16 @@ static void __md5_to64( char *s, unsigned long v, int n)
  * Use MD5 for what it is best at...
  */
 
-extern char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
+char *__md5_crypt(const unsigned char *pw, const unsigned char *salt)
 {
-       char *p = data->p;
-       const char *sp = data->sp;
-       const char *ep = data->ep;
-       char *passwd = data->key.b_data;        /* This is a nice place where we can grab 
-                                          a bit of reentrant space...  I'd create 
-                                          a separate field in struct crypt_data, 
-                                          but this spot should do nicely... */
+       /* Static stuff */
+       /* "$1$" + salt_up_to_8_chars + "$" + 22_bytes_of_hash + NUL */
+       static char passwd[3 + 8 + 1 + 22 + 1];
+
+       const unsigned char *sp, *ep;
+       char *p;
        unsigned char   final[17];      /* final[16] exists only to aid in looping */
-       int sl,pl,i,__md5__magic_len,pw_len;
+       int sl,pl,i,pw_len;
        struct MD5Context ctx,ctx1;
        unsigned long l;
 
@@ -551,9 +545,8 @@ extern char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data
        sp = salt;
 
        /* If it starts with the magic string, then skip that */
-       __md5__magic_len = strlen(__md5__magic);
-       if(!strncmp(sp,__md5__magic,__md5__magic_len))
-               sp += __md5__magic_len;
+       if(!strncmp(sp,__md5__magic,MD5_MAGIC_LEN))
+               sp += MD5_MAGIC_LEN;
 
        /* It stops at the first '$', max 8 chars */
        for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++)
@@ -569,7 +562,7 @@ extern char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data
        __md5_Update(&ctx,pw,pw_len);
 
        /* Then our magic string */
-       __md5_Update(&ctx,__md5__magic,__md5__magic_len);
+       __md5_Update(&ctx,__md5__magic,MD5_MAGIC_LEN);
 
        /* Then the raw salt */
        __md5_Update(&ctx,sp,sl);
@@ -592,9 +585,9 @@ extern char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data
        }
 
        /* Now make the output string */
-       strcpy(passwd,__md5__magic);
-       strncat(passwd,sp,sl);
-       strcat(passwd,"$");
+       strcpy(passwd,__md5__magic); /* 3 bytes */
+       strncpy(passwd+MD5_MAGIC_LEN,(char*)sp,sl); /* 8 or less */
+       passwd[MD5_MAGIC_LEN+sl] = '$';
 
        __md5_Final(final,&ctx);
 
@@ -623,15 +616,17 @@ extern char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data
                __md5_Final(final,&ctx1);
        }
 
-       p = passwd + strlen(passwd);
-
+       /* Add 5*4+2 = 22 bytes of hash, + NUL byte. */
+       p = passwd + MD5_MAGIC_LEN + sl + 1;
        final[16] = final[5];
        for ( i=0 ; i < 5 ; i++ ) {
                l = (final[i]<<16) | (final[i+6]<<8) | final[i+12];
-               __md5_to64(p,l,4); p += 4;
+               __md5_to64(p,l,4);
+               p += 4;
        }
        l = final[11];
-       __md5_to64(p,l,2); p += 2;
+       __md5_to64(p,l,2);
+       p += 2;
        *p = '\0';
 
        /* Don't leave anything around in vm they could use. */