From 23c3e723f7c1fe8fe46ca411a3f8a84f9bc28b03 Mon Sep 17 00:00:00 2001
From: vlo <vlo@kr4d.de>
Date: Thu, 9 May 2019 08:51:21 +0200
Subject: [PATCH] Implemeted table for alternate base64

Implemented alternate base64 table, removed replacement code in modules.
Fixed: whitespace/indent issues
Fixed: unsigned to signed implicit typecasts
Added new algorithm information to readme.txt and changes.txt
---
 docs/changes.txt           |  3 ++
 docs/readme.txt            |  3 ++
 include/convert.h          | 30 ++++++++++----------
 src/convert.c              | 39 ++++++++++++++++++++++++++
 src/modules/module_20200.c | 55 ++++++++----------------------------
 src/modules/module_20300.c | 57 +++++++++-----------------------------
 src/modules/module_20400.c | 54 ++++++++----------------------------
 7 files changed, 97 insertions(+), 144 deletions(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index 6f6b68b91..460774bfd 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -27,6 +27,9 @@
 - Added hash-mode: sha1($salt1.$pass.$salt2)
 - Added hash-mode: Ruby on Rails Restful-Authentication
 - Added hash-mode: DiskCryptor
+- Added hash-mode: Python passlib pbkdf2-sha1
+- Added hash-mode: Python passlib pbkdf2-sha256
+- Added hash-mode: Python passlib pbkdf2-sha512
 
 ##
 ## Bugs
diff --git a/docs/readme.txt b/docs/readme.txt
index 2614bdf0b..7f90047f7 100644
--- a/docs/readme.txt
+++ b/docs/readme.txt
@@ -270,6 +270,9 @@ NVIDIA GPUs require "NVIDIA Driver" (418.56 or later)
 - Ethereum Pre-Sale Wallet, PBKDF2-HMAC-SHA256
 - Ansible Vault
 - Plaintext
+- Python passlib pbkdf2-sha1
+- Python passlib pbkdf2-sha256
+- Python passlib pbkdf2-sha512
 
 ##
 ## Attack-Modes
diff --git a/include/convert.h b/include/convert.h
index cff45eb5a..5a7c4fad3 100644
--- a/include/convert.h
+++ b/include/convert.h
@@ -35,20 +35,22 @@ void u8_to_hex  (const u8  v, u8 hex[2]);
 void u32_to_hex (const u32 v, u8 hex[8]);
 void u64_to_hex (const u64 v, u8 hex[16]);
 
-u8 int_to_base32    (const u8 c);
-u8 base32_to_int    (const u8 c);
-u8 int_to_base64    (const u8 c);
-u8 base64_to_int    (const u8 c);
-u8 int_to_base64url (const u8 c);
-u8 base64url_to_int (const u8 c);
-u8 int_to_itoa32    (const u8 c);
-u8 itoa32_to_int    (const u8 c);
-u8 int_to_itoa64    (const u8 c);
-u8 itoa64_to_int    (const u8 c);
-u8 int_to_bf64      (const u8 c);
-u8 bf64_to_int      (const u8 c);
-u8 int_to_lotus64   (const u8 c);
-u8 lotus64_to_int   (const u8 c);
+u8 int_to_base32            (const u8 c);
+u8 base32_to_int            (const u8 c);
+u8 int_to_base64            (const u8 c);
+u8 base64_to_int            (const u8 c);
+u8 int_to_alternate_base64  (const u8 c);
+u8 alternate_base64_to_int  (const u8 c);
+u8 int_to_base64url         (const u8 c);
+u8 base64url_to_int         (const u8 c);
+u8 int_to_itoa32            (const u8 c);
+u8 itoa32_to_int            (const u8 c);
+u8 int_to_itoa64            (const u8 c);
+u8 itoa64_to_int            (const u8 c);
+u8 int_to_bf64              (const u8 c);
+u8 bf64_to_int              (const u8 c);
+u8 int_to_lotus64           (const u8 c);
+u8 lotus64_to_int           (const u8 c);
 
 size_t base32_decode (u8 (*f) (const u8), const u8 *in_buf, const size_t in_len, u8 *out_buf);
 size_t base32_encode (u8 (*f) (const u8), const u8 *in_buf, const size_t in_len, u8 *out_buf);
diff --git a/src/convert.c b/src/convert.c
index c9426f617..264ad8a90 100644
--- a/src/convert.c
+++ b/src/convert.c
@@ -555,6 +555,45 @@ u8 base64_to_int (const u8 c)
   return tbl[c];
 }
 
