Search in sources :

Example 51 with KeyData

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

the class KeysetHandle method getPublicKeysetHandle.

/**
 * If the managed keyset contains private keys, returns a {@link KeysetHandle} of the public keys.
 *
 * @throws GenernalSecurityException if the managed keyset is null or if it contains any
 *     non-private keys.
 */
public KeysetHandle getPublicKeysetHandle() throws GeneralSecurityException {
    if (keyset == null) {
        throw new GeneralSecurityException("cleartext keyset is not available");
    }
    Keyset.Builder keysetBuilder = Keyset.newBuilder();
    for (Keyset.Key key : keyset.getKeyList()) {
        KeyData keyData = createPublicKeyData(key.getKeyData());
        keysetBuilder.addKey(Keyset.Key.newBuilder().mergeFrom(key).setKeyData(keyData).build());
    }
    keysetBuilder.setPrimaryKeyId(keyset.getPrimaryKeyId());
    return new KeysetHandle(keysetBuilder.build());
}
Also used : EncryptedKeyset(com.google.crypto.tink.proto.EncryptedKeyset) Keyset(com.google.crypto.tink.proto.Keyset) GeneralSecurityException(java.security.GeneralSecurityException) KeyData(com.google.crypto.tink.proto.KeyData)

Example 52 with KeyData

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

the class KeyHandleTest method createFromKey_keyDataUnknown_shouldHaveSecret.

@Test
public void createFromKey_keyDataUnknown_shouldHaveSecret() throws Exception {
    KeyTemplate kt = KeyTemplates.get("ED25519");
    KeyData kd = KeyData.newBuilder().mergeFrom(Registry.newKeyData(kt)).setKeyMaterialType(KeyData.KeyMaterialType.UNKNOWN_KEYMATERIAL).build();
    KeyHandle kh = KeyHandle.createFromKey(kd, kt.getOutputPrefixType());
    assertThat(kh.hasSecret()).isTrue();
}
Also used : KeyTemplate(com.google.crypto.tink.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 53 with KeyData

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

the class KeyHandleTest method generateNew_generatesDifferentKeys.

@Test
public void generateNew_generatesDifferentKeys() throws Exception {
    KeyTemplate template = KeyTemplates.get("AES128_EAX");
    Set<String> keys = new TreeSet<>();
    int numKeys = 2;
    for (int j = 0; j < numKeys; j++) {
        KeyHandle handle = KeyHandle.generateNew(template);
        ProtoKey protoKey = (ProtoKey) handle.getKey(SecretKeyAccess.insecureSecretAccess());
        KeyData keyData = protoKey.getProtoKey();
        AesEaxKey aesEaxKey = AesEaxKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
        keys.add(aesEaxKey.getKeyValue().toStringUtf8());
    }
    assertThat(keys).hasSize(numKeys);
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) ProtoKey(com.google.crypto.tink.tinkkey.internal.ProtoKey) TreeSet(java.util.TreeSet) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 54 with KeyData

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

the class KeyHandleTest method createFromKey_keyDataAsymmetricPrivate_shouldHaveSecret.

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

Example 55 with KeyData

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

the class KeyHandleTest method createFromKey_keyDataSymmetric_shouldHaveSecret.

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

Aggregations

KeyData (com.google.crypto.tink.proto.KeyData)66 Test (org.junit.Test)55 Keyset (com.google.crypto.tink.proto.Keyset)17 KeyTemplate (com.google.crypto.tink.KeyTemplate)16 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)11 GeneralSecurityException (java.security.GeneralSecurityException)10 ByteString (com.google.protobuf.ByteString)9 TreeSet (java.util.TreeSet)9 AesEaxKey (com.google.crypto.tink.proto.AesEaxKey)7 KeysetReader (com.google.crypto.tink.KeysetReader)6 ProtoKey (com.google.crypto.tink.tinkkey.internal.ProtoKey)6 StringReader (java.io.StringReader)6 KeysetHandle (com.google.crypto.tink.KeysetHandle)5 RsaSsaPssPublicKey (com.google.crypto.tink.proto.RsaSsaPssPublicKey)5 BufferedReader (java.io.BufferedReader)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)5 DummyAead (com.google.crypto.tink.TestUtil.DummyAead)4 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)4 Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)4 AesEaxKeyFormat (com.google.crypto.tink.proto.AesEaxKeyFormat)3