Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Changing CLIs for Aead, Hybrid encryption and Hybrid decryption in cr…
Browse files Browse the repository at this point in the history
…oss-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
  • Loading branch information
slivova authored and chuckx committed Sep 11, 2018
1 parent f048e88 commit 67270c6
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 43 deletions.
12 changes: 7 additions & 5 deletions tools/testing/cc/aead_cli.cc
Expand Up @@ -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"
Expand All @@ -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;
Expand All @@ -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";
Expand Down
12 changes: 7 additions & 5 deletions tools/testing/cc/hybrid_decrypt_cli.cc
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
13 changes: 8 additions & 5 deletions tools/testing/cc/hybrid_encrypt_cli.cc
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
7 changes: 4 additions & 3 deletions tools/testing/cross_language/aead_test.sh
Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions tools/testing/cross_language/hybrid_encryption_test.sh
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions tools/testing/java/com/google/crypto/tink/testing/AeadCli.java
Expand Up @@ -27,28 +27,28 @@
* 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(
"Unknown operation '" + operation + "'.\nExpected 'encrypt' or 'decrypt'.");
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.
Expand All @@ -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.
Expand Down
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
Expand Up @@ -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.
Expand All @@ -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);
Expand Down

0 comments on commit 67270c6

Please sign in to comment.