#include using std::unique_ptr; #include #include #include #include #include //using BN_ptr = std::unique_ptr; //using RSA_ptr = std::unique_ptr; EVP_CIPHER_CTX *ctx; RSA* rsa; BIGNUM* bn; //RSA_ptr rsa_signing_keypair; //(RSA_new(), ::RSA_free); //BN_ptr rsa_bignum; // assumes that the digest is at least of length 256/8 bytes. uint32_t generate_sha256_hash(const unsigned char *message, size_t message_len, unsigned char *digest) { EVP_MD_CTX *mdctx; unsigned int digest_len; if((mdctx = EVP_MD_CTX_create()) == NULL) { printf("EVP_MD_CTX_create returned NULL - could not create context\n"); fflush(stdout); return 0x1; } if(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL) != 1) { printf("EVP_DigestInit_ex returned 0 - could not initialize hash with SHA256\n"); fflush(stdout); return 0x2; } if(EVP_DigestUpdate(mdctx, message, message_len) != 1) { printf("EVP_DigestUpdate returned 0 - could not compute SHA256 hash\n"); fflush(stdout); return 0x3; } if(1 != EVP_DigestFinal_ex(mdctx, digest, &digest_len)) { printf("EVP_DigestFinal_ex returned 0 - could not finalize SHA256 hash\n"); fflush(stdout); return 0x4; } if(digest_len != 32) { printf("EVP_DigestFinal_ex returned a digest length of 0x%x instead of 0x20\n", digest_len); fflush(stdout); return 0x5; } EVP_MD_CTX_destroy(mdctx); return 0; } //private: // RSA_ptr rsa(RSA_new(), ::RSA_free); // BN_ptr bn(BN_new(), ::BN_free); uint32_t generate_rsa_keypair(FILE* fp, std::string& priv_key_str, std::string& pub_key_str) //, uint8_t* hash) { int rc; rsa=RSA_new(); bn=BN_new(); rc = BN_set_word(bn, 3); if(rc != 1) return 0x1; rc = RSA_generate_key_ex(rsa, 3072, bn, NULL); if(rc != 1) return 0x2; printf("Generated key\n"); fflush(stdout); /* int pub_key_der_encoded_len, priv_key_der_encoded_len; unsigned char *pub_key_der, priv_key_der; pub_key_der = NULL; pub_key_der_encoded_len = i2d_RSAPublicKey(rsa.get(), (unsigned char**) &pub_key_der); if (pub_key_der_encoded_len < 0) return 0x3; priv_key_der = NULL; priv_key_der_encoded_len = i2d_RSAPrivateKey(rsa.get(), (unsigned char**) &priv_key_der); if (priv_key_der_encoded_len < 0) return 0x4; printf("Done\n"); fflush(stdout); // priv_key_str=std::string(priv_key_der, priv_key_der_encoded_len); //, priv_key_der); // pub_key_str=std::string(pub_key_der, pub_key_der_encoded_len); */ // BIO* bio_rsa; // rc = PEM_write_RSA_PUBKEY(fp, rsa); rc= PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL); if(rc != 1) return 0x3; fflush(fp); // bio_rsa = BIO_new_file("apache_signature_keypair.pem", "w+"); // rc = PEM_write_bio_RSAPublicKey(bio_rsa, rsa.get()); // if(rc != 1) // return 0x3; // BIO_flush(bio_rsa); free(bio_rsa); return 0; } uint32_t generate_rsa_keypair_hash(uint8_t* hash) { /* uint32_t return_internal; const BIGNUM* n_internal_bigendian_struct; RSA_get0_key(rsa, &n_internal_bigendian_struct, NULL, NULL); BIGNUM* n_bigendian_struct = BN_dup(n_internal_bigendian_struct); uint32_t count; int n_bignum_length=BN_num_bytes(n_bigendian_struct); unsigned char *n_bigendian = (unsigned char*) malloc(n_bignum_length); int length_bignum_le = BN_bn2bin(n_bigendian_struct, n_bigendian); unsigned char* n_littleendian = (unsigned char*) malloc(length_bignum_le); for(count=0; count