<?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 - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/106325/mbedtls---using-keys-handled-by-psa-crypto</link><description>Hello, 
 
 I need to implement the code that calculates the ECDSA signature, using the mbedtls library. Development environment: 
 
 nRF53DK 
 nRF Connect SDK 2.3.0 
 
 Currently, the process is implemented as follows: 
 
 The private key is generated</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 09 Feb 2024 14:10:16 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/106325/mbedtls---using-keys-handled-by-psa-crypto" /><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/468235?ContentTypeID=1</link><pubDate>Fri, 09 Feb 2024 14:10:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5da2e95-a0f4-4836-9067-85c9d9f442a2</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Hello Sigurd,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for those details.&lt;/p&gt;
&lt;p&gt;Regards.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/467197?ContentTypeID=1</link><pubDate>Thu, 01 Feb 2024 14:32:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:711a4fde-70ad-4112-9b6d-e572f2eddc81</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;They say:&lt;/p&gt;
&lt;p&gt;&amp;quot;&lt;span&gt;&lt;span dir="ltr"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Hey. It was commented out as it is not needed in TF-M build. Previously there was a false dependency on TLS/DTLS and X.509 libraries inside TF-M. But when we started optimizing we finally removed that dependency (TF-M just tried to consume Mbed TLS as a whole thing, while they really only needed the crypto toolbox functionality)&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The easiest thing to do is to just add it into the template files, but I would guard it with #if !defined(MBEDTLS_PSA_CRYPTO_SPM) to ensure that it isn&amp;#39;t enabled inside the TF-M image.&lt;/p&gt;
&lt;p&gt;Likely the user-configurable header file was easier to get to work when we were using the builtin Mbed TLS. We have gravitated away from that with PSA core from Oberon. We have no way of converting header file into CMake, so we won&amp;#39;t pull in the right files in the compilation... (or set up the right linking rules) We need to remove this chapter from the documentation (of which we appologize).&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;quot;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/467192?ContentTypeID=1</link><pubDate>Thu, 01 Feb 2024 14:18:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d2845d74-88e7-4a77-a0ac-a4c26633ed75</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;I see that the X509 options is commented out for the PSA crypto config: &lt;span&gt;&lt;span dir="ltr"&gt;&lt;a title="https://github.com/nrfconnect/sdk-nrf/blob/main/subsys/nrf_security/cmake/psa_crypto_config.cmake#l374-l382" href="https://github.com/nrfconnect/sdk-nrf/blob/main/subsys/nrf_security/cmake/psa_crypto_config.cmake#L374-L382" rel="noopener noreferrer" target="_blank"&gt;nrf/subsys/nrf_security/cmake/psa_crypto_config.cmake&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;I will ask our crypto developers what the reason for this is.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/466943?ContentTypeID=1</link><pubDate>Wed, 31 Jan 2024 13:11:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8f97a497-4155-42bb-afb3-080ced75ee0c</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Hello Sigurd,&lt;/p&gt;
&lt;p&gt;Is there a way to enable &lt;span&gt;MBEDTLS_X509_CREATE_C and MBEDTS_X509_CSR_WRITE_C&amp;nbsp;by&amp;nbsp;&lt;/span&gt;avoiding changing the file from SDK?&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;It would be good practice to distribute all the specifics of my project through the project itself. &lt;br /&gt;Otherwise, it will be necessary for me to require anyone who wants to build the project to change the SDK on his side as an additional requirement.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;It looks like it should be achievable&amp;nbsp;through from&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.5.1/nrf/libraries/nrf_security/doc/mbed_tls_header.html"&gt;User-provided Mbed TLS configuration header&lt;/a&gt;.&lt;br /&gt;However, following such a guide I got an error:&lt;br /&gt;&lt;em&gt;custom_name_nrf_config.h: No such file or directory&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;even though the path to the file is added to&amp;nbsp;target_include_directories.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure whether this can be applied to the NCS v2.3.0?&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Darko&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/461841?ContentTypeID=1</link><pubDate>Wed, 27 Dec 2023 14:18:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c7313e2e-c9fb-45c9-91f1-03a549d2adb8</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Hello Sigurd,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Just to confirm I&amp;#39;m now able to generate CSR using a key loaded through&amp;nbsp;mbedtls_pk_setup_opaque function.&lt;/p&gt;
&lt;p&gt;Thank you for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/461379?ContentTypeID=1</link><pubDate>Thu, 21 Dec 2023 08:49:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:78a46781-ef8c-4fda-ac44-a9ce66bcd042</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Hello Sigurd,&lt;/p&gt;
&lt;p&gt;Thank you for that information.&amp;nbsp;I was not aware of the fact that it is necessary to adjust the file nrf-config.h in the sdk.&amp;nbsp;&lt;br /&gt;I&amp;#39;m trying currently to solve the issue on my side by using the details you mentioned above.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/461116?ContentTypeID=1</link><pubDate>Tue, 19 Dec 2023 15:10:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:80e7b80d-7eb6-4109-8294-d548bdfe87a9</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;Here are what I got:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;There are multiple issues here, one of them being the lack of ability to convert CSR related configs in 2.3&amp;nbsp;Ask them to remove MBEDTLS_LEGACY_CRYPTO_C in their prj.conf and add MBEDTLS_X509_CREATE_C and MBEDTS_X509_CSR_WRITE_C here:&amp;nbsp;&lt;a title="https://github.com/nrfconnect/sdk-nrfxlib/blob/6d0f58448fae164cfa4d28c494d6bddf5d0d0224/nrf_security/configs/nrf-config.h#l396" href="https://github.com/nrfconnect/sdk-nrfxlib/blob/6d0f58448fae164cfa4d28c494d6bddf5d0d0224/nrf_security/configs/nrf-config.h#L396" rel="noopener noreferrer" target="_blank"&gt;https://github.com/nrfconnect/sdk-nrfxlib/blob/6d0f58448fae164cfa4d28c494d6bddf5d0d0224/nrf_security/configs/nrf-config.h#L396&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1702998527457v1.png" alt=" " /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;This file gets copied during build when the system is using PSA crypto APIs&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;Additionally they are using legacy crypto APIs for RNG for the CSR. I&amp;#39;ve removed quite a bit (entropy and ctr_drbg).&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;And here is an example for some pointers:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1702998601367v2.png" alt=" " /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span dir="ltr"&gt;I hope this can be useful for you to dig into this issue.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/460393?ContentTypeID=1</link><pubDate>Thu, 14 Dec 2023 14:36:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:16d176fb-46ab-4c22-94b5-f56ec4c336ad</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Status update: I have not been able to get any further on this yet, so I asked our crypto developers for clues.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/460281?ContentTypeID=1</link><pubDate>Thu, 14 Dec 2023 07:19:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d31f03b1-a9f8-4d86-b708-80e901bda961</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Meanwhile,&amp;nbsp;&lt;span&gt;I got firewall permission to upload&amp;nbsp;the whole test project here:&lt;/span&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/persistent_5F00_key_5F00_usage_5F00_csr.zip"&gt;devzone.nordicsemi.com/.../persistent_5F00_key_5F00_usage_5F00_csr.zip&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/460128?ContentTypeID=1</link><pubDate>Wed, 13 Dec 2023 11:59:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5805fdf8-f016-4f0a-9e0c-31486e16ef77</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;For such a phase of testing, I just edited nrf-config-user.h generated on build with&amp;nbsp;nrf-config.h (located on build\modules\nrfxlib\nrfxlib\nrf_security\src\include\).&lt;br /&gt;In the build configuration, I used the nrf5340dk_nrf5340_cpuapp board.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/460123?ContentTypeID=1</link><pubDate>Wed, 13 Dec 2023 11:38:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7560d5ac-8f1f-4c64-bedd-ee6b892f9702</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Sure, I can reproduce as you explain.&lt;br /&gt;To do so, I need to know where &lt;span&gt;&lt;em&gt;nrf-config-user.h&lt;/em&gt;&lt;/span&gt; is located, and which baord you built for (with TF-M or not).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/459988?ContentTypeID=1</link><pubDate>Tue, 12 Dec 2023 16:37:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cdffc729-2043-42b5-821c-b902c0743991</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Hello Sigurd,&lt;/p&gt;
&lt;p&gt;I completely agree it would be best to package the whole example and upload it.&lt;br /&gt;Unfortunately, our firewall does not allow us to upload such content here (regardless of whether is related to the demonstration of common/basic functionalities from sdk).&lt;/p&gt;
&lt;p&gt;I was hoping you could easily reproduce the problem by simply updating the &lt;em&gt;main.c&lt;/em&gt; and .&lt;em&gt;prj.conf&lt;/em&gt; files from the nrf_230\v2.3.0\nrf\samples\crypto\persistent_key_usage\ demo. These are the only changes I made to the &lt;em&gt;persistent_key_usage&lt;/em&gt; demo in order to reproduce the problems from the project I&amp;#39;m working on.&lt;/p&gt;
&lt;p&gt;In any case, thank you for your help, I will consult with my colleagues here on how to proceed with this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/459933?ContentTypeID=1</link><pubDate>Tue, 12 Dec 2023 13:21:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:87be5d48-52f1-4c61-a03a-081827fc4b24</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>[quote user="Darko Jolic"]&lt;em&gt;nrf-config-user.h&lt;/em&gt;:[/quote]
&lt;p&gt;Can you explain where this file is located and how you include it in the project?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user="Darko Jolic"]&lt;p&gt;&lt;span&gt;Currently, there is the following problem with the &lt;em&gt;mbedtls_x509write_csr_der&lt;/em&gt; function, that I haven&amp;#39;t solved yet:&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Such function returns the value &lt;em&gt;PSA_ERROR_NOT_SUPPORTED &lt;/em&gt;(from the inside call &lt;em&gt;psa_driver_wrapper_export_public_key&lt;/em&gt;). &lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Can you help me find what is missing in the configuration?&lt;/p&gt;[/quote]
&lt;p&gt;Maybe try to set &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/kconfig/index.html#CONFIG_MBEDTLS_KEY_EXCHANGE_ALL_ENABLED"&gt;CONFIG_MBEDTLS_KEY_EXCHANGE_ALL_ENABLED&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Also, have a look at your build log. Sometimes CONFIGs will be ignored from the prj.conf, which appears as warnings in the log. Maybe some of the configurations you thought you had set are ignored?&lt;/p&gt;
&lt;p&gt;I can copy the code you posted, and I did, but it is hard to know if I missed any changes. Could you zip the test you have and upload it? Then I can test the exact same project as you run and see if I can find anything.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/459562?ContentTypeID=1</link><pubDate>Fri, 08 Dec 2023 18:15:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4ad41508-b19e-4009-a1f2-04d97af7d901</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I upgraded the existing &lt;em&gt;persistent_key_usage&lt;/em&gt;&amp;nbsp;example with mbedtls functionality, in order to additionally describe the problem.&lt;/p&gt;
&lt;p&gt;In the code below, I tried to generate a CSR using a key generated with the &amp;quot;PSA_KEY_LIFETIME_PERSISTENT&amp;quot; attribute (in the posts above I mentioned the ECDSA signature, but for the simplicity of this example I chose to present the problem through generating a CSR).&lt;/p&gt;
&lt;p&gt;Here is the original code from the demo with added&amp;nbsp;&lt;em&gt;generate_csr&lt;/em&gt; function:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2021 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;psa/crypto.h&amp;gt;
#include &amp;lt;psa/crypto_extra.h&amp;gt;
#include &amp;lt;pk.h&amp;gt;
#include &amp;lt;mbedtls/x509_csr.h&amp;gt;
#include &amp;lt;mbedtls/ctr_drbg.h&amp;gt;
#include &amp;lt;mbedtls/entropy.h&amp;gt;

