[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) binary: $(OBJ_FILES)
$(CC) $(LDFLAGS) $(OBJ_FILES) -o $(BINARY) $(CC) $(LDFLAGS) $(OBJ_FILES) -o $(BINARY)
tags: tags: $(SRC_FILES)
@ctags --recurse $(SOURCE)/ @ctags --recurse $(SOURCE)/
clean: clean:

View file

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

View file

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