-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffs.txt
1505 lines (1499 loc) · 47 KB
/
diffs.txt
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
diff --git a/.github/workflows/ci.yml b/.github/workflows/build.yml
similarity index 69%
rename from .github/workflows/ci.yml
rename to .github/workflows/build.yml
index e35cf79..d162ea8 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/build.yml
@@ -1,24 +1,26 @@
-name: "build"
+name: "Security & Standards"
on:
+ schedule:
+ - cron: '0 0 * * 0'
push:
- branches: [ 'main' ]
- pull_request:
branches: [ '*' ]
+ pull_request:
+ branches: [ "main", "master", "develop" ]
jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
- operating-system: [ ubuntu-latest, macos-latest, windows-latest ]
- php-versions: [ '8.1', '8.2' ]
+ operating-system: [ ubuntu-latest ]
+ php-versions: [ '8.2', '8.3' ]
dependency-version: [ prefer-lowest, prefer-stable ]
name: PHP ${{ matrix.php-versions }} - ${{ matrix.operating-system }} - ${{ matrix.dependency-version }}
steps:
- name: Checkout
- uses: actions/checkout@v3
+ uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
@@ -36,11 +38,8 @@ jobs:
- name: Install dependencies
run: composer install --no-interaction --prefer-dist --optimize-autoloader
- - name: Auditing packages
+ - name: Package Audit
run: composer audit
- name: Test
- run: ./vendor/bin/pest
-#
-# - name: Rector(Dry) Tests
-# run: php vendor/bin/rector process --dry-run --config=rector.php
\ No newline at end of file
+ run: composer tests
\ No newline at end of file
diff --git a/src/Asymmetric/Sodium/SodiumBox.php b/src/Asymmetric/Sodium/SodiumBox.php
deleted file mode 100644
index e5af203..0000000
--- a/src/Asymmetric/Sodium/SodiumBox.php
+++ /dev/null
@@ -1,70 +0,0 @@
-<?php
-
-namespace AbmmHasan\SafeGuard\Asymmetric\Sodium;
-
-use SodiumException;
-
-class SodiumBox
-{
- /**
- * @param string $privateKey User private key
- * @param string $nonce Shared secret
- * @param bool $isBinary Is transportable resource binary?
- */
- public function __construct(
- private string $privateKey,
- private string $nonce,
- private bool $isBinary = true
- ) {
- }
-
- /**
- * Encrypt the message
- *
- * @param string $message Message for encryption
- * @param string $publicKey Second party public key
- * @return string Encrypted message
- * @throws SodiumException
- */
- public function encrypt(string $message, string $publicKey): string
- {
- $encrypted = sodium_crypto_box(
- $message,
- $this->nonce,
- sodium_crypto_box_keypair_from_secretkey_and_publickey(
- $this->privateKey,
- $publicKey
- )
- );
-
- if ($this->isBinary) {
- return $encrypted;
- }
-
- return sodium_bin2base64($encrypted, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
- }
-
- /**
- * Decrypt the message
- *
- * @param string $encrypted Encrypted message
- * @param string $publicKey Second party public key
- * @return false|string Decrypted message
- * @throws SodiumException
- */
- public function decrypt(string $encrypted, string $publicKey): bool|string
- {
- if (!$this->isBinary) {
- $encrypted = sodium_base642bin($encrypted, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
- }
-
- return sodium_crypto_box_open(
- $encrypted,
- $this->nonce,
- sodium_crypto_box_keypair_from_secretkey_and_publickey(
- $this->privateKey,
- $publicKey
- )
- );
- }
-}
diff --git a/src/Asymmetric/Sodium/SodiumSeal.php b/src/Asymmetric/Sodium/SodiumSeal.php
deleted file mode 100644
index 0018fcc..0000000
--- a/src/Asymmetric/Sodium/SodiumSeal.php
+++ /dev/null
@@ -1,61 +0,0 @@
-<?php
-
-namespace AbmmHasan\SafeGuard\Asymmetric\Sodium;
-
-use SodiumException;
-
-class SodiumSeal
-{
-
- /**
- * Set predefined property
- *
- * @param bool $isBinary Is transportable resource binary?
- */
- public function __construct(
- private bool $isBinary = true
- ) {
- }
-
- /**
- * Encrypt the message (using anonymous public key)
- *
- * @param string $message Message for encryption
- * @param string $publicKey Public key
- * @return string Encrypted message
- * @throws SodiumException
- */
- public function encrypt(string $message, string $publicKey): string
- {
- $encrypted = sodium_crypto_box_seal($message, $publicKey);
-
- if ($this->isBinary) {
- return $encrypted;
- }
-
- return sodium_bin2base64($encrypted, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
- }
-
- /**
- * Decrypt the message (using anonymous public key)
- *
- * @param string $encrypted Encrypted message
- * @param string $privateKey Private key
- * @return false|string Decrypted message
- * @throws SodiumException
- */
- public function decrypt(string $encrypted, string $privateKey): bool|string
- {
- if (!$this->isBinary) {
- $encrypted = sodium_base642bin($encrypted, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
- }
-
- return sodium_crypto_box_seal_open(
- $encrypted,
- sodium_crypto_box_keypair_from_secretkey_and_publickey(
- $privateKey,
- sodium_crypto_box_publickey_from_secretkey($privateKey)
- )
- );
- }
-}
diff --git a/src/Asymmetric/Sodium/SodiumSign.php b/src/Asymmetric/Sodium/SodiumSign.php
deleted file mode 100644
index 7298da2..0000000
--- a/src/Asymmetric/Sodium/SodiumSign.php
+++ /dev/null
@@ -1,51 +0,0 @@
-<?php
-
-namespace AbmmHasan\SafeGuard\Asymmetric\Sodium;
-
-use SodiumException;
-
-class SodiumSign
-{
- public function __construct(
- private bool $isBinary = true
- ) {
- }
-
- /**
- * Sign a message
- *
- * @param string $message Message to sign
- * @param string $privateKey Secret key
- * @return string Signed message
- * @throws SodiumException
- */
- public function getSignedMessage(string $message, string $privateKey): string
- {
- $encrypted = sodium_crypto_sign($message, $privateKey);
-
- if ($this->isBinary) {
- return $encrypted;
- }
-
- return sodium_bin2base64($encrypted, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
- }
-
- /**
- * Verify attached signature and get the message
- *
- * @param string $signedMessage Signed message
- * @param string $publicKey Public key
- * @return false|string Signature verified message
- * @throws SodiumException
- */
- public function getVerifiedMessage(string $signedMessage, string $publicKey): bool|string
- {
- if (!$this->isBinary) {
- $signedMessage = sodium_base642bin($signedMessage, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
- }
-
- return sodium_crypto_sign_open($signedMessage, $publicKey);
- }
-
-
-}
diff --git a/src/Misc/SodiumKeygen.php b/src/Misc/SodiumKeygen.php
deleted file mode 100644
index 441e227..0000000
--- a/src/Misc/SodiumKeygen.php
+++ /dev/null
@@ -1,139 +0,0 @@
-<?php
-
-namespace AbmmHasan\SafeGuard\Misc;
-
-use Exception;
-use SodiumException;
-
-final class SodiumKeygen
-{
- /**
- * Sodium Session Exchange keygen (Asymmetric)
- *
- * @param string|null $seed (optional) Seed for deterministic key generation
- * @return object Key resource
- * @throws SodiumException|Exception
- */
- public static function sessionExchange(string $seed = null): object
- {
- if (!is_null($seed)) {
- if (($length = strlen($seed)) !== 32) {
- throw new Exception("Invalid Seed size (Expected: 32B, Found: {$length}B)!");
- }
- $keypair = sodium_crypto_kx_seed_keypair($seed);
- } else {
- $keypair = sodium_crypto_kx_keypair();
- }
-
- return (object)[
- 'keypair' => $keypair,
- 'private' => sodium_crypto_kx_secretkey($keypair),
- 'public' => sodium_crypto_kx_publickey($keypair)
- ];
- }
-
- /**
- * Sodium Sign & Detached Sign keygen (Asymmetric)
- *
- * @param string|null $seed (optional) Seed for deterministic key generation
- * @return object Key resource
- * @throws SodiumException|Exception
- */
- public static function sign(string $seed = null): object
- {
- if (!is_null($seed)) {
- if (($length = strlen($seed)) !== SODIUM_CRYPTO_SIGN_SEEDBYTES) {
- throw new Exception(
- "Invalid Seed size (Expected: " . SODIUM_CRYPTO_SIGN_SEEDBYTES . "B, Found: {$length}B)!"
- );
- }
- $keypair = sodium_crypto_sign_seed_keypair($seed);
- } else {
- $keypair = sodium_crypto_sign_keypair();
- }
-
- return (object)[
- 'private' => sodium_crypto_sign_secretkey($keypair),
- 'public' => sodium_crypto_sign_publickey($keypair)
- ];
- }
-
- /**
- * Sodium Box & Seal keygen (Asymmetric)
- *
- * @param string|null $seed (optional) Seed for deterministic key generation
- * @return object Key resource
- * @throws SodiumException|Exception
- */
- public static function box(string $seed = null): object
- {
- if (!is_null($seed)) {
- if (($length = strlen($seed)) !== 32) {
- throw new Exception("Invalid Seed size (Expected: 32B, Found: {$length}B)!");
- }
- $keypair = sodium_crypto_box_seed_keypair($seed);
- } else {
- $keypair = sodium_crypto_box_keypair();
- }
-
- return (object)[
- 'private' => sodium_crypto_box_secretkey($keypair),
- 'public' => sodium_crypto_box_publickey($keypair),
- 'nonce' => random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES)
- ];
- }
-
- /**
- * Sodium Auth keygen (Symmetric)
- *
- * @return string Key resource
- */
- public static function auth(): string
- {
- return sodium_crypto_auth_keygen();
- }
-
- /**
- * Sodium Secret Stream keygen (Symmetric; xchacha20poly1305)
- *
- * @return string Key resource
- */
- public static function secretStream(): string
- {
- return sodium_crypto_secretstream_xchacha20poly1305_keygen();
- }
-
- /**
- * Sodium Short Hash keygen
- *
- * @return string Key resource
- */
- public static function shortHash(): string
- {
- return sodium_crypto_shorthash_keygen();
- }
-
- /**
- * Sodium Generic Hash keygen
- *
- * @return string Key resource
- */
- public static function genericHash(): string
- {
- return sodium_crypto_generichash_keygen();
- }
-
- /**
- * Sodium Secret Box keygen (Symmetric)
- *
- * @return object Key resource
- * @throws Exception
- */
- public static function secretBox(): object
- {
- return (object)[
- 'key' => sodium_crypto_secretbox_keygen(),
- 'nonce' => random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES)
- ];
- }
-}
diff --git a/src/Sodium/SodiumAead.php b/src/Sodium/SodiumAead.php
new file mode 100644
index 0000000..62e1dae
--- /dev/null
+++ b/src/Sodium/SodiumAead.php
@@ -0,0 +1,151 @@
+<?php
+
+namespace Infocyph\Epicrypt\Sodium;
+
+use Exception;
+use Infocyph\Epicrypt\Exceptions\SodiumCryptoException;
+use SodiumException;
+
+final class Aead
+{
+ private array $keyLength = [
+ 'aes-256-gcm' => 12,
+ 'chacha20-poly1305' => 8,
+ 'chacha20-poly1305-ietf' => 12,
+ 'xchacha20-poly1305-ietf' => 24
+ ];
+ private array $algorithms = [
+ 'aes-256-gcm' => 'aes256gcm',
+ 'chacha20-poly1305' => 'chacha20poly1305',
+ 'chacha20-poly1305-ietf' => 'chacha20poly1305_ietf',
+ 'xchacha20-poly1305-ietf' => 'xchacha20poly1305_ietf'
+ ];
+
+ /**
+ * Provide required data for operation
+ *
+ * @param string $algorithm Algorithm
+ * @param bool $isMessageBinary Message is binary?
+ * @param bool $isSecretBinary Secrets (nonce, encryption key) are binary?
+ */
+ public function __construct(
+ private readonly string $algorithm = 'xchacha20-poly1305-ietf',
+ private readonly bool $isMessageBinary = false,
+ private readonly bool $isSecretBinary = false
+ ) {
+ }
+
+ /**
+ * Encrypts the given message using the specified key and nonce.
+ *
+ * @param string $message The message to be encrypted.
+ * @param string $key The key used for encryption.
+ * @param string $nonce The nonce used for encryption (must be only used once, per message).
+ * @param string $additionalData Additional, authenticated data
+ * @return string The encrypted message.
+ * @throws SodiumCryptoException|SodiumException
+ */
+ public function encrypt(
+ #[\SensitiveParameter] string $message,
+ #[\SensitiveParameter] string $key,
+ string $nonce,
+ string $additionalData
+ ): string {
+ $this->checkSupport();
+
+ if (!$this->isSecretBinary) {
+ $key = sodium_base642bin($key, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $nonce = sodium_base642bin($nonce, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ $encrypted = call_user_func_array(
+ 'sodium_crypto_aead_' . $this->algorithms[$this->algorithm] . '_encrypt',
+ [$message, $additionalData, $nonce, $key]
+ );
+
+ if ($this->isMessageBinary) {
+ return $encrypted;
+ }
+
+ return base64_encode($encrypted);
+ }
+
+ /**
+ * Decrypts the given encrypted data using the specified key and nonce.
+ *
+ * @param string $encrypted The encrypted data to be decrypted.
+ * @param string $key The key used for decryption.
+ * @param string $nonce The nonce used for decryption.
+ * @param string $additionalData Additional, authenticated data
+ * @return string|false The decrypted data.
+ * @throws SodiumCryptoException|SodiumException
+ */
+ public function decrypt(
+ string $encrypted,
+ #[\SensitiveParameter] string $key,
+ string $nonce,
+ string $additionalData
+ ): string|false {
+ $this->checkSupport();
+
+ if (!$this->isSecretBinary) {
+ $key = sodium_base642bin($key, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $nonce = sodium_base642bin($nonce, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ if (!$this->isMessageBinary) {
+ $encrypted = base64_decode($encrypted, true);
+ }
+
+ return call_user_func_array(
+ 'sodium_crypto_aead_' . $this->algorithms[$this->algorithm] . '_decrypt',
+ [$encrypted, $additionalData, $nonce, $key]
+ );
+ }
+
+ /**
+ * Get nonce, usable once per message
+ *
+ * @return string The generated nonce.
+ * @throws Exception
+ */
+ public function generateNonce(): string
+ {
+ $nonce = random_bytes($this->keyLength[$this->algorithm]);
+ return $this->isSecretBinary ? $nonce : sodium_bin2base64($nonce, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ /**
+ * Retrieves the global encryption key.
+ *
+ * @return string The encryption key.
+ * @throws SodiumCryptoException|SodiumException
+ */
+ public function generateEncryptionKey(): string
+ {
+ $this->checkSupport();
+ $encryptionKey = call_user_func('sodium_crypto_aead_' . $this->algorithms[$this->algorithm] . '_keygen');
+ return $this->isSecretBinary ? $encryptionKey : sodium_bin2base64(
+ $encryptionKey,
+ SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING
+ );
+ }
+
+ /**
+ * Checks if the specified algorithm is supported and if hardware support is available for AES-256-GCM.
+ *
+ * @throws SodiumCryptoException
+ */
+ private function checkSupport(): void
+ {
+ if (!isset($this->algorithms[$this->algorithm])) {
+ throw new SodiumCryptoException(
+ "Invalid algorithm! Available: " . implode(', ', array_keys($this->algorithms))
+ );
+ }
+
+ if ($this->algorithm === 'aes-256-gcm' && !sodium_crypto_aead_aes256gcm_is_available()) {
+ throw new SodiumCryptoException('Hardware support not available for AES-256-GCM!');
+ }
+ }
+}
diff --git a/src/Sodium/SodiumAuth.php b/src/Sodium/SodiumAuth.php
new file mode 100644
index 0000000..e169dd2
--- /dev/null
+++ b/src/Sodium/SodiumAuth.php
@@ -0,0 +1,76 @@
+<?php
+
+namespace Infocyph\Epicrypt\Sodium;
+
+use SodiumException;
+
+final readonly class Auth
+{
+
+ /**
+ * Constructor for the SodiumAuth class.
+ *
+ * @param bool $isMessageBinary (optional) Whether the authentication result should be in binary format.
+ * @param bool $isSecretBinary Secret is binary?
+ */
+ public function __construct(
+ private bool $isMessageBinary = false,
+ private bool $isSecretBinary = false
+ ) {
+ }
+
+ /**
+ * Compute the authenticity of a message.
+ *
+ * @param string $message The message to be authenticated.
+ * @param string $secret The secret key used for authenticity.
+ * @return string The computed authentication in binary format or base64-encoded format.
+ * @throws SodiumException
+ */
+ public function compute(string $message, #[\SensitiveParameter] string $secret): string
+ {
+ if (!$this->isSecretBinary) {
+ $secret = sodium_base642bin($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+ $encrypted = sodium_crypto_auth($message, $secret);
+ if ($this->isMessageBinary) {
+ return $encrypted;
+ }
+ return base64_encode($encrypted);
+ }
+
+ /**
+ * Verify the authenticity of a message using provided signature.
+ *
+ * @param string $message The message to verify.
+ * @param string $secret The secret key used for authenticity.
+ * @param string $signature The signature to verify against.
+ * @return bool Returns true if the message is authentic, false otherwise.
+ * @throws SodiumException
+ */
+ public function verify(string $message, #[\SensitiveParameter] string $secret, string $signature): bool
+ {
+ if (!$this->isMessageBinary) {
+ $signature = base64_decode($signature, true);
+ }
+ if (!$this->isSecretBinary) {
+ $secret = sodium_base642bin($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+ if (sodium_crypto_auth_verify($signature, $message, $secret)) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Generate a cryptographic authentication key.
+ *
+ * @return string The generated authentication key.
+ * @throws SodiumException
+ */
+ public function generateSecret(): string
+ {
+ $secret = sodium_crypto_auth_keygen();
+ return $this->isSecretBinary ? $secret : sodium_bin2base64($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+}
diff --git a/src/Sodium/SodiumCryptoBox.php b/src/Sodium/SodiumCryptoBox.php
new file mode 100644
index 0000000..bfef01e
--- /dev/null
+++ b/src/Sodium/SodiumCryptoBox.php
@@ -0,0 +1,138 @@
+<?php
+
+namespace Infocyph\Epicrypt\Sodium;
+
+use Exception;
+use Infocyph\Epicrypt\Exceptions\SodiumCryptoException;
+use SodiumException;
+
+final readonly class CryptoBox
+{
+ /**
+ * @param bool $isMessageBinary (optional) Message in binary format?
+ * @param bool $isSecretBinary Secrets(Third party public key, Own private key, Shared secret) are binary?
+ */
+ public function __construct(
+ private bool $isMessageBinary = false,
+ private bool $isSecretBinary = false
+ ) {
+ }
+
+ /**
+ * Encrypt the message
+ *
+ * @param string $message Message for encryption
+ * @param string $thirdPartyPublicKey Third party public key
+ * @param string $ownPrivateKey Own private key
+ * @param string $sharedSecret Shared secret (only used once per message)
+ * @return string Encrypted message
+ * @throws SodiumException
+ */
+ public function encrypt(
+ #[\SensitiveParameter] string $message,
+ #[\SensitiveParameter] string $thirdPartyPublicKey,
+ #[\SensitiveParameter] string $ownPrivateKey,
+ string $sharedSecret
+ ): string {
+ if (!$this->isSecretBinary) {
+ $thirdPartyPublicKey = sodium_base642bin($thirdPartyPublicKey, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $ownPrivateKey = sodium_base642bin($ownPrivateKey, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $sharedSecret = sodium_base642bin($sharedSecret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ $encrypted = sodium_crypto_box(
+ $message,
+ $sharedSecret,
+ sodium_crypto_box_keypair_from_secretkey_and_publickey(
+ $ownPrivateKey,
+ $thirdPartyPublicKey
+ )
+ );
+
+ if ($this->isMessageBinary) {
+ return $encrypted;
+ }
+
+ return base64_encode($encrypted);
+ }
+
+ /**
+ * Decrypt the message
+ *
+ * @param string $encryptedMessage Encrypted message
+ * @param string $thirdPartyPublicKey Third party public key
+ * @param string $ownPrivateKey Own private key
+ * @param string $sharedSecret Shared secret
+ * @return false|string Decrypted message
+ * @throws SodiumException
+ */
+ public function decrypt(
+ string $encryptedMessage,
+ #[\SensitiveParameter] string $thirdPartyPublicKey,
+ #[\SensitiveParameter] string $ownPrivateKey,
+ string $sharedSecret
+ ): bool|string {
+ if (!$this->isMessageBinary) {
+ $encryptedMessage = base64_decode($encryptedMessage, true);
+ }
+ if (!$this->isSecretBinary) {
+ $thirdPartyPublicKey = sodium_base642bin($thirdPartyPublicKey, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $ownPrivateKey = sodium_base642bin($ownPrivateKey, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $sharedSecret = sodium_base642bin($sharedSecret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ return sodium_crypto_box_open(
+ $encryptedMessage,
+ $sharedSecret,
+ sodium_crypto_box_keypair_from_secretkey_and_publickey(
+ $ownPrivateKey,
+ $thirdPartyPublicKey
+ )
+ );
+ }
+
+ /**
+ * Generates a secret key pair for the Sodium Crypto Box algorithm.
+ *
+ * @param string|null $seed (optional) Seed for deterministic key generation. If provided, it must be 32 bytes long.
+ * @return array An array containing the private key, public key and shared key.
+ * @throws SodiumCryptoException|SodiumException|Exception
+ */
+ public function generateSecretPair(string $seed = null): array
+ {
+ if (!is_null($seed)) {
+ if (($length = strlen($seed)) !== 32) {
+ throw new SodiumCryptoException("Invalid Seed size (Expected: 32B, Found: {$length}B)!");
+ }
+ $keypair = sodium_crypto_box_seed_keypair($seed);
+ } else {
+ $keypair = sodium_crypto_box_keypair();
+ }
+
+ $keys = [
+ 'private' => sodium_crypto_box_secretkey($keypair),
+ 'public' => sodium_crypto_box_publickey($keypair)
+ ];
+
+ if ($this->isSecretBinary) {
+ return $keys;
+ }
+
+ return [
+ 'private' => sodium_bin2base64($keys['private'], SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING),
+ 'public' => sodium_bin2base64($keys['public'], SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING)
+ ];
+ }
+
+ /**
+ * Generates a shared secret
+ *
+ * @return string The generated shared secret.
+ * @throws SodiumException|Exception
+ */
+ public function generateSharedSecret(): string
+ {
+ $secret = random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
+ return $this->isSecretBinary ? $secret : sodium_bin2base64($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+}
diff --git a/src/Sodium/SodiumCryptoSeal.php b/src/Sodium/SodiumCryptoSeal.php
new file mode 100644
index 0000000..8fb2c9f
--- /dev/null
+++ b/src/Sodium/SodiumCryptoSeal.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Infocyph\Epicrypt\Sodium;
+
+use Infocyph\Epicrypt\Exceptions\SodiumCryptoException;
+use SodiumException;
+
+final readonly class CryptoSeal
+{
+
+ /**
+ * Set predefined property
+ *
+ * @param bool $isMessageBinary Is transportable resource binary?
+ */
+ public function __construct(
+ private bool $isMessageBinary = false,
+ private bool $isSecretBinary = false
+ ) {
+ }
+
+ /**
+ * Encrypt the message (using recipient public key)
+ *
+ * @param string $message Message for encryption
+ * @param string $publicKey Public key
+ * @return string Encrypted message
+ * @throws SodiumException
+ */
+ public function encrypt(#[\SensitiveParameter] string $message, string $publicKey): string
+ {
+ if (!$this->isSecretBinary) {
+ $publicKey = sodium_base642bin($publicKey, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ $encrypted = sodium_crypto_box_seal($message, $publicKey);
+
+ if ($this->isMessageBinary) {
+ return $encrypted;
+ }
+
+ return base64_encode($encrypted);
+ }
+
+ /**
+ * Decrypt the message (using recipient keypair)
+ *
+ * @param string $encrypted Encrypted message
+ * @param string $keypair The keypair
+ * @return false|string Decrypted message
+ * @throws SodiumException
+ */
+ public function decrypt(string $encrypted, #[\SensitiveParameter] string $keypair): bool|string
+ {
+ if (!$this->isSecretBinary) {
+ $keypair = sodium_base642bin($keypair, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ if (!$this->isMessageBinary) {
+ $encrypted = base64_decode($encrypted, true);
+ }
+
+ return sodium_crypto_box_seal_open($encrypted, $keypair);
+ }
+
+ /**
+ * Generates a secret key pair for the Sodium Crypto Box algorithm.
+ *
+ * @param string|null $seed (optional) Seed for deterministic key generation. If provided, it must be 32 bytes long.
+ * @return array An array containing the keypair and public key.
+ * @throws SodiumCryptoException|SodiumException
+ */
+ public function generateSecretPair(string $seed = null): array
+ {
+ if (!is_null($seed)) {
+ if (($length = strlen($seed)) !== 32) {
+ throw new SodiumCryptoException("Invalid Seed size (Expected: 32B, Found: {$length}B)!");
+ }
+ $keypair = sodium_crypto_box_seed_keypair($seed);
+ } else {
+ $keypair = sodium_crypto_box_keypair();
+ }
+
+ $keys = [
+ 'keypair' => $keypair,
+ 'public' => sodium_crypto_box_publickey($keypair)
+ ];
+
+ if ($this->isSecretBinary) {
+ return $keys;
+ }
+
+ return [
+ 'keypair' => sodium_bin2base64($keys['keypair'], SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING),
+ 'public' => sodium_bin2base64($keys['public'], SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING)
+ ];
+ }
+}
diff --git a/src/Sodium/SodiumSecretBox.php b/src/Sodium/SodiumSecretBox.php
new file mode 100644
index 0000000..3e728bb
--- /dev/null
+++ b/src/Sodium/SodiumSecretBox.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Infocyph\Epicrypt\Sodium;
+
+use Exception;
+use SodiumException;
+
+final readonly class SecretBox
+{
+ /**
+ * Constructor for the class.
+ *
+ * @param bool $isMessageBinary (optional) Whether the message is in binary format. Default is false.
+ * @param bool $isSecretBinary (optional) Whether the secret is in binary format. Default is false.
+ */
+ public function __construct(
+ private bool $isMessageBinary = false,
+ private bool $isSecretBinary = false
+ ) {
+ }
+
+ /**
+ * Encrypts the given message using the specified secret and nonce.
+ *
+ * @param string $message The message to be encrypted.
+ * @param string $secret The secret key used for encryption.
+ * @param string $nonce The nonce used for encryption (must be unique per message).
+ * @return string The encrypted message in binary format if $isMessageBinary is true, otherwise in base64 format.
+ * @throws SodiumException
+ */
+ public function encrypt(
+ #[\SensitiveParameter] string $message,
+ #[\SensitiveParameter] string $secret,
+ string $nonce
+ ): string {
+ if (!$this->isSecretBinary) {
+ $secret = sodium_base642bin($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $nonce = sodium_base642bin($nonce, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+ $encrypted = sodium_crypto_secretbox($message, $nonce, $secret);
+ if ($this->isMessageBinary) {
+ return $encrypted;
+ }
+ return base64_encode($encrypted);
+ }
+
+ /**
+ * Decrypts an encrypted message using the specified secret and nonce.
+ *
+ * @param string $encryptedMessage The encrypted message to be decrypted.
+ * @param string $secret The secret key used for decryption.
+ * @param string $nonce The nonce used for decryption (must be the same as the one used for encryption).
+ * @return string|bool The decrypted message in binary format if $isMessageBinary is true, otherwise in base64 format. Returns false if decryption fails.
+ * @throws SodiumException
+ */
+ public function decrypt(string $encryptedMessage, #[\SensitiveParameter] string $secret, string $nonce): string|bool
+ {
+ if (!$this->isSecretBinary) {
+ $secret = sodium_base642bin($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ $nonce = sodium_base642bin($nonce, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+ if (!$this->isMessageBinary) {
+ $encryptedMessage = base64_decode($encryptedMessage);
+ }
+ return sodium_crypto_secretbox_open($encryptedMessage, $nonce, $secret);
+ }
+
+ /**
+ * Generates a random nonce.
+ *
+ * @return string The generated nonce.
+ * @throws SodiumException|Exception
+ */
+ public function generateNonce(): string
+ {
+ $secret = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
+ return $this->isSecretBinary ? $secret : sodium_bin2base64($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+
+ /**
+ * Generates a secret key.
+ *
+ * @return string The generated secret key.
+ * @throws SodiumException
+ */
+ public function generateSecret(): string
+ {
+ $secret = sodium_crypto_secretbox_keygen();
+ return $this->isSecretBinary ? $secret : sodium_bin2base64($secret, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
+ }
+}
diff --git a/src/Sodium/SodiumSecretStream.php b/src/Sodium/SodiumSecretStream.php
new file mode 100644
index 0000000..f67f3dc
--- /dev/null
+++ b/src/Sodium/SodiumSecretStream.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Infocyph\Epicrypt\Sodium;
+
+
+use Infocyph\Epicrypt\Exceptions\FileAccessException;
+use Infocyph\Epicrypt\Misc\ReadFile;
+use SodiumException;
+