Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <vector>
- #include <thread>
- #include <unistd.h>
- #include <openssl/evp.h>
- #include <openssl/pem.h>
- using namespace std;
- EVP_PKEY *pkey = nullptr;
- volatile bool run = false;
- void worker()
- {
- const size_t SIZE = 1024*1024;
- unique_ptr<char> message(new char[SIZE]);
- size_t slen;
- EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
- int retval;
- // Just busy wait to demonstrate the error
- while(run == false);
- retval = EVP_DigestSignInit(mdctx, nullptr, EVP_sha1(), nullptr, pkey);
- if(retval <= 0)
- {
- printf("ERROR");
- goto out;
- }
- retval = EVP_DigestSignUpdate(mdctx, message.get(), SIZE);
- if(retval <= 0)
- {
- printf("ERROR");
- goto out;
- }
- retval = EVP_DigestSignFinal(mdctx, nullptr, &slen);
- if(retval <= 0)
- {
- printf("ERROR");
- goto out;
- }
- //retval = EVP_DigestSignFinal(mdctx, (unsigned char*)*sig, &slen);
- out:
- EVP_MD_CTX_cleanup(mdctx);
- EVP_MD_CTX_destroy(mdctx);
- mdctx = nullptr;
- }
- int main(void)
- {
- const int SIZE = 20;
- vector<thread> t;
- // Open some private key
- FILE *fp = fopen("private.pem", "r");
- if(!fp)
- {
- printf("Couldn't open file\n");
- exit(-1);
- }
- if( PEM_read_PrivateKey(fp, &pkey, nullptr, nullptr) <= 0 )
- {
- printf("Couldn't read private key");
- exit(-1);
- }
- fclose(fp);
- for(int i = 0 ; i < SIZE ; i++)
- {
- t.push_back( thread(worker) );
- }
- // Just wait for all threads to be at the same position and fire them at once
- sleep(1);
- run = true;
- for(auto &v : t)
- {
- v.join();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment