<div dir="ltr">Hi all,<br><div class="gmail_quote"><div dir="ltr"><div class="gmail_quote"><div dir="ltr"><div><br></div><div>Any help would be *much* appreciated. I am playing around with RSA signatures with different padding options and I have some questions.</div><div><br></div><div><span class=""><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></span><div>1 - Do I need to use a <span style="color:rgb(62,30,129);font-family:Menlo;font-size:14px">EVP_PKEY_CTX</span> with the same options when doing verify? If so, I assume I can't reuse the same PKey_Ctx and I have to define another one. 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, which makes me question if the padding option has been set.</div><div><br></div><div>2 - Is there a way to figure out what padding/hashing/etc option was used for the Sign/verify operation? This way I can be sure what algorithm or standard is being used. </div><div><br></div><div>3 - 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 for the signing option?<br></div><div><br></div><div>4 - In general, is there a way of making the Signature/Encryptions in OpenSSL be deterministic for debugging/testing purposes? </div></div><div><br></div><div>5 - I noticed that there are two ways of determining the signature size: (a) by calling <span style="font-family:Menlo;font-size:14px;color:rgb(62,30,129)">EVP_PKEY_size</span><font color="#000000" style="color:rgb(0,0,0);font-family:Menlo;font-size:14px">(</font><span style="font-family:Menlo;font-size:14px;color:rgb(79,129,135)">rsaKeypa<wbr>ir</span><font color="#000000" style="color:rgb(0,0,0);font-family:Menlo;font-size:14px">)</font> as I am doing below, as well as (b) calling <span style="font-family:Menlo;font-size:14px;color:rgb(62,30,129)">EVP_DigestSignFinal</span><span style="color:rgb(0,0,0);font-family:Menlo;font-size:14px">(<wbr>md_ctx, nil, &sig_len)</span> . Is one better than the other? </div><div><br></div><div>My sample code is 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> .<br></div><span class=""><div><br></div><div>Thanks in advance for any insight in the above.</div><div><br></div></span><div>cheers!</div><div>Gelareh</div><div><br></div><div><br></div><div><span class=""><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></span><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)">// To define padding option used in signature</span><br></p><span class="">
<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>
</span><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(p<wbr>key_ctx, RSA_PKCS1_PADDING) - complex macro needs to be replaced</p><span class="">
<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(<wbr>) 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>
</span><span class=""><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)">// EVP_DigestSignUpdate(md_ctx, 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,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)">        <br></p></span><span class=""><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"><wbr>)</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)">al<wbr>locate</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_ve<wbr>rify, <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_<wbr>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<wbr>, 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_v<wbr>erify, 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></span></div><div><br></div><div><br></div></div>
</div><br></div>
</div><br></div>