Skip to content

Commit

Permalink
Rework some hashfunc_t stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpoike committed Apr 17, 2023
1 parent 1fe478d commit c5f837d
Show file tree
Hide file tree
Showing 26 changed files with 182 additions and 276 deletions.
4 changes: 2 additions & 2 deletions engine/client/cl_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -3009,9 +3009,9 @@ void CL_QTVPoll (void)
else if (!strcmp(auth, "SHA1"))
hashfunc = &hash_sha1;
else if (!strcmp(auth, "SHA2_256"))
hashfunc = &hash_sha256;
hashfunc = &hash_sha2_256;
else if (!strcmp(auth, "SHA2_512"))
hashfunc = &hash_sha512;
hashfunc = &hash_sha2_512;
else if (*auth)
Con_Printf("Server requires unsupported auth method: %s\n", auth);

Expand Down
16 changes: 8 additions & 8 deletions engine/client/cl_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,9 @@ char *CL_GUIDString(netadr_t *adr)
{
static qbyte buf[2048];
static int buflen;
unsigned int digest[4];
qbyte digest[DIGEST_MAXSIZE];
char serveraddr[256];
void *blocks[2];
int lens[2];
void *ctx;

if (!*cl_sendguid.string && *connectinfo.ext.guidsalt)
{
Expand Down Expand Up @@ -587,11 +586,12 @@ char *CL_GUIDString(netadr_t *adr)
}
}

blocks[0] = buf;lens[0] = buflen;
blocks[1] = serveraddr;lens[1] = strlen(serveraddr);
Com_BlocksChecksum(2, blocks, lens, (void*)digest);

Q_snprintfz(connectinfo.guid, sizeof(connectinfo.guid), "%08x%08x%08x%08x", digest[0], digest[1], digest[2], digest[3]);
ctx = alloca(hash_md4.contextsize);
hash_md4.init(ctx);
hash_md4.process(ctx, buf, buflen);
hash_md4.process(ctx, serveraddr, strlen(serveraddr));
hash_md4.terminate(digest, ctx);
Base16_EncodeBlock(digest, hash_md4.digestsize, connectinfo.guid, sizeof(connectinfo.guid));
return connectinfo.guid;
}

Expand Down
4 changes: 2 additions & 2 deletions engine/client/m_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ static qboolean PM_ParsePackageList(const char *f, unsigned int parseflags, cons
size_t signsize;
enum hashvalidation_e r;
int i;
hashfunc_t *hf = &hash_sha512;
hashfunc_t *hf = &hash_sha2_512;
void *hashdata = Z_Malloc(hf->digestsize);
void *hashctx = Z_Malloc(hf->contextsize);
tokstart = COM_StringParse (tokstart, authority, sizeof(authority), false, false);
Expand Down Expand Up @@ -4071,7 +4071,7 @@ static void PM_StartADownload(void)
}

if (p->filesha512 && tmpfile)
tmpfile = FS_Hash_ValidateWrites(tmpfile, p->name, p->filesize, &hash_sha512, p->filesha512);
tmpfile = FS_Hash_ValidateWrites(tmpfile, p->name, p->filesize, &hash_sha2_512, p->filesha512);
else if (p->filesha1 && tmpfile)
tmpfile = FS_Hash_ValidateWrites(tmpfile, p->name, p->filesize, &hash_sha1, p->filesha1);

