use of com.google.crypto.tink.HybridDecrypt in project tink by google.
the class HybridDecryptCatalogueTest method testBasic.
@Test
public void testBasic() throws Exception {
HybridDecryptCatalogue catalogue = new HybridDecryptCatalogue();
// Check a single key type for decryption, incl. case-insensitve primitive name.
String keyType = "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
{
KeyManager<HybridDecrypt> manager = catalogue.getKeyManager(keyType, "HybridDecrypt", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
{
KeyManager<HybridDecrypt> manager = catalogue.getKeyManager(keyType, "HyBRidDeCRYPt", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
{
KeyManager<HybridDecrypt> manager = catalogue.getKeyManager(keyType, "HYBRIDDecRYPT", 0);
assertThat(manager.doesSupport(keyType)).isTrue();
}
// Check all entries from the current HybridConfig.
RegistryConfig config = HybridConfig.TINK_1_0_0;
int count = 0;
for (KeyTypeEntry entry : config.getEntryList()) {
if ("HybridDecrypt".equals(entry.getPrimitiveName())) {
count = count + 1;
KeyManager<HybridDecrypt> manager = catalogue.getKeyManager(entry.getTypeUrl(), "hybriddecrypt", entry.getKeyManagerVersion());
assertThat(manager.doesSupport(entry.getTypeUrl())).isTrue();
}
}
assertEquals(1, count);
}
use of com.google.crypto.tink.HybridDecrypt in project tink by google.
the class EciesAeadHkdfHybridDecryptTest method testModifyDecrypt.
private void testModifyDecrypt(CurveType curveType, KeyTemplate keyTemplate) throws Exception {
KeyPair recipientKey = EllipticCurves.generateKeyPair(curveType);
ECPublicKey recipientPublicKey = (ECPublicKey) recipientKey.getPublic();
ECPrivateKey recipientPrivateKey = (ECPrivateKey) recipientKey.getPrivate();
byte[] salt = Random.randBytes(8);
byte[] plaintext = Random.randBytes(4);
byte[] context = Random.randBytes(4);
String hmacAlgo = HybridUtil.toHmacAlgo(HashType.SHA256);
HybridEncrypt hybridEncrypt = new EciesAeadHkdfHybridEncrypt(recipientPublicKey, salt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
HybridDecrypt hybridDecrypt = new EciesAeadHkdfHybridDecrypt(recipientPrivateKey, salt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
byte[] ciphertext = hybridEncrypt.encrypt(plaintext, context);
byte[] decrypted = hybridDecrypt.decrypt(ciphertext, context);
assertArrayEquals(plaintext, decrypted);
// implicitly checks the modification of public key and the raw ciphertext.
for (int bytes = 0; bytes < ciphertext.length; bytes++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modifiedCiphertext = Arrays.copyOf(ciphertext, ciphertext.length);
modifiedCiphertext[bytes] ^= (byte) (1 << bit);
try {
hybridDecrypt.decrypt(modifiedCiphertext, context);
fail("Invalid ciphertext, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
// Modify context.
for (int bytes = 0; bytes < context.length; bytes++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modifiedContext = Arrays.copyOf(context, context.length);
modifiedContext[bytes] ^= (byte) (1 << bit);
try {
hybridDecrypt.decrypt(ciphertext, modifiedContext);
fail("Invalid context, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
// modifying the length may not be detected.
for (int bytes = 0; bytes < salt.length; bytes++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modifiedSalt = Arrays.copyOf(salt, salt.length);
modifiedSalt[bytes] ^= (byte) (1 << bit);
hybridDecrypt = new EciesAeadHkdfHybridDecrypt(recipientPrivateKey, modifiedSalt, hmacAlgo, EllipticCurves.PointFormatType.UNCOMPRESSED, new RegistryEciesAeadHkdfDemHelper(keyTemplate));
try {
hybridDecrypt.decrypt(ciphertext, context);
fail("Invalid salt, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
use of com.google.crypto.tink.HybridDecrypt in project tink by google.
the class PaymentMethodTokenHybridEncryptTest method testBasicMultipleEncrypts.
@Test
public void testBasicMultipleEncrypts() throws Exception {
ECParameterSpec spec = EllipticCurves.getNistP256Params();
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(spec);
KeyPair recipientKey = keyGen.generateKeyPair();
ECPublicKey recipientPublicKey = (ECPublicKey) recipientKey.getPublic();
ECPrivateKey recipientPrivateKey = (ECPrivateKey) recipientKey.getPrivate();
HybridEncrypt hybridEncrypt = new PaymentMethodTokenHybridEncrypt(recipientPublicKey, ProtocolVersionConfig.EC_V1);
HybridDecrypt hybridDecrypt = new PaymentMethodTokenHybridDecrypt(recipientPrivateKey, ProtocolVersionConfig.EC_V1);
testBasicMultipleEncrypts(hybridEncrypt, hybridDecrypt);
}
use of com.google.crypto.tink.HybridDecrypt in project tink by google.
the class HybridEncryptFactoryTest method testBasicEncryption.
@Test
public void testBasicEncryption() throws Exception {
EllipticCurveType curve = EllipticCurveType.NIST_P384;
HashType hashType = HashType.SHA256;
EcPointFormat primaryPointFormat = EcPointFormat.UNCOMPRESSED;
EcPointFormat rawPointFormat = EcPointFormat.COMPRESSED;
KeyTemplate primaryDemKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
KeyTemplate rawDemKeyTemplate = AeadKeyTemplates.AES128_CTR_HMAC_SHA256;
byte[] primarySalt = "some salt".getBytes("UTF-8");
byte[] rawSalt = "other salt".getBytes("UTF-8");
EciesAeadHkdfPrivateKey primaryPrivProto = TestUtil.generateEciesAeadHkdfPrivKey(curve, hashType, primaryPointFormat, primaryDemKeyTemplate, primarySalt);
Key primaryPriv = TestUtil.createKey(TestUtil.createKeyData(primaryPrivProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 8, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key primaryPub = TestUtil.createKey(TestUtil.createKeyData(primaryPrivProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 42, KeyStatusType.ENABLED, OutputPrefixType.RAW);
EciesAeadHkdfPrivateKey rawPrivProto = TestUtil.generateEciesAeadHkdfPrivKey(curve, hashType, rawPointFormat, rawDemKeyTemplate, rawSalt);
Key rawPriv = TestUtil.createKey(TestUtil.createKeyData(rawPrivProto, EciesAeadHkdfPrivateKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PRIVATE), 11, KeyStatusType.ENABLED, OutputPrefixType.RAW);
Key rawPub = TestUtil.createKey(TestUtil.createKeyData(rawPrivProto.getPublicKey(), EciesAeadHkdfPublicKeyManager.TYPE_URL, KeyData.KeyMaterialType.ASYMMETRIC_PUBLIC), 43, KeyStatusType.ENABLED, OutputPrefixType.RAW);
KeysetHandle keysetHandlePub = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPub, rawPub));
KeysetHandle keysetHandlePriv = TestUtil.createKeysetHandle(TestUtil.createKeyset(primaryPriv, rawPriv));
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.HybridDecrypt in project tink by google.
the class CreatePublicKeysetCommandTest method assertHybrid.
private void assertHybrid(KeysetReader privateReader, KeysetReader publicReader) throws Exception {
HybridDecrypt decrypter = HybridDecryptFactory.getPrimitive(CleartextKeysetHandle.read(privateReader));
HybridEncrypt encrypter = HybridEncryptFactory.getPrimitive(CleartextKeysetHandle.read(publicReader));
byte[] message = Random.randBytes(10);
byte[] contextInfo = Random.randBytes(20);
assertThat(decrypter.decrypt(encrypter.encrypt(message, contextInfo), contextInfo)).isEqualTo(message);
}
Aggregations