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();
}
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);
}
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();
}
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);
}
Aggregations