-
Notifications
You must be signed in to change notification settings - Fork 76
/
libsodium.c
4464 lines (4136 loc) · 160 KB
/
libsodium.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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sodium.h>
#include <stdint.h>
#include <string.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "zend_exceptions.h"
#include "zend_API.h"
#if PHP_VERSION_ID < 70100
# define IS_VOID 0
#endif
#if PHP_VERSION_ID < 70200
# undef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX
# define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, type, allow_null) \
static const zend_internal_arg_info name[] = { \
{ (const char *)(zend_uintptr_t) required_num_args, NULL, type, return_reference, allow_null, 0 },
#endif
#ifndef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX
# define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, num_args, type) \
ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, num_args)
#endif
#ifndef ZEND_ARG_INFO_WITH_DEFAULT_VALUE
# define ZEND_ARG_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, default_value) \
ZEND_ARG_INFO(pass_by_ref, name)
#endif
#ifndef ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE
# define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) \
ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null)
#endif
#define PHP_SODIUM_ZSTR_TRUNCATE(zs, len) do { ZSTR_LEN(zs) = (len); ZSTR_VAL(zs)[len] = 0; } while(0)
static zend_always_inline zend_string *zend_string_checked_alloc(size_t len, int persistent)
{
zend_string *zs;
if (ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)) < len) {
zend_error_noreturn(E_ERROR, "Memory allocation too large (%zu bytes)", len);
}
zs = zend_string_alloc(len, persistent);
ZSTR_VAL(zs)[len] = 0;
return zs;
}
static zend_class_entry *sodium_exception_ce;
#if (defined(__amd64) || defined(__amd64__) || defined(__x86_64__) || defined(__i386__) || \
defined(_M_AMD64) || defined(_M_IX86))
# define HAVE_AESGCM 1
#endif
#if SODIUM_LIBRARY_VERSION_MAJOR > 9 || (SODIUM_LIBRARY_VERSION_MAJOR == 9 && SODIUM_LIBRARY_VERSION_MINOR >= 2)
# define HAVE_AEAD_DETACHED 1
#endif
#ifndef crypto_aead_chacha20poly1305_IETF_KEYBYTES
# define crypto_aead_chacha20poly1305_IETF_KEYBYTES crypto_aead_chacha20poly1305_KEYBYTES
#endif
#ifndef crypto_aead_chacha20poly1305_IETF_NSECBYTES
# define crypto_aead_chacha20poly1305_IETF_NSECBYTES crypto_aead_chacha20poly1305_NSECBYTES
#endif
#ifndef crypto_aead_chacha20poly1305_IETF_ABYTES
# define crypto_aead_chacha20poly1305_IETF_ABYTES crypto_aead_chacha20poly1305_ABYTES
#endif
#if defined(crypto_secretstream_xchacha20poly1305_ABYTES) && SODIUM_LIBRARY_VERSION_MAJOR < 10
# undef crypto_secretstream_xchacha20poly1305_ABYTES
#endif
#ifndef crypto_pwhash_OPSLIMIT_MIN
# define crypto_pwhash_OPSLIMIT_MIN crypto_pwhash_OPSLIMIT_INTERACTIVE
#endif
#ifndef crypto_pwhash_MEMLIMIT_MIN
# define crypto_pwhash_MEMLIMIT_MIN crypto_pwhash_MEMLIMIT_INTERACTIVE
#endif
#ifndef crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN
# define crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE
#endif
#ifndef crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN
# define crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
#endif
#ifndef PHP_FE_END
# define PHP_FE_END { NULL, NULL, NULL }
#endif
#include "php_libsodium.h"
#include "libsodium_arginfo.h"
#define PHP_FUNC_PROTO(X) PHP_FE(X, arginfo_ ## X)
#define PHP_FUNC_ALIAS(X, Y) PHP_FALIAS(X, Y, arginfo_ ## X)
static const zend_function_entry sodium_functions[] = {
PHP_FUNC_PROTO(sodium_crypto_aead_aes256gcm_is_available)
#ifdef HAVE_AESGCM
PHP_FUNC_PROTO(sodium_crypto_aead_aes256gcm_decrypt)
PHP_FUNC_PROTO(sodium_crypto_aead_aes256gcm_encrypt)
PHP_FUNC_PROTO(sodium_crypto_aead_aes256gcm_keygen)
# ifdef HAVE_AEAD_DETACHED
PHP_FUNC_PROTO(sodium_crypto_aead_aes256gcm_decrypt_detached)
PHP_FUNC_PROTO(sodium_crypto_aead_aes256gcm_encrypt_detached)
# endif
#endif
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_decrypt)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_encrypt)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_keygen)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_ietf_decrypt)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_ietf_encrypt)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_ietf_keygen)
#ifdef HAVE_AEAD_DETACHED
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_decrypt_detached)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_encrypt_detached)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_ietf_decrypt_detached)
PHP_FUNC_PROTO(sodium_crypto_aead_chacha20poly1305_ietf_encrypt_detached)
#endif
#ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
PHP_FUNC_PROTO(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt)
PHP_FUNC_PROTO(sodium_crypto_aead_xchacha20poly1305_ietf_keygen)
PHP_FUNC_PROTO(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt)
# ifdef HAVE_AEAD_DETACHED
PHP_FUNC_PROTO(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt_detached)
PHP_FUNC_PROTO(sodium_crypto_aead_xchacha20poly1305_ietf_encrypt_detached)
# endif
#endif
PHP_FUNC_PROTO(sodium_crypto_auth)
PHP_FUNC_PROTO(sodium_crypto_auth_keygen)
PHP_FUNC_PROTO(sodium_crypto_auth_verify)
PHP_FUNC_PROTO(sodium_crypto_box)
PHP_FUNC_PROTO(sodium_crypto_box_keypair)
PHP_FUNC_PROTO(sodium_crypto_box_seed_keypair)
PHP_FUNC_PROTO(sodium_crypto_box_keypair_from_secretkey_and_publickey)
PHP_FUNC_PROTO(sodium_crypto_box_open)
PHP_FUNC_PROTO(sodium_crypto_box_publickey)
PHP_FUNC_PROTO(sodium_crypto_box_publickey_from_secretkey)
PHP_FUNC_PROTO(sodium_crypto_box_seal)
PHP_FUNC_PROTO(sodium_crypto_box_seal_open)
PHP_FUNC_PROTO(sodium_crypto_box_secretkey)
PHP_FUNC_PROTO(sodium_crypto_kx_keypair)
PHP_FUNC_PROTO(sodium_crypto_kx_publickey)
PHP_FUNC_PROTO(sodium_crypto_kx_secretkey)
PHP_FUNC_PROTO(sodium_crypto_kx_seed_keypair)
PHP_FUNC_PROTO(sodium_crypto_kx_client_session_keys)
PHP_FUNC_PROTO(sodium_crypto_kx_server_session_keys)
PHP_FUNC_PROTO(sodium_crypto_generichash)
PHP_FUNC_PROTO(sodium_crypto_generichash_keygen)
PHP_FUNC_PROTO(sodium_crypto_generichash_init)
PHP_FUNC_PROTO(sodium_crypto_generichash_update)
PHP_FUNC_PROTO(sodium_crypto_generichash_final)
PHP_FUNC_PROTO(sodium_crypto_kdf_derive_from_key)
PHP_FUNC_PROTO(sodium_crypto_kdf_keygen)
#ifdef crypto_pwhash_SALTBYTES
PHP_FUNC_PROTO(sodium_crypto_pwhash)
PHP_FUNC_PROTO(sodium_crypto_pwhash_str)
PHP_FUNC_PROTO(sodium_crypto_pwhash_str_verify)
#endif
#if SODIUM_LIBRARY_VERSION_MAJOR > 9 || (SODIUM_LIBRARY_VERSION_MAJOR == 9 && SODIUM_LIBRARY_VERSION_MINOR >= 6)
PHP_FUNC_PROTO(sodium_crypto_pwhash_str_needs_rehash)
#endif
#ifdef crypto_pwhash_scryptsalsa208sha256_SALTBYTES
PHP_FUNC_PROTO(sodium_crypto_pwhash_scryptsalsa208sha256)
PHP_FUNC_PROTO(sodium_crypto_pwhash_scryptsalsa208sha256_str)
PHP_FUNC_PROTO(sodium_crypto_pwhash_scryptsalsa208sha256_str_verify)
#endif
PHP_FUNC_PROTO(sodium_crypto_scalarmult)
PHP_FUNC_PROTO(sodium_crypto_secretbox)
PHP_FUNC_PROTO(sodium_crypto_secretbox_keygen)
PHP_FUNC_PROTO(sodium_crypto_secretbox_open)
#ifdef crypto_secretstream_xchacha20poly1305_ABYTES
PHP_FUNC_PROTO(sodium_crypto_secretstream_xchacha20poly1305_keygen)
PHP_FUNC_PROTO(sodium_crypto_secretstream_xchacha20poly1305_init_push)
PHP_FUNC_PROTO(sodium_crypto_secretstream_xchacha20poly1305_push)
PHP_FUNC_PROTO(sodium_crypto_secretstream_xchacha20poly1305_init_pull)
PHP_FUNC_PROTO(sodium_crypto_secretstream_xchacha20poly1305_pull)
PHP_FUNC_PROTO(sodium_crypto_secretstream_xchacha20poly1305_rekey)
#endif
PHP_FUNC_PROTO(sodium_crypto_shorthash)
PHP_FUNC_PROTO(sodium_crypto_shorthash_keygen)
PHP_FUNC_PROTO(sodium_crypto_sign)
PHP_FUNC_PROTO(sodium_crypto_sign_detached)
PHP_FUNC_PROTO(sodium_crypto_sign_ed25519_pk_to_curve25519)
PHP_FUNC_PROTO(sodium_crypto_sign_ed25519_sk_to_curve25519)
PHP_FUNC_PROTO(sodium_crypto_sign_keypair)
PHP_FUNC_PROTO(sodium_crypto_sign_keypair_from_secretkey_and_publickey)
PHP_FUNC_PROTO(sodium_crypto_sign_open)
PHP_FUNC_PROTO(sodium_crypto_sign_publickey)
PHP_FUNC_PROTO(sodium_crypto_sign_secretkey)
PHP_FUNC_PROTO(sodium_crypto_sign_publickey_from_secretkey)
PHP_FUNC_PROTO(sodium_crypto_sign_seed_keypair)
PHP_FUNC_PROTO(sodium_crypto_sign_verify_detached)
PHP_FUNC_PROTO(sodium_crypto_stream)
PHP_FUNC_PROTO(sodium_crypto_stream_keygen)
PHP_FUNC_PROTO(sodium_crypto_stream_xor)
#ifdef crypto_stream_xchacha20_KEYBYTES
PHP_FUNC_PROTO(sodium_crypto_stream_xchacha20)
PHP_FUNC_PROTO(sodium_crypto_stream_xchacha20_keygen)
PHP_FUNC_PROTO(sodium_crypto_stream_xchacha20_xor)
#endif
/* helpers */
PHP_FUNC_PROTO(sodium_add)
PHP_FUNC_PROTO(sodium_compare)
PHP_FUNC_PROTO(sodium_increment)
PHP_FUNC_PROTO(sodium_memcmp)
PHP_FUNC_PROTO(sodium_memzero)
PHP_FUNC_PROTO(sodium_pad)
PHP_FUNC_PROTO(sodium_unpad)
/* codecs */
PHP_FUNC_PROTO(sodium_bin2hex)
PHP_FUNC_PROTO(sodium_hex2bin)
#ifdef sodium_base64_VARIANT_ORIGINAL
PHP_FUNC_PROTO(sodium_bin2base64)
PHP_FUNC_PROTO(sodium_base642bin)
#endif
/* aliases */
PHP_FUNC_ALIAS(sodium_crypto_scalarmult_base, sodium_crypto_box_publickey_from_secretkey)
PHP_FE_END
};
zend_module_entry sodium_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"sodium",
sodium_functions,
PHP_MINIT(sodium),
PHP_MSHUTDOWN(sodium),
NULL,
NULL,
PHP_MINFO(sodium),
#if ZEND_MODULE_API_NO >= 20010901
PHP_SODIUM_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_SODIUM
ZEND_GET_MODULE(sodium)
#endif
static zend_object *sodium_exception_create_object(zend_class_entry *ce) {
zend_object *obj = zend_ce_exception->create_object(ce);
zval rv, *trace;
#if ZEND_MODULE_API_NO >= 20200930
zend_object *obj_zv = obj;
#else
zval obj_zv[1];
ZVAL_OBJ(obj_zv, obj);
#endif
trace = zend_read_property(zend_get_exception_base(obj_zv), obj_zv, "trace", (sizeof "trace") - 1, 0, &rv);
/* Remove argument information from backtrace to prevent information leaks */
if (trace && Z_TYPE_P(trace) == IS_ARRAY) {
zval *frame = NULL;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(trace), frame) {
if (Z_TYPE_P(frame) == IS_ARRAY) {
zval *args = zend_hash_str_find(Z_ARRVAL_P(frame), "args", (sizeof "args") - 1);
if (args != NULL) {
#ifdef ZVAL_EMPTY_ARRAY
zval_ptr_dtor(args);
ZVAL_EMPTY_ARRAY(args);
#else
if (Z_TYPE_P(args) == IS_ARRAY) {
zend_hash_clean(Z_ARRVAL_P(args));
}
#endif
}
}
} ZEND_HASH_FOREACH_END();
}
return obj;
}
static void sodium_separate_string(zval *zv) {
ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
if (!Z_REFCOUNTED_P(zv) || Z_REFCOUNT_P(zv) > 1) {
zend_string *copy = zend_string_init(Z_STRVAL_P(zv), Z_STRLEN_P(zv), 0);
Z_TRY_DELREF_P(zv);
ZVAL_STR(zv, copy);
}
}
PHP_MINIT_FUNCTION(sodium)
{
zend_class_entry ce;
if (sodium_init() < 0) {
zend_error(E_ERROR, "sodium_init()");
}
INIT_CLASS_ENTRY(ce, "SodiumException", NULL);
sodium_exception_ce = zend_register_internal_class_ex(&ce, zend_ce_exception);
sodium_exception_ce->create_object = sodium_exception_create_object;
REGISTER_STRING_CONSTANT("SODIUM_LIBRARY_VERSION",
(char *) (void *) sodium_version_string(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_LIBRARY_MAJOR_VERSION",
sodium_library_version_major(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_LIBRARY_MINOR_VERSION",
sodium_library_version_minor(), CONST_CS | CONST_PERSISTENT);
#ifdef HAVE_AESGCM
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES",
crypto_aead_aes256gcm_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_NSECBYTES",
crypto_aead_aes256gcm_NSECBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES",
crypto_aead_aes256gcm_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_AES256GCM_ABYTES",
crypto_aead_aes256gcm_ABYTES, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES",
crypto_aead_chacha20poly1305_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES",
crypto_aead_chacha20poly1305_NSECBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES",
crypto_aead_chacha20poly1305_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_ABYTES",
crypto_aead_chacha20poly1305_ABYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES",
crypto_aead_chacha20poly1305_IETF_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES",
crypto_aead_chacha20poly1305_IETF_NSECBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES",
crypto_aead_chacha20poly1305_IETF_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES",
crypto_aead_chacha20poly1305_IETF_ABYTES, CONST_CS | CONST_PERSISTENT);
#ifdef crypto_aead_xchacha20poly1305_IETF_NPUBBYTES
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES",
crypto_aead_xchacha20poly1305_IETF_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES",
crypto_aead_xchacha20poly1305_IETF_NSECBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES",
crypto_aead_xchacha20poly1305_IETF_NPUBBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES",
crypto_aead_xchacha20poly1305_IETF_ABYTES, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AUTH_BYTES",
crypto_auth_BYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_AUTH_KEYBYTES",
crypto_auth_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_SEALBYTES",
crypto_box_SEALBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_SECRETKEYBYTES",
crypto_box_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_PUBLICKEYBYTES",
crypto_box_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_KEYPAIRBYTES",
crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_MACBYTES",
crypto_box_MACBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_NONCEBYTES",
crypto_box_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_BOX_SEEDBYTES",
crypto_box_SEEDBYTES, CONST_CS | CONST_PERSISTENT);
#ifndef crypto_kdf_BYTES_MIN
# define crypto_kdf_BYTES_MIN 16
# define crypto_kdf_BYTES_MAX 64
# define crypto_kdf_CONTEXTBYTES 8
# define crypto_kdf_KEYBYTES 32
#endif
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_BYTES_MIN",
crypto_kdf_BYTES_MIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_BYTES_MAX",
crypto_kdf_BYTES_MAX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_CONTEXTBYTES",
crypto_kdf_CONTEXTBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KDF_KEYBYTES",
crypto_kdf_KEYBYTES, CONST_CS | CONST_PERSISTENT);
#ifndef crypto_kx_SEEDBYTES
# define crypto_kx_SEEDBYTES 32
# define crypto_kx_SESSIONKEYBYTES 32
# define crypto_kx_PUBLICKEYBYTES 32
# define crypto_kx_SECRETKEYBYTES 32
#endif
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SEEDBYTES",
crypto_kx_SEEDBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SESSIONKEYBYTES",
crypto_kx_SESSIONKEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_PUBLICKEYBYTES",
crypto_kx_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_SECRETKEYBYTES",
crypto_kx_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_KX_KEYPAIRBYTES",
crypto_kx_SECRETKEYBYTES + crypto_kx_PUBLICKEYBYTES,
CONST_CS | CONST_PERSISTENT);
#ifdef crypto_secretstream_xchacha20poly1305_ABYTES
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES",
crypto_secretstream_xchacha20poly1305_ABYTES,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES",
crypto_secretstream_xchacha20poly1305_HEADERBYTES,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES",
crypto_secretstream_xchacha20poly1305_KEYBYTES,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX",
crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_MESSAGE",
crypto_secretstream_xchacha20poly1305_TAG_MESSAGE,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH",
crypto_secretstream_xchacha20poly1305_TAG_PUSH,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY",
crypto_secretstream_xchacha20poly1305_TAG_REKEY,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL",
crypto_secretstream_xchacha20poly1305_TAG_FINAL,
CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES",
crypto_generichash_BYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES_MIN",
crypto_generichash_BYTES_MIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_BYTES_MAX",
crypto_generichash_BYTES_MAX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_KEYBYTES",
crypto_generichash_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN",
crypto_generichash_KEYBYTES_MIN, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX",
crypto_generichash_KEYBYTES_MAX, CONST_CS | CONST_PERSISTENT);
#ifdef crypto_pwhash_SALTBYTES
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_ALG_ARGON2I13",
crypto_pwhash_ALG_ARGON2I13, CONST_CS | CONST_PERSISTENT);
# ifdef crypto_pwhash_ALG_ARGON2ID13
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13",
crypto_pwhash_ALG_ARGON2ID13, CONST_CS | CONST_PERSISTENT);
# endif
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_ALG_DEFAULT",
crypto_pwhash_ALG_DEFAULT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SALTBYTES",
crypto_pwhash_SALTBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("SODIUM_CRYPTO_PWHASH_STRPREFIX",
crypto_pwhash_STRPREFIX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_STRBYTES",
crypto_pwhash_STRBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE",
crypto_pwhash_opslimit_interactive(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE",
crypto_pwhash_memlimit_interactive(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE",
crypto_pwhash_opslimit_moderate(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE",
crypto_pwhash_memlimit_moderate(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE",
crypto_pwhash_opslimit_sensitive(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE",
crypto_pwhash_memlimit_sensitive(), CONST_CS | CONST_PERSISTENT);
#endif
#ifdef crypto_pwhash_scryptsalsa208sha256_SALTBYTES
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES",
crypto_pwhash_scryptsalsa208sha256_SALTBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_STRING_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX",
crypto_pwhash_scryptsalsa208sha256_STRPREFIX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE",
crypto_pwhash_scryptsalsa208sha256_opslimit_interactive(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE",
crypto_pwhash_scryptsalsa208sha256_memlimit_interactive(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE",
crypto_pwhash_scryptsalsa208sha256_opslimit_sensitive(), CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE",
crypto_pwhash_scryptsalsa208sha256_memlimit_sensitive(), CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SCALARMULT_BYTES",
crypto_scalarmult_BYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SCALARMULT_SCALARBYTES",
crypto_scalarmult_SCALARBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SHORTHASH_BYTES",
crypto_shorthash_BYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SHORTHASH_KEYBYTES",
crypto_shorthash_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETBOX_KEYBYTES",
crypto_secretbox_KEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETBOX_MACBYTES",
crypto_secretbox_MACBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SECRETBOX_NONCEBYTES",
crypto_secretbox_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_BYTES",
crypto_sign_BYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_SEEDBYTES",
crypto_sign_SEEDBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES",
crypto_sign_PUBLICKEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_SECRETKEYBYTES",
crypto_sign_SECRETKEYBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_SIGN_KEYPAIRBYTES",
crypto_sign_SECRETKEYBYTES + crypto_sign_PUBLICKEYBYTES,
CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_STREAM_NONCEBYTES",
crypto_stream_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_STREAM_KEYBYTES",
crypto_stream_KEYBYTES, CONST_CS | CONST_PERSISTENT);
#ifdef crypto_stream_xchacha20_KEYBYTES
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES",
crypto_stream_xchacha20_NONCEBYTES, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES",
crypto_stream_xchacha20_KEYBYTES, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef sodium_base64_VARIANT_ORIGINAL
REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_ORIGINAL",
sodium_base64_VARIANT_ORIGINAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING",
sodium_base64_VARIANT_ORIGINAL_NO_PADDING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_URLSAFE",
sodium_base64_VARIANT_URLSAFE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING",
sodium_base64_VARIANT_URLSAFE_NO_PADDING, CONST_CS | CONST_PERSISTENT);
#endif
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(sodium)
{
randombytes_close();
return SUCCESS;
}
PHP_MINFO_FUNCTION(sodium)
{
php_info_print_table_start();
php_info_print_table_header(2, "sodium support", "enabled");
php_info_print_table_row(2, "sodium compiled version", PHP_SODIUM_VERSION);
php_info_print_table_row(2, "libsodium headers version", SODIUM_VERSION_STRING);
php_info_print_table_row(2, "libsodium library version", sodium_version_string());
php_info_print_table_end();
}
PHP_FUNCTION(sodium_memzero)
{
zval *buf_zv;
if (zend_parse_parameters(ZEND_NUM_ARGS(),
"z", &buf_zv) == FAILURE) {
return;
}
ZVAL_DEREF(buf_zv);
if (Z_TYPE_P(buf_zv) != IS_STRING) {
zend_throw_exception(sodium_exception_ce, "a PHP string is required", 0);
return;
}
if (Z_REFCOUNTED_P(buf_zv) && Z_REFCOUNT_P(buf_zv) == 1) {
char *buf = Z_STRVAL(*buf_zv);
size_t buf_len = Z_STRLEN(*buf_zv);
if (buf_len > 0) {
sodium_memzero(buf, (size_t) buf_len);
}
}
convert_to_null(buf_zv);
}
PHP_FUNCTION(sodium_increment)
{
zval *val_zv;
unsigned char *val;
size_t val_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(),
"z", &val_zv) == FAILURE) {
return;
}
ZVAL_DEREF(val_zv);
if (Z_TYPE_P(val_zv) != IS_STRING) {
zend_throw_exception(sodium_exception_ce, "a PHP string is required", 0);
return;
}
sodium_separate_string(val_zv);
val = (unsigned char *) Z_STRVAL(*val_zv);
val_len = Z_STRLEN(*val_zv);
sodium_increment(val, val_len);
}
PHP_FUNCTION(sodium_add)
{
zval *val_zv;
unsigned char *val;
unsigned char *addv;
size_t val_len;
size_t addv_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(),
"zs", &val_zv, &addv, &addv_len) == FAILURE) {
return;
}
ZVAL_DEREF(val_zv);
if (Z_TYPE_P(val_zv) != IS_STRING) {
zend_throw_exception(sodium_exception_ce, "PHP strings are required", 0);
return;
}
sodium_separate_string(val_zv);
val = (unsigned char *) Z_STRVAL(*val_zv);
val_len = Z_STRLEN(*val_zv);
if (val_len != addv_len) {
zend_throw_exception(sodium_exception_ce, "values must have the same length", 0);
return;
}
sodium_add(val, addv, val_len);
}
PHP_FUNCTION(sodium_memcmp)
{
char *buf1;
char *buf2;
size_t len1;
size_t len2;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
&buf1, &len1,
&buf2, &len2) == FAILURE) {
return;
}
if (len1 != len2) {
zend_throw_exception(sodium_exception_ce, "arguments have different sizes", 0);
return;
}
RETURN_LONG(sodium_memcmp(buf1, buf2, len1));
}
PHP_FUNCTION(sodium_crypto_shorthash)
{
zend_string *hash;
unsigned char *key;
unsigned char *msg;
size_t key_len;
size_t msg_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
&msg, &msg_len,
&key, &key_len) == FAILURE) {
return;
}
if (key_len != crypto_shorthash_KEYBYTES) {
zend_throw_exception(sodium_exception_ce,
"key size should be SODIUM_CRYPTO_SHORTHASH_KEYBYTES bytes",
0);
return;
}
hash = zend_string_checked_alloc(crypto_shorthash_BYTES, 0);
if (crypto_shorthash((unsigned char *) ZSTR_VAL(hash), msg,
(unsigned long long) msg_len, key) != 0) {
zend_string_free(hash);
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
RETURN_STR(hash);
}
PHP_FUNCTION(sodium_crypto_secretbox)
{
zend_string *ciphertext;
unsigned char *key;
unsigned char *msg;
unsigned char *nonce;
size_t key_len;
size_t msg_len;
size_t nonce_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss",
&msg, &msg_len,
&nonce, &nonce_len,
&key, &key_len) == FAILURE) {
return;
}
if (nonce_len != crypto_secretbox_NONCEBYTES) {
zend_throw_exception(sodium_exception_ce,
"nonce size should be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES bytes",
0);
return;
}
if (key_len != crypto_secretbox_KEYBYTES) {
zend_throw_exception(sodium_exception_ce,
"key size should be SODIUM_CRYPTO_SECRETBOX_KEYBYTES bytes",
0);
return;
}
if (SIZE_MAX - msg_len <= crypto_secretbox_MACBYTES) {
zend_throw_exception(sodium_exception_ce, "arithmetic overflow", 0);
return;
}
ciphertext = zend_string_checked_alloc((size_t) msg_len + crypto_secretbox_MACBYTES, 0);
if (crypto_secretbox_easy((unsigned char *) ZSTR_VAL(ciphertext),
msg, (unsigned long long) msg_len,
nonce, key) != 0) {
zend_string_free(ciphertext);
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
RETURN_STR(ciphertext);
}
PHP_FUNCTION(sodium_crypto_secretbox_open)
{
zend_string *msg;
unsigned char *key;
unsigned char *ciphertext;
unsigned char *nonce;
size_t key_len;
size_t ciphertext_len;
size_t nonce_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss",
&ciphertext, &ciphertext_len,
&nonce, &nonce_len,
&key, &key_len) == FAILURE) {
return;
}
if (nonce_len != crypto_secretbox_NONCEBYTES) {
zend_throw_exception(sodium_exception_ce,
"nonce size should be SODIUM_CRYPTO_SECRETBOX_NONCEBYTES bytes",
0);
return;
}
if (key_len != crypto_secretbox_KEYBYTES) {
zend_throw_exception(sodium_exception_ce,
"key size should be SODIUM_CRYPTO_SECRETBOX_KEYBYTES bytes",
0);
return;
}
if (ciphertext_len < crypto_secretbox_MACBYTES) {
RETURN_FALSE;
}
msg = zend_string_checked_alloc
((size_t) ciphertext_len - crypto_secretbox_MACBYTES, 0);
if (crypto_secretbox_open_easy((unsigned char *) ZSTR_VAL(msg), ciphertext,
(unsigned long long) ciphertext_len,
nonce, key) != 0) {
zend_string_free(msg);
RETURN_FALSE;
} else {
RETURN_STR(msg);
}
}
PHP_FUNCTION(sodium_crypto_generichash)
{
zend_string *hash;
unsigned char *key = NULL;
unsigned char *msg;
zend_long hash_len = crypto_generichash_BYTES;
size_t key_len = 0;
size_t msg_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sl",
&msg, &msg_len,
&key, &key_len,
&hash_len) == FAILURE) {
return;
}
if (hash_len < crypto_generichash_BYTES_MIN ||
hash_len > crypto_generichash_BYTES_MAX) {
zend_throw_exception(sodium_exception_ce, "unsupported output length", 0);
return;
}
if (key_len != 0 &&
(key_len < crypto_generichash_KEYBYTES_MIN ||
key_len > crypto_generichash_KEYBYTES_MAX)) {
zend_throw_exception(sodium_exception_ce, "unsupported key length", 0);
return;
}
hash = zend_string_checked_alloc(hash_len, 0);
if (crypto_generichash((unsigned char *) ZSTR_VAL(hash), (size_t) hash_len,
msg, (unsigned long long) msg_len,
key, (size_t) key_len) != 0) {
zend_string_free(hash);
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
RETURN_STR(hash);
}
PHP_FUNCTION(sodium_crypto_generichash_init)
{
crypto_generichash_state state_tmp;
zend_string *state;
unsigned char *key = NULL;
size_t state_len = sizeof (crypto_generichash_state);
zend_long hash_len = crypto_generichash_BYTES;
size_t key_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sl",
&key, &key_len,
&hash_len) == FAILURE) {
return;
}
if (hash_len < crypto_generichash_BYTES_MIN ||
hash_len > crypto_generichash_BYTES_MAX) {
zend_throw_exception(sodium_exception_ce, "unsupported output length", 0);
return;
}
if (key_len != 0 &&
(key_len < crypto_generichash_KEYBYTES_MIN ||
key_len > crypto_generichash_KEYBYTES_MAX)) {
zend_throw_exception(sodium_exception_ce, "unsupported key length", 0);
return;
}
memset(&state_tmp, 0, sizeof state_tmp);
if (crypto_generichash_init((void *) &state_tmp, key, (size_t) key_len,
(size_t) hash_len) != 0) {
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
state = zend_string_checked_alloc(state_len, 0);
memcpy(ZSTR_VAL(state), &state_tmp, state_len);
sodium_memzero(&state_tmp, sizeof state_tmp);
RETURN_STR(state);
}
PHP_FUNCTION(sodium_crypto_generichash_update)
{
crypto_generichash_state state_tmp;
zval *state_zv;
unsigned char *msg;
unsigned char *state;
size_t msg_len;
size_t state_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zs",
&state_zv, &msg, &msg_len) == FAILURE) {
return;
}
ZVAL_DEREF(state_zv);
if (Z_TYPE_P(state_zv) != IS_STRING) {
zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0);
return;
}
sodium_separate_string(state_zv);
state = (unsigned char *) Z_STRVAL(*state_zv);
state_len = Z_STRLEN(*state_zv);
if (state_len != sizeof (crypto_generichash_state)) {
zend_throw_exception(sodium_exception_ce, "incorrect state length", 0);
return;
}
memcpy(&state_tmp, state, sizeof state_tmp);
if (crypto_generichash_update((void *) &state_tmp, msg,
(unsigned long long) msg_len) != 0) {
sodium_memzero(&state_tmp, sizeof state_tmp);
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
memcpy(state, &state_tmp, state_len);
sodium_memzero(&state_tmp, sizeof state_tmp);
RETURN_TRUE;
}
PHP_FUNCTION(sodium_crypto_generichash_final)
{
crypto_generichash_state state_tmp;
zend_string *hash;
zval *state_zv;
unsigned char *state;
size_t state_len;
zend_long hash_len = crypto_generichash_BYTES;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|l",
&state_zv, &hash_len) == FAILURE) {
return;
}
ZVAL_DEREF(state_zv);
if (Z_TYPE_P(state_zv) != IS_STRING) {
zend_throw_exception(sodium_exception_ce, "a reference to a state is required", 0);
return;
}
sodium_separate_string(state_zv);
state = (unsigned char *) Z_STRVAL(*state_zv);
state_len = Z_STRLEN(*state_zv);
if (state_len != sizeof (crypto_generichash_state)) {
zend_throw_exception(sodium_exception_ce, "incorrect state length", 0);
return;
}
if (hash_len < crypto_generichash_BYTES_MIN ||
hash_len > crypto_generichash_BYTES_MAX) {
zend_throw_exception(sodium_exception_ce, "unsupported output length", 0);
return;
}
hash = zend_string_checked_alloc(hash_len, 0);
memcpy(&state_tmp, state, sizeof state_tmp);
if (crypto_generichash_final((void *) &state_tmp,
(unsigned char *) ZSTR_VAL(hash),
(size_t) hash_len) != 0) {
sodium_memzero(&state_tmp, sizeof state_tmp);
zend_string_free(hash);
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
sodium_memzero(&state_tmp, sizeof state_tmp);
sodium_memzero(state, state_len);
convert_to_null(state_zv);
RETURN_STR(hash);
}
PHP_FUNCTION(sodium_crypto_box_keypair)
{
zend_string *keypair;
size_t keypair_len;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
keypair_len = crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES;
keypair = zend_string_checked_alloc(keypair_len, 0);
if (crypto_box_keypair((unsigned char *) ZSTR_VAL(keypair) +
crypto_box_SECRETKEYBYTES,
(unsigned char *) ZSTR_VAL(keypair)) != 0) {
zend_string_free(keypair);
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
RETURN_STR(keypair);
}
PHP_FUNCTION(sodium_crypto_box_seed_keypair)
{
zend_string *keypair;
unsigned char *seed;
size_t keypair_len;
size_t seed_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
&seed, &seed_len) == FAILURE) {
return;
}
if (seed_len != crypto_box_SEEDBYTES) {
zend_throw_exception(sodium_exception_ce,
"seed should be SODIUM_CRYPTO_BOX_SEEDBYTES bytes",
0);
return;
}
keypair_len = crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES;
keypair = zend_string_checked_alloc(keypair_len, 0);
if (crypto_box_seed_keypair((unsigned char *) ZSTR_VAL(keypair) +
crypto_box_SECRETKEYBYTES,
(unsigned char *) ZSTR_VAL(keypair),
seed) != 0) {
zend_string_free(keypair);
zend_throw_exception(sodium_exception_ce, "internal error", 0);
return;
}
RETURN_STR(keypair);
}
PHP_FUNCTION(sodium_crypto_box_keypair_from_secretkey_and_publickey)
{
zend_string *keypair;
char *publickey;
char *secretkey;
size_t keypair_len;
size_t publickey_len;
size_t secretkey_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss",
&secretkey, &secretkey_len,
&publickey, &publickey_len) == FAILURE) {
return;
}
if (secretkey_len != crypto_box_SECRETKEYBYTES) {
zend_throw_exception(sodium_exception_ce,
"secretkey should be SODIUM_CRYPTO_BOX_SECRETKEYBYTES bytes",
0);
return;
}
if (publickey_len != crypto_box_PUBLICKEYBYTES) {
zend_throw_exception(sodium_exception_ce,
"publickey should be SODIUM_CRYPTO_BOX_PUBLICKEYBYTES bytes",
0);
return;
}
keypair_len = crypto_box_SECRETKEYBYTES + crypto_box_PUBLICKEYBYTES;
keypair = zend_string_checked_alloc(keypair_len, 0);
memcpy(ZSTR_VAL(keypair), secretkey, crypto_box_SECRETKEYBYTES);
memcpy(ZSTR_VAL(keypair) + crypto_box_SECRETKEYBYTES, publickey,
crypto_box_PUBLICKEYBYTES);
RETURN_STR(keypair);