X-Git-Url: https://www.flypig.org.uk/git/?a=blobdiff_plain;f=src%2Fdtk.c;h=5faffd7d1d14c5c265ae0b0ba48d77cc28a13593;hb=e48c390a86b448137a87d0d0a5863c202ba66abd;hp=3febd9f64c3f6665eeaf3412fa71fbae42305c09;hpb=8515bbba069347de07a452586d7e49a2e8bdf151;p=libcontrac.git diff --git a/src/dtk.c b/src/dtk.c index 3febd9f..5faffd7 100644 --- a/src/dtk.c +++ b/src/dtk.c @@ -1,17 +1,24 @@ -/** \ingroup contrac +/** \ingroup DailyTracingKey * @file - * @author David Llewellyn-Jones + * @author David Llewellyn-Jones * @version $(VERSION) * * @section LICENSE * + * Copyright David Llewellyn-Jones, 2020 + * Released under the GPLv2. * - * - * @brief + * @brief Daily Tracing Key functionality * @section DESCRIPTION * + * This class is used to generate and manage the Daily Tracing Key (DTK). It's + * largely internal. The functionality from \ref Contrac should generally be + * used in preference to this. * - * + */ + +/** \addtogroup DailyTracingKey + * @{ */ // Includes @@ -34,10 +41,25 @@ // Defines +/** + * Used internally. + * + * This is the prefix for the Info parameter provided to the HKDF and used to + * generate the DTK. + */ #define DTK_INFO_PREFIX "CT-DTK" // Structures +/** + * @brief The structure used to represent a Daily Tracing Key. + * + * This is an opaque structure that contains information about the DTK.. + * + * This must be passed as the first parameter of every non-static function. + * + * The structure typedef is in dtk.h + */ struct _Dtk { // Daily key unsigned char dtk[DTK_SIZE]; @@ -48,6 +70,11 @@ struct _Dtk { // Function definitions +/** + * Creates a new instance of the class. + * + * @return The newly created object. + */ Dtk * dtk_new() { Dtk * data; @@ -56,6 +83,11 @@ Dtk * dtk_new() { return data; } +/** + * Deletes an instance of the class, freeing up the memory allocated to it. + * + * @param data The instance to free. + */ void dtk_delete(Dtk * data) { if (data) { // Clear the data for security @@ -65,12 +97,25 @@ void dtk_delete(Dtk * data) { } } -bool contrac_generate_daily_key(Dtk * data, Contrac const * contrac, uint32_t day_number) { +/** + * Generates a Daily Tracing Key based on the day number provided. + * + * The operation may fail under certain circumstances, such as if the + * HKDF operation fails for some reason. + * + * For internal use. It generally makes more sense to use the + * contrac_set_day_number() function instead. + * + * @param data The context object to work with. + * @param day_number The day number to use to generate the key. + * @return true if the operation completed successfully, false otherwise. + */ +bool dtk_generate_daily_key(Dtk * data, Contrac const * contrac, uint32_t day_number) { int result = 1; char encode[sizeof(DTK_INFO_PREFIX) + sizeof(day_number)]; size_t out_length = 0; EVP_PKEY_CTX *pctx = NULL; - const unsigned char * tk; + unsigned char const * tk; // dtk_i <- HKDF(tk, NULL, (UTF8("CT-DTK") || D_i), 16) @@ -123,17 +168,62 @@ bool contrac_generate_daily_key(Dtk * data, Contrac const * contrac, uint32_t da return (result > 0); } -const unsigned char * dtk_get_daily_key(Dtk const * data) { +/** + * Gets the Daily Tracing Key for the device in binary format. + * + * For internal use. It generally makes more sense to use the + * contrac_get_daily_key() function instead. + * + * 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 + * a pointer to it. + * + * @param data The context object to work with. + * @return The Daily Tracing Key in binary format, not null terminated. + */ +unsigned char const * dtk_get_daily_key(Dtk const * data) { return data->dtk; } +/** + * Gets the day number that applies to the current DTK. + * + * For internal use. It generally makes more sense to use the + * contrac_get_day_number() function instead. + * + * @param data The context object to work with. + * @return The day number stored in the object.. + */ uint32_t dtk_get_day_number(Dtk const * data) { return data->day_number; } +/** + * Populates the data structure. + * + * Allows the DTK and day number values of the object to be set explicitly. + * + * For internal use. To set the DTK it generally makes more sense to use one of + * eiher contrac_set_day_number() or contrac_update_current_time() instead. + * + * The dtk_bytes buffer passed in must contain exactly DTK_SIZE (16) bytes of + * data. It doen't have to be null terminated. + * + * @param data The context object to work with. + * @param dtk_bytes The DTK value to set, in binary format. + * @param day_number The day number to associate with this DTK. + */ void dtk_assign(Dtk * data, unsigned char const * dtk_bytes, uint32_t day_number) { memcpy(data->dtk, dtk_bytes, DTK_SIZE); data->day_number = day_number; } +/** @} addtogroup DailyTracingKey */