+// alternate base64 using ./ instead of +/, used in python passlib hashes
+u8 int_to_alternate_base64 (const u8 c)
+{
+  const u8 tbl[0x40] =
+  {
+    0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
+    0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
+    0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
+    0x77, 0x78, 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2e, 0x2f,
+  };
+
+  return tbl[c];
+}
+
+u8 alternate_base64_to_int (const u8 c)
+{
+  const u8 tbl[0x100] =
+  {
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x3f,
+    0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
+    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
+    0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  };
+
+  return tbl[c];
+}
+
 u8 int_to_base64url (const u8 c)
 {
   const u8 tbl[0x40] =
diff --git a/src/modules/module_20200.c b/src/modules/module_20200.c
index a95ce8cf0..eec984094 100644
--- a/src/modules/module_20200.c
+++ b/src/modules/module_20200.c
@@ -152,43 +152,21 @@ int module_hash_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE
   const int salt_len = token.len[3];
   
   u8 tmp_buf[100] = { 0 };
-  u8 tmp_buf2[100] = { 0 };
-
-  // replace . with + for proper base64 decoding
-  memcpy(tmp_buf2, salt_pos, salt_len);
-  for (int i = 0; i < salt_len; i++)
-  {
-    if (tmp_buf2[i] == '.')
-    {
-      tmp_buf2[i] = '+';
-    }
-  }
-
   
-  const int salt_len_decoded = base64_decode (base64_to_int, (const u8 *) tmp_buf2, salt_len, tmp_buf);
+  const size_t salt_len_decoded = base64_decode (alternate_base64_to_int, (const u8 *) salt_pos, salt_len, tmp_buf);
 
   u8 *salt_buf_ptr = (u8 *) pbkdf2_sha512->salt_buf;
   memcpy (salt_buf_ptr, tmp_buf, salt_len_decoded);
   memcpy (salt->salt_buf, salt_buf_ptr, salt_len_decoded);
   
-  salt->salt_len    = salt_len_decoded;
+  salt->salt_len = salt_len_decoded;
   
 
   // base64 decode hash
   const u8 *hash_pos = token.buf[4];
   const int hash_len = token.len[4];
 
-  // replace . with + for proper base64 decoding
-  memcpy(tmp_buf2, hash_pos, hash_len);
-  for (int i = 0; i < hash_len; i++)
-  {
-    if (tmp_buf2[i] == '.')
-    {
-      tmp_buf2[i] = '+';
-    }
-  }
-
-  base64_decode (base64_to_int, (const u8 *) tmp_buf2, hash_len, tmp_buf);
+  base64_decode (alternate_base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
   memcpy (digest, tmp_buf, HASH_LEN_RAW);
 
   digest[0] = byte_swap_64 (digest[0]);
@@ -225,33 +203,24 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE
   char salt_enc[128] = { 0 };
   char hash_enc[128] = { 0 };
 
-  int salt_len_enc = base64_encode (int_to_base64, (const u8 *) pbkdf2_sha512->salt_buf, salt->salt_len, (u8 *) salt_enc);
-  int hash_len_enc = base64_encode (int_to_base64, (const u8 *) tmp, HASH_LEN_RAW, (u8 *) hash_enc);
+  const size_t salt_len_enc = base64_encode (int_to_alternate_base64, (const u8 *) pbkdf2_sha512->salt_buf, salt->salt_len, (u8 *) salt_enc);
+  const size_t hash_len_enc = base64_encode (int_to_alternate_base64, (const u8 *) tmp, HASH_LEN_RAW, (u8 *) hash_enc);
   
-  
-  // substitute + with . and remove padding =
-  for (int i = 0; i < salt_len_enc; i++) 
+  // remove padding =
+  for (size_t i = 0; i < salt_len_enc; i++) 
   {
-    if (salt_enc[i] == '+')
-    { 
-      salt_enc[i] = '.';
-    }
     if (salt_enc[i] == '=') 
     {
-	  salt_enc[i] = '\0';
-	}
+      salt_enc[i] = '\0';
+    }
   }
 
-  for (int i = 0; i < hash_len_enc; i++) 
+  for (size_t i = 0; i < hash_len_enc; i++) 
   {
-    if (hash_enc[i] == '+') 
-    {
-	  hash_enc[i] = '.';
-	}
     if (hash_enc[i] == '=') 
     {
-	  hash_enc[i] = '\0';
-	}  
+      hash_enc[i] = '\0';
+    }  
   }
   
   // output
