From patchwork Thu Aug 8 16:21:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Caleb Connolly X-Patchwork-Id: 817715 Delivered-To: patch@linaro.org Received: by 2002:a5d:4e11:0:b0:367:895a:4699 with SMTP id p17csp943990wrt; Thu, 8 Aug 2024 09:22:38 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVDkLudc2wPdSGr/DrrjFMgS92Up7pJfmv4eikzHlgkQdvlh73d1jjqt+qpjfenTy2H9dSI1D0S1R62F0PC9A7q X-Google-Smtp-Source: AGHT+IEpgGBs6fWaXGC8bCinjzTDjrCYrKhi2Q/oVqo0nLUWQurcMH+vneE/QjHnvbTb8gvRYcYJ X-Received: by 2002:a05:6512:238e:b0:52e:9b2f:c313 with SMTP id 2adb3069b0e04-530e581fc26mr1714745e87.22.1723134157839; Thu, 08 Aug 2024 09:22:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1723134157; cv=none; d=google.com; s=arc-20160816; b=RRQA+SzUxcyUWi8D0fCHNjkCTM/KDZaXeGarIYnLtB9gbQmaYehjLBEhMNjW3ZQ0Jc Dcz5Sgn8BlVKeVdqRVvPCx0NmCPPyVkLkOCWZ8hL+aL7e7TSUTEQ8V5EZg25rQIxMjsr vMopxtqOvKy9rs+I8c4wUV8ClKtSTGDmNF8H/Az8f+tI0XSwQt/cswCU2pbUGH5luPBx +tzh0JJpH2nlFzjPB6m9sc0VRv72dlfPtsj0lTmHO39sntPbo6KbVJBAmNABgcgFMJEj 2sXnB8nAXjvXSaqok3oDlv9Pc3k71JuYBFNHRr/rwyWFlpL6sXx70VQ8uaf987z5v+de wb/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:cc:to:in-reply-to:references :message-id:content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=egA3xRpkyqDRzibem82Wk57TPGhBhb+v8n5ItP4pcqI=; fh=D1MM0yShFdyHWcxpQmw7ZCHxPKzujVYU/yLrUnJrEWQ=; b=JIx4LbRqqDNWF3itAp84hkcw+/R9fugmt+JZm2+pPgT8+ypOKJRWXq9Ft3US7tSEIr tqjSh3A5sP4r57gsoiNyURhk5jGogX7iE+t0QN8ZouMM3TFLxPR4FwdjitL2AGmFfsw+ vSLaWpcgtcULdY0iaHHDdcL9mXrzl1RqqSQhxdIP6rzSlN2sFyAp1+7UePiy9HjVcUYW 7jq3pudwaSG6OwGQkoYo+m6zBg2PUiO1L39QxYsOVXcr8YT/mnNG2O76f12J92DDyqsB lGJZR4Hklxi9rmvPszuZJMCB4phG771N1vwOlhob/Dc/l3pxZX8VzpYfkTeJydywob1j Vyxg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H6hFPADo; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id a640c23a62f3a-a7dc9eb89a9si841639666b.566.2024.08.08.09.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 09:22:37 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=H6hFPADo; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 97C5688BA4; Thu, 8 Aug 2024 18:22:01 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="H6hFPADo"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id EB31088BA5; Thu, 8 Aug 2024 18:21:58 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 78CA388AF8 for ; Thu, 8 Aug 2024 18:21:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=caleb.connolly@linaro.org Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-5a3b866ebc9so1332346a12.3 for ; Thu, 08 Aug 2024 09:21:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1723134116; x=1723738916; darn=lists.denx.de; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=egA3xRpkyqDRzibem82Wk57TPGhBhb+v8n5ItP4pcqI=; b=H6hFPADoGz8mvIIQtmVZsR9UuR5UL/t8blLEjul3+cOlRoQ8V5nP3SZoAly5Mcv9nY ZRIGO1oHFW1JCTofRDW30vP+BENyd5OkyUqttDTgHbBHvHi4oPSPeOo0SG1ModwXo5Xi HlfcanMBImvt21JC9N4zPeMSqnmHpfZW71XnVez+X6zAtN0b4FBBVX36AveVzbZgCP97 K/Xy7fEjen+lZHWoGEOkNjp8jK5Gjy+7+ez+37i/Xig5i2q6DqYrCJ9x6As2e5TIw2dT Icf7MVHEMBeWgPpVV7YvxDUGMHiDbdZooV4rwOjlMpo1bqpkLE9DPblUyKDbSzgoOgm+ jSAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723134116; x=1723738916; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=egA3xRpkyqDRzibem82Wk57TPGhBhb+v8n5ItP4pcqI=; b=PgS4IW7CyBAyF+dSutaO44BnnY85JucqHJkWe0pKG824bas3RMC/y3vm5CGchYhFb6 8VGphnowlyIhYo9YfkODVWweaLkiEkdu2f3wjnumCqsqZwirXAlagzGp7YLyk4bKioBZ aOImDBXq3g93UVWDiZRuE9b1ZsehkPyqqvgR8pdZ3FKc+lpzC30q5vCpZYJh92Ct4Z7R z2gyii36NNx8O9FazzJ+OsHSnFTJCnlVmoYYjqXoia+gnxDdyB/qUD0fRxxsyLq/Chan Mz8jdNBpo5Sid5py/bc8y9YS7jW42xU1EnA1f2IP9BajrP1eErqUMDwmU+nzsDtSEee6 ZJ6A== X-Forwarded-Encrypted: i=1; AJvYcCW1HXAnTTXwPiye9mcASYOpg6Da4mcIxL70ozYVQUcLOV9Wbizrv2lfSOeg/s5rHf44hZitJaXCi6bGJv02LgrOEz2G7A== X-Gm-Message-State: AOJu0YzHKGBYL2R2ju4HrZf6OfAMXYJD5UaiGsXqy2yo4VFSj/VoCgqi fbhdVF+wCfviUZBvjBWZ4eOE1z9inJMgsfIa/sVrS2Tni6vNePUtzAWGlB4WJDQ= X-Received: by 2002:a05:6402:234c:b0:5a1:7362:91d9 with SMTP id 4fb4d7f45d1cf-5bbb233eefcmr2292387a12.22.1723134115654; Thu, 08 Aug 2024 09:21:55 -0700 (PDT) Received: from [192.168.0.113] ([2a02:8109:aa0d:be00::7424]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5bbb2e5c8fbsm812351a12.79.2024.08.08.09.21.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 09:21:55 -0700 (PDT) From: Caleb Connolly Date: Thu, 08 Aug 2024 18:21:49 +0200 Subject: [PATCH v6 02/11] lib: uuid: add UUID v5 support MIME-Version: 1.0 Message-Id: <20240808-b4-dynamic-uuid-v6-2-9332e7237119@linaro.org> References: <20240808-b4-dynamic-uuid-v6-0-9332e7237119@linaro.org> In-Reply-To: <20240808-b4-dynamic-uuid-v6-0-9332e7237119@linaro.org> To: Tom Rini , Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Mario Six , Alper Nebi Yasak , Abdellatif El Khlifi Cc: Richard Hughes , u-boot@lists.denx.de, Caleb Connolly X-Mailer: b4 0.14-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=5441; i=caleb.connolly@linaro.org; h=from:subject:message-id; bh=JSqqjukXOTthTri0V10jRsBkn3907XaleehDt877lBY=; b=owEBbQKS/ZANAwAIAQWDMSsZX2S2AcsmYgBmtPCeGgXM1hOPfbxx57PJhY+Q+RekZFcHA8AMp dIRWCU9NIuJAjMEAAEIAB0WIQS2UaFGPGq+0GkMVc0FgzErGV9ktgUCZrTwngAKCRAFgzErGV9k tthoD/9kg77QJoYAtLLW1PQ9a1pRHhqzKaszwxMB58GCvRLQISfTDmhjYnHAJlvPArUcMJeDM8p ZE1VMnM5LQ4KDtjMqpDycQIwyTyNyMmTLljA5f4Ah0hSM6j0S1QZ+J1qIc5+oIK1D4c2y3Tvv95 l/LEuAiVowywh5WdZMKd7iBxg8pUfU4nsMC6hLLNKg1bt23F+TaaAFHEOXXhVu03uIk5n2stsp7 4N15JyaSzJa6hjH8ljU/6r1mxNLPQpviTwc+yPt6q5qv3IgH66+mbGYkG8GtBeA1NZ7OlE6Jmj4 mdnv2vL+/7rZfjk6hOauJtFyvjtkqGpXSiS8GCPuieLf3O9ALAXTdP5URnNUMDP+gDDVfhFfE1c p5DlNwD4K365eKPnw3LwuihD9MmXYUZrUkrR0QDjvBRiuIMV3o7sA8Vr1RX9UUdq3iOng5YMeh8 xUfqSrkgjvhXRACBiywQKetFnUC3jjvw/FASomBIuav1pKtdV0BR9Y/EBdQaLC6ynIW3j1bQAev so9/5bZdgrHy7L2MYA9BMKHXJOwUzOj8YDLumlKH5cvpvIYJS0lZrERMVOhH1rFGRcip9eJvBat oAvr/xNThIVeWyGoXvG2BtNV/APOJlkqYfhzW18yazwjFQti73qiiSpILk1qB8+Ibm13Wru4lqy 35Qg84OPVFc5PkA== X-Developer-Key: i=caleb.connolly@linaro.org; a=openpgp; fpr=83B24DA7FE145076BC38BB250CD904EB673A7C47 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add support for generating version 5 UUIDs, these are determistic and work by hashing a "namespace" UUID together with some unique data. One intended usecase is to allow for dynamically generate payload UUIDs for UEFI capsule updates, so that supported boards can have their own UUIDs without needing to hardcode them. In addition, move the common bit twiddling code from gen_ran_uuid into a separate function and rewrite it not to use clrsetbits (which is not available when building as part of host tools). Tests for this are added in an upcoming patch. Signed-off-by: Caleb Connolly --- include/uuid.h | 17 +++++++++++++++-- lib/Kconfig | 1 + lib/uuid.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/include/uuid.h b/include/uuid.h index f5a941250f48..1f4fa103b5e9 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -10,8 +10,9 @@ #ifndef __UUID_H__ #define __UUID_H__ #include +#include /* * UUID - Universally Unique IDentifier - 128 bits unique number. * There are 5 versions and one variant of UUID defined by RFC4122 @@ -45,10 +46,10 @@ * where x is a hexadecimal character. Fields are separated by '-'s. * When converting to a binary UUID, le means the field should be converted * to little endian and be means it should be converted to big endian. * - * UUID is also used as GUID (Globally Unique Identifier) with the same binary - * format but it differs in string format like below. + * UUID is also used as GUID (Globally Unique Identifier) with the same format + * but with some fields stored in little endian. * * GUID: * 0 9 14 19 24 * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx @@ -142,8 +143,20 @@ void gen_rand_uuid(unsigned char *uuid_bin); * @param - uuid output type: UUID - 0, GUID - 1 */ void gen_rand_uuid_str(char *uuid_str, int str_format); +struct efi_guid; + +/** + * gen_v5_guid() - generate little endian v5 GUID from namespace and other seed data. + * + * @namespace: pointer to UUID namespace salt + * @guid: pointer to allocated GUID output + * @...: NULL terminated list of seed data as pairs of pointers + * to data and their lengths + */ +void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...); + /** * uuid_str_to_le_bin() - Convert string UUID to little endian binary data. * @uuid_str: pointer to UUID string * @uuid_bin: pointer to allocated array for little endian output [16B] diff --git a/lib/Kconfig b/lib/Kconfig index 2059219a1207..5a48c016d2c5 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -72,8 +72,9 @@ config HAVE_PRIVATE_LIBGCC bool config LIB_UUID bool + select SHA1 config RANDOM_UUID bool "GPT Random UUID generation" select LIB_UUID diff --git a/lib/uuid.c b/lib/uuid.c index dfa2320ba267..24b5881600fd 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -21,8 +21,9 @@ #include #include #include #include +#include int uuid_str_valid(const char *uuid) { int i, valid; @@ -368,8 +369,57 @@ void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str, } } } +static void configure_uuid(struct uuid *uuid, unsigned char version) +{ + uint16_t tmp; + + /* Configure variant/version bits */ + tmp = be16_to_cpu(uuid->time_hi_and_version); + tmp = (tmp & ~UUID_VERSION_MASK) | (version << UUID_VERSION_SHIFT); + uuid->time_hi_and_version = cpu_to_be16(tmp); + + uuid->clock_seq_hi_and_reserved &= ~UUID_VARIANT_MASK; + uuid->clock_seq_hi_and_reserved |= (UUID_VARIANT << UUID_VARIANT_SHIFT); +} + +void gen_v5_guid(const struct uuid *namespace, struct efi_guid *guid, ...) +{ + sha1_context ctx; + va_list args; + const uint8_t *data; + uint32_t *tmp32; + uint16_t *tmp16; + uint8_t hash[SHA1_SUM_LEN]; + + sha1_starts(&ctx); + /* Hash the namespace UUID as salt */ + sha1_update(&ctx, (unsigned char *)namespace, UUID_BIN_LEN); + va_start(args, guid); + + while ((data = va_arg(args, const uint8_t *))) { + unsigned int len = va_arg(args, size_t); + sha1_update(&ctx, data, len); + } + + va_end(args); + sha1_finish(&ctx, hash); + + /* Truncate the hash into output UUID, it is already big endian */ + memcpy(guid, hash, sizeof(*guid)); + + configure_uuid((struct uuid *)guid, 5); + + /* Make little endian */ + tmp32 = (uint32_t *)&guid->b[0]; + *tmp32 = cpu_to_le32(be32_to_cpu(*tmp32)); + tmp16 = (uint16_t *)&guid->b[4]; + *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16)); + tmp16 = (uint16_t *)&guid->b[6]; + *tmp16 = cpu_to_le16(be16_to_cpu(*tmp16)); +} + #if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID) void gen_rand_uuid(unsigned char *uuid_bin) { u32 ptr[4]; @@ -394,15 +444,9 @@ void gen_rand_uuid(unsigned char *uuid_bin) /* Set all fields randomly */ for (i = 0; i < 4; i++) ptr[i] = rand(); - clrsetbits_be16(&uuid->time_hi_and_version, - UUID_VERSION_MASK, - UUID_VERSION << UUID_VERSION_SHIFT); - - clrsetbits_8(&uuid->clock_seq_hi_and_reserved, - UUID_VARIANT_MASK, - UUID_VARIANT << UUID_VARIANT_SHIFT); + configure_uuid(uuid, UUID_VERSION); memcpy(uuid_bin, uuid, 16); }