improve patch

This commit is contained in:
Remi Collet 2017-10-02 13:49:59 +02:00
parent 5f71523662
commit 8f18e20d6c

View File

@ -1,17 +1,16 @@
From b10cf6a25d6e7ec77ba1499826026f5138301e3e Mon Sep 17 00:00:00 2001
From d3ff2d35daab910198d505be20ddad0587026cb7 Mon Sep 17 00:00:00 2001
From: Remi Collet <remi@remirepo.net>
Date: Fri, 29 Sep 2017 14:38:21 +0200
Subject: [PATCH] Fix Bug #75284 sha3 is not supported on bigendian machine
---
ext/hash/config.m4 | 32 ++++---
ext/hash/hash_slow_sha3.c | 240 ++++++++++++++++++++++++++++++++++++++++++++++
ext/hash/php_hash_sha3.h | 5 +
3 files changed, 265 insertions(+), 12 deletions(-)
create mode 100644 ext/hash/hash_slow_sha3.c
ext/hash/hash_sha3.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++
ext/hash/php_hash_sha3.h | 5 ++
3 files changed, 239 insertions(+), 12 deletions(-)
diff --git a/ext/hash/config.m4 b/ext/hash/config.m4
index 66cc7ca..e1f1c1a 100644
index 66cc7ca8108f..932e6d6f22c6 100644
--- a/ext/hash/config.m4
+++ b/ext/hash/config.m4
@@ -24,25 +24,33 @@ if test "$PHP_HASH" != "no"; then
@ -30,7 +29,8 @@ index 66cc7ca..e1f1c1a 100644
- PHP_HASH_CFLAGS="-I@ext_srcdir@/$SHA3_DIR -DKeccakP200_excluded -DKeccakP400_excluded -DKeccakP800_excluded"
+
+ if test $ac_cv_c_bigendian_php = yes; then
+ EXT_HASH_SHA3_SOURCES="hash_slow_sha3.c"
+ EXT_HASH_SHA3_SOURCES="hash_sha3.c"
+ AC_DEFINE(HAVE_SLOW_HASH3, 1, [Define is hash3 algo is available])
+ AC_MSG_WARN("Use SHA3 slow implementation on bigendian")
+ else
+ PHP_CHECK_64BIT([
@ -44,7 +44,6 @@ index 66cc7ca..e1f1c1a 100644
+ PHP_HASH_CFLAGS="-I@ext_srcdir@/$SHA3_DIR -DKeccakP200_excluded -DKeccakP400_excluded -DKeccakP800_excluded"
+
+ PHP_ADD_BUILD_DIR(ext/hash/$SHA3_DIR, 1)
+ AC_DEFINE(HAVE_FAST_HASH3, 1, [Define is hash3 algo is available])
+ fi
+
EXT_HASH_SOURCES="hash.c hash_md.c hash_sha.c hash_ripemd.c hash_haval.c \
@ -60,32 +59,16 @@ index 66cc7ca..e1f1c1a 100644
PHP_NEW_EXTENSION(hash, $EXT_HASH_SOURCES, $ext_shared,,$PHP_HASH_CFLAGS)
ifdef([PHP_INSTALL_HEADERS], [
PHP_INSTALL_HEADERS(ext/hash, $EXT_HASH_HEADERS)
diff --git a/ext/hash/hash_slow_sha3.c b/ext/hash/hash_slow_sha3.c
new file mode 100644
index 0000000..a3bfda4
--- /dev/null
+++ b/ext/hash/hash_slow_sha3.c
@@ -0,0 +1,240 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP Version 7 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997-2017 The PHP Group |
+ +----------------------------------------------------------------------+
+ | This source file is subject to version 3.01 of the PHP license, |
+ | that is bundled with this package in the file LICENSE, and is |
+ | available through the world-wide-web at the following url: |
+ | http://www.php.net/license/3_01.txt |
+ | If you did not receive a copy of the PHP license and are unable to |
+ | obtain it through the world-wide-web, please send a note to |
+ | license@php.net so we can mail you a copy immediately. |
+ +----------------------------------------------------------------------+
+ | Author: Sara Golemon <pollita@php.net> |
+ +----------------------------------------------------------------------+
+*/
+
+#include "php_hash.h"
+#include "php_hash_sha3.h"
diff --git a/ext/hash/hash_sha3.c b/ext/hash/hash_sha3.c
index ee9d010da4b2..98010c535019 100644
--- a/ext/hash/hash_sha3.c
+++ b/ext/hash/hash_sha3.c
@@ -19,6 +19,217 @@
#include "php_hash.h"
#include "php_hash_sha3.h"
+#ifdef HAVE_SLOW_HASH3
+// ================= slow algo ==============================================
+
+#if (defined(__APPLE__) || defined(__APPLE_CC__)) && \
+ (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
@ -107,7 +90,7 @@ index 0000000..a3bfda4
+
+#ifdef WORDS_BIGENDIAN
+static inline uint64_t load64(const unsigned char* x) {
+ char i;
+ signed char i;
+ uint64_t ret = 0;
+ for (i = 7; i >= 0; --i) {
+ ret <<= 8;
@ -291,38 +274,37 @@ index 0000000..a3bfda4
+ 1 \
+}
+
+DECLARE_SHA3_OPS(224);
+DECLARE_SHA3_OPS(256);
+DECLARE_SHA3_OPS(384);
+DECLARE_SHA3_OPS(512);
+#else
+
+#undef DECLARE_SHA3_OPS
+// ================= fast algo ==============================================
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=4 fdm=marker
+ * vim<600: sw=4 ts=4
+ */
#define SUCCESS SHA3_SUCCESS /* Avoid conflict between KeccacHash.h and zend_types.h */
#include "KeccakHash.h"
@@ -60,6 +271,9 @@ const php_hash_ops php_hash_sha3_##bits##_ops = { \
1 \
}
+#endif
+// ================= both algo ==============================================
+
DECLARE_SHA3_OPS(224);
DECLARE_SHA3_OPS(256);
DECLARE_SHA3_OPS(384);
diff --git a/ext/hash/php_hash_sha3.h b/ext/hash/php_hash_sha3.h
index b47d1b1..5fb3aa6 100644
index b47d1b102f37..73f0f8af3662 100644
--- a/ext/hash/php_hash_sha3.h
+++ b/ext/hash/php_hash_sha3.h
@@ -22,7 +22,12 @@
#include "php.h"
typedef struct {
+#ifdef HAVE_FAST_HASH3
void *hashinstance;
+#else
+#ifdef HAVE_SLOW_HASH3
+ unsigned char state[200]; // 5 * 5 * sizeof(uint64)
+ uint32_t pos;
+#else
void *hashinstance;
+#endif
} PHP_SHA3_CTX;
typedef PHP_SHA3_CTX PHP_SHA3_224_CTX;
--
2.9.5