Expand Down
2 changes: 1 addition & 1 deletion engine/client/pr_csqc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7997,7 +7997,7 @@ static qboolean CSQC_ValidateMainCSProgs(void *file, size_t filesize, unsigned i
}
else
{ //FTE uses folded-md4. yeah, its broken but at least its still more awkward
if (LittleLong(Com_BlockChecksum(file, filesize)) != checksum)
if (LittleLong(CalcHashInt(&hash_md4, file, filesize)) != checksum)
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion engine/client/sys_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ static void DoSign(const char *fname, int signtype)
}
else if (f)
{
hashfunc_t *h = (signtype==1)?&hash_sha256:&hash_sha512;
hashfunc_t *h = (signtype==1)?&hash_sha2_256:&hash_sha2_512;
size_t l, ts = 0;
void *ctx = alloca(h->contextsize);
qbyte data[65536*16];
Expand Down
2 changes: 1 addition & 1 deletion engine/common/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ static void Cmd_Exec_f (void)
Cbuf_InsertText (fs_manifest->defaultoverrides, level, false);

#if defined(HAVE_LEGACY) && defined(HAVE_CLIENT)
if (l == 1914 && Com_BlockChecksum(f, l) == 0x2d7b72b9)
if (l == 1914 && CalcHashInt(&hash_md4, f, l) == 0x2d7b72b9)
s = (char*)replacementq1binds;
#endif
}
Expand Down
19 changes: 9 additions & 10 deletions engine/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,12 @@ void InfoBuf_WriteToFile(vfsfile_t *f, infobuf_t *info, const char *commandname,
void InfoBuf_Enumerate (infobuf_t *info, void *ctx, void(*cb)(void *ctx, const char *key, const char *value));


void Com_BlocksChecksum (int blocks, void **buffer, int *len, unsigned char *outbuf);
unsigned int Com_BlockChecksum (const void *buffer, int length);
void Com_BlockFullChecksum (const void *buffer, int len, unsigned char *outbuf);
qbyte COM_BlockSequenceCheckByte (qbyte *base, int length, int sequence, unsigned mapchecksum);
qbyte COM_BlockSequenceCRCByte (qbyte *base, int length, int sequence);
qbyte Q2COM_BlockSequenceCRCByte (qbyte *base, int length, int sequence);

size_t Base64_EncodeBlock(const qbyte *in, size_t length, char *out, size_t outsize); //tries to null terminate, but returns length without termination.
size_t Base64_EncodeBlockURI(const qbyte *in, size_t length, char *out, size_t outsize); //slightly different chars for uri safety. also trims.
size_t Base64_DecodeBlock(const char *in, const char *in_end, qbyte *out, size_t outsize); // +/ and =
size_t Base16_EncodeBlock(const char *in, size_t length, qbyte *out, size_t outsize);
size_t Base16_DecodeBlock(const char *in, qbyte *out, size_t outsize);
Expand All @@ -951,15 +949,16 @@ typedef struct
void (*process) (void *context, const void *data, size_t datasize);
void (*terminate) (unsigned char *digest, void *context);
} hashfunc_t;
extern hashfunc_t hash_sha1;
extern hashfunc_t hash_sha224;
extern hashfunc_t hash_sha256;
extern hashfunc_t hash_sha384;
extern hashfunc_t hash_sha512;
extern hashfunc_t hash_crc16;
extern hashfunc_t hash_md4; //required for vanilla qw mapchecks
extern hashfunc_t hash_sha1; //required for websockets, and ezquake's crypted rcon
extern hashfunc_t hash_sha2_224;
extern hashfunc_t hash_sha2_256; //required for webrtc
extern hashfunc_t hash_sha2_384;
extern hashfunc_t hash_sha2_512;
extern hashfunc_t hash_crc16; //aka ccitt, required for qw's clc_move and various bits of dp compat
extern hashfunc_t hash_crc16_lower;
unsigned int hashfunc_terminate_uint(const hashfunc_t *hash, void *context); //terminate, except returning the digest as a uint instead of a blob. folds the digest if longer than 4 bytes.
unsigned int CalcHashInt(const hashfunc_t *hash, const unsigned char *data, size_t datasize);
unsigned int CalcHashInt(const hashfunc_t *hash, const void *data, size_t datasize);
size_t CalcHash(const hashfunc_t *hash, unsigned char *digest, size_t maxdigestsize, const unsigned char *data, size_t datasize);
size_t CalcHMAC(const hashfunc_t *hashfunc, unsigned char *digest, size_t maxdigestsize, const unsigned char *data, size_t datalen, const unsigned char *key, size_t keylen);

Expand Down
8 changes: 4 additions & 4 deletions engine/common/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,10 +1330,10 @@ static void COM_CalcHash_Thread(void *ctx, void *fname, size_t a, size_t b)
// {"crc16", &hash_crc16},
{"sha1", &hash_sha1},
#if defined(HAVE_SERVER) || defined(HAVE_CLIENT)
// {"sha224", &hash_sha224},
{"sha256", &hash_sha256},
// {"sha384", &hash_sha384},
// {"sha512", &hash_sha512},
// {"sha224", &hash_sha2_224},
{"sha256", &hash_sha2_256},
// {"sha384", &hash_sha2_384},
// {"sha512", &hash_sha2_512},
#endif
};
qbyte digest[DIGEST_MAXSIZE];
Expand Down
4 changes: 2 additions & 2 deletions engine/common/fs_dzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,9 +1235,9 @@ static int QDECL FSDZ_GeneratePureCRC(searchpathfuncs_t *handle, int seed, int c
}

if (crctype)
result = Com_BlockChecksum(filecrcs, numcrcs*sizeof(int));
result = CalcHashInt(&hash_md4, filecrcs, numcrcs*sizeof(int));
else
result = Com_BlockChecksum(filecrcs+1, (numcrcs-1)*sizeof(int));
result = CalcHashInt(&hash_md4, filecrcs+1, (numcrcs-1)*sizeof(int));

BZ_Free(filecrcs);
return result;
Expand Down
4 changes: 2 additions & 2 deletions engine/common/fs_pak.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ static int QDECL FSPAK_GeneratePureCRC(searchpathfuncs_t *handle, int seed, int
}

