Search in sources :

Example 1 with GCMParameterSpec

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);
}
Also used : SecretKey(javax.crypto.SecretKey) FileNotFoundException(java.io.FileNotFoundException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 2 with GCMParameterSpec

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;
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator)

Example 3 with GCMParameterSpec

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);
}
Also used : SecretKey(javax.crypto.SecretKey) FileNotFoundException(java.io.FileNotFoundException) Cipher(javax.crypto.Cipher) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 4 with GCMParameterSpec

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()));
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Example 5 with GCMParameterSpec

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());
}
Also used : GCMParameterSpec(javax.crypto.spec.GCMParameterSpec)

Aggregations

GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)82 Cipher (javax.crypto.Cipher)55 SecretKeySpec (javax.crypto.spec.SecretKeySpec)33 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)23 SecretKey (javax.crypto.SecretKey)21 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)20 InvalidKeyException (java.security.InvalidKeyException)19 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)19 BadPaddingException (javax.crypto.BadPaddingException)17 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)17 Key (java.security.Key)11 AEADBadTagException (javax.crypto.AEADBadTagException)11 GeneralSecurityException (java.security.GeneralSecurityException)9 ByteBuffer (java.nio.ByteBuffer)6 Test (org.junit.Test)6 RequiresApi (android.support.annotation.RequiresApi)5 ExcludedTest (com.google.security.wycheproof.WycheproofRunner.ExcludedTest)5 NoPresubmitTest (com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest)5 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)5 FileNotFoundException (java.io.FileNotFoundException)5