<?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>Asymmetric encryption/decryption with public key</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/50627/asymmetric-encryption-decryption-with-public-key</link><description>Hi, 
 I&amp;#39;m studying the example about the nrf_crypto for implementing asymmetric encrypt and decrypt function. 
 The suggestion on the Dev_Zone is to use the share key to symmetric encrypt, decrypt and DSA after finishing ECDH. 
 According to public-key</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 05 Aug 2019 08:13:30 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/50627/asymmetric-encryption-decryption-with-public-key" /><item><title>RE: Asymmetric encryption/decryption with public key</title><link>https://devzone.nordicsemi.com/thread/202330?ContentTypeID=1</link><pubDate>Mon, 05 Aug 2019 08:13:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3110de74-73cd-4a7e-bf12-083403006eec</guid><dc:creator>Jimmy Wong</dc:creator><description>&lt;p&gt;You can refer to the code as&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/jimmywong2003/SES-Project-For-NRF51/blob/master/ble_app_uart_bas_ecdh_aes128/main.c"&gt;https://github.com/jimmywong2003/SES-Project-For-NRF51/blob/master/ble_app_uart_bas_ecdh_aes128/main.c&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// This is to generate the public key and shared secret key
    {
        static nrf_crypto_key_t peer_pk;

        nrf_crypto_init();

        NRF_LOG_INFO(&amp;quot;Private Key\n&amp;quot;);
        //NRF_LOG_HEXDUMP_INFO(m_crypto_key_sk.p_le_data, m_crypto_key_sk.len);
        
        nrf_delay_ms(1000);

        err_code = nrf_crypto_public_key_compute(NRF_CRYPTO_CURVE_SECP256R1, &amp;amp;m_crypto_key_sk, &amp;amp;m_crypto_key_pk);
        APP_ERROR_CHECK(err_code);

        NRF_LOG_INFO(&amp;quot;Generate the Public Key\n&amp;quot;);
        //NRF_LOG_HEXDUMP_INFO(m_crypto_key_pk.p_le_data, m_crypto_key_pk.len);

        //peer_pk.p_le_data = &amp;amp;p_ble_evt-&amp;gt;evt.gap_evt.params.lesc_dhkey_request.p_pk_peer-&amp;gt;pk[0];
        peer_pk.len = BLE_GAP_LESC_P256_PK_LEN;

        nrf_delay_ms(1000);

        memcpy(peer_pk.p_le_data, m_crypto_key_pk.p_le_data, peer_pk.len);
        err_code = nrf_crypto_shared_secret_compute(NRF_CRYPTO_CURVE_SECP256R1, &amp;amp;m_crypto_key_sk, &amp;amp;m_crypto_key_pk, &amp;amp;m_crypto_key_dhkey);
        APP_ERROR_CHECK(err_code);

        nrf_delay_ms(1000);

        NRF_LOG_INFO(&amp;quot;Shared Secret Key\n&amp;quot;);
        NRF_LOG_HEXDUMP_INFO(m_crypto_key_dhkey.p_le_data, m_crypto_key_dhkey.len);
    }


    // After get the shared secret key, it uses the ecb (AES128) for encrypt and decrypt the data
    {
        test_encrypt_decrypt_ecb();
    }
    
    
    
    static uint8_t plaintext[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
static uint8_t ciphertext[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};

static uint8_t buffer_encrypt[16];
static uint8_t buffer_decrypt[16];

static void test_encrypt_decrypt_ecb(void)
{
  uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c};
  uint8_t in[]  = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a};
  uint8_t out[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
  uint8_t buffer[16];

  memcpy(key, m_crypto_key_dhkey.p_le_data, 16);
  
  NRF_LOG_HEXDUMP_INFO(key, 16);

  NRF_LOG_INFO(&amp;quot;ECB encrypt: &amp;quot;);

  AES128_ECB_encrypt(plaintext, key, buffer_encrypt);

  NRF_LOG_HEXDUMP_INFO(buffer_encrypt, 16);

  NRF_LOG_INFO(&amp;quot;ECB decrypt: &amp;quot;);

  AES128_ECB_decrypt(buffer_encrypt, key, buffer_decrypt);

  if(0 == strncmp((char*) plaintext, (char*) buffer_decrypt, 16))
  {
    NRF_LOG_INFO(&amp;quot;SUCCESS!\n&amp;quot;);
  }
  else
  {
    NRF_LOG_INFO(&amp;quot;FAILURE!\n&amp;quot;);
  }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>