From 4c8b0cdd97ffec3ae3f8d995afdfccbc261b3c79 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Thu, 29 Oct 2020 20:04:20 +0100
Subject: [PATCH] blowfish: Add casts to uint32_t.

Avoids undefined behavior, since shifting an 8-bit value left by 24
bits overflows the range of signed int. Reported by Guido Vranken.
---
 ChangeLog  | 6 ++++++
 blowfish.c | 6 ++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 57d121be..6626c6ea 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2020-10-29  Niels Möller  <nisse@lysator.liu.se>
+
+	* blowfish.c (blowfish_set_key): Add casts to uint32_t. Avoids
+	undefined behavior, since shifting an 8-bit value left by 24 bits
+	overflows the range of signed int. Reported by Guido Vranken.
+
 2020-10-28  Niels Möller  <nisse@lysator.liu.se>
 
 	* gmp-glue.h (cnd_add_n, cnd_sub_n, cnd_swap): Deleted, use
diff --git a/blowfish.c b/blowfish.c
index e73caffe..3d546694 100644
--- a/blowfish.c
+++ b/blowfish.c
@@ -385,8 +385,10 @@ blowfish_set_key (struct blowfish_ctx *ctx,
 
   for (i = j = 0; i < _BLOWFISH_ROUNDS + 2; i++)
     {
-      data = (key[j] << 24) | (key[(j+1) % length] << 16)
-	| (key[(j+2) % length] << 8) | key[(j+3) % length];
+      data = ((uint32_t) key[j] << 24)
+	| ((uint32_t) key[(j+1) % length] << 16)
+	| ((uint32_t) key[(j+2) % length] << 8)
+	| (uint32_t) key[(j+3) % length];
       ctx->p[i] ^= data;
       j = (j + 4) % length;
     }
-- 
GitLab