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;
}
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));
}
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));
}
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);
}
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);
}
Aggregations