use of com.amazonaws.services.kms.model.EncryptResult in project spring-cloud-config-aws-kms by zalando.
the class KmsTextEncryptorTest method setUp.
@Before
public void setUp() throws Exception {
mockKms = mock(AWSKMS.class);
textEncryptor = new KmsTextEncryptor(mockKms, KMS_KEY_ID);
expectedEncryptRequest = new EncryptRequest();
expectedEncryptRequest.setKeyId(KMS_KEY_ID);
expectedEncryptRequest.setPlaintext(wrap(PLAINTEXT.getBytes()));
encryptResult = new EncryptResult();
encryptResult.setCiphertextBlob(wrap(CIPHER_TEXT.getBytes()));
when(mockKms.encrypt(any(EncryptRequest.class))).thenReturn(encryptResult);
expectedDecryptRequest = new DecryptRequest();
expectedDecryptRequest.setCiphertextBlob(wrap(CIPHER_TEXT.getBytes()));
decryptResult = new DecryptResult();
decryptResult.setPlaintext(wrap(PLAINTEXT.getBytes()));
when(mockKms.decrypt(any(DecryptRequest.class))).thenReturn(decryptResult);
}
use of com.amazonaws.services.kms.model.EncryptResult in project tink by google.
the class AwsKmsAeadTest method testDecryptShouldThrowExceptionIfKeyIdIsDifferent.
@Test
public void testDecryptShouldThrowExceptionIfKeyIdIsDifferent() throws Exception {
DecryptResult mockDecryptResult = mock(DecryptResult.class);
EncryptResult mockEncryptResult = mock(EncryptResult.class);
when(mockKms.decrypt(isA(DecryptRequest.class))).thenReturn(mockDecryptResult);
when(mockKms.encrypt(isA(EncryptRequest.class))).thenReturn(mockEncryptResult);
Aead aead = new AwsKmsAead(mockKms, KEY_ID);
byte[] aad = Random.randBytes(20);
byte[] message = Random.randBytes(20);
when(mockEncryptResult.getCiphertextBlob()).thenReturn(ByteBuffer.wrap(message));
when(mockDecryptResult.getKeyId()).thenReturn(KEY_ID + "1");
byte[] ciphertext = aead.encrypt(message, aad);
try {
aead.decrypt(ciphertext, aad);
fail("Expected GeneralSecurityException");
} catch (GeneralSecurityException e) {
// expected.
}
}
use of com.amazonaws.services.kms.model.EncryptResult in project tink by google.
the class AwsKmsAeadTest method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
DecryptResult mockDecryptResult = mock(DecryptResult.class);
EncryptResult mockEncryptResult = mock(EncryptResult.class);
when(mockKms.decrypt(isA(DecryptRequest.class))).thenReturn(mockDecryptResult);
when(mockKms.encrypt(isA(EncryptRequest.class))).thenReturn(mockEncryptResult);
Aead aead = new AwsKmsAead(mockKms, KEY_ID);
byte[] aad = Random.randBytes(20);
for (int messageSize = 0; messageSize < 75; messageSize++) {
byte[] message = Random.randBytes(messageSize);
when(mockDecryptResult.getKeyId()).thenReturn(KEY_ID);
when(mockDecryptResult.getPlaintext()).thenReturn(ByteBuffer.wrap(message));
when(mockEncryptResult.getCiphertextBlob()).thenReturn(ByteBuffer.wrap(message));
byte[] ciphertext = aead.encrypt(message, aad);
byte[] decrypted = aead.decrypt(ciphertext, aad);
assertArrayEquals(message, decrypted);
}
}
use of com.amazonaws.services.kms.model.EncryptResult in project tink by google.
the class AwsKmsAeadTest method testDecryptShouldThrowExceptionIfRequestFailed.
@Test
public void testDecryptShouldThrowExceptionIfRequestFailed() throws Exception {
EncryptResult mockEncryptResult = mock(EncryptResult.class);
when(mockKms.encrypt(isA(EncryptRequest.class))).thenReturn(mockEncryptResult);
AmazonServiceException exception = mock(AmazonServiceException.class);
when(mockKms.decrypt(isA(DecryptRequest.class))).thenThrow(exception);
Aead aead = new AwsKmsAead(mockKms, KEY_ID);
byte[] aad = Random.randBytes(20);
byte[] message = Random.randBytes(20);
when(mockEncryptResult.getCiphertextBlob()).thenReturn(ByteBuffer.wrap(message));
byte[] ciphertext = aead.encrypt(message, aad);
try {
aead.decrypt(ciphertext, aad);
fail("Expected GeneralSecurityException");
} catch (GeneralSecurityException e) {
// expected.
}
}
Aggregations