Skip to content

Commit 49a7be0

Browse files
committed
Implement FIPS 180-4 algos: sha512/256 and sha512/224
These algorithms are simple extensions to the existing sha512 algo using different initialization vectors and producing truncated output.
1 parent 7594fd0 commit 49a7be0

File tree

9 files changed

+134
-1
lines changed

9 files changed

+134
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ PHP NEWS
4343

4444
- Hash:
4545
. Added SHA3 fixed mode algorithms (224, 256, 384, and 512 bit). (Sara)
46+
. Added SHA512/256 and SHA512/224 algorithms. (Sara)
4647

4748
- JSON:
4849
. Escaped U+2028 and U+2029 when JSON_UNESCAPED_UNICODE is supplied as

ext/hash/hash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,8 @@ PHP_MINIT_FUNCTION(hash)
10071007
php_hash_register_algo("sha224", &php_hash_sha224_ops);
10081008
php_hash_register_algo("sha256", &php_hash_sha256_ops);
10091009
php_hash_register_algo("sha384", &php_hash_sha384_ops);
1010+
php_hash_register_algo("sha512/224", &php_hash_sha512_224_ops);
1011+
php_hash_register_algo("sha512/256", &php_hash_sha512_256_ops);
10101012
php_hash_register_algo("sha512", &php_hash_sha512_ops);
10111013
php_hash_register_algo("sha3-224", &php_hash_sha3_224_ops);
10121014
php_hash_register_algo("sha3-256", &php_hash_sha3_256_ops);

ext/hash/hash_sha.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,42 @@ PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX * context)
939939
}
940940
/* }}} */
941941

942+
/* {{{ PHP_SHA512_256Init
943+
* SHA512/245 initialization. Identical algorithm to SHA512, using alternate initval and truncation
944+
*/
945+
PHP_HASH_API void PHP_SHA512_256Init(PHP_SHA512_CTX * context)
946+
{
947+
context->count[0] = context->count[1] = 0;
948+
949+
context->state[0] = L64(0x22312194FC2BF72C);
950+
context->state[1] = L64(0x9F555FA3C84C64C2);
951+
context->state[2] = L64(0x2393B86B6F53B151);
952+
context->state[3] = L64(0x963877195940EABD);
953+
context->state[4] = L64(0x96283EE2A88EFFE3);
954+
context->state[5] = L64(0xBE5E1E2553863992);
955+
context->state[6] = L64(0x2B0199FC2C85B8AA);
956+
context->state[7] = L64(0x0EB72DDC81C52CA2);
957+
}
958+
/* }}} */
959+
960+
/* {{{ PHP_SHA512_224Init
961+
* SHA512/224 initialization. Identical algorithm to SHA512, using alternate initval and truncation
962+
*/
963+
PHP_HASH_API void PHP_SHA512_224Init(PHP_SHA512_CTX * context)
964+
{
965+
context->count[0] = context->count[1] = 0;
966+
967+
context->state[0] = L64(0x8C3D37C819544DA2);
968+
context->state[1] = L64(0x73E1996689DCD4D6);
969+
context->state[2] = L64(0x1DFAB7AE32FF9C82);
970+
context->state[3] = L64(0x679DD514582F9FCF);
971+
context->state[4] = L64(0x0F6D2B697BD44DA8);
972+
context->state[5] = L64(0x77E36F7304C48942);
973+
context->state[6] = L64(0x3F9D85A86A1D36C8);
974+
context->state[7] = L64(0x1112E6AD91D692A1);
975+
}
976+
/* }}} */
977+
942978
/* {{{ PHP_SHA512Update
943979
SHA512 block update operation. Continues an SHA512 message-digest
944980
operation, processing another message block, and updating the
@@ -1024,6 +1060,28 @@ PHP_HASH_API void PHP_SHA512Final(unsigned char digest[64], PHP_SHA512_CTX * con
10241060
}
10251061
/* }}} */
10261062

