use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class SignalStorageCipher method decrypt.
public static byte[] decrypt(StorageCipherKey key, byte[] data) throws InvalidKeyException {
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
byte[][] split = Util.split(data, IV_LENGTH, data.length - IV_LENGTH);
byte[] iv = split[0];
byte[] cipherText = split[1];
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, iv));
return cipher.doFinal(cipherText);
} catch (java.security.InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
throw new InvalidKeyException(e);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class KeyStoreHelper method unseal.
@RequiresApi(Build.VERSION_CODES.M)
public static byte[] unseal(@NonNull SealedData sealedData) {
SecretKey secretKey = getKeyStoreEntry();
try {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, new GCMParameterSpec(128, sealedData.iv));
return cipher.doFinal(sealedData.data);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project Signal-Android by WhisperSystems.
the class ProfileCipher method encrypt.
/**
* Encrypts an input and ensures padded length.
* <p>
* Padded length does not include {@link #ENCRYPTION_OVERHEAD}.
*/
public byte[] encrypt(byte[] input, int paddedLength) {
try {
byte[] inputPadded = new byte[paddedLength];
if (input.length > inputPadded.length) {
throw new IllegalArgumentException("Input is too long: " + new String(input));
}
System.arraycopy(input, 0, inputPadded, 0, input.length);
byte[] nonce = Util.getSecretBytes(12);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.serialize(), "AES"), new GCMParameterSpec(128, nonce));
byte[] encryptedPadded = ByteUtil.combine(nonce, cipher.doFinal(inputPadded));
if (encryptedPadded.length != (paddedLength + ENCRYPTION_OVERHEAD)) {
throw new AssertionError(String.format(Locale.US, "Wrong output length %d != padded length %d + %d", encryptedPadded.length, paddedLength, ENCRYPTION_OVERHEAD));
}
return encryptedPadded;
} catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | BadPaddingException | NoSuchPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
throw new AssertionError(e);
}
}
use of javax.crypto.spec.GCMParameterSpec in project j2objc by google.
the class GCMParameterSpecTest method testGetIV_Success.
public void testGetIV_Success() throws Exception {
GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV);
byte[] actual = spec.getIV();
assertEquals(Arrays.toString(TEST_IV), Arrays.toString(actual));
// XOR with 0xFF so we're sure we changed the array
for (int i = 0; i < actual.length; i++) {
actual[i] ^= 0xFF;
}
assertFalse("Changing the IV returned shouldn't change the parameter spec", Arrays.equals(spec.getIV(), actual));
assertEquals(Arrays.toString(TEST_IV), Arrays.toString(spec.getIV()));
}
use of javax.crypto.spec.GCMParameterSpec in project j2objc by google.
the class CipherOutputStreamTest method testDecryptCorruptGCM.
// From b/36636576. CipherOutputStream had a bug where it would ignore exceptions
// thrown during close().
public void testDecryptCorruptGCM() throws Exception {
for (Provider provider : Security.getProviders()) {
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/GCM/NoPadding", provider);
} catch (NoSuchAlgorithmException e) {
continue;
}
SecretKey key;
if (provider.getName().equals("AndroidKeyStoreBCWorkaround")) {
key = getAndroidKeyStoreSecretKey();
} else {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(256);
key = keygen.generateKey();
}
GCMParameterSpec params = new GCMParameterSpec(128, new byte[12]);
byte[] unencrypted = new byte[200];
// we have to special-case it
if (provider.getName().equals("AndroidKeyStoreBCWorkaround")) {
cipher.init(Cipher.ENCRYPT_MODE, key);
} else {
cipher.init(Cipher.ENCRYPT_MODE, key, params);
}
byte[] encrypted = cipher.doFinal(unencrypted);
// Corrupt the final byte, which will corrupt the authentication tag
encrypted[encrypted.length - 1] ^= 1;
cipher.init(Cipher.DECRYPT_MODE, key, params);
CipherOutputStream cos = new CipherOutputStream(new ByteArrayOutputStream(), cipher);
try {
cos.write(encrypted);
cos.close();
fail("Writing a corrupted stream should throw an exception." + " Provider: " + provider);
} catch (IOException expected) {
assertTrue(expected.getCause() instanceof AEADBadTagException);
}
}
}
Aggregations