![](/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@13:bc1be947b497, 2015-08-30 (annotated)
- Committer:
- dgomes
- Date:
- Sun Aug 30 01:06:17 2015 +0000
- Revision:
- 13:bc1be947b497
- Parent:
- 12:eaee29bfa1c7
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 | 13:bc1be947b497 | 5 | #include "Random.h" |
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 | 13:bc1be947b497 | 25 | uint8_t new_iv[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; |
dgomes | 13:bc1be947b497 | 26 | memcpy(iv, new_iv,16); |
dgomes | 13:bc1be947b497 | 27 | ble.gattServer().write(SecurityIV.getValueHandle(), (uint8_t *)iv, 16*sizeof(uint8_t)); |
dgomes | 11:3de9b542eeac | 28 | setKey(shared_key); |
dgomes | 11:3de9b542eeac | 29 | } |
dgomes | 11:3de9b542eeac | 30 | |
dgomes | 11:3de9b542eeac | 31 | void decode(uint8_t *out, uint8_t *in, uint32_t len) { |
dgomes | 11:3de9b542eeac | 32 | AES myAES(AES_128, myKey, iv); |
dgomes | 11:3de9b542eeac | 33 | myAES.decrypt(out,in,len); |
dgomes | 11:3de9b542eeac | 34 | genIV(); |
dgomes | 11:3de9b542eeac | 35 | } |
dgomes | 11:3de9b542eeac | 36 | |
dgomes | 11:3de9b542eeac | 37 | void setKey(char *shared_key) { |
dgomes | 12:eaee29bfa1c7 | 38 | DBG("Set shared_key = %s\r\n", shared_key); |
dgomes | 12:eaee29bfa1c7 | 39 | MD5::computeHash(myKey, (uint8_t*) shared_key, strlen(shared_key)); |
dgomes | 12:eaee29bfa1c7 | 40 | } |
dgomes | 11:3de9b542eeac | 41 | |
dgomes | 11:3de9b542eeac | 42 | void genIV() { |
dgomes | 13:bc1be947b497 | 43 | Random rnd; |
dgomes | 13:bc1be947b497 | 44 | rnd.init(); |
dgomes | 13:bc1be947b497 | 45 | rnd.getBytes(iv, 16); |
dgomes | 11:3de9b542eeac | 46 | ble.gattServer().write(SecurityIV.getValueHandle(), (uint8_t *)iv, 16*sizeof(uint8_t)); |
dgomes | 11:3de9b542eeac | 47 | } |
dgomes | 11:3de9b542eeac | 48 | |
dgomes | 11:3de9b542eeac | 49 | GattAttribute::Handle_t getKeyHandle() const { |
dgomes | 11:3de9b542eeac | 50 | return SecurityKey.getValueHandle(); |
dgomes | 11:3de9b542eeac | 51 | } |
dgomes | 12:eaee29bfa1c7 | 52 | |
dgomes | 12:eaee29bfa1c7 | 53 | private: |
dgomes | 12:eaee29bfa1c7 | 54 | uint8_t iv[16]; |
dgomes | 12:eaee29bfa1c7 | 55 | uint8_t myKey[16]; |
dgomes | 11:3de9b542eeac | 56 | |
dgomes | 11:3de9b542eeac | 57 | BLE &ble; |
dgomes | 11:3de9b542eeac | 58 | ReadOnlyArrayGattCharacteristic<uint8_t, 16> SecurityIV; |
dgomes | 12:eaee29bfa1c7 | 59 | WriteOnlyArrayGattCharacteristic<uint8_t, 16> SecurityKey; |
dgomes | 11:3de9b542eeac | 60 | }; |
dgomes | 11:3de9b542eeac | 61 | |
dgomes | 11:3de9b542eeac | 62 | #endif /* #ifndef __BLE_SECURITY_SERVICE_H__ */ |