if (crctype)
result = Com_BlockChecksum(filecrcs, numcrcs*sizeof(int));
result = CalcHashInt(&hash_md4, filecrcs, numcrcs*sizeof(int));
else
result = Com_BlockChecksum(filecrcs+1, (numcrcs-1)*sizeof(int));
result = CalcHashInt(&hash_md4, filecrcs+1, (numcrcs-1)*sizeof(int));

BZ_Free(filecrcs);
return result;
Expand Down
4 changes: 2 additions & 2 deletions engine/common/fs_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ static int QDECL FSZIP_GeneratePureCRC(searchpathfuncs_t *handle, int seed, int
}

if (crctype || numcrcs < 1)
result = Com_BlockChecksum(filecrcs, numcrcs*sizeof(int));
result = CalcHashInt(&hash_md4, filecrcs, numcrcs*sizeof(int));
else
result = Com_BlockChecksum(filecrcs+1, (numcrcs-1)*sizeof(int));
result = CalcHashInt(&hash_md4, filecrcs+1, (numcrcs-1)*sizeof(int));

BZ_Free(filecrcs);
return result;
Expand Down
2 changes: 1 addition & 1 deletion engine/common/gl_q2bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4517,7 +4517,7 @@ static cmodel_t *CM_LoadMap (model_t *mod, qbyte *filein, size_t filelen, qboole
return NULL;
}

checksum = LittleLong (Com_BlockChecksum (buf, length));
checksum = LittleLong (CalcHashInt(&hash_md4, buf, length));

header = *(q2dheader_t *)(buf);
header.ident = LittleLong(header.ident);
Expand Down
55 changes: 14 additions & 41 deletions engine/common/md4.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef struct {
} MD4_CTX;

void MD4Init (MD4_CTX *);
void MD4Update (MD4_CTX *, unsigned char *, unsigned int);
void MD4Update (MD4_CTX *, const unsigned char *, size_t);
void MD4Final (unsigned char [16], MD4_CTX *);


Expand Down Expand Up @@ -84,9 +84,9 @@ These notices must be retained in any copies of any part of this documentation a
#define S33 11
#define S34 15

static void MD4Transform (UINT4 [4], unsigned char [64]);
static void MD4Transform (UINT4 [4], const unsigned char [64]);
static void Encode (unsigned char *, UINT4 *, unsigned int);
static void Decode (UINT4 *, unsigned char *, unsigned int);
static void Decode (UINT4 *, const unsigned char *, unsigned int);

static unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Expand Down Expand Up @@ -122,7 +122,7 @@ context->state[3] = 0x10325476;
}

/* MD4 block update operation. Continues an MD4 message-digest operation, processing another message block, and updating the context. */
void MD4Update (MD4_CTX *context, unsigned char *input, unsigned int inputLen)
void MD4Update (MD4_CTX *context, const unsigned char *input, size_t inputLen)
{
unsigned int i, index, partLen;

Expand Down Expand Up @@ -182,7 +182,7 @@ void MD4Final (unsigned char digest[16], MD4_CTX *context)


/* MD4 basic transformation. Transforms state based on block. */
static void MD4Transform (UINT4 state[4], unsigned char block[64])
static void MD4Transform (UINT4 state[4], const unsigned char block[64])
{
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];

Expand Down Expand Up @@ -267,7 +267,7 @@ static void Encode (unsigned char *output, UINT4 *input, unsigned int len)


/* Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. */
static void Decode (UINT4 *output, unsigned char *input, unsigned int len)
static void Decode (UINT4 *output, const unsigned char *input, unsigned int len)
{
unsigned int i, j;

Expand All @@ -276,40 +276,13 @@ for (i = 0, j = 0; j < len; i++, j += 4)
}

//===================================================================

unsigned int Com_BlockChecksum (void *buffer, int length)
{
unsigned int digest[4];
unsigned int val;
MD4_CTX ctx;

MD4Init (&ctx);
MD4Update (&ctx, (unsigned char *)buffer, length);
MD4Final ( (unsigned char *)digest, &ctx);

val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];

return val;
}

void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf)
{
MD4_CTX ctx;

MD4Init (&ctx);
MD4Update (&ctx, (unsigned char *)buffer, len);
MD4Final ( outbuf, &ctx);
}


void Com_BlocksChecksum (int blocks, void **buffer, int *len, unsigned char *outbuf)
#include "quakedef.h"
hashfunc_t hash_md4 =
{
MD4_CTX ctx;
16, //digest size
sizeof(MD4_CTX),
(void(*)(void*ctx))MD4Init,
(void(*)(void*ctx,const void*in,size_t))MD4Update,
(void(*)(qbyte*out,void*ctx))MD4Final,
};

MD4Init (&ctx);
while(blocks --> 0)
{
MD4Update (&ctx, (unsigned char *)*buffer++, *len++);
}
MD4Final (outbuf, &ctx);
}
Loading

0 comments on commit c5f837d

Please sign in to comment.