1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vera Y. Petrashkova
20* @version $Revision$
21*/
22
23package org.apache.harmony.crypto.tests.javax.crypto.interfaces;
24
25import java.math.BigInteger;
26import java.security.KeyPair;
27import java.security.KeyPairGenerator;
28import java.security.SecureRandom;
29
30import javax.crypto.KeyGenerator;
31import javax.crypto.interfaces.DHKey;
32import javax.crypto.interfaces.DHPrivateKey;
33import javax.crypto.spec.DHParameterSpec;
34
35import junit.framework.TestCase;
36
37/**
38 * Tests for <code>DHPrivateKey</code> class field
39 *
40 */
41public class DHPrivateKeyTest extends TestCase {
42
43    /**
44     * Test for <code>serialVersionUID</code> field
45     */
46    public void testField() {
47        checkDHPrivateKey key = new checkDHPrivateKey();
48        assertEquals("Incorrect serialVersionUID",
49                key.getSerVerUID(), //DHPrivateKey.serialVersionUID
50                2211791113380396553L);
51    }
52
53    public void test_getParams_initToHardCoded() throws Exception {
54        // (p, g) values from RFC 7919, Appendix A (2048-bit group)
55        String pStr = "FFFFFFFFFFFFFFFFADF85458A2BB4A9AAFDC5620273D3CF1" +
56                "D8B9C583CE2D3695A9E13641146433FBCC939DCE249B3EF9" +
57                "7D2FE363630C75D8F681B202AEC4617AD3DF1ED5D5FD6561" +
58                "2433F51F5F066ED0856365553DED1AF3B557135E7F57C935" +
59                "984F0C70E0E68B77E2A689DAF3EFE8721DF158A136ADE735" +
60                "30ACCA4F483A797ABC0AB182B324FB61D108A94BB2C8E3FB" +
61                "B96ADAB760D7F4681D4F42A3DE394DF4AE56EDE76372BB19" +
62                "0B07A7C8EE0A6D709E02FCE1CDF7E2ECC03404CD28342F61" +
63                "9172FE9CE98583FF8E4F1232EEF28183C3FE3B1B4C6FAD73" +
64                "3BB5FCBC2EC22005C58EF1837D1683B2C6F34A26C1B2EFFA" +
65                "886B423861285C97FFFFFFFFFFFFFFFF";
66        BigInteger p = new BigInteger(new BigInteger(pStr, 16).toByteArray());
67        BigInteger g = BigInteger.valueOf(2);
68        KeyPairGenerator kg = KeyPairGenerator.getInstance("DH");
69        kg.initialize(new DHParameterSpec(p, g), new SecureRandom());
70        check_getParams(kg);
71    }
72
73    public void test_getParams_initToRandom192bit() throws Exception {
74        KeyPairGenerator kg = KeyPairGenerator.getInstance("DH");
75        // DH group generation is slow, so we test with a small (insecure) value
76        kg.initialize(192);
77        check_getParams(kg);
78    }
79
80    private static void check_getParams(KeyPairGenerator kg) throws Exception {
81        KeyPair kp1 = kg.genKeyPair();
82        KeyPair kp2 = kg.genKeyPair();
83        DHPrivateKey pk1 = (DHPrivateKey) kp1.getPrivate();
84        DHPrivateKey pk2 = (DHPrivateKey) kp2.getPrivate();
85
86        assertTrue(pk1.getX().getClass().getCanonicalName().equals("java.math.BigInteger"));
87        assertTrue(pk1.getParams().getClass().getCanonicalName().equals("javax.crypto.spec.DHParameterSpec"));
88        assertFalse(pk1.equals(pk2));
89        assertTrue(pk1.getX().equals(pk1.getX()));
90    }
91
92    public class checkDHPrivateKey implements DHPrivateKey {
93        public String getAlgorithm() {
94            return "SecretKey";
95        }
96        public String getFormat() {
97            return "Format";
98        }
99        public byte[] getEncoded() {
100            return new byte[0];
101        }
102        public long getSerVerUID() {
103            return serialVersionUID;
104        }
105        public BigInteger getX() {
106            return null;
107        }
108        public DHParameterSpec getParams() {
109            return null;
110        }
111    }
112}
113