mirror of
https://github.com/Lurkars/esp-ena.git
synced 2026-05-08 20:10:37 +02:00
initial commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
idf_component_register(
|
||||
SRCS "ena-detection.c" "ena-storage.c" "ena-crypto.c" "main.c" "ena.c" "ena-bluetooth-scan.c" "ena-bluetooth-advertise.c" "ena-detection.c"
|
||||
INCLUDE_DIRS ""
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
@@ -0,0 +1,74 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_bt.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_LOGI(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,28 @@
|
||||
/**
|
||||
* provide bluetooth part of Exposure Notification API v1.2 as defined by Apple/Google
|
||||
*
|
||||
* Source documents:
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _ena_BLUETOOTH_ADVERTISE_H_
|
||||
#define _ena_BLUETOOTH_ADVERTISE_H_
|
||||
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
#define ENA_ADVERTISE_LOG "ESP-ENA-advertise" // TAG for Logging
|
||||
#define ENA_BLUETOOTH_TAG_DATA 0x1A
|
||||
|
||||
void ena_bluetooth_advertise_start(void);
|
||||
|
||||
void ena_bluetooth_advertise_set_payload(uint32_t enin, uint8_t tek[]);
|
||||
|
||||
void ena_bluetooth_advertise_stop(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-detection.h"
|
||||
|
||||
#include "ena-bluetooth-scan.h"
|
||||
|
||||
static int scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
|
||||
|
||||
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)
|
||||
{
|
||||
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_LOGI(ENA_SCAN_LOG, "start scanning...");
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
|
||||
ESP_LOGI(ENA_SCAN_LOG, "stopped scanning...");
|
||||
ena_detections_temp_refresh((uint32_t)time(NULL));
|
||||
break;
|
||||
case ESP_GAP_BLE_SCAN_RESULT_EVT:
|
||||
if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_RES_EVT)
|
||||
{
|
||||
// check for ENA Service UUID:
|
||||
if (p->scan_rst.ble_adv[0] == 0x3 && p->scan_rst.ble_adv[1] == 0x3 && p->scan_rst.ble_adv[2] == 0x6f && p->scan_rst.ble_adv[3] == 0xfd)
|
||||
{
|
||||
uint8_t rpi[ENA_KEY_LENGTH] = {0};
|
||||
for (int i = 0; i < ENA_KEY_LENGTH; i++)
|
||||
{
|
||||
rpi[i] = p->scan_rst.ble_adv[8 + i];
|
||||
}
|
||||
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
|
||||
for (int i = 0; i < ENA_AEM_METADATA_LENGTH; i++)
|
||||
{
|
||||
aem[i] = p->scan_rst.ble_adv[8 + ENA_KEY_LENGTH + i];
|
||||
}
|
||||
|
||||
ena_detection((uint32_t)time(NULL), rpi, aem, p->scan_rst.rssi);
|
||||
}
|
||||
}
|
||||
else if (p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT)
|
||||
{
|
||||
scan_status = ENA_SCAN_STATUS_NOT_SCANNING;
|
||||
ena_detections_temp_refresh((uint32_t)time(NULL));
|
||||
ESP_LOGI(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 detections
|
||||
ena_detections_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,39 @@
|
||||
/**
|
||||
* provide bluetooth scanning part of Exposure Notification API v1.2 as defined by Apple/Google
|
||||
*
|
||||
* Source documents:
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _ena_BLUETOOTH_SCAN_H_
|
||||
#define _ena_BLUETOOTH_SCAN_H_
|
||||
|
||||
#define ENA_SCAN_LOG "ESP-ENA-scan" // TAG for Logging
|
||||
|
||||
#define ENA_SCANNING_TIME 30 // scan for 30 seconds
|
||||
#define ENA_SCANNING_INTERVAL 300 // scan every 5 minutes
|
||||
|
||||
#include "esp_gap_ble_api.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
ENA_SCAN_STATUS_SCANNING = 0,
|
||||
ENA_SCAN_STATUS_NOT_SCANNING,
|
||||
ENA_SCAN_STATUS_WAITING,
|
||||
} ena_bluetooth_scan_status;
|
||||
|
||||
void ena_bluetooth_scan_init(void);
|
||||
|
||||
void ena_bluetooth_scan_start(uint32_t duration);
|
||||
|
||||
void ena_bluetooth_scan_stop(void);
|
||||
|
||||
int ena_bluetooth_scan_get_status(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
|
||||
#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[ENA_KEY_LENGTH] = "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,60 @@
|
||||
|
||||
/**
|
||||
* provide cryptographic part of Exposure Notification API v1.2 as defined by Apple/Google
|
||||
*
|
||||
* Source documents:
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#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 144 // TEKRollingPeriod
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/**
|
||||
* initialize crypto (setup entropy for key generation)
|
||||
*/
|
||||
void ena_crypto_init(void);
|
||||
|
||||
/**
|
||||
* Calculate ENIntervalNumber (ENIN) for given UNIX timestamp
|
||||
*/
|
||||
uint32_t ena_crypto_enin(uint32_t seconds);
|
||||
|
||||
/**
|
||||
* calculate a new random Temporary Exposure Key (TEK)
|
||||
*/
|
||||
void ena_crypto_tek(uint8_t tek[]);
|
||||
|
||||
/**
|
||||
* calculate a new Rolling Proximity Identifier Key (RPIK) with given TEK
|
||||
*/
|
||||
void ena_crypto_rpik(uint8_t rpik[], uint8_t tek[]);
|
||||
|
||||
/**
|
||||
* calculate a new Rolling Proximity Identifier with given RPIK and ENIN
|
||||
*/
|
||||
void ena_crypto_rpi(uint8_t rpi[], uint8_t rpik[], uint32_t enin);
|
||||
|
||||
/**
|
||||
* calculate a new Associated Encrypted Metadata Key (AEMK) with given TEK
|
||||
*/
|
||||
void ena_crypto_aemk(uint8_t aemk[], uint8_t tek[]);
|
||||
|
||||
/**
|
||||
* create Associated Encrypted Metadata (AEM) with given AEMK along the RPI
|
||||
*/
|
||||
void ena_crypto_aem(uint8_t aem[], uint8_t aemk[], uint8_t rpi[], uint8_t power_level);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,94 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "ena-crypto.h"
|
||||
#include "ena-storage.h"
|
||||
|
||||
#include "ena-detection.h"
|
||||
|
||||
static uint32_t temp_detections_count = 0;
|
||||
static uint8_t temp_detection_rpi[ENA_STOARGE_TEMP_DETECTIONS_MAX][ENA_KEY_LENGTH] = {{0}};
|
||||
static uint8_t temp_detection_aem[ENA_STOARGE_TEMP_DETECTIONS_MAX][ENA_AEM_METADATA_LENGTH] = {{0}};
|
||||
static uint32_t temp_detection_timestamp_first[ENA_STOARGE_TEMP_DETECTIONS_MAX] = {0};
|
||||
static uint32_t temp_detection_timestamp_last[ENA_STOARGE_TEMP_DETECTIONS_MAX] = {0};
|
||||
static int temp_detection_rssi_last[ENA_STOARGE_TEMP_DETECTIONS_MAX] = {0};
|
||||
|
||||
int ena_get_temp_detection_index(uint8_t rpi[], uint8_t aem[])
|
||||
{
|
||||
for (int i = 0; i < temp_detections_count; i++)
|
||||
{
|
||||
if (memcmp(temp_detection_rpi[i], rpi, sizeof(ENA_KEY_LENGTH)) == 0 &&
|
||||
memcmp(temp_detection_aem[i], aem, sizeof(ENA_AEM_METADATA_LENGTH)) == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void ena_detections_temp_refresh(uint32_t unix_timestamp)
|
||||
{
|
||||
for (int i = 0; i < temp_detections_count; i++)
|
||||
{
|
||||
// check for treshold and add permanent detection
|
||||
if (temp_detection_timestamp_last[i] - temp_detection_timestamp_first[i] >= ENA_DETECTION_TRESHOLD)
|
||||
{
|
||||
ESP_LOGD(ENA_DETECTION_LOG, "create detection after treshold");
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, temp_detection_rpi[i], ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ena_storage_write_detection(ena_crypto_enin(temp_detection_timestamp_first[i]), temp_detection_rpi[i],
|
||||
temp_detection_aem[i], temp_detection_rssi_last[i]);
|
||||
ena_storage_remove_temp_detection(i);
|
||||
}
|
||||
else
|
||||
// delete temp detections older than two times time window (two times to be safe, one times time window enough?!)
|
||||
if (unix_timestamp - temp_detection_timestamp_first[i] > (ENA_TIME_WINDOW * 2))
|
||||
{
|
||||
ESP_LOGD(ENA_DETECTION_LOG, "remove old temporary detection %u", i);
|
||||
ena_storage_remove_temp_detection(i);
|
||||
}
|
||||
}
|
||||
|
||||
// update detections
|
||||
temp_detections_count = ena_storage_temp_detections_count();
|
||||
for (int i = 0; i < temp_detections_count; i++)
|
||||
{
|
||||
ena_storage_read_temp_detection(i, &temp_detection_timestamp_first[i], temp_detection_rpi[i], temp_detection_aem[i], &temp_detection_rssi_last[i]);
|
||||
}
|
||||
|
||||
// DEBUG dump
|
||||
ena_storage_dump_tek();
|
||||
ena_storage_dump_temp_detections();
|
||||
ena_storage_dump_detections();
|
||||
}
|
||||
|
||||
void ena_detection(uint32_t unix_timestamp, uint8_t rpi[], uint8_t aem[], int rssi)
|
||||
{
|
||||
uint32_t detection_index = ena_get_temp_detection_index(rpi, aem);
|
||||
|
||||
if (detection_index == -1)
|
||||
{
|
||||
temp_detection_timestamp_first[temp_detections_count] = unix_timestamp;
|
||||
memcpy(temp_detection_rpi[temp_detections_count], rpi, ENA_KEY_LENGTH);
|
||||
memcpy(temp_detection_aem[temp_detections_count], aem, ENA_AEM_METADATA_LENGTH);
|
||||
temp_detection_rssi_last[temp_detections_count] = rssi;
|
||||
temp_detection_timestamp_last[temp_detections_count] = unix_timestamp;
|
||||
detection_index = ena_storage_write_temp_detection(unix_timestamp, rpi, aem, rssi);
|
||||
|
||||
ESP_LOGD(ENA_DETECTION_LOG, "New temporary detection at %d with timestamp %u", temp_detections_count, unix_timestamp);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_DETECTION_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEX_LEVEL(ENA_DETECTION_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_DETECTION_LOG, "RSSI %d", rssi);
|
||||
|
||||
if (detection_index != temp_detections_count)
|
||||
{
|
||||
ESP_LOGW(ENA_DETECTION_LOG, "last temporary detection index does not match array index!");
|
||||
}
|
||||
temp_detections_count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_detection_rssi_last[detection_index] = rssi;
|
||||
temp_detection_timestamp_last[detection_index] = unix_timestamp;
|
||||
ESP_LOGD(ENA_DETECTION_LOG, "New Timestamp for temporary detection %d: %u", detection_index, unix_timestamp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* combine bluetooth and crypto parts to build EXPOSURE NOTIFICATION
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _ena_DETECTION_H_
|
||||
#define _ena_DETECTION_H_
|
||||
|
||||
#define ENA_DETECTION_LOG "ESP-ENA-detection" // TAG for Logging
|
||||
|
||||
#define ENA_DETECTION_TRESHOLD 300 // meet for longer than 5 minutes
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
void ena_detections_temp_refresh(uint32_t unix_timestamp);
|
||||
|
||||
void ena_detection(uint32_t unix_timestamp, uint8_t rpi[], uint8_t aem[], int rssi);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,449 @@
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_partition.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena-storage.h"
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define BLOCK_SIZE 4096
|
||||
|
||||
void ena_storage_read(size_t address, uint8_t *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, 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, uint8_t data[], size_t size)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write");
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, PARTITION_NAME);
|
||||
assert(partition);
|
||||
|
||||
const int block_num = address / BLOCK_SIZE;
|
||||
|
||||
// split if size extends block
|
||||
if (address + size > (block_num + 1) * BLOCK_SIZE)
|
||||
{
|
||||
|
||||
ESP_LOGI(ENA_STORAGE_LOG, "overflow block at address %u with size %d (block %d)", address, size, block_num);
|
||||
const size_t block1_address = address;
|
||||
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)", block1_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);
|
||||
uint8_t *data1 = malloc(data1_size);
|
||||
for (int i = 0; i < data1_size; i++)
|
||||
{
|
||||
data1[i] = data[i];
|
||||
}
|
||||
uint8_t *data2 = malloc(data2_size);
|
||||
for (int i = 0; i < data2_size; i++)
|
||||
{
|
||||
data2[i] = data[data1_size + i];
|
||||
}
|
||||
|
||||
ena_storage_write(block1_address, data1, data1_size);
|
||||
free(data1);
|
||||
ena_storage_write(block2_address, data2, data2_size);
|
||||
free(data2);
|
||||
}
|
||||
else
|
||||
{
|
||||
const int block_start = block_num * BLOCK_SIZE;
|
||||
const int block_address = address - block_start;
|
||||
uint8_t *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));
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
buffer[block_address + i] = data[i];
|
||||
}
|
||||
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);
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write");
|
||||
}
|
||||
|
||||
void ena_storage_shift_delete(size_t address, size_t size)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_shift_delete");
|
||||
const esp_partition_t *partition = esp_partition_find_first(
|
||||
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, PARTITION_NAME);
|
||||
assert(partition);
|
||||
|
||||
const int block_num = address / BLOCK_SIZE;
|
||||
|
||||
// split if size extends block TODO this does not work like this!!! Shift puts data back to first block!
|
||||
if (address + size > (block_num + 1) * BLOCK_SIZE)
|
||||
{
|
||||
const int block1_address = address;
|
||||
const int block2_address = (block_num + 1) * BLOCK_SIZE;
|
||||
const int data2_size = address + size - block2_address;
|
||||
const int data1_size = size - data2_size;
|
||||
ena_storage_shift_delete(block1_address, data1_size);
|
||||
ena_storage_shift_delete(block2_address, data2_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
const int block_start = block_num * BLOCK_SIZE;
|
||||
const int block_address = address - block_start;
|
||||
uint8_t *buffer = malloc(BLOCK_SIZE);
|
||||
ESP_ERROR_CHECK(esp_partition_read(partition, block_start, buffer, BLOCK_SIZE));
|
||||
vTaskDelay(1);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "shift block from %u to %u with size %u (move %u)", block_address, block_address + size, size, BLOCK_SIZE - address + size);
|
||||
// shift manually
|
||||
for (int i = block_address + size; i < BLOCK_SIZE; i++)
|
||||
{
|
||||
buffer[i - size] = buffer[i];
|
||||
}
|
||||
|
||||
for (int i = BLOCK_SIZE - size; i < BLOCK_SIZE; i++)
|
||||
{
|
||||
buffer[i] = 0;
|
||||
}
|
||||
// memmove seems to lead to corrupt heap memory!
|
||||
// memmove(&buffer[block_address], &buffer[block_address + size], BLOCK_SIZE - address + size);
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, block_start, BLOCK_SIZE));
|
||||
ESP_ERROR_CHECK(esp_partition_write(partition, block_start, buffer, BLOCK_SIZE));
|
||||
free(buffer);
|
||||
}
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_shift_delete");
|
||||
}
|
||||
|
||||
void ena_storage_write_tek(uint32_t enin, uint8_t tek[])
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_tek");
|
||||
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
|
||||
size_t address = ENA_STORAGE_TEK_START_ADDRESS + (tek_count * ENA_STORAGE_TEK_LENGTH);
|
||||
ena_storage_write_u32(address, enin);
|
||||
ena_storage_write(address + 4, tek, ENA_KEY_LENGTH);
|
||||
|
||||
tek_count++;
|
||||
if (tek_count > ENA_STOARGE_TEK_STORE_PERIOD)
|
||||
{
|
||||
tek_count = 0;
|
||||
}
|
||||
ena_storage_write_u8(ENA_STORAGE_TEK_COUNT_ADDRESS, tek_count);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write tek: ENIN %u", enin);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_tek");
|
||||
}
|
||||
|
||||
uint32_t ena_storage_read_enin(void)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_enin");
|
||||
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
|
||||
if (tek_count < 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t address = ENA_STORAGE_TEK_START_ADDRESS + (tek_count - 1) * ENA_STORAGE_TEK_LENGTH;
|
||||
uint32_t result = ena_storage_read_u32(address);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read last ENIN: %u", result);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_enin");
|
||||
return result;
|
||||
}
|
||||
|
||||
void ena_storage_read_tek(uint8_t tek[])
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_tek");
|
||||
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
|
||||
if (tek_count < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
size_t address = ENA_STORAGE_TEK_START_ADDRESS + (tek_count - 1) * ENA_STORAGE_TEK_LENGTH + 4;
|
||||
ena_storage_read(address, tek, ENA_KEY_LENGTH);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read last tek:");
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, tek, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_tek");
|
||||
}
|
||||
|
||||
uint32_t ena_storage_temp_detections_count(void)
|
||||
{
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_temp_detections_count");
|
||||
uint32_t count = ena_storage_read_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read temp contancts count: %u", count);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_temp_detections_count");
|
||||
return count;
|
||||
}
|
||||
|
||||
uint32_t ena_storage_write_temp_detection(uint32_t timestamp, uint8_t rpi[], uint8_t aem[], int rssi)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_temp_detection");
|
||||
uint32_t count = ena_storage_temp_detections_count() + 1;
|
||||
|
||||
// start overwriting temporay detections?!
|
||||
if (count > ENA_STOARGE_TEMP_DETECTIONS_MAX)
|
||||
{
|
||||
count = 1;
|
||||
}
|
||||
|
||||
size_t address = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + (count - 1) * ENA_STORAGE_DETECTION_LENGTH;
|
||||
ena_storage_write_u32(address, timestamp);
|
||||
address += 4;
|
||||
ena_storage_write(address, rpi, ENA_KEY_LENGTH);
|
||||
address += ENA_KEY_LENGTH;
|
||||
ena_storage_write(address, aem, ENA_AEM_METADATA_LENGTH);
|
||||
address += ENA_AEM_METADATA_LENGTH;
|
||||
ena_storage_write_int(address, rssi);
|
||||
ena_storage_write_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, count);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write temp detection: timestamp %u and rssi %d", timestamp, rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_temp_detection");
|
||||
return count - 1;
|
||||
}
|
||||
|
||||
void ena_storage_read_temp_detection(uint32_t index, uint32_t *timestamp, uint8_t rpi[], uint8_t aem[], int *rssi)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_temp_detection");
|
||||
size_t address = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * ENA_STORAGE_DETECTION_LENGTH;
|
||||
*timestamp = ena_storage_read_u32(address);
|
||||
address += 4;
|
||||
ena_storage_read(address, rpi, ENA_KEY_LENGTH);
|
||||
address += ENA_KEY_LENGTH;
|
||||
ena_storage_read(address, aem, ENA_AEM_METADATA_LENGTH);
|
||||
address += 4;
|
||||
*rssi = ena_storage_read_int(address);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read temp detection: timestamp %u and rssi %d", *timestamp, *rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_temp_detection");
|
||||
}
|
||||
|
||||
void ena_storage_remove_temp_detection(uint32_t index)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_remove_temp_detection");
|
||||
size_t address = ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS + index * ENA_STORAGE_DETECTION_LENGTH;
|
||||
|
||||
ena_storage_shift_delete(address, ENA_STORAGE_DETECTION_LENGTH);
|
||||
|
||||
uint32_t count = ena_storage_temp_detections_count();
|
||||
count--;
|
||||
ena_storage_write_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, count);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "remove temp detection: %u", index);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_remove_temp_detection");
|
||||
}
|
||||
|
||||
uint32_t ena_storage_detections_count(void)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_detections_count");
|
||||
uint32_t count = ena_storage_read_u32(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read contancts count: %u", count);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_detections_count");
|
||||
return count;
|
||||
}
|
||||
|
||||
void ena_storage_write_detection(uint32_t timestamp, uint8_t rpi[], uint8_t aem[], int rssi)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_write_detection");
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
uint32_t count = ena_storage_detections_count() + 1;
|
||||
size_t address = ENA_STORAGE_DETECTIONS_START_ADDRESS + (count - 1) * ENA_STORAGE_DETECTION_LENGTH;
|
||||
ena_storage_write_u32(address, timestamp);
|
||||
address += 4;
|
||||
ena_storage_write(address, rpi, ENA_KEY_LENGTH);
|
||||
address += ENA_KEY_LENGTH;
|
||||
ena_storage_write(address, aem, ENA_AEM_METADATA_LENGTH);
|
||||
address += ENA_AEM_METADATA_LENGTH;
|
||||
ena_storage_write_int(address, rssi);
|
||||
ena_storage_write_u32(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, count);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "write detection: timestamp %u and rssi %d", timestamp, rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_write_detection");
|
||||
}
|
||||
|
||||
void ena_storage_read_detection(uint32_t index, uint32_t *timestamp, uint8_t rpi[], uint8_t aem[], int *rssi)
|
||||
{
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "START ena_storage_read_detection");
|
||||
size_t address = ENA_STORAGE_DETECTIONS_START_ADDRESS + index * ENA_STORAGE_DETECTION_LENGTH;
|
||||
*timestamp = ena_storage_read_u32(address);
|
||||
address += 4;
|
||||
ena_storage_read(address, rpi, ENA_KEY_LENGTH);
|
||||
address += ENA_KEY_LENGTH;
|
||||
ena_storage_read(address, aem, ENA_AEM_METADATA_LENGTH);
|
||||
address += 4;
|
||||
*rssi = ena_storage_read_int(address);
|
||||
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "read detection: timestamp %u and rssi %d", *timestamp, *rssi);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, rpi, ENA_KEY_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOG_BUFFER_HEXDUMP(ENA_STORAGE_LOG, aem, ENA_AEM_METADATA_LENGTH, ESP_LOG_DEBUG);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_read_detection");
|
||||
}
|
||||
|
||||
uint8_t ena_storage_read_u8(size_t address)
|
||||
{
|
||||
uint8_t data[1] = {0};
|
||||
ena_storage_read(address, (uint8_t *)&data, 1);
|
||||
return data[0];
|
||||
}
|
||||
|
||||
uint32_t ena_storage_read_u32(size_t address)
|
||||
{
|
||||
uint8_t data[4] = {0};
|
||||
ena_storage_read(address, (uint8_t *)&data, 4);
|
||||
|
||||
uint32_t result = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
||||
return result;
|
||||
}
|
||||
|
||||
void ena_storage_write_u8(size_t address, uint8_t byte)
|
||||
{
|
||||
uint8_t data[1] = {byte};
|
||||
ena_storage_write(address, data, 1);
|
||||
}
|
||||
|
||||
void ena_storage_write_u32(size_t address, uint32_t value)
|
||||
{
|
||||
uint8_t *data = malloc(4);
|
||||
data[0] = (value & 0x000000ff);
|
||||
data[1] = (value & 0x0000ff00) >> 8;
|
||||
data[2] = (value & 0x00ff0000) >> 16;
|
||||
data[3] = (value & 0xff000000) >> 24;
|
||||
ena_storage_write(address, data, 4);
|
||||
free(data);
|
||||
}
|
||||
|
||||
int ena_storage_read_int(size_t address)
|
||||
{
|
||||
uint8_t data[sizeof(int)] = {0};
|
||||
ena_storage_read(address, (uint8_t *)&data, sizeof(int));
|
||||
int result = 0;
|
||||
memcpy((int *)&result, (int *)&data, sizeof(int));
|
||||
return result;
|
||||
}
|
||||
|
||||
void ena_storage_write_int(size_t address, int value)
|
||||
{
|
||||
|
||||
uint8_t data[sizeof(int)] = {0};
|
||||
memcpy((int *)&data, (int *)&value, sizeof(int));
|
||||
ena_storage_write(address, data, sizeof(int));
|
||||
}
|
||||
|
||||
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, PARTITION_NAME);
|
||||
assert(partition);
|
||||
ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
|
||||
ESP_LOGI(PARTITION_NAME, "erase partition!");
|
||||
|
||||
uint8_t *tek_zeros = calloc(ENA_STORAGE_TEK_LENGTH + 1, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_TEK_COUNT_ADDRESS, tek_zeros, ENA_STORAGE_TEK_LENGTH + 1);
|
||||
|
||||
uint8_t *temp_detection_zeros = calloc(ENA_STORAGE_DETECTION_LENGTH + 4, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS, temp_detection_zeros, ENA_STORAGE_DETECTION_LENGTH + 4);
|
||||
|
||||
uint8_t *detection_zeros = calloc(ENA_STORAGE_DETECTION_LENGTH + 4, sizeof(uint8_t));
|
||||
ena_storage_write(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS, detection_zeros, ENA_STORAGE_DETECTION_LENGTH + 4);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "END ena_storage_erase");
|
||||
}
|
||||
|
||||
void ena_storage_dump_hash_array(uint8_t data[], size_t size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
printf("%0x", data[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(" %0x", data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_tek(void)
|
||||
{
|
||||
uint32_t timestamp;
|
||||
uint8_t tek[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t tek_count = ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u TEKs\n", tek_count);
|
||||
printf("#,enin,tek\n");
|
||||
for (int i = 0; i < tek_count; i++)
|
||||
{
|
||||
|
||||
size_t address = ENA_STORAGE_TEK_START_ADDRESS + i * ENA_STORAGE_TEK_LENGTH;
|
||||
timestamp = ena_storage_read_u32(address);
|
||||
ena_storage_read(address + 4, tek, ENA_KEY_LENGTH);
|
||||
printf("%d,%u,", i, timestamp);
|
||||
ena_storage_dump_hash_array(tek, ENA_KEY_LENGTH);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_temp_detections(void)
|
||||
{
|
||||
uint32_t timestamp;
|
||||
uint8_t rpi[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
|
||||
int rssi;
|
||||
uint32_t detection_count = ena_storage_read_u32(ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u temporary detections\n", detection_count);
|
||||
printf("#,timestamp,rpi,aem,rssi\n");
|
||||
for (int i = 0; i < detection_count; i++)
|
||||
{
|
||||
ena_storage_read_temp_detection(i, ×tamp, rpi, aem, &rssi);
|
||||
printf("%d,%u,", i, timestamp);
|
||||
ena_storage_dump_hash_array(rpi, ENA_KEY_LENGTH);
|
||||
printf(",");
|
||||
ena_storage_dump_hash_array(aem, ENA_AEM_METADATA_LENGTH);
|
||||
printf(",%d\n", rssi);
|
||||
}
|
||||
}
|
||||
|
||||
void ena_storage_dump_detections(void)
|
||||
{
|
||||
uint32_t enin;
|
||||
uint8_t rpi[ENA_KEY_LENGTH] = {0};
|
||||
uint8_t aem[ENA_AEM_METADATA_LENGTH] = {0};
|
||||
int rssi;
|
||||
uint32_t detection_count = ena_storage_read_u32(ENA_STORAGE_DETECTIONS_COUNT_ADDRESS);
|
||||
ESP_LOGD(ENA_STORAGE_LOG, "%u detections\n", detection_count);
|
||||
printf("#,enin,rpi,aem,rssi\n");
|
||||
for (int i = 0; i < detection_count; i++)
|
||||
{
|
||||
ena_storage_read_detection(i, &enin, rpi, aem, &rssi);
|
||||
printf("%d,%u,", i, enin);
|
||||
ena_storage_dump_hash_array(rpi, ENA_KEY_LENGTH);
|
||||
printf(",");
|
||||
ena_storage_dump_hash_array(aem, ENA_AEM_METADATA_LENGTH);
|
||||
printf(",%d\n", rssi);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
#ifndef _ena_STORAGE_H_
|
||||
#define _ena_STORAGE_H_
|
||||
|
||||
#include "ena-crypto.h"
|
||||
|
||||
#define ENA_STORAGE_LOG "ESP-ENA-storage" // TAG for Logging
|
||||
#define PARTITION_NAME "ena"
|
||||
#define ENA_STOARGE_TEK_STORE_PERIOD (14) // Period of storing TEKs
|
||||
#define ENA_STORAGE_TEK_COUNT_ADDRESS (0) // starting address for TEK COUNT
|
||||
#define ENA_STORAGE_TEK_START_ADDRESS (ENA_STORAGE_TEK_COUNT_ADDRESS + 1) // starting address for TEKs
|
||||
#define ENA_STORAGE_TEK_LENGTH (ENA_KEY_LENGTH + 4) // length of a stored TEK -> TEK keysize + 4 Bytes for ENIN
|
||||
#define ENA_STORAGE_DETECTION_LENGTH (ENA_KEY_LENGTH + ENA_AEM_METADATA_LENGTH + 4 + sizeof(int)) // length of a stored detection -> RPI keysize + AEM size + 4 Bytes for ENIN + 4 Bytes for RSSI
|
||||
#define ENA_STOARGE_TEMP_DETECTIONS_MAX (1000) // Maximum number of temporary stored detections
|
||||
#define ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS (ENA_STORAGE_TEK_START_ADDRESS + ENA_STORAGE_TEK_LENGTH * ENA_STOARGE_TEK_STORE_PERIOD) // starting address for temporary detections COUNT (offset from max. stored TEKs)
|
||||
#define ENA_STORAGE_TEMP_DETECTIONS_START_ADDRESS (ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS + 4) // starting address for temporary detections
|
||||
#define ENA_STORAGE_DETECTIONS_COUNT_ADDRESS (ENA_STORAGE_TEMP_DETECTIONS_COUNT_ADDRESS + ENA_STORAGE_DETECTION_LENGTH * ENA_STOARGE_TEMP_DETECTIONS_MAX) // starting address for detections COUNT (offset from max. stored temporary detections)
|
||||
#define ENA_STORAGE_DETECTIONS_START_ADDRESS (ENA_STORAGE_DETECTIONS_COUNT_ADDRESS + 4) // starting address of detections
|
||||
|
||||
|
||||
/**
|
||||
* read bytes at given address
|
||||
*/
|
||||
void ena_storage_read(size_t address, uint8_t *data, size_t size);
|
||||
|
||||
/**
|
||||
* store bytes at given address
|
||||
*/
|
||||
void ena_storage_write(size_t address, uint8_t data[], size_t size);
|
||||
|
||||
/**
|
||||
* deletes bytes at given address and shift other data back
|
||||
*/
|
||||
void ena_storage_shift_delete(size_t address, size_t size);
|
||||
|
||||
/**
|
||||
* store TEK with ENIN
|
||||
*/
|
||||
void ena_storage_write_tek(uint32_t enin, uint8_t tek[]);
|
||||
|
||||
/**
|
||||
* get last stored ENIN
|
||||
*/
|
||||
uint32_t ena_storage_read_enin(void);
|
||||
|
||||
/**
|
||||
* get last stored TEK
|
||||
*/
|
||||
void ena_storage_read_tek(uint8_t tek[]);
|
||||
|
||||
/**
|
||||
* get number of stored temporary detections
|
||||
*/
|
||||
uint32_t ena_storage_temp_detections_count(void);
|
||||
|
||||
/**
|
||||
* store temporary detection (RPI + AEM + RSSI with UNIX timestamp)
|
||||
*
|
||||
* returns index
|
||||
*/
|
||||
uint32_t ena_storage_write_temp_detection(uint32_t timestamp, uint8_t rpi[], uint8_t aem[], int rssi);
|
||||
|
||||
/**
|
||||
* get temporary detection (RPI + AEM + RSSI with UNIX timestamp) at given index
|
||||
*/
|
||||
void ena_storage_read_temp_detection(uint32_t index, uint32_t *timestamp, uint8_t rpi[], uint8_t aem[], int *rssi);
|
||||
|
||||
/**
|
||||
* remove temporary detection at given index
|
||||
*/
|
||||
void ena_storage_remove_temp_detection(uint32_t index);
|
||||
|
||||
/**
|
||||
* get number of stored detections
|
||||
*/
|
||||
uint32_t ena_storage_detections_count(void);
|
||||
|
||||
/**
|
||||
* store detection (RPI + AEM + RSSI with ENIN)
|
||||
*/
|
||||
void ena_storage_write_detection(uint32_t timestamp, uint8_t rpi[], uint8_t aem[], int rssi);
|
||||
|
||||
/**
|
||||
* get detection (RPI + AEM + RSSI with ENIN) at given index
|
||||
*/
|
||||
void ena_storage_read_detection(uint32_t index, uint32_t *enin, uint8_t rpi[], uint8_t aem[], int *rssi);
|
||||
|
||||
uint8_t ena_storage_read_u8(size_t address);
|
||||
|
||||
uint32_t ena_storage_read_u32(size_t address);
|
||||
|
||||
void ena_storage_write_u8(size_t address, uint8_t byte);
|
||||
|
||||
void ena_storage_write_u32(size_t address, uint32_t value);
|
||||
|
||||
int ena_storage_read_int(size_t address);
|
||||
|
||||
void ena_storage_write_int(size_t address, int value);
|
||||
|
||||
void ena_storage_erase(void);
|
||||
|
||||
void ena_storage_dump_tek(void);
|
||||
|
||||
void ena_storage_dump_temp_detections(void);
|
||||
|
||||
void ena_storage_dump_detections(void);
|
||||
|
||||
#endif
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.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 uint32_t last_enin; // last ENIN
|
||||
static uint8_t tek[ENA_KEY_LENGTH] = {0}; // current TEK
|
||||
|
||||
void ena_init(void)
|
||||
{
|
||||
// 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
|
||||
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
|
||||
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
|
||||
ESP_ERROR_CHECK(esp_bluedroid_init());
|
||||
ESP_ERROR_CHECK(esp_bluedroid_enable());
|
||||
|
||||
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));
|
||||
|
||||
/* nesseccary?
|
||||
// new bluetooth address
|
||||
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));
|
||||
*/
|
||||
// init ENA
|
||||
ena_crypto_init();
|
||||
|
||||
uint32_t current_enin = ena_crypto_enin((uint32_t)time(NULL));
|
||||
|
||||
last_enin = ena_storage_read_enin();
|
||||
|
||||
// read last TEK or create new
|
||||
if (ena_storage_read_u8(ENA_STORAGE_TEK_COUNT_ADDRESS) > 0 && (current_enin - last_enin) < ENA_TEK_ROLLING_PERIOD)
|
||||
{
|
||||
ena_storage_read_tek(tek);
|
||||
}
|
||||
else
|
||||
{
|
||||
ena_crypto_tek(tek);
|
||||
ena_storage_write_tek(ena_crypto_enin((uint32_t)time(NULL)), tek);
|
||||
last_enin = ena_storage_read_enin();
|
||||
}
|
||||
|
||||
// init scan
|
||||
ena_bluetooth_scan_init();
|
||||
|
||||
// init and start advertising
|
||||
ena_bluetooth_advertise_set_payload(current_enin, tek);
|
||||
ena_bluetooth_advertise_start();
|
||||
// initial scan on every start
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
}
|
||||
|
||||
void ena_run(void)
|
||||
{
|
||||
uint32_t unix_timestamp = (uint32_t)time(NULL);
|
||||
uint32_t current_enin = ena_crypto_enin(unix_timestamp);
|
||||
if (current_enin - last_enin >= ENA_TEK_ROLLING_PERIOD)
|
||||
{
|
||||
ena_crypto_tek(tek);
|
||||
ena_storage_write_tek(current_enin, tek);
|
||||
last_enin = current_enin;
|
||||
}
|
||||
|
||||
// change RPI
|
||||
if (unix_timestamp % ENA_TIME_WINDOW == 0)
|
||||
{
|
||||
|
||||
//
|
||||
|
||||
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, tek);
|
||||
ena_bluetooth_advertise_start();
|
||||
if (ena_bluetooth_scan_get_status() == ENA_SCAN_STATUS_WAITING)
|
||||
{
|
||||
ena_bluetooth_scan_start(ENA_SCANNING_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* combine bluetooth and crypto parts to build EXPOSURE NOTIFICATION
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _ena_H_
|
||||
#define _ena_H_
|
||||
|
||||
|
||||
#define ENA_LOG "ESP-ENA" // TAG for Logging
|
||||
|
||||
void ena_init(void);
|
||||
|
||||
void ena_run(void);
|
||||
|
||||
#endif
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* provide bluetooth part of Exposure Notification API v1.2 as defined by Apple/Google
|
||||
*
|
||||
* Source documents:
|
||||
*
|
||||
* 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
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "ena.h"
|
||||
#include "ena-storage.h"
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// DEBUG set time
|
||||
struct timeval tv = {1594459800, 0}; // current hardcoded timestamp (2020-07-11 09:30:00) ¯\_(ツ)_/¯
|
||||
settimeofday(&tv, NULL);
|
||||
|
||||
esp_log_level_set(ENA_STORAGE_LOG, ESP_LOG_INFO);
|
||||
// ena_storage_erase();
|
||||
|
||||
ena_init();
|
||||
|
||||
// one second loop enough?
|
||||
while (1)
|
||||
{
|
||||
ena_run();
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user