Note_Nordic_Keil中添加AES库

TOC

Keil 工程中添加AES库

添加文件到工程目录

nRF_Crypto

#文件目录
nRF5SDK160098a08e2\components\libraries\crypto

nRF_Crypto backend mbed TLS

#文件目录
nRF5SDK160098a08e2\components\libraries\crypto\backend\mbedtls

nRF_TLS

#文件目录
nRF5SDK160098a08e2\external\mbedtls\library

修改工程选项C/C++

Perprocessor Symbols

添加 MBEDTLS_CONFIG_FILE=<nrf_crypto_mbedtls_config.h>

Include Paths

添加

..\..\..\..\..\..\components\libraries\crypto\backend\cc310
..\..\..\..\..\..\components\libraries\crypto\backend\cc310_bl
..\..\..\..\..\..\components\libraries\crypto\backend\mbedtls
..\..\..\..\..\..\components\libraries\crypto\backend\oberon
..\..\..\..\..\..\components\libraries\crypto\backend\micro_ecc
..\..\..\..\..\..\components\libraries\crypto\backend\optiga
..\..\..\..\..\..\components\libraries\crypto\backend\nrf_sw
..\..\..\..\..\..\components\libraries\crypto\backend\nrf_hw
..\..\..\..\..\..\components\libraries\crypto\backend\cifra
..\..\..\..\..\..\components\libraries\mem_manager
..\..\..\..\..\..\external\mbedtls\include
..\..\..\..\..\..\external\mbedtls\include\mbedtls
..\..\..\..\..\..\external\cifra_AES128-EAX
..\..\..\..\..\..\external\nrf_tls\mbedtls\nrf_crypto\config

注意:具体多少个..\需要根据你的工程所在目录做出修改

修改工程选项Asm

Include Paths 添加目录

..\..\..\..\..\..\components\backend\cc310
..\..\..\..\..\..\components\backend\cc310_bl
..\..\..\..\..\..\components\backend\cifra
..\..\..\..\..\..\components\backend\mbedtls
..\..\..\..\..\..\components\backend\micro_ecc
..\..\..\..\..\..\components\backend\nrf_hw
..\..\..\..\..\..\components\backend\nrf_sw
..\..\..\..\..\..\components\backend\oberon
..\..\..\..\..\..\components\backend\optiga

注意:具体多少个..\需要根据你的工程所在目录做出修改

SDK配置

  • nRF_Crypto
  • nRF_Drivers下勾选
  • nRF_Libraries

参考工程

  • 目录
nRF5SDK160098a08e2\examples\crypto\nrf_crypto\aes\aes_all_cli
  • bug
    mico_ecc_lib_nrf52.lib文件找不到,也许是要手动生成一下?我没有继续研究

添加头文件

main.c 中添加相应的头文件

#include "nrf_crypto.h"
#include "nrf_crypto_error.h"
#include "mem_manager.h"

示例程序

/**@brief aes ceb configuration. */
static const uint8_t  *m_plain_text = "5FxxxxCExxxxxxAAA";
static const uint8_t m_key[17] = "Yxx7xxC7V0U4xxxx";

static void aes_test(void)
{
    size_t      len_in;
    size_t      len_out;
    ret_code_t  ret_val;
    char        encrypted_text[100];
    char        decrypted_text[100];

    nrf_crypto_aes_info_t const * p_ecb_info;
    nrf_crypto_aes_context_t      ecb_encr_ctx;
    nrf_crypto_aes_context_t      ecb_decr_ctx;

    memset(encrypted_text, 0, sizeof(encrypted_text));
    memset(decrypted_text, 0, sizeof(decrypted_text));

    p_ecb_info = &g_nrf_crypto_aes_ecb_128_info;

    len_in  = strlen((const char *)m_plain_text);
    len_out = sizeof(decrypted_text);
    NRF_LOG_INFO("aes test lenin:%d, lenout:%d",len_in, len_out);
    /* Encrypt text with integrated function */
    ret_val = nrf_crypto_aes_crypt(&ecb_encr_ctx,
                                   p_ecb_info,
                                   NRF_CRYPTO_ENCRYPT,
                                   (uint8_t *)m_key,
                                   NULL,
                                   (uint8_t *)m_plain_text,
                                   len_in,
                                   (uint8_t *)encrypted_text,
                                   &len_out);
    APP_ERROR_CHECK(ret_val);

    ret_val = nrf_crypto_aes_init(&ecb_decr_ctx,
                                  p_ecb_info,
                                  NRF_CRYPTO_DECRYPT);
    APP_ERROR_CHECK(ret_val);

    /* Set encryption and decryption key */

    ret_val = nrf_crypto_aes_key_set(&ecb_decr_ctx, (uint8_t *)m_key);
    APP_ERROR_CHECK(ret_val);

    /* Decrypt blocks */
    //注意,len_out一定要赋值为decrypted_text的大小,而不是为0,
    // len_out 即是输入参数,也是输出参数
    // len_out 输入参数时,为输出buffer的大小
    // len_out 输出参数时,当解密成功后,len_out被赋值解密的长度!
    len_out = sizeof(decrypted_text);
    len_in = strlen(encrypted_text);
    ret_val = nrf_crypto_aes_finalize(&ecb_decr_ctx,
                                      (uint8_t *)encrypted_text,
                                      len_in,
                                      (uint8_t *)decrypted_text,
                                      &len_out);
    APP_ERROR_CHECK(ret_val);

    NRF_LOG_INFO("aes palin text:%s, len_in:%d, len_out:%d ", m_plain_text, len_in, len_out );

    NRF_LOG_INFO("aes decrypted text:%s, %d", decrypted_text, decrypted_text[16]);
    if (strncmp((char *)m_plain_text, decrypted_text, len_out) == 0) {
        NRF_LOG_INFO("aes decryption sucess!!!");
    }
}





posted @ 2020-12-29 20:43  JerryZheng2020  阅读(673)  评论(0编辑  收藏  举报