Skip to contentSkip to conversation

Add ARMv8 CE sha1/sha256 support

PATCH In review Loic Poulain 3 participants 23 messages Jun 1, 2022 – Feb 8, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Add ARMv8 CE sha1/sha256 support

This series adds support for the SHA-1 and SHA-256 Secure Hash Algorithm for CPUs that have support of the ARM v8 Crypto Extensions. It Improves speed of integrity & signature checking procedures.

V2:
- Add cover letter & sha256 support.
- Kconfig default 'y' only if SHA1 and SHA256 selected

Loic Poulain (5):
lib: sha1: Add support for hardware specific sha1_process
sha1: Fix digest state size/type
armv8 SHA-1 using ARMv8 Crypto Extensions:
lib: sha256: Add support for hardware specific sha256_process armv8 SHA-256 using ARMv8 Crypto Extensions

 arch/arm/cpu/armv8/Kconfig          |  15 ++++
 arch/arm/cpu/armv8/Makefile         |   2 +
 arch/arm/cpu/armv8/sha1_ce_core.S   | 132 +++++++++++++++++++++++++++++++++++
 arch/arm/cpu/armv8/sha1_ce_glue.c   |  21 ++++++
 arch/arm/cpu/armv8/sha256_ce_core.S | 134 ++++++++++++++++++++++++++++++++++++
 arch/arm/cpu/armv8/sha256_ce_glue.c |  21 ++++++
 include/u-boot/sha1.h               |   2 +-
 lib/sha1.c                          |  26 +++++--
 lib/sha256.c                        |  26 +++++--
 9 files changed, 364 insertions(+), 15 deletions(-)
 create mode 100644 arch/arm/cpu/armv8/sha1_ce_core.S
 create mode 100644 arch/arm/cpu/armv8/sha1_ce_glue.c
 create mode 100644 arch/arm/cpu/armv8/sha256_ce_core.S
 create mode 100644 arch/arm/cpu/armv8/sha256_ce_glue.c
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/5 lib: sha1: Add support for hardware specific sha1_process

Mark sha1_process as weak to allow hardware specific implementation. Add parameter to support for multiple blocks processing.

---
 lib/sha1.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
lib/sha1.c +19 -7
index 8154e1e..e5e42bc 100644
@@ -25,6 +25,8 @@
 #include <watchdog.h>
 #include <u-boot/sha1.h>
 
+#include <linux/compiler_attributes.h>
+
 const uint8_t sha1_der_prefix[SHA1_DER_LEN] = {
 	0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
 	0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
@@ -65,7 +67,7 @@ void sha1_starts (sha1_context * ctx)
 	ctx->state[4] = 0xC3D2E1F0;
 }
 
-static void sha1_process(sha1_context *ctx, const unsigned char data[64])
+static void __maybe_unused sha1_process_one(sha1_context *ctx, const unsigned char data[64])
 {
 	unsigned long temp, W[16], A, B, C, D, E;
 
@@ -219,6 +221,18 @@ static void sha1_process(sha1_context *ctx, const unsigned char data[64])
 	ctx->state[4] += E;
 }
 
+__weak void sha1_process(sha1_context *ctx, const unsigned char *data,
+			 unsigned int blocks)
+{
+	if (!blocks)
+		return;
+
+	while (blocks--) {
+		sha1_process_one(ctx, data);
+		data += 64;
+	}
+}
+
 /*
  * SHA-1 process buffer
  */
