3 use PHPUnit\Framework\TestCase;
5 require_once
'hcuCommon.i';
16 $this->Cu =
"SCRUBCU";
19 $this->key_suffix = sha1($this->Cu.
":testhcucommonkey");
30 $enc_key_1 = getOpenSSLKey($this->key_suffix);
31 $enc_key_2 = getOpenSSLKey($this->key_suffix);
32 $this->assertEquals(strlen($enc_key_1), 32);
33 $this->assertEquals(strlen($enc_key_2), 32);
34 $this->assertTrue($enc_key_1 == $enc_key_2);
37 $seed = openssl_random_pseudo_bytes(8);
38 $enc_key_3 = getOpenSSLKey($seed);
39 $enc_key_4 = getOpenSSLKey($seed);
40 $this->assertEquals(strlen($enc_key_3), 32);
41 $this->assertEquals(strlen($enc_key_4), 32);
42 $this->assertTrue($enc_key_3 == $enc_key_4);
53 $seed = openssl_random_pseudo_bytes(8);
54 getOpenSSLKey($seed, $bit_size=
'invalid');
61 $message =
"test data for authentication hash";
62 $auth_hash_algo =
"sha256";
64 foreach(array(
true,
false) as $auth_hash_binary) {
66 $hash_1 = getEncryptionAuthHash($message,
70 $hash_2 = getEncryptionAuthHash($message,
74 $this->assertTrue($hash_1 == $hash_2);
77 $seed = openssl_random_pseudo_bytes(8);
78 $hash_3 = getEncryptionAuthHash($message,
82 $hash_4 = getEncryptionAuthHash($message,
86 $this->assertTrue($hash_3 == $hash_4);
99 $original_message =
"This is an aes-256-cbc encrypted text with openssl! Test with default optional arguments. Authentication hashing algorithm is sha1 and the generated hash is raw binary.";
101 $mthd =
"aes-256-cbc";
104 $auth_hash_bin =
true;
107 $ciphertext = hcuOpenSSLEncrypt($original_message,
110 $auth_hash_algo=$auth_algo,
111 $auth_hash_binary=$auth_hash_bin);
112 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
116 $auth_hash_algo=$auth_algo,
117 $auth_hash_binary=$auth_hash_bin);
118 $this->assertEquals($original_message, $decrypted_message);
121 $ciphertext_from_default_call =hcuOpenSSLEncrypt($decrypted_message,
123 $decrypted_message_from_default_call = hcuOpenSSLDecrypt(
124 $ciphertext_from_default_call[
"message"],
125 $ciphertext_from_default_call[
"hash"],
127 $this->assertEquals($decrypted_message, $decrypted_message_from_default_call);
128 $this->assertEquals($decrypted_message_from_default_call, $original_message);
138 $original_message =
"This is an aes-256-cbc encrypted text with openssl! Authentication hashing algorithm is sha1 and the generated hash are lowercase hexits.";
140 $mthd =
"aes-256-cbc";
143 $auth_hash_bin =
false;
145 $ciphertext = hcuOpenSSLEncrypt($original_message,
148 $auth_hash_algo=$auth_algo,
149 $auth_hash_binary=$auth_hash_bin);
151 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
155 $auth_hash_algo=$auth_algo,
156 $auth_hash_binary=$auth_hash_bin);
158 $this->assertEquals($original_message, $decrypted_message);
168 $original_message =
"This is an aes-256-cbc encrypted text with openssl! Authentication hashing algorithm is sha256 and the generated hash is tested for both raw binary and hexits.";
170 $mthd =
"aes-256-cbc";
171 $auth_algo =
"sha256";
173 foreach(array(
true,
false) as $auth_hash_bin) {
174 $ciphertext = array(
"message" =>
"",
"hash" =>
"");
175 $ciphertext = hcuOpenSSLEncrypt($original_message,
178 $auth_hash_algo=$auth_algo,
179 $auth_hash_binary=$auth_hash_bin);
181 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
185 $auth_hash_algo=$auth_algo,
186 $auth_hash_binary=$auth_hash_bin);
188 $this->assertEquals($original_message, $decrypted_message);
202 $original_message =
"This is an aes-256-ecb encrypted text with openssl! Authentication hashing algorithm is sha256 and the generated hash are raw binary. Initialization vector (IV) is also a part of the ciphertext. However, ECB does not use IV (i.e. iv size is zero). Therefore the main openssl based encrypt and decrypt functions should be agnoistic of the iv size (or choice of cipher mode) as well.";
204 $mthd =
"aes-256-ecb";
205 $auth_algo =
"sha256";
207 foreach(array(
true,
false) as $auth_hash_bin) {
208 $ciphertext = array(
"message" =>
"",
"hash" =>
"");
209 $ciphertext = hcuOpenSSLEncrypt($original_message,
212 $auth_hash_algo=$auth_algo,
213 $auth_hash_binary=$auth_hash_bin);
215 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
219 $auth_hash_algo=$auth_algo,
220 $auth_hash_binary=$auth_hash_bin);
222 $this->assertEquals($original_message, $decrypted_message);
233 $original_message =
"This is a des-ede3-cbc encrypted text with openssl! Authentication hashing algorithm is sha256 and the generated hash are tested for both raw and hexits.";
235 $mthd =
"des-ede3-cbc";
236 $auth_algo =
"sha256";
238 foreach(array(
false,
true) as $auth_hash_bin){
239 $ciphertext = array(
"message" =>
"",
"hash" =>
"");
240 $ciphertext = hcuOpenSSLEncrypt($original_message,
243 $auth_hash_algo=$auth_algo,
244 $auth_hash_binary=$auth_hash_bin);
246 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
250 $auth_hash_algo=$auth_algo,
251 $auth_hash_binary=$auth_hash_bin);
253 $this->assertEquals($original_message, $decrypted_message);
264 $original_message =
"This is des-ede3 [ecb] encrypted text with openssl! Authentication hashing algorithm is sha256 and the generated hash are tested for both raw and hexits.";
267 $auth_algo =
"sha256";
269 foreach(array(
false,
true) as $auth_hash_bin){
270 $ciphertext = array(
"message" =>
"",
"hash" =>
"");
271 $ciphertext = hcuOpenSSLEncrypt($original_message,
274 $auth_hash_algo=$auth_algo,
275 $auth_hash_binary=$auth_hash_bin);
277 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
281 $auth_hash_algo=$auth_algo,
282 $auth_hash_binary=$auth_hash_bin);
284 $this->assertEquals($original_message, $decrypted_message);
294 $original_message =
"This is cast5-ecb encrypted text with openssl! Authentication hashing algorithm is sha256 and the generated hash are tested for both raw and hexits.";
297 $auth_algo =
"sha256";
299 foreach(array(
false,
true) as $auth_hash_bin){
300 $ciphertext = array(
"message" =>
"",
"hash" =>
"");
301 $ciphertext = hcuOpenSSLEncrypt($original_message,
304 $auth_hash_algo=$auth_algo,
305 $auth_hash_binary=$auth_hash_bin);
307 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
311 $auth_hash_algo=$auth_algo,
312 $auth_hash_binary=$auth_hash_bin);
313 $this->assertEquals($original_message, $decrypted_message);
329 $original_message =
"This is an aes-256-cbc encrypted text with openssl! Authentication hashing algorithm is sha256 and the generated hash is lowercase hexits.";
331 $mthd =
"aes-256-cbc";
332 $auth_algo =
"sha256";
333 $auth_hash_bin =
false;
336 $ciphertext = hcuOpenSSLEncrypt($original_message,
339 $auth_hash_algo=$auth_algo,
340 $auth_hash_binary=$auth_hash_bin);
343 $received_hash = $ciphertext[
"hash"];
344 $corrupted_str =
"corrupt_text";
345 $hash_length = strlen($received_hash);
346 $corrupt_str_len = strlen($corrupted_str);
347 $corrputed_hash = substr_replace($received_hash,
349 $hash_length - $corrupt_str_len,
353 $decrypted_message = hcuOpenSSLDecrypt($ciphertext[
"message"],
357 $auth_hash_algo=$auth_algo,
358 $auth_hash_binary=$auth_hash_bin);
372 $original_message =
"This is an aes-256-cbc encrypted text with openssl! Authentication hashing algorithm is sha256 and the generated hash is raw binary.";
374 $mthd =
"aes-256-cbc";
375 $auth_algo =
"invalid_hash_algo";
376 $auth_hash_bin =
true;
378 $ciphertext = hcuOpenSSLEncrypt($original_message,
381 $auth_hash_algo=$auth_algo,
382 $auth_hash_binary=$auth_hash_bin);
394 $openssl_aes_256_modes = array(
"aes-256-ctr",
402 $auth_algo =
"sha256";
403 $auth_hash_bin =
true;
405 $previous_decrypted_data =
"";
406 foreach($openssl_aes_256_modes as $aes_cipher_mode) {
407 $openssl_ciphertext = array(
"message" =>
"",
"hash" =>
"");
408 $obtained_decrypted_text =
"";
411 $openssl_ciphertext = hcuOpenSSLEncrypt($this->trusted_data,
413 $method=$aes_cipher_mode,
414 $auth_hash_algo=$auth_algo,
415 $auth_hash_binary=$auth_hash_bin);
422 $obtained_decrypted_text = hcuOpenSSLDecrypt($openssl_ciphertext[
"message"],
423 $openssl_ciphertext[
"hash"],
425 $method=$aes_cipher_mode,
426 $auth_hash_algo=$auth_algo,
427 $auth_hash_binary=$auth_hash_bin);
429 $this->assertEquals($obtained_decrypted_text, $this->trusted_data);
431 if ($previous_decrypted_data !=
"") {
432 $this->assertEquals($previous_decrypted_data, $obtained_decrypted_text);
435 $previous_decrypted_dat = $obtained_decrypted_text;
test_corrputed_hash_auth_openssl_aes_256_cbc_sha256()
test_openssl_auth_hash_correct_algo()
test_openssl_encryption_mode_agnostic_nature()
test_openssl_key_generation()
test_openssl_aes_256_cbc_sha1_raw_default()
test_openssl_des_ede3_ecb_sha256()
test_openssl_cast5_ecb_sha256()
test_openssl_aes_256_cbc_sha256()
test_openssl_aes_256_ecb_sha256()
test_openssl_aes_256_cbc_sha1_hexits_hash()
test_invalid_hash_algo_openssl_aes_256_cbc()
test_openssl_des_ede3_cbc_sha256()
test_openssl_key_generation_invalid_size()