-
-
Slip3.js 27 Aug 2016
The previous code is reworked into an object named Slip.
The Serial.on('data') can then invoke the parsing of data in the Slip object.
As characters are parsed in the Slip object, the emit() function is used to forward data to a new Slip.on('data') function. Try the example at different baud rates. The code follows://slip3.js //26 Aug 2016 //Written on a PICO //For testing connect Tx to RX for loopback, pins B6 and B7 Serial1 //SLIP protocol // https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol var END="\xC0"; var ESC="\xdb"; var ESC_END="\xdc"; var ESC_ESC="\xdd"; // END+Packet+END //if the END byte occurs in the data to be sent, the two byte sequence // ESC, ESC_END is sent instead, //if the ESC byte occurs in the data, the two byte sequence ESC, ESC_ESC // is sent. function Slip(port){ // this.sss=""; this.State=0; this.Port=port; this.sendEND=function(){this.Port.write(END);}; this.sendESC=function(){this.Port.write(ESC);}; this.sendESC_END=function(){this.Port.write(ESC_END);}; this.sendESC_ESC=function(){this.Port.write(ESC_ESC);}; } Slip.prototype.sendPacket=function(A){ var i; this.sendEND(); for(i=0;i<A.length;i++){ switch(A.charAt(i)){ case END: this.sendESC(); this.sendESC_END(); break; case ESC: this.sendESC(); this.sendESC_ESC(); break; default: this.Port.write(A.charAt(i)); break; }//end switch }//next i this.sendEND(); }; Slip.prototype.parsnip=function(data){ var i; console.log("Port.on= "+data);console.log(" "); for(i=0;i<data.length;i++){ switch(this.State){ case 0://looking for END to start packet //console.log("0"); if(data.charAt(i)===END){ //this.sss=""; this.State=10; i++; } return; case 10://get the packet //console.log("10"); if(data.charAt(i)===END){ this.State=0; return; } this.State=20; return; case 20://look for ESC //console.log("20"); if(data.charAt(i)===ESC){this.State=30;return;} this.emit('data',data.charAt(i)); this.State=10; return; case 30://ESC was seen //console.log("30"); if(data.charAt(i)===ESC_END){ this.emit('data',END); this.State=10; return; } if(data.charAt(i)===ESC_ESC){ this.emit('data',ESC); this.State=10; return; } return; default: console.log("default error"); break; }//end switch }//next i }; /////////////////////////// function test(){ var Port=Serial1; //var Port=Serial2; //try different baud rates 300, 1200,9600,19200,115200 var Baudrate=115200; /////////////////// var P=new Slip(Port); Port.setup(Baudrate); Port.on('data', function (data){ P.parsnip(data); }); //add sss and i properties to the P instance of Slip P.sss=""; P.i=0; P.on('data',function(data){ P.sss+=data; for(P.i=0;P.i<data.length;P.i++){ if(data.charAt(P.i)==='\r'){ print(P.sss);P.sss=""; } } }); var text="Hello"+ESC+"World"+END+"Eagles\n\r"; var text1="Sally\n\r"; var text2="Mary Lamb\n\r"; console.log("Text= "+text); P.sendPacket(text); console.log("Text1= "+text1); P.sendPacket(text1); console.log("Text2 "+text2); P.sendPacket(text2); }//end test test();
The output:
>echo(0); Text= HelloÛWorldÀEagles Text1= Sally Text2 Mary Lamb =undefined Port.on= ÀHelloÛÝWorldÛÜEagles ÀÀSally ÀÀMary Lamb À HelloÛWorldÀEagles Sally Mary Lamb
-
Slip1.js 26 Aug 2016
A program to send and receive data packets in SLIP format.
Written for a PICO
https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol//slip1.js //26 Aug 2016 //Written on a PICO //For testing connect Tx to RX for loopback, pins B6 and B7 Serial1 //SLIP protocol // https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol var END=0xC0; var ESC=0xdb; var ESC_END=0xdc; var ESC_ESC=0xdd; // END+Packet+END //if the END byte occurs in the data to be sent, the two byte sequence // ESC, ESC_END is sent instead, //if the ESC byte occurs in the data, the two byte sequence ESC, ESC_ESC // is sent. var baudrate=115200; var Port=Serial1; //Port.setup(baudrate); function Packet(){ this.A=""; } var P=new Packet(); P.on('data',function(data){ console.log("P.on= "+data); }); //var sss=""; var State=0; Port.on('data', function (data){ var i; console.log("Port.on= "+data); for(i=0;i<data.length;i++){ switch(State){ case 0://looking for END to start packet //console.log("0"); if(data.charCodeAt(i)===END){ State=10; i++; } return; case 10://get the packet //console.log("10"); if(data.charCodeAt(i)===END){ State=0; P.emit('data',P.A); i++; return; } State=20; return; case 20://look for ESC //console.log("20"); if(data.charCodeAt(i)===ESC){State=30;i++;return;} P.A+=data.charAt(i); State=10; return; case 30://ESC was seen //console.log("30"); if(data.charCodeAt(i)===ESC_END){ P.A+=String.fromCharCode(END); State=10; return; } if(data.charCodeAt(i)===ESC_ESC){ P.A+=String.fromCharCode(ESC); State=10; return; } return; default: console.log("default error"); break; }//end switch }//next i }); function sendEND(){ Port.write(String.fromCharCode(END)); } function sendESC(){ Port.write(String.fromCharCode(ESC)); } function sendESC_END(){ Port.write(String.fromCharCode(ESC_END)); } function sendESC_ESC(){ Port.write(String.fromCharCode(ESC_ESC)); } function sendPacket(A){ var i; sendEND(); for(i=0;i<A.length;i++){ switch(A.charCodeAt(i)){ case END: sendESC(); sendESC_END(); break; case ESC: sendESC(); sendESC_ESC(); break; default: Port.write(A.charAt(i)); break; }//end switch }//next i sendEND(); } function test(){ var text= "Hello"+String.fromCharCode(ESC)+"World"+String.fromCharCode(END)+ "Eagles"; Port.setup(baudrate); console.log("Text= "+text); sendPacket(text); }//end test test();
The output:
>echo(0); Text= HelloÛWorldÀEagles =undefined Port.on= ÀHelloÛÝWorldÛÜEaglesÀ P.on= HelloÛWorldÀEagles
-
KryptoMessage3.js 25 Aug 2016
For use on Espruino Pico device
Requires: RNG.js, AEScmac.js, permutation.js (see posts above for files)
FlashEEPROM.js from Espruino web site.
Uses serial port 1 (can be changed in configuration at top) in a hardware loopback.
Connect pins B6 and B7 together.
Reads analog input on Pin B1.
There are a number of flag variables at the top of the program:
Flag to suppress server text to console:
var showserver=0;
//var showserver=1;
Where to get the cryptographic keys:
//var KeysFromRom=0; //Generate keys as before this point
var KeysFromRom=1;//read keys from ROM see previous post for the key generator and squirt programs to insert the keys into the EEROM
Serial port parameters:
//var useserialflag=0;
var useserialflag=1;
var baudrate=115200;
var Port=Serial1;
Flags used in the Msg transport function:
//var EncryptionFlag=false;
var EncryptionFlag=true;
//var PermutationFlag=false;
var PermutationFlag=true;
ReplayFlag controls the recording of msgs for replay:
var ReplayFlag=true;
//var ReplayFlag=false;
This version redirects the console in order to create a menu on the USB port to that the program can be operated from a terminal program or from the left pane of the WebIDE.
BE SURE TO TYPE 0 TO MAKE THE RUNNING PROGRAM RESTORE THE CONSOLE TO THE USB PORT! This will avoid having to reset the PICO by cycling the power.
The Menu:Select using digit and return key 1 Connect 6 Cshow 2 Logoff 7 LEDon 3 Creplay 8 LEDoff 4 Creset 9 Read ADC 5 Sshow 0 Exit
Connect starts the client to server connection.
It asks for the User ID and Password Id= Sam, PW= 1234
(unless you change these in the key generator program)
Logoff tells the server that connect will be needed for access.
Creplay is used after a session to replay client messages for testing
Creset resets the replay buffers
Sshow prints the recorded server messages
Cshow prints the recorded client messages
LEDon, LEDoff operate the LED on the PICO.
Read Adc reads a 0 to 4096 (12 bits) value from pin B1.
And finally Exit restores the console to the USB port.
/*
Todo add0x32 message if wrong Id and password
Todo rework PICO commands into a User client and server functions
Todo add code for lexlevel 1 to generate keys on the client and send them to the server
Todo split into server and client versions on two different PICOs
Todo connect server and client PICOs using serial port
*//*
Done see if serial1 and serial2 on PICO can be used to connect
the server and client portions of this program
Solution use serial1 in loopback with flag
Done write a program that generates keys and writes the to ROM
Done read the keys from the ROM
Done add 0x30 and 0x31 code for server bad msg, client not authentic
Done add blend function to msg send and recieve functions
Done add code to do menu on USB allows terminal program to operateSelect using digit and return key 1 Connect 6 Cshow 2 Logoff 7 LEDon 3 Creplay 8 LEDoff 4 Creset 9 Read ADC 5 Sshow 0 Exit <- USB >1 do connect Client builds and sends inital message Permutated Encrypted Message= 60,d4,88,53,63,4e,c1,43,d,94,61,c,e6,37,14,e8, a3,68,bd,c0,cd,c0,2a,e8,a4,3a,7e,aa,dd,82,91,4f, 1d,66,74,41,b7,90,45,97,7,51,13,8e,ab,7f,2c,c, 45,2,e3,4f,e3,8e,c3,aa,ec,a6,e1,f9,ab,c7,4f,93, 3b,a6,a7,61,14,3b,25,90,9b,32,4f,d4,54,70,f,d8, Enter User ID process server decrypt Server replies to connect message Permutated Encrypted Message= 8b,cf,f9,2b,85,10,79,ca,9d,3d,c9,80,19,c4,ed,5b, 2,bf,c0,5,74,fb,9e,2f,88,5f,c5,11,ba,8d,31,51, 9,e9,78,90,24,e0,35,8c,e6,a7,eb,de,69,b8,57,9a, 19,f1,f1,9f,ee,f9,6e,48,e1,d9,3a,8c,1a,7e,bc,68, 33,7b,3a,e2,39,b4,a3,61,ca,98,fd,aa,ec,d8,6f,a5, server transmits process client 80 decrypt Client checks the server message Client check of server msg OK Client checks if the server is authentic Server is Authentic Enter User ID Sam Enter Password 1234 Login using Sam and 1234 Client login message Permutated Encrypted Message= 1f,f0,25,77,2,b5,d0,13,cf,63,8e,b9,ef,fe,95,e8, 73,ae,f9,88,a3,d4,0,56,32,d5,f9,c0,4c,6,5b,d9, 7c,e4,c,ef,64,ee,2c,a3,cf,dc,c3,86,d3,f,2f,3f, 57,c4,98,31,b6,ad,47,81,3a,21,ae,3f,90,22,57,69, 7c,d0,da,0,54,88,34,7c,f0,5f,ef,a0,c5,a7,e,ab, process server decrypt Permutated Encrypted Message= 63,28,3e,c4,c5,3,b5,f0,7d,66,81,c8,74,99,5e,4f, 6,a3,46,29,49,a5,e,bf,79,b7,64,4f,98,4c,c4,34, 75,46,7c,7c,5e,a0,1f,af,32,9d,e6,13,2b,66,b9,18, 54,32,83,19,63,ff,3e,f1,de,ab,77,ab,f,f2,65,fb, d8,f0,49,e9,80,16,ee,e7,b,bd,fd,63,4d,48,49,e7, server transmits process client 80 decrypt Client checks the server message Client check of server msg OK Client checks if the server is authentic Server is Authentic Client sees msg 0x42 Select using digit and return key 1 Connect 6 Cshow 2 Logoff 7 LEDon 3 Creplay 8 LEDoff 4 Creset 9 Read ADC 5 Sshow 0 Exit
-
Alas I'm using Windows 7 and the days of XP are numbered
The SLIP protocol is simple enough./* //SLIP protocol // https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol var END=0xC0; var ESC=0xdb; var ESC_END=0xdc; var ESC_ESC=0xdd; // END+Packet+END //if the END byte occurs in the data to be sent, the two byte sequence // ESC, ESC_END is sent instead, //if the ESC byte occurs in the data, the two byte sequence ESC, ESC_ESC // is sent. */
PPP link:
https://en.wikipedia.org/wiki/Point-to-Point_ProtocolLCP link:
https://en.wikipedia.org/wiki/Link_Control_Protocol
SLIP protocol link:
https://en.wikipedia.org/wiki/Serial_Line_Internet_Protocol
CHAP protocol link:
https://en.wikipedia.org/wiki/Challenge-Handshake_Authentication_ProtocolAnother link:
https://en.wikibooks.org/wiki/Serial_Programming/IP_Over_Serial_ConnectionsSetting up PPP on Windows 7
https://mikebeach.org/2012/08/30/installing-and-configuring-a-ppp-null-modem-connection-on-windows-7/So when I try to start the PPP dial-up from windows, I captured the following:
// with window trying to dial up Xdump(); CLIENTCLIENTCLIENTCLIENTecho(0); console.log("<","<<",JSON.stringify(process.env),">>",">");echo(1); 4 //disconnect and reconnect Webide only Xdump(); echo(0); console.log("<","<<",JSON.stringify(process.env),">>",">");echo(1); 4
Windows is sending CLIENTCLIENTCLIENTCLIENT
So what do I need to reply? Dig into the RFC's and read the Vogon poetry for a while? -
The problem with using a browser is no local file or I/O access.
Around the year 2000, I found Simple Server, and used it to access RTU's via serial ports, access a database etc. It served HTML pages so that I could access the RTUs and database.
Some promising links from Googling "Simple Server" :https://github.com/balupton/simple-server
https://www.npmjs.com/package/simple-server
http://techngr.com/t-mtn-simple-server-download-and-settings
http://www.analogx.com/contents/download/Network/sswww/Freeware.htm
For PPP this link looks promising
https://github.com/adamdunkels/contiki-1.x/blob/master/contiki/ppp/ppp.c
-
Keygenerator1.js creates keys for the Kryptomessage protocol and writes them to EEROM in the Pico.
The screen output at the end is copied into the
Squirt1.js program.
Copy and paste the buff[] and IDPWlist[] arrays into the Squirt program
Loading and running Squirt1 can then write the same keys into other Pico devices.
KeyFromRom1.js is used to read the keys from the EEROM and will be incorporated into the Kryptomessage.js at a later time. -
URL to IP address is usually done by a DNS server.
https://en.wikipedia.org/wiki/Domain_Name_SystemFor IOT devices you may need to set up a local DNS for them.
https://en.wikipedia.org/wiki/BIND
https://www.isc.org/downloads/bind/
http://www.zytrax.com/books/dns/
Your router may need to add the local DNS server to its list of DNS servers.See the DNS references on this page to register a device's IP with the DNS.
http://www.espruino.com/ESP8266_WifiUsage -
An even better version is attached.
Why is this being done?
It is a way of providing an end user menu interface so they don't need to know Espruino.
Typing LEDon() and LEDoff() or other such commands is replace by a menu selection.This is basic work for the crypto-message project that I'm working on in another project.
Ideally I'd like to have the crypto-client Pico plug into a USB port and serve up a webpage to the computer using PPP over the USB port. The client clicks on buttons on the web page that get sent to the Client Pico. The Client Pico sends the crypto-message via Wi-Fi to the Server Pico. The reverse path applies as well.
The Server Pico decrypts the crypto-message, validating the message integrity and authentication, and then performs the requested task.
Do the Espruino Wi-Fi functions take care of the PPP connection, or do I need to add code that does the initial handshake and setup? -
Some improvements:
//Loopback3.js //22 Aug 2016 //use the left pane of the WebIde or //use putty or other termial program on com assigned to USB //use webide to load and run the program //disconnect webide //start putty on com port at 115200,8 no flow control //typing in putty should appear followed by connect in putty screen var Sp1=USB; var Sp2=LoopbackA; function run(){ Sp1.setup(115200); Sp2.setup(115200); LoopbackB.setup(115200); LoopbackB.setConsole(); } function menu(){ Sp1.print("Enter 1 to turn the LED on:\n\r"); Sp1.print("Enter 2 to turn the LED off:\n\r"); Sp1.print("Enter 0 to Exit:\n\r"); } Sp1.on('data', function (data) { Sp1.print(data+"\n\r"); switch(data){ case "1"://LED on Sp2.print("digitalWrite(B12,1);\n\r"); Sp1.print("digitalWrite(B12,1);\n\r"); menu(); break; case "2"://LED off Sp2.print("digitalWrite(B12,0);\n\r"); Sp1.print("digitalWrite(B12,0);\n\r"); menu(); break; case"0"://Exit Sp2.print("USB.setConsole();\n\r"); }//end switch }); function test(){ run(); menu(); } var a=setInterval (function () { test(); clearInterval(a); }, 1000);
The output:
>echo(0); =undefined -> LoopbackB Enter 1 to turn the LED on: Enter 2 to turn the LED off: Enter 0 to Exit: 1 digitalWrite(B12,1); Enter 1 to turn the LED on: Enter 2 to turn the LED off: Enter 0 to Exit: 2 digitalWrite(B12,0); Enter 1 to turn the LED on: Enter 2 to turn the LED off: Enter 0 to Exit: 0 <- LoopbackB =undefined =undefined >1 =1 >
-
Loopback2.js 22 Aug 2016
Used the Loopback devices to allow the USB port to act as program input.
Commands sent to the USB port trigger sub commands that get sent to the Espruino console.
0: Exit,
1: LEDon
2: LEDoff
The left pane of the WebIde or a terminal program connected to the USB port can be used to send the commands.//Loopback2.js //22 Aug 2016 //use the left pane of the WebIde or //use putty or other termial program on com assigned to USB //use webide to load and run the program //disconnect webide //start putty on com port at 115200,8 no flow control //typing in putty should appear followed by connect in putty screen var Sp1=USB; var Sp2=LoopbackA; Sp1.setup(115200); Sp2.setup(115200); LoopbackB.setup(115200); LoopbackB.setConsole(); Sp1.print("Enter 1 to turn the LED on:\n\r"); Sp1.print("Enter 2 to turn the LED off:\n\r"); Sp1.print("Enter 0 to Exit:\n\r"); Sp1.on('data', function (data) { Sp1.print(data+"\n\r"); switch(data){ case "1"://LED on Sp2.print("digitalWrite(B12,1);\n\r"); Sp1.print("digitalWrite(B12,1);\n\r"); break; case "2"://LED off Sp2.print("digitalWrite(B12,0);\n\r"); Sp1.print("digitalWrite(B12,0);\n\r"); break; case"0"://Exit Sp2.print("reset();\n\r"); Sp2.print("USB.setConsole();\n\r"); Sp1.print("USB.setConsole();\n\r"); }//end switch });
The output:
>echo(0); Enter 1 to turn the LED on: Enter 2 to turn the LED off: Enter 0 to Exit: echo(1) 1 digitalWrite(B12,1); 2 digitalWrite(B12,0); 1 digitalWrite(B12,1); 2 digitalWrite(B12,0); 1 digitalWrite(B12,1); 2 digitalWrite(B12,0); 1 digitalWrite(B12,1); 2 digitalWrite(B12,0); 1 digitalWrite(B12,1); 2 digitalWrite(B12,0); 1 digitalWrite(B12,1); 2 digitalWrite(B12,0); 0 USB.setConsole();
-
A fix using atob() and btoa() functions.
//jsontest3.js //21aug2016 function A(){ this.A=1; this.B= new Uint8Array(16); this.s= new Uint8Array(16); } A.prototype.setup=function(){ var i; for(i=0;i<16;i++)this.B[i]=i; }; function B(){ this.B= 2; // new Uint8Array(16); this.s= 2; //new Uint8Array(16); this.A=1; } var a=new A(); a.setup(); console.log(a); //a fix a.B=btoa(a.B); a.s=btoa(a.s); console.log(a); // var e=JSON.stringify(a); console.log(e); var f=JSON.parse(e); console.log("f= ",f); //a fix f.B=E.toUint8Array(atob(f.B)); f.s=E.toUint8Array(atob(f.s)); console.log("f= ",f); console.log(" "); var b=new B(); console.log(b); var e1=JSON.stringify(b); console.log(e1); var f1=JSON.parse(e1); console.log("f1= ",f1);
The output:
>echo(0); { "A": 1, "B": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]), "s": new Uint8Array(16) } { "A": 1, "B": "AAECAwQFBgcICQoLDA0ODw==", "s": "AAAAAAAAAAAAAAAAAAAAAA==" } {"A":1,"B":"AAECAwQFBgcICQoLDA0ODw==","s":"AAAAAAAAAAAAAAAAAAAAAA=="} f= { "A": 1, "B": "AAECAwQFBgcICQoLDA0ODw==", "s": "AAAAAAAAAAAAAAAAAAAAAA==" } f= { "A": 1, "B": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]), "s": new Uint8Array(16) } { "B": 2, "s": 2, "A": 1 } {"B":2,"s":2,"A":1} f1= { "B": 2, "s": 2, "A": 1 }
-
Almost a fix!
Replace the Uint8Array using E.toString()
do the JSON
Replace the strings using E.toUint8Array()//jsontest1.js //21aug2016 function A(){ this.A=1; this.B= new Uint8Array(16); this.s= new Uint8Array(16); } A.prototype.setup=function(){ var i; for(i=0;i<16;i++)this.B[i]=i; }; function B(){ this.B= 2; // new Uint8Array(16); this.s= 2; //new Uint8Array(16); this.A=1; } var a=new A(); a.setup(); console.log(a); //Almost a fix a.B=E.toString(a.B); a.s=E.toString(a.s); console.log(a); // var e=JSON.stringify(a); console.log(e); var f=JSON.parse(e); console.log("f= ",f); //Almost a fix f.B=E.toUint8Array(f.B); f.s=E.toUint8Array(f.s); console.log("f= ",f); console.log(" "); var b=new B(); console.log(b); var e1=JSON.stringify(b); console.log(e1); var f1=JSON.parse(e1); console.log("f1= ",f1);
The output:
>echo(0); { "A": 1, "B": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]), "s": new Uint8Array(16) } { "A": 1, "B": "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\x0B\f\r\x0E\x0F", "s": "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" } {"A":1,"B":"\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\x0B\f\r\x0E\x0F","s":"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"} f= { "A": 1, "B": "\x00\x01\x02\x03\x04\x05\x06a\b\t\n\x0B\f\r\x0E\x0F", "s": "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" } f= { "A": 1, "B": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 97, 8, 9, 10, 11, 12, 13, 14, 15]), "s": new Uint8Array(16) } { "B": 2, "s": 2, "A": 1 } {"B":2,"s":2,"A":1} f1= { "B": 2, "s": 2, "A": 1 }
Problem the 7 gets changed to 97
-
Problem with var x=JSON.parse(JSON.stringify(Obj));
When Obj contains an Uint8Array(N). x is undefined/function A(){ this.A=1; this.B= new Uint8Array(16); this.s= new Uint8Array(16); } A.prototype.setup=function(){ var i; for(i=0;i<16;i++)this.B[i]=i; }; function B(){ this.B= 2; // new Uint8Array(16); this.s= 2; //new Uint8Array(16); this.A=1; } var a=new A(); a.setup(); console.log(a); var e=JSON.stringify(a); console.log(e); var f=JSON.parse(e); console.log("f= ",f); console.log(" "); var b=new B(); console.log(b); var e1=JSON.stringify(b); console.log(e1); var f1=JSON.parse(e1); console.log("f1= ",f1);
The output:
>echo(0); { "A": 1, "B": new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]), "s": new Uint8Array(16) } {"A":1,"B":new Uint8Array([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]),"s":new Uint8Array(16)} f= undefined { "B": 2, "s": 2, "A": 1 } {"B":2,"s":2,"A":1} f1= { "B": 2, "s": 2, "A": 1 }
-
KryptoMessage1.js
20 Aug 2016
The left pane of the WebIDE is used as the client input.
Run the program and type help(); to list the commands.
Added encryption and permutation options to the message transport
Set the flags at the top of this program to enable these features.
This cryptographic protocol uses the permutation. RNG, AES, and AEScmac cryptographic primitives.
require("RNG") ("AEScmac") ("permutation")
The AEScmac is used to validate the entire message in MsgCmac.
The AEScmac is applied to the previously sent random number (Rnd.) and compared to
the Auth block to validate that the sender of the message possesses the keys and is authentic.
The server contains a list of user IDs, an AEScmac( ID,password), and a lexical level.
The client has to enter a user ID and password. The AESmac is calculated and AES encrypted using a key derived from an AES encryption of the random number in the last server message. A lexical level of zero allows access to the LED on and LED off commands. A lexical level of one is to be used to allow transfer of cryptographic keys
AES and AEScmac algorithms are NIST approved.
This implementation of AES and AEScmac are not NIST certified
The RNG (random number generators) are two different instances with different keys on the client and server and do not need to be synchronized and they can be randomized at random intervals.
// flags used in the Msg transport function
//var EncryptionFlag=true;
//var PermutationFlag=true;
var EncryptionFlag=false;
var PermutationFlag=false;
// ReplayFlag controls the recording of msgs for replay
var ReplayFlag=true;
//var ReplayFlag=false; -
Good work dealing with stepper motors at this level. I did a CNC drill for PC boards a few years back but I used the EMC2 software to control the steps to the motors on the 3 axis.
It occurred to me that an optical lever could be of use.
Attach clamp glue the motor frame to the table.
Attach a small plane mirror to the motor shaft so the flat of the mirror is parallel to the shaft.
From a few feet away fix a LASER pointer to the table so that the beam hits the mirror.
The reflected beam should make a spot on the wall.
Mark the spot on the wall.
Now step the motor one step and see how much the beam moves on the wall.
A full 360 degrees of steps should align the beam back on the wall spot..
The EMC2 website:
http://linuxcnc.org/
Use a parallel PC port to control direction and steps on 4 axis and uses G code files.
G code:
https://en.wikipedia.org/wiki/G-code
I used the coordinates in the Eagle drill files and a C++ program to create the G-code file for my drill.
G-code on Arduino
http://reprap.org/wiki/Arduino_GCode_Interpreter -
Message6.js
19 Aug 2016
Added encryption and permutation options to the message transport.
Set the flags at the top of this program to enable these features.
Additional module require("permutation")The function that creates the permutation array has been modified.
Let N=16, then there are 15*13*11*9*7*5*3 = 2,027,025 combinations
If N=80, then there are 79*77*75*…*3 = 7.9777941814E+58 combinationsSample output with the flags set as follows:
// flags used in the Msg transport function
var EncryptionFlag=false;
var PermutationFlag=false;In left pane enter test1(); =undefined >test1(); Auth,Mhash, and Transport keys f9,77,1,73,1d,86,4d,40,82,1e,29,89,0,f8,5,2c, 56,d3,9,f8,63,58,d5,35,80,63,0,6f,91,e3,56,ff, ad,7d,48,da,84,3d,d,31,5,e9,52,9d,1e,4,93,bb, Client builds and sends inital message Message= 41,40,7d,c2,d1,9b,2,a6,43,c1,24,3,6d,29,d0,6a, e2,f5,5a,9a,b0,f7,4f,fe,9f,73,17,f7,46,22,d4,53, 88,cc,f4,32,59,a1,13,4c,c3,61,eb,95,11,7c,a3,ac, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 6,d0,7,b0,2e,c7,3a,95,ec,39,14,76,2c,32,51,9d, Server checks the message
Sample output with the flags set as follows:
// flags used in the Msg transport function
var EncryptionFlag=true;
var PermutationFlag=false;In left pane enter test1(); =undefined >test1(); Auth,Mhash, and Transport keys f,96,3d,11,4,a2,3b,f8,9a,3c,4a,13,95,62,bc,49, 7c,ec,7e,a,a7,a6,e1,39,d,44,dc,8b,89,c6,38,36, 43,97,71,b7,48,f4,a9,d0,3b,57,1c,18,33,2a,22,85, Client builds and sends inital message Sending Encrpyted Message= ae,a9,64,60,ee,39,bb,4a,54,81,fc,dd,a6,1,28,34, ca,3b,e3,a3,2,d8,d2,69,6c,94,da,c8,6f,31,5a,a5, 4,4a,5e,97,83,a2,5c,e6,f1,dc,69,a9,29,c3,18,b, 49,5d,99,ab,36,22,32,34,45,d5,f4,1,65,9b,61,ad, 38,59,c2,a5,27,30,6,30,df,f0,1f,35,41,b7,17,83, decrypt Message= 41,df,1d,24,2d,c7,5f,57,64,56,48,d9,cf,6,49,d8, b4,a8,9f,48,c3,73,7d,a5,27,58,8,a8,17,95,e4,2f, c8,56,14,5a,bc,d9,35,69,6d,91,9a,85,e8,59,f8,d0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 48,71,67,d0,3d,7a,89,10,88,7a,4c,ac,f2,ba,85,c8, Server checks the message
Sample output with the flags set as follows:
// flags used in the Msg transport function
var EncryptionFlag=false;
var PermutationFlag=true;
The permutation rearranges the byte order in the message.
Can you find the 0x41?In left pane enter test1(); =undefined >test1(); Auth,Mhash, and Transport keys e1,b4,52,64,3c,68,ab,fc,c7,94,66,90,b6,ad,b7,a5, 29,b3,82,22,c3,d5,5e,36,97,a6,39,e3,46,77,4a,f1, bd,6e,76,7,f,d6,37,24,8f,10,40,c2,5f,6,49,e7, Client builds and sends inital message Sending Permutated Message= 6d,0,ef,98,0,a8,66,ea,ed,41,a8,51,fc,9,5b,8f, 0,22,a6,0,1e,d5,e,ae,94,0,0,0,0,0,88,0, 61,8,ce,0,1b,66,c5,58,7a,d4,cb,3,5b,66,4a,0, 68,b4,f5,98,9e,0,54,51,10,b4,0,58,c1,30,50,34, f8,3e,0,66,71,cf,f2,d4,74,91,36,c5,81,0,3d,0, Message= 41,c1,d4,cf,b4,81,9,ea,94,6d,c5,f2,3d,66,3e,0, 54,a6,22,98,91,3,71,61,ed,58,f5,30,68,34,ce,10, ae,58,88,8f,1b,66,f8,8,cb,ef,7a,d5,d4,4a,66,9e, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, c5,5b,51,74,e,98,51,5b,66,1e,36,a8,a8,50,fc,b4, Server checks the message
Sample output with the flags set as follows:
// flags used in the Msg transport function
var EncryptionFlag=true;
var PermutationFlag=true;In left pane enter test1(); =undefined >test1(); Auth,Mhash, and Transport keys 6,ce,ee,5b,9e,2b,7c,1c,6,cd,a2,b4,71,be,e,68, fe,67,2a,fc,85,17,2f,66,7c,4b,42,a1,e7,de,d9,46, 19,4d,1,69,61,72,64,e0,17,3b,fb,4c,2e,9e,96,f4, Client builds and sends inital message Sending Permutated Message= 23,7a,a2,ba,15,8b,9,a2,ce,7d,6a,32,83,bb,20,48, fb,ef,65,8d,79,cf,97,53,2e,30,69,98,c1,8a,62,89, fe,8a,f9,b5,6,d8,6f,cd,e4,82,17,e0,51,28,5f,72, d6,db,61,5e,cb,5,7a,4,7f,bd,9b,e4,24,1a,7e,d5, 6e,52,ad,a8,34,b7,6b,27,c7,be,e8,96,8b,34,94,1c, Sending Encrpyted Message= 8d,b5,e4,fe,32,8a,1c,ce,a2,6,6a,15,fb,5,d5,ad, 83,51,6f,23,82,2e,e0,5e,cf,7e,c7,8a,1a,8b,62,f9, ba,98,89,7a,7d,9b,65,6b,a2,79,34,97,ef,52,be,7f, 61,b7,d6,53,4,bb,bd,cb,72,7a,d8,34,a8,c1,30,20, 27,28,48,24,17,db,cd,6e,69,5f,94,8b,96,e4,e8,9, decrypt Message= 41,10,bf,23,b,4c,7,f8,a5,d9,9f,d5,f4,d7,23,6a, ec,59,7c,62,5e,3f,a,1f,94,c1,af,ed,d5,a5,38,4, 59,48,4d,c6,70,c7,90,7d,b,a8,7a,f1,53,48,99,4b, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, b8,85,cc,6,a4,2b,db,b,26,58,de,8a,8e,5,dc,ea, Server checks the message
-
Message4.js 18 Aug 2016
Testing the RNG and AEScmac cryptographic primitives for use in the cryptographic protocol.
require("RNG")
require("AEScmac")
The AEScmac is used to validate the entire message in Mhash
The AEScmac is applied to the previously sent Rnd and compared to
The Auth block to validate that the sender of the message possesses
The keys and is authentic.
The AEScmac is NIST approved
The RNG (random number generators) are two different instances with different keys on the client and server and do not need to be synchronized
They can be randomized at random intervals.
The output of Message4.jsAuth and Mhash keys 97,79,5c,b2,76,68,8,ea,88,a9,11,b3,ca,a7,d8,d, 3a,8b,60,3c,70,61,9c,7,99,3e,c,c3,65,dd,b1,ff, Client builds and sends inital message Message= 41,31,fd,1e,8f,21,41,d6,f2,33,90,16,be,c5,b0,75, 94,84,49,64,37,1b,74,7,b7,81,62,95,6c,3b,98,da, 6,6,a7,8c,7e,ff,21,29,d8,4a,e3,60,e4,a6,d,21, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ab,c3,87,86,83,e8,c6,47,e4,41,fa,62,43,70,56,a, Server checks the message checking Mhash Valid Server builds and sends reply message Message= 41,86,93,5b,ee,5f,f1,c9,b4,59,5e,42,f3,72,cf,c2, da,f9,db,87,d0,b,47,6b,e4,59,c8,26,59,9c,5e,2f, b0,9b,f7,e0,70,16,ed,bd,fe,5c,18,de,a5,25,eb,e3, 8,a9,a5,1d,59,55,37,c3,45,bc,65,37,fe,12,34,50, 2f,a6,4e,35,94,3f,77,fc,89,45,68,ec,fe,15,b6,62, Client checks the message checking Mhash Valid checking Auth Valid Client builds and sends reply message Message= 42,9,63,23,50,5f,4b,0,d1,bd,c0,32,cc,e1,2a,e9, 21,cd,fd,f,f8,4,cc,57,c0,b3,50,c1,fc,1b,1a,7, dc,98,81,85,9d,5e,e5,b1,d,e,21,ea,bd,2c,53,17, c3,9b,80,a8,bb,f1,22,27,9c,e4,71,3a,6,21,62,d8, 59,f8,7,1f,ba,d7,3a,6b,7b,b8,13,10,5c,e7,53,a7, Server checks the message checking Mhash Valid checking Auth Valid Server builds and sends reply message Message= 42,68,9a,41,25,a1,f7,76,30,ce,a,88,82,bc,e5,75, 50,8f,4e,1e,2f,97,64,c8,68,2d,9,68,77,98,ff,de, 6c,c,3c,6,46,d6,8d,76,64,47,ac,a8,76,eb,ce,3c, b9,39,9,51,80,da,7c,95,c2,4e,6e,d9,71,7,97,82, 66,72,34,4a,2a,54,61,39,a,a3,3e,cd,c,90,75,4e,
-
Run this on a PICO. See previous post to explain a CMAC.
The CMAC is working as a module. AEScmac.js
testAEScmac.js uses the module to run the NIST test values.
There are two tests.
Test one expects one long array as input.
Test two allows 16 byte blocks in different locations to be pointed to as input.
In test two if there is a partial block at the end, remember to pad the block. (see the test code for the 320 bit example. Test one inserts the padding.
The output of testAEScmac.jsAEScmac from one large array keys 2b,7e,15,16,28,ae,d2,a6,ab,f7,15,88,9,cf,4f,3c, fb,ee,d6,18,35,71,33,66,7c,85,e0,8f,72,36,a8,de, f7,dd,ac,30,6a,e2,66,cc,f9,b,c1,1e,e4,6d,51,3b, Example 1 null message bb,1d,69,29,e9,59,37,28,7f,a3,7d,12,9b,75,67,46, Valid Example 2 , 128 bit message 7,a,16,b4,6b,4d,41,44,f7,9b,dd,9d,d0,4a,28,7c, Valid example 3, 320 bit message df,a6,67,47,de,9a,e6,30,30,ca,32,61,14,97,c8,27, Valid example 4, 512 bit message 51,f0,be,bf,7e,3b,9d,92,fc,49,74,17,79,36,3c,fe, Valid AEScmac from Uint8Arrays keys 2b,7e,15,16,28,ae,d2,a6,ab,f7,15,88,9,cf,4f,3c, fb,ee,d6,18,35,71,33,66,7c,85,e0,8f,72,36,a8,de, f7,dd,ac,30,6a,e2,66,cc,f9,b,c1,1e,e4,6d,51,3b, Example 1 null message bb,1d,69,29,e9,59,37,28,7f,a3,7d,12,9b,75,67,46, Valid Example 2 , 128 bit message 7,a,16,b4,6b,4d,41,44,f7,9b,dd,9d,d0,4a,28,7c, Valid example 3, 320 bit message df,a6,67,47,de,9a,e6,30,30,ca,32,61,14,97,c8,27, Valid example 4, 512 bit message 51,f0,be,bf,7e,3b,9d,92,fc,49,74,17,79,36,3c,fe, Valid
-
For better understanding:
Cryptographic Protocol vs Cryptographic Primatives
https://en.wikipedia.org/wiki/Cryptographic_protocol
https://en.wikipedia.org/wiki/Cryptographic_primitivePrimitives used:
Block Cipher: Notation is CIPHkey(M), where CIPH = AES128, using key, on block M
https://en.wikipedia.org/wiki/Advanced_Encryption_StandardCMAC: Notation CMAC(key,M)
https://en.wikipedia.org/wiki/CBC-MAC
https://en.wikipedia.org/wiki/One-key_MAC
http://csrc.nist.gov/publications/nistpubs/800-38B/SP_800-38B.pdf
https://github.com/allan-stewart/node-aes-cmacPermutations: Notation Perm(P,M) for bit level, Permbyte(P,M)
http://courses.cs.vt.edu/~cs1044/fall02/mcquain/Projects/4/PermuCrypt.pdf
http://www.mathplanet.com/education/algebra-2/discrete-mathematics-and-probability/permutations-and-combinationsRandom Number Generator: Notation RNG(key)
The protocol: ( note the “||” indicates concatenation of blocks)
A message is composed of four 16 byte blocks followed by a 16 byte MAC,
The blocks are denoted as:
Text1=RNG(keyrng) but Text1[0] contains crypto command, other bytes can be payload
Text2=RNG(keyrng), or CMAC(keyIdPw,ID || Password), can be payload if not IDPW
RND=RNG(keyrng),
AUTH=CMAC(keyauth,RND in previous message),
and
MAC = CMAC(keymac,Text1 || Text2 || RND || AUTH )
Bit and byte permutations can be applied as well as sending the message using a stream cipher.I am currently working on porting the allan-stewart/node-aes-cmac onto the Pico in preparation of a rewrite of the protocol implementation.
-
AES is used in the hash function and the password functions. The Pico seems to do the AES without using the library. For the SHA functions the library has to be "required". If this is a problem please let me know.
The key exchange still needs to be implemented. I favor calling it the "Squirt" function after the "technical" term I heard when hanging out in a committee. A Diffie-Helman key exchange or AES? The method really depends on how many clients. One client and one server or many clients (one at a time likely) and one or more servers.
A transport layer needs to be added. TLS would add the information hiding of a stream cipher and if used with a digital certificate it would add authentication. Some reading on TLS says it can be made to authenticate both ends, although it commonly only authenticates the server.
The transport layer could also add permutations to swap bytes and bits around in the message.
It's harder search for a key given known plaintext and cipher text if the bits have been repositioned. (Get out your solar powered calculator and ask it to do 128 factorial and watch the Sun dim.)As for using the SHA algorithms, one must consider the results of a Google search such as SHA256 decrypt
For example:
https://md5hashing.net/hash/sha256 -
I'm trying to create and implement a cryptographic protocol that makes it difficult to replay, modify or spoof command messages and the reply messages. Both the server and client are together on one Pico for the development phase. At some point I would like to implement to server and client on separate devices with a suitable communication link.
I've run into problems with the one way function and am seriously considering its elimination.
It is not needed or could be replaced with one of the SHA functions.(SHA1, SHA256 etc.) -
Some changes to the oneway function:
A test program oneway16bit.js was created. (type go(); in the left pane after loading)
It was found that the oneway add and xor modes produce the same results. The xor mode doesn’t require the modit function. The add mode produces a lot of hash collisions. As written with a seed of all 1’s the xor and add modes produce 4 collisions over a field of 1024 values.
This results are shown in oneway16bit1024.xlxs.The program was modified oneway16bita.js. (type go(); in the left pane after loading)
The function is seeded with the initial data in place of all 1’s.
This change eliminated the collisions in the xor mode.
The results are shown in oneway16bita1024.xlxs.
The changes have been applied in cryptomsg5.js -
Cryptographic Parameters Used in cryptomsg4.js
The objects that use cryptographic parameters:Random
1.1. AES1 flag 0 or 1
1.2. AES2 flag 0 or 1
1.3. Pdir1 0 or 1
1.4. Pdir2 0 or 1
1.5. Owtype 0, 1, or 2
1.6. Perm1 128 bytes
1.7. Perm2 128 bytes
1.8. Key1 16 bytes
1.9. Key2 16 bytes
293 bytesMyhash
2.1. AES1 flag 0 or 1
2.2. AES2 flag 0 or 1
2.3. Pdir1 0 or 1
2.4. Pdir2 0 or 1
2.5. Owtype 0, 1, or 2
2.6. Perm1 128 bytes
2.7. Perm2 128 bytes
2.8. Key1 16 bytes
2.9. Key2 16 bytes
293 bytesClient only:
One Random object 293 bytesServer only:
One Random object 293 bytes
Shared copies on Client and Server:.permIDPW 128 bytes
4 sets of hash parametersMsgHash 293 bytes
AuthHash 293 bytes
IDPWHash 293 bytes
IDPWkeyHash 293 byes
1210 bytes
Total cryptographic bytes in server or client = 1210+292 = 1503 bytes.
Since the client and server have independent Random objects in the cryptomsg5.js, it would be possible to periodically generate new cryptographic parameters. Perhaps this rekey could be implemented with a setInterval() function using a random interval.
Additional cryptographic entropy could be added to the message by the following means:
Currently the text[1] part of the message only carries the encrypted ID+password hash and is cleared to zero for all other messages. If in the zero cases, text[1] contained a random number, and a 32 element permutation of bytes over the text[1] and Rnd blocks the entropy would be increased. A 256 element permutation of bits could also be used, or do the 32 byte permutation followed by 128 bit permutation on each resulting block.
A key exchange password could allow the text[1] to be used to move keys in a covert manner that would appear to be normal operation.
The protocol could require the ID password hash be validated on every logged in command instead of just once.
In the first call to a hash, two parameters are initialized. Cryptographic salt could be used for the initial values.
Note on the direction flags:
The direction flags make use of the bidirectional properties of AES and the permutations.
AES1 flag 0 or 1
AES2 flag 0 or 1
Pdir1 0 or 1
Pdir2 0 or 1
Plaintext can be encrypted to cipher text A and the decrypted back to plaintext, or
Plaintext can be decrypted to cipher text B and then encrypted back to plaintext.
A not equal to B.
A similar process occurs with the permutation function.
It would be possible to use bits in the RND field to modulate the flags dynamically.The low cryptographic entropy problem:
A commercial device allows remote operation of a lock.
The device is controlled by two messages Lock and Unlock.
The messages differ by one bit in the message and by several bits in the CRC or hashed check code.
A modem using a stream cipher is used to send the Lock and Unlock messages.
The stream cipher makes uses of synchronized random number generators to exclusive or a different random byte with each byte of the message. A=R1 ^ M1, M1=A^R1.
If an attacker can intercept and modify the message, show that he could use:
B= M1^M2, C=B^A to change a Lock to an Unlock or an Unlock to a Lock command.
The stream cipher produces cryptographic confusion but no diffusion. A permutation function can be used to introduce diffusion ( swapping the bits/bytes around in the message) Note that AES in block mode produces both confusion and diffusion but produces the same output for the same input making the message subject to replay without additional steps.Link from the cryptographic anxiety closet: (Once a can of worms is opened the only process that works is to contain them is a larger can,)
https://securityevaluators.com/knowledge/case_studies/rfid/
ServerKM1.js and ClientKM1.js 29 Aug. 2016
KryptoMessage3.js has been divided into the server and client programs that run on two different PICOs.
I used the Squirt.js to install the cryptographic keys on both PICOs.
To use connect the serial1 ports on each PICO to the other PICO.
P1 B6 to P2 B7, and P1 B7 to P2 B6. Connect the PICO grounds if they are to run on separate computers. I ran both from one computer.
Using WebIDE load and run one PICO with ServerKM1.js. If on one computer disconnect from the server PICO and connect to the remaining client PICO. Load and run Client KM1.js
Notice item 5 of the menu has been removed as there is no access to the sever code on the client.