Search in sources :

Example 1 with KeysetGenerateResponse

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

the class KeysetServiceImpl method generate.

@Override
public void generate(KeysetGenerateRequest request, StreamObserver<KeysetGenerateResponse> responseObserver) {
    KeysetGenerateResponse response;
    try {
        KeyTemplate template = KeyTemplateProtoConverter.fromByteArray(request.getTemplate().toByteArray());
        KeysetHandle keysetHandle = KeysetHandle.generateNew(template);
        Keyset keyset = CleartextKeysetHandle.getKeyset(keysetHandle);
        ByteArrayOutputStream keysetStream = new ByteArrayOutputStream();
        BinaryKeysetWriter.withOutputStream(keysetStream).write(keyset);
        keysetStream.close();
        response = KeysetGenerateResponse.newBuilder().setKeyset(ByteString.copyFrom(keysetStream.toByteArray())).build();
    } catch (GeneralSecurityException e) {
        response = KeysetGenerateResponse.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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) KeyTemplate(com.google.crypto.tink.KeyTemplate)

Example 2 with KeysetGenerateResponse

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

the class TestingServicesTest method aeadGenerateEncryptDecrypt_success.

@Test
public void aeadGenerateEncryptDecrypt_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(KeyTemplates.get("AES128_GCM"));
    byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    byte[] associatedData = "generate_encrypt_decrypt".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    AeadEncryptResponse encResponse = aeadEncrypt(aeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    AeadDecryptResponse decResponse = aeadDecrypt(aeadStub, keyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isEmpty();
    byte[] output = decResponse.getPlaintext().toByteArray();
    assertThat(output).isEqualTo(plaintext);
}
Also used : DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) AeadDecryptResponse(com.google.crypto.tink.proto.testing.AeadDecryptResponse) AeadEncryptResponse(com.google.crypto.tink.proto.testing.AeadEncryptResponse) DeterministicAeadEncryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse) StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 3 with KeysetGenerateResponse

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

the class TestingServicesTest method generateKeyset_failsOnBadTemplate.

@Test
public void generateKeyset_failsOnBadTemplate() throws Exception {
    byte[] badTemplate = "bad template".getBytes(UTF_8);
    KeysetGenerateResponse genResponse = generateKeyset(keysetStub, badTemplate);
    assertThat(genResponse.getErr()).isNotEmpty();
}
Also used : KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 4 with KeysetGenerateResponse

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

the class TestingServicesTest method streamingAeadGenerateEncryptDecrypt_success.

@Test
public void streamingAeadGenerateEncryptDecrypt_success() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesGcmHkdfStreamingKeyManager.aes128GcmHkdf4KBTemplate());
    byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    byte[] associatedData = "generate_encrypt_decrypt".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    StreamingAeadEncryptResponse encResponse = streamingAeadEncrypt(streamingAeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    StreamingAeadDecryptResponse decResponse = streamingAeadDecrypt(streamingAeadStub, keyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isEmpty();
    byte[] output = decResponse.getPlaintext().toByteArray();
    assertThat(output).isEqualTo(plaintext);
}
Also used : StreamingAeadEncryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse) StreamingAeadDecryptResponse(com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Example 5 with KeysetGenerateResponse

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

the class TestingServicesTest method daeadDecryptDeterministically_failsOnBadKeyset.

@Test
public void daeadDecryptDeterministically_failsOnBadKeyset() throws Exception {
    byte[] template = KeyTemplateProtoConverter.toByteArray(AesSivKeyManager.aes256SivTemplate());
    byte[] plaintext = "The quick brown fox jumps over the lazy dog".getBytes(UTF_8);
    byte[] associatedData = "generate_encrypt_decrypt".getBytes(UTF_8);
    KeysetGenerateResponse keysetResponse = generateKeyset(keysetStub, template);
    assertThat(keysetResponse.getErr()).isEmpty();
    byte[] keyset = keysetResponse.getKeyset().toByteArray();
    DeterministicAeadEncryptResponse encResponse = daeadEncrypt(daeadStub, keyset, plaintext, associatedData);
    assertThat(encResponse.getErr()).isEmpty();
    byte[] ciphertext = encResponse.getCiphertext().toByteArray();
    byte[] badKeyset = "bad keyset".getBytes(UTF_8);
    DeterministicAeadDecryptResponse decResponse = daeadDecrypt(daeadStub, badKeyset, ciphertext, associatedData);
    assertThat(decResponse.getErr()).isNotEmpty();
}
Also used : DeterministicAeadEncryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse) DeterministicAeadDecryptResponse(com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse) KeysetGenerateResponse(com.google.crypto.tink.proto.testing.KeysetGenerateResponse) Test(org.junit.Test)

Aggregations

KeysetGenerateResponse (com.google.crypto.tink.proto.testing.KeysetGenerateResponse)34 Test (org.junit.Test)33 JwtSignRequest (com.google.crypto.tink.proto.testing.JwtSignRequest)7 JwtSignResponse (com.google.crypto.tink.proto.testing.JwtSignResponse)7 JwtToken (com.google.crypto.tink.proto.testing.JwtToken)7 JwtValidator (com.google.crypto.tink.proto.testing.JwtValidator)7 JwtVerifyRequest (com.google.crypto.tink.proto.testing.JwtVerifyRequest)7 JwtVerifyResponse (com.google.crypto.tink.proto.testing.JwtVerifyResponse)7 KeysetPublicResponse (com.google.crypto.tink.proto.testing.KeysetPublicResponse)7 DeterministicAeadDecryptResponse (com.google.crypto.tink.proto.testing.DeterministicAeadDecryptResponse)6 StreamingAeadDecryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadDecryptResponse)6 DeterministicAeadEncryptResponse (com.google.crypto.tink.proto.testing.DeterministicAeadEncryptResponse)4 StreamingAeadEncryptResponse (com.google.crypto.tink.proto.testing.StreamingAeadEncryptResponse)4 AeadDecryptResponse (com.google.crypto.tink.proto.testing.AeadDecryptResponse)3 HybridDecryptResponse (com.google.crypto.tink.proto.testing.HybridDecryptResponse)3 KeysetReadEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetReadEncryptedResponse)3 KeysetWriteEncryptedResponse (com.google.crypto.tink.proto.testing.KeysetWriteEncryptedResponse)3 PrfSetComputeResponse (com.google.crypto.tink.proto.testing.PrfSetComputeResponse)3 SignatureVerifyResponse (com.google.crypto.tink.proto.testing.SignatureVerifyResponse)3 VerifyMacResponse (com.google.crypto.tink.proto.testing.VerifyMacResponse)3