#ifdef CONFIG_BUILD_WITH_TFM
#include &amp;lt;tfm_ns_interface.h&amp;gt;
#endif

#define APP_SUCCESS		(0)
#define APP_ERROR		(-1)
#define APP_SUCCESS_MESSAGE &amp;quot;Example finished successfully!&amp;quot;
#define APP_ERROR_MESSAGE &amp;quot;Example exited with error!&amp;quot;

#define PRINT_HEX(p_label, p_text, len)\
	({\
		LOG_INF(&amp;quot;---- %s (len: %u): ----&amp;quot;, p_label, len);\
		LOG_HEXDUMP_INF(p_text, len, &amp;quot;Content:&amp;quot;);\
		LOG_INF(&amp;quot;---- %s end  ----&amp;quot;, p_label);\
	})

LOG_MODULE_REGISTER(persistent_key_usage, LOG_LEVEL_DBG);

#define SEC_LIB_CSR_SUBJECT_NAME        &amp;quot;C=US,O=Test,OU=Test,CN=Test&amp;quot;
uint8_t m_csr_buffer[4096];
static 	mbedtls_pk_context m_private_key_context;
static 	psa_key_handle_t key_handle;

/* ====================================================================== */
/*			Global variables/defines for the persistent key  example	  */

/* The key id for the persistent key. The macros PSA_KEY_ID_USER_MIN and
 * PSA_KEY_ID_USER_MAX define the range of freely available key ids.
 */
#define SAMPLE_PERS_KEY_ID PSA_KEY_ID_USER_MIN



/* ====================================================================== */

int crypto_init(void)
{
	psa_status_t status;

	/* Initialize PSA Crypto */
	status = psa_crypto_init();
	if (status != PSA_SUCCESS)
		return APP_ERROR;

	return APP_SUCCESS;
}

int crypto_finish(void)
{
	psa_status_t status;
	
	/* Destroy the key handle */
	status = psa_destroy_key(key_handle);
	if (status != PSA_SUCCESS) {
		LOG_INF(&amp;quot;psa_destroy_key failed! (Error: %d)&amp;quot;, status);
		return APP_ERROR;
	}

	return APP_SUCCESS;
}

int generate_prersistent_key(void)
{
	psa_status_t status;

	LOG_INF(&amp;quot;Generating random persistent AES key...&amp;quot;);

	/* Configure the key attributes */
	psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;

	psa_set_key_usage_flags(&amp;amp;key_attributes, PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_DERIVE);
    psa_set_key_lifetime(&amp;amp;key_attributes, PSA_KEY_LIFETIME_VOLATILE);
    psa_set_key_algorithm(&amp;amp;key_attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256));
    psa_set_key_type(&amp;amp;key_attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1));
    psa_set_key_bits(&amp;amp;key_attributes, 256);

    /* Persistent key specific settings */
	psa_set_key_lifetime(&amp;amp;key_attributes, PSA_KEY_LIFETIME_PERSISTENT);
	psa_set_key_id(&amp;amp;key_attributes, SAMPLE_PERS_KEY_ID);

	// Try to destroy key (in case it previously created).
	psa_destroy_key(SAMPLE_PERS_KEY_ID);

	/* Generate a random AES key with persistent lifetime. The key can be used for
	 * encryption/decryption using the key_handle.
	 */
	status = psa_generate_key(&amp;amp;key_attributes, &amp;amp;key_handle);
	if (status != PSA_SUCCESS) {
		LOG_INF(&amp;quot;psa_generate_key failed! (Error: %d)&amp;quot;, status);
		return APP_ERROR;
	}
	
	/* After the key handle is acquired the attributes are not needed */
	psa_reset_key_attributes(&amp;amp;key_attributes);

	LOG_INF(&amp;quot;Persistent key generated successfully!&amp;quot;);

	return APP_SUCCESS;
}

