<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>SOLVED - Using LESec with nRF52840 and pc-ble-driver-js v2.6.1</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/63593/solved---using-lesec-with-nrf52840-and-pc-ble-driver-js-v2-6-1</link><description>EDIT - My solution for this is posted below as a reply and it explains the issues in the code. This is an example of how to use pc-ble-dongle-js driver to authenticate using BLE 4.2 LESec protocol. This example assumes you 1) Want to bond with the device</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 10 Jul 2020 00:40:38 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/63593/solved---using-lesec-with-nrf52840-and-pc-ble-driver-js-v2-6-1" /><item><title>RE: LESec with nRF52840 and pc-ble-driver-js v2.6.1 sending all 0's for Diffie Hellman public key</title><link>https://devzone.nordicsemi.com/thread/259288?ContentTypeID=1</link><pubDate>Fri, 10 Jul 2020 00:40:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e4a9d72b-6db5-4b64-9e32-ae2cf4361149</guid><dc:creator>Matthew Stormo</dc:creator><description>&lt;p&gt;This reply is a full answer on the two issues in the above code. It includes the fix in the first reply.&lt;/p&gt;
&lt;p&gt;There were two issues in the above code. &lt;br /&gt;1) The public key being passed to the peripheral as all 0&amp;#39;s was caused from the call to replySecParams with a null secParams. This happens in the updated event listener for &amp;#39;secParamsRequest&amp;#39;.&lt;br /&gt;2) On the event lescDhkeyRequest, the public key was being calculated instead of the shared secret key. The correct adapter function call was to computeSharedSecret with the parameter &amp;#39;event&amp;#39; passed into said function.&lt;/p&gt;
&lt;p&gt;See below for full functional code snip-it.&lt;br /&gt;&lt;pre class="ui-code" data-mode="javascript"&gt;Nordic.prototype.pair = function(cb) {
    let secParams;

    secParams = {
        bond: true,
        mitm: false,
        lesc: true,
        keypress: false,
        io_caps: adapter.driver.BLE_GAP_IO_CAPS_NONE,
        oob: false,
        min_key_size: 16,
        max_key_size: 16,
        kdist_own: {
            enc: true,   /** Long Term Key and Master Identification. */
            id: true,    /** Identity Resolving Key and Identity Address Information. */
            sign: false,  /** Connection Signature Resolving Key. */
            link: false,  /** Derive the Link Key from the LTK. */
        },
        kdist_peer: {
            enc: true,   /** Long Term Key and Master Identification. */
            id: true,    /** Identity Resolving Key and Identity Address Information. */
            sign: false,  /** Connection Signature Resolving Key. */
            link: false,  /** Derive the Link Key from the LTK. */
        },
    };

    // Respond to Security parameters request
    adapter.on(&amp;#39;secParamsRequest&amp;#39;, (device, peer_params) =&amp;gt; {
        console.log(&amp;#39;peer_params: &amp;#39; + JSON.stringify(peer_params));

        let secKeyset = {
            keys_own: {
                enc_key: null,
                id_key: null,
                sign_key: null,
                pk: null,
            },
            keys_peer: {
                enc_key: null,
                id_key: null,
                sign_key: null,
                pk: null,
            },
        };

        // Compute our public key here (nothing else is needed)
        secKeyset.keys_own.pk = { pk: adapter.computePublicKey() };
        
        // Send the response
        adapter.replySecParams(
            device.instanceId,
            adapter.driver.BLE_GAP_SEC_STATUS_SUCCESS,
            null, // This can be null IF we are a central AND we initiated the pairing
            secKeyset,
            (err, secKeyset) =&amp;gt; {
                console.log(&amp;#39;secKeyset: &amp;#39; + JSON.stringify(secKeyset));
                if(err) {
                    console.error(&amp;quot;secParamsRequest Err&amp;quot;, err);
                }
            }
        );
    });

    // Respond to the Diffie Hellman key exchange request
    adapter.on(&amp;#39;lescDhkeyRequest&amp;#39;, function(device, event) {
        // Compute a shared secret key
        let key = adapter.computeSharedSecret(event);
        adapter.replyLescDhkey(
            adapter.device.instanceId,
            key,
            function(err) {
                // Compute shared secret on success
                if(err) {
                    console.error(&amp;#39;LescDhkey err: &amp;#39; + err);
                }
                cb();
            }
        );
    });

    // Start authentication
    adapter.authenticate(adapter.device.instanceId, secParams, function(err) {
        console.log(&amp;#39;Auth err: &amp;#39; + err);
    });
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: LESec with nRF52840 and pc-ble-driver-js v2.6.1 sending all 0's for Diffie Hellman public key</title><link>https://devzone.nordicsemi.com/thread/259287?ContentTypeID=1</link><pubDate>Fri, 10 Jul 2020 00:00:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7f0c5d4-6451-40e0-91b9-5af3cdb43c7f</guid><dc:creator>Matthew Stormo</dc:creator><description>&lt;p&gt;Determined&amp;nbsp;public key being passed to the peripheral as all 0&amp;#39;s was caused from the call to&amp;nbsp;replySecParams with a null secParams. Below is the updated event listener for &amp;#39;secParamsRequest&amp;#39;&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="javascript"&gt;// Respond to Security parameters request
adapter.on(&amp;#39;secParamsRequest&amp;#39;, (device, peer_params) =&amp;gt; {
    console.log(&amp;#39;peer_params: &amp;#39; + JSON.stringify(peer_params));

    let secKeyset = {
        keys_own: {
            enc_key: null,
            id_key: null,
            sign_key: null,
            pk: null,
        },
        keys_peer: {
            enc_key: null,
            id_key: null,
            sign_key: null,
            pk: null,
        },
    };
    secKeyset.keys_own.pk = { pk: adapter.computePublicKey() };
    adapter.replySecParams(device.instanceId,
        adapter.driver.BLE_GAP_SEC_STATUS_SUCCESS,
        null,
        secKeyset,
        (err, secKeyset) =&amp;gt; {
            console.log(&amp;#39;secKeyset: &amp;#39; + JSON.stringify(secKeyset));
            console.log(&amp;quot;secParamsRequest Err&amp;quot;, err);
        }
    );
});&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;DH Auth is still failing at this point. Still investigating.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>