commit 8a6ffdedb1a9842a617ab631e723d38164a2f38c Author: theBigBlase Date: Fri Oct 6 20:22:32 2023 -0400 main: need polish, almost done diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..01dec77 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +massif* +dhat* +.vimspector.json diff --git a/IFT814-Dev2-A23.pdf b/IFT814-Dev2-A23.pdf new file mode 100644 index 0000000..be548f0 Binary files /dev/null and b/IFT814-Dev2-A23.pdf differ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9044067 --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +.PHONY: all test clean build + +SRC = src/main.c +OJB = $(SRC:.c=.o) +OUT = build + +CC = /usr/bin/gcc +CFLAGS = -ansi -Wall -std=c99 -O0 +CFDEBUG = -ansi -Wall -g -std=c99 +RM = /bin/rm -fr +TEST = -D TEST_TECH +PART = -D PART_ +EVE = -D EVE_ + +%.o: %.c + $(CC) -c $(CFLAGS) + +build: + $(CC) $(SRC) $(PART)$(part) $(EVE)$(eve) $(CFLAGS) -o $(OUT)/main.o + +build_test: + $(CC) $(SRC) $(CFLAGS) $(TEST) -o $(OUT)/main.o + +debug: + $(CC) $(SRC) $(PART)$(part) $(CFDEBUG) -o $(OUT)/main.o + +clean: + $(RM) $(OBJ) $(OUT)/main.o + +run: + $(MAKE) build && ./build/main.o + +test: + $(MAKE) build_test && ./build/main.o diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..656189d --- /dev/null +++ b/src/main.c @@ -0,0 +1,168 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" (Revision 42): + * wrote this file. As long as you retain this notice you + * can do whatever you want with this stuff. If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. -violette paulin + * ---------------------------------------------------------------------------- + */ + +#include +#include +#include +#include +#include + +/* get bits after n from k */ +#define LAST_BITS(k,n) ((k) & ((1 << (n)) - 1)) +/* get bits of k between m and n */ +#define MID_BITS(k,m,n) LAST_BITS((k) >> (m), ((n) - (m))) + +#define SIZE_ALPH 26 +#define SIZE_MSG 32 +#define SIZE_OTP 64 + +struct eve { + uint32_t msg; + uint32_t tag; +}; + +static uint32_t mac(uint32_t, uint32_t); +static char verif(uint32_t, uint32_t, uint32_t); +static inline uint32_t gen(); +static 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[]) +{ + srand(time(0)); /* init rand */ + + uint32_t key = gen(); + uint32_t msg = 0; + +#ifdef TEST_TECH + char *test = "yay"; + printf("Testing char_arr_to_uint32 and uint32_to_char_arr: \n"); + printf("%s\n", uint32_to_char_arr(char_arr_to_uint32(test))); +#endif + + printf("\n--- MSG ---\nas 0x: "); +#ifdef PART_ZERO + msg = 0; + printf("0x00000000\n"); +#endif + +#ifdef PART_MAX + msg = UINT32_MAX; + printf("0xffffffff\n"); +#endif + +#ifdef PART_MID + printf("0x0000ffff\n"); + /* is UINT16_MAX, but keeping this way for corrector convenience */ + msg = UINT32_MAX & UINT16_MAX; +#endif + +#ifdef PART_NAME + msg = *(uint32_t *)"bob"; + printf("%u\n", msg); +#endif + + printf("as char *: %s\nas uint32: %u\n", + uint32_to_char_arr(msg), msg); + + uint32_t tag = mac(msg, key); + printf("--- TAG ---\n%d \n", tag); + + printf("--- VERIF ---\n%d\n", + verif(key, msg, tag)); + + printf("\n--- ALICE ---\n"); + printf("sends msg: %u\nsends tag: %u\n", msg, tag); + printf("knows key: %u\n", key); + + printf("\n--- EVE ---\n"); + printf("recieves msg: %u\nrecieves tag: %u\n", msg, tag); + struct eve tmp = { msg, tag }; + tmp = eve_shenanigans(tmp); + msg = tmp.msg, tag = tmp.tag; + printf("sends msg: %u\nsends tag: %u\n", msg, tag); + + printf("\n--- BOB ---\n"); + printf("recieves msg: %u\nrecieves tag: %u\n", msg, tag); + 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)); + + return 0; +} + +static uint32_t +gen() +{ + /* rand is between 0 and UINT32_MAX - 1 / 2 (because sign) */ + return rand() + rand() + (rand() % 1); +} + +static struct eve +eve_shenanigans(struct eve str) +{ +#ifdef EVE_NAME + str.msg = *(uint32_t *)"eve"; + str.tag = str.tag ^ (*(uint32_t *)"bob") ^ str.msg; +#endif + +#ifdef EVE_ZERO + str.msg = UINT32_MAX; + str.tag = str.tag ^ 0 ^ str.msg; +#endif + + return str; +} + +static char +verif(uint32_t key, uint32_t msg, uint32_t tag) +{ + return (key ^ msg) == tag; +} + +static uint32_t +mac(uint32_t key, uint32_t msg) +{ + return msg ^ key; +} + +static uint32_t +char_arr_to_uint32(char *arr) +{ + return *(uint32_t *)arr; +} + +static char * +uint32_to_char_arr(uint32_t n) +{ + char *res = calloc(4, 1); + res[0] = MID_BITS(n, 0, 8); + res[1] = MID_BITS(n, 8, 16); + res[2] = MID_BITS(n, 16, 24); + res[3] = MID_BITS(n, 24, 32); + + return res; +} + +static void +print_hex(char *str) +{ + short k = 0; + while (str[k] != '\0') { + printf("%02X ", str[k] & 0xff); + ++k; + } + printf("\n"); +}