diff --git a/Makefile b/Makefile index fe8d211..f153a5d 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,22 @@ -salty: salty.o - cc ./build/salty.o -o ./build/salty -lsodium -I. - rm ./build/salty.o +CC = gcc +CFLAGS = -Wall -Wextra -std=c99 -O2 +LDFLAGS = -lsodium +BUILD_DIR = build +PREFIX = /usr/local -salty.o: build - cc -c salty.c -o ./build/salty.o -lsodium -I. +all: $(BUILD_DIR)/salty -build: - mkdir -p ./build +$(BUILD_DIR)/salty: salty.c pawstd.h | $(BUILD_DIR) + $(CC) $(CFLAGS) -o $@ salty.c $(LDFLAGS) + +$(BUILD_DIR): + mkdir -p $(BUILD_DIR) + +clean: + rm -rf $(BUILD_DIR) install: - install -m 755 ./build/salty /usr/local/bin/salty + install -m 755 $(BUILD_DIR)/salty /usr/local/bin/salty + +uninstall: + rm $(PREFIX)/bin/salty \ No newline at end of file diff --git a/salty.c b/salty.c index 11dfdb4..df417ce 100644 --- a/salty.c +++ b/salty.c @@ -2,12 +2,9 @@ #include #include #include -#include -#include -#include #include #include -#include +#include "pawstd.h" #define KEY_SIZE crypto_secretbox_KEYBYTES #define SALT_SIZE crypto_pwhash_SALTBYTES #define BUF_SIZE 1024 @@ -69,9 +66,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa fclose(outFile); return 1; } - int i = 0; - - while(true) { + while(true) { size_t readBytes = fread(buffer,1,BUF_SIZE,stdin); decLen += readBytes; // Prevents reallocating to zero bytes. @@ -99,7 +94,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa } else { // Open input file FILE *inFile = fopen(inputFile,"rb"); - if (inFile == NULL || outFile == NULL) { + if (inFile == NULL) { fprintf(stderr,ERR"Could not open input file!\n"); return 1; } @@ -126,8 +121,8 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa } size_t encLen = decLen + crypto_secretbox_MACBYTES; - fprintf(stderr,OK"File is %d bytes.\n",decLen); - fprintf(stderr,OK"Encoding file to %d bytes.\n", encLen); + fprintf(stderr,OK"File is %zu bytes.\n",decLen); + fprintf(stderr,OK"Encoding file to %zu bytes.\n", encLen); fprintf(stderr,NOR"Beginning encryption...\n"); // Generate a nonce, write headers and encrypted message @@ -251,8 +246,8 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa return 1; } - fprintf(stderr,DBG"Size of encrypted file: %d bytes.\n",inLen); - fprintf(stderr,DBG"This means the encrypted content is %d bytes.\n",encLen); + fprintf(stderr,DBG"Size of encrypted file: %zu bytes.\n",inLen); + fprintf(stderr,DBG"This means the encrypted content is %zu bytes.\n",encLen); unsigned char encrypted[encLen]; @@ -320,6 +315,38 @@ bool isFile(const char* filename) { return (stat(filename, &buffer) == 0); } +bool getPassword(char *pw, int size) { + int i = 0; + char c; + // Hide input + printf("\033[8m"); + pw[0] = '\0'; + // Get password from stdin + while (true) { + c = fgetc(stdin); + if (c == '\r' || c == '\n' || feof(stdin)) { + break; + } + // Return false on excess characters + if (i < size - 1) { + pw[i] = c; + pw[i + 1] = '\0'; + } else + return false; + i++; + } + // Position cursor and print asterisks + printf ("\033[0A"); + printf ("\033[32C"); + while (i) { + printf ( "*"); + i--; + } + // Show input + printf ("\033[28m"); + return true; +} + int main(int argc, char *argv[]) { fprintf(stderr,"- NetPaws Salty - File Encryption Program\n"); fprintf(stderr,"- 2024 Ignacio Rivero\n\n"); @@ -370,7 +397,7 @@ int main(int argc, char *argv[]) { exit = 1; } else if (pass == -1) { if (strlen(argv[i+1]) > 128) { - fprintf(stderr,ERR"Password too long."); + fprintf(stderr,ERR"Password is too long."); exit = 1; } else { pass = ++i; @@ -394,7 +421,6 @@ int main(int argc, char *argv[]) { // Set input and output filenames, or go to standard I/O if none char input[FILENAME_MAX]; char output[FILENAME_MAX]; - char psw[129]; if (in == -1) { fprintf(stderr,NOR"No input file, reading from stdin.\n"); @@ -403,7 +429,7 @@ int main(int argc, char *argv[]) { fprintf(stderr,NOR"Reading from stdin.\n"); stdinput = true; } else { - snprintf(input,sizeof(input),argv[in]); + snprintf(input,sizeof(input),"%s", argv[in]); } if (out == -1) { @@ -413,7 +439,7 @@ int main(int argc, char *argv[]) { fprintf(stderr,NOR"Writing to stdout.\n"); snprintf(output,sizeof(output),"/dev/stdout"); } else { - snprintf(output,sizeof(output),argv[out]); + snprintf(output,sizeof(output),"%s", argv[out]); } char* password; @@ -422,13 +448,18 @@ int main(int argc, char *argv[]) { if (pass > 0) { password = argv[pass]; } else if (in < 0) { - fprintf(stderr,ERR"Cannot read from stdin without a password.\n"); - return 1; - } else { - password = getpass("Enter your encryption password: "); - } + fprintf(stderr, ERR"Cannot read from stdin without a password.\n"); + return 1; + } else { + password = malloc(129); + fprintf(stderr,ERR"Enter your encryption password: "); + if (!getPassword(password,130)) { + fprintf(stderr,ERR"Password is too long."); + return 1; + } + } - if (decrypt) { + if (decrypt) { return decryptMessage(input,output,password); } else { return encryptMessage(input,output,password);