1 /* 2 * Copyright (c) 2018, 2020, 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 8171277 27 * @summary Test XEC curve operations 28 * @modules jdk.crypto.ec/sun.security.ec 29 * @library /test/lib 30 * @build jdk.test.lib.Convert 31 * @run main TestXECOps 32 */ 33 34 import sun.security.ec.*; 35 36 import java.security.spec.NamedParameterSpec; 37 import java.util.*; 38 import jdk.test.lib.Convert; 39 40 // Test vectors are from RFC 7748 41 42 public class TestXECOps { 43 44 public static void main(String[] args) { 45 TestXECOps m = new TestXECOps(); 46 47 m.runTest("X25519", 48 "a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4", 49 "e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c", 50 "c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552"); 51 52 m.runTest("X25519", 53 "4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d", 54 "e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493", 55 "95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957"); 56 57 m.runDiffieHellmanTest("X25519", 58 "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a", 59 "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb", 60 "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"); 61 62 63 m.runTest("X448", 64 "3d262fddf9ec8e88495266fea19a34d28882acef045104d0d1aae121700a77" + 65 "9c984c24f8cdd78fbff44943eba368f54b29259a4f1c600ad3", 66 "06fce640fa3487bfda5f6cf2d5263f8aad88334cbd07437f020f08f9814dc0" + 67 "31ddbdc38c19c6da2583fa5429db94ada18aa7a7fb4ef8a086", 68 "ce3e4ff95a60dc6697da1db1d85e6afbdf79b50a2412d7546d5f239fe14fba" + 69 "adeb445fc66a01b0779d98223961111e21766282f73dd96b6f"); 70 71 m.runTest("X448", 72 "203d494428b8399352665ddca42f9de8fef600908e0d461cb021f8c538345d" + 73 "d77c3e4806e25f46d3315c44e0a5b4371282dd2c8d5be3095f", 74 "0fbcc2f993cd56d3305b0b7d9e55d4c1a8fb5dbb52f8e9a1e9b6201b165d01" + 75 "5894e56c4d3570bee52fe205e28a78b91cdfbde71ce8d157db", 76 "884a02576239ff7a2f2f63b2db6a9ff37047ac13568e1e30fe63c4a7ad1b3e" + 77 "e3a5700df34321d62077e63633c575c1c954514e99da7c179d"); 78 79 m.runDiffieHellmanTest("X448", 80 "9a8f4925d1519f5775cf46b04b5800d4ee9ee8bae8bc5565d498c28dd9c9ba" + 81 "f574a9419744897391006382a6f127ab1d9ac2d8c0a598726b", 82 "1c306a7ac2a0e2e0990b294470cba339e6453772b075811d8fad0d1d6927c1" + 83 "20bb5ee8972b0d3e21374c9c921b09d1b0366f10b65173992d", 84 "07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b" + 85 "56fd2464c335543936521c24403085d59a449a5037514a879d"); 86 } 87 88 private void runDiffieHellmanTest(String opName, String a_str, 89 String b_str, String result_str) { 90 91 NamedParameterSpec paramSpec = new NamedParameterSpec(opName); 92 XECParameters settings = 93 XECParameters.get(RuntimeException::new, paramSpec); 94 XECOperations ops = new XECOperations(settings); 95 96 byte[] basePoint = Convert.byteToByteArray(settings.getBasePoint(), 97 settings.getBytes()); 98 byte[] a = Convert.hexStringToByteArray(a_str); 99 byte[] b = Convert.hexStringToByteArray(b_str); 100 byte[] expectedResult = Convert.hexStringToByteArray(result_str); 101 102 byte[] a_copy = Arrays.copyOf(a, a.length); 103 byte[] b_copy = Arrays.copyOf(b, b.length); 104 byte[] basePoint_copy = Arrays.copyOf(basePoint, basePoint.length); 105 106 byte[] resultA = ops.encodedPointMultiply(b, 107 ops.encodedPointMultiply(a, basePoint)); 108 byte[] resultB = ops.encodedPointMultiply(a_copy, 109 ops.encodedPointMultiply(b_copy, basePoint_copy)); 110 if (!Arrays.equals(resultA, expectedResult)) { 111 throw new RuntimeException("fail"); 112 } 113 if (!Arrays.equals(resultB, expectedResult)) { 114 throw new RuntimeException("fail"); 115 } 116 } 117 118 private void runTest(String opName, String k_in_str, 119 String u_in_str, String u_out_str) { 120 121 byte[] k_in = Convert.hexStringToByteArray(k_in_str); 122 byte[] u_in = Convert.hexStringToByteArray(u_in_str); 123 byte[] u_out_expected = Convert.hexStringToByteArray(u_out_str); 124 125 NamedParameterSpec paramSpec = new NamedParameterSpec(opName); 126 XECParameters settings = 127 XECParameters.get(RuntimeException::new, paramSpec); 128 XECOperations ops = new XECOperations(settings); 129 byte[] u_out = ops.encodedPointMultiply(k_in, u_in); 130 131 if (!Arrays.equals(u_out, u_out_expected)) { 132 throw new RuntimeException("fail"); 133 } 134 } 135 } 136