Skip to content

Commit

Permalink
moved decrypt_aes_gcm() into MyAES.cpp, it is now the only file which…
Browse files Browse the repository at this point in the history
… includes mbedtls
  • Loading branch information
andreas-mausch committed Mar 27, 2018
1 parent 302ce61 commit a4f2e52
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
20 changes: 20 additions & 0 deletions source/Libraries/AES/MyAES.cpp
@@ -1,4 +1,5 @@
#include "mbedtls/aes.h"
#include "mbedtls/gcm.h"
#include "../../Exceptions/Exception.h"

int aesBlocksize = 16;
Expand Down Expand Up @@ -27,3 +28,22 @@ void decrypt_aes_cbc_256(const unsigned char *input, unsigned char *output, int
}
mbedtls_aes_free(&context);
}

void decrypt_aes_gcm(const unsigned char *input, unsigned char *output, int length, const unsigned char *key, unsigned char *initVector)
{
mbedtls_gcm_context context;

mbedtls_gcm_init(&context);
if (mbedtls_gcm_setkey(&context, MBEDTLS_CIPHER_ID_AES, key, 256) != 0) {
throw Exception("Could not decrypt (mbedtls_gcm_setkey)");
}

if (mbedtls_gcm_starts(&context, MBEDTLS_GCM_DECRYPT, initVector, 16, NULL, 0) != 0) {
throw Exception("Could not decrypt (mbedtls_gcm_starts)");
}
if (mbedtls_gcm_update(&context, length, input, output) != 0) {
throw Exception("Could not decrypt (mbedtls_gcm_update)");
}
mbedtls_gcm_finish(&context, NULL, 0);
mbedtls_gcm_free(&context);
}
1 change: 1 addition & 0 deletions source/Libraries/AES/MyAES.h
Expand Up @@ -2,5 +2,6 @@

void decrypt_aes_cbc_192(const unsigned char *input, unsigned char *output, int length, const unsigned char *key, unsigned char *initVector);
void decrypt_aes_cbc_256(const unsigned char *input, unsigned char *output, int length, const unsigned char *key, unsigned char *initVector);
void decrypt_aes_gcm(const unsigned char *input, unsigned char *output, int length, const unsigned char *key, unsigned char *initVector);

extern int aesBlocksize;
23 changes: 1 addition & 22 deletions source/WhatsApp/Crypt12.cpp
Expand Up @@ -4,7 +4,6 @@

#include "../Exceptions/Exception.h"
#include "../Libraries/AES/MyAES.h"
#include "../Libraries/AES/mbedtls/gcm.h"
#include "../Libraries/Zip/zlib.h"
#include "Crypt5.h"
#include "Crypt7.h"
Expand All @@ -13,7 +12,6 @@
const int chunk = 16384;

void decryptWhatsappDatabase12(const std::string &filename, const std::string &filenameDecrypted, unsigned char *key);
void decryptAesGcm(const unsigned char *crypted, unsigned char *uncrypted, unsigned char *key, unsigned char *initVector, int size);

void decryptWhatsappDatabase12(const std::string &filename, const std::string &filenameDecrypted, const std::string &keyFilename)
{
Expand All @@ -36,7 +34,7 @@ void decryptWhatsappDatabase12(const std::string &filename, const std::string &f
memcpy(initVector, &fileBytes[51], 16);
unsigned char *decryptedDatabaseBytes = new unsigned char[databaseSize];

decryptAesGcm(cryptedDatabaseBytes, decryptedDatabaseBytes, key, initVector, databaseSize);
decrypt_aes_gcm(cryptedDatabaseBytes, decryptedDatabaseBytes, databaseSize, key, initVector);

std::vector<unsigned char> uncompressed;
uncompressGzipBuffer(decryptedDatabaseBytes, databaseSize, uncompressed);
Expand All @@ -48,22 +46,3 @@ void decryptWhatsappDatabase12(const std::string &filename, const std::string &f
delete[] cryptedDatabaseBytes;
delete[] decryptedDatabaseBytes;
}

void decryptAesGcm(const unsigned char *crypted, unsigned char *uncrypted, unsigned char *key, unsigned char *initVector, int size)
{
mbedtls_gcm_context context;

mbedtls_gcm_init(&context);
if (mbedtls_gcm_setkey(&context, MBEDTLS_CIPHER_ID_AES, key, 256) != 0) {
throw Exception("Could not decrypt (mbedtls_gcm_setkey)");
}

if (mbedtls_gcm_starts(&context, MBEDTLS_GCM_DECRYPT, initVector, 16, NULL, 0) != 0) {
throw Exception("Could not decrypt (mbedtls_gcm_starts)");
}
if (mbedtls_gcm_update(&context, size, crypted, uncrypted) != 0) {
throw Exception("Could not decrypt (mbedtls_gcm_update)");
}
mbedtls_gcm_finish(&context, NULL, 0);
mbedtls_gcm_free(&context);
}

0 comments on commit a4f2e52

Please sign in to comment.