alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (TestECDH2.java)

This example Java source code file (TestECDH2.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

biginteger, crypto, ecdh, ecgenparameterspec, ecparameterspec, ecpoint, ecprivatekeyspec, ecpublickeyspec, exception, keyagreement, keypair, math, pkcs11test, provider, security, string, testecdh2, util

The TestECDH2.java Java example source code

/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 6405536
 * @summary basic test of ECDSA signatures for P-256 and P-384 from the
 * example data in "Suite B Implementer's Guide to FIPS 186-3".
 * @library ..
 * @library ../../../../java/security/testlibrary
 * @compile -XDignore.symbol.file TestECDH2.java
 * @run main TestECDH2
 */

import java.io.*;
import java.util.*;
import java.math.BigInteger;

import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;

import sun.security.util.ECUtil;

public class TestECDH2 extends PKCS11Test {

    // values of the keys we use for the tests

    // keypair using NIST P-256
    private final static String privD256 = "70a12c2db16845ed56ff68cfc21a472b3f04d7d6851bf6349f2d7d5b3452b38a";
    private final static String pubX256 = "8101ece47464a6ead70cf69a6e2bd3d88691a3262d22cba4f7635eaff26680a8";
    private final static String pubY256 = "d8a12ba61d599235f67d9cb4d58f1783d3ca43e78f0a5abaa624079936c0c3a9";

    // keypair using NIST P-384
    private final static String privD384 = "c838b85253ef8dc7394fa5808a5183981c7deef5a69ba8f4f2117ffea39cfcd90e95f6cbc854abacab701d50c1f3cf24";
    private final static String pubX384 = "1fbac8eebd0cbf35640b39efe0808dd774debff20a2a329e91713baf7d7f3c3e81546d883730bee7e48678f857b02ca0";
    private final static String pubY384 = "eb213103bd68ce343365a8a4c3d4555fa385f5330203bdd76ffad1f3affb95751c132007e1b240353cb0a4cf1693bdf9";

    private KeyFactory kf = null;
    private KeyPairGenerator kpg = null;

    private static void testKeyAgreement(KeyPair kpA, KeyPair kpB, Provider p)
        throws Exception {
        KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p);
        ka1.init(kpA.getPrivate());
        ka1.doPhase(kpB.getPublic(), true);
        byte[] s1 = ka1.generateSecret();

        KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p);
        ka2.init(kpB.getPrivate());
        ka2.doPhase(kpA.getPublic(), true);
        byte[] s2 = ka2.generateSecret();
        if (Arrays.equals(s1, s2) == false) {
            System.out.println("expected: " + toString(s1));
            System.out.println("actual:   " + toString(s2));
            throw new Exception("Generated secrets do not match");
        }
    }

    private KeyPair genECKeyPair(String curvName, String privD, String pubX,
                                 String pubY, Provider p) throws Exception {
        ECParameterSpec ecParams = ECUtil.getECParameterSpec(p, curvName);
        ECPrivateKeySpec privKeySpec =
            new ECPrivateKeySpec(new BigInteger(privD, 16), ecParams);
        ECPublicKeySpec pubKeySpec =
            new ECPublicKeySpec(new ECPoint(new BigInteger(pubX, 16),
                                            new BigInteger(pubY, 16)),
                                ecParams);
        PrivateKey privKey = kf.generatePrivate(privKeySpec);
        PublicKey pubKey = kf.generatePublic(pubKeySpec);
        return new KeyPair(pubKey, privKey);
    }
    private KeyPair genECKeyPair(String curvName) throws Exception {
        ECGenParameterSpec genParams = new ECGenParameterSpec(curvName);
        kpg.initialize(genParams, null);
        return kpg.generateKeyPair();
    }
    public static void main(String[] args) throws Exception {
        main(new TestECDH2());
    }

    public void main(Provider provider) throws Exception {
        if (provider.getService("KeyAgreement", "ECDH") == null) {
            System.out.println("ECDH not supported, skipping");
            return;
        }

        if (isNSS(provider) && getNSSVersion() >= 3.11 &&
                getNSSVersion() < 3.12) {
            System.out.println("NSS 3.11 has a DER issue that recent " +
                    "version do not.");
            return;
        }

        kf = KeyFactory.getInstance("EC", provider);
        kpg = KeyPairGenerator.getInstance("EC", provider);

        System.out.println("Testing against NIST P-256");

        long start = System.currentTimeMillis();
        KeyPair kp256A =
            genECKeyPair("secp256r1", privD256, pubX256, pubY256, provider);
        KeyPair kp256B = genECKeyPair("secp256r1");
        testKeyAgreement(kp256A, kp256B, provider);

        System.out.println("Testing against NIST P-384");
        KeyPair kp384A =
            genECKeyPair("secp384r1", privD384, pubX384, pubY384, provider);
        KeyPair kp384B = genECKeyPair("secp384r1");
        testKeyAgreement(kp384A, kp384B, provider);

        long stop = System.currentTimeMillis();
        System.out.println("All tests passed (" + (stop - start) + " ms).");
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java TestECDH2.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.