2020-06-08 11:15:05 +02:00
|
|
|
/* main.c - Application main entry point */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Olaf Landsiedel
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-27 21:52:29 +02:00
|
|
|
#include <zephyr.h>
|
|
|
|
|
2020-06-08 11:15:05 +02:00
|
|
|
#include <bluetooth/hci.h>
|
2020-08-15 17:26:49 +02:00
|
|
|
#include <random/rand32.h>
|
2021-01-26 16:14:58 +01:00
|
|
|
#include <sys/printk.h>
|
2021-04-12 19:52:06 +02:00
|
|
|
|
2022-05-27 21:52:29 +02:00
|
|
|
#include "record_storage.h"
|
|
|
|
#include "tek_storage.h"
|
|
|
|
#include "sync_service.h"
|
|
|
|
#include "tracing.h"
|
2022-05-31 04:20:40 +02:00
|
|
|
#include "bloom.h"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill the bloom filter with all stored records.
|
|
|
|
*/
|
|
|
|
void fill_bloom_with_stored_records(bloom_filter_t* bloom) {
|
|
|
|
// init iterator for filling bloom filter
|
|
|
|
record_iterator_t iterator;
|
|
|
|
int rc = ens_records_iterator_init_timerange(&iterator, NULL, NULL);
|
|
|
|
if (rc) {
|
|
|
|
printk("init iterator failed0 (err %d)\n", rc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill bloom filter with records
|
|
|
|
record_t* current;
|
|
|
|
while ((current = ens_records_iterator_next(&iterator))) {
|
|
|
|
bloom_add_record(bloom, &(current->rolling_proximity_identifier));
|
|
|
|
}
|
|
|
|
}
|
2021-06-14 15:44:07 +02:00
|
|
|
|
2021-01-26 16:14:58 +01:00
|
|
|
void main(void) {
|
2021-12-08 17:44:42 +01:00
|
|
|
|
2021-01-26 16:14:58 +01:00
|
|
|
int err = 0;
|
2022-05-27 21:52:29 +02:00
|
|
|
printk("Starting Contact-Tracing Wristband...\n");
|
2020-06-08 11:15:05 +02:00
|
|
|
|
2021-01-26 16:14:58 +01:00
|
|
|
// Use custom randomization as the mbdet_tls context initialization messes with the Zeyhr BLE stack.
|
2020-06-08 11:15:05 +02:00
|
|
|
err = en_init(sys_csrand_get);
|
2021-01-26 16:14:58 +01:00
|
|
|
if (err) {
|
2022-05-27 21:52:29 +02:00
|
|
|
printk("Crypto init failed (err %d)\n", err);
|
2021-01-26 16:14:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
err = record_storage_init(true);
|
2021-04-12 19:20:15 +02:00
|
|
|
if (err) {
|
2022-05-27 21:52:29 +02:00
|
|
|
printk("init record storage failed (err %d)\n", err);
|
2021-04-12 19:20:15 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-12-08 18:19:27 +01:00
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
|
2022-05-27 21:52:29 +02:00
|
|
|
err = tek_storage_init(false);
|
2021-12-08 18:19:27 +01:00
|
|
|
if (err) {
|
2022-05-27 21:52:29 +02:00
|
|
|
printk("init key storage failed (err %d)\n", err);
|
2021-12-08 18:19:27 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-07-21 14:05:08 +02:00
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
k_msleep(10000);
|
2020-06-08 11:15:05 +02:00
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
printk("Filling record storage...\n");
|
|
|
|
reset_record_storage();
|
2020-06-08 11:15:05 +02:00
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
tek_t tek;
|
|
|
|
ENPeriodIdentifierKey pik;
|
2020-06-08 11:15:05 +02:00
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
tek_storage_get_latest_at_ts(&tek, 0);
|
|
|
|
en_derive_period_identifier_key(&pik, &tek.tek);
|
2021-06-22 15:48:03 +02:00
|
|
|
|
2022-05-30 00:03:14 +02:00
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
bloom_filter_t* bf = bloom_init(0); // size not used atm
|
|
|
|
|
|
|
|
/// we now fill the whole flash memory with stupid data
|
|
|
|
for (int i = 0; i < CONFIG_ENS_MAX_CONTACTS; ++i) {
|
|
|
|
record_t dummy_record;
|
|
|
|
en_derive_interval_identifier((ENIntervalIdentifier*)&dummy_record.rolling_proximity_identifier, &pik, i);
|
|
|
|
dummy_record.timestamp = i; // very clever! ;)
|
|
|
|
bloom_add_record(bf, &dummy_record.rolling_proximity_identifier);
|
|
|
|
if (!bloom_probably_has_record(bf, &dummy_record.rolling_proximity_identifier)) {
|
|
|
|
printk("error! bloom_probably_has_record\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printk("Done...\n");
|
|
|
|
k_msleep(2000);
|
2022-05-28 23:22:18 +02:00
|
|
|
|
2022-05-31 04:20:40 +02:00
|
|
|
printk("Creating bloom filter...\n");
|
|
|
|
fill_bloom_with_stored_records(bf);
|
|
|
|
printk("Done...\n");
|
|
|
|
k_msleep(3000);
|
|
|
|
|
|
|
|
do {
|
|
|
|
printk("Checking keys...\n");
|
|
|
|
for(int k = 0; k < 144; k++) {
|
|
|
|
|
|
|
|
ENIntervalIdentifier rpi;
|
|
|
|
en_derive_period_identifier_key(&pik, &tek.tek);
|
|
|
|
// derive all RPIs
|
|
|
|
for (int j = 0; j < 144; j++) {
|
|
|
|
|
|
|
|
// we assume that at least one out of 144 RPIs was met (this is still a lot actually!)
|
|
|
|
uint32_t interval = j == 0 ? (CONFIG_ENS_MAX_CONTACTS / 144)*k : (CONFIG_ENS_MAX_CONTACTS+1);
|
|
|
|
en_derive_interval_identifier(&rpi, &pik, interval); //one of each is actually
|
|
|
|
|
|
|
|
uint32_t num_met = 0;
|
|
|
|
if (bloom_probably_has_record(bf, &rpi)) {
|
|
|
|
num_met++;
|
|
|
|
/*
|
|
|
|
record_iterator_t iterator;
|
|
|
|
|
|
|
|
time_t start = interval == 0 ? 0 : (interval+(CONFIG_ENS_MAX_CONTACTS-1)) % CONFIG_ENS_MAX_CONTACTS;
|
|
|
|
time_t end = (interval+(CONFIG_ENS_MAX_CONTACTS+1)) % CONFIG_ENS_MAX_CONTACTS;
|
|
|
|
|
|
|
|
int rc = ens_records_iterator_init_timerange(&iterator, &start, &end);
|
|
|
|
if (rc) {
|
|
|
|
// on error, skip this rpi
|
|
|
|
printk("iterator error! %d\n", rc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
record_t* current;
|
|
|
|
while ((current = ens_records_iterator_next(&iterator))) {
|
|
|
|
if (memcmp(&(current->rolling_proximity_identifier), rpi.b,
|
|
|
|
sizeof(current->rolling_proximity_identifier)) == 0) {
|
|
|
|
num_met++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ens_record_iterator_clear(&iterator);*/
|
|
|
|
}
|
|
|
|
if(j == 0 && num_met == 0) {
|
|
|
|
printk("Met mismatch! %u\n", num_met);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
k_sleep(K_MSEC(2000));
|
2021-07-21 14:05:08 +02:00
|
|
|
} while (1);
|
2022-05-27 21:52:29 +02:00
|
|
|
}
|