blob: 70bb723eb97165fd1eaad002ec964843ed98ea08 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 4942494
27 * @summary KAT test for DH (normal and with secret that has leading a 0x00 byte)
28 * @author Andreas Sterbenz
29 * @library ..
30 */
31
32import java.math.BigInteger;
33import java.util.*;
34
35import java.security.*;
36
37import javax.crypto.*;
38import javax.crypto.spec.*;
39
40public class TestShort extends PKCS11Test {
41
42 private final static BigInteger p = new BigInteger
43 ("132323768951986124075479307182674357577285270296234088722451560397577130"
44 + "29036368719146452186041204237350521785240337048752071462798273003935646"
45 + "236777459223");
46
47 private final static BigInteger g = new BigInteger
48 ("542164405743647514160964848832570512804742839438047437683466730076610826"
49 + "26139005426812890807137245973106730741193551360857959820973906708903671"
50 + "85141189796");
51
52 private final static BigInteger y1 = new BigInteger
53 ("917822587297202019713917824657175324360828836418754472207798053179332700"
54 + "39938196470323405362414543604756313574842317687108720161868374135893507"
55 + "32549013008");
56
57 private final static BigInteger x1 = new BigInteger
58 ("44680539865608058021525420137770558786664900449");
59
60 private final static BigInteger y2 = new BigInteger
61 ("971516093764754129400636279042779828227876735997548759620533874940954728"
62 + "96003923584532197641582422156725687657451980378160229472095259392582713"
63 + "54693857368");
64
65 private final static BigInteger x2 = new BigInteger
66 ("433011588852527167500079509018272713204454720683");
67
68 private final static byte[] s2 = parse
69 ("19:c7:f1:bb:2e:3d:93:fa:02:d2:e9:9f:75:32:b9:e6:7a:a0:4a:10:45:81:d4:2b:"
70 + "e2:77:4c:70:41:39:7c:19:fa:65:64:47:49:8a:ad:0a:fa:9d:e9:62:68:97:c5:52"
71 + ":b1:37:03:d9:cd:aa:e1:bd:7e:71:0c:fc:15:a1:95");
72
73 private final static BigInteger y3 = new BigInteger
74 ("487191942830952492045314176949691887949505843590154039270855000076570641"
75 + "84133173374554778014985281423493547105556633876312739488944445812738030"
76 + "00691614787");
77
78 private final static BigInteger x3 = new BigInteger
79 ("1105612503769813327556221318510360767544481637404");
80
81 private final static byte[] s3 = parse
82 ("98:62:f3:e4:ff:2b:8d:8a:5a:20:fe:52:35:56:73:09:8e:b3:e2:cb:e2:45:e5:b7:"
83 + "1a:6a:15:d8:a4:8c:0a:ce:f0:15:03:0c:c2:56:82:a2:75:9b:49:fe:ed:60:c5:6e"
84 + ":de:47:55:62:4f:16:20:6d:74:cc:7b:95:93:25:2c:ea");
85
86 public void main(Provider provider) throws Exception {
87 if (provider.getService("KeyAgreement", "DH") == null) {
88 System.out.println("DH not supported, skipping");
89 return;
90 }
91 DHPublicKeySpec publicSpec;
92 DHPrivateKeySpec privateSpec;
93 KeyFactory kf = KeyFactory.getInstance("DH", provider);
94 KeyAgreement ka = KeyAgreement.getInstance("DH", provider);
95// KeyAgreement ka = KeyAgreement.getInstance("DH");
96
97 PrivateKey pr1 = kf.generatePrivate(new DHPrivateKeySpec(x1, p, g));
98 PublicKey pu2 = kf.generatePublic(new DHPublicKeySpec(y2, p, g));
99 PublicKey pu3 = kf.generatePublic(new DHPublicKeySpec(y3, p, g));
100
101 ka.init(pr1);
102 ka.doPhase(pu2, true);
103 byte[] n2 = ka.generateSecret();
104 if (Arrays.equals(s2, n2) == false) {
105 throw new Exception("mismatch 2");
106 }
107 System.out.println("short ok");
108
109 ka.init(pr1);
110 ka.doPhase(pu3, true);
111 byte[] n3 = ka.generateSecret();
112 if (Arrays.equals(s3, n3) == false) {
113 throw new Exception("mismatch 3");
114 }
115 System.out.println("normal ok");
116
117/*
118 KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", provider);
119 kpg.initialize(512);
120// KeyPair kp1 = kpg.generateKeyPair();
121// System.out.println(kp1.getPublic());
122// System.out.println(kp1.getPrivate());
123 while (true) {
124 KeyAgreement ka = KeyAgreement.getInstance("DH", provider);
125 ka.init(pr1);
126 KeyPair kp2 = kpg.generateKeyPair();
127 ka.doPhase(kp2.getPublic(), true);
128 byte[] sec = ka.generateSecret();
129 if (sec.length == 64) {
130 System.out.println(kp2.getPrivate());
131 System.out.println(kp2.getPublic());
132 System.out.println(toString(sec));
133 break;
134 }
135 }
136/**/
137 }
138
139 public static void main(String[] args) throws Exception {
140 main(new TestShort());
141 }
142
143}