Search in sources :

Example 1 with KeyType

use of co.krypt.krypton.crypto.KeyType in project krypton-android by kryptco.

the class KeyManagerInstrumentedTest method keyGenerationAndDeletion_succeed.

@Test
public void keyGenerationAndDeletion_succeed() throws Exception {
    final Context context = InstrumentationRegistry.getTargetContext();
    for (KeyType type : SUPPORTED_KEY_TYPES) {
        KeyManager.deleteKeyPair(context, type, "test");
        SSHKeyPairI kp1 = KeyManager.loadOrGenerateKeyPair(InstrumentationRegistry.getTargetContext(), type, "test");
        Log.i("TEST", kp1.publicKeyDERBase64());
        KeyManager.deleteKeyPair(context, type, "test");
    }
}
Also used : Context(android.content.Context) KeyType(co.krypt.krypton.crypto.KeyType) SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) Test(org.junit.Test)

Example 2 with KeyType

use of co.krypt.krypton.crypto.KeyType in project krypton-android by kryptco.

the class GenerateFragment method next.

private void next() {
    KeyType keyType = null;
    if (keyTypeButton.getText().equals(getString(R.string.ed25519_key_type))) {
        keyType = KeyType.Ed25519;
    } else if (keyTypeButton.getText().equals(getString(R.string.rsa_key_type))) {
        keyType = KeyType.RSA;
    } else {
        keyType = KeyType.RSA;
    }
    final KeyType finalKeyType = keyType;
    final FragmentActivity context = getActivity();
    final OnboardingProgress progress = new OnboardingProgress(getContext());
    progress.setStage(OnboardingStage.GENERATING);
    new Analytics(context).postEvent("onboard", "generate tapped", null, null, false);
    final long startMillis = System.currentTimeMillis();
    final GeneratingFragment generatingFragment = new GeneratingFragment();
    getActivity().getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).replace(R.id.activity_onboarding, generatingFragment).commit();
    final Fragment self = this;
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                final long start = System.currentTimeMillis();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SSHKeyPairI pair = KeyManager.loadOrGenerateKeyPair(context, finalKeyType, KeyManager.ME_TAG);
                new MeStorage(context).set(new Profile("", pair.publicKeySSHWireFormat(), null, null));
                final long genTime = System.currentTimeMillis() - start;
                new Analytics(context).postEvent("keypair", "generate", null, (int) (genTime / 1000), false);
                if (genTime < 5000) {
                    try {
                        Thread.sleep(5000 - genTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                generatingFragment.onGenerate();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                progress.setStage(OnboardingStage.ENTER_EMAIL);
                EnterEmailFragment enterEmailFragment = new EnterEmailFragment();
                final FragmentActivity activity = context;
                if (activity != null && !activity.isDestroyed() && !activity.isFinishing()) {
                    activity.getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).hide(generatingFragment).add(R.id.activity_onboarding, enterEmailFragment).show(enterEmailFragment).commitAllowingStateLoss();
                }
            } catch (InvalidKeyException | IOException | CryptoException | UnsupportedOperationException | IllegalArgumentException e) {
                e.printStackTrace();
                progress.reset();
                final FragmentActivity activity = context;
                if (activity != null && !activity.isDestroyed() && !activity.isFinishing()) {
                    GenerateFragment generateFragment = new GenerateFragment();
                    if (finalKeyType == KeyType.RSA) {
                        Bundle args = new Bundle();
                        args.putString(DEFAULT_KEY_TYPE_KEY, activity.getString(R.string.ed25519_key_type));
                        generateFragment.setArguments(args);
                        activity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(activity, "Error generating rsa key, try again to generate an ed25519 key.", Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                    activity.getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).replace(R.id.activity_onboarding, generateFragment).commitAllowingStateLoss();
                }
            }
        }
    }).start();
}
Also used : KeyType(co.krypt.krypton.crypto.KeyType) Bundle(android.os.Bundle) SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) Fragment(android.support.v4.app.Fragment) Analytics(co.krypt.krypton.analytics.Analytics) Profile(co.krypt.krypton.protocol.Profile) FragmentActivity(android.support.v4.app.FragmentActivity) MeStorage(co.krypt.krypton.me.MeStorage) CryptoException(co.krypt.krypton.exception.CryptoException)