@@ -242,17 +256,15 @@ void sha1_update(sha1_context *ctx, const unsigned char *input,
 
 	if (left && ilen >= fill) {
 		memcpy ((void *) (ctx->buffer + left), (void *) input, fill);
-		sha1_process (ctx, ctx->buffer);
+		sha1_process(ctx, ctx->buffer, 1);
 		input += fill;
 		ilen -= fill;
 		left = 0;
 	}
 
-	while (ilen >= 64) {
-		sha1_process (ctx, input);
-		input += 64;
-		ilen -= 64;
-	}
+	sha1_process(ctx, input, ilen / 64);
+	input += ilen / 64 * 64;
+	ilen = ilen % 64;
 
 	if (ilen > 0) {
 		memcpy ((void *) (ctx->buffer + left), (void *) input, ilen);
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH v2 1/5] lib: sha1: Add support for hardware specific sha1_process

Quoted 4 lines · Loic Poulain

Mark sha1_process as weak to allow hardware specific implementation. Add parameter to support for multiple blocks processing.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#3 lore raw

PATCH v2 2/5 sha1: Fix digest state size/type

In reply to Loic Poulain

sha1 digest size is 5*32-bit => 160-bit. Using 64-bit unsigned long does not cause issue with the current sha1 implementation, but could be problematic for vectorized access.

---
 include/u-boot/sha1.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/u-boot/sha1.h +1 -1
index 283f103..09fee59 100644
@@ -30,7 +30,7 @@ extern const uint8_t sha1_der_prefix[];
 typedef struct
 {
     unsigned long total[2];	/*!< number of bytes processed	*/
-    unsigned long state[5];	/*!< intermediate digest state	*/
+    uint32_t state[5];		/*!< intermediate digest state	*/
     unsigned char buffer[64];	/*!< data block being processed */
 }
 sha1_context;
Signature · 1 line
2.7.4
#4 lore raw

Re: [PATCH v2 2/5] sha1: Fix digest state size/type

Quoted 5 lines · Loic Poulain

sha1 digest size is 5*32-bit => 160-bit. Using 64-bit unsigned long does not cause issue with the current sha1 implementation, but could be problematic for vectorized access.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#5 lore raw

PATCH v2 3/5 armv8 SHA-1 using ARMv8 Crypto Extensions:

In reply to Loic Poulain

This patch adds support for the SHA-1 Secure Hash Algorithm for CPUs that have support for the SHA-1 part of the ARM v8 Crypto Extensions.

It greatly improves sha-1 based operations, about 10x faster on iMX8M evk board. ~12ms vs ~165ms for a 20MiB kernel sha-1 verification.

asm implementation is a simplified version of the Linux version (from Ard Biesheuvel).

---
 arch/arm/cpu/armv8/Kconfig        |  11 ++++
 arch/arm/cpu/armv8/Makefile       |   1 +
 arch/arm/cpu/armv8/sha1_ce_core.S | 132 ++++++++++++++++++++++++++++++++++++++
 arch/arm/cpu/armv8/sha1_ce_glue.c |  21 ++++++
 4 files changed, 165 insertions(+)
 create mode 100644 arch/arm/cpu/armv8/sha1_ce_core.S
 create mode 100644 arch/arm/cpu/armv8/sha1_ce_glue.c
arch/arm/cpu/armv8/Kconfig +11 -0
index 9967376..0b11ca8 100644
@@ -171,4 +171,15 @@ config ARMV8_SECURE_BASE
 
 endif
 
+menuconfig ARMV8_CRYPTO
+	bool "ARM64 Accelerated Cryptographic Algorithms"
+
+if ARMV8_CRYPTO
+
+config ARMV8_CE_SHA1
+	bool "SHA-1 digest algorithm (ARMv8 Crypto Extensions)"
+	default y if SHA1
+
+endif
+
 endif
arch/arm/cpu/armv8/Makefile +1 -0
index 85fe047..ff2495c 100644
@@ -44,3 +44,4 @@ obj-$(CONFIG_TARGET_HIKEY) += hisilicon/
 obj-$(CONFIG_ARMV8_PSCI) += psci.o
 obj-$(CONFIG_TARGET_BCMNS3) += bcmns3/
 obj-$(CONFIG_XEN) += xen/
+obj-$(CONFIG_ARMV8_CE_SHA1) += sha1_ce_glue.o sha1_ce_core.o
arch/arm/cpu/armv8/sha1_ce_core.S new file +132 -0

132 lines not on this page · raw message

arch/arm/cpu/armv8/sha1_ce_glue.c new file +21 -0
new file mode 100644
index 0000000..780b119
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * sha1_ce_glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
+ *
+ * Copyright (C) 2022 Linaro Ltd <[email protected]>
+ */
+
+#include <common.h>
+#include <u-boot/sha1.h>
+
+extern void sha1_armv8_ce_process(uint32_t state[5], uint8_t const *src,
+				  uint32_t blocks);
+
+void sha1_process(sha1_context *ctx, const unsigned char *data,
+		  unsigned int blocks)
+{
+	if (!blocks)
+		return;
+
+	sha1_armv8_ce_process(ctx->state, data, blocks);
+}
Signature · 1 line
2.7.4
#6 lore raw

Re: [PATCH v2 3/5] armv8 SHA-1 using ARMv8 Crypto Extensions:

Quoted 10 lines · Loic Poulain

This patch adds support for the SHA-1 Secure Hash Algorithm for CPUs that have support for the SHA-1 part of the ARM v8 Crypto Extensions.

It greatly improves sha-1 based operations, about 10x faster on iMX8M evk board. ~12ms vs ~165ms for a 20MiB kernel sha-1 verification.

asm implementation is a simplified version of the Linux version (from Ard Biesheuvel).

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#7 lore raw

PATCH v2 4/5 lib: sha256: Add support for hardware specific sha256_process

In reply to Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
lib/sha256.c +19 -7
index c1fe93d..50b0b51 100644
@@ -14,6 +14,8 @@
 #include <watchdog.h>
 #include <u-boot/sha256.h>
 
+#include <linux/compiler_attributes.h>
+
 const uint8_t sha256_der_prefix[SHA256_DER_LEN] = {
 	0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
 	0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
@@ -55,7 +57,7 @@ void sha256_starts(sha256_context * ctx)
 	ctx->state[7] = 0x5BE0CD19;
 }
 
-static void sha256_process(sha256_context *ctx, const uint8_t data[64])
+static void sha256_process_one(sha256_context *ctx, const uint8_t data[64])
 {
 	uint32_t temp1, temp2;
 	uint32_t W[64];
@@ -186,6 +188,18 @@ static void sha256_process(sha256_context *ctx, const uint8_t data[64])
 	ctx->state[7] += H;
 }
 
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+			   unsigned int blocks)
+{
+	if (!blocks)
+		return;
+
+	while (blocks--) {
+		sha256_process_one(ctx, data);
+		data += 64;
+	}
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
 	uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
 	if (left && length >= fill) {
 		memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-		sha256_process(ctx, ctx->buffer);
+		sha256_process(ctx, ctx->buffer, 1);
 		length -= fill;
 		input += fill;
 		left = 0;
 	}
 
-	while (length >= 64) {
-		sha256_process(ctx, input);
-		length -= 64;
-		input += 64;
-	}
+	sha256_process(ctx, input, length / 64);
+	input += length / 64 * 64;
+	length = length % 64;
 
 	if (length)
 		memcpy((void *) (ctx->buffer + left), (void *) input, length);
Signature · 1 line
2.7.4
#8 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Quoted 4 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#9 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

In reply to Loic Poulain

Hi Loic,

Quoted 73 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
lib/sha256.c +19 -8
index c1fe93d..50b0b51 100644
@@ -14,6 +14,8 @@
 #include <watchdog.h>
 #include <u-boot/sha256.h>
 
+#include <linux/compiler_attributes.h>
+
 const uint8_t sha256_der_prefix[SHA256_DER_LEN] = {
        0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
        0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
@@ -55,7 +57,7 @@ void sha256_starts(sha256_context * ctx)
        ctx->state[7] = 0x5BE0CD19;
 }
 
-static void sha256_process(sha256_context *ctx, const uint8_t data[64])
+static void sha256_process_one(sha256_context *ctx, const uint8_t data[64])
 {
        uint32_t temp1, temp2;
        uint32_t W[64];
@@ -186,6 +188,18 @@ static void sha256_process(sha256_context *ctx, const uint8_t data[64])
        ctx->state[7] += H;
 }
 
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input, length);
--
Quoted 2 lines

