-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcrypto_hash.c
747 lines (647 loc) · 19.9 KB
/
crypto_hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2013-2016 Jakub Zelenka |
+----------------------------------------------------------------------+
| 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: Jakub Zelenka <bukka@php.net> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_crypto.h"
#include "php_crypto_hash.h"
#include "php_crypto_cipher.h"
#include "php_crypto_object.h"
#include "zend_exceptions.h"
#include "ext/standard/php_string.h"
#include <openssl/evp.h>
#include <openssl/hmac.h>
PHP_CRYPTO_EXCEPTION_DEFINE(Hash)
PHP_CRYPTO_ERROR_INFO_BEGIN(Hash)
PHP_CRYPTO_ERROR_INFO_ENTRY(
HASH_ALGORITHM_NOT_FOUND,
"Hash algorithm '%s' not found"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
STATIC_METHOD_NOT_FOUND,
"Hash static method '%s' not found"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
STATIC_METHOD_TOO_MANY_ARGS,
"Hash static method %s can accept max one argument"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
INIT_FAILED,
"Initialization of hash failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
UPDATE_FAILED,
"Updating of hash context failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
DIGEST_FAILED,
"Creating of hash digest failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
INPUT_DATA_LENGTH_HIGH,
"Input data length can't exceed max integer length"
)
PHP_CRYPTO_ERROR_INFO_END()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_hash_algorithm, 0)
ZEND_ARG_INFO(0, algorithm)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_hash_data, 0)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_crypto_hash_list, 0, 0, 0)
ZEND_ARG_INFO(0, aliases)
ZEND_ARG_INFO(0, prefix)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_hash_static, 0)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, arguments)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_hash_no_args, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry php_crypto_hash_object_methods[] = {
PHP_CRYPTO_ME(
Hash, getAlgorithms,
arginfo_crypto_hash_list,
ZEND_ACC_STATIC|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, hasAlgorithm,
arginfo_crypto_hash_algorithm,
ZEND_ACC_STATIC|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, __callStatic,
arginfo_crypto_hash_static,
ZEND_ACC_STATIC|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, __construct,
arginfo_crypto_hash_algorithm,
ZEND_ACC_CTOR|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, update,
arginfo_crypto_hash_data,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, getAlgorithmName,
arginfo_crypto_hash_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, digest,
arginfo_crypto_hash_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, hexdigest,
arginfo_crypto_hash_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, getSize,
arginfo_crypto_hash_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Hash, getBlockSize,
arginfo_crypto_hash_no_args,
ZEND_ACC_PUBLIC
)
PHPC_FE_END
};
PHP_CRYPTO_EXCEPTION_DEFINE(MAC)
PHP_CRYPTO_ERROR_INFO_BEGIN(MAC)
PHP_CRYPTO_ERROR_INFO_ENTRY(
MAC_ALGORITHM_NOT_FOUND,
"MAC algorithm '%s' not found"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
KEY_LENGTH_INVALID,
"The key length for MAC is invalid"
)
PHP_CRYPTO_ERROR_INFO_END()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_mac_construct, 0)
ZEND_ARG_INFO(0, algorithm)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
static const zend_function_entry php_crypto_mac_object_methods[] = {
PHP_CRYPTO_ME(
MAC, __construct,
arginfo_crypto_mac_construct,
ZEND_ACC_PUBLIC
)
PHPC_FE_END
};
/* class entries */
PHP_CRYPTO_API zend_class_entry *php_crypto_hash_ce;
PHP_CRYPTO_API zend_class_entry *php_crypto_mac_ce;
PHP_CRYPTO_API zend_class_entry *php_crypto_hmac_ce;
PHP_CRYPTO_API zend_class_entry *php_crypto_cmac_ce;
/* object handler */
PHPC_OBJ_DEFINE_HANDLER_VAR(crypto_hash);
/* algorithm name getter macros */
#define PHP_CRYPTO_HASH_GET_ALGORITHM_NAME_EX(this_object) \
PHPC_READ_PROPERTY(php_crypto_hash_ce, this_object, \
"algorithm", sizeof("algorithm")-1, 1)
#define PHP_CRYPTO_HASH_GET_ALGORITHM_NAME(this_object) \
Z_STRVAL_P(PHP_CRYPTO_HASH_GET_ALGORITHM_NAME_EX(this_object))
/* {{{ crypto_hash free object handler */
PHPC_OBJ_HANDLER_FREE(crypto_hash)
{
PHPC_OBJ_HANDLER_FREE_INIT(crypto_hash);
if (PHPC_THIS->type == PHP_CRYPTO_HASH_TYPE_MD) {
EVP_MD_CTX_destroy(PHP_CRYPTO_HASH_CTX(PHPC_THIS));
} else if (PHPC_THIS->type == PHP_CRYPTO_HASH_TYPE_HMAC) {
HMAC_CTX_free(PHP_CRYPTO_HMAC_CTX(PHPC_THIS));
}
else if (PHPC_THIS->type == PHP_CRYPTO_HASH_TYPE_CMAC) {
CMAC_CTX_free(PHP_CRYPTO_CMAC_CTX(PHPC_THIS));
}
if (PHPC_THIS->key) {
efree(PHPC_THIS->key);
}
PHPC_OBJ_HANDLER_FREE_DESTROY();
}
/* }}} */
/* {{{ crypto_hash create_ex object helper */
PHPC_OBJ_HANDLER_CREATE_EX(crypto_hash)
{
PHPC_OBJ_HANDLER_CREATE_EX_INIT(crypto_hash);
if (PHPC_CLASS_TYPE == php_crypto_hash_ce) {
PHPC_THIS->type = PHP_CRYPTO_HASH_TYPE_MD;
PHP_CRYPTO_HASH_CTX(PHPC_THIS) = EVP_MD_CTX_create();
} else if (PHPC_CLASS_TYPE == php_crypto_hmac_ce) {
PHPC_THIS->type = PHP_CRYPTO_HASH_TYPE_HMAC;
PHP_CRYPTO_HMAC_CTX(PHPC_THIS) = HMAC_CTX_new();
}
else if (PHPC_CLASS_TYPE == php_crypto_cmac_ce) {
PHPC_THIS->type = PHP_CRYPTO_HASH_TYPE_CMAC;
PHP_CRYPTO_CMAC_CTX(PHPC_THIS) = CMAC_CTX_new();
}
else {
PHPC_THIS->type = PHP_CRYPTO_HASH_TYPE_NONE;
}
PHPC_THIS->key = NULL;
PHPC_THIS->key_len = 0;
PHPC_OBJ_HANDLER_CREATE_EX_RETURN(crypto_hash);
}
/* }}} */
/* {{{ crypto_hash create object handler */
PHPC_OBJ_HANDLER_CREATE(crypto_hash)
{
PHPC_OBJ_HANDLER_CREATE_RETURN(crypto_hash);
}
/* }}} */
/* {{{ crypto_hash clone object handler */
PHPC_OBJ_HANDLER_CLONE(crypto_hash)
{
zend_bool copy_success;
PHPC_OBJ_HANDLER_CLONE_INIT(crypto_hash);
PHPC_THAT->status = PHPC_THIS->status;
PHPC_THAT->type = PHPC_THIS->type;
if (PHPC_THIS->key) {
PHPC_THAT->key = emalloc(PHPC_THIS->key_len + 1);
memcpy(PHPC_THAT->key, PHPC_THIS->key, PHPC_THIS->key_len + 1);
PHPC_THAT->key_len = PHPC_THIS->key_len;
}
if (PHPC_THAT->type == PHP_CRYPTO_HASH_TYPE_MD) {
copy_success = EVP_MD_CTX_copy(
PHP_CRYPTO_HASH_CTX(PHPC_THAT), PHP_CRYPTO_HASH_CTX(PHPC_THIS));
PHP_CRYPTO_HASH_ALG(PHPC_THAT) = EVP_MD_CTX_md(PHP_CRYPTO_HASH_CTX(PHPC_THIS));
} else if (PHPC_THAT->type == PHP_CRYPTO_HASH_TYPE_HMAC) {
copy_success = HMAC_CTX_copy(
PHP_CRYPTO_HMAC_CTX(PHPC_THAT), PHP_CRYPTO_HMAC_CTX(PHPC_THIS));
}
else if (PHPC_THAT->type == PHP_CRYPTO_HASH_TYPE_CMAC) {
copy_success = CMAC_CTX_copy(
PHP_CRYPTO_CMAC_CTX(PHPC_THAT), PHP_CRYPTO_CMAC_CTX(PHPC_THIS));
}
else {
copy_success = 0;
}
if (!copy_success) {
php_error(E_ERROR, "Cloning of Hash object failed");
}
PHPC_OBJ_HANDLER_CLONE_RETURN();
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(crypto_hash)
{
zend_class_entry ce;
/* Hash class */
INIT_CLASS_ENTRY(ce, PHP_CRYPTO_CLASS_NAME(Hash), php_crypto_hash_object_methods);
PHPC_CLASS_SET_HANDLER_CREATE(ce, crypto_hash);
php_crypto_hash_ce = PHPC_CLASS_REGISTER(ce);
PHPC_OBJ_INIT_HANDLERS(crypto_hash);
PHPC_OBJ_SET_HANDLER_OFFSET(crypto_hash);
PHPC_OBJ_SET_HANDLER_FREE(crypto_hash);
PHPC_OBJ_SET_HANDLER_CLONE(crypto_hash);
zend_declare_property_null(php_crypto_hash_ce,
"algorithm", sizeof("algorithm")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
/* HashException registration */
PHP_CRYPTO_EXCEPTION_REGISTER(ce, Hash);
PHP_CRYPTO_ERROR_INFO_REGISTER(Hash);
/* MAC class */
INIT_CLASS_ENTRY(ce, PHP_CRYPTO_CLASS_NAME(MAC), php_crypto_mac_object_methods);
php_crypto_mac_ce = PHPC_CLASS_REGISTER_EX(ce, php_crypto_hash_ce, NULL);
php_crypto_mac_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
/* MACException registration */
PHP_CRYPTO_EXCEPTION_REGISTER_EX(ce, MAC, Hash);
PHP_CRYPTO_ERROR_INFO_REGISTER(MAC);
/* HMAC class */
INIT_CLASS_ENTRY(ce, PHP_CRYPTO_CLASS_NAME(HMAC), NULL);
php_crypto_hmac_ce = PHPC_CLASS_REGISTER_EX(ce, php_crypto_mac_ce, NULL);
/* CMAC class */
INIT_CLASS_ENTRY(ce, PHP_CRYPTO_CLASS_NAME(CMAC), NULL);
php_crypto_cmac_ce = PHPC_CLASS_REGISTER_EX(ce, php_crypto_mac_ce, NULL);
return SUCCESS;
}
/* }}} */
/* METHODS */
/* {{{ php_crypto_hash_set_algorithm_name */
static inline void php_crypto_hash_set_algorithm_name(zval *object,
char *algorithm, phpc_str_size_t algorithm_len TSRMLS_DC)
{
php_strtoupper(algorithm, algorithm_len);
zend_update_property_stringl(php_crypto_hash_ce, PHPC_OBJ_FOR_PROP(object),
"algorithm", sizeof("algorithm")-1, algorithm, algorithm_len TSRMLS_CC);
}
/* }}} */
/* {{{ php_crypto_hash_bin2hex */
PHP_CRYPTO_API void php_crypto_hash_bin2hex(char *out, const unsigned char *in, unsigned in_len)
{
static const char hexits[17] = "0123456789abcdef";
unsigned i;
for (i = 0; i < in_len; i++) {
out[i * 2] = hexits[in[i] >> 4];
out[(i * 2) + 1] = hexits[in[i] & 0x0F];
}
out[i * 2] = 0;
}
/* }}} */
/* {{{ php_crypto_hash_init */
static inline int php_crypto_hash_init(PHPC_THIS_DECLARE(crypto_hash) TSRMLS_DC)
{
int rc;
if (PHPC_THIS->type == PHP_CRYPTO_HASH_TYPE_MD) {
rc = EVP_DigestInit_ex(PHP_CRYPTO_HASH_CTX(PHPC_THIS),
PHP_CRYPTO_HASH_ALG(PHPC_THIS), NULL);
} else {
/* It is a MAC instance and the key is required */
if (!PHPC_THIS->key) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Hash, INIT_FAILED));
return FAILURE;
}
/* update hash context */
switch (PHPC_THIS->type) {
case PHP_CRYPTO_HASH_TYPE_HMAC:
rc = HMAC_Init_ex(PHP_CRYPTO_HMAC_CTX(PHPC_THIS), PHPC_THIS->key,
PHPC_THIS->key_len, PHP_CRYPTO_HMAC_ALG(PHPC_THIS), NULL);
break;
case PHP_CRYPTO_HASH_TYPE_CMAC:
rc = CMAC_Init(PHP_CRYPTO_CMAC_CTX(PHPC_THIS),
PHPC_THIS->key, PHPC_THIS->key_len,
PHP_CRYPTO_CMAC_ALG(PHPC_THIS), NULL);
break;
default:
rc = 0;
}
}
/* initialize hash */
if (!rc) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Hash, INIT_FAILED));
return FAILURE;
}
PHPC_THIS->status = PHP_CRYPTO_HASH_STATUS_HASH;
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_hash_update */
static inline int php_crypto_hash_update(PHPC_THIS_DECLARE(crypto_hash),
char *data, phpc_str_size_t data_len TSRMLS_DC)
{
int rc;
/* check if hash is initialized and if it's not, then try to initialize */
if (PHPC_THIS->status != PHP_CRYPTO_HASH_STATUS_HASH &&
php_crypto_hash_init(PHPC_THIS TSRMLS_CC) == FAILURE) {
return FAILURE;
}
/* update hash context */
switch (PHPC_THIS->type) {
case PHP_CRYPTO_HASH_TYPE_MD:
rc = EVP_DigestUpdate(PHP_CRYPTO_HASH_CTX(PHPC_THIS), data, data_len);
break;
case PHP_CRYPTO_HASH_TYPE_HMAC:
rc = HMAC_Update(PHP_CRYPTO_HMAC_CTX(PHPC_THIS), (unsigned char *) data, data_len);
break;
case PHP_CRYPTO_HASH_TYPE_CMAC:
rc = CMAC_Update(PHP_CRYPTO_CMAC_CTX(PHPC_THIS), data, data_len);
break;
default:
rc = 0;
}
if (!rc) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Hash, UPDATE_FAILED));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_hash_digest */
static inline void php_crypto_hash_digest(INTERNAL_FUNCTION_PARAMETERS, int encode_to_hex)
{
PHPC_THIS_DECLARE(crypto_hash);
PHPC_STR_DECLARE(hash);
unsigned char hash_value[EVP_MAX_MD_SIZE + 1];
unsigned int hash_len;
size_t hash_len_size;
int rc;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
PHPC_THIS_FETCH(crypto_hash);
/* check if hash is initialized and if it's not, then try to initialize */
if (PHPC_THIS->status != PHP_CRYPTO_HASH_STATUS_HASH &&
php_crypto_hash_init(PHPC_THIS TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
/* finalize hash context */
switch (PHPC_THIS->type) {
case PHP_CRYPTO_HASH_TYPE_MD:
rc = EVP_DigestFinal(PHP_CRYPTO_HASH_CTX(PHPC_THIS), hash_value, &hash_len);
break;
case PHP_CRYPTO_HASH_TYPE_HMAC:
rc = HMAC_Final(PHP_CRYPTO_HMAC_CTX(PHPC_THIS), hash_value, &hash_len);
break;
case PHP_CRYPTO_HASH_TYPE_CMAC:
rc = CMAC_Final(PHP_CRYPTO_CMAC_CTX(PHPC_THIS), hash_value, &hash_len_size);
/* this is safe because the hash_len_size is always really small */
hash_len = hash_len_size;
break;
default:
rc = 0;
}
if (!rc) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Hash, DIGEST_FAILED));
RETURN_FALSE;
}
hash_value[hash_len] = 0;
PHPC_THIS->status = PHP_CRYPTO_HASH_STATUS_CLEAR;
if (encode_to_hex) {
unsigned int hash_hex_len = hash_len * 2;
PHPC_STR_ALLOC(hash, hash_hex_len);
php_crypto_hash_bin2hex(PHPC_STR_VAL(hash), hash_value, hash_len);
} else {
PHPC_STR_INIT(hash, (char *) hash_value, hash_len);
}
PHPC_STR_RETURN(hash);
}
/* }}} */
/* {{{ proto static string Crypto\Hash::getAlgorithms(bool $aliases = false,
string $prefix = null)
Returns hash algorithms */
PHP_CRYPTO_METHOD(Hash, getAlgorithms)
{
php_crypto_object_fn_get_names(INTERNAL_FUNCTION_PARAM_PASSTHRU, OBJ_NAME_TYPE_MD_METH);
}
/* }}} */
/* {{{ proto static bool Crypto\Hash::hasAlgorithm(string $algorithm)
Finds out whether algorithm exists */
PHP_CRYPTO_METHOD(Hash, hasAlgorithm)
{
char *algorithm;
phpc_str_size_t algorithm_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&algorithm, &algorithm_len) == FAILURE) {
return;
}
if (EVP_get_digestbyname(algorithm)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto static Crypto\Hash::__callStatic(string $name, array $arguments)
Hash magic method for calling static methods */
PHP_CRYPTO_METHOD(Hash, __callStatic)
{
char *algorithm;
phpc_str_size_t algorithm_len;
int argc;
zval *args, *pz_arg;
phpc_val *ppv_arg;
const EVP_MD *digest;
PHPC_THIS_DECLARE(crypto_hash);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa",
&algorithm, &algorithm_len, &args) == FAILURE) {
return;
}
argc = PHPC_HASH_NUM_ELEMENTS(Z_ARRVAL_P(args));
if (argc > 1) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Hash, STATIC_METHOD_TOO_MANY_ARGS), algorithm);
RETURN_FALSE;
}
digest = EVP_get_digestbyname(algorithm);
if (!digest) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Hash, STATIC_METHOD_NOT_FOUND), algorithm);
RETURN_FALSE;
}
object_init_ex(return_value, php_crypto_hash_ce);
php_crypto_hash_set_algorithm_name(return_value, algorithm, algorithm_len TSRMLS_CC);
PHPC_THIS_FETCH_FROM_ZVAL(crypto_hash, return_value);
PHP_CRYPTO_HASH_ALG(PHPC_THIS) = digest;
if (argc == 1) {
PHPC_HASH_INTERNAL_POINTER_RESET(Z_ARRVAL_P(args));
PHPC_HASH_GET_CURRENT_DATA(Z_ARRVAL_P(args), ppv_arg);
convert_to_string_ex(ppv_arg);
PHPC_PVAL_TO_PZVAL(ppv_arg, pz_arg);
if (php_crypto_hash_update(PHPC_THIS,
Z_STRVAL_P(pz_arg), Z_STRLEN_P(pz_arg) TSRMLS_CC) == FAILURE) {
RETURN_NULL();
}
}
}
/* }}} */
/* {{{ proto Crypto\Hash::__construct(string $algorithm)
Hash constructor */
PHP_CRYPTO_METHOD(Hash, __construct)
{
PHPC_THIS_DECLARE(crypto_hash);
char *algorithm, *algorithm_uc;
phpc_str_size_t algorithm_len;
const EVP_MD *digest;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
&algorithm, &algorithm_len) == FAILURE) {
return;
}
algorithm_uc = estrdup(algorithm);
php_crypto_hash_set_algorithm_name(getThis(), algorithm_uc, strlen(algorithm_uc) TSRMLS_CC);
PHPC_THIS_FETCH(crypto_hash);
digest = EVP_get_digestbyname(algorithm);
if (!digest) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Hash, HASH_ALGORITHM_NOT_FOUND), algorithm);
} else {
PHP_CRYPTO_HASH_ALG(PHPC_THIS) = digest;
}
efree(algorithm_uc);
}
/* }}} */
/* {{{ proto string Crypto\Hash::getAlgorithmName()
Returns hash algorithm string */
PHP_CRYPTO_METHOD(Hash, getAlgorithmName)
{
zval *algorithm;
PHPC_READ_PROPERTY_RV_DECLARE;
algorithm = PHP_CRYPTO_HASH_GET_ALGORITHM_NAME_EX(getThis());
RETURN_ZVAL(algorithm, 1, 0);
}
/* }}} */
/* {{{ proto void Crypto\Hash::update(string $data)
Updates hash */
PHP_CRYPTO_METHOD(Hash, update)
{
PHPC_THIS_DECLARE(crypto_hash);
char *data;
phpc_str_size_t data_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) {
return;
}
PHPC_THIS_FETCH(crypto_hash);
php_crypto_hash_update(PHPC_THIS, data, data_len TSRMLS_CC);
ZVAL_ZVAL(return_value, getThis(), 1, 0);
}
/* }}} */
/* {{{ proto string Crypto\Hash::digest()
Return hash digest in raw foramt */
PHP_CRYPTO_METHOD(Hash, digest)
{
php_crypto_hash_digest(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto string Crypto\Hash::hexdigest()
Return hash digest in hex format */
PHP_CRYPTO_METHOD(Hash, hexdigest)
{
php_crypto_hash_digest(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto int Crypto\Hash::getBlockSize()
Returns hash block size */
PHP_CRYPTO_METHOD(Hash, getBlockSize)
{
int block_size;
PHPC_THIS_DECLARE(crypto_hash);
if (zend_parse_parameters_none() == FAILURE) {
return;
}
PHPC_THIS_FETCH(crypto_hash);
/* find out block size */
switch (PHPC_THIS->type) {
case PHP_CRYPTO_HASH_TYPE_MD:
block_size = EVP_MD_block_size(PHP_CRYPTO_HASH_ALG(PHPC_THIS));
break;
case PHP_CRYPTO_HASH_TYPE_HMAC:
block_size = EVP_MD_block_size(PHP_CRYPTO_HMAC_ALG(PHPC_THIS));
break;
case PHP_CRYPTO_HASH_TYPE_CMAC:
block_size = EVP_CIPHER_block_size(PHP_CRYPTO_CMAC_ALG(PHPC_THIS));
break;
default:
block_size = 0;
}
RETURN_LONG(block_size);
}
/* {{{ proto int Crypto\Hash::getSize()
Returns hash size */
PHP_CRYPTO_METHOD(Hash, getSize)
{
int hash_size;
PHPC_THIS_DECLARE(crypto_hash);
if (zend_parse_parameters_none() == FAILURE) {
return;
}
PHPC_THIS_FETCH(crypto_hash);
/* find out block size */
switch (PHPC_THIS->type) {
case PHP_CRYPTO_HASH_TYPE_MD:
hash_size = EVP_MD_size(PHP_CRYPTO_HASH_ALG(PHPC_THIS));
break;
case PHP_CRYPTO_HASH_TYPE_HMAC:
hash_size = EVP_MD_size(PHP_CRYPTO_HMAC_ALG(PHPC_THIS));
break;
case PHP_CRYPTO_HASH_TYPE_CMAC:
hash_size = EVP_CIPHER_block_size(PHP_CRYPTO_CMAC_ALG(PHPC_THIS));
break;
default:
hash_size = 0;
}
RETURN_LONG(hash_size);
}
/* {{{ proto Crypto\MAC::__construct(string $algorithm, string $key)
Create a MAC (used by MAC subclasses - HMAC and CMAC) */
PHP_CRYPTO_METHOD(MAC, __construct)
{
PHPC_THIS_DECLARE(crypto_hash);
char *algorithm, *algorithm_uc, *key;
phpc_str_size_t algorithm_len, key_len;
int key_len_int;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
&key, &key_len, &algorithm, &algorithm_len) == FAILURE) {
return;
}
algorithm_uc = estrdup(algorithm);
algorithm_len = strlen(algorithm_uc);
php_crypto_hash_set_algorithm_name(getThis(), algorithm_uc, algorithm_len TSRMLS_CC);
PHPC_THIS_FETCH(crypto_hash);
if (PHPC_THIS->type == PHP_CRYPTO_HASH_TYPE_HMAC) {
const EVP_MD *digest = EVP_get_digestbyname(algorithm_uc);
if (!digest) {
goto php_crypto_mac_alg_not_found;
}
PHP_CRYPTO_HMAC_ALG(PHPC_THIS) = digest;
}
/* CMAC algorithm uses a cipher algorithm */
else if (PHPC_THIS->type == PHP_CRYPTO_HASH_TYPE_CMAC) {
const EVP_CIPHER *cipher = php_crypto_get_cipher_algorithm(algorithm_uc, algorithm_len);
if (!cipher) {
goto php_crypto_mac_alg_not_found;
}
if (key_len != EVP_CIPHER_block_size(cipher)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(MAC, KEY_LENGTH_INVALID));
efree(algorithm_uc);
return;
}
PHP_CRYPTO_CMAC_ALG(PHPC_THIS) = cipher;
}
efree(algorithm_uc);
/* check key length overflow */
if (php_crypto_str_size_to_int(key_len, &key_len_int) == FAILURE) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(MAC, KEY_LENGTH_INVALID));
return;
}
PHPC_THIS->key = emalloc(key_len + 1);
memcpy(PHPC_THIS->key, key, key_len);
PHPC_THIS->key[key_len] = '\0';
PHPC_THIS->key_len = key_len_int;
return;
php_crypto_mac_alg_not_found:
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(MAC, MAC_ALGORITHM_NOT_FOUND), algorithm);
efree(algorithm_uc);
}