1--TEST-- 2Check for libsodium AEAD 3--SKIPIF-- 4<?php 5if (!extension_loaded("sodium")) print "skip extension not loaded"; 6if (!defined('SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES')) print "skip libsodium without AESGCM"; 7?> 8--FILE-- 9<?php 10echo "aead_chacha20poly1305:\n"; 11 12$msg = random_bytes(random_int(1, 1000)); 13$nonce = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES); 14$key = sodium_crypto_aead_chacha20poly1305_keygen(); 15$ad = random_bytes(random_int(1, 1000)); 16 17$ciphertext = sodium_crypto_aead_chacha20poly1305_encrypt($msg, $ad, $nonce, $key); 18$msg2 = sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $ad, $nonce, $key); 19var_dump($ciphertext !== $msg); 20var_dump($msg === $msg2); 21var_dump(sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, 'x' . $ad, $nonce, $key)); 22try { 23 // Switched order 24 $msg2 = sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $ad, $key, $nonce); 25 var_dump(false); 26} catch (SodiumException $ex) { 27 var_dump(true); 28} 29 30echo "aead_chacha20poly1305_ietf:\n"; 31 32if (SODIUM_LIBRARY_MAJOR_VERSION > 7 || 33 (SODIUM_LIBRARY_MAJOR_VERSION == 7 && 34 SODIUM_LIBRARY_MINOR_VERSION >= 6)) { 35 $msg = random_bytes(random_int(1, 1000)); 36 $nonce = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES); 37 $key = sodium_crypto_aead_chacha20poly1305_ietf_keygen(); 38 $ad = random_bytes(random_int(1, 1000)); 39 40 $ciphertext = sodium_crypto_aead_chacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key); 41 $msg2 = sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, $ad, $nonce, $key); 42 var_dump($ciphertext !== $msg); 43 var_dump($msg === $msg2); 44 var_dump(sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, 'x' . $ad, $nonce, $key)); 45 try { 46 // Switched order 47 $msg2 = sodium_crypto_aead_chacha20poly1305_ietf_decrypt($ciphertext, $ad, $key, $nonce); 48 var_dump(false); 49 } catch (SodiumException $ex) { 50 var_dump(true); 51 } 52} else { 53 var_dump(true); 54 var_dump(true); 55 var_dump(false); 56 var_dump(true); 57} 58 59echo "aead_xchacha20poly1305_ietf:\n"; 60 61if (SODIUM_LIBRARY_MAJOR_VERSION > 9 || 62 (SODIUM_LIBRARY_MAJOR_VERSION == 9 && 63 SODIUM_LIBRARY_MINOR_VERSION >= 4)) { 64 $msg = random_bytes(random_int(1, 1000)); 65 $nonce = random_bytes(SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES); 66 $key = sodium_crypto_aead_xchacha20poly1305_ietf_keygen(); 67 $ad = random_bytes(random_int(1, 1000)); 68 69 $ciphertext = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($msg, $ad, $nonce, $key); 70 $msg2 = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $ad, $nonce, $key); 71 var_dump($ciphertext !== $msg); 72 var_dump($msg === $msg2); 73 var_dump(sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, 'x' . $ad, $nonce, $key)); 74 try { 75 // Switched order 76 $msg2 = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $ad, $key, $nonce); 77 var_dump(false); 78 } catch (SodiumException $ex) { 79 var_dump(true); 80 } 81} else { 82 var_dump(true); 83 var_dump(true); 84 var_dump(false); 85 var_dump(true); 86} 87 88echo "aead_aes256gcm:\n"; 89 90if (sodium_crypto_aead_aes256gcm_is_available()) { 91 $msg = random_bytes(random_int(1, 1000)); 92 $nonce = random_bytes(SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES); 93 $ad = random_bytes(random_int(1, 1000)); 94 $key = sodium_crypto_aead_aes256gcm_keygen(); 95 $ciphertext = sodium_crypto_aead_aes256gcm_encrypt($msg, $ad, $nonce, $key); 96 $msg2 = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $ad, $nonce, $key); 97 var_dump($ciphertext !== $msg); 98 var_dump($msg === $msg2); 99 var_dump(sodium_crypto_aead_aes256gcm_decrypt($ciphertext, 'x' . $ad, $nonce, $key)); 100 try { 101 // Switched order 102 $msg2 = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $ad, $key, $nonce); 103 var_dump(false); 104 } catch (SodiumException $ex) { 105 var_dump(true); 106 } 107} else { 108 var_dump(true); 109 var_dump(true); 110 var_dump(false); 111 var_dump(true); 112} 113?> 114--EXPECT-- 115aead_chacha20poly1305: 116bool(true) 117bool(true) 118bool(false) 119bool(true) 120aead_chacha20poly1305_ietf: 121bool(true) 122bool(true) 123bool(false) 124bool(true) 125aead_xchacha20poly1305_ietf: 126bool(true) 127bool(true) 128bool(false) 129bool(true) 130aead_aes256gcm: 131bool(true) 132bool(true) 133bool(false) 134bool(true) 135