1 /* 2 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6405536 6414980 27 * @summary Basic consistency test for all curves using ECDSA and ECDH 28 * @author Andreas Sterbenz 29 * @library .. 30 * @compile -XDignore.symbol.file TestCurves.java 31 * @run main TestCurves 32 * @key randomness 33 */ 34 35 import java.util.*; 36 37 import java.security.*; 38 import java.security.spec.*; 39 40 import javax.crypto.*; 41 42 public class TestCurves extends PKCS11Test { 43 44 public static void main(String[] args) throws Exception { 45 main(new TestCurves()); 46 } 47 48 public void main(Provider p) throws Exception { 49 if (p.getService("KeyAgreement", "ECDH") == null) { 50 System.out.println("Not supported by provider, skipping"); 51 return; 52 } 53 54 if (isNSS(p) && getNSSVersion() >= 3.11 && getNSSVersion() < 3.12) { 55 System.out.println("NSS 3.11 has a DER issue that recent " + 56 "version do not."); 57 return; 58 } 59 60 // Check if this is sparc for later failure avoidance. 61 boolean sparc = false; 62 if (System.getProperty("os.arch").equals("sparcv9")) { 63 sparc = true; 64 System.out.println("This is a sparcv9"); 65 } 66 67 Random random = new Random(); 68 byte[] data = new byte[2048]; 69 random.nextBytes(data); 70 71 Vector<ECParameterSpec> curves = getKnownCurves(p); 72 for (ECParameterSpec params : curves) { 73 System.out.println("Testing " + params + "..."); 74 KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p); 75 kpg.initialize(params); 76 KeyPair kp1, kp2; 77 78 kp1 = kpg.generateKeyPair(); 79 kp2 = kpg.generateKeyPair(); 80 81 testSigning(p, "SHA1withECDSA", data, kp1, kp2); 82 // Check because Solaris ncp driver does not support these but 83 // Solaris metaslot causes them to be run. 84 try { 85 testSigning(p, "SHA224withECDSA", data, kp1, kp2); 86 testSigning(p, "SHA256withECDSA", data, kp1, kp2); 87 testSigning(p, "SHA384withECDSA", data, kp1, kp2); 88 testSigning(p, "SHA512withECDSA", data, kp1, kp2); 89 } catch (ProviderException e) { 90 if (sparc) { 91 Throwable t = e.getCause(); 92 if (t instanceof sun.security.pkcs11.wrapper.PKCS11Exception && 93 t.getMessage().equals("CKR_ATTRIBUTE_VALUE_INVALID")) { 94 System.out.print("-Failure not uncommon. Probably pre-T4."); 95 } else { 96 throw e; 97 } 98 } else { 99 throw e; 100 } 101 } 102 System.out.println(); 103 104 KeyAgreement ka1 = KeyAgreement.getInstance("ECDH", p); 105 ka1.init(kp1.getPrivate()); 106 ka1.doPhase(kp2.getPublic(), true); 107 byte[] secret1 = ka1.generateSecret(); 108 109 KeyAgreement ka2 = KeyAgreement.getInstance("ECDH", p); 110 ka2.init(kp2.getPrivate()); 111 ka2.doPhase(kp1.getPublic(), true); 112 byte[] secret2 = ka2.generateSecret(); 113 114 if (Arrays.equals(secret1, secret2) == false) { 115 throw new Exception("Secrets do not match"); 116 } 117 } 118 119 System.out.println("OK"); 120 } 121 122 private static void testSigning(Provider p, String algorithm, 123 byte[] data, KeyPair kp1, KeyPair kp2) throws Exception { 124 System.out.print(" " + algorithm); 125 Signature s = Signature.getInstance(algorithm, p); 126 s.initSign(kp1.getPrivate()); 127 s.update(data); 128 byte[] sig = s.sign(); 129 130 s = Signature.getInstance(algorithm, p); 131 s.initVerify(kp1.getPublic()); 132 s.update(data); 133 boolean r = s.verify(sig); 134 if (r == false) { 135 throw new Exception("Signature did not verify"); 136 } 137 138 s.initVerify(kp2.getPublic()); 139 s.update(data); 140 r = s.verify(sig); 141 if (r) { 142 throw new Exception("Signature should not verify"); 143 } 144 } 145 }