libcontrac
A library for contact tracing
Files | Data Structures | Macros | Functions
Key Generation

Core Contact Tracing functionality. More...

Files

file  contrac.c
 Core Contact Tracing functionality.
 
file  dtk_list.c
 Provides a list of DTKs.
 
file  rpi_list.c
 Provides a list of RPIs.
 
file  contrac.h
 Core Contact Tracing functionality.
 
file  contrac_private.h
 Private header for the Core Contact Tracing functionality.
 
file  dtk_list.h
 Provides a list of DTKs.
 
file  rpi_list.h
 Provides a list of RPIs.
 

Data Structures

struct  Contrac
 The core structure for storing Contact Tracing state. More...
 

Macros

#define STATUS_TK   (1 << 0)
 
#define STATUS_DTK   (1 << 1)
 
#define STATUS_RPI   (1 << 2)
 
#define STATUS_INITIALISED   (STATUS_TK | STATUS_DTK | STATUS_RPI)
 
#define TK_SIZE   (32)
 
#define TK_SIZE_BASE64   (44)
 

Functions

Contrac * contrac_new ()
 
void contrac_delete (Contrac *data)
 
bool contrac_generate_tracing_key (Contrac *data)
 
bool contrac_set_day_number (Contrac *data, uint32_t day_number)
 
bool contrac_set_time_interval_number (Contrac *data, uint8_t time_interval_number)
 
bool contrac_get_initialised (Contrac const *data)
 
uint32_t contrac_get_day_number (Contrac *data)
 
uint8_t contrac_get_time_interval_number (Contrac *data)
 
void contrac_set_tracing_key (Contrac *data, unsigned char const *tracing_key)
 
unsigned char const * contrac_get_tracing_key (Contrac const *data)
 
void contrac_get_tracing_key_base64 (Contrac const *data, char *base64)
 
bool contrac_set_tracing_key_base64 (Contrac *data, char const *tracing_key)
 
unsigned char const * contrac_get_daily_key (Contrac const *data)
 
void contrac_get_daily_key_base64 (Contrac const *data, char *base64)
 
unsigned char const * contrac_get_proximity_id (Contrac const *data)
 
void contrac_get_proximity_id_base64 (Contrac const *data, char *base64)
 
bool contrac_update_current_time (Contrac *data)
 

Detailed Description

Core Contact Tracing functionality.

This class provides the core Contact Tracing functionality. It provides an interfaces for:

  1. Generating a random Tracing Key.
  2. Generating a Daily Tracing Key based on the current day number.
  3. Generating a Rolling Proximity Identifier based on the current time interval number.

Values can be extracted and set in binary or base64 format.

Macro Definition Documentation

◆ STATUS_DTK

#define STATUS_DTK   (1 << 1)

A mask used internally with the status flags.

When set the flag indicates that the Daily Tracing Key has been correctly initialised.

◆ STATUS_INITIALISED

#define STATUS_INITIALISED   (STATUS_TK | STATUS_DTK | STATUS_RPI)

A mask used internally with the status flags.

When all of these flags are set it indicates that the structure is fully initialised.

◆ STATUS_RPI

#define STATUS_RPI   (1 << 2)

A mask used internally with the status flags.

When set the flag indicates that the Rolling Proximity Identifier has been correctly initialised.

◆ STATUS_TK

#define STATUS_TK   (1 << 0)

A mask used internally with the status flags.

When set the flag indicates that the Tracing Key has been correctly initialised.

◆ TK_SIZE

#define TK_SIZE   (32)

The size in bytes of a Tracing Key in binary format

◆ TK_SIZE_BASE64

#define TK_SIZE_BASE64   (44)

The size in bytes of a Tracing Key in base64 format

Function Documentation

◆ contrac_delete()

void contrac_delete ( Contrac *  data)

Deletes an instance of the class, freeing up the memory allocated to it.

Parameters
dataThe instance to free.

◆ contrac_generate_tracing_key()

bool contrac_generate_tracing_key ( Contrac *  data)

Generates a random Contact Tracing Key.

The operation may fail under certain circumstances, such as there being insufficient entropy in the system to guarantee a random result.

Parameters
dataThe context object to work with.
Returns
true if the operation completed successfully, false otherwise.

◆ contrac_get_daily_key()

unsigned char const * contrac_get_daily_key ( Contrac const *  data)

Gets the Daily Tracing Key for the device in binary format.

This allows the Daily Tracing Key to be extracted. The Daily Tracing Key should be kept secret (to maintain privacy) until a positive test is confirmed, at which point the user may choose to upload the key to a Diagnosis Server, so that others can be notified.

The buffer returned will contain exactly DTK_SIZE (16) bytes of data in binary format. This may therefore contain null bytes, and the buffer will not necessarily be null terminated. Future operations may cause the data to change, so the caller should make a copy of the buffer rather than keeping the pointer to it.

Parameters
dataThe context object to work with.
Returns
The Daily Tracing Key in binary format, not null terminated.

◆ contrac_get_daily_key_base64()

void contrac_get_daily_key_base64 ( Contrac const *  data,
char *  base64 
)

Gets the Daily Tracing Key for the device in base64 format.

This allows the Daily Tracing Key to be extracted. The Daily Tracing Key should be kept secret (to maintain privacy) until a positive test is confirmed, at which point the user may choose to upload the key to a Diagnosis Server, so that others can be notified.

The buffer provided must be at least DTK_SIZE_BASE64 + 1 (25) bytes long and will be filled out with the Tracing Key in base64 format (DTK_SIZE_BASE64 bytes) followed by a null terminator (1 byte).

Parameters
dataThe context object to work with.
base64A buffer of at least DTK_SIZE_BASE64 + 1 bytes for the result.

◆ contrac_get_day_number()

uint32_t contrac_get_day_number ( Contrac *  data)

Gets the current day number.

Gets the current day number used to generate the most recent DTK.

The day number is calculated as: (Number of Seconds since Epoch) / (60 * 60 * 24)

Which can be caluclated from the current epoch using the epoch_to_day_number() function.

Parameters
dataThe context object to work with.
Returns
The day number most recently used to generate the DTK.

◆ contrac_get_initialised()

bool contrac_get_initialised ( Contrac const *  data)

Gets whether the internal state has been fully configured or not.

The internal state must be fully configured before a Daily Tracing Key or Rolling Proximity Identifier can be calculated. This function returns whether it is in this state or not.

In order to fully configure the structure, a Tracing Key must either be generated using contrac_generate_tracing_key(), or set using either contrac_set_tracing_key() or contrac_set_tracing_key_base64().

In addition the day number and time interval number must be set using contrac_set_day_number() and contrac_set_time_interval_number() respectively.

Alternatively these can be set automatically based on the current time using contrac_update_current_time().

Parameters
dataThe context object to work with.
Returns
true if the state is fully initialised, false otherwise.

◆ contrac_get_proximity_id()

unsigned char const * contrac_get_proximity_id ( Contrac const *  data)

Gets the Rolling Proximity Identifier for the device in binary format.

This allows the Rolling Proximity Identifier to be extracted. The Rolling Proximity Identifier is for broadcast to other devices using BLE and changes frequently.

The buffer returned will contain exactly RPI_SIZE (16) bytes of data in binary format. This may therefore contain null bytes, and the buffer will not necessarily be null terminated. Future operations may cause the data to change, so the caller should make a copy of the buffer rather than keeping the pointer to it.

Parameters
dataThe context object to work with.
Returns
The Rolling Proximity Identifier in binary format, not null terminated.

◆ contrac_get_proximity_id_base64()

void contrac_get_proximity_id_base64 ( Contrac const *  data,
char *  base64 
)

Gets the Rolling Proximity Identifier for the device in base64 format.

This allows the Rolling Proximity Identifier to be extracted. The Rolling Proximity Identifier is for broadcast to other devices using BLE and changes frequently.

The buffer provided must be at least RPI_SIZE_BASE64 + 1 (25) bytes long and will be filled out with the Tracing Key in base64 format (RPI_SIZE_BASE64 bytes) followed by a null terminator (1 byte).

Parameters
dataThe context object to work with.
base64A buffer of at least RPI_SIZE_BASE64 + 1 bytes for the result.

◆ contrac_get_time_interval_number()

uint8_t contrac_get_time_interval_number ( Contrac *  data)

Gets the current time interval number.

Gets the current time interval number used to generate the most recent RPI.

The time interval number is calculated as: (Seconds Since Start of DayNumber) / (60 * 10)

and must fall in the interval [0, 143].

It can be caluclated from the current epoch using the epoch_to_time_interval_number() function.

Parameters
dataThe context object to work with.
Returns
The time interval number most recently used to generate the RPI.

◆ contrac_get_tracing_key()

unsigned char const * contrac_get_tracing_key ( Contrac const *  data)

Gets the Tracing Key for the device in binary format.

This allows the Tracing Key to be extracted. The Tracing Key should be kept secret (to maintain privacy), however it still may need to be extracted, for example so it can be saved in persistent storage between runs.

