Search in sources :

Example 21 with KeysetHandle

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

the class TinkeyUtil method getKeysetHandle.

/**
 * Returns a {@code KeysetHandle} from either a cleartext {@code Keyset} or a {@code
 * EncryptedKeyset}, read from {@code inputStream}.
 */
public static KeysetHandle getKeysetHandle(InputStream inputStream, String inFormat, String masterKeyUri, String credentialPath) throws IOException, GeneralSecurityException {
    KeysetReader reader = createKeysetReader(inputStream, inFormat);
    KeysetHandle handle;
    if (masterKeyUri != null) {
        Aead masterKey = KmsClients.getAutoLoaded(masterKeyUri).withCredentials(credentialPath).getAead(masterKeyUri);
        return KeysetHandle.read(reader, masterKey);
    }
    return CleartextKeysetHandle.read(reader);
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) BinaryKeysetReader(com.google.crypto.tink.BinaryKeysetReader) JsonKeysetReader(com.google.crypto.tink.JsonKeysetReader) KeysetReader(com.google.crypto.tink.KeysetReader) Aead(com.google.crypto.tink.Aead)

Example 22 with KeysetHandle

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

the class ListKeysetCommand method list.

/**
 * Lists all keys in the keyset in {@code inputStream} (using {@code credentialPath} to
 * decrypt if it is encrypted). This command doesn't output actual key material.
 */
public static void list(InputStream inputStream, String inFormat, String masterKeyUri, String credentialPath) throws Exception {
    KeysetHandle handle = TinkeyUtil.getKeysetHandle(inputStream, inFormat, masterKeyUri, credentialPath);
    KeysetInfo keysetInfo = handle.getKeysetInfo();
    System.out.println(keysetInfo.toString());
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) KeysetInfo(com.google.crypto.tink.proto.KeysetInfo)

Example 23 with KeysetHandle

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

the class AeadFactoryTest method testRawKeyAsPrimary.

@Test
public void testRawKeyAsPrimary() throws Exception {
    byte[] aesCtrKeyValue = Random.randBytes(AES_KEY_SIZE);
    byte[] hmacKeyValue = Random.randBytes(HMAC_KEY_SIZE);
    int ivSize = 12;
    int tagSize = 16;
    Key primary = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key raw = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
    Key legacy = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(aesCtrKeyValue, ivSize, hmacKeyValue, tagSize), 44, KeyStatusType.ENABLED, OutputPrefixType.LEGACY);
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(primary, raw, legacy));
    Aead aead = AeadFactory.getPrimitive(keysetHandle);
    byte[] plaintext = Random.randBytes(20);
    byte[] associatedData = Random.randBytes(20);
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertArrayEquals(plaintext, aead.decrypt(ciphertext, associatedData));
    assertEquals(CryptoFormat.RAW_PREFIX_SIZE + plaintext.length + ivSize + tagSize, ciphertext.length);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) Key(com.google.crypto.tink.proto.Keyset.Key) Test(org.junit.Test)

Example 24 with KeysetHandle

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

the class AesGcmKeyManagerTest method testCiphertextSize.

@Test
public void testCiphertextSize() throws Exception {
    byte[] keyValue = Random.randBytes(AES_KEY_SIZE);
    KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(TestUtil.createKey(TestUtil.createAesGcmKeyData(keyValue), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK)));
    Aead aead = AeadFactory.getPrimitive(keysetHandle);
    byte[] plaintext = "plaintext".getBytes("UTF-8");
    byte[] associatedData = "associatedData".getBytes("UTF-8");
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertEquals(CryptoFormat.NON_RAW_PREFIX_SIZE + 12 + /* IV_SIZE */
    plaintext.length + 16, /* TAG_SIZE */
    ciphertext.length);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) Test(org.junit.Test)

Example 25 with KeysetHandle

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

the class ChaCha20Poly1305KeyManagerTest method testCiphertextSize.

@Test
public void testCiphertextSize() throws Exception {
    KeysetHandle keysetHandle = KeysetHandle.generateNew(AeadKeyTemplates.CHACHA20_POLY1305);
    Aead aead = AeadFactory.getPrimitive(keysetHandle);
    byte[] plaintext = "plaintext".getBytes("UTF-8");
    byte[] associatedData = "associatedData".getBytes("UTF-8");
    byte[] ciphertext = aead.encrypt(plaintext, associatedData);
    assertEquals(CryptoFormat.NON_RAW_PREFIX_SIZE + 12 + /* IV_SIZE */
    plaintext.length + 16, /* TAG_SIZE */
    ciphertext.length);
}
Also used : KeysetHandle(com.google.crypto.tink.KeysetHandle) Aead(com.google.crypto.tink.Aead) 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