diff --git a/src/modules/module_20300.c b/src/modules/module_20300.c
index 6d90ae795..4f43153cd 100644
--- a/src/modules/module_20300.c
+++ b/src/modules/module_20300.c
@@ -151,43 +151,21 @@ int module_hash_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE
   const int salt_len = token.len[3];
   
   u8 tmp_buf[100] = { 0 };
-  u8 tmp_buf2[100] = { 0 };
-
-  // replace . with + for proper base64 decoding
-  memcpy(tmp_buf2, salt_pos, salt_len);
-  for (int i = 0; i < salt_len; i++)
-  {
-    if (tmp_buf2[i] == '.')
-    {
-      tmp_buf2[i] = '+';
-    }
-  }
-
-  
-  const int salt_len_decoded = base64_decode (base64_to_int, (const u8 *) tmp_buf2, salt_len, tmp_buf);
+ 
+  const size_t salt_len_decoded = base64_decode (alternate_base64_to_int, (const u8 *) salt_pos, salt_len, tmp_buf);
 
   u8 *salt_buf_ptr = (u8 *) pbkdf2_sha256->salt_buf;
   memcpy (salt_buf_ptr, tmp_buf, salt_len_decoded);
   memcpy (salt->salt_buf, salt_buf_ptr, salt_len_decoded);
   
-  salt->salt_len    = salt_len_decoded;
+  salt->salt_len = salt_len_decoded;
   
 
   // base64 decode hash
   const u8 *hash_pos = token.buf[4];
   const int hash_len = token.len[4];
 
-  // replace . with + for proper base64 decoding
-  memcpy(tmp_buf2, hash_pos, hash_len);
-  for (int i = 0; i < hash_len; i++)
-  {
-    if (tmp_buf2[i] == '.')
-    {
-      tmp_buf2[i] = '+';
-    }
-  }
-
-  base64_decode (base64_to_int, (const u8 *) tmp_buf2, hash_len, tmp_buf);
+  base64_decode (alternate_base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
   memcpy (digest, tmp_buf, HASH_LEN_RAW);
 
   digest[0] = byte_swap_32 (digest[0]);
@@ -224,33 +202,24 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE
   char salt_enc[128] = { 0 };
   char hash_enc[128] = { 0 };
 
-  int salt_len_enc = base64_encode (int_to_base64, (const u8 *) pbkdf2_sha256->salt_buf, salt->salt_len, (u8 *) salt_enc);
-  int hash_len_enc = base64_encode (int_to_base64, (const u8 *) tmp, HASH_LEN_RAW, (u8 *) hash_enc);
+  const size_t salt_len_enc = base64_encode (int_to_alternate_base64, (const u8 *) pbkdf2_sha256->salt_buf, salt->salt_len, (u8 *) salt_enc);
+  const size_t hash_len_enc = base64_encode (int_to_alternate_base64, (const u8 *) tmp, HASH_LEN_RAW, (u8 *) hash_enc);
   
-  
-  // substitute + with . and remove padding =
-  for (int i = 0; i < salt_len_enc; i++) 
+  // remove padding =
+  for (size_t i = 0; i < salt_len_enc; i++) 
   {
-    if (salt_enc[i] == '+')
-    { 
-      salt_enc[i] = '.';
-    }
     if (salt_enc[i] == '=') 
     {
-	  salt_enc[i] = '\0';
-	}
+      salt_enc[i] = '\0';
+    }
   }
 
