com php-src: Fix bug #73127: NEWS ext/hash/hash_gost.c ex t/hash/tests/bug73127.phpt

[email protected] (Nikita Popov)
Newsgroups php.cvs
Message-ID <[email protected]>
Commit:    eac8166bd468a3c7c00b5163f6f86804911b660d
Author:    Grundik <[email protected]>         Sat, 18 Feb 2017 05:53:13 +0300
Committer: Nikita Popov <[email protected]>      Fri, 24 Feb 2017 23:20:49 +0100
Parents:   9450e23b3288100c07f2990cd0bce962ca56461b
Branches:  PHP-7.0 PHP-7.1 master

Link:       http://git.php.net/?p=php-src.git;a=commitdiff;h=eac8166bd468a3c7c00b5163f6f86804911b660d

Log:
Fix bug #73127

gost-crypto hash was incorrect if input data contained long 0xFF
sequence, due to a carry-propagation bug.

Bugs:
https://bugs.php.net/73127

Changed paths:
  M  NEWS
  M  ext/hash/hash_gost.c
  A  ext/hash/tests/bug73127.phpt


Diff:
diff --git a/NEWS b/NEWS
index ee4c87e..0631c93 100644
--- a/NEWS
+++ b/NEWS
@@ -34,6 +34,10 @@ PHP                                                                        NEWS
   . Fixed bug #69860 (php-fpm process accounting is broken with keepalive).
     (Denis Yeldandi)
 
+- Hash:
+  . Fixed bug #73127 (gost-crypto hash incorrect if input data contains long
+    0xFF sequence). (Grundik)
+
 - GD:
   . Fixed bug #74031 (ReflectionFunction for imagepng is missing last two
     parameters). (finwe)
diff --git a/ext/hash/hash_gost.c b/ext/hash/hash_gost.c
index ed8ca15..c171337 100644
--- a/ext/hash/hash_gost.c
+++ b/ext/hash/hash_gost.c
@@ -227,14 +227,13 @@ static inline void Gost(PHP_GOST_CTX *context, php_hash_uint32 data[8])
 static inline void GostTransform(PHP_GOST_CTX *context, const unsigned char input[32])
 {
 	int i, j;
-	php_hash_uint32 data[8], temp = 0, save = 0;
+	php_hash_uint32 data[8], temp = 0;
 
 	for (i = 0, j = 0; i < 8; ++i, j += 4) {
 		data[i] =	((php_hash_uint32) input[j]) | (((php_hash_uint32) input[j + 1]) << 8) |
 					(((php_hash_uint32) input[j + 2]) << 16) | (((php_hash_uint32) input[j + 3]) << 24);
-		save = context->state[i + 8];
 		context->state[i + 8] += data[i] + temp;
-		temp = ((context->state[i + 8] < data[i]) || (context->state[i + 8] < save)) ? 1 : 0;
+		temp = context->state[i + 8] < data[i] ? 1 : (context->state[i + 8] == data[i] ? temp : 0);
 	}
 
 	Gost(context, data);
diff --git a/ext/hash/tests/bug73127.phpt b/ext/hash/tests/bug73127.phpt
new file mode 100644
index 0000000..d9f1f7d
--- /dev/null
+++ b/ext/hash/tests/bug73127.phpt
@@ -0,0 +1,27 @@
+--TEST--
+Bug #73127 (gost-crypto hash incorrect if input data contains long 0xFF sequence)
+--FILE--
+<?php // $Id$
+
+$test1 = str_repeat("\xFF", 40);
+$test2 = str_repeat("\x00", 40);
+echo hash('gost-crypto', $test1),
+     "\n",
+     hash('gost', $test1),
+     "\n",
+     hash('gost-crypto', $test2),
+     "\n",
+     hash('gost', $test2),
+     "\n",
+     hash('gost-crypto', ''),
+     "\n",
+     hash('gost', '')
+    ;
+?>
+--EXPECT--
+231d8bb980d3faa30fee6ec475df5669cf6c24bbce22f46d6737470043a99f8e
+9eaf96ba62f90fae6707f1d4274d1a9d6680f5a121d4387815aa3a6ec42439c5
+bbf1f3179409c853cd3e396d67b0e10a266c218a4fd19f149c39aa4f6d37a007
+a0be0b90fea5a5b50c90c9429d07bb98fa0c06f0c30210e94c7d80c3125c67ac
+981e5f3ca30c841487830f84fb433e13ac1101569b9c13584ac483234cd656c0
+ce85b99cc46752fffee35cab9a7b0278abb4c2d2055cff685af4912c49490f8d
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.