Skip to content

Commit

Permalink
use a single sha1-context
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Sep 25, 2024
1 parent 771d699 commit 8e06ac5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
25 changes: 13 additions & 12 deletions Source/codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ static void CodecInitKey(const char* pszPassword)
}

BYTE digest[SHA1HashSize];
SHA1Reset(0);
SHA1Calculate(0, pw, digest);
SHA1Reset(/*0*/);
SHA1Calculate(/*0,*/ pw, digest);
SHA1Clear();
for (unsigned i = 0; i < sizeof(key); ++i)
key[i] ^= digest[i % SHA1HashSize];
memset(pw, 0, sizeof(pw));
memset(digest, 0, sizeof(digest));
for (int n = 0; n < SHA1ContextNum; ++n) {
SHA1Reset(n);
SHA1Calculate(n, &key[sizeof(key) - SHA1BlockSize], NULL);
}
static_assert(SHA1ContextNum == 1, "CodecInitKey must initialize the sha1 contexts.");
// for (int n = 0; n < SHA1ContextNum; ++n) {
SHA1Reset(/*n*/);
SHA1Calculate(/*n,*/ &key[sizeof(key) - SHA1BlockSize], NULL);
// }
memset(key, 0, sizeof(key));
}

Expand All @@ -64,11 +65,11 @@ int codec_decode(BYTE* pbSrcDst, DWORD size, const char* pszPassword)
return 0;
for (i = size; i != 0; pbSrcDst += SHA1BlockSize, i -= SHA1BlockSize) {
memcpy(buf, pbSrcDst, SHA1BlockSize);
SHA1Result(0, dst);
SHA1Result(/*0, */dst);
for (int j = 0; j < SHA1BlockSize; j++) {
buf[j] ^= dst[j % SHA1HashSize];
}
SHA1Calculate(0, buf, NULL);
SHA1Calculate(/*0,*/ buf, NULL);
memcpy(pbSrcDst, buf, SHA1BlockSize);
}

Expand All @@ -78,7 +79,7 @@ int codec_decode(BYTE* pbSrcDst, DWORD size, const char* pszPassword)
goto error;
}

SHA1Result(0, dst);
SHA1Result(/*0,*/ dst);
if (sig->checksum != *(uint32_t*)dst) {
goto error;
}
Expand Down Expand Up @@ -116,8 +117,8 @@ void codec_encode(BYTE* pbSrcDst, DWORD size, DWORD encodedSize, const char* psz
memcpy(buf, pbSrcDst, chunk);
if (chunk < SHA1BlockSize)
memset(buf + chunk, 0, SHA1BlockSize - chunk);
SHA1Result(0, dst);
SHA1Calculate(0, buf, NULL);
SHA1Result(/*0,*/ dst);
SHA1Calculate(/*0,*/ buf, NULL);
for (int i = 0; i < SHA1BlockSize; i++) {
buf[i] ^= dst[i % SHA1HashSize];
}
Expand All @@ -126,7 +127,7 @@ void codec_encode(BYTE* pbSrcDst, DWORD size, DWORD encodedSize, const char* psz
size -= chunk;
}
memset(buf, 0, sizeof(buf));
SHA1Result(0, dst);
SHA1Result(/*0,*/ dst);
sig = (CodecSignature*)pbSrcDst;
sig->error = 0;
sig->unused = 0;
Expand Down
10 changes: 6 additions & 4 deletions Source/sha.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ void SHA1Clear()
memset(sgSHA1, 0, sizeof(sgSHA1));
}

void SHA1Result(int n, BYTE Message_Digest[SHA1HashSize])
void SHA1Result(/*int n,*/ BYTE Message_Digest[SHA1HashSize])
{
DWORD* Message_Digest_Block;
int i;
int i, n = 0;

assert(Message_Digest != NULL);
Message_Digest_Block = (DWORD*)Message_Digest;
Expand All @@ -145,15 +145,17 @@ void SHA1Result(int n, BYTE Message_Digest[SHA1HashSize])
}
}

void SHA1Calculate(int n, const BYTE* data, BYTE Message_Digest[SHA1HashSize])
void SHA1Calculate(/*int n,*/ const BYTE* data, BYTE Message_Digest[SHA1HashSize])
{
int n = 0;
SHA1Input(&sgSHA1[n], data, SHA1BlockSize);
if (Message_Digest != NULL)
SHA1Result(n, Message_Digest);
}

void SHA1Reset(int n)
void SHA1Reset(/*int n*/)
{
int n = 0;
SHA1Init(&sgSHA1[n]);
}

Expand Down
8 changes: 4 additions & 4 deletions Source/sha.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ DEVILUTION_BEGIN_NAMESPACE
extern "C" {
#endif

#define SHA1ContextNum 3
#define SHA1ContextNum 1
#define SHA1HashSize 20
#define SHA1BlockSize 64

void SHA1Clear();
void SHA1Result(int n, BYTE Message_Digest[SHA1HashSize]);
void SHA1Calculate(int n, const BYTE* data, BYTE Message_Digest[SHA1HashSize]);
void SHA1Reset(int n);
void SHA1Result(/*int n, */BYTE Message_Digest[SHA1HashSize]);
void SHA1Calculate(/*int n, */const BYTE* data, BYTE Message_Digest[SHA1HashSize]);
void SHA1Reset(/*int n*/);

#ifdef __cplusplus
}
Expand Down

0 comments on commit 8e06ac5

Please sign in to comment.