From nobody Fri Dec 19 07:53:23 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1745334723; cv=none; d=zohomail.com; s=zohoarc; b=dD3NviwqMCVZ/CqKpoXdQIdOupDaLtJyXrRhB9218IPkJNXMmfJN+fPKG2XAj67+LZEVJaXRNUcpryWBK+gbkvhSMH0BqJnfmtx8fuowBEzelJoAaH3H69eIOuzdbZ/3jmEEOOj4ue3QexZa7FFLtkq5NblngqYRL/g4UzhZT9g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745334723; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cpM4QmeM8xWvrjEkqxDqzsZbZ3g224BrNjdlnp8nQ/I=; b=PLaNA1M8T6qlKIEfmGqYcfOOMKgHlaIkOx+QTGKVnXSRiutv82OO18V6hpxpsrvzM/AmwRPGw7b9GaBTIAPSRJ6y6aLt0BBFNiVNgZs8DdzMUw8ufLgplRe8tGsW7w/li/ZPqu/gYryBoIXh9DI+VtLsO2npORDGEZEnlozLoOY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1745334723781927.1021908916343; Tue, 22 Apr 2025 08:12:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.962927.1354099 (Exim 4.92) (envelope-from ) id 1u7FHp-0003Ae-CM; Tue, 22 Apr 2025 15:11:49 +0000 Received: by outflank-mailman (output) from mailman id 962927.1354099; Tue, 22 Apr 2025 15:11:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u7FHp-00039a-7P; Tue, 22 Apr 2025 15:11:49 +0000 Received: by outflank-mailman (input) for mailman id 962927; Tue, 22 Apr 2025 15:11:47 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u7FDh-0000lQ-Gd for xen-devel@lists.xenproject.org; Tue, 22 Apr 2025 15:07:33 +0000 Received: from 10.mo550.mail-out.ovh.net (10.mo550.mail-out.ovh.net [178.32.96.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 8423ef29-1f8b-11f0-9eb0-5ba50f476ded; Tue, 22 Apr 2025 17:07:32 +0200 (CEST) Received: from director8.ghost.mail-out.ovh.net (unknown [10.108.2.23]) by mo550.mail-out.ovh.net (Postfix) with ESMTP id 4Zhlw428byz1Xrw for ; Tue, 22 Apr 2025 15:07:32 +0000 (UTC) Received: from ghost-submission-5b5ff79f4f-f9k6v (unknown [10.110.118.84]) by director8.ghost.mail-out.ovh.net (Postfix) with ESMTPS id 69FDB1FD44; Tue, 22 Apr 2025 15:07:31 +0000 (UTC) Received: from 3mdeb.com ([37.59.142.104]) by ghost-submission-5b5ff79f4f-f9k6v with ESMTPSA id rxlND7OwB2hWRgEAxaKnhg (envelope-from ); Tue, 22 Apr 2025 15:07:31 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 8423ef29-1f8b-11f0-9eb0-5ba50f476ded Authentication-Results: garm.ovh; auth=pass (GARM-104R00542a86e06-f440-4221-95ca-16fc201bff01, 7E508E014E7E7C169EB13C6E22C3C4EBF1F0FDD7) smtp.auth=sergii.dmytruk@3mdeb.com X-OVh-ClientIp: 176.111.181.178 From: Sergii Dmytruk To: xen-devel@lists.xenproject.org Cc: Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini , trenchboot-devel@googlegroups.com Subject: [PATCH 10/21] lib/sha256.c: add file Date: Tue, 22 Apr 2025 18:06:44 +0300 Message-ID: <92b461c8d5981a523293341346274b6cc1b76d9b.1745172094.git.sergii.dmytruk@3mdeb.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Ovh-Tracer-Id: 12732802049230353564 X-VR-SPAMSTATE: OK X-VR-SPAMSCORE: -105 X-VR-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddvgeegtdehucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuqfggjfdpvefjgfevmfevgfenuceurghilhhouhhtmecuhedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnegfrhhlucfvnfffucdlqdehmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefuvghrghhiihcuffhmhihtrhhukhcuoehsvghrghhiihdrughmhihtrhhukhesfehmuggvsgdrtghomheqnecuggftrfgrthhtvghrnhepvedvgfeukeehhfevuddvheetudekkefggfeiveehvefhgfehgfffhffgvefhudejnecuffhomhgrihhnpehgihhthhhusgdrtghomhdpnhhishhtrdhgohhvnecukfhppeduvdejrddtrddtrddupddujeeirdduuddurddukedurddujeekpdefjedrheelrddugedvrddutdegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepuddvjedrtddrtddruddpmhgrihhlfhhrohhmpehsvghrghhiihdrughmhihtrhhukhesfehmuggvsgdrtghomhdpnhgspghrtghpthhtohepuddprhgtphhtthhopeigvghnqdguvghvvghlsehlihhsthhsrdigvghnphhrohhjvggtthdrohhrghdpoffvtefjohhsthepmhhoheehtdgmpdhmohguvgepshhmthhpohhuth DKIM-Signature: a=rsa-sha256; bh=cpM4QmeM8xWvrjEkqxDqzsZbZ3g224BrNjdlnp8nQ/I=; c=relaxed/relaxed; d=3mdeb.com; h=From; s=ovhmo3617313-selector1; t=1745334452; v=1; b=LE9iVO/+PznrQo2udORusIQdcf0bKmGUAd4jA8Eo1BR+HmyxpHzggY9ZMLdnkwEp9pdwbPRb EI2KJINBdDu6fFBwmQIje9ajHi7/eHzOlHTqqEeceHzwDZaipnNUc4XYfwt0TCJy1v8QrehKJvR 2g0twC6fZSvdDhlyktFja1KStZIepnop32DPDj04pmi6c9IQF85uPnnO0bi4bPaeV5JRGg4UD2e bIM/otbqekD4+1bOZuuKDxCUhvGuF7zdiex7xNJnTal/W+ffN6yfJQtyH0GYQFnEFhx1hp7nBfS mNg7paDZJfTok8C26E1M4R9b9+dwfZCo1K/37gMB2Bp2w== X-ZohoMail-DKIM: pass (identity @3mdeb.com) X-ZM-MESSAGEID: 1745334724577019000 Content-Type: text/plain; charset="utf-8" The code comes from [1] and is licensed under GPL-2.0 or later version of the license. It's a combination of: - include/crypto/sha2.h - include/crypto/sha256_base.h - lib/crypto/sha256.c - crypto/sha256_generic.c Changes: - includes - formatting - renames and splicing of some trivial functions that are called once - dropping of `int` return values (only zero was ever returned) - getting rid of references to `struct shash_desc` [1]: https://github.com/torvalds/linux/tree/afdab700f65e14070d8ab92175544b1= c62b8bf03 Signed-off-by: Sergii Dmytruk Signed-off-by: Krystian Hebel --- xen/include/xen/sha256.h | 12 ++ xen/lib/Makefile | 1 + xen/lib/sha256.c | 238 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 251 insertions(+) create mode 100644 xen/include/xen/sha256.h create mode 100644 xen/lib/sha256.c diff --git a/xen/include/xen/sha256.h b/xen/include/xen/sha256.h new file mode 100644 index 0000000000..703eddc198 --- /dev/null +++ b/xen/include/xen/sha256.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef __XEN_SHA256_H +#define __XEN_SHA256_H + +#include + +#define SHA256_DIGEST_SIZE 32 + +void sha256_hash(const u8 *data, unsigned int len, u8 *out); + +#endif /* !__XEN_SHA256_H */ diff --git a/xen/lib/Makefile b/xen/lib/Makefile index 0d5774b8d7..c7a8d1bb02 100644 --- a/xen/lib/Makefile +++ b/xen/lib/Makefile @@ -39,6 +39,7 @@ lib-y +=3D strtoul.o lib-y +=3D strtoull.o lib-$(CONFIG_X86) +=3D x86-generic-hweightl.o lib-$(CONFIG_X86) +=3D sha1.o +lib-$(CONFIG_X86) +=3D sha256.o lib-$(CONFIG_X86) +=3D xxhash32.o lib-$(CONFIG_X86) +=3D xxhash64.o =20 diff --git a/xen/lib/sha256.c b/xen/lib/sha256.c new file mode 100644 index 0000000000..369a52af80 --- /dev/null +++ b/xen/lib/sha256.c @@ -0,0 +1,238 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * SHA-256, as specified in + * http://csrc.nist.gov/groups/STM/cavp/documents/shs/sha256-384-512.pdf + * + * SHA-256 code by Jean-Luc Cooke . + * + * Copyright (c) Jean-Luc Cooke + * Copyright (c) Andrew McDonald + * Copyright (c) 2002 James Morris + * Copyright (c) 2014 Red Hat Inc. + */ + +#include +#include +#include + +#define SHA256_BLOCK_SIZE 64 + +struct sha256_state { + uint32_t state[SHA256_DIGEST_SIZE / 4]; + uint64_t count; + uint8_t buf[SHA256_BLOCK_SIZE]; +}; + +typedef void sha256_block_fn(struct sha256_state *sst, uint8_t const *src, + int blocks); + +static const uint32_t SHA256_K[] =3D { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, +}; + +static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z) +{ + return z ^ (x & (y ^ z)); +} + +static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z) +{ + return (x & y) | (z & (x | y)); +} + +#define e0(x) (ror32(x, 2) ^ ror32(x, 13) ^ ror32(x, 22)) +#define e1(x) (ror32(x, 6) ^ ror32(x, 11) ^ ror32(x, 25)) +#define s0(x) (ror32(x, 7) ^ ror32(x, 18) ^ (x >> 3)) +#define s1(x) (ror32(x, 17) ^ ror32(x, 19) ^ (x >> 10)) + +static void LOAD_OP(int I, uint32_t *W, const uint8_t *input) +{ + W[I] =3D get_unaligned_be32((uint32_t *)input + I); +} + +static void BLEND_OP(int I, uint32_t *W) +{ + W[I] =3D s1(W[I - 2]) + W[I - 7] + s0(W[I - 15]) + W[I - 16]; +} + +#define SHA256_ROUND(i, a, b, c, d, e, f, g, h) do { \ + uint32_t t1, t2; \ + t1 =3D h + e1(e) + Ch(e, f, g) + SHA256_K[i] + W[i]; \ + t2 =3D e0(a) + Maj(a, b, c); \ + d +=3D t1; \ + h =3D t1 + t2; \ + } while ( 0 ) + +static void sha256_init(struct sha256_state *sctx) +{ + sctx->state[0] =3D 0x6a09e667UL; + sctx->state[1] =3D 0xbb67ae85UL; + sctx->state[2] =3D 0x3c6ef372UL; + sctx->state[3] =3D 0xa54ff53aUL; + sctx->state[4] =3D 0x510e527fUL; + sctx->state[5] =3D 0x9b05688cUL; + sctx->state[6] =3D 0x1f83d9abUL; + sctx->state[7] =3D 0x5be0cd19UL; + sctx->count =3D 0; +} + +static void sha256_do_update(struct sha256_state *sctx, + const uint8_t *data, + unsigned int len, + sha256_block_fn *block_fn) +{ + unsigned int partial =3D sctx->count % SHA256_BLOCK_SIZE; + + sctx->count +=3D len; + + if ( unlikely((partial + len) >=3D SHA256_BLOCK_SIZE) ) + { + int blocks; + + if ( partial ) + { + int p =3D SHA256_BLOCK_SIZE - partial; + + memcpy(sctx->buf + partial, data, p); + data +=3D p; + len -=3D p; + + block_fn(sctx, sctx->buf, 1); + } + + blocks =3D len / SHA256_BLOCK_SIZE; + len %=3D SHA256_BLOCK_SIZE; + + if ( blocks ) + { + block_fn(sctx, data, blocks); + data +=3D blocks * SHA256_BLOCK_SIZE; + } + partial =3D 0; + } + if ( len ) + memcpy(sctx->buf + partial, data, len); +} + +static void sha256_do_finalize(struct sha256_state *sctx, + sha256_block_fn *block_fn) +{ + const int bit_offset =3D SHA256_BLOCK_SIZE - sizeof(__be64); + __be64 *bits =3D (__be64 *)(sctx->buf + bit_offset); + unsigned int partial =3D sctx->count % SHA256_BLOCK_SIZE; + + sctx->buf[partial++] =3D 0x80; + if ( partial > bit_offset ) + { + memset(sctx->buf + partial, 0x0, SHA256_BLOCK_SIZE - partial); + partial =3D 0; + + block_fn(sctx, sctx->buf, 1); + } + + memset(sctx->buf + partial, 0x0, bit_offset - partial); + *bits =3D cpu_to_be64(sctx->count << 3); + block_fn(sctx, sctx->buf, 1); +} + +static void sha256_finish(struct sha256_state *sctx, uint8_t *out, + unsigned int digest_size) +{ + __be32 *digest =3D (__be32 *)out; + int i; + + for ( i =3D 0; digest_size > 0; i++, digest_size -=3D sizeof(__be32) ) + put_unaligned_be32(sctx->state[i], digest++); + + memset(sctx, 0, sizeof(*sctx)); +} + +static void sha256_transform(uint32_t *state, const uint8_t *input, uint32= _t *W) +{ + uint32_t a, b, c, d, e, f, g, h; + int i; + + /* load the input */ + for ( i =3D 0; i < 16; i +=3D 8 ) + { + LOAD_OP(i + 0, W, input); + LOAD_OP(i + 1, W, input); + LOAD_OP(i + 2, W, input); + LOAD_OP(i + 3, W, input); + LOAD_OP(i + 4, W, input); + LOAD_OP(i + 5, W, input); + LOAD_OP(i + 6, W, input); + LOAD_OP(i + 7, W, input); + } + + /* now blend */ + for ( i =3D 16; i < 64; i +=3D 8 ) + { + BLEND_OP(i + 0, W); + BLEND_OP(i + 1, W); + BLEND_OP(i + 2, W); + BLEND_OP(i + 3, W); + BLEND_OP(i + 4, W); + BLEND_OP(i + 5, W); + BLEND_OP(i + 6, W); + BLEND_OP(i + 7, W); + } + + /* load the state into our registers */ + a =3D state[0]; b =3D state[1]; c =3D state[2]; d =3D state[3]; + e =3D state[4]; f =3D state[5]; g =3D state[6]; h =3D state[7]; + + /* now iterate */ + for ( i =3D 0; i < 64; i +=3D 8 ) + { + SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h); + SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g); + SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f); + SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e); + SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d); + SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c); + SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b); + SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a); + } + + state[0] +=3D a; state[1] +=3D b; state[2] +=3D c; state[3] +=3D d; + state[4] +=3D e; state[5] +=3D f; state[6] +=3D g; state[7] +=3D h; +} + +static void sha256_transform_blocks(struct sha256_state *sctx, + const uint8_t *input, int blocks) +{ + uint32_t W[64]; + + do { + sha256_transform(sctx->state, input, W); + input +=3D SHA256_BLOCK_SIZE; + } while ( --blocks ); + + memset(W, 0, sizeof(W)); +} + +void sha256_hash(const uint8_t *data, unsigned int len, uint8_t *out) +{ + struct sha256_state sctx; + + sha256_init(&sctx); + sha256_do_update(&sctx, data, len, sha256_transform_blocks); + sha256_do_finalize(&sctx, sha256_transform_blocks); + sha256_finish(&sctx, out, SHA256_DIGEST_SIZE); +} --=20 2.49.0