Search in sources :

Example 36 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class CleartextKeysetHandleTest method testRead.

@Test
public void testRead() throws Exception {
    // Create a keyset that contains a single HmacKey.
    KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
    KeysetManager manager = KeysetManager.withEmptyKeyset().rotate(template);
    Keyset keyset1 = manager.getKeysetHandle().getKeyset();
    KeysetHandle handle1 = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(keyset1.toByteArray()));
    assertEquals(keyset1, handle1.getKeyset());
    KeysetHandle handle2 = KeysetHandle.generateNew(template);
    Keyset keyset2 = handle2.getKeyset();
    assertEquals(1, keyset2.getKeyCount());
    Keyset.Key key2 = keyset2.getKey(0);
    assertEquals(keyset2.getPrimaryKeyId(), key2.getKeyId());
    assertEquals(template.getTypeUrl(), key2.getKeyData().getTypeUrl());
    Mac unused = handle2.getPrimitive(Mac.class);
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 37 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class CleartextKeysetHandleTest method testParse.

@Test
public void testParse() throws Exception {
    // Create a keyset that contains a single HmacKey.
    KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
    KeysetHandle handle = KeysetHandle.generateNew(template);
    Keyset keyset = CleartextKeysetHandle.getKeyset(handle);
    handle = CleartextKeysetHandle.parseFrom(keyset.toByteArray());
    assertEquals(keyset, handle.getKeyset());
    handle.getPrimitive(Mac.class);
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) Test(org.junit.Test)

Example 38 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class JsonKeysetWriterTest method testWrite_writesNegativeIdAsPositive.

@Test
public void testWrite_writesNegativeIdAsPositive() throws Exception {
    int magicKeyId = -19230912;
    Keyset unmodified = CleartextKeysetHandle.getKeyset(KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA256_128BITTAG));
    Keyset modified = Keyset.newBuilder(unmodified).setPrimaryKeyId(magicKeyId).setKey(0, Keyset.Key.newBuilder(unmodified.getKey(0)).setKeyId(magicKeyId).build()).build();
    KeysetHandle modifiedHandle = CleartextKeysetHandle.parseFrom(modified.toByteArray());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CleartextKeysetHandle.write(modifiedHandle, JsonKeysetWriter.withOutputStream(outputStream));
    assertThat(outputStream.toString()).contains("\"primaryKeyId\":4275736384");
    assertThat(outputStream.toString()).contains("\"keyId\":4275736384");
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 39 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class KeysetHandleTest method getKeys.

@Test
public void getKeys() throws Exception {
    KeyTemplate keyTemplate = KeyTemplates.get("AES128_EAX");
    KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
    final int numKeys = 3;
    for (int i = 0; i < numKeys; i++) {
        keysetManager.add(keyTemplate);
    }
    KeysetHandle handle = keysetManager.getKeysetHandle();
    Keyset keyset = handle.getKeyset();
    List<KeyHandle> keysetKeys = handle.getKeys();
    expect.that(keysetKeys).hasSize(numKeys);
    Map<Integer, KeyHandle> keysetKeysMap = keysetKeys.stream().collect(Collectors.toMap(KeyHandle::getId, key -> key));
    for (Keyset.Key key : keyset.getKeyList()) {
        expect.that(keysetKeysMap).containsKey(key.getKeyId());
        KeyHandle keysetKey = keysetKeysMap.get(key.getKeyId());
        expect.that(KeyStatusTypeProtoConverter.toProto(keysetKey.getStatus())).isEqualTo(key.getStatus());
        KeyData keyData = ((ProtoKey) keysetKey.getKey(SecretKeyAccess.insecureSecretAccess())).getProtoKey();
        expect.that(keyData).isEqualTo(key.getKeyData());
    }
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Keyset(com.google.crypto.tink.proto.Keyset) PublicKeySignFactory(com.google.crypto.tink.signature.PublicKeySignFactory) PublicKeyVerifyFactory(com.google.crypto.tink.signature.PublicKeyVerifyFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BeforeClass(org.junit.BeforeClass) SignatureConfig(com.google.crypto.tink.signature.SignatureConfig) Assert.assertThrows(org.junit.Assert.assertThrows) KeyStatusType(com.google.crypto.tink.proto.KeyStatusType) RunWith(org.junit.runner.RunWith) EcdsaPrivateKey(com.google.crypto.tink.proto.EcdsaPrivateKey) Random(com.google.crypto.tink.subtle.Random) ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) TreeSet(java.util.TreeSet) GeneralSecurityException(java.security.GeneralSecurityException) SignatureKeyTemplates(com.google.crypto.tink.signature.SignatureKeyTemplates) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyHandle(com.google.crypto.tink.tinkkey.KeyHandle) AesEaxKeyManager(com.google.crypto.tink.aead.AesEaxKeyManager) ExtensionRegistryLite(com.google.protobuf.ExtensionRegistryLite) Map(java.util.Map) TestUtil(com.google.crypto.tink.testing.TestUtil) SecretKeyAccess(com.google.crypto.tink.tinkkey.SecretKeyAccess) Expect(com.google.common.truth.Expect) AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) UTF_8(java.nio.charset.StandardCharsets.UTF_8) TinkConfig(com.google.crypto.tink.config.TinkConfig) OutputPrefixType(com.google.crypto.tink.proto.OutputPrefixType) Set(java.util.Set) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Hex(com.google.crypto.tink.subtle.Hex) Collectors(java.util.stream.Collectors) KeyStatusTypeProtoConverter(com.google.crypto.tink.internal.KeyStatusTypeProtoConverter) List(java.util.List) Rule(org.junit.Rule) KeyAccess(com.google.crypto.tink.tinkkey.KeyAccess) AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) KeyData(com.google.crypto.tink.proto.KeyData) ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) KeyHandle(com.google.crypto.tink.tinkkey.KeyHandle) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 40 with Keyset

use of com.google.crypto.tink.proto.Keyset in project tink by google.

the class TinkProtoTest method testKeysetBasic.

@Test
public void testKeysetBasic() throws Exception {
    Keyset keyset = Keyset.newBuilder().setPrimaryKeyId(1).build();
    assertEquals(1, keyset.getPrimaryKeyId());
}
Also used : Keyset(com.google.crypto.tink.proto.Keyset) Test(org.junit.Test)

Aggregations

Keyset (com.google.crypto.tink.proto.Keyset)108 Test (org.junit.Test)81 GeneralSecurityException (java.security.GeneralSecurityException)22 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)17 KeysetHandle (com.google.crypto.tink.KeysetHandle)17 KeyData (com.google.crypto.tink.proto.KeyData)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)12 EncryptedKeyset (com.google.crypto.tink.proto.EncryptedKeyset)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteString (com.google.protobuf.ByteString)10 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)10 Key (com.google.crypto.tink.proto.Keyset.Key)9 JsonObject (com.google.gson.JsonObject)9 AesGcmKey (com.google.crypto.tink.proto.AesGcmKey)8 KeysetReader (com.google.crypto.tink.KeysetReader)7 IOException (java.io.IOException)7 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)6 AesGcmKeyFormat (com.google.crypto.tink.proto.AesGcmKeyFormat)6 Enums (com.google.crypto.tink.subtle.Enums)6 KeyHandle (com.google.crypto.tink.tinkkey.KeyHandle)6