public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Stefan Berger <stefanb@linux.ibm.com>
To: keyrings@vger.kernel.org, linux-crypto@vger.kernel.org,
	herbert@gondor.apana.org.au, davem@davemloft.net
Cc: linux-kernel@vger.kernel.org, saulo.alessandre@tse.jus.br,
	ardb@kernel.org, Stefan Berger <stefanb@linux.ibm.com>
Subject: [RFC PATCH 1/3] crypto: ecc - Implement ecc_digits_to_bytes to convert digits to byte array
Date: Wed, 12 Jun 2024 11:18:58 -0400	[thread overview]
Message-ID: <20240612151900.895156-2-stefanb@linux.ibm.com> (raw)
In-Reply-To: <20240612151900.895156-1-stefanb@linux.ibm.com>

Implement ecc_digits_to_bytes to convert an array of digits into an
nbytes-sized byte array. The first byte in the byte array holds the most
significant bits of the large integer.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 crypto/ecc.c                  | 22 ++++++++++++++++++++++
 include/crypto/internal/ecc.h | 13 +++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/crypto/ecc.c b/crypto/ecc.c
index af698f8852fb..1cdb5df3aa5d 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -90,6 +90,28 @@ void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
 }
 EXPORT_SYMBOL(ecc_digits_from_bytes);
 
+void ecc_digits_to_bytes(const u64 *in, unsigned int ndigits,
+			 u8 *out, unsigned int nbytes)
+{
+	unsigned int o = nbytes & 7;
+	__be64 msd;
+	int i;
+
+	if (o) {
+		msd = cpu_to_be64(in[--ndigits]);
+		memcpy(out, (u8 *)&msd + sizeof(msd) - o, o);
+		out += o;
+		nbytes -= o;
+	}
+
+	for (i = ndigits - 1; i >= 0 && nbytes > 0; i--) {
+		put_unaligned_be64(in[i], out);
+		out += sizeof(u64);
+		nbytes -= sizeof(u64);
+	}
+}
+EXPORT_SYMBOL(ecc_digits_to_bytes);
+
 static u64 *ecc_alloc_digits_space(unsigned int ndigits)
 {
 	size_t len = ndigits * sizeof(u64);
diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h
index 0717a53ae732..b18297aaff08 100644
--- a/include/crypto/internal/ecc.h
+++ b/include/crypto/internal/ecc.h
@@ -70,6 +70,19 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit
 void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
 			   u64 *out, unsigned int ndigits);
 
+/**
+ * ecc_digits_to_bytes() - Copy digits into a byte array of size nbytes
+ * @in:      Input digits array
+ * @ndigits: Number of digits in input digits array
+ * @out:     Output byte array
+ * @nbytes:  Number of bytes to copy into byte array
+ *
+ * The first byte in the byte array will have the most significant bits of the
+ * large integer.
+ */
+void ecc_digits_to_bytes(const u64 *in, unsigned int ndigits,
+			 u8 *out, unsigned int nbytes);
+
 /**
  * ecc_is_key_valid() - Validate a given ECDH private key
  *
-- 
2.43.0


  reply	other threads:[~2024-06-12 15:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-12 15:18 [RFC PATCH 0/3] Introduce ecc_digits_to_bytes and clean up ecdh.c Stefan Berger
2024-06-12 15:18 ` Stefan Berger [this message]
2024-06-12 15:18 ` [RFC PATCH 2/3] crypto: ecc - Implement and use ecc_curve_get_nbytes to get curve's nbytes Stefan Berger
2024-06-12 15:19 ` [RFC PATCH 3/3] crypto: ecdh - Use functions to copy digits from and to byte arrays Stefan Berger

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=20240612151900.895156-2-stefanb@linux.ibm.com \
    --to=stefanb@linux.ibm.com \
    --cc=ardb@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=saulo.alessandre@tse.jus.br \
    /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