int generate_csr(void)
{
	int status;
	int16_t lenght_of_csr;
	
	mbedtls_x509write_csr csr_ctx;
	mbedtls_entropy_context     m_entropy_ctx;
	mbedtls_ctr_drbg_context    m_ctr_drbg_ctx;

	// Initialize mbedtls_pk_context and &amp;quot;load&amp;quot; previously created key into it.
	mbedtls_pk_init(&amp;amp;m_private_key_context); 
	mbedtls_pk_setup_opaque(&amp;amp;m_private_key_context, key_handle);

	// Initialize drbg context.
	mbedtls_entropy_init(&amp;amp;m_entropy_ctx);
    mbedtls_ctr_drbg_init(&amp;amp;m_ctr_drbg_ctx);
    status = mbedtls_ctr_drbg_seed(&amp;amp;m_ctr_drbg_ctx, mbedtls_entropy_func, &amp;amp;m_entropy_ctx, NULL, 0);
    if(status != 0)
    {
        return APP_ERROR;
    }

	// Prepare CSR context.
	mbedtls_x509write_csr_init(&amp;amp;csr_ctx);
    mbedtls_x509write_csr_set_key_usage(&amp;amp;csr_ctx, MBEDTLS_X509_KU_DIGITAL_SIGNATURE);
    mbedtls_x509write_csr_set_subject_name(&amp;amp;csr_ctx, SEC_LIB_CSR_SUBJECT_NAME);
    mbedtls_x509write_csr_set_md_alg(&amp;amp;csr_ctx, MBEDTLS_MD_SHA256);
    mbedtls_x509write_csr_set_key(&amp;amp;csr_ctx, &amp;amp;m_private_key_context);

	// Create CSR.
    lenght_of_csr = mbedtls_x509write_csr_der(&amp;amp;csr_ctx, m_csr_buffer, sizeof(m_csr_buffer), mbedtls_ctr_drbg_random, &amp;amp;m_ctr_drbg_ctx);
	if(lenght_of_csr &amp;lt; 0)
	{
		return APP_ERROR;
	}

	return APP_SUCCESS;
}

