Search in sources :

Example 11 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project jdk8u_jdk by JetBrains.

the class KAParticipant method runTest.

public static boolean runTest(String algo, int numParties, String secretAlgo) {
    KAParticipant[] parties = new KAParticipant[numParties];
    Key[] keyArchives = new Key[numParties];
    try {
        // generate AlogirhtmParameterSpec
        AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH", "SunJCE");
        AlgorithmParameterSpec aps = new DHGenParameterSpec(512, 64);
        apg.init(aps);
        DHParameterSpec spec = apg.generateParameters().getParameterSpec(DHParameterSpec.class);
        //initilize all KeyAgreement participants
        for (int i = 0; i < numParties; i++) {
            parties[i] = new KAParticipant(PA_NAMES[i], algo);
            parties[i].initialize(spec);
            keyArchives[i] = parties[i].getPublicKey();
        }
        // Do all phases in the KeyAgreement for all participants
        Key[] keyBuffer = new Key[numParties];
        boolean lastPhase = false;
        for (int j = 0; j < numParties - 1; j++) {
            if (j == numParties - 2) {
                lastPhase = true;
            }
            for (int k = 0; k < numParties; k++) {
                if (k == numParties - 1) {
                    keyBuffer[k] = parties[k].doPhase(keyArchives[0], lastPhase);
                } else {
                    keyBuffer[k] = parties[k].doPhase(keyArchives[k + 1], lastPhase);
                }
            }
            System.arraycopy(keyBuffer, 0, keyArchives, 0, numParties);
        }
        //Comparison: The secret keys generated by all involved parties should be the same
        SecretKey[] sKeys = new SecretKey[numParties];
        for (int n = 0; n < numParties; n++) {
            sKeys[n] = parties[n].generateSecret(secretAlgo);
        }
        for (int q = 0; q < numParties - 1; q++) {
            if (!Arrays.equals(sKeys[q].getEncoded(), sKeys[q + 1].getEncoded())) {
                return false;
            }
        }
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}
Also used : DHGenParameterSpec(javax.crypto.spec.DHGenParameterSpec) AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) DHParameterSpec(javax.crypto.spec.DHParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) SecretKey(javax.crypto.SecretKey) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key) SecretKey(javax.crypto.SecretKey)

Example 12 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class KeyAgreementThread method test.

@Override
public void test() throws Exception {
    AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH");
    apg.init(1024, new SecureRandom());
    AlgorithmParameters ap = apg.generateParameters();
    DHParameterSpec ps = ap.getParameterSpec(DHParameterSpec.class);
    KeyAgreementGen kag1 = new KeyAgreementGen(ps);
    KeyAgreementGen kag2 = new KeyAgreementGen(ps);
    byte[] bArray1 = kag1.getPublicKeyBytes();
    byte[] bArray2 = kag2.getPublicKeyBytes();
    byte[] sk1 = kag1.getSecretKey(algName, bArray2);
    byte[] sk2 = kag2.getSecretKey(algName, bArray1);
    if (Arrays.areEqual(sk1, sk2) == false) {
        throw new Exception("Generated keys are not the same");
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 13 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class myAlgPG method testAlgorithmParameterGenerator02.

/**
     * Test for <code>getInstance(String algorithm)</code> method
     * Assertion: returns AlgorithmParameterGenerator instance
     * when algorithm is DSA
     */
public void testAlgorithmParameterGenerator02() throws NoSuchAlgorithmException {
    if (!DSASupported) {
        fail(validAlgName + " algorithm is not supported");
        return;
    }
    AlgorithmParameterGenerator apg;
    for (int i = 0; i < algs.length; i++) {
        apg = AlgorithmParameterGenerator.getInstance(algs[i]);
        assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]);
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator)

Example 14 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class AlgorithmParameterGenerator2Test method testGetInstance01.

/**
     * Test for <code>getInstance(String algorithm)</code> method
     * Assertions:
     * throws NullPointerException must be thrown is null
     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
     * returns AlgorithmParameterGenerator object
     */
public void testGetInstance01() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    try {
        AlgorithmParameterGenerator.getInstance(null);
        fail("NullPointerException or NoSuchAlgorithmException should be thrown");
    } catch (NullPointerException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            AlgorithmParameterGenerator.getInstance(invalidValues[i]);
            fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    AlgorithmParameterGenerator apG;
    for (int i = 0; i < validValues.length; i++) {
        apG = AlgorithmParameterGenerator.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", apG.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", apG.getProvider(), mProv);
        checkResult(apG);
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 15 with AlgorithmParameterGenerator

use of java.security.AlgorithmParameterGenerator in project robovm by robovm.

the class OldAlgorithmParameterGeneratorTest method test_initI.

public void test_initI() throws Exception {
    // Test for method void
    // java.security.AlgorithmParameterGenerator.init(int)
    // checks that no exception is thrown
    int[] valid = { 512, 576, 640, 960, 1024 };
    AlgorithmParameterGenerator gen = AlgorithmParameterGenerator.getInstance("DSA");
    for (int i = 0; i < valid.length; i++) {
        try {
            gen.init(valid[i]);
        } catch (Exception e) {
            fail("Exception should not be thrown for valid parameter" + valid[i]);
        }
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator)

Aggregations

AlgorithmParameterGenerator (java.security.AlgorithmParameterGenerator)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 AlgorithmParameters (java.security.AlgorithmParameters)5 SecureRandom (java.security.SecureRandom)4 DHParameterSpec (javax.crypto.spec.DHParameterSpec)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)2 BrokenTest (dalvik.annotation.BrokenTest)1 AlgorithmParameterGeneratorSpi (java.security.AlgorithmParameterGeneratorSpi)1 InvalidKeyException (java.security.InvalidKeyException)1 InvalidParameterException (java.security.InvalidParameterException)1 Key (java.security.Key)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 Provider (java.security.Provider)1 DSAGenParameterSpec (java.security.spec.DSAGenParameterSpec)1 SecretKey (javax.crypto.SecretKey)1 DHGenParameterSpec (javax.crypto.spec.DHGenParameterSpec)1 MyAlgorithmParameterGeneratorSpi (org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi)1