summaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorStefan Berger <stefanb@linux.ibm.com>2024-05-09 21:59:21 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2025-01-09 13:31:52 +0100
commit55779f26eab9af12474a447001bd17070f055712 (patch)
treead174e7345651317e67563f518cb0f5fb00cda19 /crypto
parent9457d783fb94527c215e32a97225aed7c88d979f (diff)
downloadlinux-55779f26eab9af12474a447001bd17070f055712.tar.gz
linux-55779f26eab9af12474a447001bd17070f055712.tar.bz2
linux-55779f26eab9af12474a447001bd17070f055712.zip
crypto: ecc - Prevent ecc_digits_from_bytes from reading too many bytes
[ Upstream commit c6ab5c915da460c0397960af3c308386c3f3247b ] Prevent ecc_digits_from_bytes from reading too many bytes from the input byte array in case an insufficient number of bytes is provided to fill the output digit array of ndigits. Therefore, initialize the most significant digits with 0 to avoid trying to read too many bytes later on. Convert the function into a regular function since it is getting too big for an inline function. If too many bytes are provided on the input byte array the extra bytes are ignored since the input variable 'ndigits' limits the number of digits that will be filled. Fixes: d67c96fb97b5 ("crypto: ecdsa - Convert byte arrays with key coordinates to digits") Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Signed-off-by: Stefan Berger <stefanb@linux.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/ecc.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/crypto/ecc.c b/crypto/ecc.c
index f53fb4d6af99..21504280aca2 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -66,6 +66,28 @@ const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
}
EXPORT_SYMBOL(ecc_get_curve);
+void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
+ u64 *out, unsigned int ndigits)
+{
+ int diff = ndigits - DIV_ROUND_UP(nbytes, sizeof(u64));
+ unsigned int o = nbytes & 7;
+ __be64 msd = 0;
+
+ /* diff > 0: not enough input bytes: set most significant digits to 0 */
+ if (diff > 0) {
+ ndigits -= diff;
+ memset(&out[ndigits - 1], 0, diff * sizeof(u64));
+ }
+
+ if (o) {
+ memcpy((u8 *)&msd + sizeof(msd) - o, in, o);
+ out[--ndigits] = be64_to_cpu(msd);
+ in += o;
+ }
+ ecc_swap_digits(in, out, ndigits);
+}
+EXPORT_SYMBOL(ecc_digits_from_bytes);
+
static u64 *ecc_alloc_digits_space(unsigned int ndigits)
{
size_t len = ndigits * sizeof(u64);