Search in sources :

Example 21 with KeyTemplate

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

the class AesGcmHkdfStreamingKeyManagerTest method testAes256GcmHkdf1MBTemplate.

@Test
public void testAes256GcmHkdf1MBTemplate() throws Exception {
    KeyTemplate template = AesGcmHkdfStreamingKeyManager.aes256GcmHkdf1MBTemplate();
    assertThat(template.getTypeUrl()).isEqualTo(new AesGcmHkdfStreamingKeyManager().getKeyType());
    assertThat(template.getOutputPrefixType()).isEqualTo(KeyTemplate.OutputPrefixType.RAW);
    AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
    assertThat(format.getKeySize()).isEqualTo(32);
    assertThat(format.getParams().getDerivedKeySize()).isEqualTo(32);
    assertThat(format.getParams().getHkdfHashType()).isEqualTo(HashType.SHA256);
    assertThat(format.getParams().getCiphertextSegmentSize()).isEqualTo(1 << 20);
}
Also used : AesGcmHkdfStreamingKeyFormat(com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 22 with KeyTemplate

use of com.google.crypto.tink.KeyTemplate 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 23 with KeyTemplate

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

the class KeysetServiceImpl method getTemplate.

@Override
public void getTemplate(KeysetTemplateRequest request, StreamObserver<KeysetTemplateResponse> responseObserver) {
    KeysetTemplateResponse response;
    try {
        KeyTemplate template = KeyTemplates.get(request.getTemplateName());
        response = KeysetTemplateResponse.newBuilder().setKeyTemplate(ByteString.copyFrom(KeyTemplateProtoConverter.toByteArray(template))).build();
    } catch (GeneralSecurityException e) {
        response = KeysetTemplateResponse.newBuilder().setErr(e.toString()).build();
    }
    responseObserver.onNext(response);
    responseObserver.onCompleted();
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) KeysetTemplateResponse(com.google.crypto.tink.proto.testing.KeysetTemplateResponse) KeyTemplate(com.google.crypto.tink.KeyTemplate)

Example 24 with KeyTemplate

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

the class TestingServicesTest method template_success.

@Test
public void template_success() throws Exception {
    KeysetTemplateRequest request = KeysetTemplateRequest.newBuilder().setTemplateName("AES256_GCM").build();
    KeysetTemplateResponse response = keysetStub.getTemplate(request);
    assertThat(response.getErr()).isEmpty();
    KeyTemplate template = KeyTemplateProtoConverter.fromByteArray(response.getKeyTemplate().toByteArray());
    assertThat(template.getTypeUrl()).isEqualTo("type.googleapis.com/google.crypto.tink.AesGcmKey");
}
Also used : KeysetTemplateResponse(com.google.crypto.tink.proto.testing.KeysetTemplateResponse) KeysetTemplateRequest(com.google.crypto.tink.proto.testing.KeysetTemplateRequest) KeyTemplate(com.google.crypto.tink.KeyTemplate) Test(org.junit.Test)

Example 25 with KeyTemplate

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

the class KeyHandleTest method createFromKey_keyDataAsymmetricPublic_shouldNotHaveSecret.

@Test
public void createFromKey_keyDataAsymmetricPublic_shouldNotHaveSecret() throws Exception {
    KeyTemplate kt = KeyTemplates.get("ED25519");
    KeyData kd = Registry.getPublicKeyData(kt.getTypeUrl(), Registry.newKeyData(kt).getValue());
    KeyHandle kh = KeyHandle.createFromKey(kd, kt.getOutputPrefixType());
    assertThat(kh.hasSecret()).isFalse();
}
Also used : KeyTemplate(com.google.crypto.tink.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Aggregations

KeyTemplate (com.google.crypto.tink.KeyTemplate)143 Test (org.junit.Test)135 KeysetHandle (com.google.crypto.tink.KeysetHandle)56 ByteString (com.google.protobuf.ByteString)39 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)37 KeyData (com.google.crypto.tink.proto.KeyData)16 Keyset (com.google.crypto.tink.proto.Keyset)12 JsonObject (com.google.gson.JsonObject)8 KeysetManager (com.google.crypto.tink.KeysetManager)7 BigInteger (java.math.BigInteger)7 Instant (java.time.Instant)7 Aead (com.google.crypto.tink.Aead)6 Enums (com.google.crypto.tink.subtle.Enums)6 Clock (java.time.Clock)6 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)5 AesCtrHmacStreamingKeyFormat (com.google.crypto.tink.proto.AesCtrHmacStreamingKeyFormat)4 AesGcmHkdfStreamingKeyFormat (com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat)4 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)4 AesGcmSivKeyFormat (com.google.crypto.tink.proto.AesGcmSivKeyFormat)4 EciesAeadHkdfKeyFormat (com.google.crypto.tink.proto.EciesAeadHkdfKeyFormat)4