0% found this document useful (0 votes)
179 views5 pages

Lifi Code

This document defines functions for encrypting and decrypting data using AES-128 encryption. It includes functions for encrypting/decrypting single blocks, multiple blocks, and initializing and finalizing encryption contexts for encrypting multiple blocks with CBC mode. The functions take a key, initialization vector (IV), data to encrypt/decrypt, and handle the AES encryption and decryption process.

Uploaded by

majid jalil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views5 pages

Lifi Code

This document defines functions for encrypting and decrypting data using AES-128 encryption. It includes functions for encrypting/decrypting single blocks, multiple blocks, and initializing and finalizing encryption contexts for encrypting multiple blocks with CBC mode. The functions take a key, initialization vector (IV), data to encrypt/decrypt, and handle the AES encryption and decryption process.

Uploaded by

majid jalil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <SoftwareSerial.

h>
SoftwareSerial lightComm(2, 3);
#include <AESLib.h>

#define led 5
#define audioBtn 8

String Msg;
String Answer;

uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,'A',13,14,15};


char data[] = "0123456789012345"; //16 chars == 16 bytes

void setup()
{

Serial.begin(9600);
lightComm.begin(9600);
pinMode(led, OUTPUT);
pinMode(audioBtn, INPUT_PULLUP);
digitalWrite(led, HIGH);
delay(1000);
Serial.println("Welcome\n");

void loop()
{

while(Serial.available())
{
delay(5);
if(Serial.available() > 0)
{
char s = (char)Serial.read();
//Serial.print(s);
Msg+= s;
}
}

if(Msg.length()>0)
{
Serial.print("MESSAGE: ");
Serial.println(Msg);

Msg.toCharArray(data,16);
aes128_enc_single(key,data);
Msg.remove(0);

Serial.print("ENCRYPTED-MESSAGE-SENT: ");
for(int i=0;i<16;i++)
{
Serial.print(data[i]+48);
Serial.print(" ");
}
//Serial.println(data+);
Serial.println("\n");
lightComm.print(data);

//delay(100);
digitalWrite(led,LOW);
delay(300);
digitalWrite(led,HIGH);
Msg.remove(0);
}

int t=0;
while(lightComm.available())
{
delay(5);
if(lightComm.available() > 0)
{
char c = (char)lightComm.read();
//Serial.print(c);
Answer += c;
data[t]=c;
t=t+1;
}
}

if(Answer.length()>0)
{
// Serial.println(analogRead(A0));
//Serial.print("Receive:");
//Serial.println(Answer);
if (analogRead(A0)<100)
{
// Serial.println(analogRead(A0));
delay(400);
if (analogRead(A0)>100)
{
Serial.print("RECEIVE-MESSAGE: ");
for(int i=0;i<16;i++)
{
Serial.print(data[i]+48);
Serial.print(" ");
}
Serial.println("");
//Serial.println(data);
aes128_dec_single(key, data);
Serial.print("DECRYPTED-MESSAGE: ");
Serial.println(data);
Serial.println("");
}
}
Answer.remove(0);
}
}
*// encrypt multiple blocks of 128bit data, data_len but be mod 16
// key and iv are assumed to be both 128bit thus 16 uint8_t's

void aes128_cbc_enc(const uint8_t* key, const uint8_t* iv, void* data, const
uint16_t data_len){
if (data_len % 16 != 0) {
return;
}

bcal_cbc_ctx_t ctx;
uint8_t r;
r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
if (r) {
return;
}

bcal_cbc_encMsg(iv, data, data_len / 16, &ctx);


bcal_cbc_free(&ctx);
}

// encrypt single 128bit block. data is assumed to be 16 uint8_t's


// key and iv are assumed to be both 128bit thus 16 uint8_t's
void aes128_enc_single(const uint8_t* key, void* data){
aes128_ctx_t ctx;
aes128_init(key, &ctx);
aes128_enc(data, &ctx);
}

// encrypt multiple blocks of 128bit data, data_len must be mod 16


// key is assumed to be 128bit thus 16 uint8_t's
void aes128_enc_multiple(const uint8_t* key, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
aes128_ctx_t ctx;
aes128_init(key, &ctx);

uint8_t* current = (uint8_t*)data;


uint8_t* stop = current + data_len;
while(current != stop){
aes128_enc(current, &ctx);
current += 16;
}
}

// prepare an encrypted to use for encrypting multiple blocks lateron.


// key and iv are assumed to be both 128bit thus 16 uint8_t's
aes_context aes128_cbc_enc_start(const uint8_t* key, const void* iv){
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
if (r) {
free(ctx);
return NULL;
}
bcal_cbc_loadIV(iv, ctx);
return (aes_context)ctx;
}

// encrypt one or more blocks of 128bit data


// data_len should be mod 16
void aes128_cbc_enc_continue(const aes_context ctx, void* data, const uint16_t
data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
uint16_t msg_blocks = data_len / 16;
while(msg_blocks--){
bcal_cbc_encNext(data, _ctx);
data = (uint8_t*)data + _ctx->blocksize_B;
}
}

// cleanup encryption context


void aes128_cbc_enc_finish(const aes_context ctx){
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
free(ctx);
}

// decrypt multiple blocks of 128bit data, data_len but be mod 16


// key and iv are assumed to be both 128bit thus 16 uint8_t's
void aes128_cbc_dec(const uint8_t* key, const uint8_t* iv, void* data, const
uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t ctx;
uint8_t r;
r = bcal_cbc_init(&aes128_desc, key, 128, &ctx);
if (r) {
return;
}
bcal_cbc_decMsg(iv, data, data_len / 16, &ctx);
bcal_cbc_free(&ctx);
}

// decrypt single 128bit block. data is assumed to be 16 uint8_t's


// key is assumed to be 128bit thus 16 uint8_t's
void aes128_dec_single(const uint8_t* key, void* data){
aes128_ctx_t ctx;
aes128_init(key, &ctx);
aes128_dec(data, &ctx);
}

// decrypt multiple blocks of 128bit data, data_len must be mod 16


// key is assumed to be 128bit thus 16 uint8_t's
void aes128_dec_multiple(const uint8_t* key, void* data, const uint16_t data_len){
if (data_len % 16 != 0) {
return;
}
aes128_ctx_t ctx;
aes128_init(key, &ctx);

uint8_t* current = (uint8_t*)data;


uint8_t* stop = current + data_len;
while(current != stop) {
aes128_dec(current, &ctx);
current += 16;
}
}

// prepare an decrypted to use for decrypting multiple blocks lateron.


// key and iv are assumed to be both 128bit thus 16 uint8_t's
aes_context aes128_cbc_dec_start(const uint8_t* key, const void* iv){
bcal_cbc_ctx_t* ctx = (bcal_cbc_ctx_t*)malloc(sizeof(bcal_cbc_ctx_t));
uint8_t r = bcal_cbc_init(&aes128_desc, key, 128, ctx);
if (r) {
free(ctx);
return NULL;
}
bcal_cbc_loadIV(iv, ctx);
return (aes_context)ctx;
}

// decrypt one or more blocks of 128bit data


// data_len should be mod 16
void aes128_cbc_dec_continue(const aes_context ctx, void* data, const uint16_t
data_len){
if (data_len % 16 != 0) {
return;
}
bcal_cbc_ctx_t* _ctx = (bcal_cbc_ctx_t*)ctx;
uint16_t msg_blocks = data_len / 16;
while(msg_blocks--){
bcal_cbc_decNext(data, _ctx);
data = (uint8_t*)data + _ctx->blocksize_B;
}
}
// cleanup decryption context
void aes128_cbc_dec_finish(const aes_context ctx){
bcal_cbc_free((bcal_cbc_ctx_t*)ctx);
free(ctx);
}

*/

You might also like