0N/A/*
4910N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
4910N/A * @bug 4942494 7146728
0N/A * @summary KAT test for DH (normal and with secret that has leading a 0x00 byte)
0N/A * @author Andreas Sterbenz
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.math.BigInteger;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/A
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Apublic class TestShort extends PKCS11Test {
0N/A
0N/A private final static BigInteger p = new BigInteger
0N/A ("132323768951986124075479307182674357577285270296234088722451560397577130"
0N/A + "29036368719146452186041204237350521785240337048752071462798273003935646"
0N/A + "236777459223");
0N/A
0N/A private final static BigInteger g = new BigInteger
0N/A ("542164405743647514160964848832570512804742839438047437683466730076610826"
0N/A + "26139005426812890807137245973106730741193551360857959820973906708903671"
0N/A + "85141189796");
0N/A
0N/A private final static BigInteger y1 = new BigInteger
0N/A ("917822587297202019713917824657175324360828836418754472207798053179332700"
0N/A + "39938196470323405362414543604756313574842317687108720161868374135893507"
0N/A + "32549013008");
0N/A
0N/A private final static BigInteger x1 = new BigInteger
0N/A ("44680539865608058021525420137770558786664900449");
0N/A
0N/A private final static BigInteger y2 = new BigInteger
0N/A ("971516093764754129400636279042779828227876735997548759620533874940954728"
0N/A + "96003923584532197641582422156725687657451980378160229472095259392582713"
0N/A + "54693857368");
0N/A
0N/A private final static BigInteger x2 = new BigInteger
0N/A ("433011588852527167500079509018272713204454720683");
0N/A
0N/A private final static byte[] s2 = parse
4910N/A ("00:19:c7:f1:bb:2e:3d:93:fa:02:d2:e9:9f:75:32:b9:e6:7a:a0:4a:10:45:81:d4:2b:"
0N/A + "e2:77:4c:70:41:39:7c:19:fa:65:64:47:49:8a:ad:0a:fa:9d:e9:62:68:97:c5:52"
0N/A + ":b1:37:03:d9:cd:aa:e1:bd:7e:71:0c:fc:15:a1:95");
0N/A
0N/A private final static BigInteger y3 = new BigInteger
0N/A ("487191942830952492045314176949691887949505843590154039270855000076570641"
0N/A + "84133173374554778014985281423493547105556633876312739488944445812738030"
0N/A + "00691614787");
0N/A
0N/A private final static BigInteger x3 = new BigInteger
0N/A ("1105612503769813327556221318510360767544481637404");
0N/A
0N/A private final static byte[] s3 = parse
0N/A ("98:62:f3:e4:ff:2b:8d:8a:5a:20:fe:52:35:56:73:09:8e:b3:e2:cb:e2:45:e5:b7:"
0N/A + "1a:6a:15:d8:a4:8c:0a:ce:f0:15:03:0c:c2:56:82:a2:75:9b:49:fe:ed:60:c5:6e"
0N/A + ":de:47:55:62:4f:16:20:6d:74:cc:7b:95:93:25:2c:ea");
0N/A
0N/A public void main(Provider provider) throws Exception {
0N/A if (provider.getService("KeyAgreement", "DH") == null) {
0N/A System.out.println("DH not supported, skipping");
0N/A return;
0N/A }
4910N/A try {
4910N/A DHPublicKeySpec publicSpec;
4910N/A DHPrivateKeySpec privateSpec;
4910N/A KeyFactory kf = KeyFactory.getInstance("DH", provider);
4910N/A KeyAgreement ka = KeyAgreement.getInstance("DH", provider);
0N/A
4910N/A PrivateKey pr1 = kf.generatePrivate(new DHPrivateKeySpec(x1, p, g));
4910N/A PublicKey pu2 = kf.generatePublic(new DHPublicKeySpec(y2, p, g));
4910N/A PublicKey pu3 = kf.generatePublic(new DHPublicKeySpec(y3, p, g));
0N/A
4910N/A ka.init(pr1);
4910N/A ka.doPhase(pu2, true);
4910N/A byte[] n2 = ka.generateSecret();
4910N/A if (Arrays.equals(s2, n2) == false) {
4910N/A throw new Exception("mismatch 2");
4910N/A }
4910N/A System.out.println("short ok");
0N/A
4910N/A ka.init(pr1);
4910N/A ka.doPhase(pu3, true);
4910N/A byte[] n3 = ka.generateSecret();
4910N/A if (Arrays.equals(s3, n3) == false) {
4910N/A throw new Exception("mismatch 3");
4910N/A }
4910N/A System.out.println("normal ok");
4910N/A } catch (Exception ex) {
4910N/A System.out.println("Unexpected Exception: " + ex);
4910N/A ex.printStackTrace();
4910N/A throw ex;
0N/A }
0N/A
0N/A/*
0N/A KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", provider);
0N/A kpg.initialize(512);
0N/A// KeyPair kp1 = kpg.generateKeyPair();
0N/A// System.out.println(kp1.getPublic());
0N/A// System.out.println(kp1.getPrivate());
0N/A while (true) {
0N/A KeyAgreement ka = KeyAgreement.getInstance("DH", provider);
0N/A ka.init(pr1);
0N/A KeyPair kp2 = kpg.generateKeyPair();
0N/A ka.doPhase(kp2.getPublic(), true);
0N/A byte[] sec = ka.generateSecret();
0N/A if (sec.length == 64) {
0N/A System.out.println(kp2.getPrivate());
0N/A System.out.println(kp2.getPublic());
0N/A System.out.println(toString(sec));
0N/A break;
0N/A }
0N/A }
0N/A/**/
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A main(new TestShort());
0N/A }
0N/A
0N/A}