Guest User

Untitled

a guest
Aug 28th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <memory>
  2. #include <vector>
  3. #include <thread>
  4. #include <unistd.h>
  5.  
  6. #include <openssl/evp.h>
  7. #include <openssl/pem.h>
  8.  
  9. using namespace std;
  10.  
  11. EVP_PKEY *pkey = nullptr;
  12. volatile bool run = false;
  13.  
  14. void worker()
  15. {
  16.     const size_t SIZE = 1024*1024;
  17.     unique_ptr<char> message(new char[SIZE]);
  18.  
  19.     size_t slen;
  20.     EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
  21.     int retval;
  22.  
  23.     // Just busy wait to demonstrate the error
  24.     while(run == false);
  25.  
  26.     retval = EVP_DigestSignInit(mdctx, nullptr, EVP_sha1(), nullptr, pkey);
  27.     if(retval <= 0)
  28.     {
  29.         printf("ERROR");
  30.         goto out;
  31.     }
  32.     retval = EVP_DigestSignUpdate(mdctx, message.get(), SIZE);
  33.     if(retval <= 0)
  34.     {
  35.         printf("ERROR");
  36.         goto out;
  37.     }
  38.  
  39.     retval = EVP_DigestSignFinal(mdctx, nullptr, &slen);
  40.     if(retval <= 0)
  41.     {
  42.         printf("ERROR");
  43.         goto out;
  44.     }
  45.  
  46.     //retval = EVP_DigestSignFinal(mdctx, (unsigned char*)*sig, &slen);
  47. out:
  48.     EVP_MD_CTX_cleanup(mdctx);
  49.     EVP_MD_CTX_destroy(mdctx);
  50.     mdctx = nullptr;
  51. }
  52.  
  53. int main(void)
  54. {
  55.     const int SIZE = 20;
  56.     vector<thread> t;
  57.  
  58.     // Open some private key
  59.     FILE *fp = fopen("private.pem", "r");
  60.     if(!fp)
  61.     {
  62.         printf("Couldn't open file\n");
  63.         exit(-1);
  64.     }
  65.     if( PEM_read_PrivateKey(fp, &pkey, nullptr, nullptr) <= 0 )
  66.     {
  67.         printf("Couldn't read private key");
  68.         exit(-1);
  69.     }
  70.     fclose(fp);
  71.  
  72.     for(int i = 0 ; i < SIZE ; i++)
  73.     {
  74.         t.push_back( thread(worker) );
  75.     }
  76.  
  77.     // Just wait for all threads to be at the same position and fire them at once
  78.     sleep(1);
  79.     run = true;
  80.  
  81.     for(auto &v : t)
  82.     {
  83.         v.join();
  84.     }
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment