Search in sources :

Example 1 with KeysetHandle

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

the class Commands method getKeysetHandle.

/**
 * Loads a KeysetHandle from {@code keyset} or generate a new one if it doesn't exist.
 */
private static KeysetHandle getKeysetHandle(File keyset) throws GeneralSecurityException, IOException {
    if (keyset.exists()) {
        // https://github.com/google/tink/blob/master/doc/JAVA-HOWTO.md#loading-existing-keysets.
        return CleartextKeysetHandle.read(JsonKeysetReader.withFile(keyset));
    }
    KeysetHandle handle = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM);
    CleartextKeysetHandle.write(handle, JsonKeysetWriter.withFile(keyset));
    return handle;
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle)

Example 2 with KeysetHandle

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

the class EciesAeadHkdfPrivateKeyManagerTest method testNewKey.

@Test
public void testNewKey() throws Exception {
    EllipticCurveType curve = EllipticCurveType.NIST_P384;
    HashType hashType = HashType.SHA256;
    EcPointFormat pointFormat = EcPointFormat.UNCOMPRESSED;
    KeyTemplate demKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
    byte[] salt = "some salt".getBytes("UTF-8");
    EciesAeadHkdfParams params = HybridKeyTemplates.createEciesAeadHkdfParams(curve, hashType, pointFormat, demKeyTemplate, salt);
    EciesAeadHkdfPrivateKeyManager manager = new EciesAeadHkdfPrivateKeyManager();
    EciesAeadHkdfPrivateKey keyProto = (EciesAeadHkdfPrivateKey) manager.newKey(EciesAeadHkdfKeyFormat.newBuilder().setParams(params).build());
    assertEquals(params, keyProto.getPublicKey().getParams());
    Key primaryPriv = TestUtil.createKey(TestUtil.createKeyData(keyProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 8, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key primaryPub = TestUtil.createKey(TestUtil.createKeyData(keyProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    KeysetHandle keysetHandlePub = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPub));
    KeysetHandle keysetHandlePriv = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPriv));
    HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(keysetHandlePub);
    HybridDecrypt hybridDecrypt = HybridDecryptFactory.getPrimitive(keysetHandlePriv);
    byte[] plaintext = Random.randBytes(20);
    byte[] contextInfo = Random.randBytes(20);
    byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo);
    assertArrayEquals(plaintext, hybridDecrypt.decrypt(ciphertext, contextInfo));
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) EciesAeadHkdfParams(com.google.crypto.tink.proto.EciesAeadHkdfParams) HybridDecrypt(com.google.crypto.tink.HybridDecrypt) HashType(com.google.crypto.tink.proto.HashType) EcPointFormat(com.google.crypto.tink.proto.EcPointFormat) EciesAeadHkdfPrivateKey(com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey) EllipticCurveType(com.google.crypto.tink.proto.EllipticCurveType) HybridEncrypt(com.google.crypto.tink.HybridEncrypt) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) EciesAeadHkdfPrivateKey(com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Example 3 with KeysetHandle

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

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

the class KmsAeadKeyManagerTest method testGcpKmsKeyRestricted.

@Test
public void testGcpKmsKeyRestricted() throws Exception {
    KeysetHandle keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.createKmsAeadKeyTemplate(TestUtil.RESTRICTED_CRYPTO_KEY_URI));
    TestUtil.runBasicAeadFactoryTests(keysetHandle);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Test(org.junit.Test)

Example 5 with KeysetHandle

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

the class ChaCha20Poly1305KeyManagerTest method testBasic.

@Test
public void testBasic() throws Exception {
    KeysetHandle keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.CHACHA20_POLY1305);
    TestUtil.runBasicAeadFactoryTests(keysetHandle);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Test(org.junit.Test)

Aggregations

KeysetHandle (com.google.crypto.tink.KeysetHandle)42 Test (org.junit.Test)27 Key (com.google.crypto.tink.proto.Keyset.Key)13 GeneralSecurityException (java.security.GeneralSecurityException)10 Aead (com.google.crypto.tink.Aead)9 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)5 DeterministicAead (com.google.crypto.tink.DeterministicAead)5 PublicKeySign (com.google.crypto.tink.PublicKeySign)5 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)5 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)5 HybridDecrypt (com.google.crypto.tink.HybridDecrypt)4 HybridEncrypt (com.google.crypto.tink.HybridEncrypt)4 StreamingAead (com.google.crypto.tink.StreamingAead)3 EcdsaPrivateKey (com.google.crypto.tink.proto.EcdsaPrivateKey)3 EciesAeadHkdfPrivateKey (com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey)3 KeyData (com.google.crypto.tink.proto.KeyData)3 KeysetReader (com.google.crypto.tink.KeysetReader)2 Mac (com.google.crypto.tink.Mac)2 EcPointFormat (com.google.crypto.tink.proto.EcPointFormat)2 EllipticCurveType (com.google.crypto.tink.proto.EllipticCurveType)2