Aes encryption code

Dependencies:   Crypto USBDevice mbed

Fork of larada by MZJ

Revision:
5:7214d56ee5ae
Parent:
4:11520c01d65f
Child:
6:634306947b58
diff -r 11520c01d65f -r 7214d56ee5ae main.cpp
--- a/main.cpp	Wed Mar 09 09:37:50 2016 +0000
+++ b/main.cpp	Wed Mar 09 20:50:13 2016 +0000
@@ -1,7 +1,9 @@
 #include "mbed.h"
 #include "USBSerial.h"
 #include "IAP.h"
+#include "Crypto.h"
 
+#define USE_CIPHER
 //#define FAKE_HW
 
 #define SERIAL //comment for USB operation, uncomment for serial
@@ -41,6 +43,7 @@
 #define CRYPT_BUFF_SZ 16
 #define BYTES_PER_100_MS 8 // the number of bytes to send per 100 ms
 #define TIP_UPDATE_INTERVAL_S 3 // Update the tip remaining time every x seconds
+#define TIP_READ_BLANK_TIME_MS 70
 
 float filter = DEFAULT_FILTER;
 uint8_t initial_duty = DEFAULT_INITIAL_DUTY;
@@ -51,6 +54,9 @@
 uint16_t temp_limit_lower = DEFAULT_TEMP_LIMIT_LOWER;
 uint16_t temp_limit_upper = DEFAULT_TEMP_LIMIT_UPPER;
 
+unsigned char myIV[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+AES *myAES = NULL;
+
 enum State {
     IDLE,
     WAIT_FOR_TIP,
@@ -203,7 +209,7 @@
         return false; 
     } 
     
-    snprintf(buff, buff_sz, "%d:%ld\r\n", tm->command, tm->value);
+    snprintf(buff, buff_sz, "%d:%ld", tm->command, tm->value);
     
     return true;
 }
@@ -225,6 +231,20 @@
 }
 
 /**
+ * Sends the buffer one byte at a time to the tip
+ * 
+ * @param *buff - the buffer to send
+ * @param sz - size of the buffer
+ */
+void tip_send_buff(char *buff, int sz)
+{
+    for (int i = 0; i < sz; ++i)
+    {
+        pc.putc(buff[i]);
+    }
+}
+
+/**
  * Sends a message to the tip. This accounts for the
  * max number of bytes which can be sent per 100ms.
  *
@@ -233,14 +253,19 @@
  */
 bool send_message_to_tip(struct tip_message *tm)
 {
-    static char buff[CRYPT_BUFF_SZ] = {0};
-    static char send_buff[BYTES_PER_100_MS + 1] = {0};
+    char buff[CRYPT_BUFF_SZ] = {0};
+    char send_buff[BYTES_PER_100_MS] = {0};
+    
     static uint32_t last_send = 0;
     
     int bytes_sent = 0;
     
     pack_message(buff, sizeof(buff), tm);
     
+    #ifdef USE_CIPHER
+    myAES->encrypt((uint8_t*)buff, (uint8_t*)buff, sizeof(buff));
+    #endif
+    
     bytes_sent = 0;
     
     wait_until_can_send_tip_message(last_send);
@@ -251,33 +276,34 @@
         return true;   
     }
     
-    if (strlen(buff) > BYTES_PER_100_MS)
-    {
-        memcpy(send_buff, buff, BYTES_PER_100_MS);
+    /* The start & end of the message aren't encrypted to
+     * that we can recover from a failure in the txing 
+     * of a message.
+     */
+    pc.printf("!");
+    last_send = get_time();
+    
+    wait_until_can_send_tip_message(last_send);
+    
+    memcpy(send_buff, buff, BYTES_PER_100_MS);
         
-        pc.printf("!%s", send_buff); 
-        bytes_sent = BYTES_PER_100_MS;
-        last_send = get_time();
-        
-        while (bytes_sent < strlen(buff))
-        {
-            wait_until_can_send_tip_message(last_send);
+    tip_send_buff(send_buff, sizeof(send_buff)); 
+    bytes_sent = BYTES_PER_100_MS;
+    last_send = get_time();
+    
+    wait_until_can_send_tip_message(last_send);
             
-            memcpy(send_buff, buff+bytes_sent, BYTES_PER_100_MS);
-            pc.printf("%s", send_buff); 
-            bytes_sent = BYTES_PER_100_MS;
-            last_send = get_time(); 
-        }
-    }
-    else
-    {
-        pc.printf("!%s", buff); 
-        last_send = get_time(); 
-    }
+    memcpy(send_buff, buff+bytes_sent, BYTES_PER_100_MS);
+    tip_send_buff(send_buff, sizeof(send_buff)); 
+    bytes_sent = BYTES_PER_100_MS;
+    last_send = get_time(); 
     
+    // Send \r\n to terminate message
+    wait_until_can_send_tip_message(last_send);
+    pc.printf("\r\n");
     last_serial_send = get_time();
     
-    return true;
+    return true; 
 }
 
 /**
@@ -289,7 +315,7 @@
  */
 bool get_message_from_tip(struct tip_message *tm)
 {
-    static char buff[CRYPT_BUFF_SZ] = {0};
+    static char buff[40] = {0};
     static int pos = 0;
     bool rval = false;
     
@@ -305,7 +331,7 @@
     {
         char t = pc.getc();
         
-        if (get_time() - last_serial_send < 100)
+        if (get_time() - last_serial_send < TIP_READ_BLANK_TIME_MS)
         {
             continue; // Ignore for 100ms
         }
@@ -314,6 +340,10 @@
         {
             if (pos >= 4 && buff[0] == '!')
             {
+                #ifdef USE_CIPHER
+                myAES->decrypt((uint8_t*)buff+1, (uint8_t*)buff+1, CRYPT_BUFF_SZ);
+                #endif
+                
                 tm->command = (int)(buff[1]-'0');
                 tm->value   = atol(buff+3);  
                 rval = true;
@@ -834,7 +864,6 @@
     
     if (connected && get_time() - last_print > 5000)
     {
-        pc.printf("tip_config: %d\r\n", _state);  
         last_print = get_time();  
     }
     
@@ -1067,6 +1096,9 @@
   
   init();
   
+  unsigned char myKEY[] ={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
+  myAES = new AES(AES_128, myKEY, myIV, ECB_MODE); // will default to CBC_MODE
+  
   if(FUNCTION_CHECK) functional_check();
   if(CALIBRATE){
       calibrate(true);