BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
dgomes
Date:
Tue Aug 25 23:26:43 2015 +0000
Parent:
9:329af8cdc923
Child:
11:3de9b542eeac
Commit message:
Version 1; Moving to SecurityService

Changed in this revision

GaragemService.h Show annotated file Show diff for this revision Revisions of this file
History.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GaragemService.h	Tue Aug 25 22:18:21 2015 +0000
+++ b/GaragemService.h	Tue Aug 25 23:26:43 2015 +0000
@@ -1,19 +1,3 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
 #ifndef __BLE_GARAGEM_SERVICE_H__
 #define __BLE_GARAGEM_SERVICE_H__
 #include "History.h"
@@ -24,7 +8,6 @@
 #define GARAGEM_ERROR_REPETITION_ATTACK     1
 #define GARAGEM_ERROR_WRONG_SHARED_SECRET   2
 
-
 class GaragemService {
 public:
     const static uint16_t GARAGEM_SERVICE_UUID              = 0x2000;
@@ -35,8 +18,8 @@
     GaragemService(BLE &_ble) :
         ble(_ble), 
         GaragemChallenge(GARAGEM_CHALLENGE_CHARACTERISTIC_UUID, (uint8_t *)"INIT"),
-        GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0),
-        GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT")
+        GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT", GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
     {
         GattCharacteristic *charTable[] = {&GaragemChallenge, &GaragemLastOpenTS, &GaragemLastOpenID};
         GattService         GaragemService(GARAGEM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
@@ -46,23 +29,35 @@
     GattAttribute::Handle_t getChallengeHandle() const {
         return GaragemChallenge.getValueHandle();
     }
+    GattAttribute::Handle_t getLastOpenTSHandle() const {
+        return GaragemLastOpenTS.getValueHandle();
+    }
+    void nextLastOpen(const uint8_t *data, uint16_t len) {
+        uint64_t token = history.getToken();
+        uint8_t *tok = (uint8_t *) &token;
+        DBG("LAST:\tTS=%lu\tID=%c%c%c%c\r\n", *((uint32_t *) tok), tok[4], tok[5], tok[6], tok[7]); 
+        ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (uint8_t *)&tok[0], 4*sizeof(uint8_t));
+        ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (uint8_t *)&tok[4], 4*sizeof(uint8_t));
+
+    }
+
     
     int checkMessage(uint8_t *msg) {
         DBG("WHAT ? %s\r\n", (char *) msg);
 
         uint64_t token;
-        char syskey[8];
         memcpy(&token, msg, 8);
-        memcpy(&syskey, &msg[8], 8);
+
         DBG("TS=%lu\tID=%c%c%c%c\tSYSKEY=%s\r\n", *((uint32_t *) msg), msg[4], msg[5], msg[6], msg[7], &msg[8]); 
 
         //check we are not a victim of a repetion attack
-        if(history.last_ts() >= (uint32_t) msg[0]) {
+        DBG("%lu\t>=\t%lu\r\n", history.last_ts(), *((uint32_t*) &msg[0]));
+        if(history.last_ts() >= *((uint32_t *) &msg[0])) {
             DBG("HA HA repetion here...\r\n");
             return GARAGEM_ERROR_REPETITION_ATTACK;
         }
         
-        if (strncmp(syskey,SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS
+        if (strncmp((const char *) &msg[8],SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS
             //Save our success
             history.save(token);
             ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (const uint8_t *)&msg[0], 4*sizeof(uint8_t));
--- a/History.h	Tue Aug 25 22:18:21 2015 +0000
+++ b/History.h	Tue Aug 25 23:26:43 2015 +0000
@@ -4,11 +4,19 @@
 template<uint32_t BufferSize>
 class History {
 public:
-    History() : _head(0) {
+    History() : _head(0),_read(0) {
+        for(int i=0; i<BufferSize; i++)
+            _pool[i] = 0;
     }
 
     ~History() {
     }
+    
+    uint64_t getToken() {
+        uint64_t tok = _pool[_read++];
+        _read %= BufferSize;
+        return tok;
+    }
 
     void save(const uint64_t& data) {
         _pool[_head++] = data;
@@ -39,11 +47,12 @@
      */
     void reset() {
         _head = 0;
+        _read = 0;
     }
 
 private:
     uint64_t _pool[BufferSize];
-    volatile uint32_t _head;
+    volatile uint32_t _head, _read;
 };
 
 
--- a/main.cpp	Tue Aug 25 22:18:21 2015 +0000
+++ b/main.cpp	Tue Aug 25 23:26:43 2015 +0000
@@ -32,7 +32,12 @@
 
 //how-to test:
 //create a string to write to the characteristic
-//openssl enc -aes-128-cbc -K 9734062BA852A049CF5D40593B769014 -iv A2685636521871D02306E2EB8F7027B3 -out /dev/stdout
+//openssl enc -aes-128-cbc -K 9734062BA852A049CF5D40593B769014 -iv A2685636521871D02306E2EB8F7027B3 -out /dev/stdout -in inputfile
+// TTTTIIIISSSSSSSS
+// T -> 4 bytes Timestamp uint32_t
+// I -> 4 bytes ID (chars)
+// S -> 8 bytes SHARED_SECRET   (chars)
+
 #define SHARED_KEY  "figueiredo"
 #define DEVICE_NAME    "Garagem"
 //openssl enc -aes-128-cbc -pass pass:********** -nosalt -P
@@ -72,28 +77,29 @@
  */
 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
     DBG("onDataWrittenCallback: handle = %d      len = %d\r\n", params->handle, params->len);
-    
+    garagemServicePtr->nextLastOpen(params->data, params->len);
+
     if ((params->handle == garagemServicePtr->getChallengeHandle()) && (params->len ==16)) {
-        DBG("onDataWrittenCallback: data = %s\r\n", (char *) (params->data));
-        actuatedLED = !actuatedLED;
+        DBG("We have Challenge: data = %s\r\n", (char *) (params->data));
         
         AES myAES(AES_128, myKey, iv);
         uint8_t msg[16];
         myAES.decrypt(msg,(uint8_t *) (params->data),16);
         
         if(garagemServicePtr->checkMessage(msg) == GARAGEM_OK) {  
+            DBG("ABRE-TE SESAMO!\r\n");
+
             #ifndef DEBUG
             relay = 1;
             #endif
-            DBG("ABRE-TE SESAMO!\r\n");
-
+            actuatedLED = !actuatedLED;
             //please cleanup afterwards
             offRelay.attach(&switchOffRelay, 1.0);
             
         } else {
             DBG("NO SUCH LUCK...\r\n");
         }
-    }
+    } 
 }
 
 int main(void)