mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
moved to configurable components, added SSD1306 support
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
idf_component_register(
|
||||
SRCS
|
||||
"beacons.c"
|
||||
"bluetooth-advertise.c"
|
||||
"bluetooth-scan.c"
|
||||
"crypto.c"
|
||||
"ena.c"
|
||||
"storage.c"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
spi_flash
|
||||
mbedtls
|
||||
bt
|
||||
)
|
||||
@@ -0,0 +1,85 @@
|
||||
menu "Exposure Notification API"
|
||||
|
||||
menu "Storage"
|
||||
|
||||
config ENA_STORAGE_DUMP
|
||||
bool "Dump storage"
|
||||
default false
|
||||
help
|
||||
Dump storage (stored TEKs, temp. beacons and perm. beacons) to serial output after scan.
|
||||
|
||||
config ENA_STORAGE_TEK_MAX
|
||||
int "Max. TEKs"
|
||||
default 14
|
||||
help
|
||||
Defines the maximum number of TEKs to be stored. (Default 14 [14 * 144 => 14 days])
|
||||
|
||||
config ENA_STORAGE_TEMP_BEACONS_MAX
|
||||
int "Max. temporary beacons"
|
||||
default 1000
|
||||
help
|
||||
Defines the maximum number of temporary beacons to be stored. (Default 1000)
|
||||
|
||||
config ENA_STORAGE_START_ADDRESS
|
||||
int "Storage start address"
|
||||
default 0
|
||||
help
|
||||
Defines the start address on partition. (Default 0)
|
||||
|
||||
config ENA_STORAGE_PARTITION_NAME
|
||||
string "Partition name"
|
||||
default "ena"
|
||||
help
|
||||
Name of the partition used for storage. (Default "ena", see partitions.csv)
|
||||
|
||||
config ENA_STORAGE_ERASE
|
||||
bool "Erase storage (!)"
|
||||
default false
|
||||
help
|
||||
Erases the complete(!) partition on startup and reset counters.
|
||||
|
||||
endmenu
|
||||
|
||||
menu "Scanning"
|
||||
config ENA_BEACON_TRESHOLD
|
||||
int "Contact threshold"
|
||||
default 300
|
||||
help
|
||||
Threshold in seconds after a received beacon is stored permanently. (Default 5 minutes)
|
||||
|
||||
config ENA_SCANNING_TIME
|
||||
int "Scanning time"
|
||||
default 30
|
||||
help
|
||||
Time in seconds how long a scan should run. (Default 30 seconds)
|
||||
|
||||
config ENA_SCANNING_INTERVAL
|
||||
int "Scanning interval"
|
||||
default 300
|
||||
help
|
||||
Interval in seconds for the next scan to happen. (Default 5 minutes)
|
||||
endmenu
|
||||
|
||||
menu "Advertising"
|
||||
|
||||
config ENA_BT_ROTATION_TIMEOUT_INTERVAL
|
||||
int "Rotation timeout interval"
|
||||
default 900
|
||||
help
|
||||
Base rotation timeout interval in seconds for changing BT address and therefore the advertised beacon. (Default 5 minutes)
|
||||
|
||||
config ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL
|
||||
int "Randomize rotation timeout interval"
|
||||
default 150
|
||||
help
|
||||
Range in seconds for randomize the rotation timeout interval. (Default +/- ~2.5 minutes)
|
||||
|
||||
config ENA_TEK_ROLLING_PERIOD
|
||||
int "TEK rolling period"
|
||||
default 144
|
||||
help
|
||||
Defines the TEK rolling period in 10 minute steps. (Default 144 => 24 hours)
|
||||
endmenu
|
||||
|
||||
|
||||
endmenu
|
||||
@@ -0,0 +1,119 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-storage.h"
|
||||
|
||||
#include "ena-beacons.h"
|
||||
|
||||
static uint32_t temp_beacons_count = 0;
|
||||
static ena_temp_beacon_t temp_beacons[ENA_STORAGE_TEMP_BEACONS_MAX];
|
||||
|
||||
ena_beacon_t ena_beacons_convert(ena_temp_beacon_t temp_beacon)
|
||||
{
|
||||
ena_beacon_t beacon;
|
||||
memcpy(beacon.rpi, temp_beacon.rpi, ENA_KEY_LENGTH);
|
||||
memcpy(beacon.aem, temp_beacon.aem, ENA_AEM_METADATA_LENGTH);
|
||||
beacon.timestamp = temp_beacon.timestamp_last;
|
||||
beacon.rssi = temp_beacon.rssi;
|
||||
return beacon;
|
||||
}
|
||||
|
||||
int ena_get_temp_beacon_index(uint8_t *rpi, uint8_t *aem)
|
||||
{
|
||||
for (int i = 0; i < temp_beacons_count; i++)
|
||||
{
|
||||
if (memcmp(temp_beacons[i].rpi, rpi, sizeof(ENA_KEY_LENGTH)) == 0 &&
|
||||
memcmp(temp_beacons[i].aem, aem, sizeof(ENA_AEM_METADATA_LENGTH)) == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void ena_beacons_temp_refresh(uint32_t unix_timestamp)
|
||||
{
|
||||
for (int i = temp_beacons_count - 1; i >= 0; i--)
|
||||
{
|
||||
// check for treshold and add permanent beacon
|
||||
if (temp_beacons[i].timestamp_last - temp_beacons[i].timestamp_first >= ENA_BEACON_TRESHOLD)
|
||||
{
|
||||
ESP_LOGD(ENA_BEACON_LOG, "create beacon after treshold");
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_BEACON_LOG, temp_beacons[i].rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ena_beacon_t beacon = ena_beacons_convert(temp_beacons[i]);
|
||||
ena_storage_add_beacon(&beacon);
|
||||
ena_storage_remove_temp_beacon(i);
|
||||
}
|
||||
else
|
||||
// delete temp beacons older than two times time window (two times to be safe, one times time window enough?!)
|
||||
if (unix_timestamp - temp_beacons[i].timestamp_last > (ENA_TIME_WINDOW * 2))
|
||||
{
|
||||
ESP_LOGD(ENA_BEACON_LOG, "remove old temporary beacon %u", i);
|
||||
ena_storage_remove_temp_beacon(i);
|
||||
}
|
||||
}
|
||||
|
||||
// update beacons
|
||||
temp_beacons_count = ena_storage_temp_beacons_count();
|
||||
for (int i = 0; i < temp_beacons_count; i++)
|
||||
{
|
||||
ena_storage_get_temp_beacon(i, &temp_beacons[i]);
|
||||
}
|
||||
|
||||
#if (CONFIG_ENA_STORAGE_DUMP)
|
||||
// DEBUG dump
|
||||
ena_storage_dump_tek();
|
||||
ena_storage_dump_temp_beacons();
|
||||
ena_storage_dump_beacons();
|
||||
#endif
|
||||
}
|
||||
|
||||
void ena_beacon(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi)
|
||||
{
|
||||
uint32_t beacon_index = ena_get_temp_beacon_index(rpi, aem);
|
||||
|
||||
if (beacon_index == -1)
|
||||
{
|
||||
temp_beacons[temp_beacons_count].timestamp_first = unix_timestamp;
|
||||
memcpy(temp_beacons[temp_beacons_count].rpi, rpi, ENA_KEY_LENGTH);
|
||||
memcpy(temp_beacons[temp_beacons_count].aem, aem, ENA_AEM_METADATA_LENGTH);
|
||||
temp_beacons[temp_beacons_count].rssi = rssi;
|
||||
temp_beacons[temp_beacons_count].timestamp_last = unix_timestamp;
|
||||
beacon_index = ena_storage_add_temp_beacon(&temp_beacons[temp_beacons_count]);
|
||||
|
||||
ESP_LOGD(ENA_BEACON_LOG, "New temporary beacon at %d with timestamp %u", temp_beacons_count, unix_timestamp);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_BEACON_LOG, "RSSI %d", rssi);
|
||||
|
||||
if (beacon_index != temp_beacons_count)
|
||||
{
|
||||
ESP_LOGW(ENA_BEACON_LOG, "last temporary beacon index does not match array index!");
|
||||
}
|
||||
temp_beacons_count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_beacons[beacon_index].rssi = (temp_beacons[beacon_index].rssi + rssi) / 2;
|
||||
temp_beacons[beacon_index].timestamp_last = unix_timestamp;
|
||||
ESP_LOGD(ENA_BEACON_LOG, "New Timestamp for temporary beacon %d: %u", beacon_index, unix_timestamp);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_BEACON_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ena_storage_set_temp_beacon(temp_beacons_count, &temp_beacons[temp_beacons_count]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include "esp_log.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#include "ena-bluetooth-advertise.h"
|
||||
|
||||
static esp_ble_adv_params_t ena_adv_params = {
|
||||
.adv_int_min = 0x140, // 200 ms
|
||||
.adv_int_max = 0x190, // 250 ms
|
||||
.adv_type = ADV_TYPE_NONCONN_IND,
|
||||
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
|
||||
.channel_map = ADV_CHNL_ALL,
|
||||
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
||||
};
|
||||
|
||||
void ena_bluetooth_advertise_start(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_ble_gap_start_advertising(&ena_adv_params));
|
||||
}
|
||||
|
||||
void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t *tek)
|
||||
{
|
||||
uint8_t rpik[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t rpi[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t aemk[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
|
||||
|
||||
ena_crypto_rpik(rpik, tek);
|
||||
|
||||
ena_crypto_rpi(rpi, rpik, enin);
|
||||
|
||||
ena_crypto_aemk(aemk, tek);
|
||||
|
||||
ena_crypto_aem(aem, aemk, rpi, esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_ADV));
|
||||
|
||||
uint8_t adv_raw_data[31];
|
||||
// FLAG??? skipped on sniffed android packages!?
|
||||
adv_raw_data[0] = 0x02;
|
||||
adv_raw_data[1] = 0x01;
|
||||
adv_raw_data[2] = ENA_BLUETOOTH_TAG_DATA;
|
||||
|
||||
// SERVICE UUID
|
||||
adv_raw_data[3] = 0x03;
|
||||
adv_raw_data[4] = 0x03;
|
||||
adv_raw_data[5] = 0x6F;
|
||||
adv_raw_data[6] = 0xFD;
|
||||
|
||||
// SERVICE DATA
|
||||
adv_raw_data[7] = 0x17;
|
||||
adv_raw_data[8] = 0x16;
|
||||
|
||||
adv_raw_data[9] = 0x6F;
|
||||
adv_raw_data[10] = 0xFD;
|
||||
|
||||
for (int i = 0; i < ENA_KEY_LENGTH; i++)
|
||||
{
|
||||
adv_raw_data[i + 11] = rpi[i];
|
||||
}
|
||||
for (int i = 0; i < ENA_AEM_METADATA_LENGTH; i++)
|
||||
{
|
||||
adv_raw_data[i + ENA_KEY_LENGTH + 11] = aem[i];
|
||||
}
|
||||
|
||||
esp_ble_gap_config_adv_data_raw(adv_raw_data, sizeof(adv_raw_data));
|
||||
|
||||
ESP_LOGD(ENA_ADVERTISE_LOG, "payload for ENIN %u", enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_ADVERTISE_LOG, adv_raw_data, sizeof(adv_raw_data), ESP_LOG_DEBUG);
|
||||
}
|
||||
|
||||
void ena_bluetooth_advertise_stop(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_ble_gap_stop_advertising());
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-beacons.h"
|
||||
|
||||
#include "ena-bluetooth-scan.h"
|
||||
|
||||
static int scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
|
||||
|
||||
static const uint16_t ENA_SERVICE_UUID = 0xFD6F;
|
||||
|
||||
static esp_ble_scan_params_t ena_scan_params = {
|
||||
.scan_type = BLE_SCAN_TYPE_ACTIVE,
|
||||
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
|
||||
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
|
||||
.scan_interval = 0x50, // don't know good parameters, just copied
|
||||
.scan_window = 0x30, // don't know good parameters, just copied
|
||||
.scan_duplicate = BLE_SCAN_DUPLICATE_ENABLE,
|
||||
};
|
||||
|
||||
void ena_bluetooth_scan_event_callback(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
||||
{
|
||||
|
||||
uint32_t unix_timestamp = (uint32_t)time(NULL);
|
||||
esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param;
|
||||
switch (event)
|
||||
{
|
||||
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
|
||||
ESP_LOGD(ENA_SCAN_LOG, "start scanning...");
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
|
||||
ESP_LOGD(ENA_SCAN_LOG, "stopped scanning...");
|
||||
ena_beacons_temp_refresh(unix_timestamp);
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_RESULT_EVT:
|
||||
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT)
|
||||
{
|
||||
uint8_t service_uuid_length = 0;
|
||||
uint8_t *service_uuid_data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, 0x03, &service_uuid_length);
|
||||
// check for ENA Service UUID
|
||||
if (service_uuid_length == sizeof(ENA_SERVICE_UUID) && memcmp(service_uuid_data, &ENA_SERVICE_UUID, service_uuid_length) == 0)
|
||||
{
|
||||
uint8_t service_data_length = 0;
|
||||
uint8_t *service_data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv, 0x16, &service_data_length);
|
||||
if (service_data_length != (sizeof(ENA_SERVICE_UUID) + ENA_KEY_LENGTH + ENA_AEM_METADATA_LENGTH))
|
||||
{
|
||||
ESP_LOGW(ENA_SCAN_LOG, "received ENA Service with invalid payload");
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t *rpi = malloc(ENA_KEY_LENGTH);
|
||||
memcpy(rpi, &service_data[sizeof(ENA_SERVICE_UUID)], ENA_KEY_LENGTH);
|
||||
uint8_t *aem = malloc(ENA_AEM_METADATA_LENGTH);
|
||||
memcpy(aem, &service_data[sizeof(ENA_SERVICE_UUID) + ENA_KEY_LENGTH], ENA_AEM_METADATA_LENGTH);
|
||||
ena_beacon(unix_timestamp, rpi, aem, p->scan_rst.rssi);
|
||||
free(rpi);
|
||||
free(aem);
|
||||
}
|
||||
}
|
||||
else if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT)
|
||||
{
|
||||
scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
|
||||
ena_beacons_temp_refresh(unix_timestamp);
|
||||
ESP_LOGD(ENA_SCAN_LOG, "finished scanning...");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// nothing
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ena_bluetooth_scan_init(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&ena_scan_params));
|
||||
ESP_ERROR_CHECK(esp_ble_gap_register_callback(ena_bluetooth_scan_event_callback));
|
||||
// init temporary beacons
|
||||
ena_beacons_temp_refresh((uint32_t)time(NULL));
|
||||
}
|
||||
|
||||
void ena_bluetooth_scan_start(uint32_t duration)
|
||||
{
|
||||
scan_status = ENA_SCAN_STATUS_SCANNING;
|
||||
ESP_ERROR_CHECK(esp_ble_gap_start_scanning(duration));
|
||||
}
|
||||
|
||||
void ena_bluetooth_scan_stop(void)
|
||||
{
|
||||
scan_status = ENA_SCAN_STATUS_WAITING;
|
||||
ESP_ERROR_CHECK(esp_ble_gap_stop_scanning());
|
||||
}
|
||||
|
||||
int ena_bluetooth_scan_get_status(void)
|
||||
{
|
||||
return scan_status;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/hkdf.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ESP_CRYPTO_LOG "ESP-CRYPTO"
|
||||
|
||||
static mbedtls_ctr_drbg_context ctr_drbg;
|
||||
|
||||
void ena_crypto_init(void)
|
||||
{
|
||||
mbedtls_entropy_context entropy;
|
||||
uint8_t pers[] = "Exposure Notifcation API esp32";
|
||||
int ret;
|
||||
|
||||
mbedtls_entropy_init(&entropy);
|
||||
mbedtls_ctr_drbg_init(&ctr_drbg);
|
||||
|
||||
if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, pers, sizeof(pers))) != 0)
|
||||
{
|
||||
ESP_LOGE(ESP_CRYPTO_LOG, " failed\n ! mbedtls_ctr_drbg_init returned -0x%04x\n", -ret);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ena_crypto_enin(uint32_t unix_epoch_time)
|
||||
{
|
||||
return unix_epoch_time / ENA_TIME_WINDOW;
|
||||
}
|
||||
|
||||
void ena_crypto_tek(uint8_t *tek)
|
||||
{
|
||||
int ret;
|
||||
if ((ret = mbedtls_ctr_drbg_random(&ctr_drbg, tek, ENA_KEY_LENGTH)) != 0)
|
||||
{
|
||||
ESP_LOGE(ESP_CRYPTO_LOG, " failed\n ! mbedtls_ctr_drbg_random returned -0x%04x\n", -ret);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_crypto_rpik(uint8_t *rpik, uint8_t *tek)
|
||||
{
|
||||
const uint8_t rpik_info[] = "EN-RPIK";
|
||||
mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), NULL, 0, tek, ENA_KEY_LENGTH, rpik_info, sizeof(rpik_info), rpik, ENA_KEY_LENGTH);
|
||||
}
|
||||
|
||||
void ena_crypto_rpi(uint8_t *rpi, uint8_t *rpik, uint32_t enin)
|
||||
{
|
||||
uint8_t padded_data[] = "EN-RPI";
|
||||
padded_data[12] = (enin & 0x000000ff);
|
||||
padded_data[13] = (enin & 0x0000ff00) >> 8;
|
||||
padded_data[14] = (enin & 0x00ff0000) >> 16;
|
||||
padded_data[15] = (enin & 0xff000000) >> 24;
|
||||
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
mbedtls_aes_setkey_enc(&aes, rpik, ENA_KEY_LENGTH * 8);
|
||||
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, padded_data, rpi);
|
||||
mbedtls_aes_free(&aes);
|
||||
}
|
||||
|
||||
void ena_crypto_aemk(uint8_t *aemk, uint8_t *tek)
|
||||
{
|
||||
uint8_t aemkInfo[] = "EN-AEMK";
|
||||
mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), NULL, 0, tek, ENA_KEY_LENGTH, aemkInfo, sizeof(aemkInfo), aemk, ENA_KEY_LENGTH);
|
||||
}
|
||||
|
||||
void ena_crypto_aem(uint8_t *aem, uint8_t *aemk, uint8_t *rpi, uint8_t power_level)
|
||||
{
|
||||
uint8_t metadata[ENA_AEM_METADATA_LENGTH];
|
||||
metadata[0] = 0b01000000;
|
||||
metadata[1] = power_level;
|
||||
size_t count = 0;
|
||||
uint8_t sb[16] = {0};
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
mbedtls_aes_setkey_enc(&aes, aemk, ENA_KEY_LENGTH * 8);
|
||||
mbedtls_aes_crypt_ctr(&aes, ENA_AEM_METADATA_LENGTH, &count, rpi, sb, metadata, aem);
|
||||
mbedtls_aes_free(&aes);
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_bt.h"
|
||||
#include "esp_bt_main.h"
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-storage.h"
|
||||
#include "ena-bluetooth-scan.h"
|
||||
#include "ena-bluetooth-advertise.h"
|
||||
|
||||
#include "ena.h"
|
||||
|
||||
static ena_tek_t last_tek; // last ENIN
|
||||
static uint32_t next_rpi_timestamp; // next rpi
|
||||
|
||||
void ena_next_rpi_timestamp(uint32_t timestamp)
|
||||
{
|
||||
int random_interval = esp_random() % (2 * ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL);
|
||||
if (random_interval > ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL)
|
||||
{
|
||||
random_interval = ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL - random_interval;
|
||||
}
|
||||
next_rpi_timestamp = timestamp + ENA_BT_ROTATION_TIMEOUT_INTERVAL + random_interval;
|
||||
ESP_LOGD(ENA_LOG, "next rpi at %u (%u from %u)", next_rpi_timestamp, (ENA_BT_ROTATION_TIMEOUT_INTERVAL + random_interval), timestamp);
|
||||
}
|
||||
|
||||
void ena_run(void *pvParameter)
|
||||
{
|
||||
uint32_t unix_timestamp = 0;
|
||||
uint32_t current_enin = 0;
|
||||
while (1)
|
||||
{
|
||||
unix_timestamp = (uint32_t)time(NULL);
|
||||
current_enin = ena_crypto_enin(unix_timestamp);
|
||||
if (current_enin - last_tek.enin >= last_tek.rolling_period)
|
||||
{
|
||||
ena_crypto_tek(last_tek.key_data);
|
||||
last_tek.enin = current_enin;
|
||||
// validity only to next day 00:00
|
||||
last_tek.rolling_period = ENA_TEK_ROLLING_PERIOD - (last_tek.enin % ENA_TEK_ROLLING_PERIOD);
|
||||
ena_storage_write_tek(&last_tek);
|
||||
}
|
||||
|
||||
// change RPI
|
||||
if (unix_timestamp >= next_rpi_timestamp)
|
||||
{
|
||||
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_SCANNING)
|
||||
{
|
||||
ena_bluetooth_scan_stop();
|
||||
}
|
||||
ena_bluetooth_advertise_stop();
|
||||
ena_bluetooth_advertise_set_payload(current_enin, last_tek.key_data);
|
||||
ena_bluetooth_advertise_start();
|
||||
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_WAITING)
|
||||
{
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
}
|
||||
ena_next_rpi_timestamp(unix_timestamp);
|
||||
}
|
||||
|
||||
// scan
|
||||
if (unix_timestamp % ENA_SCANNING_INTERVAL == 0 && ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_NOT_SCANNING)
|
||||
{
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
}
|
||||
|
||||
// one second loop correct?!
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_start(void)
|
||||
{
|
||||
#if (CONFIG_ENA_STORAGE_ERASE)
|
||||
ena_storage_erase();
|
||||
#endif
|
||||
// init NVS for BLE
|
||||
esp_err_t ret;
|
||||
ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
}
|
||||
|
||||
// init BLE
|
||||
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
|
||||
{
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
|
||||
while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
|
||||
}
|
||||
|
||||
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_bluedroid_init());
|
||||
}
|
||||
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_INITIALIZED)
|
||||
{
|
||||
ESP_ERROR_CHECK(esp_bluedroid_enable());
|
||||
}
|
||||
|
||||
// new bluetooth address nesseccary?
|
||||
uint8_t bt_address[ESP_BD_ADDR_LEN];
|
||||
esp_fill_random(bt_address, ESP_BD_ADDR_LEN);
|
||||
bt_address[0] |= 0xC0;
|
||||
|
||||
ESP_ERROR_CHECK(esp_ble_gap_set_rand_addr(bt_address));
|
||||
|
||||
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9));
|
||||
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P9));
|
||||
ESP_ERROR_CHECK(esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P9));
|
||||
ESP_ERROR_CHECK(esp_ble_gap_config_local_privacy(true));
|
||||
|
||||
// init ENA
|
||||
ena_crypto_init();
|
||||
|
||||
uint32_t unix_timestamp = (uint32_t)time(NULL);
|
||||
|
||||
uint32_t current_enin = ena_crypto_enin(unix_timestamp);
|
||||
uint32_t tek_count = ena_storage_read_last_tek(&last_tek);
|
||||
|
||||
ena_next_rpi_timestamp(unix_timestamp);
|
||||
|
||||
// read last TEK or create new
|
||||
if (tek_count == 0 || (current_enin - last_tek.enin) >= last_tek.rolling_period)
|
||||
{
|
||||
ena_crypto_tek(last_tek.key_data);
|
||||
last_tek.enin = ena_crypto_enin(unix_timestamp);
|
||||
// validity only to next day 00:00
|
||||
last_tek.rolling_period = ENA_TEK_ROLLING_PERIOD - (last_tek.enin % ENA_TEK_ROLLING_PERIOD);
|
||||
ena_storage_write_tek(&last_tek);
|
||||
}
|
||||
|
||||
// init scan
|
||||
ena_bluetooth_scan_init();
|
||||
|
||||
// init and start advertising
|
||||
ena_bluetooth_advertise_set_payload(current_enin, last_tek.key_data);
|
||||
ena_bluetooth_advertise_start();
|
||||
// initial scan on every start
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
|
||||
// what is a good stack size here?
|
||||
xTaskCreate(&ena_run, "ena_run", configMINIMAL_STACK_SIZE * 8, NULL, 5, NULL);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _ena_BEACON_H_
|
||||
#define _ena_BEACON_H_
|
||||
|
||||
#define ENA_BEACON_LOG "ESP-ENA-beacon" // TAG for Logging
|
||||
#define ENA_BEACON_TRESHOLD (CONFIG_ENA_BEACON_TRESHOLD) // meet for longer than 5 minutes
|
||||
|
||||
/**
|
||||
* @brief check temporary beacon for threshold or expiring
|
||||
*
|
||||
* This function checks all current temporary beacons if the contact threshold is
|
||||
* reached or if the temporary contact can be discarded.
|
||||
*
|
||||
* @param[in] unix_timestamp current time as UNIX timestamp to compate
|
||||
*
|
||||
*/
|
||||
void ena_beacons_temp_refresh(uint32_t unix_timestamp);
|
||||
|
||||
/**
|
||||
* @brief handle new beacon received from a BLE scan
|
||||
*
|
||||
* This function gets called when a running BLE scan received a new ENA payload.
|
||||
* On already detected RPI this will update just the timestamp and RSSI.
|
||||
*
|
||||
* @param[in] unix_timestamp UNIX timestamp when beacon was made
|
||||
* @param[in] rpi received RPI from scanned payload
|
||||
* @param[in] aem received AEM from scanned payload
|
||||
* @param[in] rssi measured RSSI on scan
|
||||
*
|
||||
*/
|
||||
void ena_beacon(uint32_t unix_timestamp, uint8_t *rpi, uint8_t *aem, int rssi);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _ena_BLUETOOTH_ADVERTISE_H_
|
||||
#define _ena_BLUETOOTH_ADVERTISE_H_
|
||||
|
||||
#define ENA_ADVERTISE_LOG "ESP-ENA-advertise" // TAG for Logging
|
||||
#define ENA_BLUETOOTH_TAG_DATA (0x1A) // Data for BLE payload TAG
|
||||
|
||||
/**
|
||||
* @brief Start BLE advertising
|
||||
*/
|
||||
void ena_bluetooth_advertise_start(void);
|
||||
|
||||
/**
|
||||
* @brief Set payload for BLE advertising
|
||||
*
|
||||
* This will set the payload for based on given ENIN and TEK.
|
||||
*
|
||||
* Source documents (Section: Advertising Payload)
|
||||
*
|
||||
* https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-BluetoothSpecificationv1.2.pdf
|
||||
*
|
||||
* @param[in] enin ENIN defining the start of the tek vadility. This should be the ENIN for the current timestamp
|
||||
* @param[in] tek pointer to the TEK used to encrypt the payload.
|
||||
*/
|
||||
void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief Stop BLE advertising
|
||||
*/
|
||||
void ena_bluetooth_advertise_stop(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,63 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _ena_BLUETOOTH_SCAN_H_
|
||||
#define _ena_BLUETOOTH_SCAN_H_
|
||||
|
||||
#define ENA_SCAN_LOG "ESP-ENA-scan" // TAG for Logging
|
||||
#define ENA_SCANNING_TIME (CONFIG_ENA_SCANNING_TIME) // time how long a scan should run
|
||||
#define ENA_SCANNING_INTERVAL (CONFIG_ENA_SCANNING_INTERVAL) // interval for next scan to happen
|
||||
|
||||
/**
|
||||
* @brief status of BLE scan
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ENA_SCAN_STATUS_SCANNING = 0, // scan is running
|
||||
ENA_SCAN_STATUS_NOT_SCANNING, // scan is not running
|
||||
ENA_SCAN_STATUS_WAITING, // scan is not running but stopped manually
|
||||
} ena_bluetooth_scan_status;
|
||||
|
||||
/**
|
||||
* @brief initialize the BLE scanning
|
||||
*
|
||||
*/
|
||||
void ena_bluetooth_scan_init(void);
|
||||
|
||||
/**
|
||||
* @brief start BLE scanning for a given duration
|
||||
*
|
||||
* Source documents (Section: Scanning Behavior)
|
||||
*
|
||||
* https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-BluetoothSpecificationv1.2.pdf
|
||||
*
|
||||
* @param[in] duration duration of the scan in seconds
|
||||
*/
|
||||
void ena_bluetooth_scan_start(uint32_t duration);
|
||||
|
||||
/**
|
||||
* @brief stop a running BLE scanning
|
||||
*/
|
||||
void ena_bluetooth_scan_stop(void);
|
||||
|
||||
/**
|
||||
* @brief return the current scanning status
|
||||
*
|
||||
* @return
|
||||
* current scan status
|
||||
*/
|
||||
int ena_bluetooth_scan_get_status(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,120 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _ena_CRYPTO_H_
|
||||
#define _ena_CRYPTO_H_
|
||||
|
||||
#define ENA_TIME_WINDOW (600) // time window every 10 minutes
|
||||
#define ENA_KEY_LENGTH (16) // key length
|
||||
#define ENA_AEM_METADATA_LENGTH (4) // size of metadata
|
||||
#define ENA_TEK_ROLLING_PERIOD (CONFIG_ENA_TEK_ROLLING_PERIOD) // TEKRollingPeriod
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* @brief initialize cryptography
|
||||
*
|
||||
* This initialize the cryptography by setting up entropy.
|
||||
*/
|
||||
void ena_crypto_init(void);
|
||||
|
||||
/**
|
||||
* @brief calculate ENIntervalNumber (ENIN) for given UNIX timestamp
|
||||
*
|
||||
* Source documents (Section: ENIntervalNumber)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
*
|
||||
* @param[in] unix_timestamp UNIX Timestamp to calculate ENIN for
|
||||
*
|
||||
* @return
|
||||
* ENIN for given timestamp
|
||||
*/
|
||||
uint32_t ena_crypto_enin(uint32_t unix_timestamp);
|
||||
|
||||
/**
|
||||
* @brief calculate a new random Temporary Exposure Key (TEK)
|
||||
*
|
||||
* Source documents (Section: Temporary Exposure Key)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] tek pointer to the new TEK
|
||||
*/
|
||||
void ena_crypto_tek(uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief calculate a new Rolling Proximity Identifier Key (RPIK) with given TEK
|
||||
*
|
||||
* Source documents (Section: Rolling Proximity Identifier Key)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] rpik pointer to the new RPIK
|
||||
* @param[in] tek TEK for calculating RPIK
|
||||
*/
|
||||
void ena_crypto_rpik(uint8_t *rpik, uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief calculate a new Rolling Proximity Identifier with given RPIK and ENIN
|
||||
*
|
||||
* Source documents (Section: Rolling Proximity Identifier)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] rpi pointer to the new RPI
|
||||
* @param[in] rpik RPIK for encrypting RPI
|
||||
* @param[in] enin ENIN to encrypt in RPI
|
||||
*/
|
||||
void ena_crypto_rpi(uint8_t *rpi, uint8_t *rpik, uint32_t enin);
|
||||
|
||||
/**
|
||||
* @brief calculate a new Associated Encrypted Metadata Key (AEMK) with given TEK
|
||||
*
|
||||
* Source documents (Section: Associated Encrypted Metadata Key)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] aemk pointer to the new AEMK
|
||||
* @param[in] tek TEK for calculating AEMK
|
||||
*/
|
||||
void ena_crypto_aemk(uint8_t *aemk, uint8_t *tek);
|
||||
|
||||
/**
|
||||
* @brief create Associated Encrypted Metadata (AEM) with given AEMK along the RPI
|
||||
*
|
||||
* Source documents (Section: Associated Encrypted Metadata)
|
||||
*
|
||||
* https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf
|
||||
*
|
||||
* https://covid19-static.cdn-apple.com/applications/covid19/current/static/detection-tracing/pdf/ExposureNotification-CryptographySpecificationv1.2.pdf
|
||||
*
|
||||
* @param[out] aem pointer to the new AEM
|
||||
* @param[in] aemk AEMK for encrypting AEM
|
||||
* @param[in] rpi RPI for encrypting AEM
|
||||
* @param[in] power_level BLE power level to encrypt in AEM
|
||||
*/
|
||||
void ena_crypto_aem(uint8_t *aem, uint8_t *aemk, uint8_t *rpi, uint8_t power_level);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,221 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _ena_STORAGE_H_
|
||||
#define _ena_STORAGE_H_
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ENA_STORAGE_LOG "ESP-ENA-storage" // TAG for Logging
|
||||
#define ENA_STORAGE_PARTITION_NAME (CONFIG_ENA_STORAGE_PARTITION_NAME) // name of partition to use for storing
|
||||
#define ENA_STORAGE_START_ADDRESS (CONFIG_ENA_STORAGE_START_ADDRESS) // start address of storage
|
||||
#define ENA_STORAGE_TEK_MAX (CONFIG_ENA_STORAGE_TEK_MAX) // Period of storing TEKs // length of a stored beacon -> RPI keysize + AEM size + 4 Bytes for ENIN + 4 Bytes for RSSI
|
||||
#define ENA_STORAGE_TEMP_BEACONS_MAX (CONFIG_ENA_STORAGE_TEMP_BEACONS_MAX) // Maximum number of temporary stored beacons
|
||||
|
||||
/**
|
||||
* @brief structure for TEK
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t key_data[ENA_KEY_LENGTH]; // key data for encryption
|
||||
uint32_t enin; // ENIN marking start of validity
|
||||
uint8_t rolling_period; // period after validity start to mark key as expired
|
||||
} ena_tek_t;
|
||||
|
||||
/**
|
||||
* @brief sturcture for storing a temporary beacon
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t rpi[ENA_KEY_LENGTH]; // received RPI of beacon
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH]; // received AEM of beacon
|
||||
uint32_t timestamp_first; // timestamp of first recognition
|
||||
uint32_t timestamp_last; // timestamp of last recognition
|
||||
int rssi; // average measured RSSI
|
||||
} ena_temp_beacon_t;
|
||||
|
||||
/**
|
||||
* @brief sturcture for permanently storing a beacon after threshold reached
|
||||
*/
|
||||
typedef struct __attribute__((__packed__))
|
||||
{
|
||||
uint8_t rpi[ENA_KEY_LENGTH]; // received RPI of beacon
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH]; // received AEM of beacon
|
||||
uint32_t timestamp; // timestamp of last recognition
|
||||
int rssi; // average measured RSSI
|
||||
} ena_beacon_t;
|
||||
|
||||
/**
|
||||
* @brief read bytes at given address
|
||||
*
|
||||
* @param[in] address the address to read bytes from
|
||||
* @param[out] data pointer to write the read data
|
||||
* @param[in] size how many bytes to read
|
||||
*/
|
||||
void ena_storage_read(size_t address, void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief store bytes at given address
|
||||
*
|
||||
* @param[in] address the address to write bytes to
|
||||
* @param[in] data pointer to the data to write
|
||||
* @param[in] size how many bytes to write
|
||||
*/
|
||||
void ena_storage_write(size_t address, void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief deletes bytes at given address and shift other data back
|
||||
*
|
||||
* @param[in] address the address to delete from
|
||||
* @param[in] end_address the address to mark end of shift
|
||||
* @param[in] size how many bytes to delete
|
||||
*/
|
||||
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size);
|
||||
|
||||
/**
|
||||
* @brief get last stored TEK
|
||||
*
|
||||
* @param[out] tek pointer to write last TEK to
|
||||
*
|
||||
* @return
|
||||
* total number of TEKs stored
|
||||
*/
|
||||
uint32_t ena_storage_read_last_tek(ena_tek_t *tek);
|
||||
|
||||
/**
|
||||
* @brief store given TEK
|
||||
*
|
||||
* This will store the given TEK as new TEK.
|
||||
*
|
||||
* @param[in] tek the tek to store
|
||||
*/
|
||||
void ena_storage_write_tek(ena_tek_t *tek);
|
||||
|
||||
/**
|
||||
* @brief get number of stored temporary beacons
|
||||
*
|
||||
* @return
|
||||
* total number of temporary beacons stored
|
||||
*/
|
||||
uint32_t ena_storage_temp_beacons_count(void);
|
||||
|
||||
/**
|
||||
* @brief get temporary beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the temporary beacon to read
|
||||
* @param[out] beacon pointer to temporary to write to
|
||||
*/
|
||||
void ena_storage_get_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief store temporary beacon
|
||||
*
|
||||
* @param[in] beacon new temporary beacon to store
|
||||
*
|
||||
* @return
|
||||
* index of new stored beacon
|
||||
*/
|
||||
uint32_t ena_storage_add_temp_beacon(ena_temp_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief store temporary beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the temporary beacon to overwrite
|
||||
* @param[in] beacon temporary beacon to store
|
||||
*/
|
||||
void ena_storage_set_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief remove temporary beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the temporary beacon to remove
|
||||
*/
|
||||
void ena_storage_remove_temp_beacon(uint32_t index);
|
||||
|
||||
/**
|
||||
* @brief get number of permanently stored beacons
|
||||
*
|
||||
* @return
|
||||
* total number of beacons stored
|
||||
*/
|
||||
uint32_t ena_storage_beacons_count(void);
|
||||
|
||||
/**
|
||||
* @brief get permanently stored beacon at given index
|
||||
*
|
||||
* @param[in] index the index of the beacon to read
|
||||
* @param[out] beacon pointer to to write to
|
||||
*/
|
||||
void ena_storage_get_beacon(uint32_t index, ena_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief permanently store beacon
|
||||
*
|
||||
* @param[in] beacon new beacon to permanently store
|
||||
*/
|
||||
void ena_storage_add_beacon(ena_beacon_t *beacon);
|
||||
|
||||
/**
|
||||
* @brief erase the storage
|
||||
*
|
||||
* This function completely deletes all stored data and resets the counters
|
||||
* of TEKs, temporary beacon and beacon to zero.
|
||||
*/
|
||||
void ena_storage_erase(void);
|
||||
|
||||
/**
|
||||
* @brief erase all stored TEKs
|
||||
*
|
||||
* This function deletes all stored TEKs and resets counter to zero.
|
||||
*/
|
||||
void ena_storage_erase_tek(void);
|
||||
|
||||
/**
|
||||
* @brief erase all stored temporary beacons
|
||||
*
|
||||
* This function deletes all stored temporary beacons and resets counter to zero.
|
||||
*/
|
||||
void ena_storage_erase_temporary_beacon(void);
|
||||
|
||||
/**
|
||||
* @brief erase all permanently stored beacons
|
||||
*
|
||||
* This function deletes all stored beacons and resets counter to zero.
|
||||
*/
|
||||
void ena_storage_erase_beacon(void);
|
||||
|
||||
/**
|
||||
* @brief dump all stored TEKs to serial output
|
||||
*
|
||||
* This function prints all stored TEKs to serial output in
|
||||
* the following CSV format: #,enin,tek
|
||||
*/
|
||||
void ena_storage_dump_tek(void);
|
||||
|
||||
/**
|
||||
* @brief dump all stored temporary beacons to serial output
|
||||
*
|
||||
* This function prints all stored temporary beacons to serial output in
|
||||
* the following CSV format: #,timestamp_first,timestamp_last,rpi,aem,rssi
|
||||
*/
|
||||
void ena_storage_dump_temp_beacons(void);
|
||||
|
||||
/**
|
||||
* @brief dump all stored beacons to serial output
|
||||
*
|
||||
* This function prints all stored beacons to serial output in
|
||||
* the following CSV format: #,timestamp,rpi,aem,rssi
|
||||
*/
|
||||
void ena_storage_dump_beacons(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef _ena_H_
|
||||
#define _ena_H_
|
||||
|
||||
#define ENA_LOG "ESP-ENA" // TAG for Logging
|
||||
#define ENA_BT_ROTATION_TIMEOUT_INTERVAL (CONFIG_ENA_BT_ROTATION_TIMEOUT_INTERVAL) // change advertising payload and therefore the BT address
|
||||
#define ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL (CONFIG_ENA_BT_RANDOMIZE_ROTATION_TIMEOUT_INTERVAL) // random intervall change for BT address change
|
||||
|
||||
/**
|
||||
* @brief Start Exposure Notification API
|
||||
*
|
||||
* This initializes the complete stack of ESP_ENA. It will initialize BLE module and
|
||||
* starting a task for managing advertising and scanning processes.
|
||||
*
|
||||
*/
|
||||
void ena_start(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,439 @@
|
||||
// Copyright 2020 Lukas Haubaum
|
||||
//
|
||||
// Licensed under the GNU Affero General Public License, Version 3;
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// https://www.gnu.org/licenses/agpl-3.0.html
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_partition.h"
|
||||
|
||||
#include "ena-storage.h"
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define BLOCK_SIZE (4096)
|
||||
|
||||
const int ENA_STORAGE_TEK_COUNT_ADDRESS = (ENA_STORAGE_START_ADDRESS); // starting address for TEK COUNT
|
||||
const int ENA_STORAGE_TEK_START_ADDRESS = (ENA_STORAGE_TEK_COUNT_ADDRESS + sizeof(uint32_t));
|
||||
const int ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS = (ENA_STORAGE_TEK_START_ADDRESS + sizeof(ena_tek_t) * ENA_STORAGE_TEK_MAX);
|
||||
const int ENA_STORAGE_TEMP_BEACONS_START_ADDRESS = (ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS + sizeof(uint32_t));
|
||||
const int ENA_STORAGE_BEACONS_COUNT_ADDRESS = (ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + sizeof(ena_temp_beacon_t) * ENA_STORAGE_TEMP_BEACONS_MAX);
|
||||
const int ENA_STORAGE_BEACONS_START_ADDRESS = (ENA_STORAGE_BEACONS_COUNT_ADDRESS + sizeof(uint32_t));
|
||||
|
||||
void ena_storage_read(size_t address, void *data, size_t size)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read");
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, address, data, size));
|
||||
vTaskDelay(1);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read data at %u", address);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, data, size, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read");
|
||||
}
|
||||
|
||||
void ena_storage_write(size_t address, void *data, size_t size)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write");
|
||||
const int block_num = address / BLOCK_SIZE;
|
||||
// check for overflow
|
||||
if (address + size <= (block_num + 1) * BLOCK_SIZE)
|
||||
{
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
const int block_start = block_num * BLOCK_SIZE;
|
||||
const int block_address = address - block_start;
|
||||
void *buffer = malloc(BLOCK_SIZE);
|
||||
if (buffer == NULL)
|
||||
{
|
||||
ESP_LOGE(ENA_STORAGE_LOG, "Warning %s malloc low memory", "buffer");
|
||||
return;
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read block %d buffer: start %d size %u", block_num, block_start, BLOCK_SIZE);
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, block_start, buffer, BLOCK_SIZE));
|
||||
vTaskDelay(1);
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_start, BLOCK_SIZE));
|
||||
|
||||
memcpy((buffer + block_address), data, size);
|
||||
|
||||
ESP_ERROR_CHECK(esp_partition_write(partition, block_start, buffer, BLOCK_SIZE));
|
||||
free(buffer);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write data at %u", address);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, data, size, ESP_LOG_DEBUG);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num);
|
||||
const size_t block2_address = (block_num + 1) * BLOCK_SIZE;
|
||||
const size_t data2_size = address + size - block2_address;
|
||||
const size_t data1_size = size - data2_size;
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "block1_address %d, block1_size %d (block %d)", address, data1_size, block_num);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "block2_address %d, block2_size %d (block %d)", block2_address, data2_size, block_num + 1);
|
||||
void *data1 = malloc(data1_size);
|
||||
memcpy(data1, data, data1_size);
|
||||
ena_storage_write(address, data1, data1_size);
|
||||
free(data1);
|
||||
|
||||
void *data2 = malloc(data2_size);
|
||||
memcpy(data2, (data + data1_size), data2_size);
|
||||
ena_storage_write(block2_address, data2, data2_size);
|
||||
free(data2);
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write");
|
||||
}
|
||||
|
||||
void ena_storage_shift_delete(size_t address, size_t end_address, size_t size)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_shift_delete");
|
||||
|
||||
int block_num_start = address / BLOCK_SIZE;
|
||||
// check for overflow
|
||||
if (address + size <= (block_num_start + 1) * BLOCK_SIZE)
|
||||
{
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
|
||||
int block_num_end = end_address / BLOCK_SIZE;
|
||||
size_t block_start = address - block_num_start * BLOCK_SIZE;
|
||||
while (block_num_end >= block_num_start)
|
||||
{
|
||||
|
||||
void *buffer = malloc(BLOCK_SIZE);
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, block_num_start * BLOCK_SIZE, buffer, BLOCK_SIZE));
|
||||
vTaskDelay(1);
|
||||
// shift inside buffer
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "shift block %d from %u to %u with size %u", block_num_start, (block_start + size), block_start, (BLOCK_SIZE - block_start - size));
|
||||
memcpy((buffer + block_start), (buffer + block_start + size), BLOCK_SIZE - block_start - size);
|
||||
if (block_num_end > block_num_start)
|
||||
{
|
||||
void *buffer_next_block = malloc(BLOCK_SIZE);
|
||||
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, (block_num_start + 1) * BLOCK_SIZE, buffer_next_block, BLOCK_SIZE));
|
||||
vTaskDelay(1);
|
||||
// shift from next block
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "shift next block size %u", size);
|
||||
memcpy((buffer + BLOCK_SIZE - size), buffer_next_block, size);
|
||||
free(buffer_next_block);
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_num_start * BLOCK_SIZE, BLOCK_SIZE));
|
||||
ESP_ERROR_CHECK(esp_partition_write(partition, block_num_start * BLOCK_SIZE, buffer, BLOCK_SIZE));
|
||||
free(buffer);
|
||||
|
||||
block_num_start++;
|
||||
block_start = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num_start);
|
||||
const size_t block1_address = address;
|
||||
const size_t block2_address = (block_num_start + 1) * BLOCK_SIZE;
|
||||
const size_t data2_size = address + size - block2_address;
|
||||
const size_t data1_size = size - data2_size;
|
||||
ena_storage_shift_delete(block1_address, block2_address, data1_size);
|
||||
ena_storage_shift_delete(block2_address, end_address - data1_size, data2_size);
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_shift_delete");
|
||||
}
|
||||
|
||||
uint32_t ena_storage_read_last_tek(ena_tek_t *tek)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_tek");
|
||||
uint32_t tek_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
if (tek_count < 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
uint8_t index = (tek_count % ENA_STORAGE_TEK_MAX) - 1;
|
||||
ena_storage_read(ENA_STORAGE_TEK_START_ADDRESS + index * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read last tek %u:", tek->enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek->key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_tek");
|
||||
return tek_count;
|
||||
}
|
||||
|
||||
void ena_storage_write_tek(ena_tek_t *tek)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_tek");
|
||||
|
||||
uint32_t tek_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
uint8_t index = (tek_count % ENA_STORAGE_TEK_MAX);
|
||||
ena_storage_write(ENA_STORAGE_TEK_START_ADDRESS + index * sizeof(ena_tek_t), tek, sizeof(ena_tek_t));
|
||||
|
||||
tek_count++;
|
||||
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write tek: ENIN %u", tek->enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek->key_data, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_tek");
|
||||
}
|
||||
|
||||
uint32_t ena_storage_temp_beacons_count(void)
|
||||
{
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_temp_beacons_count");
|
||||
uint32_t count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read temp contancts count: %u", count);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_temp_beacons_count");
|
||||
return count;
|
||||
}
|
||||
|
||||
void ena_storage_get_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_temp_beacon");
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_temp_beacon_t), beacon, sizeof(ena_temp_beacon_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read temp beacon: first %u, last %u and rssi %d", beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_temp_beacon");
|
||||
}
|
||||
|
||||
uint32_t ena_storage_add_temp_beacon(ena_temp_beacon_t *beacon)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_add_temp_beacon");
|
||||
uint32_t count = ena_storage_temp_beacons_count();
|
||||
// overwrite older temporary beacons?!
|
||||
uint8_t index = count % ENA_STORAGE_TEMP_BEACONS_MAX;
|
||||
ena_storage_set_temp_beacon(index, beacon);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "add temp beacon at %u: first %u, last %u and rssi %d", index, beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
count++;
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_add_temp_beacon");
|
||||
return count - 1;
|
||||
}
|
||||
|
||||
void ena_storage_set_temp_beacon(uint32_t index, ena_temp_beacon_t *beacon)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_set_temp_beacon");
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_temp_beacon_t), beacon, sizeof(ena_temp_beacon_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "set temp beacon at %u: first %u, last %u and rssi %d", index, beacon->timestamp_first, beacon->timestamp_last, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_set_temp_beacon");
|
||||
}
|
||||
|
||||
void ena_storage_remove_temp_beacon(uint32_t index)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_remove_temp_beacon");
|
||||
uint32_t count = ena_storage_temp_beacons_count();
|
||||
size_t address_from = ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + index * sizeof(ena_temp_beacon_t);
|
||||
size_t address_to = ENA_STORAGE_TEMP_BEACONS_START_ADDRESS + count * sizeof(ena_temp_beacon_t);
|
||||
|
||||
ena_storage_shift_delete(address_from, address_to, sizeof(ena_temp_beacon_t));
|
||||
|
||||
count--;
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "remove temp beacon: %u", index);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_remove_temp_beacon");
|
||||
}
|
||||
|
||||
uint32_t ena_storage_beacons_count(void)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_beacons_count");
|
||||
uint32_t count = 0;
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read contancts count: %u", count);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_beacons_count");
|
||||
return count;
|
||||
}
|
||||
|
||||
void ena_storage_get_beacon(uint32_t index, ena_beacon_t *beacon)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_beacon");
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_START_ADDRESS + index * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read beacon: timestamp %u and rssi %d", beacon->timestamp, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_beacon");
|
||||
}
|
||||
|
||||
void ena_storage_add_beacon(ena_beacon_t *beacon)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_beacon");
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
uint32_t count = ena_storage_beacons_count();
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_START_ADDRESS + count * sizeof(ena_beacon_t), beacon, sizeof(ena_beacon_t));
|
||||
count++;
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write beacon: timestamp %u and rssi %d", beacon->timestamp, beacon->rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, beacon->aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_beacon");
|
||||
}
|
||||
|
||||
void ena_storage_erase(void)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase");
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, ENA_STORAGE_PARTITION_NAME);
|
||||
assert(partition);
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased partition %s!", ENA_STORAGE_PARTITION_NAME);
|
||||
|
||||
uint32_t count = 0;
|
||||
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &count, sizeof(uint32_t));
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase");
|
||||
}
|
||||
|
||||
void ena_storage_erase_tek(void)
|
||||
{
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_teks");
|
||||
uint32_t tek_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
uint8_t stored = ENA_STORAGE_TEK_MAX;
|
||||
|
||||
if (tek_count < ENA_STORAGE_TEK_MAX)
|
||||
{
|
||||
stored = tek_count;
|
||||
}
|
||||
|
||||
size_t size = sizeof(uint32_t) + stored * sizeof(ena_tek_t);
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, zeros, size);
|
||||
free(zeros);
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d teks (size %u at %u)", stored, size, ENA_STORAGE_TEK_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_teks");
|
||||
}
|
||||
|
||||
void ena_storage_erase_temporary_beacon(void)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_temporary_beacons");
|
||||
uint32_t beacon_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
uint32_t stored = ENA_STORAGE_TEMP_BEACONS_MAX;
|
||||
|
||||
if (beacon_count < ENA_STORAGE_TEMP_BEACONS_MAX)
|
||||
{
|
||||
stored = beacon_count;
|
||||
}
|
||||
|
||||
size_t size = sizeof(uint32_t) + stored * sizeof(ena_temp_beacon_t);
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, zeros, size);
|
||||
free(zeros);
|
||||
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d temporary beacons (size %u at %u)", stored, size, ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_temporary_beacons");
|
||||
}
|
||||
|
||||
void ena_storage_erase_beacon(void)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_erase_beacon");
|
||||
uint32_t beacon_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
|
||||
size_t size = sizeof(uint32_t) + beacon_count * sizeof(ena_beacon_t);
|
||||
uint8_t *zeros = calloc(size, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_BEACONS_COUNT_ADDRESS, zeros, size);
|
||||
free(zeros);
|
||||
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "erased %d beacons (size %u at %u)", beacon_count, size, ENA_STORAGE_BEACONS_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase_beacon");
|
||||
}
|
||||
|
||||
void ena_storage_dump_hash_array(uint8_t *data, size_t size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
printf("%02x", data[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" %02x", data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_tek(void)
|
||||
{
|
||||
ena_tek_t tek;
|
||||
uint32_t tek_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEK_COUNT_ADDRESS, &tek_count, sizeof(uint32_t));
|
||||
uint8_t stored = ENA_STORAGE_TEK_MAX;
|
||||
|
||||
if (tek_count < ENA_STORAGE_TEK_MAX)
|
||||
{
|
||||
stored = tek_count;
|
||||
}
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u TEKs (%u stored)\n", tek_count, stored);
|
||||
printf("#,enin,tek,rolling_period\n");
|
||||
for (int i = 0; i < stored; i++)
|
||||
{
|
||||
|
||||
size_t address = ENA_STORAGE_TEK_START_ADDRESS + i * sizeof(ena_tek_t);
|
||||
ena_storage_read(address, &tek, sizeof(ena_tek_t));
|
||||
printf("%d,%u,", i, tek.enin);
|
||||
ena_storage_dump_hash_array(tek.key_data, ENA_KEY_LENGTH);
|
||||
printf(",%u\n", tek.rolling_period);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_temp_beacons(void)
|
||||
{
|
||||
ena_temp_beacon_t beacon;
|
||||
uint32_t beacon_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_TEMP_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
uint32_t stored = ENA_STORAGE_TEMP_BEACONS_MAX;
|
||||
|
||||
if (beacon_count < ENA_STORAGE_TEMP_BEACONS_MAX)
|
||||
{
|
||||
stored = beacon_count;
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u temporary beacons (%u stored)\n", beacon_count, stored);
|
||||
printf("#,timestamp_first,timestamp_last,rpi,aem,rssi\n");
|
||||
for (int i = 0; i < stored; i++)
|
||||
{
|
||||
ena_storage_get_temp_beacon(i, &beacon);
|
||||
printf("%d,%u,%u,", i, beacon.timestamp_first, beacon.timestamp_last);
|
||||
ena_storage_dump_hash_array(beacon.rpi, ENA_KEY_LENGTH);
|
||||
printf(",");
|
||||
ena_storage_dump_hash_array(beacon.aem, ENA_AEM_METADATA_LENGTH);
|
||||
printf(",%d\n", beacon.rssi);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_beacons(void)
|
||||
{
|
||||
|
||||
ena_beacon_t beacon;
|
||||
uint32_t beacon_count = 0;
|
||||
ena_storage_read(ENA_STORAGE_BEACONS_COUNT_ADDRESS, &beacon_count, sizeof(uint32_t));
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u beacons\n", beacon_count);
|
||||
printf("#,timestamp,rpi,aem,rssi\n");
|
||||
for (int i = 0; i < beacon_count; i++)
|
||||
{
|
||||
ena_storage_get_beacon(i, &beacon);
|
||||
printf("%d,%u,", i, beacon.timestamp);
|
||||
ena_storage_dump_hash_array(beacon.rpi, ENA_KEY_LENGTH);
|
||||
printf(",");
|
||||
ena_storage_dump_hash_array(beacon.aem, ENA_AEM_METADATA_LENGTH);
|
||||
printf(",%d\n", beacon.rssi);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user