use of javax.crypto.spec.GCMParameterSpec in project android_frameworks_base by ResurrectionRemix.
the class LockSettingsService method getDecryptedPasswordForTiedProfile.
private String getDecryptedPasswordForTiedProfile(int userId) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, CertificateException, IOException {
if (DEBUG)
Slog.v(TAG, "Get child profile decrytped key");
byte[] storedData = mStorage.readChildProfileLock(userId);
if (storedData == null) {
throw new FileNotFoundException("Child profile lock file not found");
}
byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE);
byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, storedData.length);
byte[] decryptionResult;
java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
SecretKey decryptionKey = (SecretKey) keyStore.getKey(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, null);
Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE);
cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv));
decryptionResult = cipher.doFinal(encryptedPassword);
return new String(decryptionResult, StandardCharsets.UTF_8);
}
use of javax.crypto.spec.GCMParameterSpec in project jdk8u_jdk by JetBrains.
the class TestNonexpanding method runTest.
public void runTest(String algo, String mo, String pad) throws Exception {
Cipher ci = null;
SecretKey key = null;
try {
// Initialization
Random rdm = new Random();
byte[] plainText = new byte[128];
rdm.nextBytes(plainText);
ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
kg.init(KEY_LENGTH);
key = kg.generateKey();
// encrypt
ci.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
// Comparison
if (!(plainText.length == cipherText.length)) {
// authentication tag and cipher text.
if (mo.equalsIgnoreCase("GCM")) {
GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);
int cipherTextLength = cipherText.length - spec.getTLen() / 8;
if (plainText.length == cipherTextLength) {
return;
}
}
System.out.println("Original length: " + plainText.length);
System.out.println("Cipher text length: " + cipherText.length);
throw new RuntimeException("Test failed!");
}
} catch (NoSuchAlgorithmException e) {
//CFB7 and OFB150 are for negative testing
if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
throw new RuntimeException("Test failed!");
}
} catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidParameterSpecException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
System.out.println("Test failed!");
throw e;
}
}
use of javax.crypto.spec.GCMParameterSpec in project android_frameworks_base by DirtyUnicorns.
the class LockSettingsService method getDecryptedPasswordForTiedProfile.
private String getDecryptedPasswordForTiedProfile(int userId) throws KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, CertificateException, IOException {
if (DEBUG)
Slog.v(TAG, "Get child profile decrytped key");
byte[] storedData = mStorage.readChildProfileLock(userId);
if (storedData == null) {
throw new FileNotFoundException("Child profile lock file not found");
}
byte[] iv = Arrays.copyOfRange(storedData, 0, PROFILE_KEY_IV_SIZE);
byte[] encryptedPassword = Arrays.copyOfRange(storedData, PROFILE_KEY_IV_SIZE, storedData.length);
byte[] decryptionResult;
java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
SecretKey decryptionKey = (SecretKey) keyStore.getKey(LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId, null);
Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/" + KeyProperties.ENCRYPTION_PADDING_NONE);
cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new GCMParameterSpec(128, iv));
decryptionResult = cipher.doFinal(encryptedPassword);
return new String(decryptionResult, StandardCharsets.UTF_8);
}
use of javax.crypto.spec.GCMParameterSpec in project robovm by robovm.
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 robovm by robovm.
the class GCMParameterSpecTest method testGetTLen_Success.
public void testGetTLen_Success() throws Exception {
GCMParameterSpec spec = new GCMParameterSpec(8, TEST_IV);
assertEquals(8, spec.getTLen());
}
Aggregations