use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class AesGcmHkdfStreamingKeyManagerTest method testDeriveKey_badVersion_throws.
@Test
public void testDeriveKey_badVersion_throws() throws Exception {
final int keySize = 32;
final int derivedKeySize = 16;
AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.newBuilder(createKeyFormat(keySize, derivedKeySize, HashType.SHA256, 1024)).setVersion(1).build();
byte[] keyMaterial = Random.randBytes(32);
assertThrows(GeneralSecurityException.class, () -> factory.deriveKey(format, new ByteArrayInputStream(keyMaterial)));
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class AesGcmHkdfStreamingKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
AesGcmHkdfStreamingKeyFormat keyFormat = createKeyFormat(32, 32, HashType.SHA256, 1024);
Set<String> keys = new TreeSet<>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 100;
for (int i = 0; i < numTests; i++) {
keys.add(TestUtil.hexEncode(factory.createKey(keyFormat).getKeyValue().toByteArray()));
}
assertThat(keys).hasSize(numTests);
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class AesGcmHkdfStreamingKeyManagerTest method testDeriveKey_size32.
@Test
public void testDeriveKey_size32() throws Exception {
final int keySize = 32;
final int derivedKeySize = 16;
AesGcmHkdfStreamingKeyFormat format = createKeyFormat(keySize, derivedKeySize, HashType.SHA256, 1024);
byte[] keyMaterial = Random.randBytes(100);
AesGcmHkdfStreamingKey key = factory.deriveKey(format, new ByteArrayInputStream(keyMaterial));
assertThat(key.getKeyValue()).hasSize(32);
for (int i = 0; i < keySize; ++i) {
assertThat(key.getKeyValue().byteAt(i)).isEqualTo(keyMaterial[i]);
}
assertThat(key.getParams()).isEqualTo(format.getParams());
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class StreamingAeadKeyTemplatesTest method testCreateAesGcmHkdfStreamingKeyTemplate.
@Test
public void testCreateAesGcmHkdfStreamingKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
int mainKeySize = 42;
int derivedKeySize = 24;
int ciphertextSegmentSize = 12345;
HashType hkdfHashType = HashType.SHA512;
KeyTemplate template = StreamingAeadKeyTemplates.createAesGcmHkdfStreamingKeyTemplate(mainKeySize, hkdfHashType, derivedKeySize, ciphertextSegmentSize);
assertEquals(new AesGcmHkdfStreamingKeyManager().getKeyType(), template.getTypeUrl());
assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertEquals(mainKeySize, format.getKeySize());
assertEquals(derivedKeySize, format.getParams().getDerivedKeySize());
assertEquals(hkdfHashType, format.getParams().getHkdfHashType());
assertEquals(ciphertextSegmentSize, format.getParams().getCiphertextSegmentSize());
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class StreamingAeadKeyTemplatesTest method testAes256GcmHkdf_4KB.
@Test
public void testAes256GcmHkdf_4KB() throws Exception {
KeyTemplate template = StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB;
assertEquals(new AesGcmHkdfStreamingKeyManager().getKeyType(), template.getTypeUrl());
assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue(), ExtensionRegistryLite.getEmptyRegistry());
assertEquals(32, format.getKeySize());
assertEquals(32, format.getParams().getDerivedKeySize());
assertEquals(HashType.SHA256, format.getParams().getHkdfHashType());
assertEquals(4096, format.getParams().getCiphertextSegmentSize());
}
Aggregations