This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Running ECDH and AES256 CTR in the same project

Hi,

I successfully tested ecdh and aes256 in seperate projects but I want to combine them.

This is my code:

#include "nrf.h"
#include "app_error.h"
#include "nrf_crypto.h"
#include "nrf_crypto_ecc.h"
#include "nrf_crypto_ecdh.h"
#include "nrf_crypto_error.h"
#include "mem_manager.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

static uint8_t m_key[32];
static uint8_t iv[16];

static char m_decrypted_text[100];

static nrf_crypto_aes_context_t ctr_encr_256_ctx;
static nrf_crypto_aes_context_t ctr_decr_256_ctx;

static uint8_t input[] = { 0xb1, 0xf4, 0xb2, 0x0e, 0xcb, 0x2e };

static nrf_crypto_ecc_private_key_t private_key;
static nrf_crypto_ecc_public_key_t public_key;

static nrf_crypto_ecc_secp256r1_raw_public_key_t raw_public_key_buffer;

#define DEMO_ERROR_CHECK(error)     \
do                                  \
{                                   \
    if (error != NRF_SUCCESS)       \
    {                               \
        NRF_LOG_ERROR("Error 0x%04X: %s", error, nrf_crypto_error_string_get(error));\
        APP_ERROR_CHECK(error);     \
    }                               \
} while(0)

static void log_init(void)
{
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
}

int main(void)
{
  APP_ERROR_CHECK(nrf_mem_init());
  APP_ERROR_CHECK(nrf_crypto_init());

  log_init();

  ret_code_t ret_val = nrf_crypto_aes_init(&ctr_encr_256_ctx, &g_nrf_crypto_aes_ctr_256_info, NRF_CRYPTO_ENCRYPT);
  APP_ERROR_CHECK(ret_val);

  ret_val = nrf_crypto_aes_key_set(&ctr_encr_256_ctx, m_key);
  APP_ERROR_CHECK(ret_val);

  ret_val = nrf_crypto_aes_iv_set(&ctr_encr_256_ctx, iv);
  APP_ERROR_CHECK(ret_val);

  size_t decryption_length = sizeof(m_decrypted_text);

  ret_val = nrf_crypto_aes_crypt(&ctr_decr_256_ctx,
                                &g_nrf_crypto_aes_ctr_256_info,
                                NRF_CRYPTO_DECRYPT,
                                m_key,
                                iv,
                                (uint8_t *)input,
                                sizeof(input),
                                (uint8_t *)m_decrypted_text,
                                &decryption_length);
  APP_ERROR_CHECK(ret_val);

  nrf_crypto_aes_uninit(&ctr_encr_256_ctx);

  ret_val = nrf_crypto_ecc_key_pair_generate(NULL,
                                             &g_nrf_crypto_ecc_secp256k1_curve_info,
                                             &private_key,
                                             &public_key);
  DEMO_ERROR_CHECK(ret_val);

  size_t size = sizeof(raw_public_key_buffer);
  ret_val = nrf_crypto_ecc_public_key_to_raw(&public_key,
                                             raw_public_key_buffer,
                                             &size);
  DEMO_ERROR_CHECK(ret_val);

  NRF_LOG_HEXDUMP_INFO(raw_public_key_buffer, sizeof(raw_public_key_buffer));

  while(true){}
}

Problem is that when i call the ecc functions this happens:

<error> app: Error 0x8516: An internal error occurred when calling this function

Any idea?

Parents Reply Children
No Data
Related