1063+
/* {{{ PHP_SHA512_256Final
1064+
SHA512/256 finalization. Identical to SHA512Final, but with truncation
1065+
*/
1066+
PHP_HASH_API void PHP_SHA512_256Final(unsigned char digest[32], PHP_SHA512_CTX * context)
1067+
{
1068+
unsigned char full_digest[64];
1069+
PHP_SHA512Final(full_digest, context);
1070+
memcpy(digest, full_digest, 32);
1071+
}
1072+
/* }}} */
1073+
1074+
/* {{{ PHP_SHA512_224Final
1075+
SHA512/224 finalization. Identical to SHA512Final, but with truncation
1076+
*/
1077+
PHP_HASH_API void PHP_SHA512_224Final(unsigned char digest[28], PHP_SHA512_CTX * context)
1078+
{
1079+
unsigned char full_digest[64];
1080+
PHP_SHA512Final(full_digest, context);
1081+
memcpy(digest, full_digest, 28);
1082+
}
1083+
/* }}} */
1084+
10271085
const php_hash_ops php_hash_sha512_ops = {
10281086
(php_hash_init_func_t) PHP_SHA512Init,
10291087
(php_hash_update_func_t) PHP_SHA512Update,
@@ -1034,6 +1092,26 @@ const php_hash_ops php_hash_sha512_ops = {
10341092
sizeof(PHP_SHA512_CTX)
10351093
};
10361094

1095+
const php_hash_ops php_hash_sha512_256_ops = {
1096+
(php_hash_init_func_t) PHP_SHA512_256Init,
1097+
(php_hash_update_func_t) PHP_SHA512_256Update,
1098+
(php_hash_final_func_t) PHP_SHA512_256Final,
1099+
(php_hash_copy_func_t) php_hash_copy,
1100+
32,
1101+
128,
1102+
sizeof(PHP_SHA512_CTX)
1103+
};
1104+
1105+
const php_hash_ops php_hash_sha512_224_ops = {
1106+
(php_hash_init_func_t) PHP_SHA512_224Init,
1107+
(php_hash_update_func_t) PHP_SHA512_224Update,
1108+
(php_hash_final_func_t) PHP_SHA512_224Final,
1109+
(php_hash_copy_func_t) php_hash_copy,
1110+
28,
1111+
128,
1112+
sizeof(PHP_SHA512_CTX)
1113+
};
1114+
10371115
/*
10381116
* Local variables:
10391117
* tab-width: 4

ext/hash/php_hash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ extern const php_hash_ops php_hash_sha224_ops;
6464
extern const php_hash_ops php_hash_sha256_ops;
6565
extern const php_hash_ops php_hash_sha384_ops;
6666
extern const php_hash_ops php_hash_sha512_ops;
67+
extern const php_hash_ops php_hash_sha512_256_ops;
68+
extern const php_hash_ops php_hash_sha512_224_ops;
6769
extern const php_hash_ops php_hash_sha3_224_ops;
6870
extern const php_hash_ops php_hash_sha3_256_ops;
6971
extern const php_hash_ops php_hash_sha3_384_ops;

ext/hash/php_hash_sha.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,12 @@ PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX *);
9494
PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX *, const unsigned char *, unsigned int);
9595
PHP_HASH_API void PHP_SHA512Final(unsigned char[64], PHP_SHA512_CTX *);
9696

97+
PHP_HASH_API void PHP_SHA512_256Init(PHP_SHA512_CTX *);
98+
#define PHP_SHA512_256Update PHP_SHA512Update
99+
PHP_HASH_API void PHP_SHA512_256Final(unsigned char[32], PHP_SHA512_CTX *);
100+
101+
PHP_HASH_API void PHP_SHA512_224Init(PHP_SHA512_CTX *);
102+
#define PHP_SHA512_224Update PHP_SHA512Update
103+
PHP_HASH_API void PHP_SHA512_224Final(unsigned char[28], PHP_SHA512_CTX *);
104+
97105
#endif /* PHP_HASH_SHA_H */

ext/hash/tests/hash_algos.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var_dump(hash_algos());
1818
===Done===
1919
--EXPECTF--
2020
*** Testing hash_algos() : basic functionality ***
21-
array(50) {
21+
array(52) {
2222
[%d]=>
2323
string(3) "md2"
2424
[%d]=>
@@ -34,6 +34,10 @@ array(50) {
3434
[%d]=>
3535
string(6) "sha384"
3636
[%d]=>
37+
string(10) "sha512/224"
38+
[%d]=>
39+
string(10) "sha512/256"
40+
[%d]=>
3741
string(6) "sha512"
3842
[%d]=>
3943
string(8) "sha3-224"

ext/hash/tests/hash_copy_001.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ string(64) "d3a13cf52af8e9390caed78b77b6b1e06e102204e3555d111dfd149bc5d54dba"
5252
string(6) "sha384"
5353
string(96) "6950d861ace4102b803ab8b3779d2f471968233010d2608974ab89804cef6f76162b4433d6e554e11e40a7cdcf510ea3"
5454
string(96) "6950d861ace4102b803ab8b3779d2f471968233010d2608974ab89804cef6f76162b4433d6e554e11e40a7cdcf510ea3"
55+
string(10) "sha512/224"
56+
string(56) "a2573d0e3f6c3e2d174c935a35a8ea31032f04e9e83499ac3ceda568"
57+
string(56) "a2573d0e3f6c3e2d174c935a35a8ea31032f04e9e83499ac3ceda568"
58+
string(10) "sha512/256"
59+
string(64) "fddacab80b3a610ba024c9d75a5fe0cafe5ae7c789f829b3c5fbea8ef11ccc1a"
60+
string(64) "fddacab80b3a610ba024c9d75a5fe0cafe5ae7c789f829b3c5fbea8ef11ccc1a"
5561
string(6) "sha512"
5662
string(128) "caced3db8e9e3a5543d5b933bcbe9e7834e6667545c3f5d4087b58ec8d78b4c8a4a5500c9b88f65f7368810ba9905e51f1cff3b25a5dccf76634108fb4e7ce13"
5763
string(128) "caced3db8e9e3a5543d5b933bcbe9e7834e6667545c3f5d4087b58ec8d78b4c8a4a5500c9b88f65f7368810ba9905e51f1cff3b25a5dccf76634108fb4e7ce13"
@@ -202,6 +208,12 @@ string(64) "268e7f4cf88504a53fd77136c4c4748169f46ff7150b376569ada9c374836944"
202208
string(6) "sha384"
203209
string(96) "6950d861ace4102b803ab8b3779d2f471968233010d2608974ab89804cef6f76162b4433d6e554e11e40a7cdcf510ea3"
204210
string(96) "0d44981d04bb11b1ef75d5c2932bd0aa2785e7bc454daac954d77e2ca10047879b58997533fc99650b20049c6cb9a6cc"
211+
string(10) "sha512/224"
212+
string(56) "a2573d0e3f6c3e2d174c935a35a8ea31032f04e9e83499ac3ceda568"
213+
string(56) "cbc2bbf0028ed803af785b0f264962c84ec48d8ee0908322ef995ddb"
214+
string(10) "sha512/256"
215+
string(64) "fddacab80b3a610ba024c9d75a5fe0cafe5ae7c789f829b3c5fbea8ef11ccc1a"
216+
string(64) "2cec704878ffa7128e0c4a61eef87d1f3c823184d364dfa3fed73beb00499b00"
205217
string(6) "sha512"
206218
string(128) "caced3db8e9e3a5543d5b933bcbe9e7834e6667545c3f5d4087b58ec8d78b4c8a4a5500c9b88f65f7368810ba9905e51f1cff3b25a5dccf76634108fb4e7ce13"
207219
string(128) "28d7c721433782a880f840af0c3f3ea2cad4ef55de2114dda9d504cedeb110e1cf2519c49e4b5da3da4484bb6ba4fd1621ceadc6408f4410b2ebe9d83a4202c2"

ext/hash/tests/sha512-224.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
sha512/224 algorithm
3+
--SKIPIF--
4+
<?php if(!extension_loaded("hash")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
echo hash('sha512/224', '') . "\n";
8+
echo hash('sha512/224', 'abc') . "\n";
9+
echo hash('sha512/224', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu') . "\n";
10+
--EXPECT--
11+
6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4
12+
4634270f707b6a54daae7530460842e20e37ed265ceee9a43e8924aa
13+
23fec5bb94d60b23308192640b0c453335d664734fe40e7268674af9

ext/hash/tests/sha512-256.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
sha512/256 algorithm
3+
--SKIPIF--
4+
<?php if(!extension_loaded("hash")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
echo hash('sha512/256', '') . "\n";
8+
echo hash('sha512/256', 'abc') . "\n";
9+
echo hash('sha512/256', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu') . "\n";
10+
--EXPECT--
11+
c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a
12+
53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23
13+
3928e184fb8690f840da3988121d31be65cb9d3ef83ee6146feac861e19b563a

0 commit comments

Comments
 (0)