|
| 1 | +--TEST-- |
| 2 | +openssl_decrypt() with GCM-SIV cipher algorithm tests |
| 3 | +--EXTENSIONS-- |
| 4 | +openssl |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +if (!in_array('aes-256-gcm-siv', openssl_get_cipher_methods())) { |
| 8 | + die("skip aes-256-gcm-siv not available (requires OpenSSL >= 3.2)"); |
| 9 | +} |
| 10 | +?> |
| 11 | +--FILE-- |
| 12 | +<?php |
| 13 | +require_once __DIR__ . "/cipher_tests.inc"; |
| 14 | +$method = 'aes-256-gcm-siv'; |
| 15 | +$tests = openssl_get_cipher_tests($method); |
| 16 | + |
| 17 | +foreach ($tests as $idx => $test) { |
| 18 | + echo "TEST $idx\n"; |
| 19 | + $pt = openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, |
| 20 | + $test['iv'], $test['tag'], $test['aad']); |
| 21 | + var_dump($test['pt'] === $pt); |
| 22 | +} |
| 23 | + |
| 24 | +// Use the last vector (non-empty pt + AAD) for tampering checks. |
| 25 | +echo "TEST WRONGTAG\n"; |
| 26 | +$bad_tag = $test['tag']; |
| 27 | +$bad_tag[0] = chr(ord($bad_tag[0]) ^ 0x01); |
| 28 | +var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, |
| 29 | + $test['iv'], $bad_tag, $test['aad'])); |
| 30 | + |
| 31 | +echo "TEST WRONGCT\n"; |
| 32 | +$bad_ct = $test['ct']; |
| 33 | +$bad_ct[0] = chr(ord($bad_ct[0]) ^ 0x01); |
| 34 | +var_dump(openssl_decrypt($bad_ct, $method, $test['key'], OPENSSL_RAW_DATA, |
| 35 | + $test['iv'], $test['tag'], $test['aad'])); |
| 36 | + |
| 37 | +echo "TEST WRONGNONCE\n"; |
| 38 | +$bad_iv = $test['iv']; |
| 39 | +$bad_iv[0] = chr(ord($bad_iv[0]) ^ 0x01); |
| 40 | +var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, |
| 41 | + $bad_iv, $test['tag'], $test['aad'])); |
| 42 | + |
| 43 | +echo "TEST WRONGAAD\n"; |
| 44 | +var_dump(openssl_decrypt($test['ct'], $method, $test['key'], OPENSSL_RAW_DATA, |
| 45 | + $test['iv'], $test['tag'], 'unexpected aad')); |
| 46 | +?> |
| 47 | +--EXPECT-- |
| 48 | +TEST 0 |
| 49 | +bool(true) |
| 50 | +TEST 1 |
| 51 | +bool(true) |
| 52 | +TEST 2 |
| 53 | +bool(true) |
| 54 | +TEST WRONGTAG |
| 55 | +bool(false) |
| 56 | +TEST WRONGCT |
| 57 | +bool(false) |
| 58 | +TEST WRONGNONCE |
| 59 | +bool(false) |
| 60 | +TEST WRONGAAD |
| 61 | +bool(false) |
0 commit comments