<?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>mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/69657/mbedtls-rsa-trouble-mbedtls_rsa_import-mbedtls_rsa_complete-fails</link><description>Hi, 
 Working on RSA encryption, I&amp;#39;m trying to build an RSA context from just the P, Q and E part of the key (as per https://tls.mbed.org/kb/development/how-to-fill-rsa-context-from-n-e-p-and-q ) 
 I have this sequence working on a windows machine, using</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 29 Jan 2021 14:47:21 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/69657/mbedtls-rsa-trouble-mbedtls_rsa_import-mbedtls_rsa_complete-fails" /><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/292024?ContentTypeID=1</link><pubDate>Fri, 29 Jan 2021 14:47:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f22edad3-102d-42c8-9b87-591628c91e02</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;The pull request which should fix your issues has now been published:&lt;a href="https://github.com/nrfconnect/sdk-nrfxlib/pull/390"&gt;https://github.com/nrfconnect/sdk-nrfxlib/pull/390&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t have the opportunity to test it myself today, but it should solve all the RSA problems in the cryptocell library.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/291836?ContentTypeID=1</link><pubDate>Thu, 28 Jan 2021 14:59:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:325b7c00-669a-4382-a436-0b0824d7dcbd</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi, and sorry for the late reply.&lt;/p&gt;
