use of com.google.crypto.tink.proto.AesGcmHkdfStreamingParams 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.AesGcmHkdfStreamingParams in project tink by google.
the class TestUtil method createAesGcmHkdfStreamingKeyData.
/**
* @return a {@code KeyData} containing a {@code AesGcmHkdfStreamingKey}.
*/
public static KeyData createAesGcmHkdfStreamingKeyData(byte[] keyValue, int derivedKeySize, int ciphertextSegmentSize) throws Exception {
AesGcmHkdfStreamingParams keyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(ciphertextSegmentSize).setDerivedKeySize(derivedKeySize).setHkdfHashType(HashType.SHA256).build();
AesGcmHkdfStreamingKey keyProto = AesGcmHkdfStreamingKey.newBuilder().setVersion(0).setKeyValue(ByteString.copyFrom(keyValue)).setParams(keyParams).build();
return createKeyData(keyProto, StreamingAeadConfig.AES_GCM_HKDF_STREAMINGAEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
use of com.google.crypto.tink.proto.AesGcmHkdfStreamingParams in project tink by google.
the class StreamingAeadKeyTemplates method createAesGcmHkdfStreamingKeyTemplate.
/**
* @return a {@code KeyTemplate} containing a {@code AesGcmHkdfStreamingKeyFormat}
* with some specified parameters.
*/
public static KeyTemplate createAesGcmHkdfStreamingKeyTemplate(int mainKeySize, HashType hkdfHashType, int derivedKeySize, int ciphertextSegmentSize) {
AesGcmHkdfStreamingParams keyParams = AesGcmHkdfStreamingParams.newBuilder().setCiphertextSegmentSize(ciphertextSegmentSize).setDerivedKeySize(derivedKeySize).setHkdfHashType(hkdfHashType).build();
AesGcmHkdfStreamingKeyFormat format = AesGcmHkdfStreamingKeyFormat.newBuilder().setKeySize(mainKeySize).setParams(keyParams).build();
return KeyTemplate.newBuilder().setValue(format.toByteString()).setTypeUrl(AesGcmHkdfStreamingKeyManager.TYPE_URL).setOutputPrefixType(OutputPrefixType.RAW).build();
}
Aggregations