title: Convert code using openssl to use mbedtls
body: <p>I am trying to convert some C code written to run on a Mac to an embedded device that does not have any encryption libraries. The code for the Mac is using libcrypto. I tried building libcrypto from openssl sources for the embedded device but I get hundreds of errors due to function pointer prototypes not matching. openssl is riddled with huge macros. As an alternative I am now trying to use mbedtls but I have not been able to get a decrypt function to work.</p>
  
  <p>The code I am trying to port is a bit odd. It has what it calls a public key and is actually calling <code>RSA_public_encrypt()</code> with no padding to decrypt data. As a test, I changed the Mac code to call <code>RSA_public_decrypt()</code> and it worked so I assume the key is symmetric. The key it is using looks like this:</p>
  
  <pre><code>"-----BEGIN PUBLIC KEY-----\n"
  5 lines of Base64 strings
  "-----END PUBLIC KEY-----\n"
  </code></pre>
  
  <p>For mbedtls I am using <code>mbedtls_pk_parse_public_key()</code> to parse the key. If I inspect the low level RSA key structure after parsing the key there is a 128 byte N component and a 16 byte E component. I get the same key data with both openssl and mbedtls so it appears that the key is parsed properly. When decrypting on the Mac with <code>RSA_public_decrypt()</code>, the input and output are both 128 bytes. For mbedtls, I am calling <code>mbedtls_pk_decrypt()</code> to decrypt but when I trace through the code it calls <code>mbedtls_rsa_rsaes_pkcs1_v15_decrypt()</code> which forces the padding to be 11 bytes.</p>
  
  <p>So my questions are: 1) exactly what kind of "public" key contains only N and E components and uses no padding; 2) Am I calling the correct mbedtls decryption function?</p>
  
  <p>EDIT: Tried another approach and my output buffer just gets filled with zeros.</p>
  
  <pre><code>mbedtls_rsa_context rsa;
  mbedtls_rsa_init(&amp;rsa, MBEDTLS_RSA_PKCS_V15, 0);
  mbedtls_rsa_import_raw(&amp;rsa, modulus, sizeof(modulus), NULL, 0, NULL, 0, NULL, 0, exp, sizeof(exp));
  mbedtls_rsa_complete(&amp;rsa);
  mbedtls_rsa_public(&amp;rsa, inBfr, outBfr);
  mbedtls_rsa_free(&amp;rsa);
  </code></pre>
  
  <p>EDIT 2: My ultimate target is an embedded device with an ARM processor but I was testing on Windows to see if mbedtls would work. I started with VS 2010 because that is what was being used for the project I am working on. I switched to VS 2015 and the 2nd approach of importing the raw key data and calling <code>mbedtls_rsa_public()</code> worked perfectly. I guess the VS 2010 compiler just isn't good enough. I then ported the code to the devlepment system for my embedded device and it also worked.</p>