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[k] = malloc(sizeof(char)*(EN_SIZE+1)); tm_m[k] = malloc(sizeof(char)*(RSA_size(keypair))); de_m[k] = malloc(sizeof(char)*(EN_SIZE+1)); fread(en_m[k], 1, EN_SIZE, f); en_m[k][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[i], (unsigned char*)tm_m[i], 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[k], sizeof(*tm_m[k]), RSA_size(keypair), f); } fclose(f); /************************************************************************************************/ f = fopen("en_message.txt", "r"); for (k=0; k<STR_NUM; k++){ rm_m[k] = malloc(sizeof(char)*(RSA_size(keypair))); fread(rm_m[k], 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[j], (unsigned char*)de_m[j], 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[k], de_m[k])!=0) temp += 1; } printf("Total number of dismatch = %d\n", temp); RSA_free(keypair); for (k=0; k<STR_NUM; k++){ free(en_m[k]); free(de_m[k]); free(tm_m[k]); free(rm_m[k]); } return 0; } -- View this message in context: http://openssl.6102.n7.nabble.com/openssl-with-openmp-tp48689.html Sent from the OpenSSL - User mailing list archive at Nabble.com. ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager majord...@openssl.org