Search in sources :

Example 6 with KeyData

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

the class EciesAeadHkdfPrivateKeyManagerTest method testGetPublicKeyData.

/**
 * Tests that a public key is extracted properly from a private key.
 */
@Test
public void testGetPublicKeyData() throws Exception {
    KeysetHandle privateHandle = KeysetHandle.generateNew(HybridKeyTemplates.ECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256);
    KeyData privateKeyData = TestUtil.getKeyset(privateHandle).getKey(0).getKeyData();
    EciesAeadHkdfPrivateKeyManager privateManager = new EciesAeadHkdfPrivateKeyManager();
    KeyData publicKeyData = privateManager.getPublicKeyData(privateKeyData.getValue());
    assertEquals(EciesAeadHkdfPublicKeyManager.TYPE_URL, publicKeyData.getTypeUrl());
    assertEquals(KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC, publicKeyData.getKeyMaterialType());
    EciesAeadHkdfPrivateKey privateKey = EciesAeadHkdfPrivateKey.parseFrom(privateKeyData.getValue());
    assertArrayEquals(privateKey.getPublicKey().toByteArray(), publicKeyData.getValue().toByteArray());
    EciesAeadHkdfPublicKeyManager publicManager = new EciesAeadHkdfPublicKeyManager();
    HybridEncrypt hybridEncrypt = publicManager.getPrimitive(publicKeyData.getValue());
    HybridDecrypt hybridDecrypt = privateManager.getPrimitive(privateKeyData.getValue());
    byte[] message = Random.randBytes(20);
    byte[] contextInfo = Random.randBytes(20);
    assertArrayEquals(message, hybridDecrypt.decrypt(hybridEncrypt.encrypt(message, contextInfo), contextInfo));
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) EciesAeadHkdfPrivateKey(com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 7 with KeyData

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

the class AesSivKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    for (KeyTemplate keyTemplate : keyTemplates) {
        AesSivKeyManager keyManager = new AesSivKeyManager();
        Set<String> keys = new TreeSet<String>();
        // Calls newKey multiple times and make sure that they generate different keys.
        int numTests = 10;
        for (int i = 0; i < numTests; i++) {
            AesSivKey key = (AesSivKey) keyManager.newKey(keyTemplate.getValue());
            keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
            KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
            key = AesSivKey.parseFrom(keyData.getValue());
            keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        }
        assertEquals(numTests * 2, keys.size());
    }
}
Also used : TreeSet(java.util.TreeSet) AesSivKey(com.google.crypto.tink.proto.AesSivKey) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 8 with KeyData

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

the class ChaCha20Poly1305KeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    KeyTemplate keyTemplate = AeadKeyTemplates.CHACHA20_POLY1305;
    ChaCha20Poly1305KeyManager keyManager = new ChaCha20Poly1305KeyManager();
    Set<String> keys = new TreeSet<String>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 10;
    for (int i = 0; i < numTests; i++) {
        ChaCha20Poly1305Key key = (ChaCha20Poly1305Key) keyManager.newKey(keyTemplate.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(32, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = ChaCha20Poly1305Key.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(32, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests * 2, keys.size());
}
Also used : ChaCha20Poly1305Key(com.google.crypto.tink.proto.ChaCha20Poly1305Key) TreeSet(java.util.TreeSet) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 9 with KeyData

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

the class AesEaxKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    AesEaxKeyFormat eaxKeyFormat = AesEaxKeyFormat.newBuilder().setParams(AesEaxParams.newBuilder().setIvSize(16).build()).setKeySize(16).build();
    ByteString serialized = ByteString.copyFrom(eaxKeyFormat.toByteArray());
    KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(AesEaxKeyManager.TYPE_URL).setValue(serialized).build();
    AesEaxKeyManager keyManager = new AesEaxKeyManager();
    Set<String> keys = new TreeSet<String>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 27;
    for (int i = 0; i < numTests / 3; i++) {
        AesEaxKey key = (AesEaxKey) keyManager.newKey(eaxKeyFormat);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        key = (AesEaxKey) keyManager.newKey(serialized);
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = AesEaxKey.parseFrom(keyData.getValue());
        keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
        assertEquals(16, key.getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : AesEaxKey(com.google.crypto.tink.proto.AesEaxKey) ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) AesEaxKeyFormat(com.google.crypto.tink.proto.AesEaxKeyFormat) ByteString(com.google.protobuf.ByteString) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) KeyData(com.google.crypto.tink.proto.KeyData) Test(org.junit.Test)

Example 10 with KeyData

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

the class AesCtrHmacAeadKeyManagerTest method testNewKeyMultipleTimes.

@Test
public void testNewKeyMultipleTimes() throws Exception {
    KeyTemplate keyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
    AesCtrHmacAeadKeyFormat aeadKeyFormat = AesCtrHmacAeadKeyFormat.parseFrom(keyTemplate.getValue().toByteArray());
    ByteString serialized = ByteString.copyFrom(aeadKeyFormat.toByteArray());
    AesCtrHmacAeadKeyManager keyManager = new AesCtrHmacAeadKeyManager();
    Set<String> keys = new TreeSet<String>();
    // Calls newKey multiple times and make sure that they generate different keys.
    int numTests = 24;
    for (int i = 0; i < numTests / 6; i++) {
        AesCtrHmacAeadKey key = (AesCtrHmacAeadKey) keyManager.newKey(aeadKeyFormat);
        keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
        keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
        assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
        assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
        key = (AesCtrHmacAeadKey) keyManager.newKey(serialized);
        keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
        keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
        assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
        assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
        KeyData keyData = keyManager.newKeyData(keyTemplate.getValue());
        key = AesCtrHmacAeadKey.parseFrom(keyData.getValue());
        keys.add(new String(key.getAesCtrKey().getKeyValue().toByteArray(), "UTF-8"));
        keys.add(new String(key.getHmacKey().getKeyValue().toByteArray(), "UTF-8"));
        assertEquals(16, key.getAesCtrKey().getKeyValue().toByteArray().length);
        assertEquals(32, key.getHmacKey().getKeyValue().toByteArray().length);
    }
    assertEquals(numTests, keys.size());
}
Also used : ByteString(com.google.protobuf.ByteString) TreeSet(java.util.TreeSet) AesCtrHmacAeadKeyFormat(com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat) ByteString(com.google.protobuf.ByteString) AesCtrHmacAeadKey(com.google.crypto.tink.proto.AesCtrHmacAeadKey) KeyTemplate(com.google.crypto.tink.proto.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