Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

Weird Openmp bug

Cheng_C_
Beginner
596 Views

Dear all,

I want to combine OpenMP and RSA_public_encrypt and RSA_private_decrypt routines. However, I was confused by a weird bug for a few days. 
 
In the attached program, if I generated 2 threads for parallel encryption and decryption, everything works well. If I generated 3 or more threads, the RSA_public_encrypt routine works fine. All strings are successfully encrypted (encrypt_len=256). However, the RSA_private_decrypt routine went wrong, that is, only one thread works properly, all the other threads failed to decrypt some of the strings (decrypt_len=-1, rsa_eay_private_decrypt padding check failed). If there are 1000 strings and 4 threads, the total number of string failed to decrypt went around 710 (some times as low as around 200). So as expected, if I use 4 threads for parallel RSA_public_encrypt and one thread for RSA_private_decrypt, nothing went wrong.
 
It would be great if you could give some ideas. Thanks very much. 
 

#include <openssl/rsa.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <string.h>
#include <omp.h>

#define KEY_LENGTH   2048
#define EN_SIZE      200  //number of chars in a string
#define STR_NUM      1000 //number of strings to be encrypted
#define PUB_EXP      3

int main() {

    int    i, j, k;
    int    encrypt_len;
    int    decrypt_len;

    char *en_m[STR_NUM]; //array of pointers for the input strings
    char *tm_m[STR_NUM]; //array of pointers for the encrypted strings
    char *rm_m[STR_NUM]; //array of pointers for the encrypted strings
    // rm_m read back tm_m through a .txt file
    char *de_m[STR_NUM]; //array of pointers for the decrypted strings

    // Generate key pair
    RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);

    FILE *f;
    f = fopen("message.txt", "r");
    if(f==NULL){
      perror ("Error in locating the file to be encrypted");
      return(-1);
    }

    for (k=0; k<STR_NUM; k++){
        en_m = malloc(sizeof(char)*(EN_SIZE+1)); 
        tm_m = malloc(sizeof(char)*(RSA_size(keypair)));
        de_m = malloc(sizeof(char)*(EN_SIZE+1));
        fread(en_m, 1, EN_SIZE, f);
        en_m[EN_SIZE] = '\0';
    }
    fclose(f);
    printf("The length of string to be encrypted in each encryption = %d bytes\n", EN_SIZE);
    printf("Total number of the string message to be encrypted = %d\n", STR_NUM);

    omp_set_num_threads(4); //set up 4 threads
    kmp_set_defaults("KMP_AFFINITY=scatter");

    // Encryption 
    #pragma omp parallel for shared(en_m, tm_m, keypair) private(i, encrypt_len) schedule(static)
    for(i=0; i<STR_NUM; i++)
        encrypt_len = RSA_public_encrypt(EN_SIZE, (unsigned char*)en_m, (unsigned char*)tm_m, keypair, RSA_PKCS1_OAEP_PADDING);
    printf("Encryption has been finished\n");

    f = fopen("en_message.txt", "w");
    for (k=0; k<STR_NUM; k++){
        fwrite(tm_m, sizeof(*tm_m), RSA_size(keypair), f);
    }
    fclose(f);
    
/************************************************************************************************/

    f = fopen("en_message.txt", "r");
    for (k=0; k<STR_NUM; k++){
        rm_m = malloc(sizeof(char)*(RSA_size(keypair)));
        fread(rm_m, 1, RSA_size(keypair), f);
    }
    fclose(f);

    // Decryption
    #pragma omp parallel for shared(rm_m, de_m, keypair) private(j, decrypt_len) schedule(static)
    for (j=0; j<STR_NUM; j++)
        decrypt_len = RSA_private_decrypt(RSA_size(keypair), (unsigned char*)rm_m, (unsigned char*)de_m, keypair, RSA_PKCS1_OAEP_PADDING);
    printf("Decryption has been finished.\n");

    int temp = 0;
    for (k=0; k<STR_NUM; k++){
        if (strcmp(en_m, de_m)!=0)
            temp += 1;
    }
    printf("Total number of dismatch = %d\n", temp);

    RSA_free(keypair);
    for (k=0; k<STR_NUM; k++){
        free(en_m);
        free(de_m);
        free(tm_m);
        free(rm_m);
    }

    return 0;

}

0 Kudos
1 Reply
jimdempseyatthecove
Honored Contributor III
596 Views

You might find this pertinent: http://www.openssl.org/docs/crypto/threads.html#

Jim Dempsey

0 Kudos
Reply