Compare commits
10 Commits
d3e3123373
...
a590d1842d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a590d1842d | ||
|
|
9f6d1ecdeb | ||
|
|
ced58f8a85 | ||
|
|
bb437ea4c3 | ||
|
|
e05392d44f | ||
|
|
40f9b3dce8 | ||
|
|
a0c81e7064 | ||
|
|
072db09093 | ||
|
|
84d4192cf2 | ||
|
|
29925395b3 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/build/
|
||||
26
Makefile
26
Makefile
@ -1,12 +1,22 @@
|
||||
salty: salty.o
|
||||
cc ./build/salty.o -o ./build/salty -lsodium -I.
|
||||
rm ./build/salty.o
|
||||
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
|
||||
|
||||
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
|
||||
mkdir -p $(BUILD_DIR)
|
||||
$(CC) $(CFLAGS) -o $@ salty.c $(INCLUDES) $(LIBS) $(LDFLAGS)
|
||||
|
||||
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
|
||||
1
pawstd.h
1
pawstd.h
@ -84,3 +84,4 @@
|
||||
#define NOR "[ --- ] "
|
||||
#define OK "[ "GRN"OK!"reset" ] "
|
||||
#define DBG "[ " YEL "DBG" reset " ] "
|
||||
#define WRN "[ " YEL "WRN" reset " ] "
|
||||
|
||||
252
salty.c
252
salty.c
@ -1,16 +1,16 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sodium.h>
|
||||
#include <pawstd.h>
|
||||
#include <termios.h>
|
||||
#include "pawstd.h"
|
||||
#define KEY_SIZE crypto_secretbox_KEYBYTES
|
||||
#define SALT_SIZE crypto_pwhash_SALTBYTES
|
||||
#define BUF_SIZE 1024
|
||||
#define VER "1.00"
|
||||
|
||||
bool stdinput = false;
|
||||
|
||||
@ -36,7 +36,7 @@ bool keyGen(const unsigned char* salt, const char* password, unsigned char* key)
|
||||
return true;
|
||||
}
|
||||
|
||||
int encryptMessage(const char* inputFile, const char* outputFile, const char* password) {
|
||||
int encryptFile(const char* inputFile, const char* outputFile, const char* password) {
|
||||
|
||||
// Generate salt
|
||||
unsigned char salt[SALT_SIZE];
|
||||
@ -57,58 +57,47 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
}
|
||||
|
||||
unsigned char* decrypted;
|
||||
size_t decLen;
|
||||
size_t decLen = 0;
|
||||
|
||||
// Check if input is stdin, and read the input
|
||||
if (stdinput) {
|
||||
// Begin reading into buffer
|
||||
unsigned char buffer[BUF_SIZE];
|
||||
size_t inputSize = 0;
|
||||
unsigned char c;
|
||||
unsigned char* input = malloc(BUF_SIZE);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.");
|
||||
fprintf(stderr,ERR"Memory allocation error.\n");
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
int i = 0;
|
||||
c = fgetc(stdin);
|
||||
while (feof(stdin) == 0) {
|
||||
inputSize++;
|
||||
if (i < BUF_SIZE) {
|
||||
buffer[i] = c;
|
||||
i++;
|
||||
} else {
|
||||
char *old = input;
|
||||
input = realloc(input, inputSize);
|
||||
while(true) {
|
||||
size_t readBytes = fread(buffer,1,BUF_SIZE,stdin);
|
||||
decLen += readBytes;
|
||||
// Prevents reallocating to zero bytes.
|
||||
if (decLen > 0) {
|
||||
unsigned char* old = input;
|
||||
input = realloc(input, decLen);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.");
|
||||
fprintf(stderr, ERR"Memory allocation error.\n");
|
||||
fclose(outFile);
|
||||
free(old);
|
||||
return 1;
|
||||
}
|
||||
memcpy(input + inputSize - BUF_SIZE,buffer,BUF_SIZE);
|
||||
i = 0;
|
||||
}
|
||||
c = fgetc(stdin);
|
||||
}
|
||||
if (i > 0) {
|
||||
char *old = input;
|
||||
input = realloc(input, inputSize);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.");
|
||||
memcpy(input + decLen - readBytes,buffer,readBytes);
|
||||
if (readBytes == 0 && !ferror(stdin))
|
||||
break;
|
||||
else if (readBytes == 0 && ferror(stdin)) {
|
||||
fprintf(stderr,ERR"Error reading from stdin.\n");
|
||||
fclose(outFile);
|
||||
free(old);
|
||||
free(input);
|
||||
return 1;
|
||||
}
|
||||
memcpy(input + inputSize - i,buffer,i);
|
||||
}
|
||||
decrypted = input;
|
||||
decLen = inputSize;
|
||||
} 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;
|
||||
}
|
||||
@ -128,9 +117,15 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
fclose(inFile);
|
||||
}
|
||||
|
||||
if (decLen == 0) {
|
||||
fprintf(stderr,ERR"No bytes read from file.\n");
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
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"Input file is %zu bytes.\n",decLen);
|
||||
fprintf(stderr,OK"Encrypting file to %zu bytes.\n", encLen);
|
||||
fprintf(stderr,NOR"Beginning encryption...\n");
|
||||
|
||||
// Generate a nonce, write headers and encrypted message
|
||||
@ -138,7 +133,13 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
randombytes_buf(nonce,sizeof(nonce));
|
||||
|
||||
// Encrypt the data
|
||||
unsigned char encrypted[encLen];
|
||||
unsigned char* encrypted = malloc(encLen);
|
||||
if (encrypted == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error: File too large.\n");
|
||||
free(decrypted);
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
crypto_secretbox_easy(encrypted, decrypted, decLen, nonce, key);
|
||||
|
||||
// Patchy but correct-ish attempt to solve annoying stdout issue
|
||||
@ -160,7 +161,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
memcpy(outputBuffer + offset, nonce, sizeof(nonce));
|
||||
offset += sizeof(nonce);
|
||||
|
||||
memcpy(outputBuffer + offset, encrypted, sizeof(encrypted));
|
||||
memcpy(outputBuffer + offset, encrypted, encLen);
|
||||
|
||||
fprintf(stderr,NOR"Writing encrypted data...\n");
|
||||
|
||||
@ -179,7 +180,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
return 0;
|
||||
}
|
||||
|
||||
int decryptMessage(const char* inputFile, const char* outputFile, const char* password) {
|
||||
int decryptFile(const char* inputFile, const char* outputFile, const char* password) {
|
||||
// Open output file
|
||||
FILE *outFile = fopen(outputFile,"wb");
|
||||
if (outFile == NULL) {
|
||||
@ -190,65 +191,58 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
// Variables
|
||||
unsigned char salt[SALT_SIZE];
|
||||
unsigned char nonce[crypto_secretbox_NONCEBYTES];
|
||||
|
||||
|
||||
unsigned char* fullInput;
|
||||
size_t inLen;
|
||||
size_t inLen = 0;
|
||||
|
||||
// Check if input is stdin, and read the input
|
||||
if (stdinput) {
|
||||
// Begin reading into buffer
|
||||
unsigned char buffer[BUF_SIZE];
|
||||
size_t inputSize = 0;
|
||||
unsigned char c;
|
||||
unsigned char* input = malloc(BUF_SIZE);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.");
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
int i = 0;
|
||||
c = fgetc(stdin);
|
||||
while (feof(stdin) == 0) {
|
||||
inputSize++;
|
||||
if (i < BUF_SIZE) {
|
||||
buffer[i] = c;
|
||||
i++;
|
||||
} else {
|
||||
char *old = input;
|
||||
input = realloc(input, inputSize);
|
||||
while(true) {
|
||||
size_t readBytes = fread(buffer,1,BUF_SIZE,stdin);
|
||||
inLen += readBytes;
|
||||
// Prevents reallocating to zero bytes.
|
||||
if (inLen > 0) {
|
||||
unsigned char* old = input;
|
||||
input = realloc(input, inLen);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.");
|
||||
fprintf(stderr,ERR"Memory allocation error.\n");
|
||||
fclose(outFile);
|
||||
free(old);
|
||||
return 1;
|
||||
}
|
||||
memcpy(input + inputSize - BUF_SIZE,buffer,BUF_SIZE);
|
||||
i = 0;
|
||||
}
|
||||
c = fgetc(stdin);
|
||||
}
|
||||
if (i > 0) {
|
||||
char *old = input;
|
||||
input = realloc(input, inputSize);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.");
|
||||
memcpy(input + inLen - readBytes,buffer,readBytes);
|
||||
if (readBytes == 0 && !ferror(stdin))
|
||||
break;
|
||||
else if (readBytes == 0 && ferror(stdin)) {
|
||||
fprintf(stderr,ERR"Error reading from stdin.\n");
|
||||
fclose(outFile);
|
||||
free(old);
|
||||
free(input);
|
||||
return 1;
|
||||
}
|
||||
memcpy(input + inputSize - i,buffer,i);
|
||||
}
|
||||
fullInput = input;
|
||||
inLen = inputSize;
|
||||
} else {
|
||||
FILE *inFile = fopen(inputFile,"rb");
|
||||
|
||||
// Get the size of the file and read it
|
||||
size_t inputSize = fileSize(inFile);
|
||||
unsigned char* input = malloc(inputSize);
|
||||
inLen = fileSize(inFile);
|
||||
unsigned char* input = malloc(inLen);
|
||||
if (input == NULL) {
|
||||
fprintf(stderr, ERR"Memory allocation error: File too large.\n");
|
||||
fclose(inFile);
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fread(input,1,inputSize,inFile) != inputSize) {
|
||||
if (fread(input,1,inLen,inFile) != inLen) {
|
||||
fprintf(stderr,ERR"Error reading from input file.\n");
|
||||
fclose(inFile);
|
||||
fclose(outFile);
|
||||
@ -257,21 +251,26 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
|
||||
fclose(inFile);
|
||||
fullInput = input;
|
||||
inLen = inputSize;
|
||||
}
|
||||
|
||||
size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8;
|
||||
|
||||
if (inLen == 0) {
|
||||
fprintf(stderr,ERR"File size is 0. No data.");
|
||||
fprintf(stderr,ERR"No bytes read from file.\n");
|
||||
fclose(outFile);
|
||||
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,OK"Size of input file: %zu bytes.\n",inLen);
|
||||
fprintf(stderr,OK"Encrypted content is %zu bytes.\n",encLen);
|
||||
|
||||
unsigned char encrypted[encLen];
|
||||
unsigned char* encrypted = malloc(encLen);
|
||||
if (encrypted == NULL) {
|
||||
fprintf(stderr,ERR"Memory allocation error.");
|
||||
free(fullInput);
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Verify file
|
||||
size_t offset = 0;
|
||||
@ -292,15 +291,17 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
memcpy(nonce, fullInput + offset, sizeof(nonce));
|
||||
offset += sizeof(nonce);
|
||||
|
||||
memcpy(encrypted, fullInput + offset, sizeof(encrypted));
|
||||
memcpy(encrypted, fullInput + offset, encLen);
|
||||
|
||||
fprintf(stderr,OK"Data retrieved.\n");
|
||||
|
||||
free(fullInput);
|
||||
|
||||
// Key
|
||||
unsigned char key[KEY_SIZE];
|
||||
|
||||
if (!keyGen(salt,password,key)) {
|
||||
free(fullInput);
|
||||
free(encrypted);
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
@ -308,24 +309,25 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
||||
fprintf(stderr,OK"Proceeding to decrypt file...\n");
|
||||
|
||||
size_t decLen = encLen - crypto_secretbox_MACBYTES;
|
||||
unsigned char decrypted[decLen];
|
||||
unsigned char* decrypted = malloc(decLen);
|
||||
|
||||
if (crypto_secretbox_open_easy(decrypted,encrypted,encLen,nonce,key) < 0) {
|
||||
fprintf(stderr,ERR"Error decrypting file.\n");
|
||||
free(fullInput);
|
||||
free(encrypted);
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fwrite(decrypted,1,decLen,outFile) != decLen) {
|
||||
fprintf(stderr,ERR"Error writing data to file.\n");
|
||||
free(fullInput);
|
||||
free(encrypted);
|
||||
fclose(outFile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
fclose(outFile);
|
||||
free(fullInput);
|
||||
free(encrypted);
|
||||
free(decrypted);
|
||||
|
||||
fprintf(stderr,"\n"OK"File decrypted!\n");
|
||||
|
||||
@ -337,22 +339,65 @@ bool isFile(const char* filename) {
|
||||
return (stat(filename, &buffer) == 0);
|
||||
}
|
||||
|
||||
bool getPassword(char *pw, int size) {
|
||||
struct termios term;
|
||||
tcgetattr(fileno(stdin), &term);
|
||||
int i = 0;
|
||||
char c;
|
||||
pw[0] = '\0';
|
||||
// Hide input
|
||||
term.c_lflag &= ~ECHO;
|
||||
tcsetattr(fileno(stdin), 0, &term);
|
||||
// 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++;
|
||||
}
|
||||
// Show input
|
||||
term.c_lflag |= ECHO;
|
||||
tcsetattr(fileno(stdin), 0, &term);
|
||||
return true;
|
||||
}
|
||||
|
||||
void printUsage() {
|
||||
fprintf(stderr,"\nUsage: salty [-d] -in [INPUT] -out [OUTPUT] [OPTIONS]\n");
|
||||
fprintf(stderr,"Encrypts or decrypts a file with a password. When INPUT or OUTPUT are not set,\n");
|
||||
fprintf(stderr,"or set to -, standard input or output is used respectively.\n\n");
|
||||
fprintf(stderr,"Options:\n");
|
||||
fprintf(stderr," -in\t\tTakes an input file\n");
|
||||
fprintf(stderr," -out\tTakes an output file\n");
|
||||
fprintf(stderr," -d\t\tEnable decryption mode\n");
|
||||
fprintf(stderr," -key\tTakes a password, needed if reading from stdin\n");
|
||||
fprintf(stderr," \t\tOtherwise this password is read from stdin itself\n");
|
||||
fprintf(stderr," -v\t\tDisplays version information and exits\n");
|
||||
fprintf(stderr," -h\t\tDisplays this help and exits\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
fprintf(stderr,"\n- NetPaws Salty - File Encryption Program\n");
|
||||
fprintf(stderr,"- 2024 Ignacio Rivero\n");
|
||||
fprintf(stderr,"- FOR INTERNAL USE ONLY\n\n");
|
||||
setvbuf(stdout, NULL, _IONBF, 0);
|
||||
fprintf(stderr,NOR"NetPaws Salty - File Encryption Program\n");
|
||||
fprintf(stderr,NOR"2024 Ignacio Rivero\n");
|
||||
// Initialize libsodium
|
||||
if (sodium_init() < 0) {
|
||||
fprintf(stderr,ERR"libsodium initialization failed! It is not safe to proceed.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Handle input, output and password arguments
|
||||
// Handle input, output, password, and other arguments
|
||||
int in = -1, out = -1, pass = -1;
|
||||
bool decrypt = false;
|
||||
|
||||
int i = 1, exit = 0;
|
||||
while (i < argc && exit == 0) {
|
||||
int i = 1, exit = -1;
|
||||
while (i < argc && exit == -1) {
|
||||
if (strcmp (argv[i],"-in") == 0) {
|
||||
if (i+1 >= argc) {
|
||||
fprintf(stderr,ERR"Argument -in requires a file.\n");
|
||||
@ -388,7 +433,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;
|
||||
@ -399,6 +444,13 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
} else if (strcmp (argv[i],"-d") == 0) {
|
||||
decrypt = true;
|
||||
} else if (strcmp (argv[i],"-v") == 0) {
|
||||
fprintf(stderr,NOR"Version %s\n",VER);
|
||||
exit = 0;
|
||||
} else if (strcmp (argv[i],"-h") == 0) {
|
||||
fprintf(stderr,NOR"Version %s\n",VER);
|
||||
printUsage();
|
||||
exit = 0;
|
||||
} else {
|
||||
fprintf(stderr,ERR"Invalid argument: %s\n",argv[i]);
|
||||
exit = 1;
|
||||
@ -406,13 +458,17 @@ int main(int argc, char *argv[]) {
|
||||
i++;
|
||||
}
|
||||
|
||||
if (exit != 0)
|
||||
if (exit == 1) {
|
||||
printUsage();
|
||||
return exit;
|
||||
} else if (exit != -1)
|
||||
return exit;
|
||||
|
||||
fprintf(stderr,"\n");
|
||||
|
||||
// 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");
|
||||
@ -421,7 +477,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) {
|
||||
@ -431,7 +487,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;
|
||||
@ -440,15 +496,21 @@ 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");
|
||||
fprintf(stderr, ERR"Cannot read from stdin without a password.\n");
|
||||
return 1;
|
||||
} else {
|
||||
password = getpass("Enter your encryption password: ");
|
||||
password = malloc(129);
|
||||
printf(NOR"Enter your encryption password: ");
|
||||
if (!getPassword(password,130)) {
|
||||
fprintf(stderr,"\n"ERR"Password is too long.");
|
||||
return 1;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (decrypt) {
|
||||
return decryptMessage(input,output,password);
|
||||
return decryptFile(input, output, password);
|
||||
} else {
|
||||
return encryptMessage(input,output,password);
|
||||
return encryptFile(input, output, password);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user