Browse Source

Adjust function naming and make sure everything is properly
static to avoid symbol leakage.
-Erik

Eric Andersen 23 years ago
parent
commit
41cb95ae3f
4 changed files with 98 additions and 101 deletions
  1. 3 1
      libcrypt/Makefile
  2. 3 3
      libcrypt/crypt.c
  3. 31 28
      libcrypt/des.c
  4. 61 69
      libcrypt/md5.c

+ 3 - 1
libcrypt/Makefile

@@ -28,7 +28,7 @@ LIBCRYPT=libcrypt.a
 LIBCRYPT_SHARED=libcrypt.so
 LIBCRYPT_SHARED_FULLNAME=libcrypt-$(MAJOR_VERSION).$(MINOR_VERSION).so
 
-CSRC = crypt.c des.c md5.c
+CSRC = crypt.c des.c
 OBJS=$(patsubst %.c,%.o, $(CSRC))
 
 all: $(OBJS) $(LIBCRYPT)
@@ -47,6 +47,8 @@ $(OBJS): %.o : %.c
 
 $(OBJ): Makefile
 
+des.o: des.c md5.c
+
 shared: all
 	$(LD) $(LDFLAGS) -soname=$(LIBCRYPT_SHARED).$(MAJOR_VERSION) \
 		-o $(LIBCRYPT_SHARED_FULLNAME) --whole-archive $(LIBCRYPT) \

+ 3 - 3
libcrypt/crypt.c

@@ -27,17 +27,17 @@
 /* For use by the old, non-reentrant routines (crypt/encrypt/setkey)  */
 static struct crypt_data __crypt_data;
 
-char * crypt(const char *key, const char *salt)
+extern char * crypt(const char *key, const char *salt)
 {
     return crypt_r (key, salt, &__crypt_data);
 }
 
-void setkey(const char *key)
+extern void setkey(const char *key)
 {
     return setkey_r(key, &__crypt_data);
 }
 
-void encrypt(char *block, int edflag)
+extern void encrypt(char *block, int edflag)
 {
     return encrypt_r(block, edflag, &__crypt_data);
 }

+ 31 - 28
libcrypt/des.c

@@ -46,56 +46,56 @@
 #include <string.h>
 #include <crypt.h>
 
-extern char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data);
+static char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data);
 
