From 9f6d1ecdebd237a2c2c4f33a333705b9273e4827 Mon Sep 17 00:00:00 2001 From: Ignacio Rivero Date: Wed, 3 Apr 2024 19:27:00 -0300 Subject: [PATCH] Better password hiding, POSIX macro, updated header and BSD compatibility --- Makefile | 10 +++++----- pawstd.h | 1 + salty.c | 21 ++++++++++----------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index f153a5d..9da555f 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ -CC = gcc +CC = cc CFLAGS = -Wall -Wextra -std=c99 -O2 LDFLAGS = -lsodium +INCLUDES = -I/usr/include -I/usr/local/include -I/usr/pkg/include +LIBS = -L/usr/lib -L/usr/local/lib -L/usr/pkg/lib BUILD_DIR = build PREFIX = /usr/local all: $(BUILD_DIR)/salty -$(BUILD_DIR)/salty: salty.c pawstd.h | $(BUILD_DIR) - $(CC) $(CFLAGS) -o $@ salty.c $(LDFLAGS) - -$(BUILD_DIR): +$(BUILD_DIR)/salty: salty.c pawstd.h mkdir -p $(BUILD_DIR) + $(CC) $(CFLAGS) -o $@ salty.c $(INCLUDES) $(LIBS) $(LDFLAGS) clean: rm -rf $(BUILD_DIR) diff --git a/pawstd.h b/pawstd.h index 3810971..0ed31eb 100644 --- a/pawstd.h +++ b/pawstd.h @@ -84,3 +84,4 @@ #define NOR "[ --- ] " #define OK "[ "GRN"OK!"reset" ] " #define DBG "[ " YEL "DBG" reset " ] " +#define WRN "[ " YEL "WRN" reset " ] " diff --git a/salty.c b/salty.c index 0fb8315..ccbaf4b 100644 --- a/salty.c +++ b/salty.c @@ -1,9 +1,11 @@ +#define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include +#include #include "pawstd.h" #define KEY_SIZE crypto_secretbox_KEYBYTES #define SALT_SIZE crypto_pwhash_SALTBYTES @@ -337,14 +339,16 @@ bool isFile(const char* filename) { } bool getPassword(char *pw, int size) { + struct termios term; + tcgetattr(fileno(stdin), &term); int i = 0; char c; pw[0] = '\0'; // Hide input - printf("\033[8m"); + term.c_lflag &= ~ECHO; + tcsetattr(fileno(stdin), 0, &term); // Get password from stdin while (true) { - // Overwrite password in console c = fgetc(stdin); if (c == '\r' || c == '\n' || feof(stdin)) { break; @@ -357,15 +361,9 @@ bool getPassword(char *pw, int size) { return false; i++; } - printf ("\033[0A"); - printf ("\033[40C"); - fflush(stdout); - while (i) { - fprintf (stdout,"*"); - i--; - } // Show input - printf ("\033[28m\n"); + term.c_lflag |= ECHO; + tcsetattr(fileno(stdin), 0, &term); return true; } @@ -477,9 +475,10 @@ int main(int argc, char *argv[]) { password = malloc(129); printf(NOR"Enter your encryption password: "); if (!getPassword(password,130)) { - fprintf(stderr,ERR"Password is too long."); + fprintf(stderr,"\n"ERR"Password is too long."); return 1; } + printf("\n"); } if (decrypt) {