From 55674c369be3dadf1bd2a64cfeb209705ee52d46 Mon Sep 17 00:00:00 2001 From: LDA Date: Mon, 26 Aug 2024 21:07:55 +0200 Subject: [PATCH] [MOD] Avoid allocating memory in HMAC --- Makefile | 2 +- src/Main.c | 9 +++++---- src/Parsee/HMAC.c | 29 ++++++++++++----------------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 82d651a..c76e361 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ all: binary utils binary: $(OBJ_FILES) $(CC) $(LDFLAGS) $(OBJ_FILES) -o $(BINARY) -tags: +tags: $(SRC_FILES) @ctags --recurse $(SOURCE)/ clean: diff --git a/src/Main.c b/src/Main.c index 3303db1..fd599ba 100644 --- a/src/Main.c +++ b/src/Main.c @@ -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 diff --git a/src/Parsee/HMAC.c b/src/Parsee/HMAC.c index 3904ebc..efdd09a 100644 --- a/src/Parsee/HMAC.c +++ b/src/Parsee/HMAC.c @@ -8,13 +8,11 @@ #include -/* 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; }