diff --git a/features/FEATURE_BLE/ble/BLETypes.h b/features/FEATURE_BLE/ble/BLETypes.h index 11973e0b69..9198a98972 100644 --- a/features/FEATURE_BLE/ble/BLETypes.h +++ b/features/FEATURE_BLE/ble/BLETypes.h @@ -183,6 +183,82 @@ struct io_capability_t : SafeEnum { io_capability_t(type value) : SafeEnum(value) { } }; +/** + * Passkey stored as a number. + */ +typedef uint32_t passkey_num_t; + +/** + * Passkey stored as a string of digits. + */ +class PasskeyAsci { +public: + static const uint8_t PASSKEY_LEN = 6; + static const uint8_t NUMBER_OFFSET = '0'; + + /** + * Default to all zeroes + */ + PasskeyAsci() { + memset(asci, NUMBER_OFFSET, PASSKEY_LEN); + } + + /** + * Initialize a data from a string. + * + * @param input_value value of the data. + */ + PasskeyAsci(const uint8_t* passkey) { + if (passkey) { + memcpy(asci, passkey, PASSKEY_LEN); + } else { + memset(asci, NUMBER_OFFSET, PASSKEY_LEN); + } + } + + /** + * Initialize a data from a number. + * + * @param input_value value of the data. + */ + PasskeyAsci(passkey_num_t passkey) { + for (int i = 5, m = 100000; i >= 0; --i, m /= 10) { + uint32_t result = passkey / m; + asci[i] = NUMBER_OFFSET + result; + passkey -= result; + } + } + + /** + * Cast to number. + */ + operator passkey_num_t() { + return to_num(asci); + } + + /** + * Convert ASCI string of digits into a number. + * @param ASCI string of 6 digits stored as ASCI characters + * @return Passkey as a number. + */ + static uint32_t to_num(const uint8_t *asci) { + uint32_t passkey = 0; + for (size_t i = 0, m = 1; i < PASSKEY_LEN; ++i, m *= 10) { + passkey += (asci[i] - NUMBER_OFFSET) * m; + } + return passkey; + } + + /** + * Return the pointer to the buffer holding the string. + */ + uint8_t* value() { + return asci; + } +private: + uint8_t asci[PASSKEY_LEN]; +}; + template struct octet_type_t { /** @@ -250,15 +326,20 @@ private: uint8_t value[octet_size]; }; -/* 128 bit keys */ +/** 128 bit keys used by paired devices */ class key_t : public octet_type_t<16> {} ; class irk_t : public key_t {}; class csrk_t : public key_t {}; class ltk_t : public key_t {}; +/** Used to identify LTK for legacy pairing connections */ typedef octet_type_t<2> ediv_t; typedef octet_type_t<8> rand_t; + +/** Used to store the random data generated by the chip */ typedef octet_type_t<8> random_data_t; + +/** Out of band data exchanged during pairing */ typedef octet_type_t<16> oob_data_t; } // namespace ble diff --git a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h index fac2534549..b6ab847199 100644 --- a/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h +++ b/features/FEATURE_BLE/ble/generic/GenericSecurityManager.h @@ -27,16 +27,18 @@ namespace generic { using ble::pal::address_t; using ble::pal::advertising_peer_address_type_t; +using ble::pal::AuthenticationMask; +using ble::pal::KeyDistribution; + using ble::irk_t; using ble::csrk_t; using ble::ltk_t; using ble::ediv_t; using ble::rand_t; -using ble::pal::AuthenticationMask; -using ble::pal::KeyDistribution; using ble::pairing_failure_t; -using ble::pal::PasskeyAsci; -using ble::pal::passkey_num_t; +using ble::PasskeyAsci; +using ble::passkey_num_t; + typedef SecurityManager::SecurityIOCapabilities_t SecurityIOCapabilities_t; class GenericSecurityManagerEventHandler; diff --git a/features/FEATURE_BLE/ble/pal/PalSecurityManager.h b/features/FEATURE_BLE/ble/pal/PalSecurityManager.h index 4b72c263c5..8844a8539b 100644 --- a/features/FEATURE_BLE/ble/pal/PalSecurityManager.h +++ b/features/FEATURE_BLE/ble/pal/PalSecurityManager.h @@ -32,43 +32,6 @@ typedef SecurityManager::SecurityMode_t SecurityMode_t; typedef SecurityManager::LinkSecurityStatus_t LinkSecurityStatus_t; typedef SecurityManager::Keypress_t Keypress_t; -typedef uint32_t passkey_num_t; - -class PasskeyAsci { -public: - static const uint8_t NUMBER_OFFSET = '0'; - - PasskeyAsci() { - memset(asci, NUMBER_OFFSET, SecurityManager::PASSKEY_LEN); - } - PasskeyAsci(const uint8_t* passkey) { - if (passkey) { - memcpy(asci, passkey, SecurityManager::PASSKEY_LEN); - } else { - memset(asci, NUMBER_OFFSET, SecurityManager::PASSKEY_LEN); - } - } - PasskeyAsci(passkey_num_t passkey) { - for (int i = 5, m = 100000; i >= 0; --i, m /= 10) { - uint32_t result = passkey / m; - asci[i] = NUMBER_OFFSET + result; - passkey -= result; - } - } - operator passkey_num_t() { - return to_num(asci); - } - - static uint32_t to_num(const uint8_t *asci) { - uint32_t passkey = 0; - for (size_t i = 0, m = 1; i < SecurityManager::PASSKEY_LEN; ++i, m *= 10) { - passkey += (asci[i] - NUMBER_OFFSET) * m; - } - return passkey; - } - uint8_t asci[SecurityManager::PASSKEY_LEN]; -}; - /** * Key distribution as required by the SMP with convenient setters and getters, * use value() to get the octet you can use directly in the PDU. @@ -242,7 +205,7 @@ public: * or cancel the pairing procedure (cancel_pairing). * * @param[in] connection connection handle - * @param[in] oob_data_flag is oob data present + * @param[in] oob_data_flag is out of band data present * @param[in] authentication_requirements authentication requirements * @param[in] initiator_dist key distribution * @param[in] responder_dist key distribution diff --git a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp index 59e50e6da6..860cfaac5d 100644 --- a/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp +++ b/features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp @@ -762,7 +762,7 @@ void GenericSecurityManager::on_passkey_display( passkey_num_t passkey ) { set_mitm_performed(connection); - eventHandler->passkeyDisplay(connection, PasskeyAsci(passkey).asci); + eventHandler->passkeyDisplay(connection, PasskeyAsci(passkey).value()); } void GenericSecurityManager::on_keypress_notification(