Fixed password not replacing properly, and memory bug with large files apparently

This commit is contained in:
Ignacio Rivero 2024-04-03 01:49:17 -03:00
parent a0c81e7064
commit 40f9b3dce8

43
salty.c
View File

@ -74,11 +74,11 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
unsigned char* old = input; unsigned char* old = input;
input = realloc(input, decLen); input = realloc(input, decLen);
if (input == NULL) { if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error.\n"); fprintf(stderr, ERR"Memory allocation error.\n");
fclose(outFile); fclose(outFile);
free(old); free(old);
return 1; return 1;
} }
} }
memcpy(input + decLen - readBytes,buffer,readBytes); memcpy(input + decLen - readBytes,buffer,readBytes);
if (readBytes == 0 && !ferror(stdin)) if (readBytes == 0 && !ferror(stdin))
@ -121,8 +121,8 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
} }
size_t encLen = decLen + crypto_secretbox_MACBYTES; size_t encLen = decLen + crypto_secretbox_MACBYTES;
fprintf(stderr,OK"File is %zu bytes.\n",decLen); fprintf(stderr,OK"Input file is %zu bytes.\n",decLen);
fprintf(stderr,OK"Encoding file to %zu bytes.\n", encLen); fprintf(stderr,OK"Encrypting file to %zu bytes.\n", encLen);
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
@ -130,7 +130,13 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
randombytes_buf(nonce,sizeof(nonce)); randombytes_buf(nonce,sizeof(nonce));
// Encrypt the data // 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); crypto_secretbox_easy(encrypted, decrypted, decLen, nonce, key);
// Patchy but correct-ish attempt to solve annoying stdout issue // Patchy but correct-ish attempt to solve annoying stdout issue
@ -152,7 +158,7 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
memcpy(outputBuffer + offset, nonce, sizeof(nonce)); memcpy(outputBuffer + offset, nonce, sizeof(nonce));
offset += sizeof(nonce); offset += sizeof(nonce);
memcpy(outputBuffer + offset, encrypted, sizeof(encrypted)); memcpy(outputBuffer + offset, encrypted, encLen);
fprintf(stderr,NOR"Writing encrypted data...\n"); fprintf(stderr,NOR"Writing encrypted data...\n");
@ -246,8 +252,8 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
return 1; return 1;
} }
fprintf(stderr,DBG"Size of encrypted file: %zu bytes.\n",inLen); fprintf(stderr,OK"Size of input file: %zu bytes.\n",inLen);
fprintf(stderr,DBG"This means the encrypted content is %zu bytes.\n",encLen); fprintf(stderr,OK"Encrypted content is %zu bytes.\n",encLen);
unsigned char encrypted[encLen]; unsigned char encrypted[encLen];
@ -318,11 +324,12 @@ bool isFile(const char* filename) {
bool getPassword(char *pw, int size) { bool getPassword(char *pw, int size) {
int i = 0; int i = 0;
char c; char c;
pw[0] = '\0';
// Hide input // Hide input
printf("\033[8m"); printf("\033[8m");
pw[0] = '\0';
// 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;
@ -335,15 +342,16 @@ bool getPassword(char *pw, int size) {
return false; return false;
i++; i++;
} }
// Position cursor and print asterisks
printf ("\033[0A"); printf ("\033[0A");
printf ("\033[32C"); printf ("\033[40C");
fflush(stdout);
while (i) { while (i) {
printf ( "*"); fprintf (stdout,"*");
i--; i--;
} }
fflush(stderr);
// Show input // Show input
printf ("\033[28m"); printf ("\033[28m\n");
return true; return true;
} }
@ -452,7 +460,8 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} else { } else {
password = malloc(129); password = malloc(129);
fprintf(stderr,ERR"Enter your encryption password: "); printf(NOR"Enter your encryption password: ");
fflush(stdout);
if (!getPassword(password,130)) { if (!getPassword(password,130)) {
fprintf(stderr,ERR"Password is too long."); fprintf(stderr,ERR"Password is too long.");
return 1; return 1;