2.7.4

I just came across this patch as it broke minnowmax.

This should be using driver model, not weak functions. Please can you take a look?

Regards,
Simon

#10 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Hi Simon,

Quoted 79 lines · Simon Glass

Hi Loic,

Quoted 23 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
lib/sha256.c +2 -0
index c1fe93d..50b0b51 100644
@@ -14,6 +14,8 @@
 #include <watchdog.h>
 #include <u-boot/sha256.h>
 
+#include <linux/compiler_attributes.h>
+
 const uint8_t sha256_der_prefix[SHA256_DER_LEN] = {
        0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
        0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,

lib/sha256.c void sha256_starts(sha256_context * ctx) · line 60

Quoted 5 lines
lib/sha256.c +1 -1
@@ -55,7 +57,7 @@ void sha256_starts(sha256_context * ctx)
        ctx->state[7] = 0x5BE0CD19;
 }
 
-static void sha256_process(sha256_context *ctx, const uint8_t data[64])
+static void sha256_process_one(sha256_context *ctx, const uint8_t

data[64])

lib/sha256.c static void sha256_process(sha256_context *ctx,

Quoted 4 lines
lib/sha256.c +0 -0
 {
        uint32_t temp1, temp2;
        uint32_t W[64];
@@ -186,6 +188,18 @@ static void sha256_process(sha256_context *ctx,

const uint8_t data[64])

lib/sha256.c static void sha256_process(sha256_context *ctx, const uint8_t data[64])

Quoted 4 lines
lib/sha256.c +1 -0
        ctx->state[7] += H;
 }
 
+__weak void sha256_process(sha256_context *ctx, const unsigned char

*data,

lib/sha256.c static void sha256_process(sha256_context *ctx, const uint8_t data[64])

Quoted 12 lines
lib/sha256.c +11 -0
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t

length)

lib/sha256.c void sha256_update(sha256_context *ctx, const

Quoted 3 lines
lib/sha256.c +0 -0
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const

uint8_t *input, uint32_t length)

lib/sha256.c void sha256_update(sha256_context *ctx, const · line 219

Quoted 3 lines
lib/sha256.c +0 -0
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input,

fill);

Quoted 16 lines
lib/sha256.c +4 -6
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
Quoted 8 lines

lib/sha256.c void sha256_update(sha256_context *ctx, const · line 231

Quoted 2 lines
lib/sha256.c +0 -0
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input,

length);

Quoted 3 lines
lib/sha256.c +0 -1
--

2.7.4

I just came across this patch as it broke minnowmax.

Ok, is it a build time or runtime break?

Quoted 3 lines

This should be using driver model, not weak functions. Please can you take a look?

Yes I can look at it in the next few days. I have used weak function because it’s an architecture feature offered by armv8 instructions, It’s not strictly speaking an internal device/IP.

Regards,
Loic

Quoted 1 line
#11 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Hi Loic,

Quoted 87 lines · Loic Poulain

Hi Simon,

Quoted 74 lines · Simon Glass

Hi Loic,

Quoted 70 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
lib/sha256.c +19 -7
index c1fe93d..50b0b51 100644
@@ -14,6 +14,8 @@
 #include <watchdog.h>
 #include <u-boot/sha256.h>
 
+#include <linux/compiler_attributes.h>
+
 const uint8_t sha256_der_prefix[SHA256_DER_LEN] = {
        0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
        0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
@@ -55,7 +57,7 @@ void sha256_starts(sha256_context * ctx)
        ctx->state[7] = 0x5BE0CD19;
 }
 
-static void sha256_process(sha256_context *ctx, const uint8_t data[64])
+static void sha256_process_one(sha256_context *ctx, const uint8_t data[64])
 {
        uint32_t temp1, temp2;
        uint32_t W[64];
@@ -186,6 +188,18 @@ static void sha256_process(sha256_context *ctx, const uint8_t data[64])
        ctx->state[7] += H;
 }
 
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
Quoted 7 lines
Quoted 5 lines
lib/sha256.c +0 -1
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input, length);
--

