<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[wolfSSL - Embedded SSL Library — Cryptocell RSA signing error]]></title>
		<link>https://www.wolfssl.com/forums/topic1452-cryptocell-rsa-signing-error.html</link>
		<atom:link href="https://www.wolfssl.com/forums/feed-rss-topic1452.xml" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Cryptocell RSA signing error.]]></description>
		<lastBuildDate>Wed, 23 Oct 2019 16:07:04 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Cryptocell RSA signing error]]></title>
			<link>https://www.wolfssl.com/forums/post4933.html#p4933</link>
			<description><![CDATA[<p>Hi @roelof,</p><p>I can confirm what you are seeing in the code, the key pair is being generated but the private key does not appear to be getting decoded into the same structure. I will check with the developer that worked on that porting effort and see why there is no call to wc_RsaPrivateKeyDecode following the call to decode the public key.</p><p>I&#039;ll let you know what I find out.</p><p>Warm Regards,</p><p>K</p>]]></description>
			<author><![CDATA[null@example.com (Kaleb J. Himes)]]></author>
			<pubDate>Wed, 23 Oct 2019 16:07:04 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post4933.html#p4933</guid>
		</item>
		<item>
			<title><![CDATA[Re: Cryptocell RSA signing error]]></title>
			<link>https://www.wolfssl.com/forums/post4932.html#p4932</link>
			<description><![CDATA[<p>Hi @Kaleb,</p><p>I do not believe my problem lies in the wc_GenerateSeed function as my RSA keys are generated successfully - according to WolfCrypt. The problem is that only the public key parts are available and not the private key</p><p>When generating a RSA keypair - using cryptocell the following function is called (from wolfcrypt/src/rsa.c):</p><div class="codebox"><pre><code>int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
{
#ifndef WC_NO_RNG
    mp_int p, q, tmp1, tmp2, tmp3;
    int err, i, failCount, primeSz, isPrime = 0;
    byte* buf = NULL;

    if (key == NULL || rng == NULL)
        return BAD_FUNC_ARG;

    if (!RsaSizeCheck(size))
        return BAD_FUNC_ARG;

    if (e &lt; 3 || (e &amp; 1) == 0)
        return BAD_FUNC_ARG;

#if defined(WOLFSSL_CRYPTOCELL)

    return cc310_RSA_GenerateKeyPair(key, size, e);

#endif /*WOLFSSL_CRYPTOCELL*/</code></pre></div><p>The following cryptocell function is then called:</p><p>cc310_RSA_GenerateKeyPair (in wolfcrypt/src/rsa.c) then executes :</p><div class="codebox"><pre><code> ret = CRYS_RSA_KG_GenerateKeyPair(&amp;wc_rndState,
                        wc_rndGenVectFunc,
                        (byte*)&amp;e,
                        3*sizeof(uint8_t),
                        size,
                        &amp;key-&gt;ctx.privKey,
                        &amp;key-&gt;ctx.pubKey,
                        &amp;KeyGenData,
                        &amp;FipsCtx);

    if (ret != SA_SILIB_RET_OK){
        WOLFSSL_MSG(&quot;CRYS_RSA_KG_GenerateKeyPair failed&quot;);
        return ret;
    }

    ret = CRYS_RSA_Get_PubKey(&amp;key-&gt;ctx.pubKey, ex, &amp;eSz, n, &amp;nSz);
    if (ret != SA_SILIB_RET_OK){
        WOLFSSL_MSG(&quot;CRYS_RSA_Get_PubKey failed&quot;);
        return ret;
    }
    ret = wc_RsaPublicKeyDecodeRaw(n, nSz, ex, eSz, key);

key-&gt;type = RSA_PRIVATE;</code></pre></div><p>From this code section only the public key elements ( &amp;key-&gt;ctx.pubKey) are decoded into the original &quot;struct RsaKey&quot; and not the &amp;key-&gt;ctx.privKey.</p><p>This means if a key is generated using cryptocell - only the public key elements can be used in the rest of the wolfcrypt RSA API functions like wc_RsaKeyToDer and then subsequently wc_RsaPrivateKeyDecode.</p><p>Then for some reason the key-&gt;type is set to RSA_PRIVATE</p><p>How can I get the private key from &amp;key-&gt;ctx.privKey into a compatible RSA struct to use in wc_RsaKeyToDer</p>]]></description>
			<author><![CDATA[null@example.com (roelof)]]></author>
			<pubDate>Wed, 23 Oct 2019 13:32:55 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post4932.html#p4932</guid>
		</item>
		<item>
			<title><![CDATA[Re: Cryptocell RSA signing error]]></title>
			<link>https://www.wolfssl.com/forums/post4931.html#p4931</link>
			<description><![CDATA[<p>Hi @roelof,</p><p>Thank you for using the wolfSSL forums. You&#039;ll note that in the case of not using crypto cell support the RNG and wc_GenerateSeed function are fully implemented. If you defined WOLFSSL_NRF5x the define NO_DEV_RANDOM is assumed in wolfssl-root/wolfssl/wolfcrypt/settings.h and it is expected that the user will either implement a device-specific version of generate seed to seed the wolfSSL hash DRBG. For reference please checkout the wc_GenerateSeed function in wolfssl-root/wolfcrypt/src/random.c when WOLFSSL_NRF51 is defined.</p><p>It is likely you would have seen some warnings in the build step if something was amiss. Can you tell us if the build was warning-free or were there any warnings generated?</p><p>Warm Regards,</p><p>K</p>]]></description>
			<author><![CDATA[null@example.com (Kaleb J. Himes)]]></author>
			<pubDate>Mon, 21 Oct 2019 14:45:51 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post4931.html#p4931</guid>
		</item>
		<item>
			<title><![CDATA[Cryptocell RSA signing error]]></title>
			<link>https://www.wolfssl.com/forums/post4925.html#p4925</link>
			<description><![CDATA[<p>I am using wolfcrypt 4.1.0 with the Nordic NRF52840 cryptocell enabled.<br />Since enabling cryptocell I am no longer able to use wc_RsaSSL_Sign to sign with my RSA private key.<br />I have traced it to being unable to decode the private key from the RSA generated keypair.</p><p>The following code works successfully when cryptocell is not enabled, but with cryptocell enabled , wc_RsaPrivateKeyDecode return with error code -192 (Bad state operation )</p><div class="codebox"><pre><code>   //Key generation
    RsaKey   RSAKey;
    long     exp = 65537l;
    WC_RNG   rng;
    int      keySize = 1024;
    uint8_t  *derKey = NULL;
    uint16_t derSz = 0;

    if( wc_InitRsaKey(&amp;RSAKey, NULL) != 0 ) { // not using heap hint. No custom memory
        // error initializing rng
        printf(&quot;wc_InitRng Failed&quot;);
        goto end;
    }

    //initialize random number generator
    if( wc_InitRng(&amp;rng) != 0 ) {
        // error initializing rng
        printf(&quot;wc_InitRng Failed&quot;);
        goto end;
    }

    if(wc_RsaSetRNG(&amp;RSAKey, &amp;rng) != EXIT_SUCCESS) {
        printf(&quot;wc_RsaSetRNG Failed\r\n&quot;);
        goto end; 
    }

    // generate keysize bit long private key
    if( wc_MakeRsaKey(&amp;RSAKey, keySize, exp, &amp;rng) != 0 ) {
        // error generating private key
        printf(&quot;wc_MakeRsaKey Failed\r\n&quot;);
        goto end; 
    }

    //free RNG object
    if (wc_FreeRng(&amp;rng) != 0) {
      printf(&quot;wc_FreeRng Failed \r\n&quot;);
    }

    //Check RSA key
    int ret = wc_CheckRsaKey(&amp;RSAKey);
    if (ret != 0) {
        printf(&quot;Key Error\r\n&quot;);
    }

    // Allocate memory for der
    derKey = pvPortMalloc(keySize);
    if (derKey == NULL) {
        NRF_LOG_ERROR(&quot;Could not allocate memory to create derKey&quot;);
        goto end;
    }

    derSz = 0;

    //Convert key to der
    derSz = wc_RsaKeyToDer(&amp;RSAKey, derKey, keySize);
    printf(&quot;Der size = %d \r\n&quot;,derSz);
    if (derSz == 0) {
        printf(&quot;der Error\r\n&quot;);
    }

    RsaKey privateKey;
    word32 idx = 0;

    //decode new private key from DER
    ret = wc_RsaPrivateKeyDecode(derKey, &amp;idx, &amp;privateKey, derSz);
    if( ret != 0 ) {
        printf(&quot;Cannot decode private key. ret = %d \r\n&quot;,ret);
        printf(&quot;Failed here \r\n&quot;);
    }</code></pre></div><p>Question: How can I extract the RSA private key to use to sign data using wc_RsaSSL_Sign- using cryptocell.</p>]]></description>
			<author><![CDATA[null@example.com (roelof)]]></author>
			<pubDate>Tue, 15 Oct 2019 06:08:36 +0000</pubDate>
			<guid>https://www.wolfssl.com/forums/post4925.html#p4925</guid>
		</item>
	</channel>
</rss>
