Skip to content

Commit

Permalink
Finish refactor hex2bin()
Browse files Browse the repository at this point in the history
  • Loading branch information
RealEnder committed May 19, 2024
1 parent 3cbacb5 commit d182085
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 69 deletions.
8 changes: 4 additions & 4 deletions hcxhash2cap.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,25 +806,25 @@ while(1)
pmkcapskipped++;
continue;
}
if(hex2bin(&linein[0], pmkid, 16) != true)
if(hex2bin(&linein[0], pmkid, 16) == -1)
{
fprintf(stderr, "reading hash line %d failed: %s\n", aktread, linein);
pmkcapskipped++;
continue;
}
if(hex2bin(&linein[33], macap, 6) != true)
if(hex2bin(&linein[33], macap, 6) == -1)
{
fprintf(stderr, "reading hash line %d failed: %s\n", aktread, linein);
pmkcapskipped++;
continue;
}
if(hex2bin(&linein[46], macsta, 6) != true)
if(hex2bin(&linein[46], macsta, 6) == -1)
{
fprintf(stderr, "reading hash line %d failed: %s\n", aktread, linein);
pmkcapskipped++;
continue;
}
if(hex2bin(&linein[59], essid, essidlen/2) != true)
if(hex2bin(&linein[59], essid, essidlen/2) == -1)
{
fprintf(stderr, "reading hash line %d failed: %s\n", aktread, linein);
pmkcapskipped++;
Expand Down
68 changes: 16 additions & 52 deletions hcxpmktool.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static uint8_t miccalculated[128];

static int psklen;
static char *pskstring;
static size_t essidlen;
static ssize_t essidlen;
static uint8_t essid[34];
static uint8_t macap[8];
static uint8_t macclient[8];
Expand Down Expand Up @@ -290,45 +290,11 @@ status |= HAS_PMK_CALC;
return true;
}
/*===========================================================================*/
static size_t hex2bin2(const char *str, uint8_t *bytes, size_t blen)
{
size_t pos;
uint8_t idx0;
uint8_t idx1;

uint8_t hashmap[] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // @ABCDEFG
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PQRSTUVW
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // XYZ[\]^_
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // `abcdefg
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno
};

