Added stdin functionality to encryption only
This commit is contained in:
parent
e8acab9a17
commit
09eba06535
205
salty.c
205
salty.c
@ -10,35 +10,19 @@
|
|||||||
#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
|
||||||
|
#define BUF_SIZE 1024
|
||||||
|
|
||||||
|
bool stdinput = false;
|
||||||
|
|
||||||
int encryptMessage(const char* inputFile, const char* outputFile, const char* password) {
|
size_t fileSize(FILE *file) {
|
||||||
// Open files
|
fseek(file,0,SEEK_END);
|
||||||
FILE *inFile = fopen(inputFile,"rb");
|
size_t size = ftell(file);
|
||||||
FILE *outFile = fopen(outputFile,"wb");
|
fseek(file,0,SEEK_SET);
|
||||||
if (inFile == NULL || outFile == NULL) {
|
return size;
|
||||||
fprintf(stderr,ERR"Could not open files!\n");
|
}
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
// Variables
|
|
||||||
unsigned char salt[SALT_SIZE];
|
|
||||||
unsigned char nonce[crypto_secretbox_NONCEBYTES];
|
|
||||||
unsigned char key[KEY_SIZE];
|
|
||||||
|
|
||||||
// Read input file
|
bool keyGen(const unsigned char* salt, const char* password, unsigned char* key) {
|
||||||
|
|
||||||
fprintf(stderr,NOR"Reading input filesize...\n");
|
|
||||||
fseek(inFile,0,SEEK_END);
|
|
||||||
size_t inLen = ftell(inFile);
|
|
||||||
fseek(inFile,0,SEEK_SET);
|
|
||||||
fprintf(stderr,OK"File is %d bytes.\n",inLen);
|
|
||||||
unsigned char decrypted[inLen];
|
|
||||||
|
|
||||||
// Generate salt
|
|
||||||
randombytes_buf(salt, SALT_SIZE);
|
|
||||||
fprintf(stderr,NOR"Beginning key generation...\n");
|
fprintf(stderr,NOR"Beginning key generation...\n");
|
||||||
|
|
||||||
// Key generation from password
|
|
||||||
if (crypto_pwhash(
|
if (crypto_pwhash(
|
||||||
key,KEY_SIZE,
|
key,KEY_SIZE,
|
||||||
password,strlen(password),
|
password,strlen(password),
|
||||||
@ -46,67 +30,147 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
|||||||
crypto_pwhash_MEMLIMIT_INTERACTIVE,
|
crypto_pwhash_MEMLIMIT_INTERACTIVE,
|
||||||
crypto_pwhash_ALG_DEFAULT) != 0) {
|
crypto_pwhash_ALG_DEFAULT) != 0) {
|
||||||
fprintf(stderr,ERR"Out of memory! Key could not be generated.\n");
|
fprintf(stderr,ERR"Out of memory! Key could not be generated.\n");
|
||||||
fclose(inFile);
|
return false;
|
||||||
fclose(outFile);
|
}
|
||||||
|
fprintf(stderr,OK"Key generation successful!\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int encryptMessage(const char* inputFile, const char* outputFile, const char* password) {
|
||||||
|
|
||||||
|
// Generate salt
|
||||||
|
unsigned char salt[SALT_SIZE];
|
||||||
|
randombytes_buf(salt, SALT_SIZE);
|
||||||
|
|
||||||
|
// Key generation from password
|
||||||
|
unsigned char key[KEY_SIZE];
|
||||||
|
if (!keyGen(salt,password,key)) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr,OK"Key generation successful!\n");
|
|
||||||
fprintf(stderr,NOR"Reading file...\n");
|
fprintf(stderr,NOR"Reading file...\n");
|
||||||
|
|
||||||
// Read the input file
|
FILE *outFile = fopen(outputFile,"wb");
|
||||||
if(fread(decrypted,1,inLen,inFile) != inLen) {
|
if (outFile == NULL) {
|
||||||
fprintf(stderr,ERR"Error reading from file.\n");
|
fprintf(stderr,ERR"Could not open output file!\n");
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fprintf(stderr,OK"File read successfully!\n");
|
|
||||||
|
unsigned char* decrypted;
|
||||||
|
size_t decLen;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
decrypted = input;
|
||||||
|
decLen = inputSize;
|
||||||
|
} else {
|
||||||
|
// Open input file
|
||||||
|
FILE *inFile = fopen(inputFile,"rb");
|
||||||
|
if (inFile == NULL || outFile == NULL) {
|
||||||
|
fprintf(stderr,ERR"Could not open input file!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read input file
|
||||||
|
fprintf(stderr,NOR"Reading input filesize...\n");
|
||||||
|
decLen = fileSize(inFile);
|
||||||
|
fprintf(stderr,OK"File is %d bytes.\n",decLen);
|
||||||
|
decrypted = malloc(decLen);
|
||||||
|
|
||||||
|
if (fread(decrypted,1,decLen,inFile) != decLen) {
|
||||||
|
fprintf(stderr,ERR"Error reading from file.\n");
|
||||||
|
fclose(inFile);
|
||||||
|
fclose(outFile);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fclose(inFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr,OK"Input read successfully!\n");
|
||||||
fprintf(stderr,NOR"Beginning encryption...\n");
|
fprintf(stderr,NOR"Beginning encryption...\n");
|
||||||
|
|
||||||
// Generate a nonce, write headers and encrypted message
|
// Generate a nonce, write headers and encrypted message
|
||||||
|
unsigned char nonce[crypto_secretbox_NONCEBYTES];
|
||||||
randombytes_buf(nonce,sizeof(nonce));
|
randombytes_buf(nonce,sizeof(nonce));
|
||||||
|
|
||||||
if (fwrite("gneurshk",1,strlen("gneurshk"),outFile) != strlen("gneurshk")) {
|
// Encrypt the data
|
||||||
fprintf(stderr,ERR"Error writing header to file.\n");
|
unsigned char encrypted[decLen + crypto_secretbox_MACBYTES];
|
||||||
fclose(inFile);
|
crypto_secretbox_easy(encrypted, decrypted, decLen, nonce, key);
|
||||||
fclose(outFile);
|
|
||||||
|
// Patchy but correct-ish attempt to solve annoying stdout issue
|
||||||
|
size_t totalSize = strlen("gneurshk") + sizeof(salt) + sizeof(nonce) + sizeof(encrypted);
|
||||||
|
size_t offset = 0;
|
||||||
|
unsigned char* outputBuffer = malloc(totalSize);
|
||||||
|
if (outputBuffer == NULL) {
|
||||||
|
fprintf(stderr,ERR"Memory allocation error.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
memcpy(outputBuffer + offset, "gneurshk", strlen("gneurshk"));
|
||||||
|
offset += strlen("gneurshk");
|
||||||
|
|
||||||
if (fwrite(salt,1,sizeof(salt),outFile) != sizeof(salt)) {
|
memcpy(outputBuffer + offset, salt, sizeof(salt));
|
||||||
fprintf(stderr,ERR"Error writing salt to file.\n");
|
offset += sizeof(salt);
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fwrite(nonce,1,sizeof(nonce),outFile) != sizeof(nonce)) {
|
memcpy(outputBuffer + offset, nonce, sizeof(nonce));
|
||||||
fprintf(stderr,ERR"Error writing nonce to file.\n");
|
offset += sizeof(nonce);
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
memcpy(outputBuffer + offset, encrypted, sizeof(encrypted));
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stderr,OK"Headers written!\n");
|
|
||||||
fprintf(stderr,NOR"Writing encrypted data...\n");
|
fprintf(stderr,NOR"Writing encrypted data...\n");
|
||||||
|
|
||||||
unsigned char encrypted[inLen + crypto_secretbox_MACBYTES];
|
if(fwrite(outputBuffer, 1, totalSize, outFile) != totalSize) {
|
||||||
|
|
||||||
crypto_secretbox_easy(encrypted, decrypted, inLen, nonce, key);
|
|
||||||
|
|
||||||
if(fwrite(encrypted,1,sizeof(encrypted),outFile) != sizeof(encrypted)) {
|
|
||||||
fprintf(stderr,ERR"Error writing encrypted data to file.\n");
|
fprintf(stderr,ERR"Error writing encrypted data to file.\n");
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
fclose(outFile);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fclose(inFile);
|
|
||||||
fclose(outFile);
|
fclose(outFile);
|
||||||
|
free(decrypted);
|
||||||
|
free(outputBuffer);
|
||||||
|
|
||||||
fprintf(stderr,"\n"OK"File encrypted!\n");
|
fprintf(stderr,"\n"OK"File encrypted!\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -126,13 +190,10 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
|||||||
unsigned char key[KEY_SIZE];
|
unsigned char key[KEY_SIZE];
|
||||||
|
|
||||||
// Get the size of the file and assume the length of the content
|
// Get the size of the file and assume the length of the content
|
||||||
fseek(inFile,0,SEEK_END);
|
size_t inLen = fileSize(inFile);
|
||||||
size_t fileLen = ftell(inFile);
|
size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8;
|
||||||
fseek(inFile,0,SEEK_SET);
|
|
||||||
|
|
||||||
size_t encLen = fileLen - 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",fileLen);
|
|
||||||
fprintf(stderr,DBG"This means the encrypted content is %d bytes.\n",encLen);
|
fprintf(stderr,DBG"This means the encrypted content is %d bytes.\n",encLen);
|
||||||
|
|
||||||
unsigned char encrypted[encLen];
|
unsigned char encrypted[encLen];
|
||||||
@ -217,7 +278,6 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
|
|||||||
fprintf(stderr,"\n"OK"File decrypted!\n");
|
fprintf(stderr,"\n"OK"File decrypted!\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isFile(const char* filename) {
|
bool isFile(const char* filename) {
|
||||||
@ -245,6 +305,9 @@ int main(int argc, char *argv[]) {
|
|||||||
if (i+1 >= argc) {
|
if (i+1 >= argc) {
|
||||||
fprintf(stderr,ERR"Argument -in requires a file.\n");
|
fprintf(stderr,ERR"Argument -in requires a file.\n");
|
||||||
exit = 1;
|
exit = 1;
|
||||||
|
} else if (strcmp(argv[i+1],"-") == 0) {
|
||||||
|
in = -2;
|
||||||
|
i++;
|
||||||
} else if (!isFile(argv[i+1])) {
|
} else if (!isFile(argv[i+1])) {
|
||||||
fprintf(stderr,ERR"File not found error: %s\n",argv[i+1]);
|
fprintf(stderr,ERR"File not found error: %s\n",argv[i+1]);
|
||||||
exit = 1;
|
exit = 1;
|
||||||
@ -300,8 +363,11 @@ int main(int argc, char *argv[]) {
|
|||||||
char psw[129];
|
char psw[129];
|
||||||
|
|
||||||
if (in == -1) {
|
if (in == -1) {
|
||||||
fprintf(stderr,ERR"No input file specified.\n");
|
fprintf(stderr,NOR"No input file, reading from stdin.\n");
|
||||||
return 1;
|
stdinput = true;
|
||||||
|
} else if (in == -2) {
|
||||||
|
fprintf(stderr,NOR"Reading from stdin.\n");
|
||||||
|
stdinput = true;
|
||||||
} else {
|
} else {
|
||||||
snprintf(input,sizeof(input),argv[in]);
|
snprintf(input,sizeof(input),argv[in]);
|
||||||
}
|
}
|
||||||
@ -332,5 +398,4 @@ int main(int argc, char *argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
return encryptMessage(input,output,password);
|
return encryptMessage(input,output,password);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user