main: small changes, inling and auto mac hijacking

This commit is contained in:
theBigBlase 2023-10-06 21:37:28 -04:00
parent 8a6ffdedb1
commit c744b597c4
2 changed files with 28 additions and 26 deletions

View file

@ -5,7 +5,7 @@ OJB = $(SRC:.c=.o)
OUT = build OUT = build
CC = /usr/bin/gcc CC = /usr/bin/gcc
CFLAGS = -ansi -Wall -std=c99 -O0 CFLAGS = -ansi -Wall -fsanitize=address -std=c99 -O0
CFDEBUG = -ansi -Wall -g -std=c99 CFDEBUG = -ansi -Wall -g -std=c99
RM = /bin/rm -fr RM = /bin/rm -fr
TEST = -D TEST_TECH TEST = -D TEST_TECH

View file

@ -27,15 +27,13 @@ struct eve {
uint32_t tag; uint32_t tag;
}; };
static uint32_t mac(uint32_t, uint32_t); static inline uint32_t mac(uint32_t, uint32_t);
static char verif(uint32_t, uint32_t, uint32_t); static inline char verif(uint32_t, uint32_t, uint32_t);
static inline uint32_t gen(); 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 struct eve eve_shenanigans(struct eve);
static char * uint32_to_char_arr(uint32_t n); static char * uint32_to_char_arr(uint32_t n);
static void print_hex(char *);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -72,8 +70,12 @@ main(int argc, char *argv[])
printf("%u\n", msg); printf("%u\n", msg);
#endif #endif
char *msg_char = uint32_to_char_arr(msg);
printf("as char *: %s\nas uint32: %u\n", 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); uint32_t tag = mac(msg, key);
printf("--- TAG ---\n%d \n", tag); printf("--- TAG ---\n%d \n", tag);
@ -97,13 +99,16 @@ main(int argc, char *argv[])
printf("knows key: %u\n", key); printf("knows key: %u\n", key);
char res = verif(msg, tag, key); char res = verif(msg, tag, key);
printf("verifies: %s\n", res ? "TRUE" : "FALSE"); printf("verifies: %s\n", res ? "TRUE" : "FALSE");
if (res) if (res) {
printf("msg as char *: %s\n", uint32_to_char_arr(msg)); msg_char = uint32_to_char_arr(msg);
printf("msg as char *: %s\n", msg_char);
free(msg_char);
}
return 0; return 0;
} }
static uint32_t static inline uint32_t
gen() gen()
{ {
/* rand is between 0 and UINT32_MAX - 1 / 2 (because sign) */ /* rand is between 0 and UINT32_MAX - 1 / 2 (because sign) */
@ -114,31 +119,39 @@ static struct eve
eve_shenanigans(struct eve str) eve_shenanigans(struct eve str)
{ {
#ifdef EVE_NAME #ifdef EVE_NAME
uint32_t old_msg = str.msg;
str.msg = *(uint32_t *)"eve"; str.msg = *(uint32_t *)"eve";
str.tag = str.tag ^ (*(uint32_t *)"bob") ^ str.msg; str.tag = str.tag ^ old_msg ^ str.msg;
#endif #endif
#ifdef EVE_ZERO #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.msg = UINT32_MAX;
str.tag = str.tag ^ 0 ^ str.msg; str.tag = str.tag ^ old_msg ^ str.msg;
#endif #endif
return str; return str;
} }
static char static inline char
verif(uint32_t key, uint32_t msg, uint32_t tag) verif(uint32_t key, uint32_t msg, uint32_t tag)
{ {
return (key ^ msg) == tag; return (key ^ msg) == tag;
} }
static uint32_t static inline uint32_t
mac(uint32_t key, uint32_t msg) mac(uint32_t key, uint32_t msg)
{ {
return msg ^ key; return msg ^ key;
} }
static uint32_t static inline uint32_t
char_arr_to_uint32(char *arr) char_arr_to_uint32(char *arr)
{ {
return *(uint32_t *)arr; return *(uint32_t *)arr;
@ -155,14 +168,3 @@ uint32_to_char_arr(uint32_t n)
return res; return res;
} }
static void
print_hex(char *str)
{
short k = 0;
while (str[k] != '\0') {
printf("%02X ", str[k] & 0xff);
++k;
}
printf("\n");
}