Commit a8ff2a42 authored by Arduino Enigma's avatar Arduino Enigma

encode is outputting correct data, does not exit though

parent 5bec88da
......@@ -4,6 +4,8 @@
#include <TimerOne.h>
#define MAXRANDOMSAMPLES 10
struct PROBABLYRANDOM_t
{
byte sample = 0;
......@@ -20,15 +22,25 @@ struct PROBABLYRANDOM_t
struct ARCFOUR_t /*xls*/
{
byte IV[10];
byte Key[256];
byte KeyEnd;
byte KeyEnd = 0;
byte State[256];
byte i;
byte j;
byte n;
byte i = 0;
byte j = 0;
byte n = 0;
} ARCFOUR;
struct MenuData_t
{
byte Rounds[2];
byte PrintIV;
byte PrintCount;
byte ExitCount;
} MenuData;
// Watchdog Timer Interrupt
ISR(WDT_vect)
{
......@@ -65,6 +77,7 @@ byte rotl(const byte value, int shift) {
}
// call often, fills out the random array
void PROBABLYRANDOMCollect()
{
if (PROBABLYRANDOM.sample_waiting) {
......@@ -93,24 +106,31 @@ void PROBABLYRANDOMCollect()
}
void ARCFOURPrint()
{
byte i = 0;
byte i;
i = 0;
Serial.print(F("IV: "));
do
{
Serial.print(ARCFOUR.IV[i++]);
Serial.print(' ');
} while (i < 10);
Serial.print(F("\x0d\x0a"));
Serial.print(F("S: "));
i = 0;
Serial.print(F("S: "));
do
{
Serial.print(ARCFOUR.State[i++]);
Serial.print(' ');
} while (i);
Serial.print(F("\x0d\x0a"));
}
void ARCFOURClear()
{
ARCFOUR.i = 0;
......@@ -124,6 +144,7 @@ void ARCFOURClear()
} while (ARCFOUR.i);
}
void ARCFOURSetKey(byte *PassPhrase, byte *IV, byte Loops)
{
byte i = 0;
......@@ -162,8 +183,6 @@ void ARCFOURSetKey(byte *PassPhrase, byte *IV, byte Loops)
j++;
} while (j < 10);
//i--;
ARCFOUR.KeyEnd = i;
Serial.print(F("ke:"));
......@@ -244,6 +263,7 @@ byte ARCFOUREncrypt(byte in)
return out;
}
void DoARCFOUR(byte *file)
{
byte v;
......@@ -263,15 +283,8 @@ void DoARCFOUR(byte *file)
}
void readPass()
void CipherSaberTests()
{
}
void setup() {
// put your setup code here, to run once:
byte Pass[246] = {'a', 's', 'd', 'f', 'g', 0};
byte IV[10] = {0x6f, 0x6d, 0x0b, 0xab, 0xf3, 0xaa, 0x67, 0x19, 0x03, 0x15};
......@@ -290,39 +303,43 @@ void setup() {
0x19, 0xad, 0x00
};
Serial.begin(9600);
PROBABLYRANDOMSetup();
// bad data, does not decode
byte Pass3[246] = {'t', 'e', 's', 't'};
byte IV3[10] = {0x43, 0x91, 0xe8, 0xec, 0xcd, 0x8b, 0x7d, 0x98, 0x27, 0x45};
byte file3[] = {0xda, 0xea, 0x54, 0x65, 0x00};
Serial.println(sizeof(Pass));
//Serial.println(sizeof(Pass));
ARCFOURSetKey(Pass, IV, 1);
ARCFOURInit();
ARCFOURPrint();
DoARCFOUR(file);
//ARCFOURSetKey(Pass, IV, 1);
//ARCFOURInit();
//ARCFOURPrint();
//DoARCFOUR(file);
ARCFOURSetKey(Pass, IV2, 10);
ARCFOURInit();
ARCFOURPrint();
DoARCFOUR(file2);
//ARCFOURSetKey(Pass, IV2, 10);
//ARCFOURInit();
//ARCFOURPrint();
//DoARCFOUR(file2);
//ARCFOURSetKey(Pass, IV, 1);
//ARCFOURInit();
//ARCFOURPrint();
//DoARCFOUR(file);
ARCFOURSetKey(Pass3, IV3, 1);
ARCFOURInit();
ARCFOURPrint();
DoARCFOUR(file3);
ARCFOURClear();
ARCFOURPrint();
}
void loop() {
// put your main code here, to run repeatedly:
PROBABLYRANDOMCollect();
//if (PROBABLYRANDOM.samples == MAXRANDOMSAMPLES) // show 10 new values
if (PROBABLYRANDOM.samples == 1) // show new value propagating in circular queue
//
void ShowNewRandom()
{
if (PROBABLYRANDOM.samples == MAXRANDOMSAMPLES) // show 10 new values
//if (PROBABLYRANDOM.samples == 1) // show new value propagating in circular queue
{
for (byte i = 0; i < MAXRANDOMSAMPLES; i++)
{
......@@ -332,14 +349,350 @@ void loop() {
Serial.print(F("\x0d\x0a"));
PROBABLYRANDOM.samples = 0;
}
}
//call often, returns 1 when IV has been initialized
byte FillARCFOURIV()
{
byte v = 0;
static byte collecting = false;
if (collecting == false)
{
PROBABLYRANDOM.samples = 0;
collecting = true;
}
else
{
if (PROBABLYRANDOM.samples == MAXRANDOMSAMPLES) // show 10 new values, a full IV
{
for (byte i = 0; i < MAXRANDOMSAMPLES; i++)
{
ARCFOUR.IV[i] = PROBABLYRANDOM.random[i];
}
v = 1;
collecting = false;
PROBABLYRANDOM.samples = 0;
}
}
return v;
}
byte k;
k = Serial.read();
void ShowSerial()
{
byte k = Serial.read();
if (k != 255)
{
Serial.println(k);
}
}
\ No newline at end of file
}
void PrintHex(byte val)
{
byte nibble = (val & 0xf0) >> 4;
Serial.print((char)(nibble > 9 ? 'A' - 10 + nibble : '0' + nibble));
nibble = val & 0x0f;
Serial.print((char)(nibble > 9 ? 'A' - 10 + nibble : '0' + nibble));
}
byte HexToByte(byte *HexByte)
{
byte hex = 0;
byte nibble = 0;
byte weigh = 0x10;
for (byte i = 0; i < 2; i++)
{
if ((HexByte[i] >= '0') && (HexByte[i] <= '9'))
{
nibble = HexByte[i] - '0';
}
else if (((HexByte[i] | 32) >= 'a') && ((HexByte[i] | 32) <= 'f'))
{
nibble = (HexByte[i] | 32) - 'a' + 10;
}
Serial.print(nibble);
Serial.print(' ');
hex += nibble * weigh;
weigh >>= 4;
}
return hex;
}
void DoMenu()
{
static byte state = 0;
static byte nextstate = 0;
static byte skipnewline = 0;
byte key = 0;
key = Serial.read();
if (key == 10)
{
key = 13;
}
if ((skipnewline) && (key != 255))
{
if (key == 13)
{
return;
}
else
{
skipnewline = 0;
}
}
switch (state)
{
case 0:
{
Serial.print(F("(D)ecrypt or (E)ncrypt?: "));
state = 1;
break;
}
case 1:
{
if ((key | 32) == 'e')
{
FillARCFOURIV();
Serial.print(F("E\x0a\x0d"));
skipnewline = 1;
nextstate = 8;
state = 2;
}
if ((key | 32) == 'd')
{
Serial.print(F("D\x0a\x0d"));
skipnewline = 1;
nextstate = 6;
state = 2;
}
break;
}
case 2:
{
Serial.print(F("Enter Passphrase: "));
ARCFOUR.KeyEnd = 0;
state = 3;
break;
}
case 3:
{
if (key != 255)
{
if (key == 13)
{
Serial.print(F("*********************\x0a\x0d"));
skipnewline = 1;
state = 4;
}
else
{
ARCFOUR.Key[ARCFOUR.KeyEnd++] = key;
}
}
break;
}
case 4:
{
Serial.print(F("How Many Rounds: "));
MenuData.Rounds[0] = '0';
MenuData.Rounds[1] = '0';
state = 5;
break;
}
case 5:
{
if (key != 255)
{
if (key == 13)
{
ARCFOUR.n = (MenuData.Rounds[0] - '0') * 10 + (MenuData.Rounds[1] - '0');
Serial.print(ARCFOUR.n);
Serial.print(F("\x0a\x0d"));
skipnewline = 1;
state = nextstate;
}
else
{
if ((key >= '0') && (key <= '9'))
{
MenuData.Rounds[0] = MenuData.Rounds[1];
MenuData.Rounds[1] = key;
}
}
}
break;
}
// start of decode logic
case 6:
{
Serial.print(F("Enter Text to Decode: "));
state = 7;
break;
}
case 7:
{
if (key != 255)
{
if (key == 13)
{
Serial.print(F("\x0a\x0d"));
skipnewline = 1;
//state = nextstate;
}
else
{
Serial.print((char)key);
}
}
break;
}
// START of ENCODE LOGIC
case 8:
{
if (FillARCFOURIV())
{
state = 9;
}
break;
}
case 9:
{
MenuData.PrintIV = 1;
MenuData.PrintCount = 0;
MenuData.ExitCount = 0;
for (byte i = 0; i < 10; i++)
{
ARCFOUR.Key[ARCFOUR.KeyEnd++] = ARCFOUR.IV[i];
}
/*
// remove when done debugging
for (byte i = 0; i < 10; i++)
{
PrintHex(ARCFOUR.IV[i]);
Serial.print(' ');
}
Serial.println(' ');
for (byte i = 0; i < 255; i++)
{
PrintHex(ARCFOUR.Key[i]);
Serial.print(' ');
}
Serial.println(' ');
*/
ARCFOURInit();
//ARCFOURPrint(); // remove when done debugging
state = 10;
break;
}
case 10:
{
Serial.print(F("Enter Text to Encode: (5 empty lines ends) \x0a\x0d"));
state = 11;
break;
}
case 11:
{
if (key != 255)
{
if (key == 13)
{
skipnewline = 1;
//Serial.print(F("\x0a\x0d"));
//state = nextstate;
}
else
{
if (MenuData.PrintIV)
{
MenuData.PrintIV = 0;
MenuData.PrintCount = 10;
for (byte i = 0; i < 10; i++)
{
PrintHex(ARCFOUR.IV[i]);
Serial.print(' ');
}
}
MenuData.PrintCount++;
PrintHex(ARCFOUREncrypt(key));
if (MenuData.PrintCount > 20)
{
MenuData.PrintCount = 0;
Serial.print(F("\x0a\0x0d"));
}
else
{
Serial.print(' ');
}
}
}
break;
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
PROBABLYRANDOMSetup();
//CipherSaberTests();
}
void loop() {
// put your main code here, to run repeatedly:
PROBABLYRANDOMCollect(); // call often, exits fast
DoMenu();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment