whitespaces are now part of the password

NULL character can also be part of the password. There was a failing
test that was checking exactly that (now all the tests pass).
This commit is contained in:
ATuinDev 2018-02-03 19:16:20 +01:00
parent bce58cae3e
commit 257e5ee99a
No known key found for this signature in database
GPG Key ID: 30B13E03CCAB58B7

View File

@ -43,6 +43,9 @@
#include <gcrypt.h> #include <gcrypt.h>
/* Max password size */
#define BUFFER_SIZE 1024
/* TODO: move print_hex and hex_to_binary to utils.h, with separate compiling */ /* TODO: move print_hex and hex_to_binary to utils.h, with separate compiling */
void print_hex(unsigned char *buf, int len) void print_hex(unsigned char *buf, int len)
{ {
@ -75,7 +78,7 @@ int hex_to_binary(unsigned char *buf, char *hex)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *pass = NULL; char pass[BUFFER_SIZE];
unsigned char *salt; unsigned char *salt;
int salt_len; // salt length in bytes int salt_len; // salt length in bytes
int ic=0; // iterative count int ic=0; // iterative count
@ -105,9 +108,18 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
fscanf(stdin, "%ms", &pass); int j = 0;
if ( pass[strlen(pass)-1] == '\n' ) while (j < (BUFFER_SIZE + 1)) {
pass[strlen(pass)-1] = '\0'; char c = getchar();
if (c == EOF) break;
pass[j] = c;
j++;
}
if (j == BUFFER_SIZE + 1) {
fprintf(stderr, "Error: password is too long\n");
exit(1);
}
pass[j-1] = '\0';
// PBKDF 2 // PBKDF 2
result = calloc(result_len, sizeof(unsigned char*)); result = calloc(result_len, sizeof(unsigned char*));
@ -124,7 +136,7 @@ int main(int argc, char *argv[])
/* Tell Libgcrypt that initialization has completed. */ /* Tell Libgcrypt that initialization has completed. */
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
gcry_kdf_derive( pass, strlen(pass), GCRY_KDF_PBKDF2, GCRY_MD_SHA1, salt, salt_len, ic, result_len, result); gcry_kdf_derive(pass, j-1, GCRY_KDF_PBKDF2, GCRY_MD_SHA1, salt, salt_len, ic, result_len, result);
print_hex(result, result_len); // Key + IV (as hex string) print_hex(result, result_len); // Key + IV (as hex string)
//clear and free everything //clear and free everything
@ -133,7 +145,6 @@ int main(int argc, char *argv[])
free(result); free(result);
for(i=0; i<strlen(pass); i++) //blank for(i=0; i<strlen(pass); i++) //blank
pass[i]=0; pass[i]=0;
free(pass);
for(i=0; i<strlen(argv[1])/2+3; i++) //blank for(i=0; i<strlen(argv[1])/2+3; i++) //blank
salt[i]=0; salt[i]=0;
free(salt); free(salt);