From bfde220ad7709040f3fef4e51d86265eedfeba98 Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <nmav@redhat.com>
Date: Thu, 10 Mar 2016 17:37:36 +0100
Subject: [PATCH 1/3] Enforce casting to unsigned type when needed to avoid
 undefined behavior

This corrects issues of the following type caught with -fsanitize=undefined
runtime error: left shift of 184 by 24 places cannot be represented in type 'int'
---
 blowfish.c | 4 ++--
 twofish.c  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/blowfish.c b/blowfish.c
index ba921e7..4d1b1ec 100644
--- a/blowfish.c
+++ b/blowfish.c
@@ -359,8 +359,8 @@ blowfish_decrypt (const struct blowfish_ctx *ctx,
     {
       uint32_t d1, d2;
 
-      d1 = src[0] << 24 | src[1] << 16 | src[2] << 8 | src[3];
-      d2 = src[4] << 24 | src[5] << 16 | src[6] << 8 | src[7];
+      d1 = (((uint32_t)src[0]) << 24) | src[1] << 16 | src[2] << 8 | src[3];
+      d2 = (((uint32_t)src[4]) << 24) | src[5] << 16 | src[6] << 8 | src[7];
       decrypt (ctx, &d1, &d2);
       dst[0] = (d1 >> 24) & 0xff;
       dst[1] = (d1 >> 16) & 0xff;
diff --git a/twofish.c b/twofish.c
index 45b0854..5bc375c 100644
--- a/twofish.c
+++ b/twofish.c
@@ -190,14 +190,14 @@ compute_s(uint32_t m1, uint32_t m2)
   uint32_t s = 0;
   int i;
   for (i = 0; i < 4; i++)
-    s |=  ((  gf_multiply(0x4D, m1,       rs_matrix[i][0])
+    s |= ((uint32_t)((  gf_multiply(0x4D, m1,       rs_matrix[i][0])
 	    ^ gf_multiply(0x4D, m1 >> 8,  rs_matrix[i][1])
 	    ^ gf_multiply(0x4D, m1 >> 16, rs_matrix[i][2])
 	    ^ gf_multiply(0x4D, m1 >> 24, rs_matrix[i][3])
 	    ^ gf_multiply(0x4D, m2,       rs_matrix[i][4])
 	    ^ gf_multiply(0x4D, m2 >> 8,  rs_matrix[i][5])
 	    ^ gf_multiply(0x4D, m2 >> 16, rs_matrix[i][6])
-	    ^ gf_multiply(0x4D, m2 >> 24, rs_matrix[i][7])) << (i*8));
+	    ^ gf_multiply(0x4D, m2 >> 24, rs_matrix[i][7]))) << (i*8));
   return s;
 }
 
-- 
2.5.0

