diff options
Diffstat (limited to 'tools/testing/java/com/google/crypto/tink')
-rw-r--r-- | tools/testing/java/com/google/crypto/tink/testing/AeadCli.java | 84 | ||||
-rw-r--r-- | tools/testing/java/com/google/crypto/tink/testing/CliUtil.java | 113 |
2 files changed, 0 insertions, 197 deletions
diff --git a/tools/testing/java/com/google/crypto/tink/testing/AeadCli.java b/tools/testing/java/com/google/crypto/tink/testing/AeadCli.java deleted file mode 100644 index 7f81b80ff..000000000 --- a/tools/testing/java/com/google/crypto/tink/testing/AeadCli.java +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2017 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//////////////////////////////////////////////////////////////////////////////// - -package com.google.crypto.tink.testing; - -import com.google.crypto.tink.Aead; -import com.google.crypto.tink.KeysetHandle; -import com.google.crypto.tink.aead.AeadConfig; - -/** - * A command-line utility for testing Aead-primitives. It requires 5 arguments: keyset-file: name of - * the file with the keyset to be used for encryption 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-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-file output-file"); - System.exit(1); - } - String keysetFilename = args[0]; - String operation = args[1]; - String inputFilename = args[2]; - String associatedDataFile = args[3]; - String outputFilename = args[4]; - - AeadConfig.register(); - - 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 from file " + associatedDataFile + "."); - System.out.println("The resulting output will be written to file " + outputFilename); - - // Init Tink. - CliUtil.initTink(); - - // Read the keyset. - System.out.println("Reading the keyset..."); - KeysetHandle keysetHandle = CliUtil.readKeyset(keysetFilename); - // Get the primitive. - System.out.println("Getting the primitive..."); - Aead aead = keysetHandle.getPrimitive(Aead.class); - - // 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, aad); - } else { // operation.equals("decrypt") - output = aead.decrypt(input, aad); - } - // Write the output to the output file. - CliUtil.write(output, outputFilename); - - System.out.println("All done."); - } - - private AeadCli() {} -} diff --git a/tools/testing/java/com/google/crypto/tink/testing/CliUtil.java b/tools/testing/java/com/google/crypto/tink/testing/CliUtil.java deleted file mode 100644 index cd9e051c1..000000000 --- a/tools/testing/java/com/google/crypto/tink/testing/CliUtil.java +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2017 Google Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//////////////////////////////////////////////////////////////////////////////// - -package com.google.crypto.tink.testing; - -import com.google.crypto.tink.InsecureSecretKeyAccess; -import com.google.crypto.tink.KeysetHandle; -import com.google.crypto.tink.TinkProtoKeysetFormat; -import com.google.crypto.tink.daead.DeterministicAeadConfig; -import com.google.crypto.tink.hybrid.HybridConfig; -import com.google.crypto.tink.keyderivation.KeyDerivationConfig; -import com.google.crypto.tink.prf.PrfConfig; -import com.google.crypto.tink.signature.SignatureConfig; -import com.google.crypto.tink.streamingaead.StreamingAeadConfig; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.charset.Charset; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.security.GeneralSecurityException; - -/** Helper function for CLI applications. */ -public final class CliUtil { - public static final Charset UTF_8 = Charset.forName("UTF-8"); - - /** - * Reads a keyset from the specified file. - * In case of errors throws an exception. - */ - public static KeysetHandle readKeyset(String filename) - throws GeneralSecurityException, IOException { - System.out.println("Reading the keyset..."); - return TinkProtoKeysetFormat.parseKeyset( - Files.readAllBytes(Paths.get(filename)), InsecureSecretKeyAccess.get()); - } - - /** Writes a keyset to the specified file. In case of errors throws an exception. */ - public static void writeKeyset(KeysetHandle handle, String filename) - throws IOException, GeneralSecurityException { - System.out.println("Writing the keyset..."); - byte[] serializedKeyset = - TinkProtoKeysetFormat.serializeKeyset(handle, InsecureSecretKeyAccess.get()); - Files.write(Paths.get(filename), serializedKeyset); - } - - /** - * Initializes Tink registry. - * In case of errors throws an exception. - */ - public static void initTink() throws GeneralSecurityException { - DeterministicAeadConfig.register(); - HybridConfig.register(); // includes Aead and Mac - PrfConfig.register(); - SignatureConfig.register(); - StreamingAeadConfig.register(); - KeyDerivationConfig.register(); - } - - /** - * Reads the specified file and returns the contents as a byte array. - * In case of errors throws an exception. - */ - public static byte[] read(String filename) throws GeneralSecurityException, IOException { - System.out.println("Reading file " + filename); - InputStream inputStream = new FileInputStream(Paths.get(filename).toFile()); - return read(inputStream); - } - - /** - * Reads the specified InputStream and returns the contents as a byte array. - * In case of errors throws an exception. - */ - public static byte[] read(InputStream inputStream) throws GeneralSecurityException, IOException { - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[512]; - int length; - while ((length = inputStream.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - inputStream.close(); - return result.toByteArray(); - } - - /** - * Writes the given 'output' to the specified file. - * In case of errors throws an exception. - */ - public static void write(byte[] output, String filename) throws IOException { - System.out.println("Writing to file " + filename); - OutputStream outputStream = new FileOutputStream(Paths.get(filename).toFile()); - outputStream.write(output); - outputStream.close(); - } - - private CliUtil() {} -} |