2.7.4

I just came across this patch as it broke minnowmax.

Quoted 1 line

Ok, is it a build time or runtime break?

Build, but you need the binary blobs to see it :-(

Quoted 8 lines
Quoted 5 lines

This should be using driver model, not weak functions. Please can you take a look?

Yes I can look at it in the next few days. I have used weak function because it’s an architecture feature offered by armv8 instructions, It’s not strictly speaking an internal device/IP.

Thanks.

Right, same as hardware-accelerated hashing hardware in my book.

See hash.c which has become a mess. We have been trying to make do with a list of algos, but given all the updates I think needs a new UCLASS_HASH with the same operations as in hash.h

Regards,
Simon

#12 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Hi Simon,

Quoted 21 lines · Simon Glass

Hi Loic,

Quoted 17 lines · Loic Poulain

Hi Simon,

Quoted 13 lines · Simon Glass

Hi Loic,

Quoted 9 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

[...]

Quoted 44 lines
Quoted 44 lines
Quoted 42 lines
Quoted 38 lines
lib/sha256.c +16 -7
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input, length);
--
Quoted 2 lines

2.7.4

I just came across this patch as it broke minnowmax.

Quoted 6 lines
Quoted 1 line

Ok, is it a build time or runtime break?

Build, but you need the binary blobs to see it :-(

Quoted 3 lines
Quoted 3 lines

This should be using driver model, not weak functions. Please can you take a look?

Just tested the minnowmax build (b69026c91f2e; minnowmax_defconfig; gcc-11.3.0), and I've not observed any issue (but I had to fake some of the binary blobs...). Could you share the build problem/error you encountered? As you mentioned it, Is the error specifically related to _weak function linking? Would like to have a simple and quick fix before trying to move on to a more proper DM based solution.

Thanks,
Loic

Quoted 14 lines
Quoted 3 lines

Yes I can look at it in the next few days. I have used weak function because it’s an architecture feature offered by armv8 instructions, It’s not strictly speaking an internal device/IP.

Thanks.

Right, same as hardware-accelerated hashing hardware in my book.

See hash.c which has become a mess. We have been trying to make do with a list of algos, but given all the updates I think needs a new UCLASS_HASH with the same operations as in hash.h

Regards,
Simon

#13 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Hi Loic,

Quoted 72 lines · Loic Poulain

Hi Simon,

Quoted 21 lines · Simon Glass

Hi Loic,

Quoted 17 lines · Loic Poulain

Hi Simon,

Quoted 13 lines · Simon Glass

Hi Loic,

Quoted 9 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

[...]

Quoted 46 lines
Quoted 45 lines
Quoted 35 lines
Quoted 35 lines
lib/sha256.c +16 -6
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
Quoted 7 lines
Quoted 5 lines
lib/sha256.c +0 -1
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input, length);
--

