use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AeadKeyTemplatesTest method testAES128_CTR_HMAC_SHA256.
@Test
public void testAES128_CTR_HMAC_SHA256() throws Exception {
KeyTemplate template = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasAesCtrKeyFormat());
assertTrue(format.getAesCtrKeyFormat().hasParams());
assertEquals(16, format.getAesCtrKeyFormat().getKeySize());
assertEquals(16, format.getAesCtrKeyFormat().getParams().getIvSize());
assertTrue(format.hasHmacKeyFormat());
assertTrue(format.getHmacKeyFormat().hasParams());
assertEquals(32, format.getHmacKeyFormat().getKeySize());
assertEquals(16, format.getHmacKeyFormat().getParams().getTagSize());
assertEquals(HashType.SHA256, format.getHmacKeyFormat().getParams().getHash());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AeadKeyTemplatesTest method testCreateAesGcmKeyTemplate.
@Test
public void testCreateAesGcmKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
int keySize = 42;
KeyTemplate template = AeadKeyTemplates.createAesGcmKeyTemplate(keySize);
assertEquals(AesGcmKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesGcmKeyFormat format = AesGcmKeyFormat.parseFrom(template.getValue());
assertEquals(keySize, format.getKeySize());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class AeadKeyTemplatesTest method testCreateAesCtrHmacAeadKeyTemplate.
@Test
public void testCreateAesCtrHmacAeadKeyTemplate() throws Exception {
// Intentionally using "weird" or invalid values for parameters,
// to test that the function correctly puts them in the resulting template.
int aesKeySize = 42;
int ivSize = 72;
int hmacKeySize = 24;
int tagSize = 27;
HashType hashType = HashType.SHA224;
KeyTemplate template = AeadKeyTemplates.createAesCtrHmacAeadKeyTemplate(aesKeySize, ivSize, hmacKeySize, tagSize, hashType);
assertEquals(AesCtrHmacAeadKeyManager.TYPE_URL, template.getTypeUrl());
assertEquals(OutputPrefixType.TINK, template.getOutputPrefixType());
AesCtrHmacAeadKeyFormat format = AesCtrHmacAeadKeyFormat.parseFrom(template.getValue());
assertTrue(format.hasAesCtrKeyFormat());
assertTrue(format.getAesCtrKeyFormat().hasParams());
assertEquals(aesKeySize, format.getAesCtrKeyFormat().getKeySize());
assertEquals(ivSize, format.getAesCtrKeyFormat().getParams().getIvSize());
assertTrue(format.hasHmacKeyFormat());
assertTrue(format.getHmacKeyFormat().hasParams());
assertEquals(hmacKeySize, format.getHmacKeyFormat().getKeySize());
assertEquals(tagSize, format.getHmacKeyFormat().getParams().getTagSize());
assertEquals(hashType, format.getHmacKeyFormat().getParams().getHash());
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class Ed25519PrivateKeyManagerTest method testBasic.
@Test
public void testBasic() throws Exception {
Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
KeyTemplate template = SignatureKeyTemplates.ED25519;
MessageLite key = manager.newKey(template);
assertTrue(key instanceof Ed25519PrivateKey);
Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
assertEquals(32, keyProto.getKeyValue().size());
PublicKeySign signer = manager.getPrimitive(key);
assertTrue(signer instanceof Ed25519Sign);
byte[] message = Random.randBytes(20);
byte[] signature = signer.sign(message);
assertEquals(64, signature.length);
Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
assertTrue(verifier instanceof Ed25519Verify);
try {
verifier.verify(signature, message);
} catch (GeneralSecurityException e) {
fail("Do not expect GeneralSecurityException: " + e);
}
}
use of com.google.crypto.tink.proto.KeyTemplate in project tink by google.
the class HmacKeyManagerTest method testNewKeyMultipleTimes.
@Test
public void testNewKeyMultipleTimes() throws Exception {
HmacKeyManager keyManager = new HmacKeyManager();
HmacKeyFormat hmacKeyFormat = HmacKeyFormat.newBuilder().setParams(HmacParams.newBuilder().setHash(HashType.SHA256).setTagSize(16).build()).setKeySize(32).build();
ByteString serialized = ByteString.copyFrom(hmacKeyFormat.toByteArray());
KeyTemplate keyTemplate = KeyTemplate.newBuilder().setTypeUrl(HmacKeyManager.TYPE_URL).setValue(serialized).build();
// Calls newKey multiple times and make sure that we get different HmacKey each time.
Set<String> keys = new TreeSet<String>();
int numTests = 27;
for (int i = 0; i < numTests / 3; i++) {
HmacKey key = (HmacKey) keyManager.newKey(hmacKeyFormat);
assertEquals(32, key.getKeyValue().toByteArray().length);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
key = (HmacKey) keyManager.newKey(serialized);
assertEquals(32, key.getKeyValue().toByteArray().length);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
key = HmacKey.parseFrom(keyManager.newKeyData(keyTemplate.getValue()).getValue());
assertEquals(32, key.getKeyValue().toByteArray().length);
keys.add(TestUtil.hexEncode(key.getKeyValue().toByteArray()));
}
assertEquals(numTests, keys.size());
}
Aggregations