Search in sources :

Example 16 with Aead

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

the class AesEaxKeyManagerTest method testCiphertextSize.

@Test
public void testCiphertextSize() throws Exception {
    AesEaxKey key = factory.createKey(createKeyFormat(32, 16));
    Aead aead = manager.getPrimitive(key, Aead.class);
    byte[] plaintext = "plaintext".getBytes(UTF_8);
    byte[] associatedData = "associatedData".getBytes(UTF_8);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertEquals(16 + /* IV_SIZE */
    plaintext.length + 16, /* TAG_SIZE */
    ciphertext.length);
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Example 17 with Aead

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

the class AeadThreadSafetyTest method testXChaChaPoly1305.

@Test
public void testXChaChaPoly1305() throws Exception {
    byte[] key = Random.randBytes(32);
    Aead cipher = new XChaCha20Poly1305(key);
    testEncryptionDecryption(cipher, 5, 128, 20);
}
Also used : Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Example 18 with Aead

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

the class KeysetServiceImpl method readEncrypted.

@Override
public void readEncrypted(KeysetReadEncryptedRequest request, StreamObserver<KeysetReadEncryptedResponse> responseObserver) {
    KeysetReadEncryptedResponse response;
    try {
        // get masterAead
        KeysetHandle masterKeysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getMasterKeyset().toByteArray()));
        Aead masterAead = masterKeysetHandle.getPrimitive(Aead.class);
        // read encrypted keyset to keysetHandle
        KeysetReader reader = BinaryKeysetReader.withBytes(request.getEncryptedKeyset().toByteArray());
        KeysetHandle keysetHandle;
        if (request.hasAssociatedData()) {
            keysetHandle = KeysetHandle.readWithAssociatedData(reader, masterAead, request.getAssociatedData().getValue().toByteArray());
        } else {
            keysetHandle = KeysetHandle.read(reader, masterAead);
        }
        // get keyset from keysetHandle
        Keyset keyset = CleartextKeysetHandle.getKeyset(keysetHandle);
        ByteArrayOutputStream keysetStream = new ByteArrayOutputStream();
        BinaryKeysetWriter.withOutputStream(keysetStream).write(keyset);
        keysetStream.close();
        response = KeysetReadEncryptedResponse.newBuilder().setKeyset(ByteString.copyFrom(keysetStream.toByteArray())).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = KeysetReadEncryptedResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) Aead(com.google.crypto.tink.Aead) BinaryKeysetReader(com.google.crypto.tink.BinaryKeysetReader) JsonKeysetReader(com.google.crypto.tink.JsonKeysetReader) KeysetReader(com.google.crypto.tink.KeysetReader) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 19 with Aead

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

the class FakeKmsClientTest method createNewAead_success.

@Test
public void createNewAead_success() throws GeneralSecurityException {
    String uri = FakeKmsClient.createFakeKeyUri();
    FakeKmsClient client = new FakeKmsClient(uri);
    assertThat(client.doesSupport(uri)).isTrue();
    Aead aead = client.getAead(uri);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData));
}
Also used : Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Example 20 with Aead

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

the class AeadServiceImpl method encrypt.

/**
 * Encrypts a message.
 */
@Override
public void encrypt(AeadEncryptRequest request, StreamObserver<AeadEncryptResponse> responseObserver) {
    AeadEncryptResponse response;
    try {
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        Aead aead = keysetHandle.getPrimitive(Aead.class);
        byte[] ciphertext = aead.encrypt(request.getPlaintext().toByteArray(), request.getAssociatedData().toByteArray());
        response = AeadEncryptResponse.newBuilder().setCiphertext(ByteString.copyFrom(ciphertext)).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = AeadEncryptResponse.newBuilder().setErr(e.toString()).build();
    } catch (IOException e) {
        responseObserver.onError(Status.UNKNOWN.withDescription(e.getMessage()).asException());
        return;
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) AeadEncryptResponse(com.google.crypto.tink.proto.testing.AeadEncryptResponse) GeneralSecurityException(java.security.GeneralSecurityException) Aead(com.google.crypto.tink.Aead) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException)

Aggregations

Aead (com.google.crypto.tink.Aead)84 Test (org.junit.Test)67 GeneralSecurityException (java.security.GeneralSecurityException)25 KeysetHandle (com.google.crypto.tink.KeysetHandle)21 Key (com.google.crypto.tink.proto.Keyset.Key)9 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)7 IOException (java.io.IOException)7 EncryptRequest (com.amazonaws.services.kms.model.EncryptRequest)6 KeyTemplate (com.google.crypto.tink.KeyTemplate)6 ByteString (com.google.protobuf.ByteString)6 DecryptRequest (com.amazonaws.services.kms.model.DecryptRequest)5 EncryptResult (com.amazonaws.services.kms.model.EncryptResult)5 KmsEnvelopeAeadKey (com.google.crypto.tink.proto.KmsEnvelopeAeadKey)5 File (java.io.File)5 FileOutputStream (java.io.FileOutputStream)5 DecryptResult (com.amazonaws.services.kms.model.DecryptResult)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBuffer (java.nio.ByteBuffer)4 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)3 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)3