use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.
the class EciesAeadHkdfHybridDecryptTest method testEncryptDecrypt_mutatedContext_throws.
private static void testEncryptDecrypt_mutatedContext_throws(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);
String hmacAlgo = HybridUtil.toHmacAlgo(HashType.SHA256);
byte[] plaintext = Random.randBytes(4);
byte[] context = Random.randBytes(4);
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);
for (BytesMutation mutation : TestUtil.generateMutations(context)) {
// The test takes too long in TSan, so we stop after the first case.
assertThrows(GeneralSecurityException.class, () -> hybridDecrypt.decrypt(ciphertext, mutation.value));
if (TestUtil.isTsan()) {
return;
}
}
}
use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.
the class EciesAeadHkdfHybridDecryptTest method testEncryptDecrypt_mutatedCiphertext_throws.
private static void testEncryptDecrypt_mutatedCiphertext_throws(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);
String hmacAlgo = HybridUtil.toHmacAlgo(HashType.SHA256);
byte[] plaintext = Random.randBytes(4);
byte[] context = Random.randBytes(4);
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);
for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
assertThrows(GeneralSecurityException.class, () -> hybridDecrypt.decrypt(mutation.value, context));
// The test takes too long in TSan, so we stop after the first case.
if (TestUtil.isTsan()) {
return;
}
}
}
use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.
the class InsecureNonceChaCha20Poly1305Test method testModifyCiphertext.
@Test
public void testModifyCiphertext() throws Exception {
Assume.assumeFalse(TinkFips.useOnlyFips());
byte[] key = Random.randBytes(KEY_SIZE_IN_BYTES);
InsecureNonceChaCha20Poly1305 cipher = createInstance(key);
byte[] aad = Random.randBytes(16);
byte[] message = Random.randBytes(32);
byte[] nonce = Random.randBytes(NONCE_SIZE_IN_BYTES);
byte[] ciphertext = cipher.encrypt(nonce, message, aad);
for (BytesMutation mutation : TestUtil.generateMutations(ciphertext)) {
assertThrows(String.format("Decrypting modified ciphertext should fail : ciphertext = %s, aad = %s," + " description = %s", Hex.encode(mutation.value), Arrays.toString(aad), mutation.description), GeneralSecurityException.class, () -> {
byte[] unused = cipher.decrypt(nonce, mutation.value, aad);
});
}
// Modify AAD
for (int b = 0; b < aad.length; b++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modified = Arrays.copyOf(aad, aad.length);
modified[b] ^= (byte) (1 << bit);
assertThrows(AEADBadTagException.class, () -> {
byte[] unused = cipher.decrypt(nonce, ciphertext, modified);
});
}
}
}
use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.
the class EcdsaVerifyKeyManagerTest method testRfcTestVectors.
@Test
public void testRfcTestVectors() throws Exception {
for (int i = 0; i < rfcTestVectors.length; i++) {
RfcTestVector t = rfcTestVectors[i];
PublicKeyVerify verifier = createVerifier(t);
verifier.verify(t.sig, t.msg);
for (BytesMutation mutation : TestUtil.generateMutations(t.sig)) {
assertThrows(String.format("Invalid signature, should have thrown exception : sig = %s, msg = %s," + " description = %s", Hex.encode(mutation.value), Hex.encode(t.msg), mutation.description), GeneralSecurityException.class, () -> verifier.verify(mutation.value, t.msg));
}
}
}
use of com.google.crypto.tink.testing.TestUtil.BytesMutation in project tink by google.
the class EcdsaVerifyJceTest method testModification.
@Test
public void testModification() throws Exception {
Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
ECParameterSpec ecParams = EllipticCurves.getNistP256Params();
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecParams);
KeyPair keyPair = keyGen.generateKeyPair();
ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
EcdsaEncoding[] encodings = new EcdsaEncoding[] { EcdsaEncoding.IEEE_P1363, EcdsaEncoding.DER };
for (EcdsaEncoding encoding : encodings) {
// Sign with EcdsaSignJce
EcdsaSignJce signer = new EcdsaSignJce(priv, HashType.SHA256, encoding);
byte[] message = "Hello".getBytes("UTF-8");
byte[] signature = signer.sign(message);
// Verify with EcdsaVerifyJce.
EcdsaVerifyJce verifier = new EcdsaVerifyJce(pub, HashType.SHA256, encoding);
for (final BytesMutation mutation : TestUtil.generateMutations(signature)) {
assertThrows(String.format("Invalid signature, should have thrown exception : signature = %s, message = %s, " + " description = %s", Hex.encode(mutation.value), Arrays.toString(message), mutation.description), GeneralSecurityException.class, () -> verifier.verify(mutation.value, message));
}
// Encodings mismatch.
EcdsaVerifyJce verifier2 = new EcdsaVerifyJce(pub, HashType.SHA256, encoding == EcdsaEncoding.IEEE_P1363 ? EcdsaEncoding.DER : EcdsaEncoding.IEEE_P1363);
assertThrows(GeneralSecurityException.class, () -> verifier2.verify(signature, message));
}
}
Aggregations