-static const struct ordering InitialTr = { {
+static const struct ordering __des_InitialTr = { {
 	58,50,42,34,26,18,10, 2,60,52,44,36,28,20,12, 4,
 	62,54,46,38,30,22,14, 6,64,56,48,40,32,24,16, 8,
 	57,49,41,33,25,17, 9, 1,59,51,43,35,27,19,11, 3,
 	61,53,45,37,29,21,13, 5,63,55,47,39,31,23,15, 7,
 } };
 
-static const struct ordering FinalTr = { {
+static const struct ordering __des_FinalTr = { {
 	40, 8,48,16,56,24,64,32,39, 7,47,15,55,23,63,31,
 	38, 6,46,14,54,22,62,30,37, 5,45,13,53,21,61,29,
 	36, 4,44,12,52,20,60,28,35, 3,43,11,51,19,59,27,
 	34, 2,42,10,50,18,58,26,33, 1,41, 9,49,17,57,25,
 } };
 
-static const struct ordering swap = { {
+static const struct ordering __des_Swap = { {
 	33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
 	49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,
 	 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,
 	17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
 } };
 
-static const struct ordering KeyTr1 = { {
+static const struct ordering __des_KeyTr1 = { {
 	57,49,41,33,25,17, 9, 1,58,50,42,34,26,18,
 	10, 2,59,51,43,35,27,19,11, 3,60,52,44,36,
 	63,55,47,39,31,23,15, 7,62,54,46,38,30,22,
 	14, 6,61,53,45,37,29,21,13, 5,28,20,12, 4,
 } };
 
-static const struct ordering KeyTr2 = { {
+static const struct ordering __des_KeyTr2 = { {
 	14,17,11,24, 1, 5, 3,28,15, 6,21,10,
 	23,19,12, 4,26, 8,16, 7,27,20,13, 2,
 	41,52,31,37,47,55,30,40,51,45,33,48,
 	44,49,39,56,34,53,46,42,50,36,29,32,
 } };
 
-static const struct ordering etr = { {
+static const struct ordering __des_Etr = { {
 	32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
 	 8, 9,10,11,12,13,12,13,14,15,16,17,
 	16,17,18,19,20,21,20,21,22,23,24,25,
 	24,25,26,27,28,29,28,29,30,31,32, 1,
 } };
 
-static const struct ordering ptr = { {
+static const struct ordering __des_Ptr = { {
 	16, 7,20,21,29,12,28,17, 1,15,23,26, 5,18,31,10,
 	 2, 8,24,14,32,27, 3, 9,19,13,30, 6,22,11, 4,25,
 } };
 
-static const unsigned char s_boxes[8][64] = {
+static const unsigned char __des_S_boxes[8][64] = {
 {	14, 4,13, 1, 2,15,11, 8, 3,10, 6,12, 5, 9, 0, 7,
 	 0,15, 7, 4,14, 2,13, 1,10, 6,12,11, 9, 5, 3, 8,
 	 4, 1,14, 8,13, 6, 2,11,15,12, 9, 7, 3,10, 5, 0,
@@ -145,11 +145,11 @@ static const unsigned char s_boxes[8][64] = {
 },
 };
 
-static const int rots[] = {
+static const int __des_Rots[] = {
 	1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,
 };
 
-static void transpose(struct block *data, const struct ordering *t, int n)
+static void __des_transpose(struct block *data, const struct ordering *t, int n)
 {
 	struct block x;
 
@@ -160,7 +160,7 @@ static void transpose(struct block *data, const struct ordering *t, int n)
 	}
 }
 
-static void rotate(struct block *key)
+static void __des_rotate(struct block *key)
 {
 	unsigned char *p = key->b_data;
 	unsigned char *ep = &(key->b_data[55]);
@@ -171,17 +171,17 @@ static void rotate(struct block *key)
 	key->b_data[55] = data28;
 }
 
-static void f(int i, struct block *key, struct block *a, struct block *x, struct crypt_data *data)
+static void __des_encrypt(int i, struct block *key, struct block *a, struct block *x, struct crypt_data *data)
 {
 	struct block e, ikey, y;
 	int k;
 	unsigned char *p, *q, *r;
 
 	e = *a;
-	transpose(&e, data->EP, 48);
-	for (k = rots[i]; k; k--) rotate(key);
+	__des_transpose(&e, data->EP, 48);
+	for (k = __des_Rots[i]; k; k--) __des_rotate(key);
 	ikey = *key;
-	transpose(&ikey, &KeyTr2, 48);
+	__des_transpose(&ikey, &__des_KeyTr2, 48);
 	p = &(y.b_data[48]);
 	q = &(e.b_data[48]);
 	r = &(ikey.b_data[48]);
@@ -199,21 +199,21 @@ static void f(int i, struct block *key, struct block *a, struct block *x, struct
 		r += *p++;
 		r += *p++ << 4;
 
-		xb = s_boxes[k][r];
+		xb = __des_S_boxes[k][r];
 
 		*q++ = (xb >> 3) & 1;
 		*q++ = (xb>>2) & 1;
 		*q++ = (xb>>1) & 1;
 		*q++ = (xb & 1);
 	}
-	transpose(x, &ptr, 32);
+	__des_transpose(x, &__des_Ptr, 32);
 }
 
-void setkey_r(const char *k, struct crypt_data *data)
+extern void setkey_r(const char *k, struct crypt_data *data)
 {
 	struct block *key = &(data->key);
 	memcpy(key, k, (sizeof(struct block)));
-	transpose(key, &KeyTr1, 56);
+	__des_transpose(key, &__des_KeyTr1, 56);
 }
 
 extern void encrypt_r(char *blck, int edflag, struct crypt_data *data)
@@ -222,7 +222,8 @@ extern void encrypt_r(char *blck, int edflag, struct crypt_data *data)
 	struct block *p = (struct block *) blck;
 	int i;
 
-	transpose(p, &InitialTr, 64);
+	__des_transpose(p, &__des_InitialTr, 64);
+	data->EP = &__des_Etr;
 	for (i = 15; i>= 0; i--) {
 		int j = edflag ? i : 15 - i;
 		int k;
@@ -232,13 +233,13 @@ extern void encrypt_r(char *blck, int edflag, struct crypt_data *data)
 		for (k = 31; k >= 0; k--) {
 			p->b_data[k] = b.b_data[k + 32];
 		}
-		f(j, key, p, &x, data);
+		__des_encrypt(j, key, p, &x, data);
 		for (k = 31; k >= 0; k--) {
 			p->b_data[k+32] = b.b_data[k] ^ x.b_data[k];
 		}
 	}
-	transpose(p, &swap, 64);
-	transpose(p, &FinalTr, 64);
+	__des_transpose(p, &__des_Swap, 64);
+	__des_transpose(p, &__des_FinalTr, 64);
 }
 
 extern char *crypt_r(const char *pw, const char *salt, struct crypt_data *data)
@@ -253,9 +254,8 @@ extern char *crypt_r(const char *pw, const char *salt, struct crypt_data *data)
 	/* First, check if we are supposed to be using the MD5 replacement
 	 * instead of DES...  */
 	if (salt[0]=='$' && salt[1]=='1' && salt[2]=='$')
-		return md5_crypt_r(pw, salt, data);
+		return __md5_crypt_r(pw, salt, data);
 
-	data->EP = &etr;
 	while (*pw && p < &pwb[64]) {
 		int j = 7;
 
@@ -271,7 +271,7 @@ extern char *crypt_r(const char *pw, const char *salt, struct crypt_data *data)
 
 	while (p < &pwb[66]) *p++ = 0;
 
-	new_etr = etr;
+	new_etr = __des_Etr;
 	data->EP = &new_etr;
 	if (salt[0] == 0 || salt[1] == 0) salt = "**";
 	for (i = 0; i < 2; i++) {
@@ -295,8 +295,9 @@ extern char *crypt_r(const char *pw, const char *salt, struct crypt_data *data)
 
 	if (result[1] == 0) result[1] = result[0];
 
+	data->EP = &__des_Etr;
 	for (i = 0; i < 25; i++) encrypt_r(pwb,0, data);
-	data->EP = &etr;
+	data->EP = &__des_Etr;
 
 	p = pwb;
 	cp = result+2;
@@ -355,3 +356,5 @@ extern char *crypt_r(const char *pw, const char *salt, struct crypt_data *data)
  */
 
 
+#include <md5.c>
+

+ 61 - 69
libcrypt/md5.c

@@ -76,47 +76,39 @@
 #include <sys/cdefs.h>
 	
 /* MD5 context. */
-typedef struct MD5Context {
+struct MD5Context {
   u_int32_t state[4];	/* state (ABCD) */
   u_int32_t count[2];	/* number of bits, modulo 2^64 (lsb first) */
   unsigned char buffer[64];	/* input buffer */
-} MD5_CTX;
+};
 
-void   MD5Init (MD5_CTX *);
-void   MD5Update (MD5_CTX *, const unsigned char *, unsigned int);
-void   MD5Pad (MD5_CTX *);
-void   MD5Final (unsigned char [16], MD5_CTX *);
-char * MD5End(MD5_CTX *, char *);
-char * MD5File(const char *, char *);
-char * MD5Data(const unsigned char *, unsigned int, char *);
-char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data);
+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]));
 
 
-char	*md5_magic = "$1$";	/* * This string is magic for this algorithm.  Having 
-				   it this way, we can get better later on */
-static const unsigned char itoa64[] =		/* 0 ... 63 => ascii - 64 */
+static const char __md5__magic[] = "$1$";	/* This string is magic for this algorithm.  Having 
+						   it this way, we can get better later on */
+static const unsigned char __md5_itoa64[] =		/* 0 ... 63 => ascii - 64 */
 	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 
 
-static void MD5Transform __P((u_int32_t [4], const unsigned char [64]));
-
-#ifdef KERNEL
-#define memset(x,y,z)	bzero(x,z);
-#define memcpy(x,y,z)	bcopy(y, x, z)
-#endif
 
 #ifdef i386
-#define Encode memcpy
-#define Decode memcpy
+#define __md5_Encode memcpy
+#define __md5_Decode memcpy
 #else /* i386 */
 
 /*
- * Encodes input (u_int32_t) into output (unsigned char). Assumes len is
+ * __md5_Encodes input (u_int32_t) into output (unsigned char). Assumes len is
  * a multiple of 4.
  */
 
 static void
-Encode (output, input, len)
+__md5_Encode (output, input, len)
 	unsigned char *output;
 	u_int32_t *input;
 	unsigned int len;
@@ -132,12 +124,12 @@ Encode (output, input, len)
 }
 
 /*
- * Decodes input (unsigned char) into output (u_int32_t). Assumes len is
+ * __md5_Decodes input (unsigned char) into output (u_int32_t). Assumes len is
  * a multiple of 4.
  */
 
 static void
-Decode (output, input, len)
+__md5_Decode (output, input, len)
 	u_int32_t *output;
 	const unsigned char *input;
 	unsigned int len;
@@ -186,7 +178,7 @@ Decode (output, input, len)
 
 /* MD5 initialization. Begins an MD5 operation, writing a new context. */
 
-void MD5Init (MD5_CTX *context)
+static void __md5_Init (struct MD5Context *context)
 {
 	context->count[0] = context->count[1] = 0;
 
@@ -203,7 +195,7 @@ void MD5Init (MD5_CTX *context)
  * context.
  */
 
-void MD5Update ( MD5_CTX *context, const unsigned char *input, unsigned int inputLen)
+static void __md5_Update ( struct MD5Context *context, const unsigned char *input, unsigned int inputLen)
 {
 	unsigned int i, index, partLen;
 
@@ -222,10 +214,10 @@ void MD5Update ( MD5_CTX *context, const unsigned char *input, unsigned int inpu
 	if (inputLen >= partLen) {
 		memcpy((void *)&context->buffer[index], (const void *)input,
 		    partLen);
-		MD5Transform (context->state, context->buffer);
+		__md5_Transform (context->state, context->buffer);
 
 		for (i = partLen; i + 63 < inputLen; i += 64)
-			MD5Transform (context->state, &input[i]);
+			__md5_Transform (context->state, &input[i]);
 
 		index = 0;
 	}
@@ -241,7 +233,7 @@ void MD5Update ( MD5_CTX *context, const unsigned char *input, unsigned int inpu
  * MD5 padding. Adds padding followed by original length.
  */
 
-void MD5Pad ( MD5_CTX *context)
+static void __md5_Pad ( struct MD5Context *context)
 {
 	unsigned char bits[8];
 	unsigned int index, padLen;
@@ -251,15 +243,15 @@ void MD5Pad ( MD5_CTX *context)
 	PADDING[0] = 0x80;
 
 	/* Save number of bits */
-	Encode (bits, context->count, 8);
+	__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);
-	MD5Update (context, PADDING, padLen);
+	__md5_Update (context, PADDING, padLen);
 
 	/* Append length (before padding) */
-	MD5Update (context, bits, 8);
+	__md5_Update (context, bits, 8);
 }
 
 /*
@@ -267,13 +259,13 @@ void MD5Pad ( MD5_CTX *context)
  * the message digest and zeroizing the context.
  */
 
-void MD5Final ( unsigned char digest[16], MD5_CTX *context)
+static void __md5_Final ( unsigned char digest[16], struct MD5Context *context)
 {
 	/* Do padding. */
-	MD5Pad (context);
+	__md5_Pad (context);
 
 	/* Store state in digest */
-	Encode (digest, context->state, 16);
+	__md5_Encode (digest, context->state, 16);
 
 	/* Zeroize sensitive information. */
 	memset ((void *)context, 0, sizeof (*context));
@@ -282,7 +274,7 @@ void MD5Final ( unsigned char digest[16], MD5_CTX *context)
 /* MD5 basic transformation. Transforms state based on block. */
 
 static void
-MD5Transform (state, block)
+__md5_Transform (state, block)
 	u_int32_t state[4];
 	const unsigned char block[64];
 {
@@ -337,7 +329,7 @@ MD5Transform (state, block)
 
 #endif /* MD5_SIZE_OVER_SPEED > 0 */
 
-	Decode (x, block, 64);
+	__md5_Decode (x, block, 64);
 
 	a = state[0]; b = state[1]; c = state[2]; d = state[3]; 
 
@@ -527,10 +519,10 @@ MD5Transform (state, block)
 }
 
 
-static void to64( char *s, unsigned long v, int n)
+static void __md5_to64( char *s, unsigned long v, int n)
 {
 	while (--n >= 0) {
-		*s++ = itoa64[v&0x3f];
+		*s++ = __md5_itoa64[v&0x3f];
 		v >>= 6;
 	}
 }
@@ -541,7 +533,7 @@ static void to64( char *s, unsigned long v, int n)
  * Use MD5 for what it is best at...
  */
 
-char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
+static char * __md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
 {
 	char *p = data->p;
 	const char *sp = data->sp;
@@ -551,17 +543,17 @@ char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
 					   a separate field in struct crypt_data, 
 					   but this spot should do nicely... */
 	unsigned char	final[17];	/* final[16] exists only to aid in looping */
-	int sl,pl,i,md5_magic_len,pw_len;
-	MD5_CTX	ctx,ctx1;
+	int sl,pl,i,__md5__magic_len,pw_len;
+	struct MD5Context ctx,ctx1;
 	unsigned long l;
 
 	/* Refine the Salt first */
 	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;
+	__md5__magic_len = strlen(__md5__magic);
+	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++)
@@ -570,41 +562,41 @@ char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
 	/* get the length of the true salt */
 	sl = ep - sp;
 
-	MD5Init(&ctx);
+	__md5_Init(&ctx);
 
 	/* The password first, since that is what is most unknown */
 	pw_len = strlen(pw);
-	MD5Update(&ctx,pw,pw_len);
+	__md5_Update(&ctx,pw,pw_len);
 
 	/* Then our magic string */
-	MD5Update(&ctx,md5_magic,md5_magic_len);
+	__md5_Update(&ctx,__md5__magic,__md5__magic_len);
 
 	/* Then the raw salt */
-	MD5Update(&ctx,sp,sl);
+	__md5_Update(&ctx,sp,sl);
 
 	/* Then just as many characters of the MD5(pw,salt,pw) */
-	MD5Init(&ctx1);
-	MD5Update(&ctx1,pw,pw_len);
-	MD5Update(&ctx1,sp,sl);
-	MD5Update(&ctx1,pw,pw_len);
-	MD5Final(final,&ctx1);
+	__md5_Init(&ctx1);
+	__md5_Update(&ctx1,pw,pw_len);
+	__md5_Update(&ctx1,sp,sl);
+	__md5_Update(&ctx1,pw,pw_len);
+	__md5_Final(final,&ctx1);
 	for(pl = pw_len; pl > 0; pl -= 16)
-		MD5Update(&ctx,final,pl>16 ? 16 : pl);
+		__md5_Update(&ctx,final,pl>16 ? 16 : pl);
 
 	/* Don't leave anything around in vm they could use. */
 	memset(final,0,sizeof final);
 
 	/* Then something really weird... */
 	for (i = pw_len; i ; i >>= 1) {
-		MD5Update(&ctx, ((i&1) ? final : (const unsigned char *) pw), 1);
+		__md5_Update(&ctx, ((i&1) ? final : (const unsigned char *) pw), 1);
 	}
 
 	/* Now make the output string */
-	strcpy(passwd,md5_magic);
+	strcpy(passwd,__md5__magic);
 	strncat(passwd,sp,sl);
 	strcat(passwd,"$");
 
-	MD5Final(final,&ctx);
+	__md5_Final(final,&ctx);
 
 	/*
 	 * and now, just to make sure things don't run too fast
@@ -612,23 +604,23 @@ char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
 	 * need 30 seconds to build a 1000 entry dictionary...
 	 */
 	for(i=0;i<1000;i++) {
-		MD5Init(&ctx1);
+		__md5_Init(&ctx1);
 		if(i & 1)
-			MD5Update(&ctx1,pw,pw_len);
+			__md5_Update(&ctx1,pw,pw_len);
 		else
-			MD5Update(&ctx1,final,16);
+			__md5_Update(&ctx1,final,16);
 
 		if(i % 3)
-			MD5Update(&ctx1,sp,sl);
+			__md5_Update(&ctx1,sp,sl);
 
 		if(i % 7)
-			MD5Update(&ctx1,pw,pw_len);
+			__md5_Update(&ctx1,pw,pw_len);
 
 		if(i & 1)
-			MD5Update(&ctx1,final,16);
+			__md5_Update(&ctx1,final,16);
 		else
-			MD5Update(&ctx1,pw,pw_len);
-		MD5Final(final,&ctx1);
+			__md5_Update(&ctx1,pw,pw_len);
+		__md5_Final(final,&ctx1);
 	}
 
 	p = passwd + strlen(passwd);
@@ -636,10 +628,10 @@ char * md5_crypt_r( const char *pw, const char *salt, struct crypt_data * data)
 	final[16] = final[5];
 	for ( i=0 ; i < 5 ; i++ ) {
 		l = (final[i]<<16) | (final[i+6]<<8) | final[i+12];
-		to64(p,l,4); p += 4;
+		__md5_to64(p,l,4); p += 4;
 	}
 	l = final[11];
-	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. */