-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCrypto.php
781 lines (643 loc) · 15.8 KB
/
Crypto.php
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
<?php
namespace Crypto;
/**
* Class providing cipher algorithms
*/
class Cipher {
const MODE_ECB = 1;
const MODE_CBC = 2;
const MODE_CFB = 3;
const MODE_OFB = 4;
const MODE_CTR = 5;
const MODE_GCM = 6;
const MODE_CCM = 7;
const MODE_XTS = 65537;
/**
* Returns cipher algorithms
* @param bool $aliases
* @param string $prefix
* @return string
*/
public static function getAlgorithms($aliases = false, $prefix = null) {}
/**
* Finds out whether algorithm exists
* @param string $algorithm
* @return bool
*/
public static function hasAlgorithm($algorithm) {}
/**
* Finds out whether the cipher mode is defined in the used OpenSSL library
* @param int $mode
* @return bool
*/
public static function hasMode($mode) {}
/**
* Cipher magic method for calling static methods
* @param string $name
* @param array $arguments
*/
public static function __callStatic($name, $arguments) {}
/**
* Cipher constructor
* @param string $algorithm
* @param int $mode
* @param string $key_size
*/
public function __construct($algorithm, $mode = NULL, $key_size = NULL) {}
/**
* Returns cipher algorithm string
* @return string
*/
public function getAlgorithmName() {}
/**
* Initializes cipher encryption
* @param string $key
* @param string $iv
* @return bool
*/
public function encryptInit($key, $iv = null) {}
/**
* Updates cipher encryption
* @param string $data
* @return string
*/
public function encryptUpdate($data) {}
/**
* Finalizes cipher encryption
* @return string
*/
public function encryptFinish() {}
/**
* Encrypts text to ciphertext
* @param string $data
* @param string $key
* @param string $iv
* @return string
*/
public function encrypt($data, $key, $iv = null) {}
/**
* Initializes cipher decryption
* @param string $key
* @param string $iv
* @return null
*/
public function decryptInit($key, $iv = null) {}
/**
* Updates cipher decryption
* @param string $data
* @return string
*/
public function decryptUpdate($data) {}
/**
* Finalizes cipher decryption
* @return string
*/
public function decryptFinish() {}
/**
* Decrypts ciphertext to decrypted text
* @param string $data
* @param string $key
* @param string $iv
* @return string
*/
public function decrypt($data, $key, $iv = null) {}
/**
* Returns cipher block size
* @return int
*/
public function getBlockSize() {}
/**
* Returns cipher key length
* @return int
*/
public function getKeyLength() {}
/**
* Returns cipher IV length
* @return int
*/
public function getIVLength() {}
/**
* Returns cipher mode
* @return int
*/
public function getMode() {}
/**
* Returns authentication tag
* @return string
*/
public function getTag() {}
/**
* Sets authentication tag
* @param string $tag
* @return bool
*/
public function setTag($tag) {}
/**
* Set authentication tag length
* @param int $tag_length
* @return bool
*/
public function setTagLength($tag_length) {}
/**
* Sets additional application data for authenticated encryption
* @param string $aad
* @return bool
*/
public function setAAD($aad) {}
}
/**
* Exception class for cipher errors
*/
class CipherException extends \Exception {
/**
* Cipher '%s' algorithm not found
*/
const ALGORITHM_NOT_FOUND = 1;
/**
* Cipher static method '%s' not found
*/
const STATIC_METHOD_NOT_FOUND = 2;
/**
* Cipher static method %s can accept max two arguments
*/
const STATIC_METHOD_TOO_MANY_ARGS = 3;
/**
* Cipher mode not found
*/
const MODE_NOT_FOUND = 4;
/**
* Cipher mode %s is not available in installed OpenSSL library
*/
const MODE_NOT_AVAILABLE = 5;
/**
* The authentication is not supported for %s cipher mode
*/
const AUTHENTICATION_NOT_SUPPORTED = 6;
/**
* Invalid length of key for cipher '%s' algorithm (required length: %d)
*/
const KEY_LENGTH_INVALID = 7;
/**
* Invalid length of initial vector for cipher '%s' algorithm (required length: %d)
*/
const IV_LENGTH_INVALID = 8;
/**
* AAD setter has to be called before encryption or decryption
*/
const AAD_SETTER_FORBIDDEN = 9;
/**
* AAD setter failed
*/
const AAD_SETTER_FAILED = 10;
/**
* AAD length can't exceed max integer length
*/
const AAD_LENGTH_HIGH = 11;
/**
* Tag getter has to be called after encryption
*/
const TAG_GETTER_FORBIDDEN = 12;
/**
* Tag setter has to be called before decryption
*/
const TAG_SETTER_FORBIDDEN = 13;
/**
* Tag getter failed
*/
const TAG_GETTER_FAILED = 14;
/**
* Tag setter failed
*/
const TAG_SETTER_FAILED = 15;
/**
* Tag length setter has to be called before encryption
*/
const TAG_LENGTH_SETTER_FORBIDDEN = 16;
/**
* Tag length can't be lower than 32 bits (4 characters)
*/
const TAG_LENGTH_LOW = 17;
/**
* Tag length can't exceed 128 bits (16 characters)
*/
const TAG_LENGTH_HIGH = 18;
/**
* Tag verification failed
*/
const TAG_VERIFY_FAILED = 19;
/**
* Initialization of cipher algorithm failed
*/
const INIT_ALG_FAILED = 20;
/**
* Initialization of cipher context failed
*/
const INIT_CTX_FAILED = 21;
/**
* Cipher object is already used for decryption
*/
const INIT_ENCRYPT_FORBIDDEN = 22;
/**
* Cipher object is already used for encryption
*/
const INIT_DECRYPT_FORBIDDEN = 23;
/**
* Updating of cipher failed
*/
const UPDATE_FAILED = 24;
/**
* Cipher object is not initialized for encryption
*/
const UPDATE_ENCRYPT_FORBIDDEN = 25;
/**
* Cipher object is not initialized for decryption
*/
const UPDATE_DECRYPT_FORBIDDEN = 26;
/**
* Finalizing of cipher failed
*/
const FINISH_FAILED = 27;
/**
* Cipher object is not initialized for encryption
*/
const FINISH_ENCRYPT_FORBIDDEN = 28;
/**
* Cipher object is not initialized for decryption
*/
const FINISH_DECRYPT_FORBIDDEN = 29;
/**
* Input data length can't exceed max integer length
*/
const INPUT_DATA_LENGTH_HIGH = 30;
}
/**
* Class providing hash algorithms
*/
class Hash {
/**
* Returns hash algorithms
* @param bool $aliases
* @param string $prefix
* @return string
*/
public static function getAlgorithms($aliases = false, $prefix = null) {}
/**
* Finds out whether algorithm exists
* @param string $algorithm
* @return bool
*/
public static function hasAlgorithm($algorithm) {}
/**
* Hash magic method for calling static methods
* @param string $name
* @param array $arguments
*/
public static function __callStatic($name, $arguments) {}
/**
* Hash constructor
* @param string $algorithm
*/
public function __construct($algorithm) {}
/**
* Returns hash algorithm string
* @return string
*/
public function getAlgorithmName() {}
/**
* Updates hash
* @param string $data
* @return null
*/
public function update($data) {}
/**
* Return hash digest in raw foramt
* @return string
*/
public function digest() {}
/**
* Return hash digest in hex format
* @return string
*/
public function hexdigest() {}
/**
* Returns hash block size
* @return int
*/
public function getBlockSize() {}
/**
* Returns hash size
* @return int
*/
public function getSize() {}
}
/**
* Exception class for hash errors
*/
class HashException extends \Exception {
/**
* Hash algorithm '%s' not found
*/
const HASH_ALGORITHM_NOT_FOUND = 1;
/**
* Hash static method '%s' not found
*/
const STATIC_METHOD_NOT_FOUND = 2;
/**
* Hash static method %s can accept max one argument
*/
const STATIC_METHOD_TOO_MANY_ARGS = 3;
/**
* Initialization of hash failed
*/
const INIT_FAILED = 4;
/**
* Updating of hash context failed
*/
const UPDATE_FAILED = 5;
/**
* Creating of hash digest failed
*/
const DIGEST_FAILED = 6;
/**
* Input data length can't exceed max integer length
*/
const INPUT_DATA_LENGTH_HIGH = 7;
}
/**
* Abstract class for MAC subclasses
*/
abstract class MAC extends Hash {
/**
* Create a MAC (used by MAC subclasses - HMAC and CMAC)
* @param string $algorithm
* @param string $key
*/
public function __construct($algorithm, $key) {}
}
/**
* Exception class for MAC errors
*/
class MACException extends HashException {
/**
* MAC algorithm '%s' not found
*/
const MAC_ALGORITHM_NOT_FOUND = 1;
/**
* The key length for MAC is invalid
*/
const KEY_LENGTH_INVALID = 2;
}
/**
* Class providing HMAC functionality
*/
class HMAC extends MAC {
}
/**
* Class providing CMAC functionality
*/
class CMAC extends MAC {
}
/**
* Abstract class for KDF subclasses
*/
abstract class KDF {
/**
* KDF constructor
* @param int $length
* @param string $salt
*/
public function __construct($length, $salt = NULL) {}
/**
* Get key length
* @return int
*/
public function getLength() {}
/**
* Set key length
* @param int $length
* @return bool
*/
public function setLength($length) {}
/**
* Get salt
* @return string
*/
public function getSalt() {}
/**
* Set salt
* @param string $salt
* @return bool
*/
public function setSalt($salt) {}
}
/**
* Exception class for KDF errors
*/
class KDFException {
/**
* The key lenght is too low
*/
const KEY_LENGTH_LOW = 1;
/**
* The key lenght is too high
*/
const KEY_LENGTH_HIGH = 2;
/**
* The salt is too long
*/
const SALT_LENGTH_HIGH = 3;
/**
* The password is too long
*/
const PASSWORD_LENGTH_INVALID = 4;
/**
* KDF derivation failed
*/
const DERIVATION_FAILED = 5;
}
/**
* Class providing PBKDF2 functionality
*/
class PBKDF2 extends KDF {
/**
* KDF constructor
* @param string $hashAlgorithm
* @param int $length
* @param string $salt
* @param int $iterations
*/
public function __construct($hashAlgorithm, $length, $salt = NULL, $iterations = 1000) {}
/**
* Deriver hash for password
* @param string $password
* @return string
*/
public function derive($password) {}
/**
* Get iterations
* @return int
*/
public function getIterations() {}
/**
* Set iterations
* @param int $iterations
* @return bool
*/
public function setIterations($iterations) {}
/**
* Get hash algorithm
* @return string
*/
public function getHashAlgorithm() {}
/**
* Set hash algorithm
* @param string $hashAlgorithm
* @return bool
*/
public function setHashAlgorithm($hashAlgorithm) {}
}
/**
* Exception class for PBKDF2 errors
*/
class PBKDF2Exception extends KDFException {
/**
* Hash algorithm '%s' not found
*/
const HASH_ALGORITHM_NOT_FOUND = 1;
/**
* Iterations count is too high
*/
const ITERATIONS_HIGH = 2;
}
/**
* Class for base64 encoding and docoding
*/
class Base64 {
/**
* Encodes string $data to base64 encoding
* @param string $data
* @return string
*/
public function encode($data) {}
/**
* Decodes base64 string $data to raw encoding
* @param string $data
* @return string
*/
public function decode($data) {}
/**
* Base64 constructor
*/
public function __construct() {}
/**
* Encodes block of characters from $data and saves the reminder of the last block
* to the encoding context
* @param string $data
*/
public function encodeUpdate($data) {}
/**
* Encodes characters that left in the encoding context
*/
public function encodeFinish() {}
/**
* Decodes block of characters from $data and saves the reminder of the last block
* to the encoding context
* @param string $data
*/
public function decodeUpdate($data) {}
/**
* Decodes characters that left in the encoding context
*/
public function decodeFinish() {}
}
/**
* Exception class for base64 errors
*/
class Base64Exception extends \Exception {
/**
* The object is already used for decoding
*/
const ENCODE_UPDATE_FORBIDDEN = 1;
/**
* The object has not been intialized for encoding
*/
const ENCODE_FINISH_FORBIDDEN = 2;
/**
* The object is already used for encoding
*/
const DECODE_UPDATE_FORBIDDEN = 3;
/**
* The object has not been intialized for decoding
*/
const DECODE_FINISH_FORBIDDEN = 4;
/**
* Base64 decoded string does not contain valid characters
*/
const DECODE_UPDATE_FAILED = 5;
/**
* Input data length can't exceed max integer length
*/
const INPUT_DATA_LENGTH_HIGH = 6;
}
/**
* Class for generating random numbers
*/
class Rand {
/**
* Generates pseudo random bytes
* @param int $num
* @param bool $must_be_strong
* @param bool $returned_strong_result
* @return string
*/
public static function generate($num, $must_be_strong = true, &$returned_strong_result = true) {}
/**
* Mixes bytes in $buf into PRNG state
* @param string $buf
* @param float $entropy
* @return null
*/
public static function seed($buf, $entropy = (float) strlen($buf)) {}
/**
* Cleans up PRNG state
* @return null
*/
public static function cleanup() {}
/**
* Reads a number of bytes from file $filename and adds them to the PRNG. If
* max_bytes is non-negative, up to to max_bytes are read; if $max_bytes is
* negative, the complete file is read
* @param string $filename
* @param int $max_bytes
* @return int
*/
public static function loadFile($filename, $max_bytes = -1) {}
/**
* Writes a number of random bytes (currently 1024) to file $filename which can be
* used to initializethe PRNG by calling Crypto\Rand::loadFile() in a later session
* @param string $filename
* @return int
*/
public static function writeFile($filename) {}
}
/**
* Exception class for rand errors
*/
class RandException extends \Exception {
/**
* The PRNG state is not yet unpredictable
*/
const GENERATE_PREDICTABLE = 1;
/**
* The bytes written were generated without appropriate seed
*/
const FILE_WRITE_PREDICTABLE = 2;
/**
* The requested number of bytes is too high
*/
const REQUESTED_BYTES_NUMBER_TOO_HIGH = 3;
/**
* The supplied seed length is too high
*/
const SEED_LENGTH_TOO_HIGH = 4;
}