//-------------------------------------------------------------------------- // Copyright 2016, RIoT International Pty Ltd // // @author Aaron Ardiri //-------------------------------------------------------------------------- // microTLS - none/rc4 //-------------------------------------------------------------------------- // 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-rc4.h" #define SESS_SECURITY "none" #define PING_SECURITY "rc4" //-------------------------------------------------------------------------- // additional globals //-------------------------------------------------------------------------- typedef struct xGlobals { uint8_t rc4key[RC4_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 == RC4_KEYLEN) { // this is our RC4 key memcpy(gx.rc4key, 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; RC4Engine rc4; int param; int ofs; SYS_RAM_CHECK; // how many bytes should we throw away? param = 2048 + xrand(); // we must prepare our param buffer g.scratch.param.len = 0; ofs = g.scratch.token.len + g.scratch.param.len + g.scratch.send.len + g.scratch.recv.len; g.scratch.param.buf = &g.scratch.reserved[ofs]; // store the parameter value in the buffer u32toa((uint32_t)param, (char *)g.scratch.param.buf); g.scratch.param.len = strlen((char *)g.scratch.param.buf); // 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; // encode our message (in place) rc4initState(&rc4, gx.rc4key, RC4_KEYLEN); rc4crypt(&rc4, param, (uint8_t *)g.scratch.send.buf, g.scratch.send.len); } bool microTLS_ping_process() { RC4Engine rc4; int param; SYS_RAM_CHECK; // what is the value stored in the param buffer? param = (int)atou32((char *)g.scratch.param.buf, g.scratch.param.len); // decode our message (in place) rc4initState(&rc4, gx.rc4key, RC4_KEYLEN); rc4crypt(&rc4, param, (uint8_t *)g.scratch.recv.buf, g.scratch.recv.len); return true; } //--------------------------------------------------------------------------