jdk/test/sun/security/pkcs11/ec/TestECDSA.java
changeset 28974 71fe221460f5
parent 19067 5271291b7121
child 30046 cf2c86e1819e
equal deleted inserted replaced
28973:86da37969769 28974:71fe221460f5
     1 /*
     1 /*
     2  * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 6405536
    26  * @bug 6405536 8042967
    27  * @summary basic test of SHA1withECDSA and NONEwithECDSA signing/verifying
    27  * @summary basic test of SHA1withECDSA and NONEwithECDSA signing/verifying
    28  * @author Andreas Sterbenz
    28  * @author Andreas Sterbenz
    29  * @library ..
    29  * @library ..
    30  * @library ../../../../java/security/testlibrary
    30  * @library ../../../../java/security/testlibrary
    31  */
    31  */
   174         verify(provider, "SHA1withECDSA", publicKey, data2Raw, sig, false);
   174         verify(provider, "SHA1withECDSA", publicKey, data2Raw, sig, false);
   175 
   175 
   176         verify(provider, "NONEwithECDSA", publicKey, data1SHA, sig, true);
   176         verify(provider, "NONEwithECDSA", publicKey, data1SHA, sig, true);
   177         verify(provider, "NONEwithECDSA", publicKey, data2SHA, sig, false);
   177         verify(provider, "NONEwithECDSA", publicKey, data2SHA, sig, false);
   178 
   178 
   179         testSigning(provider, privateKey, publicKey);
   179         System.out.println("Testing with default signature format: ASN.1");
   180     }
   180         testSigning(provider, privateKey, publicKey, false);
   181 
   181 
   182     private void testSigning(Provider provider, PrivateKey privateKey,
   182         System.out.println("Testing with IEEE P1363 signature format");
   183             PublicKey publicKey) throws Exception {
   183         testSigning(provider, privateKey, publicKey, true);
       
   184     }
       
   185 
       
   186     private void testSigning(Provider provider,
       
   187                              PrivateKey privateKey,
       
   188                              PublicKey publicKey,
       
   189                              boolean p1363Format) throws Exception {
   184         byte[] data = new byte[2048];
   190         byte[] data = new byte[2048];
   185         new Random().nextBytes(data);
   191         new Random().nextBytes(data);
   186 
   192 
   187         // sign random data using SHA1withECDSA and verify using
   193         // sign random data using SHA1withECDSA and verify using
   188         // SHA1withECDSA and NONEwithECDSA
   194         // SHA1withECDSA and NONEwithECDSA
   189         Signature s = Signature.getInstance("SHA1withECDSA", provider);
   195         Signature s;
       
   196         if (p1363Format) {
       
   197             s = Signature.getInstance("SHA1withECDSAinP1363Format", provider);
       
   198         } else {
       
   199             s = Signature.getInstance("SHA1withECDSA", provider);
       
   200         }
   190         s.initSign(privateKey);
   201         s.initSign(privateKey);
   191         s.update(data);
   202         s.update(data);
   192         byte[] s1 = s.sign();
   203         byte[] s1 = s.sign();
   193 
   204 
   194         s.initVerify(publicKey);
   205         s.initVerify(publicKey);
   195         s.update(data);
   206         s.update(data);
   196         if (!s.verify(s1)) {
   207         if (!s.verify(s1)) {
   197             throw new Exception("Sign/verify 1 failed");
   208             throw new Exception("Sign/verify 1 failed");
   198         }
   209         }
   199 
   210 
   200         s = Signature.getInstance("NONEwithECDSA", provider);
   211         if (p1363Format) {
       
   212             s = Signature.getInstance("NONEwithECDSAinP1363Format", provider);
       
   213         } else {
       
   214             s = Signature.getInstance("NONEwithECDSA", provider);
       
   215         }
   201         MessageDigest md = MessageDigest.getInstance("SHA-1");
   216         MessageDigest md = MessageDigest.getInstance("SHA-1");
   202         byte[] digest = md.digest(data);
   217         byte[] digest = md.digest(data);
   203         s.initVerify(publicKey);
   218         s.initVerify(publicKey);
   204         s.update(digest);
   219         s.update(digest);
   205         if (!s.verify(s1)) {
   220         if (!s.verify(s1)) {
   216         s.update(digest);
   231         s.update(digest);
   217         if (!s.verify(s2)) {
   232         if (!s.verify(s2)) {
   218             throw new Exception("Sign/verify 3 failed");
   233             throw new Exception("Sign/verify 3 failed");
   219         }
   234         }
   220 
   235 
   221         s = Signature.getInstance("SHA1withECDSA", provider);
   236         if (p1363Format) {
       
   237             s = Signature.getInstance("SHA1withECDSAinP1363Format", provider);
       
   238         } else {
       
   239             s = Signature.getInstance("SHA1withECDSA", provider);
       
   240         }
   222         s.initVerify(publicKey);
   241         s.initVerify(publicKey);
   223         s.update(data);
   242         s.update(data);
   224         if (!s.verify(s2)) {
   243         if (!s.verify(s2)) {
   225             throw new Exception("Sign/verify 4 failed");
   244             throw new Exception("Sign/verify 4 failed");
   226         }
   245         }