Added stdin functionality to decryption.
This commit is contained in:
parent
09eba06535
commit
c0e5bc4a5b
138
salty.c
138
salty.c
@ -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) {
|
int decryptMessage(const char* inputFile, const char* outputFile, const char* password) {
|
||||||
// Open files
|
// Open output file
|
||||||
FILE *inFile = fopen(inputFile,"rb");
|
|
||||||
FILE *outFile = fopen(outputFile,"wb");
|
FILE *outFile = fopen(outputFile,"wb");
|
||||||
if (inFile == NULL || outFile == NULL) {
|
if (outFile == NULL) {
|
||||||
fprintf(stderr,ERR"Could not open files.\n");
|
fprintf(stderr,ERR"Could not open output file!\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
unsigned char salt[SALT_SIZE];
|
unsigned char salt[SALT_SIZE];
|
||||||
unsigned char nonce[crypto_secretbox_NONCEBYTES];
|
unsigned char nonce[crypto_secretbox_NONCEBYTES];
|
||||||
unsigned char key[KEY_SIZE];
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
// Get the size of the file and assume the length of the content
|
|
||||||
size_t inLen = fileSize(inFile);
|
|
||||||
size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8;
|
size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8;
|
||||||
|
|
||||||
fprintf(stderr,DBG"Size of encrypted file: %d bytes.\n",inLen);
|
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];
|
unsigned char encrypted[encLen];
|
||||||
|
|
||||||
// Verify file
|
// Verify file
|
||||||
unsigned char header[8];
|
size_t offset = 0;
|
||||||
if (fread(header,1,sizeof(header),inFile) != sizeof(header)) {
|
if (memcmp(fullInput,"gneurshk",strlen("gneurshk")) != 0) {
|
||||||
fprintf(stderr,ERR"Error reading header from file.\n");
|
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (memcmp(header,"gneurshk",sizeof(header)) != 0) {
|
|
||||||
fprintf(stderr,ERR"Invalid header.\n");
|
fprintf(stderr,ERR"Invalid header.\n");
|
||||||
fprintf(stderr,ERR"Make sure the input file was signed by this program!\n");
|
fprintf(stderr,ERR"Make sure the input file was signed by this program!\n");
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
fclose(outFile);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fprintf(stderr,OK"Valid header! Reading the rest...\n");
|
fprintf(stderr,OK"Valid header! Reading the rest...\n");
|
||||||
|
offset += strlen("gneurshk");
|
||||||
|
|
||||||
// Read salt and nonce
|
// Read salt and nonce
|
||||||
if(fread(salt,1,sizeof(salt),inFile) != sizeof(salt)) {
|
memcpy(salt, fullInput + offset, sizeof(salt));
|
||||||
fprintf(stderr,ERR"Error reading salt from file.\n");
|
offset += sizeof(salt);
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
fprintf(stderr,OK"Salt retrieved.\n");
|
|
||||||
|
|
||||||
if(fread(nonce,1,sizeof(nonce),inFile) != sizeof(nonce)) {
|
memcpy(nonce, fullInput + offset, sizeof(nonce));
|
||||||
fprintf(stderr,ERR"Error reading nonce from file.\n");
|
offset += sizeof(nonce);
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
memcpy(encrypted, fullInput + offset, sizeof(encrypted));
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
fprintf(stderr,OK"Nonce retrieved.\n");
|
|
||||||
|
|
||||||
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"Data retrieved.\n");
|
||||||
fprintf(stderr,OK"Deriving key from password...\n");
|
|
||||||
|
|
||||||
// Key derivation
|
// Key
|
||||||
if (crypto_pwhash(
|
unsigned char key[KEY_SIZE];
|
||||||
key,KEY_SIZE,
|
|
||||||
password,strlen(password),
|
if (!keyGen(salt,password,key)) {
|
||||||
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");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fprintf(stderr,OK"Key derivation successful!\n");
|
|
||||||
fprintf(stderr,OK"Proceeding to decrypt file...\n");
|
fprintf(stderr,OK"Proceeding to decrypt file...\n");
|
||||||
|
|
||||||
size_t decLen = encLen - crypto_secretbox_MACBYTES;
|
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) {
|
if (crypto_secretbox_open_easy(decrypted,encrypted,encLen,nonce,key) < 0) {
|
||||||
fprintf(stderr,ERR"Error decrypting file.\n");
|
fprintf(stderr,ERR"Error decrypting file.\n");
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
fclose(outFile);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fwrite(decrypted,1,decLen,outFile) != decLen) {
|
if (fwrite(decrypted,1,decLen,outFile) != decLen) {
|
||||||
fprintf(stderr,ERR"Error writing data to file.\n");
|
fprintf(stderr,ERR"Error writing data to file.\n");
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
fclose(outFile);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
fclose(outFile);
|
||||||
|
|
||||||
fprintf(stderr,"\n"OK"File decrypted!\n");
|
fprintf(stderr,"\n"OK"File decrypted!\n");
|
||||||
@ -387,10 +423,8 @@ int main(int argc, char *argv[]) {
|
|||||||
// Set password and run!
|
// Set password and run!
|
||||||
if (pass == -1) {
|
if (pass == -1) {
|
||||||
password = getpass("Enter your encryption password: ");
|
password = getpass("Enter your encryption password: ");
|
||||||
printf(DBG"Password : %s\n",password);
|
|
||||||
} else {
|
} else {
|
||||||
password = argv[pass];
|
password = argv[pass];
|
||||||
printf(DBG"Password : %s\n",password);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (decrypt) {
|
if (decrypt) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user