1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.apache.harmony.crypto.tests.javax.crypto.func;
17
18import com.android.org.bouncycastle.util.Arrays;
19import java.security.AlgorithmParameterGenerator;
20import java.security.AlgorithmParameters;
21import java.security.KeyFactory;
22import java.security.KeyPair;
23import java.security.KeyPairGenerator;
24import java.security.PrivateKey;
25import java.security.PublicKey;
26import java.security.SecureRandom;
27import java.security.spec.X509EncodedKeySpec;
28import javax.crypto.KeyAgreement;
29import javax.crypto.spec.DHParameterSpec;
30
31public class KeyAgreementThread extends TestThread {
32    class KeyAgreementGen {
33        private PrivateKey privateKey = null;
34        private byte[] publicKeyBytes = null;
35
36        KeyAgreementGen(DHParameterSpec parameterSpec)
37                throws Exception {
38            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
39            keyGen.initialize(parameterSpec);
40            KeyPair keypair = keyGen.generateKeyPair();
41
42            privateKey     = keypair.getPrivate();
43            publicKeyBytes = keypair.getPublic().getEncoded();
44        }
45
46        public byte[] getPublicKeyBytes () {
47            return publicKeyBytes;
48        }
49
50        public byte[] getSecretKey(String alg, byte[] publicKey) throws Exception {
51            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
52            KeyFactory keyFact = KeyFactory.getInstance("DH");
53            PublicKey pubKey = keyFact.generatePublic(x509KeySpec);
54
55            KeyAgreement ka = KeyAgreement.getInstance("DH");
56            ka.init(privateKey);
57            ka.doPhase(pubKey, true);
58
59            return ka.generateSecret();
60        }
61    }
62
63    public KeyAgreementThread(String[] names) {
64        super(names);
65    }
66
67    @Override
68    public void test() throws Exception {
69        AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH");
70        apg.init(1024, new SecureRandom());
71        AlgorithmParameters ap = apg.generateParameters();
72        DHParameterSpec ps = ap.getParameterSpec(DHParameterSpec.class);
73
74        KeyAgreementGen kag1 = new KeyAgreementGen(ps);
75        KeyAgreementGen kag2 = new KeyAgreementGen(ps);
76
77        byte[] bArray1 = kag1.getPublicKeyBytes();
78        byte[] bArray2 = kag2.getPublicKeyBytes();
79
80        byte[] sk1 = kag1.getSecretKey(algName, bArray2);
81        byte[] sk2 = kag2.getSecretKey(algName, bArray1);
82
83        if (Arrays.areEqual(sk1, sk2) == false) {
84            throw new Exception ("Generated keys are not the same");
85        }
86    }
87}
88