use of com.google.crypto.tink.proto.AesCtrHmacStreamingKey in project tink by google.
the class AesCtrHmacStreamingKeyManager method getPrimitive.
/**
* @param key {@code AesCtrHmacStreamingKey} proto
*/
@Override
public StreamingAead getPrimitive(MessageLite key) throws GeneralSecurityException {
if (!(key instanceof AesCtrHmacStreamingKey)) {
throw new GeneralSecurityException("expected AesCtrHmacStreamingKey proto");
}
AesCtrHmacStreamingKey keyProto = (AesCtrHmacStreamingKey) key;
validate(keyProto);
return new AesCtrHmacStreaming(keyProto.getKeyValue().toByteArray(), StreamingAeadUtil.toHmacAlgo(keyProto.getParams().getHkdfHashType()), keyProto.getParams().getDerivedKeySize(), StreamingAeadUtil.toHmacAlgo(keyProto.getParams().getHmacParams().getHash()), keyProto.getParams().getHmacParams().getTagSize(), keyProto.getParams().getCiphertextSegmentSize(), /* firstSegmentOffset= */
0);
}
use of com.google.crypto.tink.proto.AesCtrHmacStreamingKey in project tink by google.
the class AesCtrHmacStreamingKeyManagerTest method testNewKeyWithBadFormat.
@Test
public void testNewKeyWithBadFormat() throws Exception {
// key_size too small.
AesCtrHmacStreamingKeyFormat keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(15).build();
testNewKeyWithBadFormat(keyFormat);
// Unknown HKDF HashType.
AesCtrHmacStreamingParams badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(AES_KEY_SIZE).build();
testNewKeyWithBadFormat(badKeyParams);
// derived_key_size too small.
badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(128).setDerivedKeySize(10).setHkdfHashType(HashType.SHA256).build();
testNewKeyWithBadFormat(badKeyParams);
// ciphertext_segment_size too small.
badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(15).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
testNewKeyWithBadFormat(badKeyParams);
// No HmacParams.
badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).build();
testNewKeyWithBadFormat(badKeyParams);
// Unknown HmacParams.hash.
badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(HmacParams.newBuilder().build()).build();
testNewKeyWithBadFormat(badKeyParams);
// tag size too small.
badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(9).build()).build();
testNewKeyWithBadFormat(badKeyParams);
// tag size too big.
badKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(33).build()).build();
testNewKeyWithBadFormat(badKeyParams);
// All params good.
AesCtrHmacStreamingParams goodKeyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(130).setDerivedKeySize(AES_KEY_SIZE).setHkdfHashType(HashType.SHA256).setHmacParams(hmacParams).build();
keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(goodKeyParams).setKeySize(16).build();
ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
AesCtrHmacStreamingKey unusedKey = (AesCtrHmacStreamingKey) keyManager.newKey(keyFormat);
unusedKey = (AesCtrHmacStreamingKey) keyManager.newKey(serializedKeyFormat);
}
use of com.google.crypto.tink.proto.AesCtrHmacStreamingKey in project tink by google.
the class AesCtrHmacStreamingKeyManagerTest method testBasic.
@Test
public void testBasic() throws Exception {
// Create primitive from a given key.
AesCtrHmacStreamingKey key = AesCtrHmacStreamingKey.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.
AesCtrHmacStreamingKeyFormat keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
key = (AesCtrHmacStreamingKey) keyManager.newKey(serializedKeyFormat);
streamingAead = keyManager.getPrimitive(key);
StreamingTestUtil.testEncryptionAndDecryption(streamingAead);
}
use of com.google.crypto.tink.proto.AesCtrHmacStreamingKey in project tink by google.
the class AesCtrHmacStreamingKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
AesCtrHmacStreamingKeyFormat keyFormat = AesCtrHmacStreamingKeyFormat.newBuilder().setParams(keyParams).setKeySize(16).build();
ByteString serializedKeyFormat = ByteString.copyFrom(keyFormat.toByteArray());
Set<String> keys = new TreeSet<String>();
// Calls newKey multiple times and make sure that they generate different keys.
int numTests = 27;
for (int i = 0; i < numTests / 3; i++) {
AesCtrHmacStreamingKey key = (AesCtrHmacStreamingKey) keyManager.newKey(keyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
key = (AesCtrHmacStreamingKey) keyManager.newKey(serializedKeyFormat);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
KeyData keyData = keyManager.newKeyData(serializedKeyFormat);
key = AesCtrHmacStreamingKey.parseFrom(keyData.getValue());
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
assertEquals(16, key.getKeyValue().toByteArray().length);
}
assertEquals(numTests, keys.size());
}
use of com.google.crypto.tink.proto.AesCtrHmacStreamingKey in project tink by google.
the class TestUtil method createAesCtrHmacStreamingKeyData.
/**
* @return a {@code KeyData} containing a {@code AesCtrHmacStreamingKey}.
*/
public static KeyData createAesCtrHmacStreamingKeyData(byte[] keyValue, int derivedKeySize, int ciphertextSegmentSize) throws Exception {
HmacParams hmacParams = HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(16).build();
AesCtrHmacStreamingParams keyParams = AesCtrHmacStreamingParams.newBuilder().setCiphertextSegmentSize(ciphertextSegmentSize).setDerivedKeySize(derivedKeySize).setHkdfHashType(HashType.SHA256).setHmacParams(hmacParams).build();
AesCtrHmacStreamingKey keyProto = AesCtrHmacStreamingKey.newBuilder().setVersion(0).setKeyValue(ByteString.copyFrom(keyValue)).setParams(keyParams).build();
return createKeyData(keyProto, StreamingAeadConfig.AES_CTR_HMAC_STREAMINGAEAD_TYPE_URL, KeyData.KeyMaterialType.SYMMETRIC);
}
Aggregations