1--TEST--
2openssl_encrypt() with ChaCha20 and Poly1305 cipher algorithm tests
3--EXTENSIONS--
4openssl
5--SKIPIF--
6<?php
7if (!in_array('chacha20-poly1305', openssl_get_cipher_methods()))
8    die("skip: chacha20-poly1305 not available");
9?>
10--FILE--
11<?php
12require_once __DIR__ . "/cipher_tests.inc";
13$method = 'chacha20-poly1305';
14$tests = openssl_get_cipher_tests($method);
15
16foreach ($tests as $idx => $test) {
17    echo "TEST $idx\n";
18    $ct = openssl_encrypt($test['pt'], $method, $test['key'], OPENSSL_RAW_DATA,
19        $test['iv'], $tag, $test['aad'], strlen($test['tag']));
20    var_dump($test['ct'] === $ct);
21    var_dump($test['tag'] === $tag);
22}
23
24// Empty IV error
25var_dump(openssl_encrypt('data', $method, 'password', 0, '', $tag, ''));
26
27// Failing to retrieve tag (max is 16 bytes)
28var_dump(openssl_encrypt('data', $method, 'password', 0, str_repeat('x', 12), $tag, '', 20));
29
30// Failing when no tag supplied
31var_dump(openssl_encrypt('data', $method, 'password', 0, str_repeat('x', 12)));
32?>
33--EXPECTF--
34TEST 0
35bool(true)
36bool(true)
37TEST 1
38bool(true)
39bool(true)
40
41Warning: openssl_encrypt(): Setting of IV length for AEAD mode failed in %s on line %d
42bool(false)
43
44Warning: openssl_encrypt(): Retrieving verification tag failed in %s on line %d
45bool(false)
46
47Warning: openssl_encrypt(): A tag should be provided when using AEAD mode in %s on line %d
48bool(false)
49