From 67270c6c56a4053558d1515641a50d88bee1fe92 Mon Sep 17 00:00:00 2001 From: Veronika Slivova Date: Tue, 11 Sep 2018 08:15:18 -0700 Subject: [PATCH] Changing CLIs for Aead, Hybrid encryption and Hybrid decryption in cross-language tests. -- Passing associated-data/context-info in a file (the same way that input plaintext/ciphertext) as it makes the JavaScript version simpler. PiperOrigin-RevId: 212454344 GitOrigin-RevId: 2518a07e3cedf9f7efb861b3982e493415a67b5d --- tools/testing/cc/aead_cli.cc | 12 +++++++----- tools/testing/cc/hybrid_decrypt_cli.cc | 12 +++++++----- tools/testing/cc/hybrid_encrypt_cli.cc | 13 ++++++++----- tools/testing/cross_language/aead_test.sh | 7 ++++--- .../cross_language/hybrid_encryption_test.sh | 7 ++++--- .../google/crypto/tink/testing/AeadCli.java | 13 +++++++------ .../crypto/tink/testing/HybridDecryptCli.java | 18 ++++++++++-------- .../crypto/tink/testing/HybridEncryptCli.java | 18 ++++++++++-------- 8 files changed, 57 insertions(+), 43 deletions(-) diff --git a/tools/testing/cc/aead_cli.cc b/tools/testing/cc/aead_cli.cc index e58e6aa3ee..4d1dca7b3e 100644 --- a/tools/testing/cc/aead_cli.cc +++ b/tools/testing/cc/aead_cli.cc @@ -32,18 +32,19 @@ using crypto::tink::KeysetHandle; // operation: the actual AEAD-operation, i.e. "encrypt" or "decrypt" // input-file: name of the file with input (plaintext for encryption, or // or ciphertext for decryption) -// associated-data: a std::string to be used as assciated data +// associated-data-file: name of the file containing associated data // output-file: name of the file for the resulting output int main(int argc, char** argv) { if (argc != 6) { std::clog << "Usage: " << argv[0] - << " keyset-file operation input-file associated-data output-file\n"; + << " keyset-file operation input-file associated-data-file " + << "output-file\n"; exit(1); } std::string keyset_filename(argv[1]); std::string operation(argv[2]); std::string input_filename(argv[3]); - std::string associated_data(argv[4]); + std::string associated_data_file(argv[4]); std::string output_filename(argv[5]); if (!(operation == "encrypt" || operation == "decrypt")) { std::clog << "Unknown operation '" << operation << "'.\n" @@ -53,8 +54,8 @@ int main(int argc, char** argv) { std::clog << "Using keyset from file " << keyset_filename << " to AEAD-" << operation << " file "<< input_filename - << " with associated data '" << associated_data << "'.\n" - << "The resulting output will be written to file " + << " with associated data from from file " << associated_data_file + << ".\n" << "The resulting output will be written to file " << output_filename << std::endl; // Init Tink; @@ -76,6 +77,7 @@ int main(int argc, char** argv) { // Read the input. std::string input = CliUtil::Read(input_filename); + std::string associated_data = CliUtil::Read(associated_data_file); // Compute the output. std::clog << operation << "ing...\n"; diff --git a/tools/testing/cc/hybrid_decrypt_cli.cc b/tools/testing/cc/hybrid_decrypt_cli.cc index 774b7bc1ee..f258c38870 100644 --- a/tools/testing/cc/hybrid_decrypt_cli.cc +++ b/tools/testing/cc/hybrid_decrypt_cli.cc @@ -30,23 +30,24 @@ using crypto::tink::KeysetHandle; // It requires 4 arguments: // keyset-file: name of the file with the keyset to be used for decryption // ciphertext-file: name of the file that contains ciphertext to be decrypted -// context-info: a std::string to be used as "context info" during the decryption +// context-info-file: name of the file that contains "context info" which +// will be used during the decryption // output-file: name of the output file for the resulting plaintext int main(int argc, char** argv) { if (argc != 5) { std::clog << "Usage: " << argv[0] - << " keyset-file ciphertext-file context-info output-file\n"; + << " keyset-file ciphertext-file context-info-file output-file\n"; exit(1); } std::string keyset_filename(argv[1]); std::string ciphertext_filename(argv[2]); - std::string context_info(argv[3]); + std::string context_info_filename(argv[3]); std::string output_filename(argv[4]); std::clog << "Using keyset from file " << keyset_filename << " to decrypt file " << ciphertext_filename - << " with context info '" << context_info << "'.\n" - << "The resulting ciphertext will be written to file " + << " with context info from file " << context_info_filename + << ".\n" << "The resulting ciphertext will be written to file " << output_filename << std::endl; // Init Tink; @@ -68,6 +69,7 @@ int main(int argc, char** argv) { // Read the ciphertext. std::string ciphertext = CliUtil::Read(ciphertext_filename); + std::string context_info = CliUtil::Read(context_info_filename); // Compute the plaintext. std::clog << "Decrypting...\n"; diff --git a/tools/testing/cc/hybrid_encrypt_cli.cc b/tools/testing/cc/hybrid_encrypt_cli.cc index 93db7d291a..e09dc4416c 100644 --- a/tools/testing/cc/hybrid_encrypt_cli.cc +++ b/tools/testing/cc/hybrid_encrypt_cli.cc @@ -27,23 +27,25 @@ using crypto::tink::KeysetHandle; // It requires 4 arguments: // keyset-file: name of the file with the keyset to be used for encryption // plaintext-file: name of the file that contains plaintext to be encrypted -// context-info: a std::string to be used as "context info" during the encryption +// context-info-file: name of the file that contains "context info" which +// will be used during the decryption // output-file: name of the output file for the resulting ciphertext int main(int argc, char** argv) { if (argc != 5) { std::clog << "Usage: " << argv[0] - << " keyset-file plaintext-file context-info output-file\n"; + << " keyset-file plaintext-file context-info-file " + << "output-file\n"; exit(1); } std::string keyset_filename(argv[1]); std::string plaintext_filename(argv[2]); - std::string context_info(argv[3]); + std::string context_info_filename(argv[3]); std::string output_filename(argv[4]); std::clog << "Using keyset from file " << keyset_filename << " to encrypt file " << plaintext_filename - << " with context info '" << context_info << "'.\n" - << "The resulting ciphertext will be written to file " + << " with context info from file " << context_info_filename + << ".\n" << "The resulting ciphertext will be written to file " << output_filename << std::endl; // Init Tink; @@ -65,6 +67,7 @@ int main(int argc, char** argv) { // Read the plaintext. std::string plaintext = CliUtil::Read(plaintext_filename); + std::string context_info = CliUtil::Read(context_info_filename); // Compute the ciphertext. std::clog << "Encrypting...\n"; diff --git a/tools/testing/cross_language/aead_test.sh b/tools/testing/cross_language/aead_test.sh index a3e0f5d3c0..919a55ac67 100755 --- a/tools/testing/cross_language/aead_test.sh +++ b/tools/testing/cross_language/aead_test.sh @@ -42,12 +42,13 @@ aead_basic_test() { local encrypted_file="$TEST_TMPDIR/${test_instance}_encrypted.bin" local decrypted_file="$TEST_TMPDIR/${test_instance}_decrypted.bin" - local associated_data="some associated data for $test_instance" + local associated_data_file="$TEST_TMPDIR/${test_instance}_aad.bin" + echo "some associated data for $test_instance" > $associated_data_file $encrypt_cli $symmetric_key_file "encrypt" $plaintext_file\ - "$associated_data" $encrypted_file || exit 1 + $associated_data_file $encrypted_file || exit 1 assert_files_different $plaintext_file $encrypted_file $decrypt_cli $symmetric_key_file "decrypt" $encrypted_file\ - "$associated_data" $decrypted_file || exit 1 + $associated_data_file $decrypted_file || exit 1 assert_files_equal $plaintext_file $decrypted_file done } diff --git a/tools/testing/cross_language/hybrid_encryption_test.sh b/tools/testing/cross_language/hybrid_encryption_test.sh index 8a026e8bdb..0ab9da1057 100755 --- a/tools/testing/cross_language/hybrid_encryption_test.sh +++ b/tools/testing/cross_language/hybrid_encryption_test.sh @@ -45,11 +45,12 @@ hybrid_basic_test() { local encrypted_file="$TEST_TMPDIR/${test_instance}_encrypted.bin" local decrypted_file="$TEST_TMPDIR/${test_instance}_decrypted.bin" - local context_info="some context info for $test_instance" - $encrypt_cli $pub_key_file $plaintext_file "$context_info" \ + local context_info_file="$TEST_TMPDIR/${test_instance}_context_info.bin" + echo "some context info for $test_instance" > $context_info_file + $encrypt_cli $pub_key_file $plaintext_file $context_info_file \ $encrypted_file || exit 1 assert_files_different $plaintext_file $encrypted_file - $decrypt_cli $priv_key_file $encrypted_file "$context_info" \ + $decrypt_cli $priv_key_file $encrypted_file $context_info_file \ $decrypted_file || exit 1 assert_files_equal $plaintext_file $decrypted_file done diff --git a/tools/testing/java/com/google/crypto/tink/testing/AeadCli.java b/tools/testing/java/com/google/crypto/tink/testing/AeadCli.java index c1414cbca2..ba5be13395 100644 --- a/tools/testing/java/com/google/crypto/tink/testing/AeadCli.java +++ b/tools/testing/java/com/google/crypto/tink/testing/AeadCli.java @@ -27,20 +27,20 @@ * operation: the actual AEAD-operation, i.e. "encrypt" or "decrypt" * input-file: name of the file with input (plaintext for encryption, or * or ciphertext for decryption) - * associated-data: a string to be used as assciated data + * associated-data-file: name of the file containing associated data * output-file: name of the file for the resulting output */ public class AeadCli { public static void main(String[] args) throws Exception { if (args.length != 5) { System.out.println( - "Usage: AeadCli keyset-file operation input-file associated-data output-file"); + "Usage: AeadCli keyset-file operation input-file associated-data-file output-file"); System.exit(1); } String keysetFilename = args[0]; String operation = args[1]; String inputFilename = args[2]; - String associatedData = args[3]; + String associatedDataFile = args[3]; String outputFilename = args[4]; if (!(operation.equals("encrypt") || operation.equals("decrypt"))) { System.out.println( @@ -48,7 +48,7 @@ public static void main(String[] args) throws Exception { System.exit(1); } System.out.println("Using keyset from file " + keysetFilename + " to AEAD-" + operation - + " file " + inputFilename + " with associated data '" + associatedData + "'."); + + " file " + inputFilename + " with associated data from file " + associatedDataFile + "."); System.out.println("The resulting output will be written to file " + outputFilename); // Init Tink. @@ -64,14 +64,15 @@ public static void main(String[] args) throws Exception { // Read the input. byte[] input = CliUtil.read(inputFilename); + byte[] aad = CliUtil.read(associatedDataFile); // Compute the output. System.out.println(operation + "ing..."); byte[] output; if (operation.equals("encrypt")) { - output = aead.encrypt(input, associatedData.getBytes(CliUtil.UTF_8)); + output = aead.encrypt(input, aad); } else { // operation.equals("decrypt") - output = aead.decrypt(input, associatedData.getBytes(CliUtil.UTF_8)); + output = aead.decrypt(input, aad); } // Write the output to the output file. diff --git a/tools/testing/java/com/google/crypto/tink/testing/HybridDecryptCli.java b/tools/testing/java/com/google/crypto/tink/testing/HybridDecryptCli.java index 2fc0a155d3..56b7f7b2ce 100644 --- a/tools/testing/java/com/google/crypto/tink/testing/HybridDecryptCli.java +++ b/tools/testing/java/com/google/crypto/tink/testing/HybridDecryptCli.java @@ -23,24 +23,25 @@ /** * A command-line utility for testing HybridDecrypt-primitives. * It requires 4 arguments: - * keyset-file: name of the file with the keyset to be used for decryption - * ciphertext-file: name of the file that contains ciphertext to be decrypted - * context-info: a string to be used as "context info" during the decryption - * output-file: name of the output file for the resulting ciphertext + * keyset-file: name of the file with the keyset to be used for decryption + * ciphertext-file: name of the file that contains ciphertext to be decrypted + * context-info-file: name of the file that contains "context info" which will + * be used during the decryption + * output-file: name of the output file for the resulting ciphertext */ public class HybridDecryptCli { public static void main(String[] args) throws Exception { if (args.length != 4) { System.out.println( - "Usage: HybridDecryptCli keyset-file ciphertext-file context-info output-file"); + "Usage: HybridDecryptCli keyset-file ciphertext-file context-info-file output-file"); System.exit(1); } String keysetFilename = args[0]; String ciphertextFilename = args[1]; - String contextInfo = args[2]; + String contextInfoFilename = args[2]; String outputFilename = args[3]; System.out.println("Using keyset from file " + keysetFilename + " to decrypt file " - + ciphertextFilename + " with context info '" + contextInfo + "'."); + + ciphertextFilename + " with context info from file " + contextInfoFilename + "."); System.out.println("The resulting plaintext will be written to file " + outputFilename); // Init Tink. @@ -56,10 +57,11 @@ public static void main(String[] args) throws Exception { // Read the ciphertext. byte[] ciphertext = CliUtil.read(ciphertextFilename); + byte[] contextInfo = CliUtil.read(contextInfoFilename); // Compute the plaintext. System.out.println("Decrypting..."); - byte[] plaintext = hybridDecrypt.decrypt(ciphertext, contextInfo.getBytes(CliUtil.UTF_8)); + byte[] plaintext = hybridDecrypt.decrypt(ciphertext, contextInfo); // Write the plaintext to the output file. CliUtil.write(plaintext, outputFilename); diff --git a/tools/testing/java/com/google/crypto/tink/testing/HybridEncryptCli.java b/tools/testing/java/com/google/crypto/tink/testing/HybridEncryptCli.java index 893c90f878..22b0ef2349 100644 --- a/tools/testing/java/com/google/crypto/tink/testing/HybridEncryptCli.java +++ b/tools/testing/java/com/google/crypto/tink/testing/HybridEncryptCli.java @@ -23,24 +23,25 @@ /** * A command-line utility for testing HybridEncrypt-primitives. * It requires 4 arguments: - * keyset-file: name of the file with the keyset to be used for encryption - * plaintext-file: name of the file that contains plaintext to be encrypted - * context-info: a string to be used as "context info" during the encryption - * output-file: name of the output file for the resulting ciphertext + * keyset-file: name of the file with the keyset to be used for encryption + * plaintext-file: name of the file that contains plaintext to be encrypted + * context-info-file: name of the file that contains "context info" which will + * be used during the decryption + * output-file: name of the output file for the resulting ciphertext */ public class HybridEncryptCli { public static void main(String[] args) throws Exception { if (args.length != 4) { System.out.println( - "Usage: HybridEncryptCli keyset-file plaintext-file context-info output-file"); + "Usage: HybridEncryptCli keyset-file plaintext-file context-info-file output-file"); System.exit(1); } String keysetFilename = args[0]; String plaintextFilename = args[1]; - String contextInfo = args[2]; + String contextInfoFilename = args[2]; String outputFilename = args[3]; System.out.println("Using keyset from file " + keysetFilename + " to encrypt file " - + plaintextFilename + " with context info '" + contextInfo + "'."); + + plaintextFilename + " with context info from file " + contextInfoFilename + "."); System.out.println("The resulting ciphertext will be written to file " + outputFilename); // Init Tink. @@ -56,10 +57,11 @@ public static void main(String[] args) throws Exception { // Read the plaintext. byte[] plaintext = CliUtil.read(plaintextFilename); + byte[] contextInfo = CliUtil.read(contextInfoFilename); // Compute the ciphertext. System.out.println("Encrypting..."); - byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo.getBytes(CliUtil.UTF_8)); + byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo); // Write the ciphertext to the output file. CliUtil.write(ciphertext, outputFilename);