Skip to content

Commit 6f3e608

Browse files
author
Colum Paget
committed
v1.21. fixes for -hmac, openssl hash support, -sha384 added
1 parent d2c9eb6 commit 6f3e608

15 files changed

+188
-24
lines changed

CHANGELOG

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v1.21 (2024-06-20)
2+
* fix -hmac option that's been broken by changes in libUseful
3+
* add '-list-hashes' option to list available has types
4+
* add -sha384 option
5+
* openssl-provided hashes now supported
6+
17
v1.20 (2024-06-18)
28
* Bring "make check" (check.sh) up to date with latest functioning of hashrat
39
* Compile if openssl not available (update to latest libUseful)

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CC = gcc
2-
CFLAGS = -g -O2 -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -fstack-protector-strong
2+
CFLAGS = -g -O2
33
LIBS = libUseful-5/libUseful.a -lssl -lcrypto -lz
44
INSTALL=/usr/bin/install -c
55
prefix=/usr/local

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ OPTIONS
5050
-? Print this help
5151
--version Print program version
5252
-version Print program version
53+
-list-hashes List available hash functions, including those supported by openssl
54+
-type <hash> Hash using supplied type. This supports chaining hashes like so: '-type sha512,md5'
5355
-md5 Use md5 hash algorithmn
5456
-sha1 Use sha1 hash algorithmn
5557
-sha256 Use sha256 hash algorithmn
58+
-sha384 Use sha256 hash algorithmn
5659
-sha512 Use sha512 hash algorithmn
5760
-whirl Use whirlpool hash algorithmn
5861
-whirlpool Use whirlpool hash algorithmn
@@ -159,6 +162,7 @@ Hashrat can also detect if it's being run under any of the following names (e.g.
159162
shasum run with '-trad -sha1'
160163
sha1sum run with '-trad -sha1'
161164
sha256sum run with '-trad -sha256'
165+
sha384sum run with '-trad -sha256'
162166
sha512sum run with '-trad -sha512'
163167
jh224sum run with '-trad -jh224'
164168
jh256sum run with '-trad -jh256'
@@ -250,6 +254,31 @@ USE EXAMPLES
250254
Search for duplicate files under /home. Update hashes stored in filesystem attributes as you go
251255
```
252256

257+
258+
OPENSSL HASHES
259+
==============
260+
261+
From v1.21 hashrat supports using hash functions supplied by openssl (provided it's been compiled with --enable-openssl). A list of available hashes can be viewed with `hashrat -list-hashes` and then any listed hash function can be used via the type option.
262+
263+
e.g.
264+
265+
`hashrat -type openssl:shake128`
266+
267+
268+
269+
CHAINING HASHES
270+
===============
271+
272+
Hashes can be 'chained' (fed into each other) using the '-type' option and a comma-separated list of hash names.
273+
274+
e.g.
275+
276+
`hashrat -type sha512,sha512,whirl,md5`
277+
278+
this would pipe any input into sha512, then the output of that goes into another sha512 round, then into whirlpool, and finally md5
279+
280+
281+
253282
USES FOR HASHRAT
254283
================
255284

check.sh

+42
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ do
4646
if [ "$HR_OUT" != "$3" ]
4747
then
4848
RESULT=FAIL
49+
break
4950
fi
5051
I=`expr $I + 1`
5152
done
@@ -60,6 +61,37 @@ fi
6061
}
6162

6263

64+
TestHMAC()
65+
{
66+
RESULT=PASS
67+
68+
if [ "$2" = "" ]
69+
then
70+
MSG="$1 HMAC"
71+
else
72+
MSG="$2"
73+
fi
74+
75+
#this test is compatible with those on https://en.wikipedia.org/wiki/HMAC
76+
HR_OUT=`echo -n "The quick brown fox jumps over the lazy dog" | ./hashrat -$1 -hmac key`
77+
78+
if [ "$HR_OUT" != "$3" ]
79+
then
80+
RESULT=FAIL
81+
fi
82+
83+
84+
if [ "$RESULT" = "FAIL" ]
85+
then
86+
FailMessage "$MSG BROKEN"
87+
else
88+
OkayMessage "$MSG works"
89+
fi
90+
}
91+
92+
93+
94+
6395
TestLocate()
6496
{
6597
HR_OUT=`echo $1 | ./hashrat -m -md5 -r .`
@@ -136,6 +168,7 @@ Title "Testing Hash Types"
136168
TestHash md5 "" 68e88e7b46a0fbd8a54c8932d2a9710d
137169
TestHash sha1 "" d27f161a82d2834afccda6bfc1d10b2024fc6ec0
138170
TestHash sha256 "" c7fadad016311a935a56dcdfb585cf5a4781073f7da13afa22177796e566434f
171+
TestHash sha384 "" 74bad027d593889aecd64042cdad05d01792e8f36f2c65f19cbfce2e4a61ef72bccc1eea4188e59ed03d711daa1410f6
139172
TestHash sha512 "" 0b8ac7af4b8e2dc449781888287aa50e9501b68766254b0c1bc6e17e7e86288c0a83b03d34f9c4c32836ca00a026323d8bbafc39f0c50f0c6b19200a28095595
140173
TestHash whirlpool "" b690486285b18a9cbea3105a8f7e8ee439ef878530fe2e389e0b5ab17658df79ad6c83c1f836f81f51ce5c73a6899f0355fdad9f257526fc718ea04f7aa1b792
141174
TestHash jh224 "" af0d674cdaaa7ec27b9c80acc763c6d51301c4273cd929fe043f67ca
@@ -146,9 +179,18 @@ TestHash jh512 "" 05feebb3148d9b0d12025759e4e054fe851dc6ad5bf58d3f79afb7d61caf8c
146179
Title "Testing Repeated Iterations (may take some time)"
147180
TestHash md5 "1000 md5" 68e88e7b46a0fbd8a54c8932d2a9710d 1000
148181
TestHash sha1 "1000 sha1" d27f161a82d2834afccda6bfc1d10b2024fc6ec0 1000
182+
TestHash sha256 "1000 sha256" c7fadad016311a935a56dcdfb585cf5a4781073f7da13afa22177796e566434f 1000
183+
TestHash sha384 "1000 sha384" 74bad027d593889aecd64042cdad05d01792e8f36f2c65f19cbfce2e4a61ef72bccc1eea4188e59ed03d711daa1410f6 1000
149184
TestHash whirlpool "1000 whirlpool" b690486285b18a9cbea3105a8f7e8ee439ef878530fe2e389e0b5ab17658df79ad6c83c1f836f81f51ce5c73a6899f0355fdad9f257526fc718ea04f7aa1b792 1000
150185
TestHash jh384 "1000 jh384" 55c63e4c22303227495c076ba0b11cda09a77856b98ee7d285283509415ca47141b09136daaada9fa3f10522456484db 1000
151186

187+
Title "Testing HMAC digests"
188+
TestHMAC md5 "" 80070713463e7749b90c2dc24911e275
189+
TestHMAC sha1 "" de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9
190+
TestHMAC sha256 "" f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8
191+
TestHMAC sha384 "" d7f4727e2c0b39ae0f1e40cc96f60242d5b7801841cea6fc592c5d3e1ae50700582a96cf35e1e554995fe4e03381c237
192+
TestHMAC sha512 "" b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a
193+
152194
Title "Testing Encoding"
153195
TestHash 8 "base 8 (octal) encoding" 322177026032202322203112374315246277301321013040044374156300
154196
TestHash 10 "base 10 (decimal) encoding" 210127022026130210131074252205166191193209011032036252110192

command-line-args.c

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ HashratCtx *CommandLineParseArg0()
167167
)
168168
CommandLineHandleArg(Ctx,FLAG_TRAD_OUTPUT, "HashType", "sha1",Ctx->Vars);
169169
if (strcmp(ptr,"sha256sum")==0) CommandLineHandleArg(Ctx,FLAG_TRAD_OUTPUT, "HashType", "sha256",Ctx->Vars);
170+
if (strcmp(ptr,"sha384sum")==0) CommandLineHandleArg(Ctx,FLAG_TRAD_OUTPUT, "HashType", "sha384",Ctx->Vars);
170171
if (strcmp(ptr,"sha512sum")==0) CommandLineHandleArg(Ctx,FLAG_TRAD_OUTPUT, "HashType", "sha512",Ctx->Vars);
171172
if (strcmp(ptr,"whirlpoolsum")==0) CommandLineHandleArg(Ctx,FLAG_TRAD_OUTPUT, "HashType", "whirlpool",Ctx->Vars);
172173
if (strcmp(ptr,"jh224sum")==0) CommandLineHandleArg(Ctx,FLAG_TRAD_OUTPUT, "HashType", "jh-224",Ctx->Vars);
@@ -241,10 +242,12 @@ HashratCtx *CommandLineParseArgs(int argc, char *argv[])
241242
else if (strcmp(arg,"-cB")==0) Ctx->Action = ACT_CHECKBACKUP;
242243
else if (strcmp(arg,"-cgi")==0) Ctx->Action = ACT_CGI;
243244
else if (strcmp(arg,"-xdialog")==0) Ctx->Action = ACT_XDIALOG;
245+
else if (strcmp(arg,"-list-hashes")==0) Ctx->Action = ACT_LIST_TYPES;
244246
else if (strcmp(arg,"-md5")==0) CommandLineHandleArg(Ctx,0, "HashType", "md5",Ctx->Vars);
245247
else if (strcmp(arg,"-sha")==0) CommandLineHandleArg(Ctx,0, "HashType", "sha1",Ctx->Vars);
246248
else if (strcmp(arg,"-sha1")==0) CommandLineHandleArg(Ctx,0, "HashType", "sha1",Ctx->Vars);
247249
else if (strcmp(arg,"-sha256")==0) CommandLineHandleArg(Ctx,0, "HashType", "sha256",Ctx->Vars);
250+
else if (strcmp(arg,"-sha384")==0) CommandLineHandleArg(Ctx,0, "HashType", "sha384",Ctx->Vars);
248251
else if (strcmp(arg,"-sha512")==0) CommandLineHandleArg(Ctx,0, "HashType", "sha512",Ctx->Vars);
249252
else if (strcmp(arg,"-whirl")==0) CommandLineHandleArg(Ctx,0, "HashType", "whirlpool",Ctx->Vars);
250253
else if (strcmp(arg,"-whirlpool")==0) CommandLineHandleArg(Ctx,0, "HashType", "whirlpool",Ctx->Vars);
@@ -444,9 +447,12 @@ void CommandLinePrintUsage()
444447
printf(" %-15s %s\n","-?", "Print this help");
445448
printf(" %-15s %s\n","--version", "Print program version");
446449
printf(" %-15s %s\n","-version", "Print program version");
450+
printf(" %-15s %s\n","-list-hashes", "Print a list of hashes that can be used with the '-type' option");
451+
printf(" %-15s %s\n","-type <hash>", "specify a hash type to use. This supports hashes coming from other subsystems, such as openssl. It also supports 'chaining' hash types like so: -type sha256,whirl");
447452
printf(" %-15s %s\n","-md5", "Use md5 hash algorithmn");
448453
printf(" %-15s %s\n","-sha1", "Use sha1 hash algorithmn");
449454
printf(" %-15s %s\n","-sha256", "Use sha256 hash algorithmn");
455+
printf(" %-15s %s\n","-sha384", "Use sha384 hash algorithmn");
450456
printf(" %-15s %s\n","-sha512", "Use sha512 hash algorithmn");
451457
printf(" %-15s %s\n","-whirl", "Use whirlpool hash algorithmn");
452458
printf(" %-15s %s\n","-whirlpool", "Use whirlpool hash algorithmn");
@@ -547,6 +553,7 @@ void CommandLinePrintUsage()
547553
printf(" %-15s %s\n","shasum","run with '-trad -sha1'");
548554
printf(" %-15s %s\n","sha1sum","run with '-trad -sha1'");
549555
printf(" %-15s %s\n","sha256sum","run with '-trad -sha256'");
556+
printf(" %-15s %s\n","sha384sum","run with '-trad -sha384'");
550557
printf(" %-15s %s\n","sha512sum","run with '-trad -sha512'");
551558
printf(" %-15s %s\n","jh224sum","run with '-trad -jh224'");
552559
printf(" %-15s %s\n","jh256sum","run with '-trad -jh256'");

common.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ int MatchCount=0, DiffCount=0;
1111
time_t Now;
1212
uint64_t HashStartTime;
1313

14-
const char *HashratHashTypes[]= {"md5","sha1","sha256","sha512","whirl","whirlpool","jh-224","jh-256","jh-384","jh-512",NULL};
14+
const char *HashratHashTypes[]= {"md5","sha1","sha256","sha384","sha512","whirl","whirlpool","jh-224","jh-256","jh-384","jh-512",NULL};
1515

1616

1717

common.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "libUseful-5/libUseful.h"
66
#include "glob.h"
77

8-
#define VERSION "1.20"
8+
#define VERSION "1.21"
99

1010
#define ACT_NONE 0
1111
#define ACT_HASH 1
@@ -27,6 +27,7 @@
2727
#define ACT_BACKUP 24
2828
#define ACT_CHECKBACKUP 25
2929
#define ACT_OTP 26
30+
#define ACT_LIST_TYPES 27
3031

3132
#define FLAG_NEXTARG 1
3233
//Two flags with the same values, but used in different contexts

files.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ void ProcessData(char **RetStr, HashratCtx *Ctx, const char *Data, int DataLen)
283283

284284
ptr=GetVar(Ctx->Vars, "InputPrefix");
285285
if (StrValid(ptr)) Hash->Update(Hash,ptr, StrLen(ptr));
286-
Hash->Update(Hash,Data, DataLen);
286+
Hash->Update(Hash, Data, DataLen);
287287
HashratFinishHash(RetStr, Ctx, Hash);
288288
}
289289
}

find.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ void *MatchesLoad(HashratCtx *Ctx, int Flags)
266266
{
267267
StripTrailingWhitespace(Line);
268268
FP=TFingerprintParse(Line);
269-
if (FP)
270-
{
271-
if (MatchAdd(FP, "", Flags)) count++;
272-
//native format can specify the type of hash that it is supplying
273-
if (StrValid(FP->HashType)) Ctx->HashType=CopyStr(Ctx->HashType, FP->HashType);
269+
if (FP)
270+
{
271+
if (MatchAdd(FP, "", Flags)) count++;
272+
//native format can specify the type of hash that it is supplying
273+
if (StrValid(FP->HashType)) Ctx->HashType=CopyStr(Ctx->HashType, FP->HashType);
274274
}
275275
Line=STREAMReadLine(Line, S);
276276
}

libUseful-5/HMAC.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,13 @@ void HMACPrepare(HASH *HMAC, const char *Data, int Len)
9999
HASH *HMACInit(const char *Type)
100100
{
101101
HASH *Hash;
102+
const char *ptr;
102103

103104
Hash=(HASH *) calloc(1, sizeof(HASH));
104-
Hash->Ctx=(void *) HashInit(Type);
105-
Hash->Type=MCopyStr(Hash->Type, "hmac-", Type, NULL);
105+
if (strncasecmp(Type, "hmac-", 5)==0) ptr=Type+5;
106+
else ptr=Type;
107+
Hash->Ctx=(void *) HashInit(ptr);
108+
Hash->Type=MCopyStr(Hash->Type, "hmac-", ptr, NULL);
106109
Hash->Update=HMACPrepare;
107110
Hash->Finish=HMACFinish;
108111

libUseful-5/Hash.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "HashJH.h"
99
#include "HashWhirlpool.h"
1010
#include "HashOpenSSL.h"
11-
11+
#include "HMAC.h"
1212

1313
static ListNode *HashTypes=NULL;
1414

@@ -83,7 +83,10 @@ HASH *HashInit(const char *Type)
8383

8484
if (! HashTypes) HashRegisterAll();
8585

86-
GetToken(Type, ",", &InitialType, 0);
86+
GetToken(Type, ",", &InitialType, 0);
87+
if (strncmp(InitialType, "hmac-", 5) == 0) Hash=HMACInit(InitialType+5);
88+
else
89+
{
8790
Node=ListFindNamedItem(HashTypes, InitialType);
8891
if (Node)
8992
{
@@ -98,6 +101,7 @@ HASH *HashInit(const char *Type)
98101
}
99102
}
100103
else RaiseError(0, "HashInit", "Unsupported Hash Type: '%s'", InitialType);
104+
}
101105

102106
Destroy(InitialType);
103107

libUseful-5/HashOpenSSL.c

+28-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44
#include <openssl/evp.h>
55
#include <openssl/objects.h>
66

7+
static void OpenSSLFreeHashCTX(HASH *Hash)
8+
{
9+
#ifdef HAVE_EVP_MD_CTX_FREE
10+
EVP_MD_CTX_free(Hash->Ctx);
11+
#else
12+
EVP_MD_CTX_destroy(Hash->Ctx);
13+
#endif
14+
15+
Hash->Ctx=NULL;
16+
}
17+
718

819
static int OpenSSLFinishHash(HASH *Hash, char **Digest)
920
{
1021
int Len;
1122

1223
*Digest=SetStrLen(*Digest, EVP_MAX_MD_SIZE);
1324
EVP_DigestFinal((EVP_MD_CTX *) Hash->Ctx, *Digest, &Len);
25+
OpenSSLFreeHashCTX(Hash);
1426

15-
#ifdef HAVE_EVP_MD_CTX_FREE
16-
EVP_MD_CTX_free(Hash->Ctx);
17-
#else
18-
EVP_MD_CTX_destroy(Hash->Ctx);
19-
#endif
20-
21-
Hash->Ctx=NULL;
2227
return(Len);
2328
}
2429

@@ -44,22 +49,35 @@ static int OpenSSLInitHash(HASH *Hash, const char *Name, int Size)
4449
Hash->Ctx=(EVP_MD_CTX *) EVP_MD_CTX_create();
4550
#endif
4651

47-
EVP_DigestInit(Hash->Ctx, MD);
52+
if (! EVP_DigestInit(Hash->Ctx, MD))
53+
{
54+
OpenSSLFreeHashCTX(Hash);
55+
return(FALSE);
56+
}
57+
4858
Hash->Update=OpenSSLUpdateHash;
4959
Hash->Finish=OpenSSLFinishHash;
60+
5061
return(TRUE);
5162
}
5263
return(FALSE);
5364
}
5465

66+
//this function gets 'called back' by the call to 'OBJ_NAME_do_all' in HashRegisterOpenSSL
67+
//and is called for each algorithm name that openssl supports
5568
static void OpenSSLDigestCallback(const OBJ_NAME *obj, void *arg)
5669
{
5770
char *Tempstr=NULL;
71+
HASH *Hash;
5872

59-
73+
Hash=(HASH *) calloc(1, sizeof(HASH));
74+
if (OpenSSLInitHash(Hash, obj->name, 0))
75+
{
6076
HashRegister(obj->name, 0, OpenSSLInitHash);
6177
Tempstr=MCopyStr(Tempstr, "openssl:", obj->name, NULL);
6278
HashRegister(Tempstr, 0, OpenSSLInitHash);
79+
}
80+
OpenSSLFreeHashCTX(Hash);
6381

6482
Destroy(Tempstr);
6583
}
@@ -69,6 +87,6 @@ void HashRegisterOpenSSL()
6987
{
7088
#ifdef HAVE_LIBSSL
7189
OpenSSL_add_all_digests(); //make sure they're loaded
72-
OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, OpenSSLDigestCallback, NULL);
90+
OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, OpenSSLDigestCallback, NULL);
7391
#endif
7492
}

0 commit comments

Comments
 (0)