@@ -76,7 +76,7 @@ const php_hash_ops php_hash_sha1_ops = {
76
76
sizeof (PHP_SHA1_CTX )
77
77
};
78
78
79
- /* sha256 */
79
+ /* sha224/ sha256 */
80
80
81
81
const php_hash_ops php_hash_sha256_ops = {
82
82
(php_hash_init_func_t ) PHP_SHA256Init ,
@@ -88,6 +88,16 @@ const php_hash_ops php_hash_sha256_ops = {
88
88
sizeof (PHP_SHA256_CTX )
89
89
};
90
90
91
+ const php_hash_ops php_hash_sha224_ops = {
92
+ (php_hash_init_func_t ) PHP_SHA224Init ,
93
+ (php_hash_update_func_t ) PHP_SHA224Update ,
94
+ (php_hash_final_func_t ) PHP_SHA224Final ,
95
+ (php_hash_copy_func_t ) php_hash_copy ,
96
+ 28 ,
97
+ 64 ,
98
+ sizeof (PHP_SHA224_CTX )
99
+ };
100
+
91
101
#define ROTR32 (b ,x ) ((x >> b) | (x << (32 - b)))
92
102
#define ROTR64 (b ,x ) ((x >> b) | (x << (64 - b)))
93
103
#define SHR (b , x ) (x >> b)
@@ -175,6 +185,102 @@ static void SHA256Transform(php_hash_uint32 state[8], const unsigned char block[
175
185
}
176
186
/* }}} */
177
187
188
+ /* {{{ PHP_SHA224Init
189
+ * SHA224 initialization. Begins an SHA224 operation, writing a new context.
190
+ */
191
+ PHP_HASH_API void PHP_SHA224Init (PHP_SHA224_CTX * context )
192
+ {
193
+ context -> count [0 ] = context -> count [1 ] = 0 ;
194
+ /* Load magic initialization constants.
195
+ */
196
+ context -> state [0 ] = 0xc1059ed8 ;
197
+ context -> state [1 ] = 0x367cd507 ;
198
+ context -> state [2 ] = 0x3070dd17 ;
199
+ context -> state [3 ] = 0xf70e5939 ;
200
+ context -> state [4 ] = 0xffc00b31 ;
201
+ context -> state [5 ] = 0x68581511 ;
202
+ context -> state [6 ] = 0x64f98fa7 ;
203
+ context -> state [7 ] = 0xbefa4fa4 ;
204
+ }
205
+ /* }}} */
206
+
207
+ /* {{{ PHP_SHA224Update
208
+ SHA224 block update operation. Continues an SHA224 message-digest
209
+ operation, processing another message block, and updating the
210
+ context.
211
+ */
212
+ PHP_HASH_API void PHP_SHA224Update (PHP_SHA224_CTX * context , const unsigned char * input , unsigned int inputLen )
213
+ {
214
+ unsigned int i , index , partLen ;
215
+
216
+ /* Compute number of bytes mod 64 */
217
+ index = (unsigned int ) ((context -> count [0 ] >> 3 ) & 0x3F );
218
+
219
+ /* Update number of bits */
220
+ if ((context -> count [0 ] += ((php_hash_uint32 ) inputLen << 3 )) < ((php_hash_uint32 ) inputLen << 3 )) {
221
+ context -> count [1 ]++ ;
222
+ }
223
+ context -> count [1 ] += ((php_hash_uint32 ) inputLen >> 29 );
224
+
225
+ partLen = 64 - index ;
226
+
227
+ /* Transform as many times as possible.
228
+ */
229
+ if (inputLen >= partLen ) {
230
+ memcpy ((unsigned char * ) & context -> buffer [index ], (unsigned char * ) input , partLen );
231
+ SHA256Transform (context -> state , context -> buffer );
232
+
233
+ for (i = partLen ; i + 63 < inputLen ; i += 64 ) {
234
+ SHA256Transform (context -> state , & input [i ]);
235
+ }
236
+
237
+ index = 0 ;
238
+ } else {
239
+ i = 0 ;
240
+ }
241
+
242
+ /* Buffer remaining input */
243
+ memcpy ((unsigned char * ) & context -> buffer [index ], (unsigned char * ) & input [i ], inputLen - i );
244
+ }
245
+ /* }}} */
246
+
247
+ /* {{{ PHP_SHA224Final
248
+ SHA224 finalization. Ends an SHA224 message-digest operation, writing the
249
+ the message digest and zeroizing the context.
250
+ */
251
+ PHP_HASH_API void PHP_SHA224Final (unsigned char digest [28 ], PHP_SHA224_CTX * context )
252
+ {
253
+ unsigned char bits [8 ];
254
+ unsigned int index , padLen ;
255
+
256
+ /* Save number of bits */
257
+ bits [7 ] = (unsigned char ) (context -> count [0 ] & 0xFF );
258
+ bits [6 ] = (unsigned char ) ((context -> count [0 ] >> 8 ) & 0xFF );
259
+ bits [5 ] = (unsigned char ) ((context -> count [0 ] >> 16 ) & 0xFF );
260
+ bits [4 ] = (unsigned char ) ((context -> count [0 ] >> 24 ) & 0xFF );
261
+ bits [3 ] = (unsigned char ) (context -> count [1 ] & 0xFF );
262
+ bits [2 ] = (unsigned char ) ((context -> count [1 ] >> 8 ) & 0xFF );
263
+ bits [1 ] = (unsigned char ) ((context -> count [1 ] >> 16 ) & 0xFF );
264
+ bits [0 ] = (unsigned char ) ((context -> count [1 ] >> 24 ) & 0xFF );
265
+
266
+ /* Pad out to 56 mod 64.
267
+ */
268
+ index = (unsigned int ) ((context -> count [0 ] >> 3 ) & 0x3f );
269
+ padLen = (index < 56 ) ? (56 - index ) : (120 - index );
270
+ PHP_SHA224Update (context , PADDING , padLen );
271
+
272
+ /* Append length (before padding) */
273
+ PHP_SHA224Update (context , bits , 8 );
274
+
275
+ /* Store state in digest */
276
+ SHAEncode32 (digest , context -> state , 28 );
277
+
278
+ /* Zeroize sensitive information.
279
+ */
280
+ memset ((unsigned char * ) context , 0 , sizeof (* context ));
281
+ }
282
+ /* }}} */
283
+
178
284
/* {{{ PHP_SHA256Update
179
285
SHA256 block update operation. Continues an SHA256 message-digest
180
286
operation, processing another message block, and updating the
@@ -544,10 +650,10 @@ PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX * context, const unsigned char
544
650
/* }}} */
545
651
546
652
/* {{{ PHP_SHA512Final
547
- SHA512 finalization. Ends an SHA384 message-digest operation, writing the
653
+ SHA512 finalization. Ends an SHA512 message-digest operation, writing the
548
654
the message digest and zeroizing the context.
549
655
*/
550
- PHP_HASH_API void PHP_SHA512Final (unsigned char digest [48 ], PHP_SHA512_CTX * context )
656
+ PHP_HASH_API void PHP_SHA512Final (unsigned char digest [64 ], PHP_SHA512_CTX * context )
551
657
{
552
658
unsigned char bits [16 ];
553
659
unsigned int index , padLen ;
0 commit comments