title: OpenSSL Fails at EVP_VerifyFinal
body: <p>After a few days of scouring the internet and openssl docs i've hit a wall. I'm attempting to sign a message with a private key, and then verify the authenticity of the message with a public key.</p>
  
  <p>Key Generation Code:</p>
  
  <pre><code>    //Generate RSA Keys
      EVP_PKEY_CTX* KeyCtx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
      if (KeyCtx == nullptr)
          return false;
  
      if (EVP_PKEY_keygen_init(KeyCtx) &lt;= 0)
          return false;
  
      if (EVP_PKEY_CTX_set_rsa_keygen_bits(KeyCtx, RSA_KeyLen) &lt;= 0)
          return false;
  
      if (EVP_PKEY_keygen(KeyCtx, &amp;m_ServerKeyPair) &lt;= 0)
          return false;
  
      if (EVP_PKEY_keygen(KeyCtx, &amp;m_ClientKeyPair) &lt;= 0)
          return false;
  
      //Write Keys to EVP_PKEYS for actual internal encryption,BIO mem is wiped after read so we need a new copy
      PEM_read_bio_PrivateKey(KeyToPrivBio(m_ServerKeyPair), &amp;m_ServerPrivateKey, NULL, NULL);
      PEM_read_bio_PUBKEY(KeyToPubBio(m_ServerKeyPair), &amp;m_ServerPublicKey, NULL, NULL);
      PEM_read_bio_PrivateKey(KeyToPrivBio(m_ClientKeyPair), &amp;m_ClientPrivateKey, NULL, NULL);
      PEM_read_bio_PUBKEY(KeyToPubBio(m_ClientKeyPair), &amp;m_ClientPublicKey, NULL, NULL);
  </code></pre>
  
  <p>RSA Sign:</p>
  
  <pre><code>bool SecureCrypto::RSASign(const unsigned char* Msg, size_t MsgLen,unsigned char** EncMsg, size_t&amp; MsgLenEnc)
  {
      EVP_PKEY_size(m_ServerPrivateKey);
      *EncMsg = (unsigned char*)malloc(EVP_PKEY_size(m_ServerPrivateKey));
  
      if (EVP_SignInit_ex(m_RSASignCtx, EVP_sha1(), 0) &lt;= 0)
      {
          printf("Failed Init\n");
          return false;
      }
  
      if (EVP_SignUpdate(m_RSASignCtx, Msg, MsgLen) &lt;= 0)
      {
          printf("Failed Update\n");
          return false;
      }
  
      if (EVP_SignFinal(m_RSASignCtx, *EncMsg, &amp;MsgLenEnc, m_ServerPrivateKey) &lt;=0)
      {
          printf("Failed Final Sign\n");
          return false;
      }
      EVP_MD_CTX_cleanup(m_RSASignCtx);
  
      return true;
  }
  </code></pre>
  
  <p>RSA Verify:</p>
  
  <pre><code>bool SecureCrypto::RSAVerifySignature(const unsigned char* MsgHash, size_t MsgHashLen,unsigned char* Msg, size_t MsgLen)
  {
      if (EVP_VerifyInit(m_RSAVerifyCtx, EVP_sha1()) &lt;= 0)
      {
          printf("Failed Verify Init\n");
          return false;
      }
  
      if (EVP_VerifyUpdate(m_RSAVerifyCtx, Msg, MsgLen) &lt;= 0)
      {
          printf("Failed Verify \n");
          return false;
      }
  
      if (EVP_VerifyFinal(m_RSAVerifyCtx, MsgHash, MsgHashLen, m_ServerPublicKey)&lt;=0);
      {
          printf("Failed Final Verify %s\n",ERR_error_string(ERR_get_error(),NULL));
          return false;
      }
      EVP_MD_CTX_cleanup(m_RSAVerifyCtx);
      return true;
  }
  </code></pre>
  
  <p>Key Generation is successful, and i have viewed the keys it creates they appear to be valid and are in the format <code>-----BEGIN PRIVATE KEY-----</code>, some large prime, <code>-----END PRIVATE KEY-----</code>. Sign appears to be successful and returns a hash. EVP_Verify fails on the last step EVP_VerifyFinal, ERR_error_string reports this: </p>
  
  <pre><code>error:00000000:lib(0):func(0):reason(0)
  </code></pre>
  
  <p>Which is obviously not helpful, can someone please point out what i may be doing wrong. This is my first dive into openssl, so there may be some big issues here.</p>