#include <AESLib.h>
#include <string.h>
/*
int SIZE = 128;
char tram_aes[128];
char msg_completed[128];
char key[128];
char nonce[32];
short ctr = 0;
char tram[32];
char cipher[32];
char plaintext[32];
char msg[32];
*/
short fibo0;
short fibo1;
char nonce[15] = "0123456789abcde";
char trame_aes[16];
char key[16] = "abcdefghijklmnop";
char blockCipher[16];
char cipherText[16];
char plainText[16];
char counter;
uint32_t interval;
//Adds a 2 bytes counter from the byte number "n" of "tram"
//Commonly used to add the counter at the end of the "none"
void addCTR(char tram[], char nonce[], unsigned short ctr, int n)
{
for (int i=0;i<n-1;i++)
{
tram[i] = nonce[i];
}
tram[n-1] = ctr>>8;
tram[n] = ctr & 0xff;
}
void AES(char cipher[], char tram[], int key){
}
//Operates a xor between cipher, from the position "nCipher-nMsg" to "nCipher", and nMsg, from the position "0" to "nMsg".
//Commonly used to operate a xor between end of "cipher" and the first part of the message until the start of the CTR part.
void xorMsg(char plaintext[], int nPlaintext, char cipher[], int nCipher, char msg[], int nMsg)
{
for (int i=nCipher - nMsg; i < nCipher; i++)
{
plaintext[i] = cipher[i] ^ msg[i];
}
}
//repeat the addCTR on the plaintext and bam!
/*
void initialise_tram(char tra
m[], int size_tram){
for (int i=0; i<size_tram; i++){
tram[i] = 'a';
}
}
void complete_tram(char tram[], uint16_t compteur){
memcpy(tram,&compteur,2);
}
uint16_t get_compteur(char tram[]){
uint16_t compteur;
memcpy(&compteur, tram, 2);
return compteur;
}
void encode_decode(char msg[], char tram[], int size_msg){
for (int i = 2; i<size_msg;i++){
msg[i] = msg[i] ^ tram[i];
}
}
void complete_msg(char msg_completed[], char msg[], uint16_t compteur, int size_msg){
memcpy(msg_completed, &compteur, 2);
memcpy(msg_completed + 2, msg, size_msg);
}
uint16_t extract_msg(char msg[], char msg_cnt[], int size_msg){
uint16_t compteur;
memcpy(&compteur, msg_cnt, 2);
memcpy(msg, msg_cnt, size_msg);
return compteur;
}
*/
/*
void setString(char* dest, char* origin,int size)
{
for (int i = 0; i<size; i++)
{
dest[i] = origin[i];
}
}
*/
void setCipherText(char* cipherText, short fibo1, char counter)
{
for (int i =0; i<16; i++)
{
cipherText[i] = '\0';
}
snprintf(cipherText, 16, "fibo: %d", fibo1);
cipherText[15] = counter;
}
void setTrame_aes(char * trame_aes, char * nonce, char counter)
{
for (int i =0; i<15; i++)
{
trame_aes[i] = nonce[i];
}
trame_aes[15] = counter;
}
void setBlockCipher(char * blockCipher,char * key,char * trame_aes )
{
}
void setPlainText(char * plainText, char * cipherText, char * blockCipher)
{
for (int i =0; i < 16; i++)
{
Serial.print("i = ");
Serial.println(i);
Serial.print("Valeur de l'élément i de cipherText: ");
Serial.println(cipherText[i],BIN);
Serial.print("Valeur de l'élément i de blockCipher: ");
Serial.println(blockCipher[i],BIN);
plainText[i] = cipherText[i] ^ blockCipher[i];
Serial.print("Valeur de l'élément i de plainText: ");
Serial.println(plainText[i],BIN);
}
}
void printCharArray(char * trame)
{
for (int i =0; i<16; i++)
{
Serial.print((int)trame[i]);
Serial.print("; ");
}
Serial.println("");
}
void setup(){
Serial.begin(9600);
counter = 0;
fibo0 = 0;
fibo1 = 1;
interval = 2000;
//initialise_tram(tram, 128);
//complete_tram(tram, 1);
//AES(tram, 1);
}
void loop(){
// the next time that you want to do your action
static uint32_t nextTime;
// check if it's time
if (millis() - nextTime >= interval)
{
Serial.println("Nonce:");
printCharArray(nonce);
Serial.println("Key:");
printCharArray(key);
// update next time
nextTime += interval;
setCipherText(cipherText, fibo1, counter);
Serial.println("CipherText:");
printCharArray(cipherText);
fibo1 = fibo1 + fibo0;
fibo0 = fibo1 - fibo0;
setTrame_aes(trame_aes, nonce, counter);
Serial.println("Trame_aes:");
printCharArray(trame_aes);
//setPlainText(plainText, cipherText , blockCypher);
counter++;
// do your action here
}
/*
char msg[] = "coucou";
encode_decode(msg, tram, 7);
char msg_cnt[9];
complete_tram(msg_cnt, 1);
char msg1[7];
int c = extract_msg(msg1, msg_cnt, 7);
encode_decode(msg1, tram, 7);
Serial.print("Message reçu n° ");
Serial.print(c);
Serial.println(" : ");
Serial.println(msg1);
for (int i=0;i<32;i++)
{
nonce[i] = i;
}
addCTR(tram, nonce, 0xffff, 31);
Serial.println("tram:");
for (int i=0;i<32;i++)
{
Serial.print("[");
Serial.print((int)tram[i]);
Serial.print("],");
}
Serial.println("");
*/
}