Skip to content

Commit

Permalink
fixing bugs from gcry_free
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGouchet committed Mar 4, 2016
1 parent 723ac2a commit efed90e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ int AES::aes_cbc_256_encrypt(const char *plaintextPath, const char *cipherPath,
FILE *ciphertextFile = NULL;

int keylen = gcry_cipher_get_algo_keylen(GCRY_CIPHER_AES256);
int blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES256);
int blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES);

unsigned char *fileContent = NULL;
unsigned char *ciphertext = NULL;
Expand Down
40 changes: 26 additions & 14 deletions src/cipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,14 @@ void Cipher::computeAES(){

int keylen = comboSize->currentText().toLocal8Bit().toInt()/8;
int pass_len = leKey->text().length();

Util print;
// Here check whether radioIv is checked and if not randomly generate IV and store in the file
unsigned char *iv = NULL;
int ivSize = 16;
int blklen = gcry_cipher_get_algo_blklen(GCRY_CIPHER_AES);
if(!radioIv->isChecked())
{
iv = (unsigned char *) gcry_malloc(ivSize*sizeof(unsigned char));
gcry_randomize(iv, ivSize, GCRY_STRONG_RANDOM);
Util print;
print.printBuff(iv, ivSize);
iv = (unsigned char *) gcry_calloc(blklen, sizeof(unsigned char));
gcry_randomize(iv, blklen, GCRY_STRONG_RANDOM);
}
else
iv = (unsigned char *) leIv->text().toLocal8Bit().constData();
Expand All @@ -421,11 +419,12 @@ void Cipher::computeAES(){

Util print;
printf("iv: ");
print.printBuff(iv, ivSize);
print.printBuff(iv, blklen);
printf("%u\n", iv);

int hash_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256);

unsigned char *pass = (unsigned char *) gcry_malloc_secure(sizeof(unsigned char)*hash_len);
unsigned char *pass = (unsigned char *) gcry_calloc(sizeof(unsigned char), hash_len);
strcpy( (char *) pass, leKey->text().toLocal8Bit().constData());

fprintf(stdout, "pass: ");
Expand All @@ -444,8 +443,13 @@ void Cipher::computeAES(){

gcry_md_write(hd, pass, pass_len);

if(pass)
gcry_free(pass);

pass = gcry_md_read(hd, GCRY_MD_SHA256);

print.printBuff(pass, 32);

printf("hash_len: %d\n", hash_len);

fprintf(stdout, "Digest of key: ");
Expand Down Expand Up @@ -530,13 +534,21 @@ void Cipher::computeAES(){

if(hd)
gcry_md_close(hd);

memset(pass, 0, hash_len);
memset(iv, 0, blklen);

print.printBuff(pass, hash_len);

printf("%u\n", iv);

print.printBuff(iv, blklen);

if(iv)
gcry_free(iv);

print.printBuff(iv, blklen);
return ;
/* if(hd)
gcry_md_close(hd);
if(pass)
gcry_free(pass);*/
/*if(iv)
free(iv);*/
}

void Cipher::computeDES()
Expand Down
10 changes: 5 additions & 5 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ MainWindow::MainWindow() : QWidget()
this->fail();
}

if(err = gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0))
if(err = gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P, 0))
{
fprintf(stderr, "Initialization failed: %s/%s\n",
gcry_strsource (err),
Expand Down Expand Up @@ -188,25 +188,25 @@ MainWindow::MainWindow() : QWidget()

this->setLayout(glMain);

if(err = gcry_control(GCRYCTL_TERM_SECMEM))
/*if(err = gcry_control(GCRYCTL_TERM_SECMEM))
{
fprintf(stderr, "Initialization failed: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
this->fail();
}
}*/
}

void MainWindow::terminate()
{
gcry_error_t err = 0;
if(err = gcry_control(GCRYCTL_TERM_SECMEM))
/*if(err = gcry_control(GCRYCTL_TERM_SECMEM))
{
fprintf(stderr, "Initialization failed: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
this->fail();
}
}*/

close();
}
Expand Down
Binary file modified src/src
Binary file not shown.

0 comments on commit efed90e

Please sign in to comment.