view test/standalone/TestECDSA.java @ 2880:0c5b97b56309

PR2900: Don't use WithSeed versions of NSS functions as they don't fully process the seed 2016-04-23 Andrew John Hughes <gnu_andrew@member.fsf.org> PR2900: Don't use WithSeed versions of NSS functions as they don't fully process the seed * Makefile.am: (ECC_CHECK_SRCS): Add TestECDSA.java (ecccheck): Only compile tests if SunEC is enabled. (clean-ecccheck): Only remove build directory if SunEC is enabled. (check-ecc): Only run tests if SunEC is enabled. Add running of TestECDSA. Call set -e so rule fails if any test fails. * test/standalone/TestECDSA.java: New test to make sure SunEC can produce ECDSA signatures.
author Andrew John Hughes <gnu_andrew@member.fsf.org>
date Thu, 28 Jul 2016 03:55:52 +0100
parents
children
line wrap: on
line source

/* TestECDSA -- Ensure ECDSA signatures are working.
   Copyright (C) 2016 Red Hat, Inc.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;

/**
 * @test
 */
public class TestECDSA {

    public static void main(String[] args) throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
        KeyPair key = keyGen.generateKeyPair();
        
        byte[] data = "This is a string to sign".getBytes("UTF-8");
        
        Signature dsa = Signature.getInstance("NONEwithECDSA");
        dsa.initSign(key.getPrivate());
        dsa.update(data);
        byte[] sig = dsa.sign();
        System.out.println("Signature: " + new BigInteger(1, sig).toString(16));
        
        Signature dsaCheck = Signature.getInstance("NONEwithECDSA");
        dsaCheck.initVerify(key.getPublic());
        dsaCheck.update(data);
        boolean success = dsaCheck.verify(sig);
        if (!success) {
            throw new RuntimeException("Test failed. Signature verification error");
        }
        System.out.println("Test passed.");
    }
}