The buffer returned will contain exactly TK_SIZE (32) bytes of data in binary format. This may therefore contain null bytes, and the buffer will not necessarily be null terminated. Future operations may cause the data to change, so the caller should make a copy of the buffer rather than keeping the pointer to it.

Parameters
dataThe context object to work with.
Returns
The Tracing Key in binary format, not null terminated.

◆ contrac_get_tracing_key_base64()

void contrac_get_tracing_key_base64 ( Contrac const *  data,
char *  base64 
)

Gets the Tracing Key for the device in base64 format.

This allows the Tracing Key to be extracted. The Tracing Key should be kept secret (to maintain privacy), however it still may need to be extracted, for example so it can be saved in persistent storage between runs.

The buffer provided must be at least TK_SIZE_BAS64 + 1 (45) bytes long and will be filled out with the Tracing Key in base64 format (TK_SIZE_BAS64 bytes) followed by a null terminator (1 byte).

Parameters
dataThe context object to work with.
base64A buffer of at least TK_SIZE_BAS64 + 1 bytes for the result.

◆ contrac_new()

Contrac * contrac_new ( )

Creates a new instance of the class.

Returns
The newly created object.

◆ contrac_set_day_number()

bool contrac_set_day_number ( Contrac *  data,
uint32_t  day_number 
)

Sets the current day number.

This will result in a new Daily Tracing Key being generated based on the day provided. If neither the Tracing Key nor the day have changed, the DTK will remain the same.

The day number is calculated as: (Number of Seconds since Epoch) / (60 * 60 * 24)

Which can be calculated from the current epoch using the epoch_to_day_number() function.

The operation may fail if a Tracing Key has yet to be configured.

Parameters
dataThe context object to work with.
day_numberThe day number used to generate the DTK.
Returns
true if the operation completed successfully, false otherwise.

◆ contrac_set_time_interval_number()

bool contrac_set_time_interval_number ( Contrac *  data,
uint8_t  time_interval_number 
)

Sets the current time interval number.

This will result in a new Rolling Proximity Idetifier being generated based on the time interval number. If none of the Tracing Key, day nor time interval have changed, the RPI will stay the same.

The time interval number is calculated as: (Seconds Since Start of DayNumber) / (60 * 10)

and must fall in the interval [0, 143].

It can be calculated from the current epoch using the epoch_to_time_interval_number() function.

The operation may fail if a Tracing Key or Daily Tracing Key have yet to be configured.

Parameters
dataThe context object to work with.
time_interval_numberThe time interval number to set.
Returns
true if the operation completed successfully, false otherwise.

◆ contrac_set_tracing_key()

void contrac_set_tracing_key ( Contrac *  data,
unsigned char const *  tracing_key 
)

Sets the Tracing Key for the device in binary format.

When first configuring a system, the Tracing Key must be generated randomly, e.g. using contrac_generate_tracing_key().

However, on future runs it's important that the Tracing Key stays the same. In this case the key can be restored using this function.

The tracing_key buffer passed in must contain exactly TK_SIZE (32) bytes of data.It doen't have to be null terminated.

Parameters
dataThe context object to work with.
tracing_keyThe Tracing Key to set in binary format.

◆ contrac_set_tracing_key_base64()

bool contrac_set_tracing_key_base64 ( Contrac *  data,
char const *  tracing_key 
)

Sets the Tracing Key for the device in base64 format.

When first configuring a system, the Tracing Key must be generated randomly, e.g. using contrac_generate_tracing_key().

However, on future runs it's important that the Tracing Key stays the same. In this case the key can be restored using this function.

The tracing_key buffer passed in must contain exactly TK_SIZE_BASE64 (44) bytes of base64-encoded data. It can be null terminated, but doesn't need to be.

Parameters
dataThe context object to work with.
tracing_keyThe Tracing Key to set in base64 format.

◆ contrac_update_current_time()

bool contrac_update_current_time ( Contrac *  data)

Updates the Daily Tracing Key and Random Proxmity Identifer.

The Daily Tracing Key changes every day, the Random Proximity Identifier changes every 10 minutes.

Calling this function will update them both based on the current system time.

Note that getting either the DTK or RPI does not cause an update, so if you want to get the correct values based on the time, it makes sense to call this function before getting them.

The operation may fail if the state has not yet been fully initialised (for example if a Tracing Key has not yet been generated or set).

Parameters
dataThe context object to work with.
Returns
true if the operation completed successfully, false otherwise.