<?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>Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/65399/function-to-encrypt-all-data-to-be-sent</link><description>Hi, 
 Based on other discussions of encryption etc , I created a function to encrypt all the data, so that whenever I call the function It encrypts. 
 
 But I am not able to call the function, there&amp;#39;s always an error 
 
 
 
 Can you please help</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 10 Sep 2020 13:13:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/65399/function-to-encrypt-all-data-to-be-sent" /><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/268998?ContentTypeID=1</link><pubDate>Thu, 10 Sep 2020 13:13:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e41c12e4-4b0e-4ad8-ac10-665d09df28d3</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;You welcome, and the best of luck with your project &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/268839?ContentTypeID=1</link><pubDate>Thu, 10 Sep 2020 06:16:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aa4ed7e9-1691-4a6a-8316-203a79633f30</guid><dc:creator>codebreaker176</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Yes I am checking that, Thanks a lot for the help&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/268694?ContentTypeID=1</link><pubDate>Wed, 09 Sep 2020 09:32:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2ea334d7-0745-411a-9487-6a4be6ba452c</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have to agree with Jörg here. If you are new to C development, then making your own encryption algorithm might not be the best place to start....&lt;/p&gt;
&lt;p&gt;The only advantage of cooking your own encryption is &amp;quot;security by obscurity&amp;quot;, which isn&amp;#39;t really security at all.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you investigated the security enhancements added to Bluetooth 4.2, namely the LE Secure Connections (LESC) pairing scheme?&lt;/p&gt;
&lt;p&gt;This was added as a response to the admittedly insecure pairing used in the original Bluetooth 4.0 implementation, and uses an industry standard Diffie Hellman key exchange algorithm to safely distribute encryption keys over a non secure channel.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The paper referred to in Daniel&amp;#39;s blog is discussing the older pairing scheme in 4.0, and does not cover the enhancements in 4.2.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Unless you are able to implement a security scheme better than this then there really isn&amp;#39;t any point adding another (less secure) layer on top.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/268140?ContentTypeID=1</link><pubDate>Sat, 05 Sep 2020 13:26:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9a73a0af-7ddc-4ae8-8421-7420bafa7a17</guid><dc:creator>Turbo J</dc:creator><description>[quote userid="92700" url="~/f/nordic-q-a/65399/function-to-encrypt-all-data-to-be-sent/267881"]By calling these two functions together does it give us the original data back?[/quote]
&lt;p&gt;Nope. Consider where the data output of these functions goes.&lt;/p&gt;
&lt;p&gt;I get a strong feeling that your C programming skills are simply not up to the task. There are too many beginner questions/mistakes.&lt;/p&gt;
&lt;p&gt;Consider working through a good introductory book (or course) - this 40+ year old language is NOT beginner friendly at all.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Note: &lt;em&gt;Proper&lt;/em&gt; crypto is &lt;em&gt;hard&lt;/em&gt; even for advanced programmers. There are waaay too many pitfalls. You are almost always better off using some higher level library - that solves problems you don&amp;#39;t even know about yet...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/268125?ContentTypeID=1</link><pubDate>Fri, 04 Sep 2020 18:49:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5696fb32-c681-4ad4-a882-729283ecb3e3</guid><dc:creator>codebreaker176</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;Torbj&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I checked the code now, there is no change at all in the data , I recieve the same data, it does not get encrypted.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I am using nrf tool box app, And I am trying to send an encrypted data to it.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I followed the instructions as per the below link:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/intro-to-application-level-security-using-the-ecb-"&gt;https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/intro-to-application-level-security-using-the-ecb-&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/268069?ContentTypeID=1</link><pubDate>Fri, 04 Sep 2020 12:34:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:00f3a189-25ba-4051-bd34-9de0e14f2c30</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Have you tried to&amp;nbsp;run the debugger to see exactly what data the different arrays contain when running the XOR operation in ctr_encrypt and ctr_decrypt?&lt;/p&gt;
&lt;p&gt;If the result of&amp;nbsp;the ctr_encrypt operation is XOR&amp;#39;ed with the same chiphertext then you should get the same data back.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If this is not happening I suspect you are doing something wrong in the handling of the different arrays.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/267881?ContentTypeID=1</link><pubDate>Thu, 03 Sep 2020 14:18:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:28eed999-8476-46bb-8458-e2964164b3a9</guid><dc:creator>codebreaker176</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Had not noticed, that was a very fatal error from my side sorry.&lt;/p&gt;
&lt;p&gt;Thank you&amp;nbsp;&lt;span&gt;Torbj&amp;oslash;rn!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;By calling these two functions together does it give us the original data back?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ctr_encrypt(&amp;amp;vib_cnt_buf[0]);
ctr_decrypt(&amp;amp;vib_cnt_buf[0]);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I get a different (probably encrypted) data&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/267768?ContentTypeID=1</link><pubDate>Thu, 03 Sep 2020 08:29:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58f52c37-c5eb-4cb2-ad2b-b31a51a89bf1</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The following line is definitely not correct:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;uint32_t *encrypted_data_ptr = ctr_encrypt(&amp;amp;vib_cnt_buf);&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;First off, when you use the name of an array without an index you are essentially getting the address of the first element in the array.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Put another way, vib_cnt_buf and &amp;amp;vib_cnt_buf[0] is the same thing.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Using&amp;nbsp;&lt;em&gt;&amp;amp;vib_cnt_buf &lt;/em&gt;will not work, as you are trying to get the address of the address of the first element in the array.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Secondly, you are defining encrypted_data_ptr as a pointer, but ctr_encrypt returns a uint32_t, not a pointer to a uint32_t.&lt;/p&gt;
&lt;p&gt;I would suggest starting by reading up on pointers in C either from online tutorials like &lt;a href="https://www.programiz.com/c-programming/c-pointers"&gt;this one&lt;/a&gt;, or from a book such as &amp;quot;The C Programming Language by Kernighan and Ritchie&amp;quot;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/267524?ContentTypeID=1</link><pubDate>Wed, 02 Sep 2020 06:23:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2aea4250-60a4-46c0-b40d-1b57c252e272</guid><dc:creator>codebreaker176</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I am now using ECB mode of AES, there&amp;nbsp;were lot of errors in previous one.&lt;/p&gt;
&lt;p&gt;Now in the below code for ECB mode I have just one error : which I can&amp;#39;t figure out the meaning,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static void nus_data_handler(ble_nus_evt_t * p_evt)
{   
    //some codes over here

    Convert_to_ascii(Battery_voltage,(&amp;amp;vib_cnt_buf[0]),4);
    uint32_t *encrypted_data_ptr = ctr_encrypt(&amp;amp;vib_cnt_buf);
    memcpy(vib_cnt_buf, encrypted_data_ptr, length);
    err_code = ble_nus_data_send(&amp;amp;m_nus, (&amp;amp;vib_cnt_buf), &amp;amp;thi, m_conn_handle);
}
/* 
.
.
.
.   
.
.
.
 */
