//-------------------------------------------------------------------------- // Copyright 2016, RIoT International Pty Ltd // // @author Aaron Ardiri //-------------------------------------------------------------------------- // microTLS - none/xor128 //-------------------------------------------------------------------------- // configuration //-------------------------------------------------------------------------- const char WiFi_SSID[] PROGMEM = "xxxx"; const char WiFi_PASS[] PROGMEM = "xxxx"; const uint8_t SERVER[] PROGMEM = { xxx, xxx, xxx, xxx }; #define SERVER_URI "/xxx/index.php" #define SERVER_NAME "xxx.xxxxxxxx.xxx" #define CLIENT_GUID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #define CLIENT_INTERVAL 300 // 5 minutes //-------------------------------------------------------------------------- // pre-shared keys //-------------------------------------------------------------------------- // none //-------------------------------------------------------------------------- // modules //-------------------------------------------------------------------------- #include "microTLS-module-xor128.h" #define SESS_SECURITY "none" #define PING_SECURITY "xor-128" //-------------------------------------------------------------------------- // additional globals //-------------------------------------------------------------------------- typedef struct xGlobals { uint8_t xorkey[XOR_KEYLEN]; } xGlobals; xGlobals gx; //-------------------------------------------------------------------------- // entry point //-------------------------------------------------------------------------- void microTLS_setup() { } //-------------------------------------------------------------------------- // session: creation //-------------------------------------------------------------------------- void microTLS_sess_prepare() { HashEngine ctx_hash; int ofs; SYS_RAM_CHECK; // we must prepare our send buffer g.scratch.send.len = 0; ofs = g.scratch.token.len + g.scratch.param.len + g.scratch.send.len + g.scratch.recv.len; g.scratch.send.buf = &g.scratch.reserved[ofs]; // copy the token to the send buffer, server expects it g.scratch.send.len = MIN(g.scratch.token.len, CLIENT_SCRATCH_LEN - ofs); memcpy((char *)g.scratch.send.buf, (char *)g.scratch.token.buf, g.scratch.send.len); // we need to generate an integrity hash for the buffer we are sending hashInit(&ctx_hash); hashUpdate(&ctx_hash, g.scratch.send.buf, g.scratch.send.len); hashFinal(&ctx_hash); g.scratch.send.digest = ctx_hash.digest; } bool microTLS_sess_process() { // did we receive a key? if (g.scratch.recv.len == XOR_KEYLEN) { // this is our XOR key memcpy(gx.xorkey, g.scratch.recv.buf, g.scratch.recv.len); } return false; } //-------------------------------------------------------------------------- // session: update //-------------------------------------------------------------------------- const char msg[] PROGMEM = "sending a message over microTLS"; void microTLS_ping_prepare() { HashEngine ctx_hash; XOREngine ctx_xor; int ofs; SYS_RAM_CHECK; // we must prepare our send buffer g.scratch.send.len = 0; ofs = g.scratch.token.len + g.scratch.param.len + g.scratch.send.len + g.scratch.recv.len; g.scratch.send.buf = &g.scratch.reserved[ofs]; // copy the message to the send buffer g.scratch.send.len = MIN(strlen_P(msg), CLIENT_SCRATCH_LEN - ofs); strncpy_P((char *)g.scratch.send.buf, msg, g.scratch.send.len); // we need to generate an integrity hash for the buffer we are sending hashInit(&ctx_hash); hashUpdate(&ctx_hash, g.scratch.send.buf, g.scratch.send.len); hashFinal(&ctx_hash); g.scratch.send.digest = ctx_hash.digest; // we must encode the data in scratch using xor XORinitState(&ctx_xor, gx.xorkey); XORcrypt(&ctx_xor, (char *)g.scratch.send.buf, g.scratch.send.len); } bool microTLS_ping_process() { XOREngine ctx_xor; // we must decode the data in scratch using xor XORinitState(&ctx_xor, gx.xorkey); XORcrypt(&ctx_xor, (char *)g.scratch.recv.buf, g.scratch.recv.len); return true; } //--------------------------------------------------------------------------