-  for (int i = 0; i < hash_len_enc; i++) 
+  for (size_t i = 0; i < hash_len_enc; i++) 
   {
-    if (hash_enc[i] == '+') 
-    {
-	  hash_enc[i] = '.';
-	}
     if (hash_enc[i] == '=') 
     {
-	  hash_enc[i] = '\0';
-	}  
+      hash_enc[i] = '\0';
+    }  
   }
   
   // output
diff --git a/src/modules/module_20400.c b/src/modules/module_20400.c
index dda3e8479..9324eb462 100644
--- a/src/modules/module_20400.c
+++ b/src/modules/module_20400.c
@@ -151,43 +151,20 @@ int module_hash_decode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE
   const int salt_len = token.len[3];
   
   u8 tmp_buf[100] = { 0 };
-  u8 tmp_buf2[100] = { 0 };
 
-  // replace . with + for proper base64 decoding
-  memcpy(tmp_buf2, salt_pos, salt_len);
-  for (int i = 0; i < salt_len; i++)
-  {
-    if (tmp_buf2[i] == '.')
-    {
-      tmp_buf2[i] = '+';
-    }
-  }
-
-  
-  const int salt_len_decoded = base64_decode (base64_to_int, (const u8 *) tmp_buf2, salt_len, tmp_buf);
+  const size_t salt_len_decoded = base64_decode (alternate_base64_to_int, (const u8 *) salt_pos, salt_len, tmp_buf);
 
   u8 *salt_buf_ptr = (u8 *) pbkdf2_sha1->salt_buf;
   memcpy (salt_buf_ptr, tmp_buf, salt_len_decoded);
   memcpy (salt->salt_buf, salt_buf_ptr, salt_len_decoded);
   
-  salt->salt_len    = salt_len_decoded;
-  
+  salt->salt_len = salt_len_decoded;
 
   // base64 decode hash
   const u8 *hash_pos = token.buf[4];
   const int hash_len = token.len[4];
 
-  // replace . with + for proper base64 decoding
-  memcpy(tmp_buf2, hash_pos, hash_len);
-  for (int i = 0; i < hash_len; i++)
-  {
-    if (tmp_buf2[i] == '.')
-    {
-      tmp_buf2[i] = '+';
-    }
-  }
-
-  base64_decode (base64_to_int, (const u8 *) tmp_buf2, hash_len, tmp_buf);
+  base64_decode (alternate_base64_to_int, (const u8 *) hash_pos, hash_len, tmp_buf);
   memcpy (digest, tmp_buf, HASH_LEN_RAW);
 
   digest[0] = byte_swap_32 (digest[0]);
@@ -218,33 +195,24 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE
   char salt_enc[128] = { 0 };
   char hash_enc[128] = { 0 };
 
-  int salt_len_enc = base64_encode (int_to_base64, (const u8 *) pbkdf2_sha1->salt_buf, salt->salt_len, (u8 *) salt_enc);
-  int hash_len_enc = base64_encode (int_to_base64, (const u8 *) tmp, HASH_LEN_RAW, (u8 *) hash_enc);
-  
+  const size_t salt_len_enc = base64_encode (int_to_alternate_base64, (const u8 *) pbkdf2_sha1->salt_buf, salt->salt_len, (u8 *) salt_enc);
+  const size_t hash_len_enc = base64_encode (int_to_alternate_base64, (const u8 *) tmp, HASH_LEN_RAW, (u8 *) hash_enc);
   
   // substitute + with . and remove padding =
-  for (int i = 0; i < salt_len_enc; i++) 
+  for (size_t i = 0; i < salt_len_enc; i++) 
   {
-    if (salt_enc[i] == '+')
-    { 
-      salt_enc[i] = '.';
-    }
     if (salt_enc[i] == '=') 
     {
-	  salt_enc[i] = '\0';
-	}
+      salt_enc[i] = '\0';
+    }
   }
 
-  for (int i = 0; i < hash_len_enc; i++) 
+  for (size_t i = 0; i < hash_len_enc; i++) 
   {
-    if (hash_enc[i] == '+') 
-    {
-	  hash_enc[i] = '.';
-	}
     if (hash_enc[i] == '=') 
     {
-	  hash_enc[i] = '\0';
-	}  
+      hash_enc[i] = '\0';
+    }  
   }
   
   // output