<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle18
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal" style="text-indent:.5in"><span lang="EN-IN">We do have generated the key using EVP_PKEY_gen as suggested in earlier emails, but since this was a non-ephemeral and we wanted to store the key in "raw" octet bytes, so we did extracted the
 whole DH priv/pub key pair out from the key generated via  EVP_PKEY_gen  ( using as suggested… EVP_PKEY_get_raw_public_key (pkey, pub, &len)  )<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"> <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">Now, at a later stage in application we have to compute the Secret key using the stored key’s (in above step).<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">As of now,  these keys are in uchar format, but are converted to BIGNUM and given to DH_compute_key as below.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"> <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">   BIGNUM      *<span style="background:yellow;mso-highlight:yellow">bn_publicKey</span>;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">    <span style="background:aqua;mso-highlight:aqua">
dh->priv_key</span> = BN_bin2bn(privateKey, octet_len, NULL);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">    <span style="background:yellow;mso-highlight:yellow">
bn_publicKey</span> = BN_bin2bn(publicKey, octet_len, NULL);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">    rv = DH_compute_key(sharedSecret, bn_publicKey, dh);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"> <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">So in order to keep the existing frame work in place and just replace the DH_compute_key, we should be using the  dh->priv_key/ bn_publicKey  to compute shared secret key.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">So we require to convert the BIGNUM key types to EVP_KEY types to use in EVP_PKEY_derive_init, EVP_PKEY_derive_set_peer, and EVP_PKEY_derive to get shared secret<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">Please suggest…<br>
<br>
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">Is it possible to change the format of your raw blob?  If so, you can use i2d_PrivateKey or friends to output the entire private key to your raw data blob, and use d2i_PrivateKey et al to read it back into a working EVP_PKEY
 in a single call.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">Otherwise, one shortcut you can do to avoid all the params work is to create a static array since you should already know how many params you need.  But you need the public key, the private key, the generator (g), and
 the prime modulus (p).  The following (untested) code ought to work.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">OSSL_PARAM params[5];<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">params[0] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_FFC_P, <prime modulus>, <prime modulus bytes size>);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">params[1] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_G, <generator>);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">params[2] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_PUB_KEY, <public key>, <public key bytes size>);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">params[3] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_PRIV_KEY, <private key>, <private key bytes size>);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">params[4] = OSSL_PARAM_construct_end();<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">my_key_ctx = EVP_PKEY_CTX_new_from_name(NULL, “DH”, NULL);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">EVP_PKEY_derive_init(my_key_ctx);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">EVP_PKEY_CTX_set_params(my_key_ctx, params);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN">…<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-IN"><o:p> </o:p></span></p>
</div>
</body>
</html>