Skip to contentSkip to conversation

xen/lib: SHA2 cleanup, and SHA1 support

PATCH In review Andrew Cooper 2 participants 8 messages Nov 28, 2025 – Dec 2, 2025

lore
Share & tools
lore
#0 lore raw

cover xen/lib: SHA2 cleanup, and SHA1 support

Pulled out of the Trenchboot series.

Andrew Cooper (1):
xen/lib: Misc SHA2 cleanup

Krystian Hebel (1):
xen/lib: Introduce SHA-1

 xen/arch/x86/cpu/microcode/amd.c |   2 +-
 xen/include/xen/sha1.h           |  14 ++
 xen/include/xen/sha2.h           |   4 +-
 xen/lib/Makefile                 |   1 +
 xen/lib/sha1.c                   | 215 +++++++++++++++++++++++++++++++
 xen/lib/sha2-256.c               |   8 +-
 6 files changed, 237 insertions(+), 7 deletions(-)
 create mode 100644 xen/include/xen/sha1.h
 create mode 100644 xen/lib/sha1.c
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/2 xen/lib: Misc SHA2 cleanup

In sha2_256_transform(), make state be an array of 8 rather than a pointer, which lets the compiler do slightly more checking.

For better or worse, the canonical API in Linux is just the algorithm name. As we're intending to import more from Linux in this area, drop the digest suffix before we gain more users.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/microcode/amd.c | 2 +-
 xen/include/xen/sha2.h           | 4 ++--
 xen/lib/sha2-256.c               | 8 ++++----
 3 files changed, 7 insertions(+), 7 deletions(-)
xen/arch/x86/cpu/microcode/amd.c +1 -1
index e7ae1e802353..adabe6e6e838 100644
@@ -139,7 +139,7 @@ static bool check_digest(const struct container_microcode *mc)
         return false;
     }
 
-    sha2_256_digest(digest, patch, mc->len);
+    sha2_256(digest, patch, mc->len);
 
     if ( memcmp(digest, pd->digest, sizeof(digest)) )
     {
xen/include/xen/sha2.h +2 -2
index 09c69195a97d..7a99c1259d6e 100644
@@ -9,8 +9,8 @@
 
 #define SHA2_256_DIGEST_SIZE 32
 
-void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
-                     const void *msg, size_t len);
+void sha2_256(uint8_t digest[SHA2_256_DIGEST_SIZE],
+              const void *msg, size_t len);
 
 struct sha2_256_state {
     uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];
xen/lib/sha2-256.c +4 -4
index d1b2c20b9812..ed585ac0d4c1 100644
@@ -68,7 +68,7 @@ static const uint32_t K[] = {
     0x90befffaU, 0xa4506cebU, 0xbef9a3f7U, 0xc67178f2U,
 };
 
