scottmac Mon Jun 2 14:13:49 2008 UTC
Modified files:
/php-src/ext/hash hash.c hash_sha.c php_hash.h php_hash_sha.h
/php-src/ext/hash/tests sha224.phpt
Log:
MFB: Add sha224 support.
http://cvs.php.net/viewvc.cgi/php-src/ext/hash/hash.c?r1=1.41&r2=1.42&diff_format=u
Index: php-src/ext/hash/hash.c
diff -u php-src/ext/hash/hash.c:1.41 php-src/ext/hash/hash.c:1.42
--- php-src/ext/hash/hash.c:1.41 Mon Apr 21 15:37:07 2008
+++ php-src/ext/hash/hash.c Mon Jun 2 14:13:49 2008
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: hash.c,v 1.41 2008/04/21 15:37:07 tony2001 Exp $ */
+/* $Id: hash.c,v 1.42 2008/06/02 14:13:49 scottmac Exp $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -745,6 +745,7 @@
php_hash_register_algo("md4", &php_hash_md4_ops);
php_hash_register_algo("md5", &php_hash_md5_ops);
php_hash_register_algo("sha1", &php_hash_sha1_ops);
+ php_hash_register_algo("sha224", &php_hash_sha224_ops);
php_hash_register_algo("sha256", &php_hash_sha256_ops);
php_hash_register_algo("sha384", &php_hash_sha384_ops);
php_hash_register_algo("sha512", &php_hash_sha512_ops);
http://cvs.php.net/viewvc.cgi/php-src/ext/hash/hash_sha.c?r1=1.17&r2=1.18&diff_format=u
Index: php-src/ext/hash/hash_sha.c
diff -u php-src/ext/hash/hash_sha.c:1.17 php-src/ext/hash/hash_sha.c:1.18
--- php-src/ext/hash/hash_sha.c:1.17 Mon Apr 21 15:37:07 2008
+++ php-src/ext/hash/hash_sha.c Mon Jun 2 14:13:49 2008
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: hash_sha.c,v 1.17 2008/04/21 15:37:07 tony2001 Exp $ */
+/* $Id: hash_sha.c,v 1.18 2008/06/02 14:13:49 scottmac Exp $ */
#include "php_hash.h"
#include "php_hash_sha.h"
@@ -76,7 +76,7 @@
sizeof(PHP_SHA1_CTX)
};
-/* sha256 */
+/* sha224/sha256 */
const php_hash_ops php_hash_sha256_ops = {
(php_hash_init_func_t) PHP_SHA256Init,
@@ -88,6 +88,16 @@
sizeof(PHP_SHA256_CTX)
};
+const php_hash_ops php_hash_sha224_ops = {
+ (php_hash_init_func_t) PHP_SHA224Init,
+ (php_hash_update_func_t) PHP_SHA224Update,
+ (php_hash_final_func_t) PHP_SHA224Final,
+ (php_hash_copy_func_t) php_hash_copy,
+ 28,
+ 64,
+ sizeof(PHP_SHA224_CTX)
+};
+
#define ROTR32(b,x) ((x >> b) | (x << (32 - b)))
#define ROTR64(b,x) ((x >> b) | (x << (64 - b)))
#define SHR(b, x) (x >> b)
@@ -175,6 +185,102 @@
}
/* }}} */
+/* {{{ PHP_SHA224Init
+ * SHA224 initialization. Begins an SHA224 operation, writing a new context.
+ */
+PHP_HASH_API void PHP_SHA224Init(PHP_SHA224_CTX * context)
+{
+ context->count[0] = context->count[1] = 0;
+ /* Load magic initialization constants.
+ */
+ context->state[0] = 0xc1059ed8;
+ context->state[1] = 0x367cd507;
+ context->state[2] = 0x3070dd17;
+ context->state[3] = 0xf70e5939;
+ context->state[4] = 0xffc00b31;
+ context->state[5] = 0x68581511;
+ context->state[6] = 0x64f98fa7;
+ context->state[7] = 0xbefa4fa4;
+}
+/* }}} */
+
+/* {{{ PHP_SHA224Update
+ SHA224 block update operation. Continues an SHA224 message-digest
+ operation, processing another message block, and updating the
+ context.
+ */
+PHP_HASH_API void PHP_SHA224Update(PHP_SHA224_CTX * context, const unsigned
char *input, unsigned int inputLen)
+{
+ unsigned int i, index, partLen;
+
+ /* Compute number of bytes mod 64 */
+ index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
+
+ /* Update number of bits */
+ if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) <
((php_hash_uint32) inputLen << 3)) {
+ context->count[1]++;
+ }
+ context->count[1] += ((php_hash_uint32) inputLen >> 29);
+
+ partLen = 64 - index;
+
+ /* Transform as many times as possible.
+ */
+ if (inputLen >= partLen) {
+ memcpy((unsigned char*) & context->buffer[index], (unsigned
char*) input, partLen);
+ SHA256Transform(context->state, context->buffer);
+
+ for (i = partLen; i + 63 < inputLen; i += 64) {
+ SHA256Transform(context->state, &input[i]);
+ }
+
+ index = 0;
+ } else {
+ i = 0;
+ }
+
+ /* Buffer remaining input */
+ memcpy((unsigned char*) & context->buffer[index], (unsigned char*) &
input[i], inputLen - i);
+}
+/* }}} */
+
+/* {{{ PHP_SHA224Final
+ SHA224 finalization. Ends an SHA224 message-digest operation, writing the
+ the message digest and zeroizing the context.
+ */
+PHP_HASH_API void PHP_SHA224Final(unsigned char digest[28], PHP_SHA224_CTX *
context)
+{
+ unsigned char bits[8];
+ unsigned int index, padLen;
+
+ /* Save number of bits */
+ bits[7] = (unsigned char) (context->count[0] & 0xFF);
+ bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
+ bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
+ bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
+ bits[3] = (unsigned char) (context->count[1] & 0xFF);
+ bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
+ bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
+ bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
+
+ /* Pad out to 56 mod 64.
+ */
+ index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
+ padLen = (index < 56) ? (56 - index) : (120 - index);
+ PHP_SHA224Update(context, PADDING, padLen);
+
+ /* Append length (before padding) */
+ PHP_SHA224Update(context, bits, 8);
+
+ /* Store state in digest */
+ SHAEncode32(digest, context->state, 28);
+
+ /* Zeroize sensitive information.
+ */
+ memset((unsigned char*) context, 0, sizeof(*context));
+}
+/* }}} */
+
/* {{{ PHP_SHA256Update
SHA256 block update operation. Continues an SHA256 message-digest
operation, processing another message block, and updating the
@@ -544,10 +650,10 @@
/* }}} */
/* {{{ PHP_SHA512Final
- SHA512 finalization. Ends an SHA384 message-digest operation, writing the
+ SHA512 finalization. Ends an SHA512 message-digest operation, writing the
the message digest and zeroizing the context.
*/
-PHP_HASH_API void PHP_SHA512Final(unsigned char digest[48], PHP_SHA512_CTX *
context)
+PHP_HASH_API void PHP_SHA512Final(unsigned char digest[64], PHP_SHA512_CTX *
context)
{
unsigned char bits[16];
unsigned int index, padLen;
http://cvs.php.net/viewvc.cgi/php-src/ext/hash/php_hash.h?r1=1.26&r2=1.27&diff_format=u
Index: php-src/ext/hash/php_hash.h
diff -u php-src/ext/hash/php_hash.h:1.26 php-src/ext/hash/php_hash.h:1.27
--- php-src/ext/hash/php_hash.h:1.26 Mon Apr 21 15:37:07 2008
+++ php-src/ext/hash/php_hash.h Mon Jun 2 14:13:49 2008
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_hash.h,v 1.26 2008/04/21 15:37:07 tony2001 Exp $ */
+/* $Id: php_hash.h,v 1.27 2008/06/02 14:13:49 scottmac Exp $ */
#ifndef PHP_HASH_H
#define PHP_HASH_H
@@ -62,6 +62,7 @@
extern const php_hash_ops php_hash_md4_ops;
extern const php_hash_ops php_hash_md5_ops;
extern const php_hash_ops php_hash_sha1_ops;
+extern const php_hash_ops php_hash_sha224_ops;
extern const php_hash_ops php_hash_sha256_ops;
extern const php_hash_ops php_hash_sha384_ops;
extern const php_hash_ops php_hash_sha512_ops;
http://cvs.php.net/viewvc.cgi/php-src/ext/hash/php_hash_sha.h?r1=1.7&r2=1.8&diff_format=u
Index: php-src/ext/hash/php_hash_sha.h
diff -u php-src/ext/hash/php_hash_sha.h:1.7 php-src/ext/hash/php_hash_sha.h:1.8
--- php-src/ext/hash/php_hash_sha.h:1.7 Mon Dec 31 07:12:10 2007
+++ php-src/ext/hash/php_hash_sha.h Mon Jun 2 14:13:49 2008
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_hash_sha.h,v 1.7 2007/12/31 07:12:10 sebastian Exp $ */
+/* $Id: php_hash_sha.h,v 1.8 2008/06/02 14:13:49 scottmac Exp $ */
#ifndef PHP_HASH_SHA_H
#define PHP_HASH_SHA_H
@@ -25,6 +25,17 @@
#include "ext/standard/sha1.h"
#include "ext/standard/basic_functions.h"
+/* SHA224 context. */
+typedef struct {
+ php_hash_uint32 state[8]; /* state */
+ php_hash_uint32 count[2]; /* number of bits, modulo 2^64
*/
+ unsigned char buffer[64]; /* input buffer */
+} PHP_SHA224_CTX;
+
+PHP_HASH_API void PHP_SHA224Init(PHP_SHA224_CTX *);
+PHP_HASH_API void PHP_SHA224Update(PHP_SHA224_CTX *, const unsigned char *,
unsigned int);
+PHP_HASH_API void PHP_SHA224Final(unsigned char[28], PHP_SHA224_CTX *);
+
/* SHA256 context. */
typedef struct {
php_hash_uint32 state[8]; /* state */
http://cvs.php.net/viewvc.cgi/php-src/ext/hash/tests/sha224.phpt?r1=1.1&r2=1.2&diff_format=u
Index: php-src/ext/hash/tests/sha224.phpt
diff -u /dev/null php-src/ext/hash/tests/sha224.phpt:1.2
--- /dev/null Mon Jun 2 14:13:49 2008
+++ php-src/ext/hash/tests/sha224.phpt Mon Jun 2 14:13:49 2008
@@ -0,0 +1,22 @@
+--TEST--
+sha224 algorithm
+--SKIPIF--
+<?php if(!extension_loaded("hash")) print "skip"; ?>
+--FILE--
+<?php
+echo hash('sha224', '') . "\n";
+echo hash('sha224', 'a') . "\n";
+echo hash('sha224',
'012345678901234567890123456789012345678901234567890123456789') . "\n";
+
+/* FIPS-180 Vectors */
+echo hash('sha224', 'abc') . "\n";
+echo hash('sha224',
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') . "\n";
+echo hash('sha224', str_repeat('a', 1000000)) . "\n";
+--EXPECT--
+d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
+abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5
+ae5c0d27fe120752911c994718296a3bccc77000aac07b8810714932
+23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7
+75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525
+20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67
+
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php