if(ishexvalue(str, blen) == false) return 0;
memset(bytes, 0, blen);
pos = 0;
while(str[pos+0] != 0)
{
idx0 = ((uint8_t)str[pos+0] & 0x1F) ^ 0x10;
idx1 = ((uint8_t)str[pos+1] & 0x1F) ^ 0x10;
bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
pos += 2;
if(str[pos+0] == '*') return pos/2;
if(pos/2 > blen) return 0;
};
if((pos %2) != 0) return 0;
return pos/2;
}
/*===========================================================================*/
static bool parsehashlinestring(char *hashlinestring)
{
static size_t hlen;
static size_t plen;
static size_t flen;
static ssize_t flen;

static const char *wpa1 = "WPA*01*";
static const char *wpa2 = "WPA*02*";
Expand All @@ -338,21 +304,20 @@ if(hlen < 71) return false;
plen = 7;
if(memcmp(wpa1, hashlinestring, 7) == 0)
{
flen = hex2bin2(&hashlinestring[plen], pmkid, 16);
flen = hex2bin(&hashlinestring[plen], pmkid, 16);
if(flen != 16) return false;
plen += flen *2;
if(hashlinestring[plen++] != '*') return false;
flen = hex2bin2(&hashlinestring[plen], macap, 6);
flen = hex2bin(&hashlinestring[plen], macap, 6);
if(flen != 6) return false;
plen += flen *2;
if(hashlinestring[plen++] != '*') return false;
flen = hex2bin2(&hashlinestring[plen], macclient, 6);
flen = hex2bin(&hashlinestring[plen], macclient, 6);
if(flen != 6) return false;
plen += flen *2;
if(hashlinestring[plen++] != '*') return false;
essidlen = 0;
essidlen = hex2bin2(&hashlinestring[plen], essid, 34);
if((essidlen == 0) || (essidlen > 32)) return false;
essidlen = hex2bin(&hashlinestring[plen], essid, 34);
if((essidlen <= 0) || (essidlen > 32)) return false;
plen += essidlen *2;
if(hashlinestring[plen++] != '*') return false;
status |= HAS_PMKID_LINE;
Expand All @@ -364,28 +329,27 @@ if(memcmp(wpa1, hashlinestring, 7) == 0)
}
if(memcmp(wpa2, hashlinestring, 7) == 0)
{
flen = hex2bin2(&hashlinestring[plen], mic, 16);
flen = hex2bin(&hashlinestring[plen], mic, 16);
if(flen != 16) return false;
plen += flen *2;
if(hashlinestring[plen++] != '*') return false;
flen = hex2bin2(&hashlinestring[plen], macap, 6);
flen = hex2bin(&hashlinestring[plen], macap, 6);
if(flen != 6) return false;
plen += flen *2;
if(hashlinestring[plen++] != '*') return false;
flen = hex2bin2(&hashlinestring[plen], macclient, 6);
flen = hex2bin(&hashlinestring[plen], macclient, 6);
if(flen != 6) return false;
plen += flen *2;
if(hashlinestring[plen++] != '*') return false;
essidlen = 0;
essidlen = hex2bin2(&hashlinestring[plen], essid, 34);
if((essidlen == 0) || (essidlen > 32)) return false;
essidlen = hex2bin(&hashlinestring[plen], essid, 34);
if((essidlen <= 0) || (essidlen > 32)) return false;
plen += essidlen *2;
if(hashlinestring[plen++] != '*') return false;
flen = hex2bin2(&hashlinestring[plen], anonce, 32);
flen = hex2bin(&hashlinestring[plen], anonce, 32);
if(flen == -1) return false;
plen += flen *2;
if(hashlinestring[plen++] != '*') return false;
eapollen = 0;
eapollen = hex2bin2(&hashlinestring[plen], eapol, 1024);
eapollen = hex2bin(&hashlinestring[plen], eapol, 1024);
eapptr = (eapauth_t*)eapol;
eapauthlen = ntohs(eapptr->len);
if(eapollen < eapauthlen +4) return false;
Expand Down Expand Up @@ -588,7 +552,7 @@ if(pskstring != NULL)
}
else if(psklen == 64)
{
if(hex2bin2(pskstring, pmkcalculated, 32) != 32)
if(hex2bin(pskstring, pmkcalculated, 32) != 32)
{
fprintf(stderr, "\nPMK error\n");
return EXIT_FAILURE;
Expand Down
12 changes: 6 additions & 6 deletions hcxpsktool.c
Original file line number Diff line number Diff line change
Expand Up @@ -2540,7 +2540,7 @@ static void readpmkidfile(char *pmkidname)
{
static int len;
static int aktread = 1;
static int essidlen;
static ssize_t essidlen;
static char *macaddrstop = NULL;
static unsigned long long int macaddr;
static FILE *fh_file;
Expand Down Expand Up @@ -2583,7 +2583,7 @@ while(1)
aktread++;
continue;
}
if(hex2bin(&linein[59], essid, essidlen/2) == true) addapessid(macaddr, essidlen/2, essid);
if(hex2bin(&linein[59], essid, essidlen/2) != -1) addapessid(macaddr, essidlen/2, essid);
aktread++;
}
fclose(fh_file);
Expand All @@ -2594,7 +2594,7 @@ static void readpmkideapolfile(char *pmkideapolname)
{
static int len;
static int aktread = 1;
static int essidlen;
static ssize_t essidlen;
static char *macaddrstop = NULL;
static char *essidstop = NULL;
static unsigned long long int macaddr;
Expand Down Expand Up @@ -2653,7 +2653,7 @@ while(1)
aktread++;
continue;
}
if(hex2bin(&linein[66], essid, essidlen/2) == true) addapessid(macaddr, essidlen/2, essid);
if(hex2bin(&linein[66], essid, essidlen/2) != -1) addapessid(macaddr, essidlen/2, essid);
aktread++;
}
fclose(fh_file);
Expand Down Expand Up @@ -2791,7 +2791,7 @@ return;
static void readcommandline(char *macapname, char *essidname)
{
static int essidlen = 0;
static int essidlenuh = 0;
static ssize_t essidlenuh = 0;
static char *macaddrstop = NULL;
static unsigned long long int macaddr = 0xffffffffffffL;
static uint8_t essid[ESSID_LEN_MAX];
Expand All @@ -2808,7 +2808,7 @@ if(essidname != NULL)
essidlenuh = ishexify(essidname);
if((essidlenuh > 0) && (essidlenuh <= ESSID_LEN_MAX))
{
if(hex2bin(&essidname[5], essid, essidlenuh) == true) addapessid(macaddr, essidlenuh, essid);
if(hex2bin(&essidname[5], essid, essidlenuh) != -1) addapessid(macaddr, essidlenuh, essid);
return;
}
memset(&essid, 0, ESSID_LEN_MAX);
Expand Down
10 changes: 5 additions & 5 deletions include/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ for(i = 0; i < len; i++)
return true;
}
/*===========================================================================*/
bool hex2bin(const char *str, uint8_t *bytes, size_t blen)
ssize_t hex2bin(const char *str, uint8_t *bytes, size_t blen)
{
uint8_t pos;
uint8_t idx0;
uint8_t idx1;

uint8_t hashmap[] =
const uint8_t hashmap[] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
Expand All @@ -56,18 +56,18 @@ uint8_t hashmap[] =
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno
};

if(ishexvalue(str, blen) == false) return false;
if(ishexvalue(str, blen) == false) return -1;
memset(bytes, 0, blen);
for (pos = 0; ((pos < (blen*2)) && (pos < strlen(str))); pos += 2)
{
idx0 = ((uint8_t)str[pos+0] & 0x1F) ^ 0x10;
idx1 = ((uint8_t)str[pos+1] & 0x1F) ^ 0x10;
bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
};
return true;
return pos/2;
}
/*===========================================================================*/
size_t ishexify(const char *string)
ssize_t ishexify(const char *string)
{
size_t len;

Expand Down
4 changes: 2 additions & 2 deletions whoismac.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ for(p = 0; p < l; p++)
}

memset(&essidbuffer, 0, 66);
if(hex2bin(essidname, essidbuffer, l /2) == false)
if(hex2bin(essidname, essidbuffer, l /2) == -1)
{
fprintf(stderr, "not a valid ESSID hex string\n");
return;
Expand Down Expand Up @@ -155,7 +155,7 @@ if((l%2 != 0) || (l > 64))
return;
}
memset(&essidbuffer, 0, 66);
if(hex2bin(essidptr, essidbuffer, l /2) == false)
if(hex2bin(essidptr, essidbuffer, l /2) == -1)
{
fprintf(stderr, "wrong ESSID %s\n", essidptr);
return;
Expand Down

0 comments on commit d182085

Please sign in to comment.