diff mbox series

[v3,06/10] crypte: ecc - Implement ecc_curve_get_nbits to get number of bits

Message ID 20240223204149.4055630-7-stefanb@linux.ibm.com
State New
Headers show
Series Add support for NIST P521 to ecdsa | expand

Commit Message

Stefan Berger Feb. 23, 2024, 8:41 p.m. UTC
If curve->nbits is set then return this number, otherwise use
the curve->ndigits to calculate the number of bits.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 include/crypto/internal/ecc.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Lukas Wunner Feb. 27, 2024, 8:15 p.m. UTC | #1
On Fri, Feb 23, 2024 at 03:41:45PM -0500, Stefan Berger wrote:
> --- a/include/crypto/internal/ecc.h
> +++ b/include/crypto/internal/ecc.h
> @@ -75,6 +75,17 @@ static inline void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
>  	ecc_swap_digits(tmp, out, ndigits);
>  }
>  
> +/**
> + * ecc_curve_get_nbits() - Get the number of bits of the curve
> + * @curve:    The curve
> + */
> +static inline unsigned int ecc_curve_get_nbits(const struct ecc_curve *curve)
> +{
> +	if (curve->nbits)
> +		return curve->nbits;
> +	return curve->g.ndigits << ECC_DIGITS_TO_BYTES_SHIFT * 8;
> +}

Since you're amending struct ecc_curve with an extra nbits value anyway,
why not statically fill it in for all curves, instead of adding this
extra complexity in the code?

Thanks,

Lukas
Stefan Berger Feb. 29, 2024, 8:29 p.m. UTC | #2
On 2/27/24 15:15, Lukas Wunner wrote:
> On Fri, Feb 23, 2024 at 03:41:45PM -0500, Stefan Berger wrote:
>> --- a/include/crypto/internal/ecc.h
>> +++ b/include/crypto/internal/ecc.h
>> @@ -75,6 +75,17 @@ static inline void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
>>   	ecc_swap_digits(tmp, out, ndigits);
>>   }
>>   
>> +/**
>> + * ecc_curve_get_nbits() - Get the number of bits of the curve
>> + * @curve:    The curve
>> + */
>> +static inline unsigned int ecc_curve_get_nbits(const struct ecc_curve *curve)
>> +{
>> +	if (curve->nbits)
>> +		return curve->nbits;
>> +	return curve->g.ndigits << ECC_DIGITS_TO_BYTES_SHIFT * 8;
>> +}
> 
> Since you're amending struct ecc_curve with an extra nbits value anyway,
> why not statically fill it in for all curves, instead of adding this
> extra complexity in the code?

I filled in all curves now, including ecrdsa cruves and curve25519.

    Stefan

> 
> Thanks,
> 
> Lukas
diff mbox series

Patch

diff --git a/include/crypto/internal/ecc.h b/include/crypto/internal/ecc.h
index b8ca5023b3b5..2d321b47d0f7 100644
--- a/include/crypto/internal/ecc.h
+++ b/include/crypto/internal/ecc.h
@@ -75,6 +75,17 @@  static inline void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
 	ecc_swap_digits(tmp, out, ndigits);
 }
 
+/**
+ * ecc_curve_get_nbits() - Get the number of bits of the curve
+ * @curve:    The curve
+ */
+static inline unsigned int ecc_curve_get_nbits(const struct ecc_curve *curve)
+{
+	if (curve->nbits)
+		return curve->nbits;
+	return curve->g.ndigits << ECC_DIGITS_TO_BYTES_SHIFT * 8;
+}
+
 /**
  * ecc_is_key_valid() - Validate a given ECDH private key
  *