2.7.4

I just came across this patch as it broke minnowmax.

Ok, is it a build time or runtime break?

Quoted 8 lines
Quoted 4 lines

Build, but you need the binary blobs to see it :-(

Quoted 3 lines
Quoted 3 lines

This should be using driver model, not weak functions. Please can you take a look?

Just tested the minnowmax build (b69026c91f2e; minnowmax_defconfig; gcc-11.3.0), and I've not observed any issue (but I had to fake some of the binary blobs...). Could you share the build problem/error you

Unfortunately you need the blobs!

Quoted 3 lines

encountered? As you mentioned it, Is the error specifically related to _weak function linking? Would like to have a simple and quick fix before trying to move on to a more proper DM based solution.

It is just because of the code size increase, I believe. I am planning to dig into it a bit as Bin Meng asked for more info as to why I sent a revert for his patch moving U-Boot.

Regards,
Simon

Quoted 19 lines

Thanks,
Loic

Quoted 14 lines
Quoted 3 lines

Yes I can look at it in the next few days. I have used weak function because it’s an architecture feature offered by armv8 instructions, It’s not strictly speaking an internal device/IP.

Thanks.

Right, same as hardware-accelerated hashing hardware in my book.

See hash.c which has become a mess. We have been trying to make do with a list of algos, but given all the updates I think needs a new UCLASS_HASH with the same operations as in hash.h

Regards,
Simon

#14 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Quoted 86 lines · Simon Glass

Hi Loic,

Quoted 72 lines · Loic Poulain

Hi Simon,

Quoted 21 lines · Simon Glass

Hi Loic,

Quoted 17 lines · Loic Poulain

Hi Simon,

Quoted 13 lines · Simon Glass

Hi Loic,

Quoted 9 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

[...]

Quoted 46 lines
Quoted 45 lines
Quoted 35 lines
Quoted 35 lines
lib/sha256.c +16 -6
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
Quoted 7 lines
Quoted 5 lines
lib/sha256.c +0 -1
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input, length);
--

2.7.4

I just came across this patch as it broke minnowmax.

Ok, is it a build time or runtime break?

Quoted 8 lines
Quoted 4 lines

