[PATCH RFC 1/4] lib: uuid: add UUID v5 support

Caleb Connolly caleb.connolly at linaro.org
Fri May 24 14:20:49 CEST 2024



On 24/05/2024 08:01, Ilias Apalodimas wrote:
> [...]
> 
>>   #include <dm/uclass.h>
>>   #include <rng.h>
>> +#include <u-boot/sha1.h>
>>
>>   int uuid_str_valid(const char *uuid)
>>   {
>>          int i, valid;
>> @@ -368,8 +369,40 @@ void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str,
>>                  }
>>          }
>>   }
>>
>> +#if CONFIG_IS_ENABLED(UUID_GEN_V5)
>> +void gen_uuid_v5(struct uuid *namespace, struct uuid *uuid, ...)
>> +{
>> +       sha1_context ctx;
>> +       va_list args;
>> +       const u8 *data;
>> +       u8 hash[SHA1_SUM_LEN];
>> +
>> +       sha1_starts(&ctx);
>> +       /* Hash the namespace UUID as salt */
>> +       sha1_update(&ctx, (char *)namespace, UUID_BIN_LEN);
>> +       va_start(args, uuid);
> 
> Should we use sha1 here? Is it described somewhere in UUIDv5 requirements?
> If not I'd rather have a sha256

The spec says sha1 yeah, this doesn't need to be cryptographically 
secure (the inputs are generally known) but just not have collisions.

That said, we don't need to be spec compliant - just consistent. So I'm 
fine either way. I'd err on the side of what's fastest to compute (if 
that even matters here).
> 
>> +
>> +       while ((data = va_arg(args, const u8 *)))
>> +               sha1_update(&ctx, (char *)data, va_arg(args, int));
> 
> sha1_update second argument is an unsigned int

Ah thanks.
> 
>> +
>> +       va_end(args);
>> +       sha1_finish(&ctx, hash);
>> +
>> +       /* Truncate the hash into output UUID and convert it to big endian */
>> +       cpu_to_be32_array((u32 *)uuid, (u32 *)hash, 4);
>> +
>> +       /* Configure variant/version bits */
>> +       clrsetbits_be16(&uuid->time_hi_and_version,
>> +                       UUID_VERSION_MASK,
>> +                       5 << UUID_VERSION_SHIFT);
>> +       clrsetbits_8(&uuid->clock_seq_hi_and_reserved,
>> +                    UUID_VARIANT_MASK,
>> +                    UUID_VARIANT << UUID_VARIANT_SHIFT);
>> +}
>> +#endif
>> +
>>   #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
>>   void gen_rand_uuid(unsigned char *uuid_bin)
>>   {
>>          u32 ptr[4];
>>
>> --
>> 2.44.0
>>
> 
> Thanks
> /Ilias

-- 
// Caleb (they/them)


More information about the U-Boot mailing list