![](/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@12:eaee29bfa1c7, 2015-08-26 (annotated)
- Committer:
- dgomes
- Date:
- Wed Aug 26 00:59:07 2015 +0000
- Revision:
- 12:eaee29bfa1c7
- Parent:
- 11:3de9b542eeac
- Child:
- 13:bc1be947b497
All works :) ?
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 | 12:eaee29bfa1c7 | 5 | |
dgomes | 12:eaee29bfa1c7 | 6 | |
dgomes | 11:3de9b542eeac | 7 | class SecurityService { |
dgomes | 11:3de9b542eeac | 8 | public: |
dgomes | 11:3de9b542eeac | 9 | const static uint16_t SECURITY_SERVICE_UUID = 0x3000; |
dgomes | 11:3de9b542eeac | 10 | const static uint16_t SECURITY_IV_CHARACTERISTIC_UUID = 0x3001; |
dgomes | 11:3de9b542eeac | 11 | const static uint16_t SECURITY_KEY_CHARACTERISTIC_UUID = 0x3002; |
dgomes | 11:3de9b542eeac | 12 | |
dgomes | 12:eaee29bfa1c7 | 13 | SecurityService(BLE &_ble) : |
dgomes | 11:3de9b542eeac | 14 | ble(_ble), |
dgomes | 12:eaee29bfa1c7 | 15 | SecurityIV(SECURITY_IV_CHARACTERISTIC_UUID, (uint8_t *) "IV"), |
dgomes | 12:eaee29bfa1c7 | 16 | SecurityKey(SECURITY_KEY_CHARACTERISTIC_UUID, (uint8_t *) "KEY") |
dgomes | 11:3de9b542eeac | 17 | { |
dgomes | 11:3de9b542eeac | 18 | GattCharacteristic *charTable[] = {&SecurityIV, &SecurityKey}; |
dgomes | 11:3de9b542eeac | 19 | |
dgomes | 11:3de9b542eeac | 20 | GattService SecurityService(SECURITY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
dgomes | 11:3de9b542eeac | 21 | ble.gattServer().addService(SecurityService); |
dgomes | 12:eaee29bfa1c7 | 22 | } |
dgomes | 12:eaee29bfa1c7 | 23 | void init(char *shared_key) { |
dgomes | 11:3de9b542eeac | 24 | //Initialize AES |
dgomes | 11:3de9b542eeac | 25 | setKey(shared_key); |
dgomes | 12:eaee29bfa1c7 | 26 | genIV(); |
dgomes | 11:3de9b542eeac | 27 | } |
dgomes | 11:3de9b542eeac | 28 | |
dgomes | 11:3de9b542eeac | 29 | void decode(uint8_t *out, uint8_t *in, uint32_t len) { |
dgomes | 11:3de9b542eeac | 30 | AES myAES(AES_128, myKey, iv); |
dgomes | 11:3de9b542eeac | 31 | myAES.decrypt(out,in,len); |
dgomes | 11:3de9b542eeac | 32 | genIV(); |
dgomes | 11:3de9b542eeac | 33 | } |
dgomes | 11:3de9b542eeac | 34 | |
dgomes | 11:3de9b542eeac | 35 | void setKey(char *shared_key) { |
dgomes | 12:eaee29bfa1c7 | 36 | DBG("Set shared_key = %s\r\n", shared_key); |
dgomes | 12:eaee29bfa1c7 | 37 | MD5::computeHash(myKey, (uint8_t*) shared_key, strlen(shared_key)); |
dgomes | 12:eaee29bfa1c7 | 38 | } |
dgomes | 11:3de9b542eeac | 39 | |
dgomes | 11:3de9b542eeac | 40 | void genIV() { |
dgomes | 11:3de9b542eeac | 41 | //TODO RANDOM THIS: |
dgomes | 11:3de9b542eeac | 42 | uint8_t new_iv[16] = { 0xA2, 0x68, 0x56, 0x36, 0x52, 0x18, 0x71, 0xD0, 0x23, 0x06, 0xE2, 0xEB, 0x8F, 0x70, 0x27, 0xB3 }; |
dgomes | 11:3de9b542eeac | 43 | memcpy(iv, new_iv,16); |
dgomes | 11:3de9b542eeac | 44 | ble.gattServer().write(SecurityIV.getValueHandle(), (uint8_t *)iv, 16*sizeof(uint8_t)); |
dgomes | 11:3de9b542eeac | 45 | } |
dgomes | 11:3de9b542eeac | 46 | |
dgomes | 11:3de9b542eeac | 47 | GattAttribute::Handle_t getKeyHandle() const { |
dgomes | 11:3de9b542eeac | 48 | return SecurityKey.getValueHandle(); |
dgomes | 11:3de9b542eeac | 49 | } |
dgomes | 12:eaee29bfa1c7 | 50 | |
dgomes | 12:eaee29bfa1c7 | 51 | private: |
dgomes | 12:eaee29bfa1c7 | 52 | uint8_t iv[16]; |
dgomes | 12:eaee29bfa1c7 | 53 | uint8_t myKey[16]; |
dgomes | 11:3de9b542eeac | 54 | |
dgomes | 11:3de9b542eeac | 55 | BLE &ble; |
dgomes | 11:3de9b542eeac | 56 | ReadOnlyArrayGattCharacteristic<uint8_t, 16> SecurityIV; |
dgomes | 12:eaee29bfa1c7 | 57 | WriteOnlyArrayGattCharacteristic<uint8_t, 16> SecurityKey; |
dgomes | 11:3de9b542eeac | 58 | }; |
dgomes | 11:3de9b542eeac | 59 | |
dgomes | 11:3de9b542eeac | 60 | #endif /* #ifndef __BLE_SECURITY_SERVICE_H__ */ |