Browse Source

Begin efforts at reentrance by seperating out the stuff that isn't
reentrant and labeling it, and making everything else const.
-Erik

Eric Andersen 22 years ago
parent
commit
3a4b78aa11
1 changed files with 49 additions and 36 deletions
  1. 49 36
      libcrypt/des.c

+ 49 - 36
libcrypt/des.c

@@ -60,30 +60,54 @@
 #include <string.h>
 #include <crypt.h>
 
-static u_char	ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+/* Re-entrantify me -- all this junk needs to be in 
+ * struct crypt_data to make this really reentrant... */
+static u_char	inv_key_perm[64];
+static u_char	inv_comp_perm[56];
+static u_char	u_sbox[8][64];
+static u_char	u_key_perm[56];
+static u_char	un_pbox[32];
+static u_int32_t en_keysl[16], en_keysr[16];
+static u_int32_t de_keysl[16], de_keysr[16];
+static u_int32_t ip_maskl[8][256], ip_maskr[8][256];
+static u_int32_t fp_maskl[8][256], fp_maskr[8][256];
+static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128];
+static u_int32_t comp_maskl[8][128], comp_maskr[8][128];
+static u_int32_t saltbits;
+
+
+/* Static stuff that stays resident and doesn't change after 
+ * being initialized, and therefore doesn't need to be made 
+ * reentrant. */
+static u_char	init_perm[64], final_perm[64];
+static u_char	m_sbox[4][4096];
+static u_int32_t psbox[4][256];
+
+
+
 
-static u_char	IP[64] = {
+/* A pile of data */
+static const u_char	ascii64[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
+static const u_char	IP[64] = {
 	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 u_char	inv_key_perm[64];
-static u_char	u_key_perm[56];
-static u_char	key_perm[56] = {
+static const u_char	key_perm[56] = {
 	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 u_char	key_shifts[16] = {
+static const u_char	key_shifts[16] = {
 	1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
 };
 
-static u_char	inv_comp_perm[56];
-static u_char	comp_perm[48] = {
+static const u_char	comp_perm[48] = {
 	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,
@@ -94,8 +118,7 @@ static u_char	comp_perm[48] = {
  *	No E box is used, as it's replaced by some ANDs, shifts, and ORs.
  */
 
-static u_char	u_sbox[8][64];
-static u_char	sbox[8][64] = {
+static const u_char	sbox[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,
@@ -146,13 +169,12 @@ static u_char	sbox[8][64] = {
 	}
 };
 
-static u_char	un_pbox[32];
-static u_char	pbox[32] = {
+static const u_char	pbox[32] = {
 	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 u_int32_t bits32[32] =
+static const u_int32_t bits32[32] =
 {
 	0x80000000, 0x40000000, 0x20000000, 0x10000000,
 	0x08000000, 0x04000000, 0x02000000, 0x01000000,
@@ -164,22 +186,10 @@ static u_int32_t bits32[32] =
 	0x00000008, 0x00000004, 0x00000002, 0x00000001
 };
 
-static u_char	bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
+static const u_char	bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
+static const u_int32_t *bits28, *bits24;
+
 
-static u_int32_t saltbits;
-static int32_t	old_salt;
-static u_int32_t *bits28, *bits24;
-static u_char	init_perm[64], final_perm[64];
-static u_int32_t en_keysl[16], en_keysr[16];
-static u_int32_t de_keysl[16], de_keysr[16];
-static int	des_initialised = 0;
-static u_char	m_sbox[4][4096];
-static u_int32_t psbox[4][256];
-static u_int32_t ip_maskl[8][256], ip_maskr[8][256];
-static u_int32_t fp_maskl[8][256], fp_maskr[8][256];
-static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128];
-static u_int32_t comp_maskl[8][128], comp_maskr[8][128];
-static u_int32_t old_rawkey0, old_rawkey1;
 
 static __inline int ascii_to_bin(char ch)
 {
@@ -202,10 +212,12 @@ static void des_init(void)
 {
 	int	i, j, b, k, inbit, obit;
 	u_int32_t	*p, *il, *ir, *fl, *fr;
+	static int des_initialised = 0;
+
+	if (des_initialised==1)
+	    return;
 
-	old_rawkey0 = old_rawkey1 = 0;
 	saltbits = 0;
-	old_salt = 0;
 	bits24 = (bits28 = bits32 + 4) + 4;
 
 	/*
@@ -331,6 +343,7 @@ static void setup_salt(int32_t salt)
 {
 	u_int32_t	obit, saltbit;
 	int	i;
+	static int32_t old_salt = 0;
 
 	if (salt == old_salt)
 		return;
@@ -473,6 +486,9 @@ static int des_setkey_r(const char *key, struct crypt_data *data)
 {
 	u_int32_t k0, k1, rawkey0, rawkey1;
 	int	shifts, round;
+	static u_int32_t old_rawkey0=0, old_rawkey1=0;
+
+
 #if 0
 	u_int32_t *en_keysl = &(data->key[0]);
 	u_int32_t *en_keysr = &(data->key[16]);
@@ -480,8 +496,7 @@ static int des_setkey_r(const char *key, struct crypt_data *data)
 	u_int32_t *de_keysr = &(data->key[48]);
 #endif
 
-	if (!des_initialised)
-		des_init();
+	des_init();
 
 	rawkey0 = ntohl(*(u_int32_t *) key);
 	rawkey1 = ntohl(*(u_int32_t *) (key + 4));
@@ -577,8 +592,7 @@ static int __des_encrypt_r(char *block, int flag, struct crypt_data *data)
 	u_char	*p;
 	int	i, j, retval;
 
-	if (!des_initialised)
-		des_init();
+	des_init();
 
 	setup_salt((int32_t)0);
 	p = (u_char *)block;
@@ -604,8 +618,7 @@ extern char *__des_crypt_r(const char *key, const char *setting, struct crypt_da
 	 * should do nicely for now... */
 	char		*output = data->key.b_data;	
 
-	if (!des_initialised)
-		des_init();
+	des_init();
 
 	/*
 	 * Copy the key, shifting each character up by one bit