Search in sources :

Example 1 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project LabDayApp by jakdor.

the class ProjectRepository method loadAccessToken.

/**
 * Loads access token and decrypts it with Conceal
 * @param context required for SharedPreferences/Conceal
 * @return boolean success/fail
 */
public boolean loadAccessToken(Context context) {
    KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
    Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
    try {
        byte[] encryptedToken = readFile(context, "lab");
        if (encryptedToken == null) {
            Log.e(CLASS_TAG, "Access token not found");
            return false;
        } else if (encryptedToken.length == 0) {
            Log.e(CLASS_TAG, "Access token not available");
            return false;
        }
        byte[] plainToken = crypto.decrypt(encryptedToken, Entity.create("token"));
        this.accessToken = new String(plainToken);
        Log.i(CLASS_TAG, "Loaded and decrypted access token");
    } catch (Exception e) {
        Log.wtf(CLASS_TAG, "unable to decipher access token");
        return false;
    }
    return true;
}
Also used : Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)

Example 2 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project TeamCityApp by vase4kin.

the class AppModule method providesCryptoManager.

@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
@Provides
@Singleton
protected CryptoManager providesCryptoManager() {
    KeyChain keyChain = new SharedPrefsBackedKeyChain(mApplication.getApplicationContext(), CryptoConfig.KEY_256);
    Crypto crypto = AndroidConceal.get().createDefaultCrypto(keyChain);
    return new CryptoManagerImpl(crypto);
}
Also used : Crypto(com.facebook.crypto.Crypto) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) CryptoManagerImpl(com.github.vase4kin.teamcityapp.crypto.CryptoManagerImpl) KeyChain(com.facebook.crypto.keychain.KeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) VisibleForTesting(android.support.annotation.VisibleForTesting) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 3 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project conceal by facebook.

the class SharedPrefsBackedKeyChainTest method testLegacy128Bits.

public void testLegacy128Bits() throws Exception {
    KeyChain keyChain = new SharedPrefsBackedKeyChain(this.getInstrumentation().getContext());
    // destroy keys if they were present in prefs from previous test
    keyChain.destroyKeys();
    byte[] key = keyChain.getCipherKey();
    assertEquals(16, key.length);
    KeyChain keyChain2 = new SharedPrefsBackedKeyChain(this.getInstrumentation().getContext());
    byte[] key2 = keyChain.getCipherKey();
    assertTrue(Arrays.equals(key, key2));
}
Also used : SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) SharedPrefsBackedKeyChain(com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain)

Example 4 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project conceal by facebook.

the class Cipher128BitsTest method testCase.

public void testCase(String key, String iv, String plain, String cipher) throws Exception {
    // Test Case 16 - Page 40
    KeyChain keyChain = fixedKeyChain(key, iv);
    CryptoConfig config = CryptoConfig.KEY_128;
    Crypto crypto = new Crypto(keyChain, new AndroidCryptoLibrary(), config);
    byte[] plainBytes = toBytes(plain);
    byte[] encrypted = crypto.encrypt(plainBytes, new Entity("whatever"));
    byte[] expected = toBytes(cipher);
    // remove initial 2 bytes + IV
    // remove final tag 16 bytes
    int metadataLength = config.getHeaderLength() + config.getTailLength();
    int prefix = metadataLength - config.tagLength;
    byte[] bareEncrypted = Arrays.copyOfRange(encrypted, prefix, encrypted.length - config.tagLength);
    Assert.assertTrue(Arrays.equals(expected, bareEncrypted));
}
Also used : CryptoTestUtils.fixedKeyChain(com.facebook.crypto.CryptoTestUtils.fixedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain) AndroidCryptoLibrary(com.facebook.android.crypto.keychain.AndroidCryptoLibrary)

Example 5 with KeyChain

use of com.facebook.crypto.keychain.KeyChain in project conceal by facebook.

the class EntityFactoryTest method testCase.

public void testCase(String key, String iv, String plain, String entityId) throws Exception {
    // Test Case 16 - Page 40
    KeyChain keyChain = fixedKeyChain(key, iv);
    CryptoConfig config = CryptoConfig.KEY_256;
    Crypto crypto = new Crypto(keyChain, new AndroidCryptoLibrary(), config);
    byte[] plainBytes = toBytes(plain);
    byte[] encrypted = crypto.encrypt(plainBytes, Entity.create(entityId));
    byte[] decrypted = crypto.decrypt(encrypted, Entity.create(entityId));
    Assert.assertFalse(Arrays.equals(encrypted, plainBytes));
    Assert.assertTrue(Arrays.equals(decrypted, plainBytes));
    try {
        crypto.decrypt(encrypted, Entity.utf16(entityId));
        Assert.fail("Decryption with old entity should have failed!");
    } catch (IOException ioe) {
    // ok, it shouldn't match!
    }
}
Also used : CryptoTestUtils.fixedKeyChain(com.facebook.crypto.CryptoTestUtils.fixedKeyChain) KeyChain(com.facebook.crypto.keychain.KeyChain) IOException(java.io.IOException) AndroidCryptoLibrary(com.facebook.android.crypto.keychain.AndroidCryptoLibrary)

Aggregations

KeyChain (com.facebook.crypto.keychain.KeyChain)12 SharedPrefsBackedKeyChain (com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain)5 AndroidCryptoLibrary (com.facebook.android.crypto.keychain.AndroidCryptoLibrary)3 Crypto (com.facebook.crypto.Crypto)3 CryptoTestUtils.fixedKeyChain (com.facebook.crypto.CryptoTestUtils.fixedKeyChain)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 SystemNativeCryptoLibrary (com.facebook.crypto.util.SystemNativeCryptoLibrary)2 OutputStream (java.io.OutputStream)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 CryptoManagerImpl (com.github.vase4kin.teamcityapp.crypto.CryptoManagerImpl)1 Provides (dagger.Provides)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Random (java.util.Random)1 Singleton (javax.inject.Singleton)1