Commit ce64b224 authored by Arduino Enigma's avatar Arduino Enigma

Update CipherSaber.ino

parent c79334a8
//Arduino CipherSaber-2 (http://www.ciphersaber.com)
//@arduinoenigma 2019
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <TimerOne.h>
#define MAXRANDOMSAMPLES 10
......@@ -34,13 +39,16 @@ struct ARCFOUR_t /*xls*/
struct MenuData_t
{
byte State = 0;
byte NextState = 0;
byte SkipNewline = 0;
byte Rounds[2];
byte PrintIV;
byte PrintCount;
byte ExitCount;
byte IVRead;
byte PrintIV = 0;
byte PrintCount = 0;
byte ExitCount = 0;
byte IVRead = 0;
byte Hex[2];
byte HexRead;
byte HexRead = 0;
} MenuData;
......@@ -162,7 +170,8 @@ void ARCFOURSetKey(byte *PassPhrase, byte *IV, byte Loops)
Serial.print(i);
Serial.print(' ');
Serial.println((char)c);
Serial.print((char)c);
Serial.print(F("\x0a\x0d"));
ARCFOUR.Key[i] = c;
......@@ -179,7 +188,8 @@ void ARCFOURSetKey(byte *PassPhrase, byte *IV, byte Loops)
{
Serial.print(i);
Serial.print(' ');
Serial.println(IV[j]);
Serial.print(IV[j]);
Serial.print(F("\x0a\x0d"));
ARCFOUR.Key[i] = IV[j];
i++;
......@@ -189,7 +199,8 @@ void ARCFOURSetKey(byte *PassPhrase, byte *IV, byte Loops)
ARCFOUR.KeyEnd = i;
Serial.print(F("ke:"));
Serial.println(i);
Serial.print(i);
Serial.print(F("\x0a\x0d"));
ARCFOUR.n = Loops;
}
......@@ -322,20 +333,20 @@ void CipherSaberTests()
//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(Pass, IV, 1);
ARCFOURInit();
ARCFOURPrint();
DoARCFOUR(file);
ARCFOURSetKey(Pass3, IV3, 1);
ARCFOURInit();
......@@ -356,7 +367,7 @@ void ShowNewRandom()
for (byte i = 0; i < MAXRANDOMSAMPLES; i++)
{
Serial.print(PROBABLYRANDOM.random[i]);
Serial.print(' ');
Serial.print(F(" "));
}
Serial.print(F("\x0d\x0a"));
PROBABLYRANDOM.samples = 0;
......@@ -401,9 +412,9 @@ void ShowSerial()
if (k != 255)
{
Serial.println(k);
Serial.print(k);
Serial.print(F("\x0a\x0d"));
}
}
......@@ -433,9 +444,6 @@ byte HexToByte(byte *HexByte)
nibble = (HexByte[i] | 32) - 'a' + 10;
}
//Serial.print(nibble);
//Serial.print(' ');
hex += nibble * weigh;
weigh >>= 4;
......@@ -447,20 +455,27 @@ byte HexToByte(byte *HexByte)
void DoMenu()
{
static byte state = 0;
static byte nextstate = 0;
static byte skipnewline = 0;
byte key = 0;
key = Serial.read();
byte key = Serial.read();
if (key == 10)
{
key = 13;
}
if ((skipnewline) && (key != 255))
if (key == 13)
{
MenuData.ExitCount++;
}
if (MenuData.ExitCount == 4)
{
MenuData.ExitCount = 0;
Serial.print(F("\x0a\x0d\x0a\x0d"));
MenuData.State = 0;
}
if ((MenuData.SkipNewline) && (key != 255))
{
if (key == 13)
{
......@@ -468,36 +483,42 @@ void DoMenu()
}
else
{
skipnewline = 0;
MenuData.SkipNewline = 0;
}
}
switch (state)
switch (MenuData.State)
{
case 0:
{
Serial.print(F("(D)ecrypt or (E)ncrypt?: "));
state = 1;
MenuData.ExitCount = 0;
MenuData.State = 1;
break;
}
case 1:
{
MenuData.ExitCount = 0;
if ((key | 32) == 'e')
{
FillARCFOURIV();
Serial.print(F("E\x0a\x0d"));
skipnewline = 1;
nextstate = 8;
state = 2;
MenuData.SkipNewline = 1;
MenuData.NextState = 8;
MenuData.State = 2;
}
if ((key | 32) == 'd')
{
Serial.print(F("D\x0a\x0d"));
skipnewline = 1;
nextstate = 6;
state = 2;
MenuData.SkipNewline = 1;
MenuData.NextState = 6;
MenuData.State = 2;
}
break;
......@@ -506,20 +527,25 @@ void DoMenu()
case 2:
{
Serial.print(F("Enter Passphrase: "));
MenuData.ExitCount = 0;
ARCFOUR.KeyEnd = 0;
state = 3;
MenuData.State = 3;
break;
}
case 3:
{
MenuData.ExitCount = 0;
if (key != 255)
{
if (key == 13)
{
Serial.print(F("*********************\x0a\x0d"));
skipnewline = 1;
state = 4;
MenuData.SkipNewline = 1;
MenuData.State = 4;
}
else
{
......@@ -533,14 +559,18 @@ void DoMenu()
case 4:
{
Serial.print(F("How Many Rounds: "));
MenuData.ExitCount = 0;
MenuData.Rounds[0] = '0';
MenuData.Rounds[1] = '0';
state = 5;
MenuData.State = 5;
break;
}
case 5:
{
MenuData.ExitCount = 0;
if (key != 255)
{
if (key == 13)
......@@ -548,8 +578,9 @@ void DoMenu()
ARCFOUR.n = (MenuData.Rounds[0] - '0') * 10 + (MenuData.Rounds[1] - '0');
Serial.print(ARCFOUR.n);
Serial.print(F("\x0a\x0d"));
skipnewline = 1;
state = nextstate;
MenuData.SkipNewline = 1;
MenuData.State = MenuData.NextState;
}
else
{
......@@ -560,17 +591,21 @@ void DoMenu()
}
}
}
break;
}
// start of decode logic
// START OF DECODE LOGIC
case 6:
{
Serial.print(F("Enter Text to Decode:\x0a\x0d"));
Serial.print(F("Enter Text to Decode (send empty lines to end):\x0a\x0d"));
MenuData.ExitCount = 0;
MenuData.IVRead = 0;
MenuData.HexRead = 0;
state = 7;
MenuData.PrintCount = 0;
MenuData.State = 7;
break;
}
......@@ -580,12 +615,13 @@ void DoMenu()
{
if (key == 13)
{
Serial.print(F("\x0a\x0d"));
skipnewline = 1;
//state = nextstate;
MenuData.ExitCount = 0;
MenuData.SkipNewline = 1;
}
else
{
MenuData.ExitCount = 0;
if (((key >= '0') && (key <= '9')) || (((key | 32) >= 'a') && ((key | 32) <= 'f')))
{
MenuData.Hex[MenuData.HexRead++] = key;
......@@ -595,11 +631,8 @@ void DoMenu()
if (MenuData.IVRead < 10)
{
ARCFOUR.IV[MenuData.IVRead++] = HexToByte(MenuData.Hex);
//Serial.print(MenuData.IVRead);
//Serial.println('+');
if (MenuData.IVRead == 10)
{
//Serial.println('!');
ARCFOURAppendIVtoKey();
ARCFOURInit();
}
......@@ -607,13 +640,16 @@ void DoMenu()
else
{
Serial.print((char)ARCFOUREncrypt(HexToByte(MenuData.Hex)));
MenuData.PrintCount++;
if (MenuData.PrintCount == 40)
{
Serial.print(F("\x0a\x0d"));
}
}
//Serial.println(HexToByte(MenuData.Hex));
MenuData.HexRead = 0;
}
//Serial.print((char)key);
}
}
}
......@@ -625,9 +661,11 @@ void DoMenu()
case 8:
{
MenuData.ExitCount = 0;
if (FillARCFOURIV())
{
state = 9;
MenuData.State = 9;
}
break;
}
......@@ -641,33 +679,16 @@ void DoMenu()
ARCFOURAppendIVtoKey();
ARCFOURInit();
//ARCFOURPrint(); // remove when done debugging
/*
// 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(' ');
*/
state = 10;
MenuData.State = 10;
break;
}
case 10:
{
Serial.print(F("Enter Text to Encode: (5 empty lines ends) \x0a\x0d"));
state = 11;
Serial.print(F("Enter Text to Encode (send empty lines to end):\x0a\x0d"));
MenuData.ExitCount = 0;
MenuData.State = 11;
break;
}
......@@ -677,12 +698,13 @@ void DoMenu()
{
if (key == 13)
{
skipnewline = 1;
//Serial.print(F("\x0a\x0d"));
//state = nextstate;
MenuData.ExitCount = 0;
MenuData.SkipNewline = 1;
}
else
{
MenuData.ExitCount = 0;
if (MenuData.PrintIV)
{
MenuData.PrintIV = 0;
......@@ -691,7 +713,7 @@ void DoMenu()
for (byte i = 0; i < 10; i++)
{
PrintHex(ARCFOUR.IV[i]);
Serial.print(' ');
Serial.print(F(" "));
}
}
......@@ -704,16 +726,14 @@ void DoMenu()
}
else
{
Serial.print(' ');
Serial.print(F(" "));
}
}
}
break;
}
}
}
......@@ -722,6 +742,8 @@ void setup() {
Serial.begin(9600);
Serial.print(F("Arduino CipherSaber-2 20191201\x0d\x0a\x0d\x0a"));
PROBABLYRANDOMSetup();
//CipherSaberTests();
......@@ -733,5 +755,4 @@ void loop() {
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