Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /* mbed Microcontroller Library
MrBedfordVan 0:b9164b348919 2 * Copyright (c) 2006-2013 ARM Limited
MrBedfordVan 0:b9164b348919 3 *
MrBedfordVan 0:b9164b348919 4 * Licensed under the Apache License, Version 2.0 (the "License");
MrBedfordVan 0:b9164b348919 5 * you may not use this file except in compliance with the License.
MrBedfordVan 0:b9164b348919 6 * You may obtain a copy of the License at
MrBedfordVan 0:b9164b348919 7 *
MrBedfordVan 0:b9164b348919 8 * http://www.apache.org/licenses/LICENSE-2.0
MrBedfordVan 0:b9164b348919 9 *
MrBedfordVan 0:b9164b348919 10 * Unless required by applicable law or agreed to in writing, software
MrBedfordVan 0:b9164b348919 11 * distributed under the License is distributed on an "AS IS" BASIS,
MrBedfordVan 0:b9164b348919 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MrBedfordVan 0:b9164b348919 13 * See the License for the specific language governing permissions and
MrBedfordVan 0:b9164b348919 14 * limitations under the License.
MrBedfordVan 0:b9164b348919 15 */
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 #ifndef __NRF51822_SECURITY_MANAGER_H__
MrBedfordVan 0:b9164b348919 18 #define __NRF51822_SECURITY_MANAGER_H__
MrBedfordVan 0:b9164b348919 19
MrBedfordVan 0:b9164b348919 20 #include <stddef.h>
MrBedfordVan 0:b9164b348919 21
MrBedfordVan 0:b9164b348919 22 #include "nRF5xGap.h"
MrBedfordVan 0:b9164b348919 23 #include "ble/SecurityManager.h"
MrBedfordVan 0:b9164b348919 24 #include "btle_security.h"
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 class nRF5xSecurityManager : public SecurityManager
MrBedfordVan 0:b9164b348919 27 {
MrBedfordVan 0:b9164b348919 28 public:
MrBedfordVan 0:b9164b348919 29 /* Functions that must be implemented from SecurityManager */
MrBedfordVan 0:b9164b348919 30 virtual ble_error_t init(bool enableBonding,
MrBedfordVan 0:b9164b348919 31 bool requireMITM,
MrBedfordVan 0:b9164b348919 32 SecurityIOCapabilities_t iocaps,
MrBedfordVan 0:b9164b348919 33 const Passkey_t passkey) {
MrBedfordVan 0:b9164b348919 34 return btle_initializeSecurity(enableBonding, requireMITM, iocaps, passkey);
MrBedfordVan 0:b9164b348919 35 }
MrBedfordVan 0:b9164b348919 36
MrBedfordVan 0:b9164b348919 37 virtual ble_error_t getLinkSecurity(Gap::Handle_t connectionHandle, LinkSecurityStatus_t *securityStatusP) {
MrBedfordVan 0:b9164b348919 38 return btle_getLinkSecurity(connectionHandle, securityStatusP);
MrBedfordVan 0:b9164b348919 39 }
MrBedfordVan 0:b9164b348919 40
MrBedfordVan 0:b9164b348919 41 virtual ble_error_t setLinkSecurity(Gap::Handle_t connectionHandle, SecurityMode_t securityMode) {
MrBedfordVan 0:b9164b348919 42 return btle_setLinkSecurity(connectionHandle, securityMode);
MrBedfordVan 0:b9164b348919 43 }
MrBedfordVan 0:b9164b348919 44
MrBedfordVan 0:b9164b348919 45 virtual ble_error_t purgeAllBondingState(void) {
MrBedfordVan 0:b9164b348919 46 return btle_purgeAllBondingState();
MrBedfordVan 0:b9164b348919 47 }
MrBedfordVan 0:b9164b348919 48
MrBedfordVan 0:b9164b348919 49 /**
MrBedfordVan 0:b9164b348919 50 * @brief Returns a list of addresses from peers in the stacks bond table.
MrBedfordVan 0:b9164b348919 51 *
MrBedfordVan 0:b9164b348919 52 * @param[in/out] addresses
MrBedfordVan 0:b9164b348919 53 * (on input) @ref Gap::Whitelist_t structure where at
MrBedfordVan 0:b9164b348919 54 * most addresses.capacity addresses from bonded peers will
MrBedfordVan 0:b9164b348919 55 * be stored.
MrBedfordVan 0:b9164b348919 56 * (on output) A copy of the addresses from bonded peers.
MrBedfordVan 0:b9164b348919 57 *
MrBedfordVan 0:b9164b348919 58 * @return
MrBedfordVan 0:b9164b348919 59 * BLE_ERROR_NONE if successful.
MrBedfordVan 0:b9164b348919 60 */
MrBedfordVan 0:b9164b348919 61 virtual ble_error_t getAddressesFromBondTable(Gap::Whitelist_t &addresses) const {
MrBedfordVan 0:b9164b348919 62 uint8_t i;
MrBedfordVan 0:b9164b348919 63
MrBedfordVan 0:b9164b348919 64 ble_gap_whitelist_t whitelistFromBondTable;
MrBedfordVan 0:b9164b348919 65 ble_gap_addr_t *addressPtr[YOTTA_CFG_WHITELIST_MAX_SIZE];
MrBedfordVan 0:b9164b348919 66 ble_gap_irk_t *irkPtr[YOTTA_CFG_IRK_TABLE_MAX_SIZE];
MrBedfordVan 0:b9164b348919 67
MrBedfordVan 0:b9164b348919 68 /* Initialize the structure so that we get as many addreses as the whitelist can hold */
MrBedfordVan 0:b9164b348919 69 whitelistFromBondTable.addr_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE;
MrBedfordVan 0:b9164b348919 70 whitelistFromBondTable.pp_addrs = addressPtr;
MrBedfordVan 0:b9164b348919 71 whitelistFromBondTable.irk_count = YOTTA_CFG_IRK_TABLE_MAX_SIZE;
MrBedfordVan 0:b9164b348919 72 whitelistFromBondTable.pp_irks = irkPtr;
MrBedfordVan 0:b9164b348919 73
MrBedfordVan 0:b9164b348919 74 ble_error_t error = createWhitelistFromBondTable(whitelistFromBondTable);
MrBedfordVan 0:b9164b348919 75 if (error != BLE_ERROR_NONE) {
MrBedfordVan 0:b9164b348919 76 addresses.size = 0;
MrBedfordVan 0:b9164b348919 77 addresses.bonds = 0;
MrBedfordVan 0:b9164b348919 78 return error;
MrBedfordVan 0:b9164b348919 79 }
MrBedfordVan 0:b9164b348919 80
MrBedfordVan 0:b9164b348919 81 addresses.bonds = whitelistFromBondTable.irk_count;
MrBedfordVan 0:b9164b348919 82
MrBedfordVan 0:b9164b348919 83 /* Put all the addresses in the structure */
MrBedfordVan 0:b9164b348919 84 for (i = 0; i < whitelistFromBondTable.addr_count; ++i) {
MrBedfordVan 0:b9164b348919 85 if (i >= addresses.capacity) {
MrBedfordVan 0:b9164b348919 86 /* Ran out of space in the output Gap::Whitelist_t */
MrBedfordVan 0:b9164b348919 87 addresses.size = i;
MrBedfordVan 0:b9164b348919 88 return BLE_ERROR_NONE;
MrBedfordVan 0:b9164b348919 89 }
MrBedfordVan 0:b9164b348919 90 memcpy(&addresses.addresses[i], whitelistFromBondTable.pp_addrs[i], sizeof(BLEProtocol::Address_t));
MrBedfordVan 0:b9164b348919 91 }
MrBedfordVan 0:b9164b348919 92
MrBedfordVan 0:b9164b348919 93 /* Update the current address count */
MrBedfordVan 0:b9164b348919 94 addresses.size = i;
MrBedfordVan 0:b9164b348919 95
MrBedfordVan 0:b9164b348919 96 /* The assumption here is that the underlying implementation of
MrBedfordVan 0:b9164b348919 97 * createWhitelistFromBondTable() will not return the private resolvable
MrBedfordVan 0:b9164b348919 98 * addresses (which is the case in the SoftDevice). Rather it returns the
MrBedfordVan 0:b9164b348919 99 * IRKs, so we need to generate the private resolvable address by ourselves.
MrBedfordVan 0:b9164b348919 100 */
MrBedfordVan 0:b9164b348919 101 for (i = 0; i < whitelistFromBondTable.irk_count; ++i) {
MrBedfordVan 0:b9164b348919 102 if (i + addresses.size >= addresses.capacity) {
MrBedfordVan 0:b9164b348919 103 /* Ran out of space in the output Gap::Whitelist_t */
MrBedfordVan 0:b9164b348919 104 addresses.size += i;
MrBedfordVan 0:b9164b348919 105 return BLE_ERROR_NONE;
MrBedfordVan 0:b9164b348919 106 }
MrBedfordVan 0:b9164b348919 107 btle_generateResolvableAddress(
MrBedfordVan 0:b9164b348919 108 *whitelistFromBondTable.pp_irks[i],
MrBedfordVan 0:b9164b348919 109 (ble_gap_addr_t &) addresses.addresses[i + addresses.size]
MrBedfordVan 0:b9164b348919 110 );
MrBedfordVan 0:b9164b348919 111 }
MrBedfordVan 0:b9164b348919 112
MrBedfordVan 0:b9164b348919 113 /* Update the current address count */
MrBedfordVan 0:b9164b348919 114 addresses.size += i;
MrBedfordVan 0:b9164b348919 115
MrBedfordVan 0:b9164b348919 116 return BLE_ERROR_NONE;
MrBedfordVan 0:b9164b348919 117 }
MrBedfordVan 0:b9164b348919 118
MrBedfordVan 0:b9164b348919 119 /**
MrBedfordVan 0:b9164b348919 120 * @brief Clear nRF5xSecurityManager's state.
MrBedfordVan 0:b9164b348919 121 *
MrBedfordVan 0:b9164b348919 122 * @return
MrBedfordVan 0:b9164b348919 123 * BLE_ERROR_NONE if successful.
MrBedfordVan 0:b9164b348919 124 */
MrBedfordVan 0:b9164b348919 125 virtual ble_error_t reset(void)
MrBedfordVan 0:b9164b348919 126 {
MrBedfordVan 0:b9164b348919 127 if (SecurityManager::reset() != BLE_ERROR_NONE) {
MrBedfordVan 0:b9164b348919 128 return BLE_ERROR_INVALID_STATE;
MrBedfordVan 0:b9164b348919 129 }
MrBedfordVan 0:b9164b348919 130
MrBedfordVan 0:b9164b348919 131 return BLE_ERROR_NONE;
MrBedfordVan 0:b9164b348919 132 }
MrBedfordVan 0:b9164b348919 133
MrBedfordVan 0:b9164b348919 134 bool hasInitialized(void) const {
MrBedfordVan 0:b9164b348919 135 return btle_hasInitializedSecurity();
MrBedfordVan 0:b9164b348919 136 }
MrBedfordVan 0:b9164b348919 137
MrBedfordVan 0:b9164b348919 138 public:
MrBedfordVan 0:b9164b348919 139 /*
MrBedfordVan 0:b9164b348919 140 * Allow instantiation from nRF5xn when required.
MrBedfordVan 0:b9164b348919 141 */
MrBedfordVan 0:b9164b348919 142 friend class nRF5xn;
MrBedfordVan 0:b9164b348919 143
MrBedfordVan 0:b9164b348919 144 nRF5xSecurityManager() {
MrBedfordVan 0:b9164b348919 145 /* empty */
MrBedfordVan 0:b9164b348919 146 }
MrBedfordVan 0:b9164b348919 147
MrBedfordVan 0:b9164b348919 148 private:
MrBedfordVan 0:b9164b348919 149 nRF5xSecurityManager(const nRF5xSecurityManager &);
MrBedfordVan 0:b9164b348919 150 const nRF5xSecurityManager& operator=(const nRF5xSecurityManager &);
MrBedfordVan 0:b9164b348919 151
MrBedfordVan 0:b9164b348919 152 /*
MrBedfordVan 0:b9164b348919 153 * Expose an interface that allows us to query the SoftDevice bond table
MrBedfordVan 0:b9164b348919 154 * and extract a whitelist.
MrBedfordVan 0:b9164b348919 155 */
MrBedfordVan 0:b9164b348919 156 ble_error_t createWhitelistFromBondTable(ble_gap_whitelist_t &whitelistFromBondTable) const {
MrBedfordVan 0:b9164b348919 157 return btle_createWhitelistFromBondTable(&whitelistFromBondTable);
MrBedfordVan 0:b9164b348919 158 }
MrBedfordVan 0:b9164b348919 159
MrBedfordVan 0:b9164b348919 160 /*
MrBedfordVan 0:b9164b348919 161 * Given a BLE address and a IRK this function check whether the address
MrBedfordVan 0:b9164b348919 162 * can be generated from the IRK. To do so, this function uses the hash
MrBedfordVan 0:b9164b348919 163 * function and algorithm described in the Bluetooth low Energy
MrBedfordVan 0:b9164b348919 164 * Specification. Internally, Nordic SDK functions are used.
MrBedfordVan 0:b9164b348919 165 */
MrBedfordVan 0:b9164b348919 166 bool matchAddressAndIrk(ble_gap_addr_t *address, ble_gap_irk_t *irk) const {
MrBedfordVan 0:b9164b348919 167 return btle_matchAddressAndIrk(address, irk);
MrBedfordVan 0:b9164b348919 168 }
MrBedfordVan 0:b9164b348919 169
MrBedfordVan 0:b9164b348919 170 /*
MrBedfordVan 0:b9164b348919 171 * Give nRF5xGap access to createWhitelistFromBondTable() and
MrBedfordVan 0:b9164b348919 172 * matchAddressAndIrk()
MrBedfordVan 0:b9164b348919 173 */
MrBedfordVan 0:b9164b348919 174 friend class nRF5xGap;
MrBedfordVan 0:b9164b348919 175 };
MrBedfordVan 0:b9164b348919 176
MrBedfordVan 0:b9164b348919 177 #endif // ifndef __NRF51822_SECURITY_MANAGER_H__