|
|
@@ -2,14 +2,24 @@
|
|
|
#include "crypto.h"
|
|
|
#include "rng.h"
|
|
|
|
|
|
-#define MLEN 32
|
|
|
-
|
|
|
+#include <WiFi.h>
|
|
|
+#include <WiFiClient.h>
|
|
|
// Energia constructors
|
|
|
#include "Arduino.h"
|
|
|
-//#include "Energia.h"
|
|
|
+
|
|
|
+#define MLEN 32
|
|
|
+
|
|
|
void setup();
|
|
|
void loop();
|
|
|
|
|
|
+char ssid[] = "";
|
|
|
+char pass[] = "";
|
|
|
+
|
|
|
+uint16_t ID = 1050; // Device ID
|
|
|
+uint16_t port = 9000;
|
|
|
+IPAddress server(192,168,0,10);
|
|
|
+WiFiClient wclient;
|
|
|
+
|
|
|
uint8_t aes_key[16] = {
|
|
|
0x03, 0x02, 0x01, 0x00,
|
|
|
0x07, 0x06, 0x05, 0x04,
|
|
|
@@ -17,6 +27,11 @@ uint8_t aes_key[16] = {
|
|
|
0x0f, 0x0e, 0x0d, 0x0c
|
|
|
};
|
|
|
|
|
|
+typedef struct {
|
|
|
+ uint16_t ID;
|
|
|
+ uint16_t LEN;
|
|
|
+} header;
|
|
|
+
|
|
|
uint8_t AESData[MLEN], *AESResult, AESResult2[MLEN];
|
|
|
uint32_t AESDataLength=MLEN;
|
|
|
|
|
|
@@ -32,8 +47,22 @@ void setup() {
|
|
|
AESSetup(AES_KEY_128BIT, aes_key);
|
|
|
Serial.println("OK");
|
|
|
|
|
|
- String msg = "This is secret!";
|
|
|
- msg.toCharArray((char *)AESData, MLEN);
|
|
|
+ Serial.print("Setup WiFi ..");
|
|
|
+ WiFi.begin(ssid, pass);
|
|
|
+ while (WiFi.status() != WL_CONNECTED) {delay(200);}
|
|
|
+ Serial.println("OK");
|
|
|
+
|
|
|
+ Serial.print("Setup DHCP ..");
|
|
|
+ while (WiFi.localIP() == INADDR_NONE) {delay(200);}
|
|
|
+ Serial.print("OK ");
|
|
|
+ Serial.println(WiFi.localIP());
|
|
|
+
|
|
|
+ Serial.print("Setup Socket ..");
|
|
|
+ while(wclient.connect(server, port) == false) {delay(200);}
|
|
|
+ Serial.println("OK");
|
|
|
+
|
|
|
+ //String msg = "This is secret!";
|
|
|
+ //msg.toCharArray((char *)AESData, MLEN);
|
|
|
//
|
|
|
// Serial.print("KEY: ");
|
|
|
// for(int i=0;i<16;i++) {
|
|
|
@@ -56,16 +85,16 @@ void setup() {
|
|
|
// }
|
|
|
// Serial.println();
|
|
|
//
|
|
|
- AESResult = AESEncrypt(AESData, AESDataLength);
|
|
|
+ //AESResult = AESEncrypt(AESData, AESDataLength);
|
|
|
|
|
|
|
|
|
// AESCrypt(AES_DIR_ENCRYPT, aes_iv, AESData, AESResult, AESDataLength);
|
|
|
- Serial.print("Encrypted: ");
|
|
|
- for(uint32_t i=0;i<AESDataLength+getBlockSize();i++) {
|
|
|
- Serial.print(AESResult[i], HEX);
|
|
|
- Serial.print(' ');
|
|
|
- }
|
|
|
- Serial.println();
|
|
|
+ //Serial.print("Encrypted: ");
|
|
|
+ //for(uint32_t i=0;i<AESDataLength+getBlockSize();i++) {
|
|
|
+ //Serial.print(AESResult[i], HEX);
|
|
|
+ //Serial.print(' ');
|
|
|
+ //}
|
|
|
+ //Serial.println();
|
|
|
|
|
|
// AESCrypt(AES_DIR_DECRYPT, &aesv, AESResult, AESResult2, AESDataLength);
|
|
|
// Serial.print("Decrypted: ");
|
|
|
@@ -76,9 +105,36 @@ void setup() {
|
|
|
// Serial.println();
|
|
|
}
|
|
|
|
|
|
+uint8_t buffer[32] = {0};
|
|
|
+uint16_t bufferPnt = 0;
|
|
|
+
|
|
|
+void send() {
|
|
|
+ uint32_t resLen = 0;
|
|
|
+ Serial.print("encrypting..");
|
|
|
+ uint8_t *res = AESEncrypt(buffer, 32, &resLen);
|
|
|
+ Serial.println(resLen);
|
|
|
+ header h = {ID, resLen};
|
|
|
+ wclient.write((uint8_t *) &h, sizeof(h));
|
|
|
+ wclient.write(res, (uint16_t) resLen);
|
|
|
+ free(res);
|
|
|
+ bufferPnt = 0;
|
|
|
+}
|
|
|
+
|
|
|
void loop() {
|
|
|
while (Serial.available() > 0) {
|
|
|
- Serial.println(random(), HEX);
|
|
|
+ uint8_t val = Serial.read();
|
|
|
+ Serial.write(val); // echo
|
|
|
+ if ((val == 8 || val == 127) && bufferPnt > 0) {
|
|
|
+ bufferPnt--;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(val == 13) {
|
|
|
+ send();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(bufferPnt >= 255) send();
|
|
|
+ buffer[bufferPnt] = val;
|
|
|
+ bufferPnt++;
|
|
|
}
|
|
|
}
|
|
|
|