use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class PrimitiveSetTest method testAddInvalidKey.
@Test
public void testAddInvalidKey() throws Exception {
PrimitiveSet<Mac> pset = PrimitiveSet.newPrimitiveSet();
Key key1 = Key.newBuilder().setKeyId(1).setStatus(KeyStatusType.ENABLED).build();
try {
pset.addPrimitive(new DummyMac1(), key1);
fail("Expected GeneralSecurityException.");
} catch (GeneralSecurityException e) {
assertExceptionContains(e, "unknown output prefix type");
}
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class EciesAeadHkdfPrivateKeyManagerTest method testNewKey.
@Test
public void testNewKey() throws Exception {
EllipticCurveType curve = EllipticCurveType.NIST_P384;
HashType hashType = HashType.SHA256;
EcPointFormat pointFormat = EcPointFormat.UNCOMPRESSED;
KeyTemplate demKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
byte[] salt = "some salt".getBytes("UTF-8");
EciesAeadHkdfParams params = HybridKeyTemplates.createEciesAeadHkdfParams(curve, hashType, pointFormat, demKeyTemplate, salt);
EciesAeadHkdfPrivateKeyManager manager = new EciesAeadHkdfPrivateKeyManager();
EciesAeadHkdfPrivateKey keyProto = (EciesAeadHkdfPrivateKey) manager.newKey(EciesAeadHkdfKeyFormat.newBuilder().setParams(params).build());
assertEquals(params, keyProto.getPublicKey().getParams());
Key primaryPriv = TestUtil.createKey(TestUtil.createKeyData(keyProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 8, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key primaryPub = TestUtil.createKey(TestUtil.createKeyData(keyProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandlePub = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPub));
KeysetHandle keysetHandlePriv = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPriv));
HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(keysetHandlePub);
HybridDecrypt hybridDecrypt = HybridDecryptFactory.getPrimitive(keysetHandlePriv);
byte[] plaintext = Random.randBytes(20);
byte[] contextInfo = Random.randBytes(20);
byte[] ciphertext = hybridEncrypt.encrypt(plaintext, contextInfo);
assertArrayEquals(plaintext, hybridDecrypt.decrypt(ciphertext, contextInfo));
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class TestUtil method createPrimitiveSet.
/**
* @return a {@code PrimitiveSet} from a {@code KeySet}
*/
public static <P> PrimitiveSet<P> createPrimitiveSet(Keyset keyset, Class<P> inputClass) throws GeneralSecurityException {
PrimitiveSet<P> primitives = PrimitiveSet.newPrimitiveSet(inputClass);
for (Keyset.Key key : keyset.getKeyList()) {
if (key.getStatus() == KeyStatusType.ENABLED) {
P primitive = Registry.getPrimitive(key.getKeyData(), inputClass);
PrimitiveSet.Entry<P> entry = primitives.addPrimitive(primitive, key);
if (key.getKeyId() == keyset.getPrimaryKeyId()) {
primitives.setPrimary(entry);
}
}
}
return primitives;
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class TestUtil method createKeyset.
/**
* @return a keyset from a list of keys. The first key is primary.
*/
public static Keyset createKeyset(Key primary, Key... keys) throws Exception {
Keyset.Builder builder = Keyset.newBuilder();
builder.addKey(primary).setPrimaryKeyId(primary.getKeyId());
for (Key key : keys) {
builder.addKey(key);
}
return builder.build();
}
use of com.google.crypto.tink.proto.Keyset.Key in project tink by google.
the class DeterministicAeadFactoryTest method testInvalidKeyMaterial.
@Test
public void testInvalidKeyMaterial() throws Exception {
Key valid = TestUtil.createKey(TestUtil.createAesSivKeyData(64), 42, KeyStatusType.ENABLED, OutputPrefixType.TINK);
Key invalid = TestUtil.createKey(TestUtil.createAesCtrHmacAeadKeyData(Random.randBytes(16), 12, Random.randBytes(16), 16), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandle = TestUtil.createKeysetHandle(TestUtil.createKeyset(valid, invalid));
GeneralSecurityException e = assertThrows(GeneralSecurityException.class, () -> DeterministicAeadFactory.getPrimitive(keysetHandle));
assertExceptionContains(e, "com.google.crypto.tink.DeterministicAead not supported");
// invalid as the primary key.
KeysetHandle keysetHandle2 = TestUtil.createKeysetHandle(TestUtil.createKeyset(invalid, valid));
GeneralSecurityException e2 = assertThrows(GeneralSecurityException.class, () -> DeterministicAeadFactory.getPrimitive(keysetHandle2));
assertExceptionContains(e2, "com.google.crypto.tink.DeterministicAead not supported");
}
Aggregations