⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lib_crypt.c

📁 非常全的nrf2401设计资料
💻 C
字号:
/* Copyright (c) 2008 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is confidential property of Nordic 
 * Semiconductor. The use, copying, transfer or disclosure of such information 
 * is prohibited except by express written agreement with Nordic Semiconductor.
 */

/** @file lib_crypt.c
 * 
 * This file implements the encryption library. This library can be used on both 
 * nRF24LU1 and nRF24LE1 and enables encryption between the two devices. 
 * 
 * @author Ivar Conradi OEsthus
 */

#include "lib_crypt.h"
#include "hal_aes.h"
#include <stdint.h>
#include <Nordic\reg24le1.h>

/**
The "aes_counter" is defined as shown below:
@code
15.14...                                             5..4...            ...1..0
|------------------------------------------------------|-----------------------|
|                 MS11B  (11 bytes)                    |    LS5B (5 bytes)     |
|------------------------------------------------------|-----------------------|
  (never incremented)                                   (increment before encr.)

*) MS11B: 11 most  significant bytes
*) LS5B :  5 least significant bytes
@endcode
*/
static uint8_t xdata aes_counter[16];

void lib_crypt_init(uint8_t * key, uint8_t * init_counter)
{
	hal_aes_setup(false, ECB, key, NULL);

	if(init_counter)
	{
		lib_crypt_set_counter(init_counter);
	}
}

void lib_crypt_set_counter(uint8_t * counter)
{
	uint8_t i;
	for(i=0;i<16;i++)
	{
		aes_counter[i] = counter[i];
	}
}

void lib_crypt(uint8_t * dest_buf, uint8_t * src_buf, uint8_t length, uint8_t * ls5b_value)
{
	uint8_t i;
  xdata uint8_t encr_buffer[16];   //AES buffer. Needed to do XOR

  //Set LS5B
	for(i=0;i<5;i++)
	{
		aes_counter[i] = ls5b_value[i];
	}	

  //Run AES with aes_counter
	hal_aes_crypt(encr_buffer,aes_counter);
	
  //Encrypt data, based on XOR operation in AES counter mode.
	for(i=0;i<length; i++)
	{
		dest_buf[i] = src_buf[i] ^ encr_buffer[i];
	}
}

void lib_crypt_generate_ls5b(uint8_t * dest_buf)
{
	uint8_t i;
  bool wrap = true;
  
  //Increment LS5B, and write back the new LS5B.
  for(i=0;i<5;i++)
	{		
	 	if(wrap)  //Check if we need to increment position i.
	 	{
	 	  aes_counter[i]++;
      if(aes_counter[i] != 0x00) wrap = false;	
		}

    //Write out LS5B
    dest_buf[i] = aes_counter[i]; 
	} 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -