Better password hiding, POSIX macro, updated header and BSD compatibility

This commit is contained in:
Ignacio Rivero 2024-04-03 19:27:00 -03:00
parent ced58f8a85
commit 9f6d1ecdeb
3 changed files with 16 additions and 16 deletions

View File

@ -1,16 +1,16 @@
CC = gcc CC = cc
CFLAGS = -Wall -Wextra -std=c99 -O2 CFLAGS = -Wall -Wextra -std=c99 -O2
LDFLAGS = -lsodium 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 BUILD_DIR = build
PREFIX = /usr/local PREFIX = /usr/local
all: $(BUILD_DIR)/salty all: $(BUILD_DIR)/salty
$(BUILD_DIR)/salty: salty.c pawstd.h | $(BUILD_DIR) $(BUILD_DIR)/salty: salty.c pawstd.h
$(CC) $(CFLAGS) -o $@ salty.c $(LDFLAGS)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR) mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) -o $@ salty.c $(INCLUDES) $(LIBS) $(LDFLAGS)
clean: clean:
rm -rf $(BUILD_DIR) rm -rf $(BUILD_DIR)

View File

@ -84,3 +84,4 @@
#define NOR "[ --- ] " #define NOR "[ --- ] "
#define OK "[ "GRN"OK!"reset" ] " #define OK "[ "GRN"OK!"reset" ] "
#define DBG "[ " YEL "DBG" reset " ] " #define DBG "[ " YEL "DBG" reset " ] "
#define WRN "[ " YEL "WRN" reset " ] "

21
salty.c
View File

@ -1,9 +1,11 @@
#define _POSIX_C_SOURCE 200809L
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sodium.h> #include <sodium.h>
#include <termios.h>
#include "pawstd.h" #include "pawstd.h"
#define KEY_SIZE crypto_secretbox_KEYBYTES #define KEY_SIZE crypto_secretbox_KEYBYTES
#define SALT_SIZE crypto_pwhash_SALTBYTES #define SALT_SIZE crypto_pwhash_SALTBYTES
@ -337,14 +339,16 @@ bool isFile(const char* filename) {
} }
bool getPassword(char *pw, int size) { bool getPassword(char *pw, int size) {
struct termios term;
tcgetattr(fileno(stdin), &term);
int i = 0; int i = 0;
char c; char c;
pw[0] = '\0'; pw[0] = '\0';
// Hide input // Hide input
printf("\033[8m"); term.c_lflag &= ~ECHO;
tcsetattr(fileno(stdin), 0, &term);
// Get password from stdin // Get password from stdin
while (true) { while (true) {
// Overwrite password in console
c = fgetc(stdin); c = fgetc(stdin);
if (c == '\r' || c == '\n' || feof(stdin)) { if (c == '\r' || c == '\n' || feof(stdin)) {
break; break;
@ -357,15 +361,9 @@ bool getPassword(char *pw, int size) {
return false; return false;
i++; i++;
} }
printf ("\033[0A");
printf ("\033[40C");
fflush(stdout);
while (i) {
fprintf (stdout,"*");
i--;
}
// Show input // Show input
printf ("\033[28m\n"); term.c_lflag |= ECHO;
tcsetattr(fileno(stdin), 0, &term);
return true; return true;
} }
@ -477,9 +475,10 @@ int main(int argc, char *argv[]) {
password = malloc(129); password = malloc(129);
printf(NOR"Enter your encryption password: "); printf(NOR"Enter your encryption password: ");
if (!getPassword(password,130)) { if (!getPassword(password,130)) {
fprintf(stderr,ERR"Password is too long."); fprintf(stderr,"\n"ERR"Password is too long.");
return 1; return 1;
} }
printf("\n");
} }
if (decrypt) { if (decrypt) {