From f8e94a1179633799579a09dce1841f41e3b73f05 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 6 Jan 2025 18:34:13 -0600 Subject: Start work on encryption --- src/Makefile | 8 +++++--- src/encryption.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/encryption.h | 7 +++++++ src/main.c | 55 ------------------------------------------------------- src/shared.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/shared.h | 18 ++++++++++++++---- 6 files changed, 119 insertions(+), 62 deletions(-) create mode 100644 src/encryption.c create mode 100644 src/encryption.h (limited to 'src') diff --git a/src/Makefile b/src/Makefile index d3798bb..a97848d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,15 +3,15 @@ SHELL = /usr/bin/bash DEBUG_CFLAGS := -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -Og RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto -CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors $(DEBUG_CFLAGS) +CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors $(DEBUG_CFLAGS) $$(pkg-config --cflags libsodium) DEBUG_LDLIBS := RELEASE_LDLIBS := -LDLIBS += $(DEBUG_LDLIBS) +LDLIBS += $(DEBUG_LDLIBS) $$(pkg-config --libs-only-l libsodium) DEBUG_LDFLAGS := RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto -LDFLAGS += $(DEBUG_LDFLAGS) +LDFLAGS += $(DEBUG_LDFLAGS) $$(pkg-config --libs-only-L libsodium) BINARIES := main @@ -25,5 +25,7 @@ main: main.o shared.o main.o: main.c shared.h shared.o: shared.c shared.h +encryption: encryption.c encryption.h shared.o shared.h + c clean: # huh, didn't think that would work rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file diff --git a/src/encryption.c b/src/encryption.c new file mode 100644 index 0000000..e92e4e7 --- /dev/null +++ b/src/encryption.c @@ -0,0 +1,51 @@ +#include "encryption.h" +#include "shared.h" + +#include + +#include +#include +#include +#include + +int checkSodium(void) { + int ret = sodium_init(); + if(ret < 0) + error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); + + return ret; +} + +// To encrypt: +// 1- Create a temp file with the correct name in the root folder of the partition being encrypted + // 1.1- Detect the partition and find the root folder + // 1.2- Create the temp file with the correct name +// 2- Encrypt the file's contents to the temp file + // 2.1- Open the file + // 2.2- Stream the file's contents into some encryption algo + // 2.3- Pipe the output of the encryption into the temp file +// 3- Once the file has been encrypted, hard link it back to the original location +// 4- Delete the original file +// 5- Delete the temp file + + +int maketmp(const char *dest, const char *format, ...) { + va_list ap; + va_start(ap, format); + + + + va_end(ap); + return 0; +} + + + +int main() { + char *test = NULL; + + saprintf(&test, "We do a little trolling %d", 900); + printf("%s\n", test); + + return 0; +} \ No newline at end of file diff --git a/src/encryption.h b/src/encryption.h new file mode 100644 index 0000000..945f73d --- /dev/null +++ b/src/encryption.h @@ -0,0 +1,7 @@ +#ifndef __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ +#define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ + +// Checks if sodium is initialized. Initializes it if not +int checkSodium(void); + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 4ecf545..3f6d566 100644 --- a/src/main.c +++ b/src/main.c @@ -4,64 +4,9 @@ #include #include -#include -#include -#include -#include -#include - -#include - -#include - -regex_t* initfilterregex() { - static regex_t *reg = NULL; - - if(reg != NULL) - return reg; - - reg = xcalloc(1, sizeof(*reg)); - if(regcomp(reg, "^(.{1,2})$", REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0) - return NULL; - - return reg; -} - -int testfilter(const struct dirent *node) { - regex_t *reg = initfilterregex(); - - if(regexec(reg, node->d_name, 0, NULL, 0) != (REG_NOMATCH | REG_NOERROR) && S_ISDIR(DTTOIF(node->d_type))) - return 0; - - return 1; -} - int main() { - // Alright, going to start simple. First: scanning for files. I want to do this quickly and in one motion - // No reason to do things in O(n2) time if I can do it in O(n) - - int nnodes = -1; - struct dirent **nodes = NULL; - if((nnodes = scandir(".", &nodes, testfilter, alphasort)) < 0) - error(1, errno, "scandir broke"); - - for(int i = 0; i < nnodes; i++) { - printf("\"%s\"", nodes[i]->d_name); - #if defined _DIRENT_HAVE_D_TYPE - int mode = DTTOIF(nodes[i]->d_type); - printf(", Type: %s%s%s", - S_ISREG(mode) ? "Regular file" : "",\ - S_ISDIR(mode) ? "Directory" : "",\ - (!S_ISREG(mode) && !S_ISDIR(mode)) ? "Unknown" : ""); - - #endif - printf("\n"); - } - // Ok so scandir is annoying and doesn't create a linked list, but rather an array of dirent objects. This means that if I want - // to iterate over the list myself and it'll be a pain in the ass to do so. That, or I can implement a linked list, which would - // also be a pain in the ass. I'm starting to remember why I finished vxgg the way I did return 0; } \ No newline at end of file diff --git a/src/shared.c b/src/shared.c index 5e58de0..0c7c8e2 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1,8 +1,10 @@ #include "shared.h" +#include #include #include #include +#include void* xcalloc(size_t nmemb, size_t size) { void *mem = calloc(nmemb, size); @@ -17,4 +19,44 @@ void* xcalloc(size_t nmemb, size_t size) { return mem; +} + +void* xreallocarray(void *ptr, size_t nmemb, size_t size) { + void *mem = reallocarray(ptr, nmemb, size); + if(mem == NULL) { + #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 + error(1, errno, " Could not allocate memory"); + + #endif + + abort(); + } + + return mem; +} + +int vsaprintf(char **str, const char *format, va_list ap) { + va_list ap2; + va_copy(ap2, ap); + + int length = vsnprintf(NULL, 0, format, ap2) + 1; // + 1 because sprintf does not count the null byte + char *temp = reallocarray(*str, length, sizeof(char)); + if(temp == NULL) + return -1; + + int ret = vsnprintf(temp, length, format, ap); + *str = temp; + + va_end(ap2); + return ret; +} + +int saprintf(char **str, const char *format, ...) { + va_list ap; + va_start(ap, format); + + int ret = vsaprintf(str, format, ap); + + va_end(ap); + return ret; } \ No newline at end of file diff --git a/src/shared.h b/src/shared.h index ac214f7..fef19ca 100644 --- a/src/shared.h +++ b/src/shared.h @@ -2,13 +2,23 @@ #define __VXGG_REWRITE___SHARED_H___3880294315821___ #include +#include -// Defines how `xcalloc()` should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions registered with -// `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` will ALWAYS 'abort', -// doing otherwise defeats the purpose of the function +// Defines how `x___alloc()` functions should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions +// registered with `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` +// will ALWAYS 'abort', doing otherwise defeats the purpose of the function #define ___VXGG___XCALLOC_EXIT_ON_ERROR___ 1 -// `calloc()` with error checking. Calls `error()` or `abort()` on error depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` +// `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` void* xcalloc(size_t nmemb, size_t size); +// `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` +void* xreallocarray(void *ptr, size_t nmemb, size_t size); + +// `vsprintf()`, but reallocates enough room for the resulting string before writing to `str` +int vsaprintf(char **str, const char *format, va_list ap); + +// `sprintf()`, but reallocates enough room for the resulting string before writing to `str` +int saprintf(char **str, const char *format, ...); + #endif \ No newline at end of file -- cgit v1.2.3