Build, but you need the binary blobs to see it :-(

Quoted 3 lines
Quoted 3 lines

This should be using driver model, not weak functions. Please can you take a look?

Just tested the minnowmax build (b69026c91f2e; minnowmax_defconfig; gcc-11.3.0), and I've not observed any issue (but I had to fake some of the binary blobs...). Could you share the build problem/error you

Unfortunately you need the blobs!

Quoted 7 lines
Quoted 3 lines

encountered? As you mentioned it, Is the error specifically related to _weak function linking? Would like to have a simple and quick fix before trying to move on to a more proper DM based solution.

It is just because of the code size increase, I believe. I am planning to dig into it a bit as Bin Meng asked for more info as to why I sent a revert for his patch moving U-Boot.

That honestly makes more sense, having stared at the commit in question. Perhaps Minnow needs LTO enabled.

Signature · 1 line
Tom
#15 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Hi Tom,

Quoted 92 lines · Tom Rini
Quoted 90 lines · Simon Glass

Hi Loic,

Quoted 72 lines · Loic Poulain

Hi Simon,

Quoted 21 lines · Simon Glass

Hi Loic,

Quoted 17 lines · Loic Poulain

Hi Simon,

Quoted 13 lines · Simon Glass

Hi Loic,

Quoted 9 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

[...]

Quoted 46 lines
Quoted 45 lines
Quoted 35 lines
Quoted 35 lines
(quoted diff) +16 -6
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
Quoted 7 lines
Quoted 5 lines
(quoted diff) +0 -1
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input, length);
--

2.7.4

I just came across this patch as it broke minnowmax.

Ok, is it a build time or runtime break?

Quoted 8 lines
Quoted 4 lines

