?? lib_crypt.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 + -