Example 3 with KeyType

use of co.krypt.krypton.crypto.KeyType in project krypton-android by kryptco.

the class KeyManagerInstrumentedTest method keyGeneration_isIdempotent.

@Test
public void keyGeneration_isIdempotent() throws Exception {
    final Context context = InstrumentationRegistry.getTargetContext();
    for (KeyType type : SUPPORTED_KEY_TYPES) {
        SSHKeyPairI key1 = KeyManager.loadOrGenerateKeyPair(context, type, "test");
        SSHKeyPairI key2 = KeyManager.loadOrGenerateKeyPair(context, type, "test");
        assertEquals(key1, key2);
    }
}
Also used : Context(android.content.Context) KeyType(co.krypt.krypton.crypto.KeyType) SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) Test(org.junit.Test)

Example 4 with KeyType

use of co.krypt.krypton.crypto.KeyType in project krypton-android by kryptco.

the class KeyManagerInstrumentedTest method signTamperAndVerify_fails.

@Test
public void signTamperAndVerify_fails() throws Exception {
    final Context context = InstrumentationRegistry.getTargetContext();
    for (KeyType type : SUPPORTED_KEY_TYPES) {
        for (SSHKeyPairI key : new SSHKeyPairI[] { KeyManager.loadOrGenerateKeyPair(context, type, "test"), new RSAKeyManager(context).loadOrGenerateNoDigestKeyPair("testnodigest") }) {
            byte[] data = SecureRandom.getSeed(32);
            for (String digest : SUPPORTED_DIGESTS) {
                byte[] signature = key.signDigest(digest, data);
                signature[signature.length - 1] ^= 0x01;
                assertTrue(!key.verifyDigest(digest, signature, data));
            }
        }
    }
}
Also used : Context(android.content.Context) KeyType(co.krypt.krypton.crypto.KeyType) SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) RSAKeyManager(co.krypt.krypton.crypto.RSAKeyManager) Test(org.junit.Test)

Example 5 with KeyType

use of co.krypt.krypton.crypto.KeyType in project krypton-android by kryptco.

the class KeyManagerInstrumentedTest method signAndVerify_succeed.

@Test
public void signAndVerify_succeed() throws Exception {
    final Context context = InstrumentationRegistry.getTargetContext();
    for (KeyType type : SUPPORTED_KEY_TYPES) {
        for (SSHKeyPairI key : new SSHKeyPairI[] { KeyManager.loadOrGenerateKeyPair(context, type, "test"), new RSAKeyManager(context).loadOrGenerateNoDigestKeyPair("testnodigest") }) {
            byte[] data = SecureRandom.getSeed(32);
            for (String digest : SUPPORTED_DIGESTS) {
                byte[] signature = key.signDigest(digest, data);
                assertTrue(key.verifyDigest(digest, signature, data));
            }
        }
    }
}
Also used : Context(android.content.Context) KeyType(co.krypt.krypton.crypto.KeyType) SSHKeyPairI(co.krypt.krypton.crypto.SSHKeyPairI) RSAKeyManager(co.krypt.krypton.crypto.RSAKeyManager) Test(org.junit.Test)

Aggregations

KeyType (co.krypt.krypton.crypto.KeyType)6 SSHKeyPairI (co.krypt.krypton.crypto.SSHKeyPairI)6 Test (org.junit.Test)5 Context (android.content.Context)4 RSAKeyManager (co.krypt.krypton.crypto.RSAKeyManager)2 Bundle (android.os.Bundle)1 Fragment (android.support.v4.app.Fragment)1 FragmentActivity (android.support.v4.app.FragmentActivity)1 Analytics (co.krypt.krypton.analytics.Analytics)1 CryptoException (co.krypt.krypton.exception.CryptoException)1 MeStorage (co.krypt.krypton.me.MeStorage)1 PGPPublicKey (co.krypt.krypton.pgp.PGPPublicKey)1 UserID (co.krypt.krypton.pgp.UserID)1 CertifiedPublicKey (co.krypt.krypton.pgp.publickey.CertifiedPublicKey)1 Profile (co.krypt.krypton.protocol.Profile)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 InvalidKeyException (java.security.InvalidKeyException)1