crypto: ecc - Protect ecc_digits_from_bytes from reading too many bytes
Subsystems (2)
PATCH 1/1 crypto: ecc - Protect ecc_digits_from_bytes from reading too many bytes
Protect 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.
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.
--- include/crypto/internal/ecc.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/crypto/internal/ecc.h +7 -0
@@ -67,9 +67,16 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigitstatic inline 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 */ +while (diff >0 ) { + out[--ndigits] =0 ; + diff--; + } +if (o) {memcpy ((u8 *)&msd +sizeof (msd) - o, in, o); out[--ndigits] =be64_to_cpu (msd);
Signature · 1 line
2.43.0
Jarkko Sakkinen
maintainerQuoted 22 lines · Stefan Berger
Protect
ecc_digits_from_bytesfrom 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.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")Signed-off-by Stefan Berger <[email protected]>--- include/crypto/internal/ecc.h | 7 +++++++ 1 file changed, 7 insertions(+)include/crypto/internal/ecc.h +0
-0@@ -67,9 +67,16 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigitstatic inline void ecc_digits_from_bytes (const u8 *in,unsigned int nbytes,u64 *out,unsigned int ndigits)
include/crypto/internal/ecc.h static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit · line 78
Quoted 10 lines
include/crypto/internal/ecc.h +6
-0{ +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 */ +while (diff >0 ) { + out[--ndigits] =0 ; + diff--; + }
Could be just trivial for-loop:
for (i = 0; i < diff; i++)
out[--ndigits] = 0;
Or also simpler while-loop could work:
while (diff-- > 0)
out[--ndigits] = 0;
BR, Jarkko
Quoted 36 lines · Jarkko Sakkinen
Quoted 22 lines · Stefan Berger
Protect
ecc_digits_from_bytesfrom 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.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")Signed-off-by Stefan Berger <[email protected]>--- include/crypto/internal/ecc.h | 7 +++++++ 1 file changed, 7 insertions(+)include/crypto/internal/ecc.h +0
-0@@ -67,9 +67,16 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigitstatic inline void ecc_digits_from_bytes (const u8 *in,unsigned int nbytes,u64 *out,unsigned int ndigits)include/crypto/internal/ecc.h static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit · line 78
Quoted 10 lines
include/crypto/internal/ecc.h +6
-0{ +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 */ +while (diff >0 ) { + out[--ndigits] =0 ; + diff--; + }Could be just trivial for-loop:
Quoted 7 lines
for (i = 0; i < diff; i++)
out[--ndigits] = 0;Or also simpler while-loop could work:
while (diff-- > 0)
out[--ndigits] = 0;
Or just use memset(), which uses optimized instructions on many arches.
Jarkko Sakkinen
maintainerQuoted 40 lines · Lukas Wunner
Quoted 39 lines · Jarkko Sakkinen
Quoted 22 lines · Stefan Berger
Protect
ecc_digits_from_bytesfrom 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.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")Signed-off-by Stefan Berger <[email protected]>--- include/crypto/internal/ecc.h | 7 +++++++ 1 file changed, 7 insertions(+)include/crypto/internal/ecc.h +0
-0@@ -67,9 +67,16 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigitstatic inline void ecc_digits_from_bytes (const u8 *in,unsigned int nbytes,u64 *out,unsigned int ndigits)include/crypto/internal/ecc.h static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit · line 78
Quoted 10 lines
include/crypto/internal/ecc.h +6
-0{ +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 */ +while (diff >0 ) { + out[--ndigits] =0 ; + diff--; + }Could be just trivial for-loop:
for (i = 0; i < diff; i++)
out[--ndigits] = 0;
Quoted 6 lines
Quoted 4 lines
Or also simpler while-loop could work:
while (diff-- > 0)
out[--ndigits] = 0;Or just use
memset(), which uses optimized instructions on many arches.
Yeah, sure, that would be even better, or even memzero_explicit()?
BR, Jarkko
Quoted 41 lines · Jarkko Sakkinen
Quoted 40 lines · Lukas Wunner
Quoted 39 lines · Jarkko Sakkinen
Quoted 22 lines · Stefan Berger
Protect
ecc_digits_from_bytesfrom 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.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")Signed-off-by Stefan Berger <[email protected]>--- include/crypto/internal/ecc.h | 7 +++++++ 1 file changed, 7 insertions(+)include/crypto/internal/ecc.h +0
-0@@ -67,9 +67,16 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigitstatic inline void ecc_digits_from_bytes (const u8 *in,unsigned int nbytes,u64 *out,unsigned int ndigits)include/crypto/internal/ecc.h static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit · line 78
Quoted 10 lines
include/crypto/internal/ecc.h +6
-0{ +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 */ +while (diff >0 ) { + out[--ndigits] =0 ; + diff--; + }Could be just trivial for-loop:
for (i = 0; i < diff; i++)
out[--ndigits] = 0;
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Or also simpler while-loop could work:
while (diff-- > 0)
out[--ndigits] = 0;Or just use
memset(), which uses optimized instructions on many arches.Yeah, sure, that would be even better, or even
memzero_explicit()?
Thanks. The function isn't getting too big for an inline?
Quoted 2 lines
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 46 lines · Stefan Berger
Quoted 43 lines · Jarkko Sakkinen
Quoted 42 lines · Lukas Wunner
Quoted 41 lines · Jarkko Sakkinen
Quoted 22 lines · Stefan Berger
Protect
ecc_digits_from_bytesfrom 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.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")Signed-off-by Stefan Berger <[email protected]>--- include/crypto/internal/ecc.h | 7 +++++++ 1 file changed, 7 insertions(+)include/crypto/internal/ecc.h +0
-0@@ -67,9 +67,16 @@ static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigitstatic inline void ecc_digits_from_bytes (const u8 *in,unsigned int nbytes,u64 *out,unsigned int ndigits)include/crypto/internal/ecc.h static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigit · line 78
Quoted 10 lines
include/crypto/internal/ecc.h +6
-0{ +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 */ +while (diff >0 ) { + out[--ndigits] =0 ; + diff--; + }Could be just trivial for-loop:
for (i = 0; i < diff; i++)
out[--ndigits] = 0;Or also simpler while-loop could work:
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
while (diff-- > 0)
out[--ndigits] = 0;Or just use
memset(), which uses optimized instructions on many arches.Yeah, sure, that would be even better, or even
memzero_explicit()?Thanks. The function isn't getting too big for an inline?
Hmm... so as far as I'm concerned you pick what works for you. Just was pointing out at it would make to simplify the original a bit :-)
BR, Jarkko
Herbert Xu
maintainerQuoted 2 lines · Jarkko Sakkinen
Yeah, sure, that would be even better, or even
memzero_explicit()?
memzero_explicit should only be used for stack memory.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Jarkko Sakkinen
maintainerQuoted 5 lines · Herbert Xu
Quoted 2 lines · Jarkko Sakkinen
Yeah, sure, that would be even better, or even
memzero_explicit()?
memzero_explicitshould only be used for stack memory.
BTW, is this in kernel documentation? It's a guideline really and would be nice to have reminder, that's all.
BR, Jarkko
Jarkko Sakkinen
maintainerQuoted 9 lines · Jarkko Sakkinen
Quoted 5 lines · Herbert Xu
Quoted 2 lines · Jarkko Sakkinen
Yeah, sure, that would be even better, or even
memzero_explicit()?
memzero_explicitshould only be used for stack memory.BTW, is this in kernel documentation? It's a guideline really and would be nice to have reminder, that's all.
Any case makes sense and is pretty obvious now that you said it.
BR, Jarkko