diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 15 | ||||
| -rw-r--r-- | src/encryption.c | 73 | ||||
| -rw-r--r-- | src/tests.c | 84 |
3 files changed, 95 insertions, 77 deletions
diff --git a/src/Makefile b/src/Makefile index e8c04f9..de0c9f6 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -16,7 +16,7 @@ SOURCES := $(wildcard *.c) | |||
| 16 | TIMESTAMP_DIR := .timestamps | 16 | TIMESTAMP_DIR := .timestamps |
| 17 | TIMESTAMPS := $(patsubst %.c,$(TIMESTAMP_DIR)/%.t,$(SOURCES)) | 17 | TIMESTAMPS := $(patsubst %.c,$(TIMESTAMP_DIR)/%.t,$(SOURCES)) |
| 18 | 18 | ||
| 19 | .PHONY: all c clean val | 19 | .PHONY: all c clean v val t test |
| 20 | .DELETE_ON_ERROR: | 20 | .DELETE_ON_ERROR: |
| 21 | .ONESHELL: | 21 | .ONESHELL: |
| 22 | 22 | ||
| @@ -28,12 +28,19 @@ $(TIMESTAMP_DIR): | |||
| 28 | $(TIMESTAMPS): $(TIMESTAMP_DIR)/%.t: %.c | $(TIMESTAMP_DIR) | 28 | $(TIMESTAMPS): $(TIMESTAMP_DIR)/%.t: %.c | $(TIMESTAMP_DIR) |
| 29 | touch $@ | 29 | touch $@ |
| 30 | 30 | ||
| 31 | main: main.c $(TIMESTAMPS) | 31 | main tests: %: %.c $(TIMESTAMPS) |
| 32 | $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@ | 32 | $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@ |
| 33 | 33 | ||
| 34 | |||
| 35 | # Phony rules | ||
| 36 | |||
| 34 | c clean: | 37 | c clean: |
| 35 | -rm -rvf main $(TIMESTAMP_DIR) $(wildcard *.test*) $(wildcard *.enc) | 38 | -rm -rvf main $(TIMESTAMP_DIR) $(wildcard *.test*) $(wildcard *.enc) |
| 36 | 39 | ||
| 37 | val: | 40 | v val: |
| 38 | $(MAKE) all | 41 | $(MAKE) all |
| 39 | valgrind --leak-check=yes ./main \ No newline at end of file | 42 | valgrind --leak-check=yes ./main |
| 43 | |||
| 44 | t test: | ||
| 45 | $(MAKE) tests | ||
| 46 | valgrind --leak-check=yes ./tests \ No newline at end of file | ||
diff --git a/src/encryption.c b/src/encryption.c index 382f6d5..aafb972 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -634,77 +634,4 @@ ctqueue * cryptscan() { | |||
| 634 | return res; | 634 | return res; |
| 635 | } | 635 | } |
| 636 | 636 | ||
| 637 | /* | ||
| 638 | int main(void) { | ||
| 639 | // Example code for creating a temp file, writing to it, then linking it back into the fs | ||
| 640 | const char *dir = ".", *testmsg = "we do a little testing\n"; | ||
| 641 | char *path = NULL; | ||
| 642 | |||
| 643 | int fd = maketmp(dir); | ||
| 644 | if(fd < 0) | ||
| 645 | error(1, errno, "Couldn't make temp file at %s", dir); | ||
| 646 | |||
| 647 | if(write(fd, testmsg, strlen(testmsg)) < 0) | ||
| 648 | error(1, errno, "write broke"); | ||
| 649 | |||
| 650 | asprintf(&path, "/proc/self/fd/%d", fd); | ||
| 651 | linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); | ||
| 652 | free(path); | ||
| 653 | |||
| 654 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages | ||
| 655 | |||
| 656 | if(close(fd) < 0) | ||
| 657 | error(1, errno, "close broke"); | ||
| 658 | //*/// | ||
| 659 | |||
| 660 | /*// Example code for getting a password using genpassword | ||
| 661 | checksodium(); | ||
| 662 | |||
| 663 | char *password = NULL; | ||
| 664 | genpassword(&password, 20); | ||
| 665 | printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); | ||
| 666 | free(password); | ||
| 667 | //*/// | ||
| 668 | |||
| 669 | /*// Example code for generating a password, derriving a secret key from it, and storing things properly | ||
| 670 | |||
| 671 | // Initialization | ||
| 672 | checksodium(); | ||
| 673 | char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; | ||
| 674 | |||
| 675 | if(genpassword(&pass, 20) < 0) { | ||
| 676 | error(1, 0, "Could not generate password, quitting..."); | ||
| 677 | abort(); // Makes gcc happy. Not sure why gcc randomly decides that error() isn't a proper exit, but hey whatever | ||
| 678 | } | ||
| 679 | sodium_mlock(pass, strlen(pass) + 1); | ||
| 680 | printf("Password:%s\n", pass); | ||
| 681 | |||
| 682 | // Store the password | ||
| 683 | if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) | ||
| 684 | error(1, errno, "Couldn't generate password, quitting..."); | ||
| 685 | // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly | ||
| 686 | // shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is | ||
| 687 | // barely noticable. I might do MODERATE for testing and SENSITIVE for release | ||
| 688 | |||
| 689 | sodium_munlock(pass, strlen(pass) + 1); | ||
| 690 | free(pass); | ||
| 691 | |||
| 692 | printf("Hashed password: %s\n", hpass); | ||
| 693 | |||
| 694 | // Check if the password from the user is correct | ||
| 695 | char *uin = NULL; int size = -1; | ||
| 696 | if((size = readwholebuffer(&uin, 1, STDIN_FILENO)) < 0) | ||
| 697 | error(1, errno, "Could not read from stdin"); | ||
| 698 | sodium_mlock(uin, size); | ||
| 699 | |||
| 700 | printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); | ||
| 701 | |||
| 702 | |||
| 703 | sodium_munlock(uin, strlen(uin) + 1); | ||
| 704 | free(uin); | ||
| 705 | |||
| 706 | return 0; | ||
| 707 | } | ||
| 708 | */ | ||
| 709 | |||
| 710 | #endif \ No newline at end of file | 637 | #endif \ No newline at end of file |
diff --git a/src/tests.c b/src/tests.c new file mode 100644 index 0000000..882fdc2 --- /dev/null +++ b/src/tests.c | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | |||
| 3 | #include "shared.c" | ||
| 4 | #include "encryption.c" | ||
| 5 | #include "threadpool.c" | ||
| 6 | |||
| 7 | void test_encryption(void) { | ||
| 8 | // TODO: Figure out if I care about this test existing or not. Currently, this has just been | ||
| 9 | // copied from ecryption.c & slapped | ||
| 10 | |||
| 11 | // Example code for creating a temp file, writing to it, then linking it back into the fs | ||
| 12 | const char *dir = ".", *testmsg = "we do a little testing\n"; | ||
| 13 | char *path = NULL; | ||
| 14 | |||
| 15 | int fd = maketmp(dir); | ||
| 16 | if(fd < 0) | ||
| 17 | error(1, errno, "Couldn't make temp file at %s", dir); | ||
| 18 | |||
| 19 | if(write(fd, testmsg, strlen(testmsg)) < 0) | ||
| 20 | error(1, errno, "write broke"); | ||
| 21 | |||
| 22 | asprintf(&path, "/proc/self/fd/%d", fd); | ||
| 23 | linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); | ||
| 24 | free(path); | ||
| 25 | |||
| 26 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages | ||
| 27 | |||
| 28 | if(close(fd) < 0) | ||
| 29 | error(1, errno, "close broke"); | ||
| 30 | //*/// | ||
| 31 | |||
| 32 | //*// Example code for getting a password using genpassword | ||
| 33 | checksodium(); | ||
| 34 | |||
| 35 | char *password = NULL; | ||
| 36 | genpassword(&password, 20); | ||
| 37 | printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); | ||
| 38 | free(password); | ||
| 39 | /*/// | ||
| 40 | |||
| 41 | //*/// Example code for generating a password, derriving a secret key from it, and storing things properly | ||
| 42 | |||
| 43 | // Initialization | ||
| 44 | checksodium(); | ||
| 45 | char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; | ||
| 46 | |||
| 47 | if(genpassword(&pass, 20) < 0) { | ||
| 48 | error(1, 0, "Could not generate password, quitting..."); | ||
| 49 | abort(); // Makes gcc happy. Not sure why gcc randomly decides that error() isn't a proper exit, but hey whatever | ||
| 50 | } | ||
| 51 | sodium_mlock(pass, strlen(pass) + 1); | ||
| 52 | printf("Password:%s\n", pass); | ||
| 53 | |||
| 54 | // Store the password | ||
| 55 | if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) | ||
| 56 | error(1, errno, "Couldn't generate password, quitting..."); | ||
| 57 | // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly | ||
| 58 | // shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is | ||
| 59 | // barely noticable. I might do MODERATE for testing and SENSITIVE for release | ||
| 60 | |||
| 61 | sodium_munlock(pass, strlen(pass) + 1); | ||
| 62 | free(pass); | ||
| 63 | |||
| 64 | printf("Hashed password: %s\n", hpass); | ||
| 65 | |||
| 66 | // Check if the password from the user is correct | ||
| 67 | char *uin = NULL; int size = -1; | ||
| 68 | if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) | ||
| 69 | error(1, errno, "Could not read from stdin"); | ||
| 70 | sodium_mlock(uin, size); | ||
| 71 | |||
| 72 | printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); | ||
| 73 | |||
| 74 | |||
| 75 | sodium_munlock(uin, strlen(uin) + 1); | ||
| 76 | free(uin); | ||
| 77 | |||
| 78 | return; | ||
| 79 | } | ||
| 80 | |||
| 81 | int main(void) { | ||
| 82 | test_encryption(); | ||
| 83 | return 0; | ||
| 84 | } \ No newline at end of file | ||