&lt;p&gt;Since my last comment, the developers has found even more problems with the library.&lt;/p&gt;
&lt;p&gt;It seems like you at least also have to provide E, although that is unpractical for the application.&lt;/p&gt;
&lt;p&gt;However, I have been promised that a Pull Request with an updated version of the library will be available within the next few days.&lt;/p&gt;
&lt;p&gt;Sorry for the inconvenience, and the time this is taking.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Didrik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/290945?ContentTypeID=1</link><pubDate>Mon, 25 Jan 2021 08:46:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d887e9c5-82cb-4af8-a8b7-c0103c08b1b6</guid><dc:creator>basvkesteren</dc:creator><description>&lt;p&gt;Hi Didrik,&lt;/p&gt;
&lt;p&gt;Thanks for the response. So, do I understand it right that this missing functionality will be added to a future NCS release? Looks to me the crypto-library is binary only, I can&amp;#39;t modify it myself, right?&lt;/p&gt;
&lt;p&gt;Also, I did a quick test this morning. In the following code the test_RSA_N/P/Q/E contain a valid RSA key. This sequence works with CONFIG_CC3XX_BACKEND=n, but fails with CONFIG_CC3XX_BACKEND=y (I get a &amp;#39;mbedtls_rsa_complete failed: -16512&amp;#39; printout).&lt;/p&gt;
&lt;p&gt;What am I missing? I&amp;#39;m using NCS 1.4.0&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    LOG_DBG(&amp;quot;RSA import, again:&amp;quot;);
    mbedtls_rsa_init(&amp;amp;rsa, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
    if((r = mbedtls_rsa_import_raw(&amp;amp;rsa, test_RSA_N, sizeof(test_RSA_N),
                                         test_RSA_P, sizeof(test_RSA_P), 
                                         test_RSA_Q, sizeof(test_RSA_Q),
                                         NULL, 0, /* D */
                                         test_RSA_E, sizeof(test_RSA_E)))) {
        mbedtls_strerror(r, errbuf, sizeof(errbuf));
        LOG_DBG(&amp;quot;mbedtls_rsa_import_raw failed: %d (%s)&amp;quot;, r, errbuf);
    }
    LOG_DBG(&amp;quot;ok&amp;quot;);

    LOG_DBG(&amp;quot;mbedtls_rsa_complete&amp;quot;);
    if((r = mbedtls_rsa_complete(&amp;amp;rsa))) {
        mbedtls_strerror(r, errbuf, sizeof(errbuf));
        LOG_DBG(&amp;quot;mbedtls_rsa_complete failed: %d (%s)\n&amp;quot;, r, errbuf);
    }
    LOG_DBG(&amp;quot;ok&amp;quot;);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/290808?ContentTypeID=1</link><pubDate>Fri, 22 Jan 2021 15:50:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2dfdfe99-c9fc-4b62-9f4f-0e89b7a694eb</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi, and sorry for the slow response.&lt;/p&gt;
&lt;p&gt;The reason for why it fails when using the cryptocell is because the library doesn&amp;#39;t calculate N based on P and Q.&lt;/p&gt;
&lt;p&gt;However, if you calculate N and provide it to the library, it will work.&lt;/p&gt;
&lt;p&gt;We will also add this missing detail in future versions of the crypto libraries.&lt;/p&gt;
&lt;p&gt;This is the code snippet that should have been in the crypto library:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;   if( !have_N &amp;amp;&amp;amp; have_P &amp;amp;&amp;amp; have_Q )
    {
        if( ( ret = mbedtls_mpi_mul_mpi( &amp;amp;ctx-&amp;gt;N, &amp;amp;ctx-&amp;gt;P,
                                         &amp;amp;ctx-&amp;gt;Q ) ) != 0 )
        {
            return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret );
        }

        ctx-&amp;gt;len = mbedtls_mpi_size( &amp;amp;ctx-&amp;gt;N );
    }&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/289002?ContentTypeID=1</link><pubDate>Wed, 13 Jan 2021 16:37:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec71f421-fa70-49d0-b935-8d06f9216173</guid><dc:creator>basvkesteren</dc:creator><description>&lt;p&gt;Hi Didrik,&lt;/p&gt;
&lt;p&gt;Thanks for the reply! About that upload, dunno how I&amp;#39;ve missed that button... A well.&lt;/p&gt;
&lt;p&gt;But, any word from the developers?&lt;/p&gt;
&lt;p&gt;Since I&amp;#39;ve uploaded my initial testcode I&amp;#39;ve been tweaking and fixing the mbedtls code; it&amp;#39;s working stable now, but rather slow (especially the RSA calls). Would like to fix that &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: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/287781?ContentTypeID=1</link><pubDate>Wed, 06 Jan 2021 16:08:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5b7230b1-175f-424a-aa94-d95736955a68</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi, and happy new year to you too.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="basvkesteren"](think i don&amp;#39;t have the option of uploading stuff to the ticket, like you did?)[/quote]
&lt;p&gt;&amp;nbsp;You should be able to, although not every file type is allowed, so e.g., your prj.conf must either be renamed to something else than *.conf, or it must be in a zipped folder.&lt;/p&gt;
&lt;p&gt;You can find the option to upload files here:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1609944265562v1.png" alt=" " /&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1609944274254v2.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve looked at your code, and you are correct that it doesn&amp;#39;t use the cryptocell for the RSA encryption.&lt;/p&gt;
&lt;p&gt;I believe the cryptocell is still used to get random numbers, but it is definitely used if you change your f_rng() to this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;drivers/entropy.h&amp;gt;

static int f_rng(void *ctx, unsigned char *buf, size_t len)   
    const struct device *dev = device_get_binding(DT_CHOSEN_ZEPHYR_ENTROPY_LABEL);
    
    static int first = 1;
    if (first) {
        printk(&amp;quot;Name of entropy device: %s\n&amp;quot;, dev-&amp;gt;name);
        first = 0;
    }
    
	if (!dev) {
		printk(&amp;quot;error: no entropy device\n&amp;quot;);
		return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
	}
	int err = entropy_get_entropy(dev, buf, len);
	if (err) {
		printk(&amp;quot;Failed to get entropy\n&amp;quot;);
		return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
	}

	return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I would also change your call to mbedtls_rsa_private to use mbedtls_ctr_drbg_random instead of f_rng.&lt;/p&gt;
&lt;p&gt;Unfortunately, I have not yet been able to get the cryptocell to perform the actual RSA encryption (though it should be possible). I will ask our developers if they can see what we are doing wrong.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/287288?ContentTypeID=1</link><pubDate>Mon, 04 Jan 2021 14:34:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aa7dac67-3183-495c-90f2-13f819e39ec5</guid><dc:creator>basvkesteren</dc:creator><description>&lt;p&gt;Hi Didrik and colleagues,&lt;/p&gt;
&lt;p&gt;Best wishes for the new year &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;p&gt;Just making sure my ticket won&amp;#39;t be forgotten over the holidays.. ;)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/286505?ContentTypeID=1</link><pubDate>Wed, 23 Dec 2020 16:45:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:db5054ff-f060-4231-a1d3-d842ea30033f</guid><dc:creator>basvkesteren</dc:creator><description>&lt;p&gt;Hi Didrik,&lt;/p&gt;
&lt;p&gt;Spend the day doing some testing. When I do the same entropy-trick (using the local f_rng function instead of mbedtls_entropy_func) stuff starts to work.&lt;/p&gt;
&lt;p&gt;BUT, if I&amp;#39;m correct, I&amp;#39;m not using the cryptocell hardware. What do I need to do to get that working?&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve created a test-project, just doing the encryption, nothing else. I need AES-128 and RSA-1024. To get AES working, I need CONFIG_OBERON_BACKEND=y, otherwise the output is all zero&amp;#39;s.&lt;/p&gt;
&lt;p&gt;For RSA, i need to set CONFIG_CC3XX_BACKEND=n, or mbedtls_rsa_complete() fails with an input error.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;If you could take a look at my code, i&amp;#39;ve uploaded it &lt;a href="https://edeation.nl/dl/nordic/app.c"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;and the included testdata &lt;a href="https://edeation.nl/dl/nordic/testdata.h"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;prj.conf is &lt;a href="https://edeation.nl/dl/nordic/prj.conf"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(think i don&amp;#39;t have the option of uploading stuff to the ticket, like you did?)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/286412?ContentTypeID=1</link><pubDate>Wed, 23 Dec 2020 08:38:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5011381d-347e-4afc-87e6-ba52f5724951</guid><dc:creator>basvkesteren</dc:creator><description>&lt;p&gt;Hi Didrik,&lt;/p&gt;
&lt;p&gt;Thanks for the example, I&amp;#39;ll start working on that with my own code right now!&lt;/p&gt;
&lt;p&gt;One more question right of, though; you&amp;#39;re not using the CC3XX backend (CONFIG_CC3XX_BACKEND=n), so there&amp;#39;s no hardware-acceleration for the crypto stuff, right? Is it not possible to use the CC3XX backend on the 52840?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/286118?ContentTypeID=1</link><pubDate>Mon, 21 Dec 2020 16:11:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b52e05a1-cddc-49a1-9c9c-645fe9a50c96</guid><dc:creator>Didrik Rokhaug</dc:creator><description>[quote user="basvkesteren"]Because I don&amp;#39;t really have a clear picture about how and when the whole nrfxlib crypto stuff comes into play, the documentation (&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfxlib/crypto/README.html"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfxlib/crypto/README.html&lt;/a&gt;) is sort of useless; a changelog and an api-list really doesn&amp;#39;t do...[/quote]
&lt;p&gt;&amp;nbsp;It is not meant that you should use the crypto libraries directly, instead, you should use the Nordic Security Module: &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfxlib/nrf_security/README.html"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfxlib/nrf_security/README.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have also made a very small sample that seems to be working correctly, though I had to use Zephyr&amp;#39;s sys_csrand_get() instead of mbedTLS&amp;#39;s hardware_poll():&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/mbedtls_5F00_rsa_5F00_import.zip"&gt;devzone.nordicsemi.com/.../mbedtls_5F00_rsa_5F00_import.zip&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/285892?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 15:59:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:293e27f6-2f3c-46ea-a85a-be243e4ead4d</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Unfortunately, I am not able to look at your ticket today, but I will try to get an answer to you in the beginning of next week.&lt;/p&gt;
&lt;p&gt;Sorry for the inconvenience.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Didrik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/285785?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 10:20:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5af1440-f0da-4671-9c0b-6d731bec3b7e</guid><dc:creator>basvkesteren</dc:creator><description>&lt;p&gt;Replying to myself again&amp;nbsp;&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;p&gt;When I disable the CC3XX backend, I get a little bit further. I now have these config&amp;#39;s:&lt;/p&gt;
&lt;p&gt;CONFIG_NORDIC_SECURITY_BACKEND=y&lt;br /&gt;CONFIG_CC3XX_BACKEND=n&lt;br /&gt;CONFIG_OBERON_BACKEND=y&lt;br /&gt;CONFIG_MBEDTLS_VANILLA_BACKEND=n&lt;/p&gt;
&lt;p&gt;And now the mbedtls_rsa_import/mbedtls_rsa_complete calls work as expected. However, doing an actual RSA encryption fails with a fault in mbedtls_hardware_poll.&lt;/p&gt;
&lt;p&gt;If I then disable the CC3XX entropy thingy (CONFIG_ENTROPY_CC3XX=n) it no longer crashes, but a call to mbedtls_ctr_drbg_seed()&amp;nbsp; returns&amp;nbsp;&lt;span style="color:#000000;"&gt;-52 (CTR_DRBG - The entropy source failed), which probably makes sense; it doesn&amp;#39;t have an entropy source?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#000000;"&gt;(Edit: found out how to select the entropy-source; setting &amp;#39;zephyr,entropy = &amp;amp;rng;&amp;#39; in my dts makes it use the RNG &lt;span&gt;peripheral&lt;/span&gt;, but then the mbedtls_ctr_drbg_seed() call ends in a recursive spinlock assert..) &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="color:#000000;"&gt;So, all this leads me to believe that I&amp;#39;m not using the CC3XX stuff properly, or it&amp;#39;s not working properly. Is there any useful documentation, or an example perhaps, on how to configure, initialize and use this stuff for my platform?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: mbedtls RSA trouble, mbedtls_rsa_import/mbedtls_rsa_complete fails</title><link>https://devzone.nordicsemi.com/thread/285755?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 07:53:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:749c7906-389d-4f71-b0b7-748060d46ad5</guid><dc:creator>basvkesteren</dc:creator><description>&lt;p&gt;Or, thinking a bit about this, maybe I&amp;#39;m using the wrong API or configuration? Because I don&amp;#39;t really have a clear picture about how and when the whole nrfxlib crypto stuff comes into play, the documentation (&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfxlib/crypto/README.html"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrfxlib/crypto/README.html&lt;/a&gt;) is sort of useless; a changelog and an api-list really doesn&amp;#39;t do...&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>