![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
fork to force BLE id to have prefix that is assumed by the app
Dependencies: BLE_API Crypto RNG mbed nRF51822
Fork of Garagem by
SecurityService.h@11:3de9b542eeac, 2015-08-26 (annotated)
- Committer:
- dgomes
- Date:
- Wed Aug 26 00:13:10 2015 +0000
- Revision:
- 11:3de9b542eeac
- Child:
- 12:eaee29bfa1c7
Not working (no Bluetooth)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgomes | 11:3de9b542eeac | 1 | #ifndef __BLE_SECURITY_SERVICE_H__ |
dgomes | 11:3de9b542eeac | 2 | #define __BLE_SECURITY_SERVICE_H__ |
dgomes | 11:3de9b542eeac | 3 | |
dgomes | 11:3de9b542eeac | 4 | #include "Crypto.h" |
dgomes | 11:3de9b542eeac | 5 | class SecurityService { |
dgomes | 11:3de9b542eeac | 6 | public: |
dgomes | 11:3de9b542eeac | 7 | const static uint16_t SECURITY_SERVICE_UUID = 0x3000; |
dgomes | 11:3de9b542eeac | 8 | const static uint16_t SECURITY_IV_CHARACTERISTIC_UUID = 0x3001; |
dgomes | 11:3de9b542eeac | 9 | const static uint16_t SECURITY_KEY_CHARACTERISTIC_UUID = 0x3002; |
dgomes | 11:3de9b542eeac | 10 | |
dgomes | 11:3de9b542eeac | 11 | SecurityService(BLE &_ble, char *shared_key) : |
dgomes | 11:3de9b542eeac | 12 | ble(_ble), |
dgomes | 11:3de9b542eeac | 13 | SecurityIV(SECURITY_IV_CHARACTERISTIC_UUID, (uint8_t *) 0), |
dgomes | 11:3de9b542eeac | 14 | SecurityKey(SECURITY_KEY_CHARACTERISTIC_UUID, (uint8_t *) 0) |
dgomes | 11:3de9b542eeac | 15 | { |
dgomes | 11:3de9b542eeac | 16 | boot = true; |
dgomes | 11:3de9b542eeac | 17 | GattCharacteristic *charTable[] = {&SecurityIV, &SecurityKey}; |
dgomes | 11:3de9b542eeac | 18 | |
dgomes | 11:3de9b542eeac | 19 | GattService SecurityService(SECURITY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
dgomes | 11:3de9b542eeac | 20 | ble.gattServer().addService(SecurityService); |
dgomes | 11:3de9b542eeac | 21 | |
dgomes | 11:3de9b542eeac | 22 | //Initialize AES |
dgomes | 11:3de9b542eeac | 23 | setKey(shared_key); |
dgomes | 11:3de9b542eeac | 24 | genIV(); |
dgomes | 11:3de9b542eeac | 25 | } |
dgomes | 11:3de9b542eeac | 26 | |
dgomes | 11:3de9b542eeac | 27 | void decode(uint8_t *out, uint8_t *in, uint32_t len) { |
dgomes | 11:3de9b542eeac | 28 | AES myAES(AES_128, myKey, iv); |
dgomes | 11:3de9b542eeac | 29 | myAES.decrypt(out,in,len); |
dgomes | 11:3de9b542eeac | 30 | genIV(); |
dgomes | 11:3de9b542eeac | 31 | } |
dgomes | 11:3de9b542eeac | 32 | |
dgomes | 11:3de9b542eeac | 33 | void setKey(char *shared_key) { |
dgomes | 11:3de9b542eeac | 34 | if(boot) { |
dgomes | 11:3de9b542eeac | 35 | DBG("Set shared_key = %s\r\n", shared_key); |
dgomes | 11:3de9b542eeac | 36 | MD5::computeHash(myKey, (uint8_t*) shared_key, strlen(shared_key)); |
dgomes | 11:3de9b542eeac | 37 | } |
dgomes | 11:3de9b542eeac | 38 | } |
dgomes | 11:3de9b542eeac | 39 | |
dgomes | 11:3de9b542eeac | 40 | void bootComplete() { |
dgomes | 11:3de9b542eeac | 41 | boot = false; |
dgomes | 11:3de9b542eeac | 42 | } |
dgomes | 11:3de9b542eeac | 43 | |
dgomes | 11:3de9b542eeac | 44 | void genIV() { |
dgomes | 11:3de9b542eeac | 45 | //TODO RANDOM THIS: |
dgomes | 11:3de9b542eeac | 46 | uint8_t new_iv[16] = { 0xA2, 0x68, 0x56, 0x36, 0x52, 0x18, 0x71, 0xD0, 0x23, 0x06, 0xE2, 0xEB, 0x8F, 0x70, 0x27, 0xB3 }; |
dgomes | 11:3de9b542eeac | 47 | memcpy(iv, new_iv,16); |
dgomes | 11:3de9b542eeac | 48 | ble.gattServer().write(SecurityIV.getValueHandle(), (uint8_t *)iv, 16*sizeof(uint8_t)); |
dgomes | 11:3de9b542eeac | 49 | } |
dgomes | 11:3de9b542eeac | 50 | |
dgomes | 11:3de9b542eeac | 51 | GattAttribute::Handle_t getKeyHandle() const { |
dgomes | 11:3de9b542eeac | 52 | return SecurityKey.getValueHandle(); |
dgomes | 11:3de9b542eeac | 53 | } |
dgomes | 11:3de9b542eeac | 54 | |
dgomes | 11:3de9b542eeac | 55 | private: |
dgomes | 11:3de9b542eeac | 56 | BLE &ble; |
dgomes | 11:3de9b542eeac | 57 | ReadOnlyArrayGattCharacteristic<uint8_t, 16> SecurityIV; |
dgomes | 11:3de9b542eeac | 58 | WriteOnlyArrayGattCharacteristic<uint8_t, 16> SecurityKey; |
dgomes | 11:3de9b542eeac | 59 | bool boot; |
dgomes | 11:3de9b542eeac | 60 | |
dgomes | 11:3de9b542eeac | 61 | uint8_t iv[16]; |
dgomes | 11:3de9b542eeac | 62 | //openssl enc -aes-128-cbc -pass pass:********** -nosalt -P |
dgomes | 11:3de9b542eeac | 63 | uint8_t myKey[16]; |
dgomes | 11:3de9b542eeac | 64 | |
dgomes | 11:3de9b542eeac | 65 | }; |
dgomes | 11:3de9b542eeac | 66 | |
dgomes | 11:3de9b542eeac | 67 | #endif /* #ifndef __BLE_SECURITY_SERVICE_H__ */ |