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
17echo "*** Testing mcrypt_cbc() : basic functionality ***\n";
18
19
20$cipher = MCRYPT_TRIPLEDES;
21$data = b"This is the secret message which must be encrypted";
22$mode = MCRYPT_DECRYPT;
23
24// tripledes uses keys upto 192 bits (24 bytes)
25$keys = array(
26   b'12345678',
27   b'12345678901234567890',
28   b'123456789012345678901234',
29   b'12345678901234567890123456'
30);
31$data1 = array(
32   'IleMhoxiOthmHua4tFBHOw==',
33   'EeF1s6C+w1IiHj1gdDn81g==',
34   'EEuXpjZPueyYoG0LGQ199Q==',
35   'EEuXpjZPueyYoG0LGQ199Q=='
36);
37// tripledes is a block cipher of 64 bits (8 bytes)
38$ivs = array(
39   b'1234',
40   b'12345678',
41   b'123456789'
42);
43   // data represented in base64 (ascii)
44$data2 = array(
45   '+G7nGcWIxij3TZjpI9lJdQ==',
46   '3bJiFMeyScxOLQcE6mZtLg==',
47   '+G7nGcWIxij3TZjpI9lJdQ=='
48);
49
50$iv = b'12345678';
51echo "\n--- testing different key lengths\n";
52for ($i = 0; $i < sizeof($keys); $i++) {
53   echo "\nkey length=".strlen($keys[$i])."\n";
54   special_var_dump(mcrypt_cbc($cipher, $keys[$i], base64_decode($data1[$i]), $mode, $iv));
55}
56
57$key = b'1234567890123456';
58echo "\n--- testing different iv lengths\n";
59for ($i = 0; $i < sizeof($ivs); $i++) {
60   echo "\niv length=".strlen($ivs[$i])."\n";
61   special_var_dump(mcrypt_cbc($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i]));
62}
63
64function special_var_dump($str) {
65   var_dump(bin2hex($str));
66}
67?>
68===DONE===
69--EXPECTF--
70*** Testing mcrypt_cbc() : basic functionality ***
71
72--- testing different key lengths
73
74key length=8
75
76Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
77string(32) "736563726574206d6573736167650000"
78
79key length=20
80
81Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
82string(32) "736563726574206d6573736167650000"
83
84key length=24
85
86Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
87string(32) "736563726574206d6573736167650000"
88
89key length=26
90
91Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
92
93Warning: mcrypt_cbc(): Size of key is too large for this algorithm in %s on line %d
94string(32) "736563726574206d6573736167650000"
95
96--- testing different iv lengths
97
98iv length=4
99
100Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
101
102Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
103string(32) "736563726574206d6573736167650000"
104
105iv length=8
106
107Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
108string(32) "736563726574206d6573736167650000"
109
110iv length=9
111
112Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
113
114Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
115string(32) "736563726574206d6573736167650000"
116===DONE===
117