Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // g++ -Wall -Wextra -Wconversion -g3 -std=c++11 openssl-test.cpp -o openssl-test.exe -lssl -lcrypto
- #include <iostream>
- using std::cout;
- using std::cerr;
- using std::endl;
- #include <string>
- using std::string;
- #include <memory>
- using std::unique_ptr;
- #include <openssl/bn.h>
- #include <openssl/rsa.h>
- #include <openssl/evp.h>
- #include <openssl/err.h>
- const char nz[] =
- "20446702916744654562596343388758805860065209639960173505037453331270270518732245"
- "08977372301204320323609709562340204469011575537734525469644875960570778896584888"
- "95017468362112062706438336639499925362469853626937363871851454247879222415857219"
- "92924045675229348655595626434390043002821512765630397723028023792577935108185822"
- "75369257422156693093780503115582009714681996492027000881132703628678639279359312"
- "17624250488602118597634417704467037220158572506211078553986931332640811506974231"
- "88751482418465308470313958250757758547155699749157985955379381294962058862159085"
- "915015369381046959790476428631998204940879604226680285601";
- const char ez[] = "65537";
- const char dz[] =
- "23583109899396195101799862623499368829246520235662137651186064319555667005065389"
- "11356936879137503597382515919515633242482643314423192704128296593672966061810149"
- "31632061789402182278402640746140338406535182197235078430096761014345948432406842"
- "76746396884059179774424728049430754391920261073195321175575450790865379829879825"
- "22396626690057355718157403493216553255260857777965627529169195827622139772389760"
- "13057175483467867984218114225248961766503010944557397801270779301059273764049922"
- "00150833924259148778478404572782464027609558833769999511998277062853834711506435"
- "61410605789710883438795588594095047409018233862167884701";
- const char pz[] =
- "15737705590244743839558616502896029191493197327877753279847020015603526753735923"
- "90718294084119093232085749598005372477289597182368848096852332845373492076546615"
- "30801859889389455120932077199406250387226339056140578989122526711937239401762061"
- "949364440402067108084155200696015505170135950332209194782224750221639";
- const char qz[] =
- "12992175256740635899099334754006444501823007340248226099417932857332386190837921"
- "12746269565434716649972371852989646481333243433270528522640603220881224011247812"
- "49085873464824282666514908127141915943024862618996371026577302203267804867959037"
- "802770797169483022132210859867700312376409633383772189122488119155159";
- const char dmodp1z[] =
- "49227606481837320706006017716613302169096929249354401671859241393391870004972220"
- "28735680413269055839870281941362914961691371628021024152077883230870590287807744"
- "29930898230386473286709428656763070164661176228829801375865815889453805901417866"
- "2376933683632720014228880806671525788467258162275185762295508460173";
- const char dmodq1z[] =
- "95501022448116849078110281587424883261489167280943290677534750975651978631525094"
- "58693377793498640446735285662438504904366643141183520919433056069507619439072908"
- "27084374978171871336296929080814602230691019089602999501706662853078906832637851"
- "45958472051553704139602453015343925544671829327400421727981791235189";
- const char invqmodpz[] =
- "23545019917990284444784037831882732213707743418529123971725460465297450415859883"
- "70728413617913564636615863358005459444719505281341294577593327462082221309955672"
- "00897700599820911444355459765155081084657241882422419679677095553363318743253968"
- "76783846248039429242763646988988076187339075374375350105207330456437";
- using BN_ptr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>;
- using RSA_ptr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
- using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
- using EVP_MD_CTX_ptr = std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_destroy)>;
- #define UNUSED(x) ((void)x)
- int main(int argc, char* argv[])
- {
- UNUSED(argc); UNUSED(argv);
- int rc;
- long err;
- RSA_ptr rsa(RSA_new(), ::RSA_free);
- BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL, *dmodp1 = NULL, *dmodq1 = NULL, *invqmodp = NULL;
- rc = BN_dec2bn(&n, nz);
- if(rc == 0 || n == NULL) {
- cerr << "BN_dec2bn failed for n" << endl;
- exit(1);
- }
- rsa->n = n; n = NULL;
- rc = BN_dec2bn(&e, ez);
- if(rc == 0 || e == NULL) {
- cerr << "BN_dec2bn failed for e" << endl;
- exit(1);
- }
- rsa->e = e; e = NULL;
- rc = BN_dec2bn(&d, dz);
- if(rc == 0 || d == NULL) {
- cerr << "BN_dec2bn failed for d" << endl;
- exit(1);
- }
- rsa->d = d; d = NULL;
- rc = BN_dec2bn(&p, pz);
- if(rc == 0 || p == NULL) {
- cerr << "BN_dec2bn failed for p" << endl;
- exit(1);
- }
- rsa->p = p; p = NULL;
- rc = BN_dec2bn(&q, qz);
- if(rc == 0 || q == NULL) {
- cerr << "BN_dec2bn failed for q" << endl;
- exit(1);
- }
- rsa->q = q; q = NULL;
- rc = BN_dec2bn(&dmodp1, dmodp1z);
- if(rc == 0 || dmodp1 == NULL) {
- cerr << "BN_dec2bn failed for dmodp1" << endl;
- exit(1);
- }
- rsa->dmp1 = dmodp1; dmodp1 = NULL;
- rc = BN_dec2bn(&dmodq1, dmodq1z);
- if(rc == 0 || dmodq1 == NULL) {
- cerr << "BN_dec2bn failed for dmodq1" << endl;
- exit(1);
- }
- rsa->dmq1 = dmodq1; dmodq1 = NULL;
- rc = BN_dec2bn(&invqmodp, invqmodpz);
- if(rc == 0 || invqmodp == NULL) {
- cerr << "BN_dec2bn failed for invpmodq" << endl;
- exit(1);
- }
- rsa->iqmp = invqmodp; invqmodp = NULL;
- rc = RSA_check_key(rsa.get());
- err = ERR_get_error();
- if(rc != 1) {
- cerr << "RSA_check_key failed, error 0x" << std::hex << err << endl;
- exit(1);
- }
- EVP_PKEY_ptr pkey(EVP_PKEY_new(), ::EVP_PKEY_free);
- rc = EVP_PKEY_set1_RSA(pkey.get(), rsa.get());
- err = ERR_get_error();
- if(rc != 1) {
- cerr << "EVP_PKEY_set1_RSA failed, error 0x" << std::hex << err << endl;
- exit(1);
- }
- EVP_MD_CTX_ptr ctx(EVP_MD_CTX_create(), ::EVP_MD_CTX_destroy);
- EVP_MD_CTX_init(ctx.get());
- const EVP_MD* md = EVP_sha256();
- rc = EVP_SignInit(ctx.get(), md);
- err = ERR_get_error();
- if(rc != 1) {
- cerr << "EVP_SignInit_ex failed, error 0x" << std::hex << err << endl;
- exit(1);
- }
- const char message[] = "Now is the time for all good men...";
- rc = EVP_SignUpdate(ctx.get(), message, (unsigned int)sizeof(message));
- err = ERR_get_error();
- if(rc != 1) {
- cerr << "EVP_SignUpdate failed, error 0x" << std::hex << err << endl;
- exit(1);
- }
- const unsigned int req = std::max(EVP_MD_size(md), EVP_PKEY_size(pkey.get()));
- unique_ptr<unsigned char[]> signature(new unsigned char[req]);
- unsigned int size = req;
- rc = EVP_SignFinal(ctx.get(), signature.get(), &size, pkey.get());
- err = ERR_get_error();
- if(rc != 1) {
- cerr << "EVP_SignFinal failed, error 0x" << std::hex << err << endl;
- exit(1);
- }
- size = std::min(size, (unsigned int)EVP_MD_size(md));
- cout << "Signature: ";
- for(unsigned i = 0; i < size; i++)
- cout << std::hex << (signature[i] & 0xFF);
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement