<?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>Adding encryption to secure DFU SDK v16.0.0 - Failed decryption</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/69129/adding-encryption-to-secure-dfu-sdk-v16-0-0---failed-decryption</link><description>I&amp;#39;m working on adding encryption to the secure bootloader in SDK v16.0.0. I&amp;#39;ve based my changes largely on the framework and code provided in this thread . I believe I have made the required modifications to nrfutil and the secure bootloader but my DFU</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 08 Dec 2020 23:47:02 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/69129/adding-encryption-to-secure-dfu-sdk-v16-0-0---failed-decryption" /><item><title>RE: Adding encryption to secure DFU SDK v16.0.0 - Failed decryption</title><link>https://devzone.nordicsemi.com/thread/283928?ContentTypeID=1</link><pubDate>Tue, 08 Dec 2020 23:47:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:79a80da0-5b2c-4d1e-8a17-67468f402bab</guid><dc:creator>cbrown_cdi</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/sigurdon"&gt;Sigurd&lt;/a&gt; Thanks for the help. I got it working, I was passing the little-endian versions of the ECB KEY and nonce into the openssl encryption command. Once I got that fixed everything worked as expected.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Adding encryption to secure DFU SDK v16.0.0 - Failed decryption</title><link>https://devzone.nordicsemi.com/thread/283910?ContentTypeID=1</link><pubDate>Tue, 08 Dec 2020 18:46:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:af0fa75c-728c-417b-9074-399d113ef75c</guid><dc:creator>cbrown_cdi</dc:creator><description>&lt;p&gt;Hi Sigurd,&lt;/p&gt;
&lt;p&gt;Absolutely, I understand. I&amp;#39;m posting this here in hopes that this may help others in the future as well.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No, I did not try that. I was hoping to avoid making the changes twice.&lt;/li&gt;
&lt;li&gt;Yes, I added debug prints to those functions in the following locations - &lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void ctr_init(const uint8_t * p_nonce, const uint8_t * p_ecb_key)
{
    m_initialized = true;
    m_index = 0;
    m_counter = 0;

    // Copy the nonce.
    memcpy(&amp;amp;m_ecb_data.cleartext[0], p_nonce, ECB_KEY_LEN);

    /* Reverse the array as ECB expects it in big-endian format */
    for (uint8_t i=0; i&amp;lt;ECB_KEY_LEN; i++)
    {
        // Save the key.
        m_ecb_data.key[i] = p_ecb_key[ECB_KEY_LEN-1-i];
    }

    //TODO remove prints
    NRF_LOG_INFO(&amp;quot;Init Nonce: &amp;quot;);
    NRF_LOG_HEXDUMP_DEBUG(m_ecb_data.cleartext, ECB_KEY_LEN);
    NRF_LOG_INFO(&amp;quot;Init ECB Key: &amp;quot;);
    NRF_LOG_HEXDUMP_DEBUG(m_ecb_data.key, ECB_KEY_LEN);
}&lt;/pre&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;uint32_t nrf_dfu_validation_crypt(uint8_t * buf)
{
    uint32_t err_code;

    if (!m_initialized)
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (m_index == 0)
    {
        //TODO remove prints
        NRF_LOG_INFO(&amp;quot;Working Nonce: &amp;quot;);
        NRF_LOG_HEXDUMP_DEBUG(m_ecb_data.cleartext, ECB_KEY_LEN);
        NRF_LOG_INFO(&amp;quot;Working ECB Key: &amp;quot;);
        NRF_LOG_HEXDUMP_DEBUG(m_ecb_data.key, ECB_KEY_LEN);

        err_code = sd_ecb_block_encrypt(&amp;amp;m_ecb_data);
        if (NRF_SUCCESS != err_code)
        {
            return err_code;
        }
    }

    * buf ^= m_ecb_data.ciphertext[m_index];

    m_index++;

    if (m_index == 16)
    {
        m_index = 0;

        //Increment the counter
        m_counter++;

        m_ecb_data.cleartext[ECB_KEY_LEN-1] = (uint8_t)(m_counter &amp;amp; 0xFF);
        m_ecb_data.cleartext[ECB_KEY_LEN-2] = (uint8_t)((m_counter &amp;gt;&amp;gt; 8) &amp;amp; 0xFF);
        m_ecb_data.cleartext[ECB_KEY_LEN-3] = (uint8_t)((m_counter &amp;gt;&amp;gt; 16) &amp;amp; 0xFF);
        m_ecb_data.cleartext[ECB_KEY_LEN-4] = (uint8_t)((m_counter &amp;gt;&amp;gt; 24) &amp;amp; 0xFF);
    }

    return NRF_SUCCESS;
}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The relevant sections of the log show that the nonce and ECB key are being copied into m_ecb_data and are big-endian. The nonce and key match the nonce in key used when generating the DFU zip. &lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation: PB: Init packet data len: 78
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: Init Nonce: 
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  FF E5 87 F1 2E 3F 7A 1A|.....?z.
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  7B 62 FE 75 00 00 00 00|{b.u....
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: Init ECB Key: 
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  5F 12 D1 50 98 87 12 E3|_..P....
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  18 5B 37 6B AD 0F 33 93|.[7k..3.
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: nrf_dfu_validation_crypt_init()&lt;/pre&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_req_handler: Creating object with size: 4096. Offset: 0x00000000, CRC: 0x00000000
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_req_handler: Request handling complete. Result: 0x1
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_flash: Flash erase success: addr=0x00026000, pending 0
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: Working Nonce: 
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  FF E5 87 F1 2E 3F 7A 1A|.....?z.
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  7B 62 FE 75 00 00 00 05|{b.u....
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: Working ECB Key: 
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  5F 12 D1 50 98 87 12 E3|_..P....
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  18 5B 37 6B AD 0F 33 93|.[7k..3.
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: Working Nonce: 
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  FF E5 87 F1 2E 3F 7A 1A|.....?z.
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  7B 62 FE 75 00 00 00 06|{b.u....
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: Working ECB Key: 
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  5F 12 D1 50 98 87 12 E3|_..P....
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  18 5B 37 6B AD 0F 33 93|.[7k..3.
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: Working Nonce: 
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  FF E5 87 F1 2E 3F 7A 1A|.....?z.
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  7B 62 FE 75 00 00 00 07|{b.u....&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The log shown above for the working nonce/ECB key shows the first calls to &lt;strong&gt;nrf_dfu_validation_crypt. &lt;/strong&gt;I&amp;#39;ve just noticed that the 4 byte counter in the 4 LSBs of the m_ecb_data.cleartext is starting at 5. Should this be starting at 0? &lt;/li&gt;
&lt;li&gt;Yes, I saw that post but &lt;span&gt;ibeckermayer&amp;#39;s issue seems to be different. My DFU failure seems to be caused by a hash verification failure. My suspicion was that this was caused by the decryption issue. &lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_req_handler: Handle NRF_DFU_OP_OBJECT_EXECUTE (data)
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_req_handler: Whole firmware image received. Postvalidating.
00&amp;gt; &amp;lt;info&amp;gt; nrf_dfu_validation: in postvalidate()
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation: Hash verification. start address: 0x26000, size: 0x28B9C
00&amp;gt; &amp;lt;warning&amp;gt; nrf_dfu_validation: Hash verification failed.
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation: Expected FW hash:
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  CE 5F D2 8D 99 8B DE 7A|._.....z
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  B6 8E 67 CA 32 46 E9 5C|..g.2F.\
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  68 87 52 27 48 BF 6A 5A|h.R&amp;#39;H.jZ
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  1D F7 1C 8A 18 B9 F9 91|........
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation: Actual FW hash:
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  86 64 66 BB 8C 6E C7 46|.df..n.F
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  81 38 67 8A C8 E5 CE 23|.8g....#
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  95 16 FB D3 9E 98 1E 35|.......5
00&amp;gt; &amp;lt;debug&amp;gt; nrf_dfu_validation:  9D 93 1B DB 00 55 01 44|.....U.D
00&amp;gt; &amp;lt;warning&amp;gt; nrf_dfu_ble: DFU request 4 failed with error: 0xB&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Adding encryption to secure DFU SDK v16.0.0 - Failed decryption</title><link>https://devzone.nordicsemi.com/thread/283890?ContentTypeID=1</link><pubDate>Tue, 08 Dec 2020 16:14:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9d9c510c-571e-423b-b437-cae1fd915fcd</guid><dc:creator>Sigurd</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;First of all, please note that the code posted in the other thread by devzone user&amp;nbsp;Matthew, is not official code from Nordic Semiconductor. That said, the code has been very useful for several users that wants to add support for DFU encryption.&lt;/p&gt;
&lt;p&gt;I believe the post from&amp;nbsp;&lt;span&gt;Matthew is written for SDK v15.0. There were several changes in the bootloader and&amp;nbsp;nrfutil from SDK v15.0 to SDK v16.0. There was e.g. a new bootloader settings versions&amp;nbsp;introduced in SDK v15.3 because of the changes. See &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fug_nrfutil%2FUG%2Fnrfutil%2Fnrfutil_settings_generate_display.html"&gt;this page&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Some questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Were you able to follow the steps in the guide, and add&amp;nbsp;&lt;span&gt;support for DFU&amp;nbsp;&lt;/span&gt;&lt;span&gt;encryption in SDK v15, before trying to port it to SDK v16 ?&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Did&amp;nbsp;you check that the&amp;nbsp;nrf_dfu_validation_crypt_init() function was able to properly copy/save the ecb_key and nonce to m_ecb_data? And that the same key is actually used in&amp;nbsp;nrf_dfu_validation_crypt() /&amp;nbsp;sd_ecb_block_encrypt() ?&lt;/span&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span&gt;Did you see &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/44392/adding-encryption-to-secure-dfu-sdk-v15"&gt;this post&lt;/a&gt;&amp;nbsp;?&amp;nbsp;Devzone user&amp;nbsp;ibeckermayer tried to port it to SDK v15.2 in that post, he had some issues, but there might be some useful pointers there.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>