-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcrypto_cipher.c
1507 lines (1321 loc) · 42.6 KB
/
crypto_cipher.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| 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> |
+----------------------------------------------------------------------+
*/
#define PHPC_SMART_CSTR_INCLUDE 1
#include "php.h"
#include "php_crypto.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>
/* ERRORS */
PHP_CRYPTO_EXCEPTION_DEFINE(Cipher)
PHP_CRYPTO_ERROR_INFO_BEGIN(Cipher)
PHP_CRYPTO_ERROR_INFO_ENTRY(
ALGORITHM_NOT_FOUND,
"Cipher '%s' algorithm not found"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
STATIC_METHOD_NOT_FOUND,
"Cipher static method '%s' not found"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
STATIC_METHOD_TOO_MANY_ARGS,
"Cipher static method %s can accept max two arguments"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
MODE_NOT_FOUND,
"Cipher mode not found"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
MODE_NOT_AVAILABLE,
"Cipher mode %s is not available in installed OpenSSL library"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
AUTHENTICATION_NOT_SUPPORTED,
"The authentication is not supported for %s cipher mode"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
KEY_LENGTH_INVALID,
"Invalid length of key for cipher '%s' algorithm "
"(required length: %d)"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
IV_LENGTH_INVALID,
"Invalid length of initial vector for cipher '%s' algorithm "
"(required length: %d)"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
AAD_SETTER_FORBIDDEN,
"AAD setter has to be called before encryption or decryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
AAD_SETTER_FAILED,
"AAD setter failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
AAD_LENGTH_HIGH,
"AAD length can't exceed max integer length"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_GETTER_FORBIDDEN,
"Tag getter has to be called after encryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_SETTER_FORBIDDEN,
"Tag setter has to be called before decryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_GETTER_FAILED,
"Tag getter failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_SETTER_FAILED,
"Tag setter failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_LENGTH_SETTER_FORBIDDEN,
"Tag length setter has to be called before encryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_LENGTH_LOW,
"Tag length can't be lower than 32 bits (4 characters)"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_LENGTH_HIGH,
"Tag length can't exceed 128 bits (16 characters)"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
TAG_VERIFY_FAILED,
"Tag verification failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
INIT_ALG_FAILED,
"Initialization of cipher algorithm failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
INIT_CTX_FAILED,
"Initialization of cipher context failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
INIT_ENCRYPT_FORBIDDEN,
"Cipher object is already used for decryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
INIT_DECRYPT_FORBIDDEN,
"Cipher object is already used for encryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
UPDATE_FAILED,
"Updating of cipher failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
UPDATE_ENCRYPT_FORBIDDEN,
"Cipher object is not initialized for encryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
UPDATE_DECRYPT_FORBIDDEN,
"Cipher object is not initialized for decryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
FINISH_FAILED,
"Finalizing of cipher failed"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
FINISH_ENCRYPT_FORBIDDEN,
"Cipher object is not initialized for encryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
FINISH_DECRYPT_FORBIDDEN,
"Cipher object is not initialized for decryption"
)
PHP_CRYPTO_ERROR_INFO_ENTRY(
INPUT_DATA_LENGTH_HIGH,
"Input data length can't exceed max integer length"
)
PHP_CRYPTO_ERROR_INFO_END()
/* ARG INFOS */
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_algorithm, 0)
ZEND_ARG_INFO(0, algorithm)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_data, 0)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_crypto_cipher_list, 0, 0, 0)
ZEND_ARG_INFO(0, aliases)
ZEND_ARG_INFO(0, prefix)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_static, 0)
ZEND_ARG_INFO(0, name)
ZEND_ARG_INFO(0, arguments)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_crypto_cipher_construct, 0, 0, 1)
ZEND_ARG_INFO(0, algorithm)
ZEND_ARG_INFO(0, mode)
ZEND_ARG_INFO(0, key_size)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_crypto_cipher_init, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, iv)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_mode, 0)
ZEND_ARG_INFO(0, mode)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_set_tag_len, 0)
ZEND_ARG_INFO(0, tag_length)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_set_tag, 0)
ZEND_ARG_INFO(0, tag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_set_aad, 0)
ZEND_ARG_INFO(0, aad)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_crypto_cipher_crypt, 0, 0, 2)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, iv)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_crypto_cipher_no_args, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry php_crypto_cipher_object_methods[] = {
PHP_CRYPTO_ME(
Cipher, getAlgorithms,
arginfo_crypto_cipher_list,
ZEND_ACC_STATIC|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, hasAlgorithm,
arginfo_crypto_cipher_algorithm,
ZEND_ACC_STATIC|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, hasMode,
arginfo_crypto_cipher_mode,
ZEND_ACC_STATIC|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, __callStatic,
arginfo_crypto_cipher_static,
ZEND_ACC_STATIC|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, __construct,
arginfo_crypto_cipher_construct,
ZEND_ACC_CTOR|ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, getAlgorithmName,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, encryptInit,
arginfo_crypto_cipher_init,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, encryptUpdate,
arginfo_crypto_cipher_data,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, encryptFinish,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, encrypt,
arginfo_crypto_cipher_crypt,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, decryptInit,
arginfo_crypto_cipher_init,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, decryptUpdate,
arginfo_crypto_cipher_data,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, decryptFinish,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, decrypt,
arginfo_crypto_cipher_crypt,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, getBlockSize,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, getKeyLength,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, getIVLength,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, getMode,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, getTag,
arginfo_crypto_cipher_no_args,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, setTag,
arginfo_crypto_cipher_set_tag,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, setTagLength,
arginfo_crypto_cipher_set_tag_len,
ZEND_ACC_PUBLIC
)
PHP_CRYPTO_ME(
Cipher, setAAD,
arginfo_crypto_cipher_set_aad,
ZEND_ACC_PUBLIC
)
PHPC_FE_END
};
/* cipher modes lookup table */
static const php_crypto_cipher_mode php_crypto_cipher_modes[] = {
PHP_CRYPTO_CIPHER_MODE_ENTRY(ECB)
PHP_CRYPTO_CIPHER_MODE_ENTRY(CBC)
PHP_CRYPTO_CIPHER_MODE_ENTRY(CFB)
PHP_CRYPTO_CIPHER_MODE_ENTRY(OFB)
#ifdef EVP_CIPH_CTR_MODE
PHP_CRYPTO_CIPHER_MODE_ENTRY(CTR)
#else
PHP_CRYPTO_CIPHER_MODE_ENTRY_NOT_DEFINED(CTR)
#endif
#ifdef EVP_CIPH_GCM_MODE
PHP_CRYPTO_CIPHER_MODE_ENTRY_EX(GCM, 1, 0,
EVP_CTRL_GCM_SET_IVLEN,
EVP_CTRL_GCM_SET_TAG, EVP_CTRL_GCM_GET_TAG)
#else
PHP_CRYPTO_CIPHER_MODE_ENTRY_NOT_DEFINED(GCM)
#endif
#ifdef EVP_CIPH_CCM_MODE
PHP_CRYPTO_CIPHER_MODE_ENTRY_EX(CCM, 1, 1,
EVP_CTRL_CCM_SET_IVLEN,
EVP_CTRL_CCM_SET_TAG, EVP_CTRL_CCM_GET_TAG)
#else
PHP_CRYPTO_CIPHER_MODE_ENTRY_NOT_DEFINED(CCM)
#endif
#ifdef EVP_CIPH_XTS_MODE
PHP_CRYPTO_CIPHER_MODE_ENTRY(XTS)
#else
PHP_CRYPTO_CIPHER_MODE_ENTRY_NOT_DEFINED(XTS)
#endif
PHP_CRYPTO_CIPHER_MODE_ENTRY_END
};
/* class entry */
PHP_CRYPTO_API zend_class_entry *php_crypto_cipher_ce;
/* object handler */
PHPC_OBJ_DEFINE_HANDLER_VAR(crypto_cipher);
/* algorithm name getter macros */
#define PHP_CRYPTO_CIPHER_GET_ALGORITHM_NAME_EX(this_object) \
PHPC_READ_PROPERTY(php_crypto_cipher_ce, this_object, \
"algorithm", sizeof("algorithm")-1, 1)
#define PHP_CRYPTO_CIPHER_GET_ALGORITHM_NAME(this_object) \
Z_STRVAL_P(PHP_CRYPTO_CIPHER_GET_ALGORITHM_NAME_EX(this_object))
/* {{{ crypto_cipher free object handler */
PHPC_OBJ_HANDLER_FREE(crypto_cipher)
{
PHPC_OBJ_HANDLER_FREE_INIT(crypto_cipher);
EVP_CIPHER_CTX_free(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS));
if (PHP_CRYPTO_CIPHER_AAD(PHPC_THIS)) {
efree(PHP_CRYPTO_CIPHER_AAD(PHPC_THIS));
}
if (PHP_CRYPTO_CIPHER_TAG(PHPC_THIS)) {
efree(PHP_CRYPTO_CIPHER_TAG(PHPC_THIS));
}
PHPC_OBJ_HANDLER_FREE_DESTROY();
}
/* }}} */
/* {{{ crypto_cipher create_ex object helper */
PHPC_OBJ_HANDLER_CREATE_EX(crypto_cipher)
{
PHPC_OBJ_HANDLER_CREATE_EX_INIT(crypto_cipher);
PHP_CRYPTO_CIPHER_CTX(PHPC_THIS) = EVP_CIPHER_CTX_new();
if (!PHP_CRYPTO_CIPHER_CTX(PHPC_THIS)) {
php_error(E_ERROR, "Creating Cipher object failed");
}
PHP_CRYPTO_CIPHER_AAD(PHPC_THIS) = NULL;
PHP_CRYPTO_CIPHER_AAD_LEN(PHPC_THIS) = 0;
PHP_CRYPTO_CIPHER_TAG(PHPC_THIS) = NULL;
/* this is a default len for the tag */
PHP_CRYPTO_CIPHER_TAG_LEN(PHPC_THIS) =
PHP_CRYPTO_CIPHER_AUTH_TAG_LENGTH_DEFAULT;
PHPC_OBJ_HANDLER_CREATE_EX_RETURN(crypto_cipher);
}
/* }}} */
/* {{{ crypto_cipher create object handler */
PHPC_OBJ_HANDLER_CREATE(crypto_cipher)
{
PHPC_OBJ_HANDLER_CREATE_RETURN(crypto_cipher);
}
/* }}} */
/* {{{ crypto_cipher clone object handler */
PHPC_OBJ_HANDLER_CLONE(crypto_cipher)
{
zend_bool copy_success;
PHPC_OBJ_HANDLER_CLONE_INIT(crypto_cipher);
PHPC_THAT->status = PHPC_THIS->status;
if (PHP_CRYPTO_CIPHER_TAG(PHPC_THIS)) {
PHP_CRYPTO_CIPHER_TAG(PHPC_THAT) = emalloc(
PHP_CRYPTO_CIPHER_TAG_LEN(PHPC_THIS));
memcpy(PHP_CRYPTO_CIPHER_TAG(PHPC_THAT),
PHP_CRYPTO_CIPHER_TAG(PHPC_THIS),
PHP_CRYPTO_CIPHER_TAG_LEN(PHPC_THIS));
PHP_CRYPTO_CIPHER_TAG_LEN(PHPC_THAT) =
PHP_CRYPTO_CIPHER_TAG_LEN(PHPC_THIS);
}
if (PHP_CRYPTO_CIPHER_AAD(PHPC_THIS)) {
PHP_CRYPTO_CIPHER_AAD(PHPC_THIS) = emalloc(
PHP_CRYPTO_CIPHER_AAD_LEN(PHPC_THIS));
memcpy(PHP_CRYPTO_CIPHER_AAD(PHPC_THAT),
PHP_CRYPTO_CIPHER_AAD(PHPC_THIS),
PHP_CRYPTO_CIPHER_AAD_LEN(PHPC_THIS));
PHP_CRYPTO_CIPHER_AAD_LEN(PHPC_THAT) =
PHP_CRYPTO_CIPHER_AAD_LEN(PHPC_THIS);
}
copy_success = EVP_CIPHER_CTX_copy(
PHP_CRYPTO_CIPHER_CTX(PHPC_THAT),
PHP_CRYPTO_CIPHER_CTX(PHPC_THIS));
PHP_CRYPTO_CIPHER_ALG(PHPC_THAT) = EVP_CIPHER_CTX_cipher(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS));
if (!copy_success) {
php_error(E_ERROR, "Cloning of Cipher object failed");
}
PHPC_OBJ_HANDLER_CLONE_RETURN();
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(crypto_cipher)
{
zend_class_entry ce;
const php_crypto_cipher_mode *mode;
/* CipherException registration */
PHP_CRYPTO_EXCEPTION_REGISTER(ce, Cipher);
PHP_CRYPTO_ERROR_INFO_REGISTER(Cipher);
/* Cipher class */
INIT_CLASS_ENTRY(ce, PHP_CRYPTO_CLASS_NAME(Cipher), php_crypto_cipher_object_methods);
PHPC_CLASS_SET_HANDLER_CREATE(ce, crypto_cipher);
php_crypto_cipher_ce = PHPC_CLASS_REGISTER(ce);
PHPC_OBJ_INIT_HANDLERS(crypto_cipher);
PHPC_OBJ_SET_HANDLER_OFFSET(crypto_cipher);
PHPC_OBJ_SET_HANDLER_FREE(crypto_cipher);
PHPC_OBJ_SET_HANDLER_CLONE(crypto_cipher);
zend_declare_property_null(php_crypto_cipher_ce,
"algorithm", sizeof("algorithm")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
/* Cipher constants for modes */
for (mode = php_crypto_cipher_modes; mode->name[0]; mode++) {
zend_declare_class_constant_long(php_crypto_cipher_ce,
mode->constant, strlen(mode->constant), mode->value TSRMLS_CC);
}
return SUCCESS;
}
/* }}} */
/* METHODS */
/* {{{ php_crypto_get_algorithm_object_ex */
static inline void php_crypto_cipher_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_cipher_ce, PHPC_OBJ_FOR_PROP(object),
"algorithm", sizeof("algorithm")-1, algorithm, algorithm_len TSRMLS_CC);
}
/* }}} */
/* {{{ php_crypto_get_cipher_algorithm */
PHP_CRYPTO_API const EVP_CIPHER *php_crypto_get_cipher_algorithm(
char *algorithm, phpc_str_size_t algorithm_len)
{
const EVP_CIPHER *cipher;
if (algorithm_len > PHP_CRYPTO_CIPHER_ALGORITHM_LEN_MAX) {
return NULL;
}
php_strtoupper(algorithm, algorithm_len);
cipher = EVP_get_cipherbyname(algorithm);
if (!cipher) {
php_strtolower(algorithm, algorithm_len);
cipher = EVP_get_cipherbyname(algorithm);
}
return cipher;
}
/* }}} */
/* {{{ php_crypto_get_cipher_algorithm_from_params_ex */
static const EVP_CIPHER *php_crypto_get_cipher_algorithm_from_params_ex(
zval *object, char *algorithm, phpc_str_size_t algorithm_len, zval *pz_mode,
zval *pz_key_size, zend_bool is_static TSRMLS_DC)
{
const EVP_CIPHER *cipher;
phpc_smart_cstr alg_buf = {0};
/* if mode is not set, then it is already contained in the algorithm string */
if (!pz_mode || Z_TYPE_P(pz_mode) == IS_NULL) {
cipher = php_crypto_get_cipher_algorithm(algorithm, algorithm_len);
if (!cipher) {
if (is_static) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, STATIC_METHOD_NOT_FOUND),
algorithm);
} else {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, ALGORITHM_NOT_FOUND),
algorithm);
}
} else if (object) {
php_crypto_cipher_set_algorithm_name(object, algorithm, algorithm_len TSRMLS_CC);
}
return cipher;
}
phpc_smart_cstr_appendl(&alg_buf, algorithm, algorithm_len);
phpc_smart_cstr_appendc(&alg_buf, '-');
/* copy key size if available */
if (pz_key_size && Z_TYPE_P(pz_key_size) != IS_NULL) {
if (Z_TYPE_P(pz_key_size) == IS_STRING) {
phpc_smart_cstr_appendl(&alg_buf, Z_STRVAL_P(pz_key_size), Z_STRLEN_P(pz_key_size));
} else {
zval z_key_size = *pz_key_size;
zval_copy_ctor(&z_key_size);
convert_to_string(&z_key_size);
phpc_smart_cstr_appendl(&alg_buf, Z_STRVAL(z_key_size), Z_STRLEN(z_key_size));
phpc_smart_cstr_appendc(&alg_buf, '-');
zval_dtor(&z_key_size);
}
}
/* copy mode */
if (Z_TYPE_P(pz_mode) == IS_LONG) {
const php_crypto_cipher_mode *mode = php_crypto_get_cipher_mode_ex(Z_LVAL_P(pz_mode));
if (!mode) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, MODE_NOT_FOUND));
phpc_smart_cstr_free(&alg_buf);
return NULL;
}
if (mode->value == PHP_CRYPTO_CIPHER_MODE_NOT_DEFINED) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, MODE_NOT_AVAILABLE), mode->name);
phpc_smart_cstr_free(&alg_buf);
return NULL;
}
phpc_smart_cstr_appendl(&alg_buf, mode->name, PHP_CRYPTO_CIPHER_MODE_LEN);
} else if (Z_TYPE_P(pz_mode) == IS_STRING) {
phpc_smart_cstr_appendl(&alg_buf, Z_STRVAL_P(pz_mode), Z_STRLEN_P(pz_mode));
} else {
zval z_mode = *pz_mode;
zval_copy_ctor(&z_mode);
convert_to_string(&z_mode);
phpc_smart_cstr_appendl(&alg_buf, Z_STRVAL(z_mode), Z_STRLEN(z_mode));
zval_dtor(&z_mode);
}
phpc_smart_cstr_0(&alg_buf);
cipher = php_crypto_get_cipher_algorithm(alg_buf.c, alg_buf.len);
if (!cipher) {
if (is_static) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, STATIC_METHOD_NOT_FOUND), alg_buf.c);
} else {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, ALGORITHM_NOT_FOUND), alg_buf.c);
}
} else if (object) {
php_crypto_cipher_set_algorithm_name(object, alg_buf.c, alg_buf.len TSRMLS_CC);
}
phpc_smart_cstr_free(&alg_buf);
return cipher;
}
/* }}} */
/* {{{ php_crypto_get_cipher_algorithm_from_params */
PHP_CRYPTO_API const EVP_CIPHER *php_crypto_get_cipher_algorithm_from_params(
char *algorithm, phpc_str_size_t algorithm_len, zval *pz_mode, zval *pz_key_size TSRMLS_DC)
{
return php_crypto_get_cipher_algorithm_from_params_ex(
NULL, algorithm, algorithm_len, pz_mode, pz_key_size, 0 TSRMLS_CC);
}
/* }}} */
/* {{{ php_crypto_set_cipher_algorithm_ex */
static int php_crypto_set_cipher_algorithm_ex(PHPC_THIS_DECLARE(crypto_cipher),
char *algorithm, phpc_str_size_t algorithm_len TSRMLS_DC)
{
const EVP_CIPHER *cipher = php_crypto_get_cipher_algorithm(algorithm, algorithm_len);
if (!cipher) {
return FAILURE;
}
PHP_CRYPTO_CIPHER_ALG(PHPC_THIS) = cipher;
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_set_cipher_algorithm */
static int php_crypto_set_cipher_algorithm(zval *object,
char *algorithm, phpc_str_size_t algorithm_len TSRMLS_DC)
{
PHPC_THIS_DECLARE_AND_FETCH_FROM_ZVAL(crypto_cipher, object);
php_crypto_cipher_set_algorithm_name(object, algorithm, algorithm_len TSRMLS_CC);
return php_crypto_set_cipher_algorithm_ex(PHPC_THIS, algorithm, algorithm_len TSRMLS_CC);
}
/* }}} */
/* {{{ php_crypto_set_cipher_algorithm_from_params_ex */
static int php_crypto_set_cipher_algorithm_from_params_ex(
zval *object, char *algorithm, phpc_str_size_t algorithm_len,
zval *pz_mode, zval *pz_key_size, zend_bool is_static TSRMLS_DC)
{
PHPC_THIS_DECLARE_AND_FETCH_FROM_ZVAL(crypto_cipher, object);
const EVP_CIPHER *cipher = php_crypto_get_cipher_algorithm_from_params_ex(
object, algorithm, algorithm_len, pz_mode, pz_key_size, is_static TSRMLS_CC);
if (!cipher) {
return FAILURE;
}
PHP_CRYPTO_CIPHER_ALG(PHPC_THIS) = cipher;
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_set_cipher_algorithm_from_params */
static int php_crypto_set_cipher_algorithm_from_params(
zval *object, char *algorithm, phpc_str_size_t algorithm_len,
zval *pz_mode, zval *pz_key_size TSRMLS_DC)
{
return php_crypto_set_cipher_algorithm_from_params_ex(
object, algorithm, algorithm_len, pz_mode, pz_key_size, 0 TSRMLS_CC);
}
/* }}} */
/* {{{ php_crypto_get_cipher_mode_ex */
PHP_CRYPTO_API const php_crypto_cipher_mode *php_crypto_get_cipher_mode_ex(long mode_value)
{
const php_crypto_cipher_mode *mode;
for (mode = php_crypto_cipher_modes; mode->name[0]; mode++) {
if (mode_value == mode->value) {
return mode;
}
}
return NULL;
}
/* }}} */
/* {{{ php_crypto_get_cipher_mode_ex */
PHP_CRYPTO_API const php_crypto_cipher_mode *php_crypto_get_cipher_mode(const EVP_CIPHER *cipher)
{
return php_crypto_get_cipher_mode_ex(EVP_CIPHER_mode(cipher));
}
/* }}} */
/* {{{ php_crypto_cipher_is_mode_authenticated_ex */
static int php_crypto_cipher_is_mode_authenticated_ex(const php_crypto_cipher_mode *mode TSRMLS_DC)
{
if (!mode) { /* this should never happen */
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, MODE_NOT_FOUND));
return FAILURE;
}
if (!mode->auth_enc) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, AUTHENTICATION_NOT_SUPPORTED),
mode->name);
return FAILURE;
}
return SUCCESS;
}
/* {{{ php_crypto_cipher_is_mode_authenticated */
static int php_crypto_cipher_is_mode_authenticated(PHPC_THIS_DECLARE(crypto_cipher) TSRMLS_DC)
{
return php_crypto_cipher_is_mode_authenticated_ex(
php_crypto_get_cipher_mode_ex(PHP_CRYPTO_CIPHER_MODE_VALUE(PHPC_THIS)) TSRMLS_CC);
}
/* }}} */
/* {{{ php_crypto_cipher_set_tag */
PHP_CRYPTO_API int php_crypto_cipher_set_tag(EVP_CIPHER_CTX *cipher_ctx,
const php_crypto_cipher_mode *mode, unsigned char *tag, int tag_len TSRMLS_DC)
{
if (!tag) {
return SUCCESS;
}
if (!EVP_CIPHER_CTX_ctrl(cipher_ctx, mode->auth_set_tag_flag, tag_len, tag)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, TAG_SETTER_FAILED));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_cipher_check_tag_len */
static int php_crypto_cipher_check_tag_len(int tag_len TSRMLS_DC)
{
if (tag_len < PHP_CRYPTO_CIPHER_AUTH_TAG_LENGTH_MIN) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, TAG_LENGTH_LOW));
return FAILURE;
}
if (tag_len > PHP_CRYPTO_CIPHER_AUTH_TAG_LENGTH_MAX) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, TAG_LENGTH_HIGH));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_cipher_check_key_len */
static int php_crypto_cipher_check_key_len(zval *zobject, PHPC_THIS_DECLARE(crypto_cipher),
phpc_str_size_t key_len TSRMLS_DC)
{
int int_key_len, alg_key_len = EVP_CIPHER_key_length(PHP_CRYPTO_CIPHER_ALG(PHPC_THIS));
PHPC_READ_PROPERTY_RV_DECLARE;
if (php_crypto_str_size_to_int(key_len, &int_key_len) == SUCCESS &&
int_key_len != alg_key_len &&
!EVP_CIPHER_CTX_set_key_length(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS), int_key_len)) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, KEY_LENGTH_INVALID),
PHP_CRYPTO_CIPHER_GET_ALGORITHM_NAME(zobject), alg_key_len);
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_cipher_check_iv_len */
static int php_crypto_cipher_check_iv_len(zval *zobject, PHPC_THIS_DECLARE(crypto_cipher),
const php_crypto_cipher_mode *mode, phpc_str_size_t iv_len TSRMLS_DC)
{
int int_iv_len, alg_iv_len = EVP_CIPHER_iv_length(PHP_CRYPTO_CIPHER_ALG(PHPC_THIS));
PHPC_READ_PROPERTY_RV_DECLARE;
if (php_crypto_str_size_to_int(iv_len, &int_iv_len) == FAILURE) {
return FAILURE;
}
if (int_iv_len == alg_iv_len) {
return SUCCESS;
}
if (!mode->auth_enc || int_iv_len == INT_MAX ||
!EVP_CIPHER_CTX_ctrl(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS),
mode->auth_ivlen_flag, int_iv_len, NULL)) {
php_crypto_error_ex(PHP_CRYPTO_ERROR_ARGS(Cipher, IV_LENGTH_INVALID),
PHP_CRYPTO_CIPHER_GET_ALGORITHM_NAME(zobject), alg_iv_len);
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_cipher_init_ex */
static PHPC_OBJ_STRUCT_NAME(crypto_cipher) *php_crypto_cipher_init_ex(
zval *zobject, char *key, phpc_str_size_t key_len,
char *iv, phpc_str_size_t iv_len, int enc TSRMLS_DC)
{
const php_crypto_cipher_mode *mode;
PHPC_THIS_DECLARE_AND_FETCH_FROM_ZVAL(crypto_cipher, zobject);
/* check algorithm status */
if (enc && PHP_CRYPTO_CIPHER_IS_INITIALIZED_FOR_DECRYPTION(PHPC_THIS)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, INIT_ENCRYPT_FORBIDDEN));
return NULL;
} else if (!enc && PHP_CRYPTO_CIPHER_IS_INITIALIZED_FOR_ENCRYPTION(PHPC_THIS)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, INIT_DECRYPT_FORBIDDEN));
return NULL;
}
/* initialize encryption/decryption */
if (!EVP_CipherInit_ex(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS), PHP_CRYPTO_CIPHER_ALG(PHPC_THIS),
NULL, NULL, NULL, enc)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, INIT_ALG_FAILED));
return NULL;
}
/* check key length */
if (php_crypto_cipher_check_key_len(zobject, PHPC_THIS, key_len TSRMLS_CC) == FAILURE) {
return NULL;
}
/* get mode */
mode = php_crypto_get_cipher_mode_ex(PHP_CRYPTO_CIPHER_MODE_VALUE(PHPC_THIS));
/* mode with inlen init requires also pre-setting tag length */
if (mode->auth_inlen_init && enc) {
EVP_CIPHER_CTX_ctrl(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS), mode->auth_set_tag_flag,
PHP_CRYPTO_CIPHER_TAG_LEN(PHPC_THIS), NULL);
}
/* check initialization vector length */
if (php_crypto_cipher_check_iv_len(zobject, PHPC_THIS, mode, iv_len TSRMLS_CC) == FAILURE) {
return NULL;
}
if (mode->auth_enc && !enc &&
php_crypto_cipher_set_tag(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS), mode,
PHP_CRYPTO_CIPHER_TAG(PHPC_THIS),
PHP_CRYPTO_CIPHER_TAG(PHPC_THIS) ? PHP_CRYPTO_CIPHER_TAG_LEN(PHPC_THIS) : 0
TSRMLS_CC) == FAILURE) {
return NULL;
}
/* initialize encryption */
if (!EVP_CipherInit_ex(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS), NULL, NULL,
(unsigned char *) key, (unsigned char *) iv, enc)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, INIT_CTX_FAILED));
return NULL;
}
PHP_CRYPTO_CIPHER_SET_STATUS(PHPC_THIS, enc, INIT);
return PHPC_THIS;
}
/* }}} */
/* {{{ php_crypto_cipher_init */
static inline void php_crypto_cipher_init(INTERNAL_FUNCTION_PARAMETERS, int enc)
{
char *key, *iv = NULL;
phpc_str_size_t key_len, iv_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s",
&key, &key_len, &iv, &iv_len) == FAILURE) {
return;
}
if (php_crypto_cipher_init_ex(getThis(), key, key_len, iv, iv_len, enc TSRMLS_CC)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ php_crypto_cipher_write_aad */
PHP_CRYPTO_API int php_crypto_cipher_write_aad(
EVP_CIPHER_CTX *cipher_ctx, unsigned char *aad, int aad_len TSRMLS_DC)
{
int outlen, ret;
if (aad) {
ret = EVP_CipherUpdate(cipher_ctx, NULL, &outlen, aad, aad_len);
} else {
unsigned char buf[4];
ret = EVP_CipherUpdate(cipher_ctx, NULL, &outlen, buf, 0);
}
if (!ret) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, AAD_SETTER_FAILED));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_cipher_write_inlen */
static int php_crypto_cipher_write_inlen(
EVP_CIPHER_CTX *cipher_ctx, int inlen TSRMLS_DC)
{
int outlen;
if (!EVP_CipherUpdate(cipher_ctx, NULL, &outlen, NULL, inlen)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, UPDATE_FAILED));
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_cipher_auth_init */
static int php_crypto_cipher_auth_init(
PHPC_THIS_DECLARE(crypto_cipher), int inlen TSRMLS_DC)
{
EVP_CIPHER_CTX *cipher_ctx = PHP_CRYPTO_CIPHER_CTX(PHPC_THIS);
const php_crypto_cipher_mode *mode = php_crypto_get_cipher_mode_ex(
PHP_CRYPTO_CIPHER_MODE_VALUE(PHPC_THIS));
/* auth init is just for auth modes */
if (!mode->auth_enc) {
return SUCCESS;
}
/* check if plain text length needs to be initialized (CCM mode) */
if (mode->auth_inlen_init && php_crypto_cipher_write_inlen(
cipher_ctx, inlen TSRMLS_CC) == FAILURE) {
return FAILURE;
}
/* write additional authenticated data */
if (php_crypto_cipher_write_aad(
cipher_ctx,
PHP_CRYPTO_CIPHER_AAD(PHPC_THIS),
PHP_CRYPTO_CIPHER_AAD_LEN(PHPC_THIS) TSRMLS_CC) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ php_crypto_cipher_update */
static inline void php_crypto_cipher_update(INTERNAL_FUNCTION_PARAMETERS, int enc)
{
PHPC_THIS_DECLARE(crypto_cipher);
PHPC_STR_DECLARE(out);
char *data;
phpc_str_size_t data_str_size;
int out_len, update_len, data_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_str_size) == FAILURE) {
return;
}
if (php_crypto_str_size_to_int(data_str_size, &data_len)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, INPUT_DATA_LENGTH_HIGH));
RETURN_FALSE;
}
PHPC_THIS_FETCH(crypto_cipher);
/* check algorithm status */
if (enc && !PHP_CRYPTO_CIPHER_IS_INITIALIZED_FOR_ENCRYPTION(PHPC_THIS)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, UPDATE_ENCRYPT_FORBIDDEN));
RETURN_FALSE;
} else if (!enc && !PHP_CRYPTO_CIPHER_IS_INITIALIZED_FOR_DECRYPTION(PHPC_THIS)) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, UPDATE_DECRYPT_FORBIDDEN));
RETURN_FALSE;
}
/* if the crypto is in init state (first update), then do auth init */
if (PHP_CRYPTO_CIPHER_IS_IN_INIT_STATE(PHPC_THIS) &&
php_crypto_cipher_auth_init(PHPC_THIS, data_len TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
out_len = data_len + EVP_CIPHER_block_size(PHP_CRYPTO_CIPHER_ALG(PHPC_THIS));
update_len = out_len;
PHPC_STR_ALLOC(out, out_len);
/* update encryption context */
if (!EVP_CipherUpdate(PHP_CRYPTO_CIPHER_CTX(PHPC_THIS),
(unsigned char *) PHPC_STR_VAL(out), &update_len,
(unsigned char *) data, data_len)) {
/* get mode info */
const php_crypto_cipher_mode *mode = php_crypto_get_cipher_mode_ex(
PHP_CRYPTO_CIPHER_MODE_VALUE(PHPC_THIS));
if (!enc && mode->auth_inlen_init) {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, TAG_VERIFY_FAILED));
} else {
php_crypto_error(PHP_CRYPTO_ERROR_ARGS(Cipher, UPDATE_FAILED));
}
PHPC_STR_RELEASE(out);
RETURN_FALSE;
}
PHP_CRYPTO_CIPHER_SET_STATUS(PHPC_THIS, enc, UPDATE);
if (out_len > update_len) {
PHPC_STR_REALLOC(out, update_len);
}
PHPC_STR_VAL(out)[update_len] = 0;
PHPC_STR_RETURN(out);
}
/* }}} */
/* {{{ php_crypto_cipher_finish */
static inline void php_crypto_cipher_finish(INTERNAL_FUNCTION_PARAMETERS, int enc)