static uint32_t crypt(uint8_t * buf)
{
    uint8_t  i;
    uint32_t err_code;

    if (!m_initialized)
    {
	    return NRF_ERROR_INVALID_STATE;
    }

    err_code = sd_ecb_block_encrypt(&amp;amp;m_ecb_data);
    if (NRF_SUCCESS != err_code)
    {
	    return err_code;
    }

    for (i=0; i &amp;lt; ECB_KEY_LEN; i++)
    {
	    buf[i] ^= m_ecb_data.ciphertext[i];
    }

    // Increment the counter.
    (*((uint32_t*) m_ecb_data.cleartext))++;

    return NRF_SUCCESS;
}


uint32_t ctr_encrypt(uint8_t * p_clear_text)
{
    return crypt(p_clear_text);
}


uint32_t ctr_decrypt(uint8_t * p_cipher_text)
{
    return crypt(p_cipher_text);
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The following line has error (line 47 in above snippet) :&amp;nbsp;&lt;/p&gt;
&lt;p&gt;uint32_t ctr_encrypt(uint8_t * p_clear_text)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Error : conflicting types for &amp;#39;ctr_encrypt&amp;#39;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/267505?ContentTypeID=1</link><pubDate>Tue, 01 Sep 2020 19:45:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:13523e84-b607-4bc3-a71c-01eefa75b633</guid><dc:creator>codebreaker176</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;There is another error in the above mentioned code at line 9 :&amp;nbsp;memset (&amp;amp;aes_struct, 0, sizeof(aes_struct));&lt;/p&gt;
&lt;p&gt;Error : expected declaration specifiers or &amp;#39;...&amp;#39; before &amp;#39;&amp;amp;&amp;#39; token.&lt;/p&gt;
&lt;p&gt;Thanks for the support&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/267384?ContentTypeID=1</link><pubDate>Tue, 01 Sep 2020 08:23:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cecdf406-4757-47f7-89b7-1c2cc7cc8a98</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Sorry, I didn&amp;#39;t notice that line earlier, it is clearly an illegal operation.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You are taking a pointer to a pointer to the vib_cnt_buf array and trying to assign a pointer to it, this will not work.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Instead I would suggest doing something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;uint8_t *encrypted_data_ptr = encrypt(&amp;amp;vib_cnt_buf);
memcpy(vib_cnt_buf, encrypted_data_ptr, length);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/267282?ContentTypeID=1</link><pubDate>Mon, 31 Aug 2020 14:25:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1c01a4f3-edb3-4cc7-9a3f-81b4cf46ae0f</guid><dc:creator>codebreaker176</dc:creator><description>&lt;p&gt;Hi again,&lt;/p&gt;
&lt;p&gt;Error :&amp;nbsp;lvalue required as left operand of assignment&lt;/p&gt;
&lt;p&gt;this error is at this lilne :&amp;nbsp;&amp;amp;vib_cnt_buf = encrypt(&amp;amp;vib_cnt_buf);&lt;/p&gt;
&lt;p&gt;I can&amp;#39;t seem to get rid of it at all&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;the convert_to_ascii() function works well and converts the same variable to ascii values&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function to encrypt all data to be sent</title><link>https://devzone.nordicsemi.com/thread/267253?ContentTypeID=1</link><pubDate>Mon, 31 Aug 2020 13:05:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b5fb3d0-3fa2-49f9-913e-d30c244dd4dd</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Are you able to provide some more information on the error you are seeing?&lt;/p&gt;
&lt;p&gt;Is it the Convert_to_ascii(..) function that returns the error?&lt;/p&gt;
&lt;p&gt;Are you able to share the implementation of this function with me?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>