1--TEST--
2Test mcrypt_cbc() function : basic functionality
3--SKIPIF--
4<?php
5if (!extension_loaded("mcrypt")) {
6	print "skip - mcrypt extension not loaded";
7}
8?>
9--FILE--
10<?php
11/* Prototype  : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv)
12 * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv
13 * Source code: ext/mcrypt/mcrypt.c
14 * Alias to functions:
15 */
16
17$cipher = MCRYPT_TRIPLEDES;
18$data = b"This is the secret message which must be encrypted";
19
20// tripledes uses keys with exactly 192 bits (24 bytes)
21$keys = array(
22   b'12345678',
23   b'12345678901234567890',
24   b'123456789012345678901234',
25   b'12345678901234567890123456'
26);
27$data1 = array(
28   'IleMhoxiOthmHua4tFBHOw==',
29   'EeF1s6C+w1IiHj1gdDn81g==',
30   'EEuXpjZPueyYoG0LGQ199Q==',
31   'EEuXpjZPueyYoG0LGQ199Q=='
32);
33// tripledes is a block cipher of 64 bits (8 bytes)
34$ivs = array(
35   b'1234',
36   b'12345678',
37   b'123456789'
38);
39   // data represented in base64 (ascii)
40$data2 = array(
41   '+G7nGcWIxij3TZjpI9lJdQ==',
42   '3bJiFMeyScxOLQcE6mZtLg==',
43   '+G7nGcWIxij3TZjpI9lJdQ=='
44);
45
46$iv = b'12345678';
47echo "\n--- testing different key lengths\n";
48for ($i = 0; $i < sizeof($keys); $i++) {
49   echo "\nkey length=".strlen($keys[$i])."\n";
50   special_var_dump(mcrypt_decrypt($cipher, $keys[$i], base64_decode($data1[$i]), MCRYPT_MODE_CBC, $iv));
51}
52
53$key = b'123456789012345678901234';
54echo "\n--- testing different iv lengths\n";
55for ($i = 0; $i < sizeof($ivs); $i++) {
56   echo "\niv length=".strlen($ivs[$i])."\n";
57   special_var_dump(mcrypt_decrypt($cipher, $key, base64_decode($data2[$i]), MCRYPT_MODE_CBC, $ivs[$i]));
58}
59
60function special_var_dump($str) {
61   var_dump(bin2hex($str));
62}
63?>
64===DONE===
65--EXPECTF--
66--- testing different key lengths
67
68key length=8
69
70Warning: mcrypt_decrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
71string(0) ""
72
73key length=20
74
75Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
76string(0) ""
77
78key length=24
79string(32) "736563726574206d6573736167650000"
80
81key length=26
82
83Warning: mcrypt_decrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
84string(0) ""
85
86--- testing different iv lengths
87
88iv length=4
89
90Warning: mcrypt_decrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d
91string(0) ""
92
93iv length=8
94string(32) "659ec947f4dc3a3b9c50de744598d3c8"
95
96iv length=9
97
98Warning: mcrypt_decrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d
99string(0) ""
100===DONE===
101