Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. OpenSSL Decryption on Another QThread
Forum Updated to NodeBB v4.3 + New Features

OpenSSL Decryption on Another QThread

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 198 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adam Crowe
    wrote on 23 Jun 2020, 21:49 last edited by
    #1

    Hello,

    I've got an application that loads the main window and proceeds to decrypt a whole bunch of AES encrypted files and then displays them in a list. I'm using OpenSSL to decrypt these files and it's all working very nicely.

    Because of the amount of files, in order to not choke the main program, I want to decrypt these files "in the background" on another thread.

    Following the Qt QThread Controller/Worker model I have implemented other similar mechanisms for other tests in my app, so I was hoping that moving the decryption code to another thread would be trivial but unfortunately that is not the case.

    The decryption thread processes about 30 files and then crashes the program. The debugger doesn't point to any code but rather shows assembly code as it can't find any symbols for whatever it is that crashed. I have narrowed it down to the decryption code as bypassing it makes the thread happy and working.

    The question I suppose is - is it ok to use OpenSSL on another thread? Are there special practices for this? I saw OpenSSL itself is thread safe if you use its locking functions but I would have assumed that giving the threading control to QThread takes care of that?

    Does anyone have any experience with this?

    Thank you!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 24 Jun 2020, 04:17 last edited by
      #2

      @Adam-Crowe said in OpenSSL Decryption on Another QThread:

      The question I suppose is - is it ok to use OpenSSL on another thread?

      Yes

      Are there special practices for this?

      Simply follow the Qt examples, I would use QtConcurrent for this.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • A Offline
        A Offline
        Adam Crowe
        wrote on 24 Jun 2020, 21:47 last edited by
        #3

        Thank you very much for the suggestion Christian!

        I've been experimenting with QtConcurrent but the result was the same. If I change the decryption code to using the Qt-AES library, everything works perfectly - just extremely slowly as it's a far less efficient algorithm it seems.

        I've gone back to the controller/worker architecture but instead tried to figure out what I'm doing wrong with the OpenSSL code. I believe I wasn't allocating the memory correctly for the plain text buffer. If anyone's interested this works for me:

        QString MyClass::decypt(const QByteArray &byteArray, const QByteArray &key, const QByteArray &iv)
        {
            EVP_CIPHER_CTX *ctx;
        
            /* Create and initialise the context */
            if (!(ctx = EVP_CIPHER_CTX_new()))
            {
                ERR_print_errors_fp(stderr);
            }
        
            const unsigned char *cipherText = (const unsigned char *) byteArray.data();
            int cipherTextLength = byteArray.size();
            int plainTextBufferSize = cipherTextLength + EVP_CIPHER_block_size(EVP_aes_128_cbc());
            int plainTextLength;
            QByteArray plainText;
        
            plainText.resize(plainTextBufferSize);
        
            /* Initialise the decryption operation */
            if (EVP_DecryptInit(ctx, EVP_aes_128_cbc(), (const unsigned char *) key.data(), (const unsigned char *) iv.data()) != 1)
            {
                ERR_print_errors_fp(stderr);
            }
        
            /* Obtain the plaintext output */
            if (EVP_DecryptUpdate(ctx, (unsigned char *) plainText.data(), &plainTextLength, cipherText, cipherTextLength) != 1)
            {
                ERR_print_errors_fp(stderr);
            }
        
            int finalLen = 0;
        
            /* Finalise the decryption */
            if (EVP_DecryptFinal(ctx, (unsigned char *) plainText.data() + plainTextLength, &finalLen) != 1)
            {
                ERR_print_errors_fp(stderr);
            }
        
            plainTextLength += finalLen;
        
            plainText.resize(plainTextLength);
        
            /* Clean up */
            EVP_CIPHER_CTX_cleanup(ctx);
            EVP_CIPHER_CTX_free(ctx);
        
            return QString(plainText);
        }
        
        1 Reply Last reply
        1

        1/3

        23 Jun 2020, 21:49

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved