Added stdin functionality to decryption.

This commit is contained in:
Ignacio 2024-04-01 12:34:29 -03:00
parent 09eba06535
commit c0e5bc4a5b

138
salty.c
View File

@ -176,21 +176,86 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
}
int decryptMessage(const char* inputFile, const char* outputFile, const char* password) {
// Open files
FILE *inFile = fopen(inputFile,"rb");
// Open output file
FILE *outFile = fopen(outputFile,"wb");
if (inFile == NULL || outFile == NULL) {
fprintf(stderr,ERR"Could not open files.\n");
if (outFile == NULL) {
fprintf(stderr,ERR"Could not open output file!\n");
return 1;
}
// Variables
unsigned char salt[SALT_SIZE];
unsigned char nonce[crypto_secretbox_NONCEBYTES];
unsigned char key[KEY_SIZE];
// Get the size of the file and assume the length of the content
size_t inLen = fileSize(inFile);
unsigned char* fullInput;
size_t inLen;
// 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);
if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error.");
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.");
fclose(outFile);
free(old);
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);
if (fread(input,1,inputSize,inFile) != inputSize) {
fprintf(stderr,ERR"Error reading from input file.\n");
fclose(inFile);
fclose(outFile);
return 1;
}
fclose(inFile);
fullInput = input;
inLen = inputSize;
}
size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8;
fprintf(stderr,DBG"Size of encrypted file: %d bytes.\n",inLen);
@ -199,60 +264,34 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
unsigned char encrypted[encLen];
// Verify file
unsigned char header[8];
if (fread(header,1,sizeof(header),inFile) != sizeof(header)) {
fprintf(stderr,ERR"Error reading header from file.\n");
fclose(inFile);
fclose(outFile);
return 1;
}
if (memcmp(header,"gneurshk",sizeof(header)) != 0) {
size_t offset = 0;
if (memcmp(fullInput,"gneurshk",strlen("gneurshk")) != 0) {
fprintf(stderr,ERR"Invalid header.\n");
fprintf(stderr,ERR"Make sure the input file was signed by this program!\n");
fclose(inFile);
fclose(outFile);
return 1;
}
fprintf(stderr,OK"Valid header! Reading the rest...\n");
offset += strlen("gneurshk");
// Read salt and nonce
if(fread(salt,1,sizeof(salt),inFile) != sizeof(salt)) {
fprintf(stderr,ERR"Error reading salt from file.\n");
fclose(inFile);
fclose(outFile);
return 1;
}
fprintf(stderr,OK"Salt retrieved.\n");
memcpy(salt, fullInput + offset, sizeof(salt));
offset += sizeof(salt);
if(fread(nonce,1,sizeof(nonce),inFile) != sizeof(nonce)) {
fprintf(stderr,ERR"Error reading nonce from file.\n");
fclose(inFile);
fclose(outFile);
return 1;
}
fprintf(stderr,OK"Nonce retrieved.\n");
memcpy(nonce, fullInput + offset, sizeof(nonce));
offset += sizeof(nonce);
memcpy(encrypted, fullInput + offset, sizeof(encrypted));
if(fread(encrypted,1,encLen,inFile) != encLen) {
fprintf(stderr,ERR"Error reading data from file.\n");
fclose(inFile);
fclose(outFile);
return 1;
}
fprintf(stderr,OK"Data retrieved.\n");
fprintf(stderr,OK"Deriving key from password...\n");
// Key derivation
if (crypto_pwhash(
key,KEY_SIZE,
password,strlen(password),
salt,crypto_pwhash_OPSLIMIT_INTERACTIVE,
crypto_pwhash_MEMLIMIT_INTERACTIVE,
crypto_pwhash_ALG_DEFAULT) != 0) {
fprintf(stderr,ERR"Out of memory! Key could not be generated.\n");
// Key
unsigned char key[KEY_SIZE];
if (!keyGen(salt,password,key)) {
return 1;
}
fprintf(stderr,OK"Key derivation successful!\n");
fprintf(stderr,OK"Proceeding to decrypt file...\n");
size_t decLen = encLen - crypto_secretbox_MACBYTES;
@ -260,19 +299,16 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
if (crypto_secretbox_open_easy(decrypted,encrypted,encLen,nonce,key) < 0) {
fprintf(stderr,ERR"Error decrypting file.\n");
fclose(inFile);
fclose(outFile);
return 1;
}
if (fwrite(decrypted,1,decLen,outFile) != decLen) {
fprintf(stderr,ERR"Error writing data to file.\n");
fclose(inFile);
fclose(outFile);
return 1;
}
fclose(inFile);
fclose(outFile);
fprintf(stderr,"\n"OK"File decrypted!\n");
@ -387,10 +423,8 @@ int main(int argc, char *argv[]) {
// Set password and run!
if (pass == -1) {
password = getpass("Enter your encryption password: ");
printf(DBG"Password : %s\n",password);
} else {
password = argv[pass];
printf(DBG"Password : %s\n",password);
}
if (decrypt) {