Search in sources :

Example 6 with KeysetHandle

use of com.google.crypto.tink.KeysetHandle in project tink by google.

the class StreamingAeadFactoryTest method testBasicAesCtrHmacStreamingAead.

@Test
public void testBasicAesCtrHmacStreamingAead() throws Exception {
    byte[] keyValue = Random.randBytes(AES_KEY_SIZE);
    int derivedKeySize = AES_KEY_SIZE;
    int ciphertextSegmentSize = 128;
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(TestUtil.createKey(TestUtil.createAesCtrHmacStreamingKeyData(keyValue, derivedKeySize, ciphertextSegmentSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW)));
    StreamingAead streamingAead = StreamingAeadFactory.getPrimitive(keysetHandle);
    StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) StreamingAead(com.google.crypto.tink.StreamingAead) Test(org.junit.Test)

Example 7 with KeysetHandle

use of com.google.crypto.tink.KeysetHandle in project tink by google.

the class PublicKeyVerifyCli method main.

public static void main(String[] args) throws Exception {
    if (args.length != 4) {
        System.out.println("Usage: PublicKeyVerifyCli keyset-file signature-file message-file output-file");
        System.exit(1);
    }
    String keysetFilename = args[0];
    String signatureFilename = args[1];
    String messageFilename = args[2];
    String outputFilename = args[3];
    System.out.println("Using keyset from file " + keysetFilename + " to verify signature from file " + signatureFilename + " of the message from file " + messageFilename + ".");
    System.out.println("The verification result 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...");
    PublicKeyVerify pkVerify = PublicKeyVerifyFactory.getPrimitive(keysetHandle);
    // Read the signature.
    byte[] signature = CliUtil.read(signatureFilename);
    // Read the message.
    byte[] message = CliUtil.read(messageFilename);
    // Verify the signature.
    System.out.println("Verifying...");
    String verificationResult;
    try {
        pkVerify.verify(signature, message);
        verificationResult = "valid";
    } catch (GeneralSecurityException e) {
        System.out.println("Verification failed: " + e);
        verificationResult = "invalid";
    }
    // Write the verification result to the output file.
    CliUtil.write(verificationResult.getBytes(CliUtil.UTF_8), outputFilename);
    System.out.println("All done.");
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify)

Example 8 with KeysetHandle

use of com.google.crypto.tink.KeysetHandle in project tink by google.

the class ConvertKeysetCommand method convert.

/**
 * Changes format, encrypts or decrypts a keyset.
 *
 * <p>The keyset is read from {@code inputStream}. Its format can be either <code>json</code>
 * or <code>binary</code>, and is specified by {@code inFormat}. The new key is generated
 * from template {@code keyTemplate}. If the input keyset is encrypted, use
 * {@code masterKeyUri} and {@code credentialPath} to decrypt. The output keyset
 * is written to {@code outputStream} in {@code outFormat}, and encrypted if the
 * input keyset is encrypted.
 *
 * @throws GeneralSecurityException if cannot encrypt/decrypt the keyset
 * @throws IOException if cannot read/write the keyset
 */
public static void convert(OutputStream outputStream, String outFormat, InputStream inputStream, String inFormat, String masterKeyUri, String credentialPath, String newMasterKeyUri, String newCredentialPath) throws GeneralSecurityException, IOException {
    KeysetHandle handle = TinkeyUtil.getKeysetHandle(inputStream, inFormat, masterKeyUri, credentialPath);
    TinkeyUtil.writeKeyset(handle, outputStream, outFormat, newMasterKeyUri, newCredentialPath);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle)

Example 9 with KeysetHandle

use of com.google.crypto.tink.KeysetHandle in project tink by google.

the class CreatePublicKeysetCommand method create.

/**
 * Extracts public keys from {@code inputStream} (using {@code credentialPath} and
 * {@code masterKeyUri} to decrypt if it is encrypted) and writes public keys to
 * {@code outputStream}.
 */
public static void create(OutputStream outputStream, String outFormat, InputStream inputStream, String inFormat, String masterKeyUri, String credentialPath) throws Exception {
    KeysetHandle handle = TinkeyUtil.getKeysetHandle(inputStream, inFormat, masterKeyUri, credentialPath);
    KeysetWriter writer = TinkeyUtil.createKeysetWriter(outputStream, outFormat);
    CleartextKeysetHandle.write(handle.getPublicKeysetHandle(), writer);
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) KeysetWriter(com.google.crypto.tink.KeysetWriter)

Example 10 with KeysetHandle

use of com.google.crypto.tink.KeysetHandle in project tink by google.

the class AesEaxKeyManagerTest method testCiphertextSize.

@Test
public void testCiphertextSize() throws Exception {
    byte[] keyValue = Random.randBytes(AES_KEY_SIZE);
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(TestUtil.createKey(TestUtil.createAesEaxKeyData(keyValue, 16), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK)));
    Aead aead = AeadFactory.getPrimitive(keysetHandle);
    byte[] plaintext = "plaintext".getBytes("UTF-8");
    byte[] associatedData = "associatedData".getBytes("UTF-8");
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertEquals(CryptoFormat.NON_RAW_PREFIX_SIZE + 16 + /* IV_SIZE */
    plaintext.length + 16, /* TAG_SIZE */
    ciphertext.length);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Aggregations

KeysetHandle (com.google.crypto.tink.KeysetHandle)42 Test (org.junit.Test)27 Key (com.google.crypto.tink.proto.Keyset.Key)13 GeneralSecurityException (java.security.GeneralSecurityException)10 Aead (com.google.crypto.tink.Aead)9 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)5 DeterministicAead (com.google.crypto.tink.DeterministicAead)5 PublicKeySign (com.google.crypto.tink.PublicKeySign)5 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)5 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)5 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)4 HybridEncrypt (com.google.crypto.tink.HybridEncrypt)4 StreamingAead (com.google.crypto.tink.StreamingAead)3 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)3 EciesAeadHkdfPrivateKey (com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey)3 KeyData (com.google.crypto.tink.proto.KeyData)3 KeysetReader (com.google.crypto.tink.KeysetReader)2 Mac (com.google.crypto.tink.Mac)2 EcPointFormat (com.google.crypto.tink.proto.EcPointFormat)2 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)2