Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MemorySecurityDb.h Source File

MemorySecurityDb.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef GENERIC_MEMORY_SECURITY_DB_H_
00018 #define GENERIC_MEMORY_SECURITY_DB_H_
00019 
00020 #include "SecurityDb.h"
00021 
00022 namespace ble {
00023 namespace generic {
00024 
00025 /** Naive memory implementation for verification. */
00026 class MemorySecurityDb : public SecurityDb {
00027 private:
00028     struct entry_t {
00029         entry_t() { };
00030         SecurityDistributionFlags_t flags;
00031         SecurityEntryKeys_t local_keys;
00032         SecurityEntryKeys_t peer_keys;
00033         SecurityEntryIdentity_t peer_identity;
00034         SecurityEntrySigning_t peer_signing;
00035     };
00036 
00037     static const size_t MAX_ENTRIES = 5;
00038 
00039     static entry_t* as_entry(entry_handle_t db_handle)
00040     {
00041         return reinterpret_cast<entry_t*>(db_handle);
00042     }
00043 
00044 public:
00045     MemorySecurityDb() : SecurityDb() { }
00046     virtual ~MemorySecurityDb() { }
00047 
00048     virtual SecurityDistributionFlags_t* get_distribution_flags(
00049         entry_handle_t db_handle
00050     ) {
00051         return reinterpret_cast<SecurityDistributionFlags_t*>(db_handle);
00052     }
00053 
00054     /* local keys */
00055 
00056     /* set */
00057     virtual void set_entry_local_ltk(
00058         entry_handle_t db_handle,
00059         const ltk_t  &ltk
00060     ) {
00061         entry_t *entry = as_entry(db_handle);
00062         if (entry) {
00063             entry->flags.ltk_sent = true;
00064             entry->local_keys.ltk = ltk;
00065         }
00066     }
00067 
00068     virtual void set_entry_local_ediv_rand(
00069         entry_handle_t db_handle,
00070         const ediv_t  &ediv,
00071         const rand_t  &rand
00072     ) {
00073         entry_t *entry = as_entry(db_handle);
00074         if (entry) {
00075             entry->local_keys.ediv = ediv;
00076             entry->local_keys.rand = rand;
00077         }
00078     }
00079 
00080     /* peer's keys */
00081 
00082     /* set */
00083 
00084     virtual void set_entry_peer_ltk(
00085         entry_handle_t db_handle,
00086         const ltk_t  &ltk
00087     ) {
00088         entry_t *entry = as_entry(db_handle);
00089         if (entry) {
00090             entry->peer_keys.ltk = ltk;
00091             entry->flags.ltk_stored = true;
00092         }
00093     }
00094 
00095     virtual void set_entry_peer_ediv_rand(
00096         entry_handle_t db_handle,
00097         const ediv_t  &ediv,
00098         const rand_t  &rand
00099     ) {
00100         entry_t *entry = as_entry(db_handle);
00101         if (entry) {
00102             entry->peer_keys.ediv = ediv;
00103             entry->peer_keys.rand = rand;
00104         }
00105     }
00106 
00107     virtual void set_entry_peer_irk(
00108         entry_handle_t db_handle,
00109         const irk_t  &irk
00110     ) {
00111         entry_t *entry = as_entry(db_handle);
00112         if (entry) {
00113             entry->peer_identity.irk = irk;
00114             entry->flags.irk_stored = true;
00115         }
00116     }
00117 
00118     virtual void set_entry_peer_bdaddr(
00119         entry_handle_t db_handle,
00120         bool address_is_public,
00121         const address_t &peer_address
00122     ) {
00123         entry_t *entry = as_entry(db_handle);
00124         if (entry) {
00125             entry->peer_identity.identity_address = peer_address;
00126             entry->peer_identity.identity_address_is_public = address_is_public;
00127         }
00128     }
00129 
00130     virtual void set_entry_peer_csrk(
00131         entry_handle_t db_handle,
00132         const csrk_t  &csrk
00133     ) {
00134         entry_t *entry = as_entry(db_handle);
00135         if (entry) {
00136             entry->flags.csrk_stored = true;
00137             entry->peer_signing.csrk = csrk;
00138         }
00139     }
00140 
00141     virtual void set_entry_peer_sign_counter(
00142         entry_handle_t db_handle,
00143         sign_count_t sign_counter
00144     ) {
00145         entry_t *entry = as_entry(db_handle);
00146         if (entry) {
00147             entry->peer_signing.counter = sign_counter;
00148         }
00149     }
00150 
00151 private:
00152     virtual uint8_t get_entry_count() {
00153         return MAX_ENTRIES;
00154     }
00155 
00156     virtual SecurityDistributionFlags_t* get_entry_handle_by_index(uint8_t index) {
00157         if (index < MAX_ENTRIES) {
00158             return &_entries[index].flags;
00159         } else {
00160             return NULL;
00161         }
00162     }
00163 
00164     virtual void reset_entry(entry_handle_t db_entry) {
00165         entry_t *entry = reinterpret_cast<entry_t*>(db_entry);
00166         *entry = entry_t();
00167     }
00168 
00169     virtual SecurityEntryIdentity_t* read_in_entry_peer_identity(entry_handle_t db_entry) {
00170         entry_t *entry = reinterpret_cast<entry_t*>(db_entry);
00171         return &entry->peer_identity;
00172     };
00173 
00174     virtual SecurityEntryKeys_t* read_in_entry_peer_keys(entry_handle_t db_entry) {
00175         entry_t *entry = reinterpret_cast<entry_t*>(db_entry);
00176         return &entry->peer_keys;
00177     };
00178 
00179     virtual SecurityEntryKeys_t* read_in_entry_local_keys(entry_handle_t db_entry) {
00180         entry_t *entry = reinterpret_cast<entry_t*>(db_entry);
00181         return &entry->local_keys;
00182     };
00183 
00184     virtual SecurityEntrySigning_t* read_in_entry_peer_signing(entry_handle_t db_entry) {
00185         entry_t *entry = reinterpret_cast<entry_t*>(db_entry);
00186         return &entry->peer_signing;
00187     };
00188 
00189 private:
00190     entry_t _entries[MAX_ENTRIES];
00191 };
00192 
00193 } /* namespace pal */
00194 } /* namespace ble */
00195 
00196 #endif /*GENERIC_MEMORY_SECURITY_DB_H_*/