Search in sources :

Example 1 with DHGenParameterSpec

use of javax.crypto.spec.DHGenParameterSpec in project robovm by robovm.

the class DHGenParameterSpecTest method testDHGenParameterSpec.

/**
     * DHGenParameterSpec class testing. Tests the equivalence of
     * parameters specified in the constructor with the values returned
     * by getters.
     */
public void testDHGenParameterSpec() {
    int[] primes = { Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE };
    int[] exponents = { Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE };
    for (int i = 0; i < primes.length; i++) {
        DHGenParameterSpec ps = new DHGenParameterSpec(primes[i], exponents[i]);
        assertEquals("The value returned by getPrimeSize() must be " + "equal to the value specified in the constructor", ps.getPrimeSize(), primes[i]);
        assertEquals("The value returned by getExponentSize() must be " + "equal to the value specified in the constructor", ps.getPrimeSize(), exponents[i]);
    }
}
Also used : DHGenParameterSpec(javax.crypto.spec.DHGenParameterSpec)

Example 2 with DHGenParameterSpec

use of javax.crypto.spec.DHGenParameterSpec 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 3 with DHGenParameterSpec

use of javax.crypto.spec.DHGenParameterSpec in project jdk8u_jdk by JetBrains.

the class DHParameterGenerator method engineInit.

/**
     * Initializes this parameter generator with a set of parameter
     * generation values, which specify the size of the prime modulus and
     * the size of the random exponent, both in bits.
     *
     * @param params the set of parameter generation values
     * @param random the source of randomness
     *
     * @exception InvalidAlgorithmParameterException if the given parameter
     * generation values are inappropriate for this parameter generator
     */
protected void engineInit(AlgorithmParameterSpec genParamSpec, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(genParamSpec instanceof DHGenParameterSpec)) {
        throw new InvalidAlgorithmParameterException("Inappropriate parameter type");
    }
    DHGenParameterSpec dhParamSpec = (DHGenParameterSpec) genParamSpec;
    primeSize = dhParamSpec.getPrimeSize();
    // Re-uses DSA parameters and thus have the same range
    try {
        checkKeySize(primeSize);
    } catch (InvalidParameterException ipe) {
        throw new InvalidAlgorithmParameterException(ipe.getMessage());
    }
    exponentSize = dhParamSpec.getExponentSize();
    if (exponentSize <= 0) {
        throw new InvalidAlgorithmParameterException("Exponent size must be greater than zero");
    }
    // Require exponentSize < primeSize
    if (exponentSize >= primeSize) {
        throw new InvalidAlgorithmParameterException("Exponent size must be less than modulus size");
    }
}
Also used : DHGenParameterSpec(javax.crypto.spec.DHGenParameterSpec)

Example 4 with DHGenParameterSpec

use of javax.crypto.spec.DHGenParameterSpec in project robovm by robovm.

the class AlgorithmParameterGeneratorSpi method engineInit.

protected void engineInit(AlgorithmParameterSpec genParamSpec, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(genParamSpec instanceof DHGenParameterSpec)) {
        throw new InvalidAlgorithmParameterException("DH parameter generator requires a DHGenParameterSpec for initialisation");
    }
    DHGenParameterSpec spec = (DHGenParameterSpec) genParamSpec;
    this.strength = spec.getPrimeSize();
    this.l = spec.getExponentSize();
    this.random = random;
}
Also used : DHGenParameterSpec(javax.crypto.spec.DHGenParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException)

Example 5 with DHGenParameterSpec

use of javax.crypto.spec.DHGenParameterSpec in project robovm by robovm.

the class myMac method testInit.

/**
     * Test for
     * <code>init(Key key, AlgorithmParameterSpec params)</code>
     * <code>init(Key key)</code>
     * methods
     * Assertion: throws InvalidKeyException and InvalidAlgorithmParameterException
     * when parameters are not appropriate
     */
public void testInit() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, IllegalStateException, InvalidAlgorithmParameterException, InvalidKeyException {
    if (!DEFSupported) {
        fail(NotSupportedMsg);
        return;
    }
    Mac[] macs = createMacs();
    assertNotNull("Mac objects were not created", macs);
    byte[] b = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
    SecretKeySpec sks = new SecretKeySpec(b, "SHA1");
    DHGenParameterSpec algPS = new DHGenParameterSpec(1, 2);
    PSSParameterSpec algPSS = new PSSParameterSpec(20);
    SecretKeySpec sks1 = new SecretKeySpec(b, "RSA");
    for (int i = 0; i < macs.length; i++) {
        macs[i].init(sks);
        try {
            macs[i].init(sks1, algPSS);
            fail("init(..) accepts incorrect AlgorithmParameterSpec parameter");
        } catch (InvalidAlgorithmParameterException e) {
        }
        try {
            macs[i].init(sks, algPS);
            fail("init(..) accepts incorrect AlgorithmParameterSpec parameter");
        } catch (InvalidAlgorithmParameterException e) {
        }
        try {
            macs[i].init(null, null);
            fail("InvalidKeyException must be thrown");
        } catch (InvalidKeyException e) {
        }
        try {
            macs[i].init(null);
            fail("InvalidKeyException must be thrown");
        } catch (InvalidKeyException e) {
        }
    //            macs[i].init(sks, null);
    }
}
Also used : DHGenParameterSpec(javax.crypto.spec.DHGenParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) PSSParameterSpec(java.security.spec.PSSParameterSpec) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Aggregations

DHGenParameterSpec (javax.crypto.spec.DHGenParameterSpec)5 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 InvalidKeyException (java.security.InvalidKeyException)2 AlgorithmParameterGenerator (java.security.AlgorithmParameterGenerator)1 Key (java.security.Key)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1 PSSParameterSpec (java.security.spec.PSSParameterSpec)1 Mac (javax.crypto.Mac)1 SecretKey (javax.crypto.SecretKey)1 DHParameterSpec (javax.crypto.spec.DHParameterSpec)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1