use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class StreamingAeadKeyTemplatesTest method testAES128_GCM_HKDF_4KB.
@Test
public void testAES128_GCM_HKDF_4KB() throws Exception {
KeyTemplate template = StreamingAeadKeyTemplates.AES128_GCM_HKDF_4KB;
assertEquals(AesGcmHkdfStreamingKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue());
assertEquals(16, format.getKeySize());
assertEquals(16, format.getParams().getDerivedKeySize());
assertEquals(HashType.SHA256, format.getParams().getHkdfHashType());
assertEquals(4096, format.getParams().getCiphertextSegmentSize());
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class AesGcmHkdfStreamingKeyManager method newKey.
/**
* @param keyFormat {@code AesGcmHkdfStreamingKeyFormat} proto
* @return new {@code AesGcmHkdfStreamingKey} proto
*/
@Override
public MessageLite newKey(MessageLite keyFormat) throws GeneralSecurityException {
if (!(keyFormat instanceof AesGcmHkdfStreamingKeyFormat)) {
throw new GeneralSecurityException("expected AesGcmHkdfStreamingKeyFormat proto");
}
AesGcmHkdfStreamingKeyFormat format = (AesGcmHkdfStreamingKeyFormat) keyFormat;
validate(format);
return AesGcmHkdfStreamingKey.newBuilder().setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))).setParams(format.getParams()).setVersion(VERSION).build();
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class AesGcmHkdfStreamingKeyManagerTest method testNewKeyWithBadFormat.
@Test
public void testNewKeyWithBadFormat() throws Exception {
// key_size too small.
AesGcmHkdfStreamingKeyFormat keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(15).build();
ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
try {
keyManager.newKey(keyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
try {
keyManager.newKeyData(serializedKeyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
// Unknown HKDF HashType.
AesGcmHkdfStreamingParams badKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(AES_KEY_SIZE).build();
keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(badKeyParams).setKeySize(16).build();
serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
try {
keyManager.newKey(keyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
try {
keyManager.newKeyData(serializedKeyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
// derived_key_size too small.
badKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(10).setHkdfHashType(HashType.SHA256).build();
keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(badKeyParams).setKeySize(16).build();
serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
try {
keyManager.newKey(keyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
try {
keyManager.newKeyData(serializedKeyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
// ciphertext_segment_size too small.
badKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(15).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(badKeyParams).setKeySize(16).build();
serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
try {
keyManager.newKey(keyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
try {
keyManager.newKeyData(serializedKeyFormat);
fail("Bad format, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
// All params good.
AesGcmHkdfStreamingParams goodKeyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(goodKeyParams).setKeySize(16).build();
serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
AesGcmHkdfStreamingKey unusedKey = (AesGcmHkdfStreamingKey) keyManager.newKey(keyFormat);
unusedKey = (AesGcmHkdfStreamingKey) keyManager.newKey(serializedKeyFormat);
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class AesGcmHkdfStreamingKeyManagerTest method testBasic.
@Test
public void testBasic() throws Exception {
// Create primitive from a given key.
AesGcmHkdfStreamingKey key = AesGcmHkdfStreamingKey.newBuilder().setVersion(0).setKeyValue(ByteString.copyFrom(Random.randBytes(20))).setParams(keyParams).build();
StreamingAead streamingAead = keyManager.getPrimitive(key);
StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
// Create a key from KeyFormat, and use the key.
AesGcmHkdfStreamingKeyFormat keyFormat = AesGcmHkdfStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
key = (AesGcmHkdfStreamingKey) keyManager.newKey(serializedKeyFormat);
streamingAead = keyManager.getPrimitive(key);
StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingKeyFormat in project tink by google.
the class StreamingAeadKeyTemplatesTest method testAES256_GCM_HKDF_4KB.
@Test
public void testAES256_GCM_HKDF_4KB() throws Exception {
KeyTemplate template = StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB;
assertEquals(AesGcmHkdfStreamingKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.RAW, template.getOutputPrefixType());
AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.parseFrom(template.getValue());
assertEquals(32, format.getKeySize());
assertEquals(32, format.getParams().getDerivedKeySize());
assertEquals(HashType.SHA256, format.getParams().getHkdfHashType());
assertEquals(4096, format.getParams().getCiphertextSegmentSize());
}
Aggregations