Search in sources :

Example 1 with KeysetWriteEncryptedResponse

use of com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse in project tink by google.

the class TestingServicesTest method encryptDecryptInvalidKeyset_fails.

@Test
public void encryptDecryptInvalidKeyset_fails() throws Exception {
    byte[] invalidData = "invalid".getBytes(UTF_8);
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    KeysetGenerateResponse masterKeysetResponse = generateKeyset(keysetStub, template);
    assertThat(masterKeysetResponse.getErr()).isEmpty();
    byte[] masterKeyset = masterKeysetResponse.getKeyset().toByteArray();
    KeysetWriteEncryptedResponse writeResponse1 = keysetWriteEncrypted(keysetStub, keyset, invalidData, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse1.getErr()).isNotEmpty();
    KeysetWriteEncryptedResponse writeResponse2 = keysetWriteEncrypted(keysetStub, invalidData, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse2.getErr()).isNotEmpty();
    KeysetReadEncryptedResponse readResponse1 = keysetReadEncrypted(keysetStub, keyset, invalidData, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse1.getErr()).isNotEmpty();
    KeysetReadEncryptedResponse readResponse2 = keysetReadEncrypted(keysetStub, invalidData, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse2.getErr()).isNotEmpty();
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 2 with KeysetWriteEncryptedResponse

use of com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse in project tink by google.

the class TestingServicesTest method generateEncryptDecryptKeyset.

@Test
public void generateEncryptDecryptKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    KeysetGenerateResponse masterKeysetResponse = generateKeyset(keysetStub, template);
    assertThat(masterKeysetResponse.getErr()).isEmpty();
    byte[] masterKeyset = masterKeysetResponse.getKeyset().toByteArray();
    KeysetWriteEncryptedResponse writeResponse = keysetWriteEncrypted(keysetStub, keyset, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(writeResponse.getErr()).isEmpty();
    byte[] encryptedKeyset = writeResponse.getEncryptedKeyset().toByteArray();
    assertThat(encryptedKeyset).isNotEqualTo(keyset);
    KeysetReadEncryptedResponse readResponse = keysetReadEncrypted(keysetStub, encryptedKeyset, masterKeyset, /*associatedData=*/
    Optional.empty());
    assertThat(readResponse.getErr()).isEmpty();
    byte[] output = readResponse.getKeyset().toByteArray();
    assertThat(output).isEqualTo(keyset);
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 3 with KeysetWriteEncryptedResponse

use of com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse in project tink by google.

the class KeysetServiceImpl method writeEncrypted.

@Override
public void writeEncrypted(KeysetWriteEncryptedRequest request, StreamObserver<KeysetWriteEncryptedResponse> responseObserver) {
    KeysetWriteEncryptedResponse response;
    try {
        // get masterAead
        KeysetHandle masterKeysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getMasterKeyset().toByteArray()));
        Aead masterAead = masterKeysetHandle.getPrimitive(Aead.class);
        // get keysetHandle
        KeysetHandle keysetHandle = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(request.getKeyset().toByteArray()));
        // write keysetHandle as encrypted keyset
        ByteArrayOutputStream keysetStream = new ByteArrayOutputStream();
        KeysetWriter writer = BinaryKeysetWriter.withOutputStream(keysetStream);
        if (request.hasAssociatedData()) {
            keysetHandle.writeWithAssociatedData(writer, masterAead, request.getAssociatedData().getValue().toByteArray());
        } else {
            keysetHandle.write(writer, masterAead);
        }
        keysetStream.close();
        response = KeysetWriteEncryptedResponse.newBuilder().setEncryptedKeyset(ByteString.copyFrom(keysetStream.toByteArray())).build();
    } catch (GeneralSecurityException | InvalidProtocolBufferException e) {
        response = KeysetWriteEncryptedResponse.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) KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) JsonKeysetWriter(com.google.crypto.tink.JsonKeysetWriter) BinaryKeysetWriter(com.google.crypto.tink.BinaryKeysetWriter) KeysetWriter(com.google.crypto.tink.KeysetWriter) GeneralSecurityException(java.security.GeneralSecurityException) Aead(com.google.crypto.tink.Aead) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 4 with KeysetWriteEncryptedResponse

use of com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse in project tink by google.

the class TestingServicesTest method generateEncryptDecryptKeysetWithAssociatedData.

@Test
public void generateEncryptDecryptKeysetWithAssociatedData() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    byte[] associatedData = "a".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    KeysetGenerateResponse masterKeysetResponse = generateKeyset(keysetStub, template);
    assertThat(masterKeysetResponse.getErr()).isEmpty();
    byte[] masterKeyset = masterKeysetResponse.getKeyset().toByteArray();
    KeysetWriteEncryptedResponse writeResponse = keysetWriteEncrypted(keysetStub, keyset, masterKeyset, Optional.of(associatedData));
    assertThat(writeResponse.getErr()).isEmpty();
    byte[] encryptedKeyset = writeResponse.getEncryptedKeyset().toByteArray();
    assertThat(encryptedKeyset).isNotEqualTo(keyset);
    KeysetReadEncryptedResponse readResponse = keysetReadEncrypted(keysetStub, encryptedKeyset, masterKeyset, Optional.of(associatedData));
    assertThat(readResponse.getErr()).isEmpty();
    byte[] output = readResponse.getKeyset().toByteArray();
    assertThat(output).isEqualTo(keyset);
}
Also used : KeysetWriteEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse) KeysetReadEncryptedResponse(com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

KeysetWriteEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse)4 KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)3 KeysetReadEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse)3 Test (org.junit.Test)3 Aead (com.google.crypto.tink.Aead)1 BinaryKeysetWriter (com.google.crypto.tink.BinaryKeysetWriter)1 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)1 JsonKeysetWriter (com.google.crypto.tink.JsonKeysetWriter)1 KeysetHandle (com.google.crypto.tink.KeysetHandle)1 KeysetWriter (com.google.crypto.tink.KeysetWriter)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1