-static void sha2_256_transform(uint32_t *state, const void *_input)
+static void sha2_256_transform(uint32_t state[8], const void *_input)
 {
     const uint32_t *input = _input;
     uint32_t a, b, c, d, e, f, g, h, t1, t2;
@@ -197,8 +197,8 @@ void sha2_256_final(struct sha2_256_state *s, uint8_t digest[SHA2_256_DIGEST_SIZ
         put_unaligned_be32(s->state[i], &dst[i]);
 }
 
-void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
-                     const void *msg, size_t len)
+void sha2_256(uint8_t digest[SHA2_256_DIGEST_SIZE],
+              const void *msg, size_t len)
 {
     struct sha2_256_state s;
 
@@ -243,7 +243,7 @@ static void __init __constructor test_sha2_256(void)
         const struct test *t = &tests[i];
         uint8_t res[SHA2_256_DIGEST_SIZE] = {};
 
-        sha2_256_digest(res, t->msg, strlen(t->msg));
+        sha2_256(res, t->msg, strlen(t->msg));
 
         if ( memcmp(res, t->digest, sizeof(t->digest)) == 0 )
             continue;
Signature · 1 line
2.39.5
#2 lore raw

PATCH 2/2 xen/lib: Introduce SHA-1

In reply to Andrew Cooper

From: Krystian Hebel <[email protected]>

Future work will need to interact with the TPM, which requires calculating digests for all active hash banks. Introduce an implementation in lib/, partially derived from Trenchboot which itself is derived from Linux.

In order to be useful to other architectures, it is careful with endianness and misaligned accesses as well as being more MISRA friendly, but is only wired up for x86 in the short term.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>

* Split out of Trenchboot series.
* Add selftests, adjust types

---
 xen/include/xen/sha1.h |  14 +++
 xen/lib/Makefile       |   1 +
 xen/lib/sha1.c         | 215 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 230 insertions(+)
 create mode 100644 xen/include/xen/sha1.h
 create mode 100644 xen/lib/sha1.c
xen/include/xen/sha1.h new file +14 -0
new file mode 100644
index 000000000000..d649da8ebd97
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * SHA1: https://csrc.nist.gov/pubs/fips/180-4/upd1/final
+ */
+#ifndef XEN_SHA1_H
+#define XEN_SHA1_H
+
+#include <xen/types.h>
+
+#define SHA1_DIGEST_SIZE  20
+
+void sha1(uint8_t digest[SHA1_DIGEST_SIZE], const void *msg, size_t len);
+
+#endif /* XEN_SHA1_H */
xen/lib/Makefile +1 -0
index 5ccb1e5241c5..fd4b9ece63fb 100644
@@ -17,6 +17,7 @@ lib-y += memset.o
 lib-y += muldiv64.o
 lib-y += parse-size.o
 lib-y += rbtree.o
+lib-$(CONFIG_X86) += sha1.o
 lib-$(CONFIG_X86) += sha2-256.o
 lib-y += sort.o
 lib-y += strcasecmp.o
xen/lib/sha1.c new file +215 -0

215 lines not on this page · raw message

Signature · 1 line
2.39.5
#3 lore raw

Re: [PATCH 2/2] xen/lib: Introduce SHA-1

xen/lib/Makefile lib-y += memset.o · line 23

Quoted 10 lines · Andrew Cooper
xen/lib/Makefile +1 -0
@@ -17,6 +17,7 @@ lib-y += memset.o
 lib-y += muldiv64.o
 lib-y += parse-size.o
 lib-y += rbtree.o
+lib-$(CONFIG_X86) += sha1.o
 lib-$(CONFIG_X86) += sha2-256.o
 lib-y += sort.o
 lib-y += strcasecmp.o

Why exactly are we confining the two SHA<n> to x86? They're both plain C implementations, so ought to be fine to build everywhere. Being in $(lib-y) they also wouldn't make it into the final binary until a reference would appear.

Jan

#4 lore raw

Re: [PATCH 2/2] xen/lib: Introduce SHA-1

Quoted 15 lines · Jan Beulich

xen/lib/Makefile lib-y += memset.o · line 23

Quoted 10 lines · Andrew Cooper
xen/lib/Makefile +1 -0
@@ -17,6 +17,7 @@ lib-y += memset.o
 lib-y += muldiv64.o
 lib-y += parse-size.o
 lib-y += rbtree.o
+lib-$(CONFIG_X86) += sha1.o
 lib-$(CONFIG_X86) += sha2-256.o
 lib-y += sort.o
 lib-y += strcasecmp.o

Why exactly are we confining the two SHA<n> to x86? They're both plain C implementations, so ought to be fine to build everywhere. Being in $(lib-y) they also wouldn't make it into the final binary until a reference would appear.

For the SHA2 patch, an objection was made to compiling it on the other architectures.  Personally I think they ought to be plain lib-y.

I could always have patch 1 fix up to lib-y and have patch 2 match...

~Andrew

#5 lore raw

Re: [PATCH 2/2] xen/lib: Introduce SHA-1

Quoted 17 lines · Andrew Cooper
Quoted 15 lines · Jan Beulich

xen/lib/Makefile lib-y += memset.o · line 23

Quoted 10 lines · Andrew Cooper
xen/lib/Makefile +1 -0
@@ -17,6 +17,7 @@ lib-y += memset.o
 lib-y += muldiv64.o
 lib-y += parse-size.o
 lib-y += rbtree.o
+lib-$(CONFIG_X86) += sha1.o
 lib-$(CONFIG_X86) += sha2-256.o
 lib-y += sort.o
 lib-y += strcasecmp.o

Why exactly are we confining the two SHA<n> to x86? They're both plain C implementations, so ought to be fine to build everywhere. Being in $(lib-y) they also wouldn't make it into the final binary until a reference would appear.

Quoted 2 lines

For the SHA2 patch, an objection was made to compiling it on the other architectures.  Personally I think they ought to be plain lib-y.

Everyone (not knowing where the objection came from) - can we please re- consider this, ideally ...

Quoted 1 line

I could always have patch 1 fix up to lib-y and have patch 2 match...

... allowing this to be done?

Jan

#6 lore raw

Re: [PATCH 2/2] xen/lib: Introduce SHA-1

Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Jan Beulich

xen/lib/Makefile lib-y += memset.o · line 23

Quoted 10 lines · Andrew Cooper
xen/lib/Makefile +1 -0
@@ -17,6 +17,7 @@ lib-y += memset.o
 lib-y += muldiv64.o
 lib-y += parse-size.o
 lib-y += rbtree.o
+lib-$(CONFIG_X86) += sha1.o
 lib-$(CONFIG_X86) += sha2-256.o
 lib-y += sort.o
 lib-y += strcasecmp.o

Why exactly are we confining the two SHA<n> to x86? They're both plain C implementations, so ought to be fine to build everywhere. Being in $(lib-y) they also wouldn't make it into the final binary until a reference would appear.

Quoted 7 lines
Quoted 2 lines

For the SHA2 patch, an objection was made to compiling it on the other architectures.  Personally I think they ought to be plain lib-y.

Everyone (not knowing where the objection came from) - can we please re- consider this, ideally ...

Quoted 1 line

I could always have patch 1 fix up to lib-y and have patch 2 match...

... allowing this to be done?

FYI, we discussed this on the committers/maintainers call, and decided that lib/ does want to be generally enabled.

As such, I'll do as suggested.  Have patch 1 make sha2 non-x86-specific, and have patch 2 be general from its introduction.

~Andrew

Acked-by on the v1 series
#7 lore raw
In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper

Pulled out of the Trenchboot series.

Andrew Cooper (1):
xen/lib: Misc SHA2 cleanup

Krystian Hebel (1):
xen/lib: Introduce SHA-1

Quoted 9 lines
 xen/arch/x86/cpu/microcode/amd.c |   2 +-
 xen/include/xen/sha1.h           |  14 ++
 xen/include/xen/sha2.h           |   4 +-
 xen/lib/Makefile                 |   1 +
 xen/lib/sha1.c                   | 215 +++++++++++++++++++++++++++++++
 xen/lib/sha2-256.c               |   8 +-
 6 files changed, 237 insertions(+), 7 deletions(-)
 create mode 100644 xen/include/xen/sha1.h
 create mode 100644 xen/lib/sha1.c

I'll raise one question on patch 2, but that's not to make the ack above conditional in any way.

Jan