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