1 /* |
1 /* |
2 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2006, 2016, 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. |
26 * @bug 6405536 |
26 * @bug 6405536 |
27 * @summary Basic known answer test for ECDH |
27 * @summary Basic known answer test for ECDH |
28 * @author Andreas Sterbenz |
28 * @author Andreas Sterbenz |
29 * @library .. |
29 * @library .. |
30 * @library ../../../../java/security/testlibrary |
30 * @library ../../../../java/security/testlibrary |
|
31 * @run main/othervm TestECDH |
|
32 * @run main/othervm TestECDH sm policy |
31 */ |
33 */ |
32 |
34 |
33 import java.io.*; |
35 import java.security.KeyFactory; |
34 import java.util.*; |
36 import java.security.KeyPair; |
35 |
37 import java.security.KeyPairGenerator; |
36 import java.security.*; |
38 import java.security.PrivateKey; |
37 import java.security.spec.*; |
39 import java.security.Provider; |
|
40 import java.security.PublicKey; |
38 import java.security.interfaces.ECPublicKey; |
41 import java.security.interfaces.ECPublicKey; |
39 |
42 import java.security.spec.PKCS8EncodedKeySpec; |
40 import javax.crypto.*; |
43 import java.security.spec.X509EncodedKeySpec; |
|
44 import java.util.Arrays; |
|
45 import javax.crypto.KeyAgreement; |
41 |
46 |
42 public class TestECDH extends PKCS11Test { |
47 public class TestECDH extends PKCS11Test { |
43 |
48 |
44 private final static String pub192a = "30:49:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:03:32:00:04:bc:49:85:81:4d:d0:a4:ef:67:09:f1:9f:f5:ee:ff:4c:2f:0e:74:2c:a0:98:a8:69:79:9c:0c:3c:e8:99:f2:f2:3c:6f:48:bf:2a:ea:45:e9:76:be:1b:4a:45:0c:a2:99"; |
49 private final static String pub192a = "30:49:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:03:32:00:04:bc:49:85:81:4d:d0:a4:ef:67:09:f1:9f:f5:ee:ff:4c:2f:0e:74:2c:a0:98:a8:69:79:9c:0c:3c:e8:99:f2:f2:3c:6f:48:bf:2a:ea:45:e9:76:be:1b:4a:45:0c:a2:99"; |
45 private final static String priv192a = "30:39:02:01:00:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:04:1f:30:1d:02:01:01:04:18:50:9a:f1:fb:14:91:08:91:18:b9:46:7f:c3:ff:84:db:be:4c:70:89:41:5e:5a:f5"; |
50 private final static String priv192a = "30:39:02:01:00:30:13:06:07:2a:86:48:ce:3d:02:01:06:08:2a:86:48:ce:3d:03:01:01:04:1f:30:1d:02:01:01:04:18:50:9a:f1:fb:14:91:08:91:18:b9:46:7f:c3:ff:84:db:be:4c:70:89:41:5e:5a:f5"; |
53 private final static String pub163b = "30:40:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:03:2c:00:04:03:59:e7:69:a5:89:2f:28:ba:75:ac:bf:01:d5:ad:14:d8:f8:19:25:81:01:31:b3:e2:2d:f3:db:f1:d2:cd:fc:94:af:d2:1d:16:58:94:fe:d5:65"; |
58 private final static String pub163b = "30:40:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:03:2c:00:04:03:59:e7:69:a5:89:2f:28:ba:75:ac:bf:01:d5:ad:14:d8:f8:19:25:81:01:31:b3:e2:2d:f3:db:f1:d2:cd:fc:94:af:d2:1d:16:58:94:fe:d5:65"; |
54 private final static String priv163b = "30:33:02:01:00:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:04:1c:30:1a:02:01:01:04:15:02:4e:49:b1:8b:36:d8:71:22:81:06:8d:14:a9:4c:5c:7c:61:8b:e2:95"; |
59 private final static String priv163b = "30:33:02:01:00:30:10:06:07:2a:86:48:ce:3d:02:01:06:05:2b:81:04:00:0f:04:1c:30:1a:02:01:01:04:15:02:4e:49:b1:8b:36:d8:71:22:81:06:8d:14:a9:4c:5c:7c:61:8b:e2:95"; |
55 |
60 |
56 private final static String secret163 = "04:ae:71:c1:c6:4d:f4:34:4d:72:70:a4:64:65:7f:2d:88:2d:3f:50:be"; |
61 private final static String secret163 = "04:ae:71:c1:c6:4d:f4:34:4d:72:70:a4:64:65:7f:2d:88:2d:3f:50:be"; |
57 |
62 |
|
63 @Override |
58 public void main(Provider p) throws Exception { |
64 public void main(Provider p) throws Exception { |
59 if (p.getService("KeyAgreement", "ECDH") == null) { |
65 if (p.getService("KeyAgreement", "ECDH") == null) { |
60 System.out.println("Provider does not support ECDH, skipping"); |
66 System.out.println("Provider does not support ECDH, skipping"); |
61 return; |
67 return; |
62 } |
68 } |
87 test(p, pub163a, priv163a, pub163b, priv163b, secret163); |
93 test(p, pub163a, priv163a, pub163b, priv163b, secret163); |
88 |
94 |
89 System.out.println("OK"); |
95 System.out.println("OK"); |
90 } |
96 } |
91 |
97 |
92 private final static void test(Provider p, String pub1s, String priv1s, String pub2s, String priv2s, String secrets) throws Exception { |
98 private final static void test(Provider p, String pub1s, String priv1s, |
|
99 String pub2s, String priv2s, String secrets) throws Exception { |
93 KeyFactory kf = KeyFactory.getInstance("EC", p); |
100 KeyFactory kf = KeyFactory.getInstance("EC", p); |
94 PublicKey pub1 = kf.generatePublic(new X509EncodedKeySpec(parse(pub1s))); |
101 PublicKey pub1 = kf.generatePublic(new X509EncodedKeySpec(parse(pub1s))); |
95 System.out.println("Testing using parameters " + ((ECPublicKey)pub1).getParams() + "..."); |
102 System.out.println("Testing using parameters " |
|
103 + ((ECPublicKey)pub1).getParams() + "..."); |
96 |
104 |
97 PrivateKey priv1 = kf.generatePrivate(new PKCS8EncodedKeySpec(parse(priv1s))); |
105 PrivateKey priv1 = kf.generatePrivate(new PKCS8EncodedKeySpec(parse(priv1s))); |
98 PublicKey pub2 = kf.generatePublic(new X509EncodedKeySpec(parse(pub2s))); |
106 PublicKey pub2 = kf.generatePublic(new X509EncodedKeySpec(parse(pub2s))); |
99 PrivateKey priv2 = kf.generatePrivate(new PKCS8EncodedKeySpec(parse(priv2s))); |
107 PrivateKey priv2 = kf.generatePrivate(new PKCS8EncodedKeySpec(parse(priv2s))); |
100 byte[] secret = parse(secrets); |
108 byte[] secret = parse(secrets); |