diff mbox series

crypto: sm3 - fix undefined shift by >= width of value

Message ID 20190109061241.6943-1-ebiggers@kernel.org (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series crypto: sm3 - fix undefined shift by >= width of value | expand

Commit Message

Eric Biggers Jan. 9, 2019, 6:12 a.m. UTC
From: Eric Biggers <ebiggers@google.com>

sm3_compress() calls rol32() with shift >= 32, which causes undefined
behavior.  This is easily detected by enabling CONFIG_UBSAN.

Explicitly AND with 31 to make the behavior well defined.

Fixes: 4f0fc1600edb ("crypto: sm3 - add OSCCA SM3 secure hash")
Cc: <stable@vger.kernel.org> # v4.15+
Cc: Gilad Ben-Yossef <gilad@benyossef.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 crypto/sm3_generic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Herbert Xu Jan. 10, 2019, 2:06 p.m. UTC | #1
On Tue, Jan 08, 2019 at 10:12:41PM -0800, Eric Biggers wrote:
> From: Eric Biggers <ebiggers@google.com>
> 
> sm3_compress() calls rol32() with shift >= 32, which causes undefined
> behavior.  This is easily detected by enabling CONFIG_UBSAN.
> 
> Explicitly AND with 31 to make the behavior well defined.
> 
> Fixes: 4f0fc1600edb ("crypto: sm3 - add OSCCA SM3 secure hash")
> Cc: <stable@vger.kernel.org> # v4.15+
> Cc: Gilad Ben-Yossef <gilad@benyossef.com>
> Signed-off-by: Eric Biggers <ebiggers@google.com>
> ---
>  crypto/sm3_generic.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Patch applied.  Thanks.
diff mbox series

Patch

diff --git a/crypto/sm3_generic.c b/crypto/sm3_generic.c
index 9a5c60f08aad8..c0cf87ae7ef6d 100644
--- a/crypto/sm3_generic.c
+++ b/crypto/sm3_generic.c
@@ -100,7 +100,7 @@  static void sm3_compress(u32 *w, u32 *wt, u32 *m)
 
 	for (i = 0; i <= 63; i++) {
 
-		ss1 = rol32((rol32(a, 12) + e + rol32(t(i), i)), 7);
+		ss1 = rol32((rol32(a, 12) + e + rol32(t(i), i & 31)), 7);
 
 		ss2 = ss1 ^ rol32(a, 12);