[MOD] Avoid allocating memory in HMAC

This commit is contained in:
LDA 2024-08-26 21:07:55 +02:00
commit 55674c369b
3 changed files with 18 additions and 22 deletions

View file

@ -41,7 +41,7 @@ all: binary utils
binary: $(OBJ_FILES)
$(CC) $(LDFLAGS) $(OBJ_FILES) -o $(BINARY)
tags:
tags: $(SRC_FILES)
@ctags --recurse $(SOURCE)/
clean:

View file

@ -42,12 +42,13 @@ static const Argument arguments[] =
Arg('J', true, "number(=8)", "Sets the number of XMPP threads")
Arg('C', true, "file(='parsee.json')", "Sets the JSON config to use")
Arg('g', false, NULL,"Generates a parsee.yaml AS file before exiting")
Arg('g', false, NULL,
"Generates a parsee.yaml AS file before exiting")
Arg('v', false, NULL,
"Forces Parsee to print in a more verbose fashion "
"(-vv prints stanzas to stderr)"
)
Arg('h', false, NULL,"Generates an help screen(this one!)")
"(-vv prints stanzas to stderr)")
Arg('h', false, NULL,
"Generates an help screen(this one!)")
EndOfArgs

View file

@ -8,13 +8,11 @@
#include <string.h>
/* A 64-byte key! */
static uint8_t *
ComputeKPad(char *key, uint8_t pad)
static void
ComputeKPad(char *key, uint8_t pad, uint8_t *kopad)
{
size_t klen;
uint8_t *kp;
uint8_t *kopad;
size_t i;
if ((klen = strlen(key)) <= 64)
{
@ -28,7 +26,6 @@ ComputeKPad(char *key, uint8_t pad)
}
/* Now that we have K', lets compute it XORd with opad */
kopad = Malloc(64 * sizeof(uint8_t));
for (i = 0; i < 64; i++)
{
uint8_t byte = i < klen ? kp[i] : 0x00;
@ -36,41 +33,39 @@ ComputeKPad(char *key, uint8_t pad)
}
Free(kp);
return kopad;
}
char *
ParseeHMAC(char *key, uint8_t *msg, size_t msglen)
{
uint8_t *opad, *ipad;
uint8_t opad[64], ipad[64 + msglen];
uint8_t outer[64 + 32];
uint8_t *innersha;
uint8_t *outer;
unsigned char *sha;
uint8_t *sha;
char *str;
if (!key || !msg || !msglen)
{
return NULL;
}
opad = ComputeKPad(key, 0x5C);
/* Initialise K' XOR opad and K' XOR ipad */
ComputeKPad(key, 0x5C, opad);
ComputeKPad(key, 0x36, ipad);
ipad = ComputeKPad(key, 0x36);
ipad = Realloc(ipad, 64 + msglen);
/* Compute H((K' XOR ipad) || msg) */
memcpy(ipad + 64, msg, msglen);
innersha = Sha256Raw(ipad, 64 + msglen);
outer = Malloc(64 + 32);
/* Compute (K' XOR opad) || H((K' XOR ipad) || msg) */
memcpy(outer, opad, 64);
memcpy(outer + 64, innersha, 32);
/* Compute H((K' XOR opad) || H((K' XOR ipad) || msg)) */
sha = Sha256Raw(outer, 64 + 32);
str = ShaToHex(sha, HASH_SHA256);
Free(innersha);
Free(outer);
Free(ipad);
Free(opad);
Free(sha);
return str;
}