BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Committer:
duststorm
Date:
Mon May 01 00:33:07 2017 +0000
Revision:
15:88ea59cb2b9e
Parent:
12:eaee29bfa1c7
force address for Nano to F2:9B:4B:03:02:01 to match the prefix expected by the app

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgomes11:3de9b542eeac 1#ifndef __BLE_SECURITY_SERVICE_H__
dgomes11:3de9b542eeac 2#define __BLE_SECURITY_SERVICE_H__
dgomes11:3de9b542eeac 3
dgomes11:3de9b542eeac 4#include "Crypto.h"
dgomes13:bc1be947b497 5#include "Random.h"
dgomes12:eaee29bfa1c7 6
dgomes11:3de9b542eeac 7class SecurityService {
dgomes11:3de9b542eeac 8public:
dgomes11:3de9b542eeac 9 const static uint16_t SECURITY_SERVICE_UUID = 0x3000;
dgomes11:3de9b542eeac 10 const static uint16_t SECURITY_IV_CHARACTERISTIC_UUID = 0x3001;
dgomes11:3de9b542eeac 11 const static uint16_t SECURITY_KEY_CHARACTERISTIC_UUID = 0x3002;
dgomes11:3de9b542eeac 12
dgomes12:eaee29bfa1c7 13 SecurityService(BLE &_ble) :
dgomes11:3de9b542eeac 14 ble(_ble),
dgomes12:eaee29bfa1c7 15 SecurityIV(SECURITY_IV_CHARACTERISTIC_UUID, (uint8_t *) "IV"),
dgomes12:eaee29bfa1c7 16 SecurityKey(SECURITY_KEY_CHARACTERISTIC_UUID, (uint8_t *) "KEY")
dgomes11:3de9b542eeac 17 {
dgomes11:3de9b542eeac 18 GattCharacteristic *charTable[] = {&SecurityIV, &SecurityKey};
dgomes11:3de9b542eeac 19
dgomes11:3de9b542eeac 20 GattService SecurityService(SECURITY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
dgomes11:3de9b542eeac 21 ble.gattServer().addService(SecurityService);
dgomes12:eaee29bfa1c7 22 }
dgomes12:eaee29bfa1c7 23 void init(char *shared_key) {
dgomes11:3de9b542eeac 24 //Initialize AES
dgomes13:bc1be947b497 25 uint8_t new_iv[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
dgomes13:bc1be947b497 26 memcpy(iv, new_iv,16);
dgomes13:bc1be947b497 27 ble.gattServer().write(SecurityIV.getValueHandle(), (uint8_t *)iv, 16*sizeof(uint8_t));
dgomes11:3de9b542eeac 28 setKey(shared_key);
dgomes11:3de9b542eeac 29 }
dgomes11:3de9b542eeac 30
dgomes11:3de9b542eeac 31 void decode(uint8_t *out, uint8_t *in, uint32_t len) {
dgomes11:3de9b542eeac 32 AES myAES(AES_128, myKey, iv);
dgomes11:3de9b542eeac 33 myAES.decrypt(out,in,len);
dgomes11:3de9b542eeac 34 genIV();
dgomes11:3de9b542eeac 35 }
dgomes11:3de9b542eeac 36
dgomes11:3de9b542eeac 37 void setKey(char *shared_key) {
dgomes12:eaee29bfa1c7 38 DBG("Set shared_key = %s\r\n", shared_key);
dgomes12:eaee29bfa1c7 39 MD5::computeHash(myKey, (uint8_t*) shared_key, strlen(shared_key));
dgomes12:eaee29bfa1c7 40 }
dgomes11:3de9b542eeac 41
dgomes11:3de9b542eeac 42 void genIV() {
dgomes13:bc1be947b497 43 Random rnd;
dgomes13:bc1be947b497 44 rnd.init();
dgomes13:bc1be947b497 45 rnd.getBytes(iv, 16);
dgomes11:3de9b542eeac 46 ble.gattServer().write(SecurityIV.getValueHandle(), (uint8_t *)iv, 16*sizeof(uint8_t));
dgomes11:3de9b542eeac 47 }
dgomes11:3de9b542eeac 48
dgomes11:3de9b542eeac 49 GattAttribute::Handle_t getKeyHandle() const {
dgomes11:3de9b542eeac 50 return SecurityKey.getValueHandle();
dgomes11:3de9b542eeac 51 }
dgomes12:eaee29bfa1c7 52
dgomes12:eaee29bfa1c7 53private:
dgomes12:eaee29bfa1c7 54 uint8_t iv[16];
dgomes12:eaee29bfa1c7 55 uint8_t myKey[16];
dgomes11:3de9b542eeac 56
dgomes11:3de9b542eeac 57 BLE &ble;
dgomes11:3de9b542eeac 58 ReadOnlyArrayGattCharacteristic<uint8_t, 16> SecurityIV;
dgomes12:eaee29bfa1c7 59 WriteOnlyArrayGattCharacteristic<uint8_t, 16> SecurityKey;
dgomes11:3de9b542eeac 60};
dgomes11:3de9b542eeac 61
dgomes11:3de9b542eeac 62#endif /* #ifndef __BLE_SECURITY_SERVICE_H__ */