Build, but you need the binary blobs to see it :-(

Quoted 3 lines
Quoted 3 lines

This should be using driver model, not weak functions. Please can you take a look?

Just tested the minnowmax build (b69026c91f2e; minnowmax_defconfig; gcc-11.3.0), and I've not observed any issue (but I had to fake some of the binary blobs...). Could you share the build problem/error you

Unfortunately you need the blobs!

Quoted 3 lines

encountered? As you mentioned it, Is the error specifically related to _weak function linking? Would like to have a simple and quick fix before trying to move on to a more proper DM based solution.

Quoted 6 lines
Quoted 3 lines

It is just because of the code size increase, I believe. I am planning to dig into it a bit as Bin Meng asked for more info as to why I sent a revert for his patch moving U-Boot.

That honestly makes more sense, having stared at the commit in question. Perhaps Minnow needs LTO enabled.

Yes, that would likely help. But Bin's point is that it should be possible to move the text base.

Anyway, the point of this thread is that this needs to be done as drivers rather than weak functions. Unfortunately hash.c has grown a few appendages...this is yet another.

Regards,
Simon

#16 lore raw

Re: [PATCH v2 4/5] lib: sha256: Add support for hardware specific sha256_process

Quoted 99 lines · Simon Glass

Hi Tom,

Quoted 96 lines · Tom Rini
Quoted 93 lines · Simon Glass

Hi Loic,

Quoted 72 lines · Loic Poulain

Hi Simon,

Quoted 21 lines · Simon Glass

Hi Loic,

Quoted 17 lines · Loic Poulain

Hi Simon,

Quoted 13 lines · Simon Glass

Hi Loic,

Quoted 9 lines · Loic Poulain

Mark sha256_process as weak to allow hardware specific implementation. Add parameter for supporting multiple blocks processing.

---
 lib/sha256.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

[...]

Quoted 46 lines
Quoted 45 lines
Quoted 35 lines
Quoted 35 lines
(quoted diff) +16 -6
+__weak void sha256_process(sha256_context *ctx, const unsigned char *data,
+                          unsigned int blocks)
+{
+       if (!blocks)
+               return;
+
+       while (blocks--) {
+               sha256_process_one(ctx, data);
+               data += 64;
+       }
+}
+
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 {
        uint32_t left, fill;
@@ -204,17 +218,15 @@ void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
 
        if (left && length >= fill) {
                memcpy((void *) (ctx->buffer + left), (void *) input, fill);
-               sha256_process(ctx, ctx->buffer);
+               sha256_process(ctx, ctx->buffer, 1);
                length -= fill;
                input += fill;
                left = 0;
        }
 
-       while (length >= 64) {
-               sha256_process(ctx, input);
-               length -= 64;
-               input += 64;
-       }
+       sha256_process(ctx, input, length / 64);
+       input += length / 64 * 64;
+       length = length % 64;
 
Quoted 7 lines
Quoted 5 lines
(quoted diff) +0 -1
        if (length)
                memcpy((void *) (ctx->buffer + left), (void *) input, length);
--

2.7.4

I just came across this patch as it broke minnowmax.

Ok, is it a build time or runtime break?

Quoted 8 lines
Quoted 4 lines

Build, but you need the binary blobs to see it :-(

Quoted 3 lines
Quoted 3 lines

This should be using driver model, not weak functions. Please can you take a look?

Just tested the minnowmax build (b69026c91f2e; minnowmax_defconfig; gcc-11.3.0), and I've not observed any issue (but I had to fake some of the binary blobs...). Could you share the build problem/error you

Unfortunately you need the blobs!

Quoted 3 lines

encountered? As you mentioned it, Is the error specifically related to _weak function linking? Would like to have a simple and quick fix before trying to move on to a more proper DM based solution.

It is just because of the code size increase, I believe. I am planning to dig into it a bit as Bin Meng asked for more info as to why I sent a revert for his patch moving U-Boot.

Quoted 5 lines
Quoted 2 lines

That honestly makes more sense, having stared at the commit in question. Perhaps Minnow needs LTO enabled.

Yes, that would likely help. But Bin's point is that it should be possible to move the text base.

I suspect that might just be more fall-out of the size problem and possibly some hard coded assumptions in the blobs?

Quoted 3 lines

Anyway, the point of this thread is that this needs to be done as drivers rather than weak functions. Unfortunately hash.c has grown a few appendages...this is yet another.

I don't know, it seems fine, especially since we aren't really talking about a "hash driver" but rather introducing some performance sensitive code.

Signature · 1 line
Tom
#17 lore raw

PATCH v2 5/5 armv8 SHA-256 using ARMv8 Crypto Extensions

In reply to Loic Poulain

This patch adds support for the SHA-256 Secure Hash Algorithm for CPUs that have support for the SHA-256 part of the ARM v8 Crypto Extensions.

It greatly improves sha-256 based operations, about 17x faster on iMX8M evk board. ~12ms vs ~208ms for a 20MiB kernel sha-256 verification.

asm implementation is a simplified version of the Linux version (from Ard Biesheuvel).

---
 arch/arm/cpu/armv8/Kconfig          |   4 ++
 arch/arm/cpu/armv8/Makefile         |   1 +
 arch/arm/cpu/armv8/sha256_ce_core.S | 134 ++++++++++++++++++++++++++++++++++++
 arch/arm/cpu/armv8/sha256_ce_glue.c |  21 ++++++
 4 files changed, 160 insertions(+)
 create mode 100644 arch/arm/cpu/armv8/sha256_ce_core.S
 create mode 100644 arch/arm/cpu/armv8/sha256_ce_glue.c
arch/arm/cpu/armv8/Kconfig +4 -0
index 0b11ca8..0494a08 100644
@@ -180,6 +180,10 @@ config ARMV8_CE_SHA1
 	bool "SHA-1 digest algorithm (ARMv8 Crypto Extensions)"
 	default y if SHA1
 
+config ARMV8_CE_SHA256
+	bool "SHA-256 digest algorithm (ARMv8 Crypto Extensions)"
+	default y if SHA256
+
 endif
 
 endif
arch/arm/cpu/armv8/Makefile +1 -0
index ff2495c..2e4bf9e 100644
@@ -45,3 +45,4 @@ obj-$(CONFIG_ARMV8_PSCI) += psci.o
 obj-$(CONFIG_TARGET_BCMNS3) += bcmns3/
 obj-$(CONFIG_XEN) += xen/
 obj-$(CONFIG_ARMV8_CE_SHA1) += sha1_ce_glue.o sha1_ce_core.o
+obj-$(CONFIG_ARMV8_CE_SHA256) += sha256_ce_glue.o sha256_ce_core.o
arch/arm/cpu/armv8/sha256_ce_core.S new file +134 -0

134 lines not on this page · raw message

arch/arm/cpu/armv8/sha256_ce_glue.c new file +21 -0
new file mode 100644
index 0000000..67dd796
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * sha256_ce_glue.c - SHA-256 secure hash using ARMv8 Crypto Extensions
+ *
+ * Copyright (C) 2022 Linaro Ltd <[email protected]>
+ */
+
+#include <common.h>
+#include <u-boot/sha256.h>
+
+extern void sha256_armv8_ce_process(uint32_t state[8], uint8_t const *src,
+				    uint32_t blocks);
+
+void sha256_process(sha256_context *ctx, const unsigned char *data,
+		    unsigned int blocks)
+{
+	if (!blocks)
+		return;
+
+	sha256_armv8_ce_process(ctx->state, data, blocks);
+}
Signature · 1 line
2.7.4
#18 lore raw

PATCH 1/1 qemu_arm64: Enable CONFIG_ARMV8_CRYPTO support

Now that we can make use of CPU features for sha1/sha256, enable in QEMU so that we get some test coverage.

Cc Loic Poulain <[email protected]>
Cc Tuomas Tynkkynen <[email protected]>
---
 configs/qemu_arm64_defconfig | 1 +
 1 file changed, 1 insertion(+)
configs/qemu_arm64_defconfig +1 -0
index 87acf00f30e3..f7c93ba2af54 100644
@@ -8,6 +8,7 @@ CONFIG_ENV_SECT_SIZE=0x40000
 CONFIG_DEFAULT_DEVICE_TREE="qemu-arm64"
 CONFIG_DEBUG_UART_BASE=0x9000000
 CONFIG_DEBUG_UART_CLOCK=0
+CONFIG_ARMV8_CRYPTO=y
 CONFIG_SYS_LOAD_ADDR=0x40200000
 CONFIG_ENV_ADDR=0x4000000
 CONFIG_DEBUG_UART=y
Signature · 1 line
2.25.1
#20 lore raw

Re: [PATCH v2 5/5] armv8 SHA-256 using ARMv8 Crypto Extensions

In reply to Loic Poulain
Quoted 10 lines · Loic Poulain

This patch adds support for the SHA-256 Secure Hash Algorithm for CPUs that have support for the SHA-256 part of the ARM v8 Crypto Extensions.

It greatly improves sha-256 based operations, about 17x faster on iMX8M evk board. ~12ms vs ~208ms for a 20MiB kernel sha-256 verification.

asm implementation is a simplified version of the Linux version (from Ard Biesheuvel).

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#21 lore raw
In reply to Loic Poulain

Hi Folks,

Any comments on this series? Anyone else to CC?

Thanks,
Loic

Quoted 34 lines · Loic Poulain

This series adds support for the SHA-1 and SHA-256 Secure Hash Algorithm for CPUs that have support of the ARM v8 Crypto Extensions. It Improves speed of integrity & signature checking procedures.

V2:
- Add cover letter & sha256 support.
- Kconfig default 'y' only if SHA1 and SHA256 selected

Loic Poulain (5):
lib: sha1: Add support for hardware specific sha1_process
sha1: Fix digest state size/type
armv8 SHA-1 using ARMv8 Crypto Extensions:
lib: sha256: Add support for hardware specific sha256_process armv8 SHA-256 using ARMv8 Crypto Extensions

 arch/arm/cpu/armv8/Kconfig          |  15 ++++
 arch/arm/cpu/armv8/Makefile         |   2 +
 arch/arm/cpu/armv8/sha1_ce_core.S   | 132 +++++++++++++++++++++++++++++++++++
 arch/arm/cpu/armv8/sha1_ce_glue.c   |  21 ++++++
 arch/arm/cpu/armv8/sha256_ce_core.S | 134 ++++++++++++++++++++++++++++++++++++
 arch/arm/cpu/armv8/sha256_ce_glue.c |  21 ++++++
 include/u-boot/sha1.h               |   2 +-
 lib/sha1.c                          |  26 +++++--
 lib/sha256.c                        |  26 +++++--
 9 files changed, 364 insertions(+), 15 deletions(-)
 create mode 100644 arch/arm/cpu/armv8/sha1_ce_core.S
 create mode 100644 arch/arm/cpu/armv8/sha1_ce_glue.c
 create mode 100644 arch/arm/cpu/armv8/sha256_ce_core.S
 create mode 100644 arch/arm/cpu/armv8/sha256_ce_glue.c

--
2.7.4

#22 lore raw
Quoted 3 lines · Loic Poulain

Hi Folks,

Any comments on this series? Anyone else to CC?

I will likely pick this up for next soon, thanks.

Signature · 1 line
Tom