<div dir="ltr">Hi,<div><br></div><div>I am playing around with RSA signatures with different padding and have some questions.</div><div><br></div><div>I have my sample code below for reference. It's in Swift (but it should still be close enough to C to be readable). Also in Swift, some of the complex macros in OpenSSL have to be broken down to be compilable hence my usage of <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_DigestUpdate</span> instead of <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_DigestVerifyUpdate</span> .</div><div><br></div><div>I am trying to define different padding options and so am defining and using a <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_PKEY_CTX</span> . However I am not sure if this padding is getting used in the signature since  my Verify outputs OK regardless of which option my Sign uses. Which leads to:</div><div><br></div><div><div>1 - Do I need to use the same <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_PKEY_CTX</span> with the same options when doing verify? Right now even when I don't use any <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_PKEY_CTX</span> in Verify, I still verify OK.</div></div><div><br></div><div>2 - Do I need to set the hash function I am using in both <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_PKEY_CTX</span>  as well as <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_MD_CTX</span> ? Or the latter is what defines this?<br></div><div><br></div><div>3 - In general, is there a way of making the Signature/Encryptions in OpenSSL be deterministic for debugging/testing purposes? </div><div><br></div><div>Thanks in advance for any insight in the above.</div><div>Gelareh</div><div><br></div><div><br></div><div><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(186,45,162)">let</span> md_ctx = <span style="color:rgb(62,30,129)">EVP_MD_CTX_create</span>()</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(186,45,162)">let</span> md_ctx_verify = <span style="color:rgb(62,30,129)">EVP_MD_CTX_create</span>()</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        </p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px"><span style="color:rgb(0,132,0)">        // OPTIONS</span><br></p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px"><span style="color:rgb(0,132,0)"><br></span></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">        </span>// To define padding option used in signature</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(186,45,162)">let</span> pkey_ctx = <span style="color:rgb(62,30,129)">EVP_PKEY_CTX_new</span>(<span style="color:rgb(79,129,135)">rsaKeypair</span>, <span style="color:rgb(186,45,162)">nil</span>)</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        </p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">        // EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PADDING)</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">        // complex macro needs to be replaced</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">        </span><span style="color:rgb(62,30,129)">EVP_PKEY_CTX_ctrl</span><span style="color:rgb(0,0,0)">(pkey_ctx, </span>EVP_PKEY_RSA<span style="color:rgb(0,0,0)">, </span><span style="color:rgb(39,42,216)">-1</span><span style="color:rgb(0,0,0)">, </span>EVP_PKEY_CTRL_RSA_PADDING<span style="color:rgb(0,0,0)">, </span>RSA_X931_PADDING<span style="color:rgb(0,0,0)">, </span><span style="color:rgb(186,45,162)">nil</span><span style="color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)"><br></span></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">        // EVP_PKEY_CTX_set_signature_md() When should this be set?</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        </p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">        </span>//  SIGN</p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        <span style="color:rgb(186,45,162)">var</span> rc = <span style="color:rgb(62,30,129)">EVP_DigestSignInit</span>(md_ctx, &pkey_ctx, <span style="color:rgb(62,30,129)">EVP_sha256</span>(), <span style="color:rgb(186,45,162)">nil</span>, <span style="color:rgb(79,129,135)">myRSA</span>.<span style="color:rgb(79,129,135)">rsaKeypair</span>)<br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(62,30,129)">print</span>(<span style="color:rgb(209,47,27)">"rc = </span>\<span style="color:rgb(209,47,27)">(</span>rc<span style="color:rgb(209,47,27)">)"</span>)</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">        </span>// EVP_DigestSignUpdate(md_ctx, message, message.count)</p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)">        // Complex macro needs to be replaced</p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">        rc = </span><span style="color:rgb(62,30,129)">EVP_DigestUpdate</span><span style="color:rgb(0,0,0)">(md_ctx, message, message.</span><span style="color:rgb(112,61,170)">count</span><span style="color:rgb(0,0,0)">)</span><br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(62,30,129)">print</span>(<span style="color:rgb(209,47,27)">"rc = </span>\<span style="color:rgb(209,47,27)">(</span>rc<span style="color:rgb(209,47,27)">)"</span>)</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        </p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">        </span>// allocate memory for signature</p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        <span style="color:rgb(186,45,162)">var</span> sig_len: <span style="color:rgb(112,61,170)">Int</span> = <span style="color:rgb(112,61,170)">Int</span><span style="color:rgb(209,47,27)">(</span><span style="color:rgb(62,30,129)">EVP_PKEY_size</span><font color="#000000">(</font><span style="color:rgb(79,129,135)">rsaKeypair</span><font color="#000000">)</font><font color="#d12f1b">)</font><br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(186,45,162)">let</span> sig = <span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><<span style="color:rgb(112,61,170)">UInt8</span>>.<span style="color:rgb(62,30,129)">allocate</span>(capacity: sig_len)</p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        rc = <span style="color:rgb(62,30,129)">EVP_DigestSignFinal</span>(md_ctx, sig, &sig_len)</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">      </p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        </p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">        </span>// VERIFY</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        rc = <span style="color:rgb(62,30,129)">EVP_DigestVerifyInit</span>(md_ctx_verify, <span style="color:rgb(186,45,162)">nil</span>, <span style="color:rgb(62,30,129)">EVP_sha256</span>(), <span style="color:rgb(186,45,162)">nil</span>, <span style="color:rgb(79,129,135)">rsaKeypair</span>)</p><p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        <span style="color:rgb(0,132,0)">//        rc = EVP_DigestVerifyUpdate(md_ctx_verify, message, message.count)</span><br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">        rc = <span style="color:rgb(62,30,129)">EVP_DigestUpdate</span>(md_ctx_verify, message, message.<span style="color:rgb(112,61,170)">count</span>)</p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">       <br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">        rc = </span><span style="color:rgb(62,30,129)">EVP_DigestVerifyFinal</span><span style="color:rgb(0,0,0)">(md_ctx_verify, sig, sig_len)</span><br></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)"><span style="color:rgb(0,0,0)">        </span><span style="color:rgb(62,30,129)">print</span><span style="color:rgb(0,0,0)">(</span>"signature verified = <span style="color:rgb(0,0,0)">\</span>(<span style="color:rgb(0,0,0)">rc == </span><span style="color:rgb(39,42,216)">1</span><span style="color:rgb(0,0,0)"> ? </span>"OK"<span style="color:rgb(0,0,0)"> : </span>"FAIL")"<span style="color:rgb(0,0,0)">)</span></p>
<p style="margin:0px;font-stretch:normal;font-size:14px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);min-height:16px">        </p></div><div><br></div><div><br></div></div>