From: Philippe Reynes <philippe.reynes@softathome.com>
To: marko.makela@iki.fi, jonny.green@keytechinc.com, raymondmaoca@gmail.com
Cc: u-boot@lists.denx.de, Philippe Reynes <philippe.reynes@softathome.com>
Subject: [RFC PATCH 2/4] ecdsa: initial support of ecdsa using mbedtls
Date: Mon, 2 Feb 2026 18:03:05 +0100 [thread overview]
Message-ID: <20260202170307.217200-3-philippe.reynes@softathome.com> (raw)
In-Reply-To: <20260202170307.217200-1-philippe.reynes@softathome.com>
Adds an initial support of ecdsa verify using mbedtls.
Signed-off-by: Philippe Reynes <philippe.reynes@softathome.com>
---
include/crypto/internal/sw_ecdsa.h | 14 +++++
lib/mbedtls/Makefile | 3 +
lib/mbedtls/sw_ecdsa.c | 94 ++++++++++++++++++++++++++++++
3 files changed, 111 insertions(+)
create mode 100644 include/crypto/internal/sw_ecdsa.h
create mode 100644 lib/mbedtls/sw_ecdsa.c
diff --git a/include/crypto/internal/sw_ecdsa.h b/include/crypto/internal/sw_ecdsa.h
new file mode 100644
index 00000000000..b1ca31da0f8
--- /dev/null
+++ b/include/crypto/internal/sw_ecdsa.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2026, Philippe Reynes <philippe.reynes@softathome.com>
+ */
+#ifndef _SW_ECDSA
+#define _SW_ECDSA
+
+struct ecdsa_public_key;
+
+int sw_ecdsa_verify(const struct ecdsa_public_key *pubkey,
+ const void *hash, size_t hash_len,
+ const void *signature, size_t sig_len);
+
+#endif
diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
index 54a893609cf..a5331313a60 100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -11,6 +11,9 @@ obj-$(CONFIG_$(PHASE_)SHA1_MBEDTLS) += sha1.o
obj-$(CONFIG_$(PHASE_)SHA256_MBEDTLS) += sha256.o
obj-$(CONFIG_$(PHASE_)SHA512_MBEDTLS) += sha512.o
+# shim layer for crypto
+obj-$(CONFIG_$(PHASE_)ECDSA_MBEDTLS) += sw_ecdsa.o
+
# x509 libraries
obj-$(CONFIG_$(PHASE_)ASYMMETRIC_PUBLIC_KEY_MBEDTLS) += \
public_key.o
diff --git a/lib/mbedtls/sw_ecdsa.c b/lib/mbedtls/sw_ecdsa.c
new file mode 100644
index 00000000000..0ed95f4407f
--- /dev/null
+++ b/lib/mbedtls/sw_ecdsa.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2026 Philippe Reynes <philippe.reynes@softathome.com>
+ */
+
+#include <crypto/ecdsa-uclass.h>
+#include "mbedtls_options.h" /* required to access private fields */
+#include <mbedtls/ecdsa.h>
+#include <mbedtls/ecp.h>
+
+static mbedtls_ecp_group_id sw_ecdsa_search_group_id(const char *curve_name)
+{
+ mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
+ const mbedtls_ecp_curve_info *info;
+
+ if (!curve_name)
+ goto out;
+
+ if (!strcmp(curve_name, "prime256v1"))
+ grp_id = MBEDTLS_ECP_DP_SECP256R1;
+
+ info = mbedtls_ecp_curve_list();
+ while (info && info->name) {
+ if (!strcmp(curve_name, info->name))
+ grp_id = info->grp_id;
+ info++;
+ }
+
+ out:
+ return grp_id;
+}
+
+int sw_ecdsa_verify(const struct ecdsa_public_key *pubkey,
+ const void *hash, size_t hash_len,
+ const void *signature, size_t sig_len)
+{
+ mbedtls_ecp_group_id grp_id;
+ mbedtls_ecp_group grp;
+ const unsigned char *buf = hash;
+ size_t blen = hash_len;
+ mbedtls_ecp_point Q;
+ mbedtls_mpi r, s;
+ int key_len;
+ int err = -1;
+
+ if (!(pubkey->size_bits % 8))
+ key_len = pubkey->size_bits / 8;
+ else
+ key_len = pubkey->size_bits / 8 + 1;
+
+ /* search the group */
+ grp_id = sw_ecdsa_search_group_id(pubkey->curve_name);
+ if (grp_id == MBEDTLS_ECP_DP_NONE) {
+ printf("%s: curve name %s not found\n",
+ __func__, pubkey->curve_name);
+ goto out;
+ }
+
+ /* init and load the group */
+ mbedtls_ecp_group_init(&grp);
+ err = mbedtls_ecp_group_load(&grp, grp_id);
+ if (err < 0)
+ goto out;
+
+ /* prepare the pubkey */
+ mbedtls_ecp_point_init(&Q);
+ mbedtls_mpi_init(&Q.X);
+ mbedtls_mpi_init(&Q.Y);
+ mbedtls_mpi_init(&Q.Z);
+ mbedtls_mpi_read_binary(&Q.X, pubkey->x, key_len);
+ mbedtls_mpi_read_binary(&Q.Y, pubkey->y, key_len);
+ mbedtls_mpi_lset(&Q.Z, 1);
+
+ /* check if the pubkey is valid */
+ err = mbedtls_ecp_check_pubkey(&grp, &Q);
+ if (err < 0) {
+ printf("%s: public key is invalid (err = %d)\n", __func__, err);
+ goto out;
+ }
+
+ /* compute r */
+ mbedtls_mpi_init(&r);
+ mbedtls_mpi_read_binary(&r, signature, key_len);
+
+ /* compute s */
+ mbedtls_mpi_init(&s);
+ mbedtls_mpi_read_binary(&s, signature + key_len, key_len);
+
+ /* check the signature */
+ err = mbedtls_ecdsa_verify(&grp, buf, blen, &Q, &r, &s);
+
+ out:
+ return err;
+}
--
2.43.0
next prev parent reply other threads:[~2026-02-02 17:05 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-02 17:03 [RFC PATCH 0/4] add software ecdsa support Philippe Reynes
2026-02-02 17:03 ` [RFC PATCH 1/4] mbedtls: enable support of ecc Philippe Reynes
2026-02-02 19:03 ` Raymond Mao
2026-02-02 17:03 ` Philippe Reynes [this message]
2026-02-02 17:03 ` [RFC PATCH 3/4] test: lib: sw_ecdsa: add initial test Philippe Reynes
2026-02-02 17:03 ` [RFC PATCH 4/4] drivers: crypto: add software ecdsa support Philippe Reynes
2026-02-02 19:09 ` [RFC PATCH 0/4] " Raymond Mao
2026-02-02 19:44 ` Tom Rini
2026-02-04 19:02 ` Marko Mäkelä
2026-02-04 19:28 ` Raymond Mao
2026-02-05 18:16 ` Marko Mäkelä
2026-02-05 18:47 ` Raymond Mao
2026-02-08 18:37 ` Marko Mäkelä
2026-02-09 16:04 ` Marko Mäkelä
2026-02-14 19:38 ` Marko Mäkelä
2026-02-15 18:31 ` Marko Mäkelä
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260202170307.217200-3-philippe.reynes@softathome.com \
--to=philippe.reynes@softathome.com \
--cc=jonny.green@keytechinc.com \
--cc=marko.makela@iki.fi \
--cc=raymondmaoca@gmail.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox