SWRA846 July   2025 CC2642R-Q1

 

  1.   1
  2.   Abstract
  3. 1Analysis of Bluetooth Pairing Technology Principles
    1. 1.1 LE Legacy Pairing and LE Secure Connections Pairing
    2. 1.2 Analysis of Association Mode Security Characteristics
    3. 1.3 Privacy Protection and Address Management
  4. 2Common Issues in Pairing Encryption
    1. 2.1 Integrity Check Fault—MIC Verification Failure (MIC failure)
    2. 2.2 Abnormal Key Negotiation—DHKey Verification Failure (DHKey check fail)
    3. 2.3 Key Management Defect—PIN/Key Loss (PIN or Key missing)
      1. 2.3.1 Key Missing During Pairing Phase
      2. 2.3.2 LTK Missing During Encryption Phase
  5. 3A Solution for Encryption Resource Conflicts
    1. 3.1 Environment Selection and Configuration
    2. 3.2 Implementing AES-256 Encryption and Decryption Using TinyAES Library
    3. 3.3 System-Level Optimization Exploration
      1. 3.3.1 Memory Allocation Inspection
      2. 3.3.2 Padding Processing and Thread Safety
  6. 4Conclusion
  7. 5References

Implementing AES-256 Encryption and Decryption Using TinyAES Library

  • Add implementation of AES encryption task
 encrypt_task.c 
#include "FreeRTOS.h"
#include "task.h"
#include "aes.h"
#include <stdio.h>
#include <string.h>

#define AES_BLOCK_SIZE 16

// AES-256密钥(必须为32字节)
static uint8_t key[32] = {xx};

// 初始化向量(IV),必须为16字节
static uint8_t iv[AES_BLOCK_SIZE] = {yy};

void EncryptTask(void *pvParameters) {
  uint8_t plaintext_block[AES_BLOCK_SIZE] = {0};  
  memcpy(plaintext_block, plaintext, strlen(plaintext));

  uint8_t ciphertext_block[AES_BLOCK_SIZE] = {0};
  uint8_t decrypted_block[AES_BLOCK_SIZE] = {0};
  
  // AES CBC 加密上下文
  struct AES_ctx ctx;  
  AES_init_ctx_iv(&ctx, key, iv);

  // 加密
  AES_CBC_encrypt_buffer(&ctx, plaintext_block, AES_BLOCK_SIZE);  
  memcpy(ciphertext_block, plaintext_block, AES_BLOCK_SIZE);

  // 解密(重新初始化上下文以确保IV被重置)
  AES_init_ctx_iv(&ctx, key, iv);
  AES_CBC_decrypt_buffer(&ctx, ciphertext_block, AES_BLOCK_SIZE);
  memcpy(decrypted_block, ciphertext_block, AES_BLOCK_SIZE);

  vTaskDelay(pdMS_TO_TICKS(1000)); // 任务延时
  • Create task in main function
 main.c 
#include "FreeRTOS.h"
#include "task.h"
#include "encrypt_task.h"

int main(void) {
  // 初始化硬件
  // 创建加密任务
  xTaskCreate(EncryptTask,     // 任务函数
             "Encrypt Task",  // 任务名称
              512,           // 堆栈大小(字节)      
              NULL,         // 任务参数
              1,           // 优先级(任务越重要越高)
              NULL);      // 任务句柄
  // 启动RTOS调度器  
  vTaskStartScheduler();
}

The advantages of this method lie in:

  • The lightweight software library provides an encryption option that consumes almost no CPU computing or storage power. In fact, the TinyAES library occupies very lightweight code space, requiring approximately ~2-3 KB of code space in the absence of floating-point operations (including AES implementation and initialization vector support), which also depends on the selected encryption mode (the CBC mode takes slightly more code than the CTR mode). If the application code contains critical print statements (such as printf) and local data, it will occupy approximately ~2-4KB of extra Flash and ~2.5KB of extra RAM.
  • The software method allows developers to optimize algorithms according to specific needs. For example, optimizing confusion through look-up tables, introducing chaotic mapping to enhance key security, or dynamically adjusting key generation strategies. Hardware implementations usually solidify algorithm logic, making it difficult to modify. Adopting different software libraries can support more encryption modes (such as ECB, CBC, CTR, etc.), and developers can flexibly choose and extend encryption algorithms not supported by the AES engine according to requirements.
  • The encryption logic implemented in software can quickly fix vulnerabilities or update algorithms through firmware upgrades (such as OTA), whereas the hardware accelerator requires adjusting the design logic of the entire application to seek circumvention as introduced previously. The cost is greater.