int main(void)
{
	int status;

	LOG_INF(&amp;quot;Starting persistent key example...&amp;quot;);

	status = crypto_init();
	if (status != APP_SUCCESS) {
		LOG_INF(APP_ERROR_MESSAGE);
		return APP_ERROR;
	}

	status = generate_prersistent_key();
	if (status != APP_SUCCESS) {
		LOG_INF(APP_ERROR_MESSAGE);
		return APP_ERROR;
	}

	status = generate_csr();
	if (status != APP_SUCCESS) {
		LOG_INF(APP_ERROR_MESSAGE);
		return APP_ERROR;
	}

	status = crypto_finish();
	if (status != APP_SUCCESS) {
		LOG_INF(APP_ERROR_MESSAGE);
		return APP_ERROR;
	}

	LOG_INF(APP_SUCCESS_MESSAGE);

	return APP_SUCCESS;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;# The Zephyr CMSIS emulation assumes that ticks are ms, currently
CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000

CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=4096

# Enable loging using RTT and UART
CONFIG_CONSOLE=y
CONFIG_LOG=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_BACKEND_UART=y
CONFIG_LOG_BUFFER_SIZE=15360
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=15360

# Enable nordic security backend and PSA APIs
CONFIG_NRF_SECURITY=y
CONFIG_MBEDTLS_PSA_CRYPTO_C=y

# Enable persistent storage APIs
CONFIG_MBEDTLS_PSA_CRYPTO_STORAGE_C=y
CONFIG_PSA_NATIVE_ITS=y

CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=8192
CONFIG_PSA_CRYPTO_DRIVER_OBERON=n
CONFIG_PSA_CRYPTO_DRIVER_CC3XX=y


CONFIG_NRF_SECURITY=y

CONFIG_MBEDTLS_LIBRARY_NRF_SECURITY=y
CONFIG_MBEDTLS_USE_PSA_CRYPTO=y
CONFIG_MBEDTLS_PSA_CRYPTO_C=y
CONFIG_MBEDTLS_PSA_CRYPTO_STORAGE_C=y

CONFIG_MBEDTLS_PK_C=y
CONFIG_MBEDTLS_RSA_C=y
CONFIG_MBEDTLS_PKCS1_V15=y
CONFIG_MBEDTLS_SHA256_C=y
CONFIG_MBEDTLS_ECP_C=y
CONFIG_MBEDTLS_ECDSA_C=y
CONFIG_MBEDTLS_ECDH_C=y
CONFIG_MBEDTLS_CTR_DRBG_C=y
CONFIG_MBEDTLS_ENTROPY_C=y
CONFIG_MBEDTLS_PK_PARSE_C=y

CONFIG_MBEDTLS_TLS_LIBRARY=y
CONFIG_MBEDTLS_X509_LIBRARY=y
CONFIG_MBEDTLS_PK_WRITE_C=y
CONFIG_MBEDTLS_X509_CREATE_C=y
CONFIG_MBEDTLS_X509_CSR_WRITE_C=y
CONFIG_MBEDTLS_LEGACY_CRYPTO_C=y
CONFIG_MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG=n&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;With the configuration from the prj file mentioned in the first post I got the following errors:&lt;br /&gt;- &amp;quot;&lt;em&gt;undefined reference to `mbedtls_pk_setup_opaque&lt;/em&gt;&amp;quot; - MBEDTLS_USE_PSA_CRYPTO was not defined in pk.h, although it was enabled with CONFIG_MBEDTLS_USE_PSA_CRYPTO=y.&lt;br /&gt;- The key generation (&lt;em&gt;psa_generate_key&lt;/em&gt;) failing because in &lt;em&gt;psa_validate_key_persistence&lt;/em&gt; MBEDTLS_PSA_CRYPTO_STORAGE_C was not defined (also enabled with the&amp;nbsp;&lt;span&gt;CONFIG_MBEDTLS_PSA_CRYPTO_STORAGE_C&lt;/span&gt;&lt;span&gt;=y)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I solved both problems only when I included in &lt;em&gt;nrf-config-user.h&lt;/em&gt;:&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define MBEDTLS_PSA_CRYPTO_STORAGE_C 1
#define MBEDTLS_USE_PSA_CRYPTO 1&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote userid="106736" url="~/f/nordic-q-a/106325/mbedtls---using-keys-handled-by-psa-crypto/459011"]If I build the nrf/samples/crypto/ecdsa sample with your above configs except for EXTERNAL_RNG (it makes the build fail), I get MBEDTLS_USE_PSA_CRYPTO=y[/quote]
&lt;p&gt;&lt;span&gt;If I understood you correctly, you didn&amp;#39;t have to additionally include MBEDTLS_USE_PSA_CRYPTO through the nrf-config-user.h file?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Currently, there is the following problem with the &lt;em&gt;mbedtls_x509write_csr_der&lt;/em&gt; function, that I haven&amp;#39;t solved yet:&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Such function returns the value &lt;em&gt;PSA_ERROR_NOT_SUPPORTED &lt;/em&gt;(from the inside call &lt;em&gt;psa_driver_wrapper_export_public_key&lt;/em&gt;). &lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Can you help me find what is missing in the configuration?&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/459044?ContentTypeID=1</link><pubDate>Tue, 05 Dec 2023 15:47:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bdcb0eb1-43b3-4ccb-b137-e39758c364f0</guid><dc:creator>Darko</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Thanks for answering.&lt;/p&gt;
[quote userid="106736" url="~/f/nordic-q-a/106325/mbedtls---using-keys-handled-by-psa-crypto/459011"]If you want to rule out an &lt;a href="https://en.wikipedia.org/wiki/XY_problem"&gt;XY Problem&lt;/a&gt;, you can tell me what the use-case is. If not I will help you as best I can with what you ask for.[/quote]
&lt;p&gt;I will try to rephrase the question.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I implemented an initial solution for calculating ECDSA signature, using the mbedtls library, that works fine.&lt;br /&gt;Such implementation loads the private key in DER format (stored in Flash memory) into&amp;nbsp;&lt;strong&gt;&lt;em&gt;mbedtls_pk_context&lt;/em&gt;&lt;/strong&gt;&amp;nbsp;using&amp;nbsp;&lt;strong&gt;&lt;em&gt;mbedtls_pk_parse_key, &lt;/em&gt;&lt;/strong&gt;and further calculates signature providing such&amp;nbsp;&lt;strong&gt;&lt;em&gt;mbedtls_pk_context&amp;nbsp;&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;&lt;/em&gt;to the&amp;nbsp;&lt;em&gt;&lt;strong&gt;mbedtls_ecdsa_write_signature&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;function.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Now I want to stop using the key preloaded in Flash&amp;nbsp;and generate it within the code.&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;My approach is to generate key with&amp;nbsp;&lt;em&gt;&lt;strong&gt;psa_generate_key&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;function, and load such key to&amp;nbsp;&lt;em&gt;&lt;strong&gt;mbedtls_pk_context&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;using&amp;nbsp;&lt;em&gt;&lt;strong&gt;mbedtls_pk_setup_opaque&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;(practically for replacing the call&amp;nbsp;of&amp;nbsp;&lt;strong&gt;&lt;em&gt;mbedtls_pk_parse_key&lt;/em&gt;&lt;/strong&gt; with the call of &lt;em&gt;&lt;strong&gt;mbedtls_pk_setup_opaque&lt;/strong&gt;&lt;/em&gt;). &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The bottom problem is that I&amp;#39;m not able to compile code using&amp;nbsp;&lt;em&gt;&lt;strong&gt;mbedtls_pk_setup_opaque &lt;/strong&gt;&lt;/em&gt;call.&lt;/span&gt;&lt;/p&gt;
[quote userid="106736" url="~/f/nordic-q-a/106325/mbedtls---using-keys-handled-by-psa-crypto/459011"]I suggest that you use the VS Code Kconfig tool or &amp;quot;menuconfig&amp;quot; (west build -t menuconfig) to check which dependencies unset CONFIG_MBEDTLS_USE_PSA_CRYPTO.[/quote]
&lt;p&gt;I will check that.&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;br /&gt;Darko&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mbedtls - Using keys handled by PSA Crypto</title><link>https://devzone.nordicsemi.com/thread/459011?ContentTypeID=1</link><pubDate>Tue, 05 Dec 2023 14:27:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:719f8a21-6beb-42e6-9bc6-5832a7bf6761</guid><dc:creator>Sigurd Hellesvik</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user=""]I need to implement the code that calculates the ECDSA signature, using the mbedtls library.[/quote]
&lt;p&gt;If you want to rule out an &lt;a href="https://en.wikipedia.org/wiki/XY_problem"&gt;XY Problem&lt;/a&gt;, you can tell me what the use-case is. If not I will help you as best I can with what you ask for.&lt;/p&gt;
[quote user=""] it looks like &lt;em&gt;&lt;strong&gt;MBEDTLS_USE_PSA_CRYPTO&lt;/strong&gt;&lt;/em&gt; is not defined.[/quote]
&lt;p&gt;I suggest that you use the VS Code Kconfig tool or &amp;quot;menuconfig&amp;quot; (west build -t menuconfig) to check which dependencies unset CONFIG_MBEDTLS_USE_PSA_CRYPTO.&lt;/p&gt;
&lt;p&gt;If I build the nrf/samples/crypto/ecdsa sample with your above configs except for EXTERNAL_RNG (it makes the build fail), I get MBEDTLS_USE_PSA_CRYPTO=y&lt;/p&gt;
&lt;p&gt;Does this help you?&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Sigurd Hellesvik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>