From c744b597c4ff98989904b59191694f0d17af1246 Mon Sep 17 00:00:00 2001 From: theBigBlase Date: Fri, 6 Oct 2023 21:37:28 -0400 Subject: [PATCH] main: small changes, inling and auto mac hijacking --- Makefile | 2 +- src/main.c | 52 +++++++++++++++++++++++++++------------------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 9044067..9cef5d9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ OJB = $(SRC:.c=.o) OUT = build CC = /usr/bin/gcc -CFLAGS = -ansi -Wall -std=c99 -O0 +CFLAGS = -ansi -Wall -fsanitize=address -std=c99 -O0 CFDEBUG = -ansi -Wall -g -std=c99 RM = /bin/rm -fr TEST = -D TEST_TECH diff --git a/src/main.c b/src/main.c index 656189d..c731bd7 100644 --- a/src/main.c +++ b/src/main.c @@ -27,15 +27,13 @@ struct eve { uint32_t tag; }; -static uint32_t mac(uint32_t, uint32_t); -static char verif(uint32_t, uint32_t, uint32_t); +static inline uint32_t mac(uint32_t, uint32_t); +static inline char verif(uint32_t, uint32_t, uint32_t); static inline uint32_t gen(); -static uint32_t char_arr_to_uint32(char *); +static inline uint32_t char_arr_to_uint32(char *); static struct eve eve_shenanigans(struct eve); static char * uint32_to_char_arr(uint32_t n); -static void print_hex(char *); - int main(int argc, char *argv[]) { @@ -72,8 +70,12 @@ main(int argc, char *argv[]) printf("%u\n", msg); #endif + + char *msg_char = uint32_to_char_arr(msg); printf("as char *: %s\nas uint32: %u\n", - uint32_to_char_arr(msg), msg); + msg_char, msg); + + free(msg_char); uint32_t tag = mac(msg, key); printf("--- TAG ---\n%d \n", tag); @@ -97,13 +99,16 @@ main(int argc, char *argv[]) printf("knows key: %u\n", key); char res = verif(msg, tag, key); printf("verifies: %s\n", res ? "TRUE" : "FALSE"); - if (res) - printf("msg as char *: %s\n", uint32_to_char_arr(msg)); + if (res) { + msg_char = uint32_to_char_arr(msg); + printf("msg as char *: %s\n", msg_char); + free(msg_char); + } return 0; } -static uint32_t +static inline uint32_t gen() { /* rand is between 0 and UINT32_MAX - 1 / 2 (because sign) */ @@ -114,31 +119,39 @@ static struct eve eve_shenanigans(struct eve str) { #ifdef EVE_NAME + uint32_t old_msg = str.msg; str.msg = *(uint32_t *)"eve"; - str.tag = str.tag ^ (*(uint32_t *)"bob") ^ str.msg; + str.tag = str.tag ^ old_msg ^ str.msg; #endif #ifdef EVE_ZERO + uint32_t old_msg = str.msg; + str.msg = 0; + str.tag = str.tag ^ old_msg ^ str.msg; +#endif + +#ifdef EVE_MAX + uint32_t old_msg = str.msg; str.msg = UINT32_MAX; - str.tag = str.tag ^ 0 ^ str.msg; + str.tag = str.tag ^ old_msg ^ str.msg; #endif return str; } -static char +static inline char verif(uint32_t key, uint32_t msg, uint32_t tag) { return (key ^ msg) == tag; } -static uint32_t +static inline uint32_t mac(uint32_t key, uint32_t msg) { return msg ^ key; } -static uint32_t +static inline uint32_t char_arr_to_uint32(char *arr) { return *(uint32_t *)arr; @@ -155,14 +168,3 @@ uint32_to_char_arr(uint32_t n) return res; } - -static void -print_hex(char *str) -{ - short k = 0; - while (str[k] != '\0') { - printf("%02X ", str[k] & 0xff); - ++k; - } - printf("\n"); -}